main+errors: move pretty printing errors into a method

This commit is contained in:
Antoine Viallon 2024-04-11 20:20:00 +02:00
parent d2823f5abf
commit 92e02c8973
Signed by: aviallon
GPG key ID: 186FC35EDEB25716
2 changed files with 10 additions and 6 deletions

View file

@ -7,8 +7,8 @@ import typing
from . import semantic, ir, optimizations from . import semantic, ir, optimizations
from .errors import CompilationError, CompilationWarning from .errors import CompilationError, CompilationWarning
from .interpreter import virtual_machine
from .lexer import Lexer, Tokens from .lexer import Lexer, Tokens
from .logger import rootLogger, LogLevel
from .parser import Parser from .parser import Parser
@ -81,11 +81,7 @@ def main():
except CompilationError as e: except CompilationError as e:
e.location.source = tokens.data e.location.source = tokens.data
print(f"{e}\n{e.location.show_in_source()}", flush=True) e.pretty_print()
if e.__cause__ is not None:
if rootLogger.level <= LogLevel.Debug:
raise e.__cause__
print(f"Caused by:\n{e.__cause__.__class__.__name__}: {e.__cause__}", flush=True)
finally: finally:
CompilationWarning.show_warnings(data, file=sys.stdout) CompilationWarning.show_warnings(data, file=sys.stdout)

View file

@ -8,6 +8,7 @@ from typing import TextIO
from termcolor import termcolor from termcolor import termcolor
from . import source, lexer from . import source, lexer
from .logger import rootLogger, LogLevel
class LevelType: class LevelType:
@ -40,6 +41,13 @@ class CompilationError(Exception):
super().__init__(f"{str(location)}: {Levels.Error.value.show_with_suffix(':')} {message}") super().__init__(f"{str(location)}: {Levels.Error.value.show_with_suffix(':')} {message}")
self.location = location self.location = location
def pretty_print(self):
print(f"{self}\n{self.location.show_in_source()}", flush=True)
if self.__cause__ is not None:
if rootLogger.level <= LogLevel.Debug:
raise self.__cause__
print(f"Caused by:\n{self.__cause__.__class__.__name__}: {self.__cause__}", flush=True)
class CompilationWarning(Warning): class CompilationWarning(Warning):
_pending_warnings: list[CompilationWarning] = [] _pending_warnings: list[CompilationWarning] = []