errors: add warning implementation + fix a few issues
This commit is contained in:
parent
29d6d1a8de
commit
3fa039110b
1 changed files with 26 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue