mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Simplify error & warning handling
This commit is contained in:
parent
122b049ce9
commit
b6ee649458
8 changed files with 38 additions and 21 deletions
|
@ -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:
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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:
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -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."""
|
||||||
|
|
||||||
ctx = ParseContext(tokens)
|
try:
|
||||||
AnyOf(UI).parse(ctx)
|
ctx = ParseContext(tokens)
|
||||||
|
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 = [*ctx.errors, *ast_node.errors]
|
||||||
errors = MultipleErrors(ctx.errors) if len(ctx.errors) else None
|
warnings = [*ctx.warnings, *ast_node.warnings]
|
||||||
warnings = ctx.warnings
|
|
||||||
|
|
||||||
return (ast_node, errors, 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]), [])
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
using Gtk 4.0;
|
using Gtk 4.0;
|
||||||
|
|
||||||
menu {
|
menu menu {
|
||||||
not-allowed: true;
|
not-allowed: true;
|
||||||
}
|
}
|
|
@ -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,8 +92,9 @@ class TestSamples(unittest.TestCase):
|
||||||
tokens = tokenizer.tokenize(blueprint)
|
tokens = tokenizer.tokenize(blueprint)
|
||||||
ast, errors, warnings = parser.parse(tokens)
|
ast, errors, warnings = parser.parse(tokens)
|
||||||
|
|
||||||
self.assert_docs_dont_crash(blueprint, ast)
|
if ast is not None:
|
||||||
self.assert_completions_dont_crash(blueprint, ast, tokens)
|
self.assert_docs_dont_crash(blueprint, ast)
|
||||||
|
self.assert_completions_dont_crash(blueprint, ast, tokens)
|
||||||
|
|
||||||
if errors:
|
if errors:
|
||||||
raise errors
|
raise errors
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue