mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add ScopeCtx instead of root.objects_by_id
This allows us to introduce new scopes, such as in GtkBuilderListItemFactory templates.
This commit is contained in:
parent
ff5fff7f4b
commit
ec844b10ca
8 changed files with 65 additions and 40 deletions
|
@ -19,9 +19,44 @@
|
|||
|
||||
import typing as T
|
||||
from dataclasses import dataclass
|
||||
from functools import cached_property
|
||||
|
||||
from .common import *
|
||||
from .gobject_object import Object
|
||||
|
||||
|
||||
@dataclass
|
||||
class ValueTypeCtx:
|
||||
value_type: T.Optional[GirType]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ScopeCtx:
|
||||
node: AstNode
|
||||
|
||||
@cached_property
|
||||
def objects(self) -> T.Dict[str, Object]:
|
||||
return {
|
||||
obj.tokens["id"]: obj
|
||||
for obj in self._iter_recursive(self.node)
|
||||
if obj.tokens["id"] is not None
|
||||
}
|
||||
|
||||
def validate_unique_ids(self) -> None:
|
||||
passed = {}
|
||||
for obj in self._iter_recursive(self.node):
|
||||
if obj.tokens["id"] is None:
|
||||
continue
|
||||
|
||||
if obj.tokens["id"] in passed:
|
||||
token = obj.group.tokens["id"]
|
||||
raise CompileError(
|
||||
f"Duplicate object ID '{obj.tokens['id']}'", token.start, token.end
|
||||
)
|
||||
passed[obj.tokens["id"]] = obj
|
||||
|
||||
def _iter_recursive(self, node: AstNode):
|
||||
yield node
|
||||
for child in node.children:
|
||||
if child.context[ScopeCtx] is self:
|
||||
yield from self._iter_recursive(child)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue