mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
cleanup: Format using black
This commit is contained in:
parent
4b42016837
commit
af03c2ac0f
36 changed files with 928 additions and 616 deletions
|
@ -23,21 +23,24 @@ import sys, traceback
|
|||
from . import utils
|
||||
from .utils import Colors
|
||||
|
||||
|
||||
class PrintableError(Exception):
|
||||
""" Parent class for errors that can be pretty-printed for the user, e.g.
|
||||
compilation warnings and errors. """
|
||||
"""Parent class for errors that can be pretty-printed for the user, e.g.
|
||||
compilation warnings and errors."""
|
||||
|
||||
def pretty_print(self, filename, code):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class CompileError(PrintableError):
|
||||
""" A PrintableError with a start/end position and optional hints """
|
||||
"""A PrintableError with a start/end position and optional hints"""
|
||||
|
||||
category = "error"
|
||||
color = Colors.RED
|
||||
|
||||
def __init__(self, message, start=None, end=None, did_you_mean=None, hints=None, actions=None):
|
||||
def __init__(
|
||||
self, message, start=None, end=None, did_you_mean=None, hints=None, actions=None
|
||||
):
|
||||
super().__init__(message)
|
||||
|
||||
self.message = message
|
||||
|
@ -53,7 +56,6 @@ class CompileError(PrintableError):
|
|||
self.hints.append(hint)
|
||||
return self
|
||||
|
||||
|
||||
def _did_you_mean(self, word: str, options: T.List[str]):
|
||||
if word.replace("_", "-") in options:
|
||||
self.hint(f"use '-', not '_': `{word.replace('_', '-')}`")
|
||||
|
@ -77,9 +79,11 @@ class CompileError(PrintableError):
|
|||
# Display 1-based line numbers
|
||||
line_num += 1
|
||||
|
||||
stream.write(f"""{self.color}{Colors.BOLD}{self.category}: {self.message}{Colors.CLEAR}
|
||||
stream.write(
|
||||
f"""{self.color}{Colors.BOLD}{self.category}: {self.message}{Colors.CLEAR}
|
||||
at {filename} line {line_num} column {col_num}:
|
||||
{Colors.FAINT}{line_num :>4} |{Colors.CLEAR}{line.rstrip()}\n {Colors.FAINT}|{" "*(col_num-1)}^{Colors.CLEAR}\n""")
|
||||
{Colors.FAINT}{line_num :>4} |{Colors.CLEAR}{line.rstrip()}\n {Colors.FAINT}|{" "*(col_num-1)}^{Colors.CLEAR}\n"""
|
||||
)
|
||||
|
||||
for hint in self.hints:
|
||||
stream.write(f"{Colors.FAINT}hint: {hint}{Colors.CLEAR}\n")
|
||||
|
@ -103,9 +107,9 @@ class CodeAction:
|
|||
|
||||
|
||||
class MultipleErrors(PrintableError):
|
||||
""" If multiple errors occur during compilation, they can be collected into
|
||||
"""If multiple errors occur during compilation, they can be collected into
|
||||
a list and re-thrown using the MultipleErrors exception. It will
|
||||
pretty-print all of the errors and a count of how many errors there are. """
|
||||
pretty-print all of the errors and a count of how many errors there are."""
|
||||
|
||||
def __init__(self, errors: T.List[CompileError]):
|
||||
super().__init__()
|
||||
|
@ -119,22 +123,23 @@ class MultipleErrors(PrintableError):
|
|||
|
||||
|
||||
class CompilerBugError(Exception):
|
||||
""" Emitted on assertion errors """
|
||||
"""Emitted on assertion errors"""
|
||||
|
||||
|
||||
def assert_true(truth: bool, message:str=None):
|
||||
def assert_true(truth: bool, message: str = None):
|
||||
if not truth:
|
||||
raise CompilerBugError(message)
|
||||
|
||||
|
||||
def report_bug(): # pragma: no cover
|
||||
""" Report an error and ask people to report it. """
|
||||
def report_bug(): # pragma: no cover
|
||||
"""Report an error and ask people to report it."""
|
||||
|
||||
print(traceback.format_exc())
|
||||
print(f"Arguments: {sys.argv}\n")
|
||||
print(f"""{Colors.BOLD}{Colors.RED}***** COMPILER BUG *****
|
||||
print(
|
||||
f"""{Colors.BOLD}{Colors.RED}***** COMPILER BUG *****
|
||||
The blueprint-compiler program has crashed. Please report the above stacktrace,
|
||||
along with the input file(s) if possible, on GitLab:
|
||||
{Colors.BOLD}{Colors.BLUE}{Colors.UNDERLINE}https://gitlab.gnome.org/jwestman/blueprint-compiler/-/issues/new?issue
|
||||
{Colors.CLEAR}""")
|
||||
|
||||
{Colors.CLEAR}"""
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue