From 3fa039110bf4a30a4acf68e40369ab80d3d97ab9 Mon Sep 17 00:00:00 2001 From: Antoine Viallon Date: Fri, 5 Jan 2024 22:59:50 +0100 Subject: [PATCH] errors: add warning implementation + fix a few issues --- compiler/errors.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/compiler/errors.py b/compiler/errors.py index f9b9d84..64648b4 100644 --- a/compiler/errors.py +++ b/compiler/errors.py @@ -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: