errors: init dedicated error file

This commit is contained in:
Antoine Viallon 2023-05-09 01:51:52 +02:00
parent 4bc481ed54
commit e9324f4f71
Signed by: aviallon
GPG key ID: D126B13AB555E16F
4 changed files with 28 additions and 18 deletions

View file

@ -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
View 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

View file

@ -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

View file

@ -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