main+errors: move pretty printing errors into a method
This commit is contained in:
parent
d2823f5abf
commit
92e02c8973
2 changed files with 10 additions and 6 deletions
|
|
@ -7,8 +7,8 @@ import typing
|
|||
|
||||
from . import semantic, ir, optimizations
|
||||
from .errors import CompilationError, CompilationWarning
|
||||
from .interpreter import virtual_machine
|
||||
from .lexer import Lexer, Tokens
|
||||
from .logger import rootLogger, LogLevel
|
||||
from .parser import Parser
|
||||
|
||||
|
||||
|
|
@ -81,11 +81,7 @@ def main():
|
|||
|
||||
except CompilationError as e:
|
||||
e.location.source = tokens.data
|
||||
print(f"{e}\n{e.location.show_in_source()}", flush=True)
|
||||
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)
|
||||
e.pretty_print()
|
||||
|
||||
finally:
|
||||
CompilationWarning.show_warnings(data, file=sys.stdout)
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from typing import TextIO
|
|||
from termcolor import termcolor
|
||||
|
||||
from . import source, lexer
|
||||
from .logger import rootLogger, LogLevel
|
||||
|
||||
|
||||
class LevelType:
|
||||
|
|
@ -40,6 +41,13 @@ class CompilationError(Exception):
|
|||
super().__init__(f"{str(location)}: {Levels.Error.value.show_with_suffix(':')} {message}")
|
||||
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):
|
||||
_pending_warnings: list[CompilationWarning] = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue