errors: init dedicated error file
This commit is contained in:
parent
4bc481ed54
commit
e9324f4f71
4 changed files with 28 additions and 18 deletions
|
|
@ -4,8 +4,9 @@ import sys
|
|||
from pprint import pprint
|
||||
|
||||
from . import semantic
|
||||
from .errors import CompilationError
|
||||
from .logger import rootLogger, LogLevel
|
||||
from .parser import Parser, ParsingError
|
||||
from .parser import Parser
|
||||
from .tokenizer import Tokenizer, Tokens
|
||||
|
||||
|
||||
|
|
@ -42,7 +43,7 @@ def main():
|
|||
messages += [f"{repr(ir_item)}\n"]
|
||||
|
||||
print("\n".join(messages))
|
||||
except ParsingError as e:
|
||||
except CompilationError as e:
|
||||
e.location.source = data
|
||||
print(f"{e}\n{e.location.show_in_source()}", file=sys.stderr)
|
||||
if e.__cause__ is not None:
|
||||
|
|
|
|||
21
compiler/errors.py
Normal file
21
compiler/errors.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from . import source, tokenizer
|
||||
|
||||
|
||||
class CompilationError(Exception):
|
||||
def __init__(self, location: source.SourceLocation, message: str = "Unknown error"):
|
||||
super().__init__(f"{message} at {str(location)}")
|
||||
self.location = location
|
||||
|
||||
|
||||
class UnexpectedTokenError(CompilationError):
|
||||
def __init__(self, got: tokenizer.Token, wanted: tokenizer.Tokens | str):
|
||||
message = wanted
|
||||
if type(wanted) == tokenizer.Tokens:
|
||||
message = str(wanted)
|
||||
super().__init__(got.loc, f"Unexpected token '{got}', wanted {message}")
|
||||
|
||||
|
||||
class SemanticAnalysisError(CompilationError):
|
||||
pass
|
||||
|
|
@ -7,6 +7,7 @@ from typing import Any, Iterable
|
|||
from beartype import beartype
|
||||
|
||||
from . import ir, semantic
|
||||
from .errors import SemanticAnalysisError
|
||||
from .logger import Logger
|
||||
from .source import SourceLocation
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
|||
|
||||
from beartype.typing import List, Dict, Callable
|
||||
|
||||
from .errors import CompilationError, UnexpectedTokenError
|
||||
from .logger import Logger
|
||||
from .nodes import Float, Sum, Value, Product, Node, Division, Sub, Integer, Expression
|
||||
from .source import SourceLocation
|
||||
|
|
@ -10,20 +11,6 @@ from .tokenizer import Tokens, Token
|
|||
logger = Logger(__name__)
|
||||
|
||||
|
||||
class ParsingError(Exception):
|
||||
def __init__(self, location: SourceLocation, message: str = "Unknown error"):
|
||||
super().__init__(f"{message} at {str(location)}")
|
||||
self.location = location
|
||||
|
||||
|
||||
class UnexpectedTokenError(ParsingError):
|
||||
def __init__(self, got: Token, wanted: Tokens | str):
|
||||
message = wanted
|
||||
if type(wanted) == Tokens:
|
||||
message = str(wanted)
|
||||
super().__init__(got.loc, f"Unexpected token '{got}', wanted {message}")
|
||||
|
||||
|
||||
class Parser:
|
||||
def __init__(self, tokens: List[Token]):
|
||||
self.tokens = tokens
|
||||
|
|
@ -118,10 +105,10 @@ class Parser:
|
|||
def parse(self) -> Node:
|
||||
try:
|
||||
return self.root()
|
||||
except ParsingError:
|
||||
except CompilationError:
|
||||
raise
|
||||
except Exception as e:
|
||||
tok = self._last_accepted_token
|
||||
if tok is None:
|
||||
tok = self.token
|
||||
raise ParsingError(tok.loc) from e
|
||||
raise CompilationError(tok.loc) from e
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue