parser: Tweak parsing during error conditions

When an explicit parsing error is encountered and a CompileError raised,
apply the changes to the context state. This way, the rule that catches
the exception (e.g. Statement or Until) knows where the error occurred.

Also, changed "Expected" errors to be reported at the end of the
previous non-whitespace token.
This commit is contained in:
James Westman 2025-05-03 10:10:06 -05:00
parent e5d6910626
commit 8f3ae9a626
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
10 changed files with 69 additions and 37 deletions

View file

@ -48,7 +48,7 @@ class ScopeCtx:
return self.node
@cached_property
def objects(self) -> T.Dict[str, Object]:
def objects(self) -> T.Dict[str, AstNode]:
return {
obj.tokens["id"]: obj
for obj in self._iter_recursive(self.node)
@ -58,7 +58,7 @@ class ScopeCtx:
def validate_unique_ids(self) -> None:
from .gtk_list_item_factory import ExtListItemFactory
passed = {}
passed: T.Dict[str, AstNode] = {}
for obj in self._iter_recursive(self.node):
if obj.tokens["id"] is None:
continue
@ -71,10 +71,16 @@ class ScopeCtx:
raise CompileError(
f"Duplicate object ID '{obj.tokens['id']}'",
token.range,
references=[
ErrorReference(
passed[obj.tokens["id"]].group.tokens["id"].range,
"previous declaration was here",
)
],
)
passed[obj.tokens["id"]] = obj
def _iter_recursive(self, node: AstNode):
def _iter_recursive(self, node: AstNode) -> T.Generator[AstNode, T.Any, None]:
yield node
for child in node.children:
if child.context[ScopeCtx] is self: