Simplify error & warning handling

This commit is contained in:
James Westman 2022-12-25 17:10:21 -06:00
parent 122b049ce9
commit b6ee649458
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
8 changed files with 38 additions and 21 deletions

View file

@ -79,7 +79,19 @@ class AstNode:
@cached_property
def errors(self):
return list(self._get_errors())
return list(
error
for error in self._get_errors()
if not isinstance(error, CompileWarning)
)
@cached_property
def warnings(self):
return list(
warning
for warning in self._get_errors()
if isinstance(warning, CompileWarning)
)
def _get_errors(self):
for validator in self.validators:

View file

@ -24,7 +24,7 @@ import os
from . import decompiler, tokenizer, parser
from .outputs.xml import XmlOutput
from .errors import MultipleErrors, PrintableError
from .errors import MultipleErrors, PrintableError, CompilerBugError
from .utils import Colors
@ -57,8 +57,8 @@ def decompile_file(in_file, out_file) -> T.Union[str, CouldNotPort]:
if errors:
raise errors
if len(ast.errors):
raise MultipleErrors(ast.errors)
if not ast:
raise CompilerBugError()
output = XmlOutput()
output.emit(ast)

View file

@ -75,7 +75,6 @@ class OpenFile:
self.diagnostics += warnings
if errors is not None:
self.diagnostics += errors.errors
self.diagnostics += self.ast.errors
except MultipleErrors as e:
self.diagnostics += e.errors
except CompileError as e:

View file

@ -21,7 +21,7 @@
import typing as T
import argparse, json, os, sys
from .errors import PrintableError, report_bug, MultipleErrors
from .errors import PrintableError, report_bug, MultipleErrors, CompilerBugError
from .lsp import LanguageServer
from . import parser, tokenizer, decompiler, interactive_port
from .utils import Colors
@ -149,8 +149,8 @@ class BlueprintApp:
if errors:
raise errors
if len(ast.errors):
raise MultipleErrors(ast.errors)
if ast is None:
raise CompilerBugError()
formatter = XmlOutput()

View file

@ -24,14 +24,21 @@ from .tokenizer import TokenType
from .language import OBJECT_CONTENT_HOOKS, VALUE_HOOKS, Template, UI
def parse(tokens) -> T.Tuple[UI, T.Optional[MultipleErrors], T.List[PrintableError]]:
def parse(
tokens: T.List[Token],
) -> T.Tuple[T.Optional[UI], T.Optional[MultipleErrors], T.List[PrintableError]]:
"""Parses a list of tokens into an abstract syntax tree."""
try:
ctx = ParseContext(tokens)
AnyOf(UI).parse(ctx)
ast_node = ctx.last_group.to_ast() if ctx.last_group else None
errors = MultipleErrors(ctx.errors) if len(ctx.errors) else None
warnings = ctx.warnings
return (ast_node, errors, warnings)
errors = [*ctx.errors, *ast_node.errors]
warnings = [*ctx.warnings, *ast_node.warnings]
return (ast_node, MultipleErrors(errors) if len(errors) else None, warnings)
except MultipleErrors as e:
return (None, e, [])
except CompileError as e:
return (None, MultipleErrors([e]), [])

View file

@ -26,7 +26,7 @@ def fuzz(buf):
ast, errors, warnings = parser.parse(tokens)
xml = XmlOutput()
if errors is None and len(ast.errors) == 0:
if errors is None and ast is not None:
xml.emit(ast)
except CompilerBugError as e:
raise e

View file

@ -1,5 +1,5 @@
using Gtk 4.0;
menu {
menu menu {
not-allowed: true;
}

View file

@ -57,8 +57,6 @@ class TestSamples(unittest.TestCase):
if errors:
raise errors
if len(ast.errors):
raise MultipleErrors(ast.errors)
if len(warnings):
raise MultipleErrors(warnings)
@ -94,6 +92,7 @@ class TestSamples(unittest.TestCase):
tokens = tokenizer.tokenize(blueprint)
ast, errors, warnings = parser.parse(tokens)
if ast is not None:
self.assert_docs_dont_crash(blueprint, ast)
self.assert_completions_dont_crash(blueprint, ast, tokens)