errors: add warning implementation + fix a few issues

This commit is contained in:
Antoine Viallon 2024-01-05 22:59:50 +01:00
parent 29d6d1a8de
commit 3fa039110b
Signed by: aviallon
GPG key ID: 186FC35EDEB25716

View file

@ -1,20 +1,38 @@
from __future__ import annotations
import inspect
import sys
from . import source, lexer
class CompilationError(Exception):
def __init__(self, location: source.SourceLocation, message: str = "Unknown error"):
super().__init__(f"{str(location)}: {message} ")
super().__init__(f"{str(location)}: \033[1merror:\033[0m {message}")
self.location = location
class CompilationWarning(Warning):
_pending_warnings: list[CompilationWarning] = []
def __init__(self, location: source.SourceLocation, message: str = "Unknown warning"):
super().__init__(f"{str(location)}: \033[1mwarning:\033[0m {message}")
self.location = location
@classmethod
def show_warnings(cls, _source: str):
for warning in CompilationWarning._pending_warnings:
warning.location.source = _source
print(f"{warning}\n{warning.location.show_in_source()}", file=sys.stderr)
def raise_warning(self):
CompilationWarning._pending_warnings += [self]
class UnexpectedTokenError(CompilationError):
def __init__(self, got: lexer.Token, wanted: lexer.Tokens | str):
message = wanted
if type(wanted) == lexer.Tokens:
if isinstance(wanted, lexer.Tokens):
message = str(wanted)
super().__init__(got.loc, f"Unexpected token '{got}', wanted {message}")
@ -23,6 +41,12 @@ class SemanticAnalysisError(CompilationError):
pass
class CodegenError(Exception):
def __init__(self, ir_location: source.IRLocation, message: str = "Unknown codegen error"):
super().__init__(f"Codegen error: {message}\n{str(ir_location)}")
pass
class OverrideMandatoryError(NotImplementedError):
def __init__(self, klass: object = None, function_name: str = None):
if klass is None or function_name is None: