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 @cached_property
def errors(self): 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): def _get_errors(self):
for validator in self.validators: for validator in self.validators:

View file

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

View file

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

View file

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

View file

@ -24,14 +24,21 @@ from .tokenizer import TokenType
from .language import OBJECT_CONTENT_HOOKS, VALUE_HOOKS, Template, UI 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.""" """Parses a list of tokens into an abstract syntax tree."""
try:
ctx = ParseContext(tokens) ctx = ParseContext(tokens)
AnyOf(UI).parse(ctx) AnyOf(UI).parse(ctx)
ast_node = ctx.last_group.to_ast() if ctx.last_group else None 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) ast, errors, warnings = parser.parse(tokens)
xml = XmlOutput() xml = XmlOutput()
if errors is None and len(ast.errors) == 0: if errors is None and ast is not None:
xml.emit(ast) xml.emit(ast)
except CompilerBugError as e: except CompilerBugError as e:
raise e raise e

View file

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

View file

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