mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add errors for duplicate properties and blocks
This commit is contained in:
parent
f18c8b7a2d
commit
824476bda1
12 changed files with 122 additions and 0 deletions
|
@ -131,6 +131,16 @@ class AstNode:
|
|||
yield from child.iterate_children_recursive()
|
||||
|
||||
|
||||
def validate_unique_in_parent(self, error, check=None):
|
||||
for child in self.parent.children:
|
||||
if child is self:
|
||||
break
|
||||
|
||||
if type(child) is type(self):
|
||||
if check is None or check(child):
|
||||
raise CompileError(error)
|
||||
|
||||
|
||||
def validate(token_name=None, end_token_name=None, skip_incomplete=False):
|
||||
""" Decorator for functions that validate an AST node. Exceptions raised
|
||||
during validation are marked with range information from the tokens. """
|
||||
|
|
|
@ -112,6 +112,14 @@ class Property(AstNode):
|
|||
)
|
||||
|
||||
|
||||
@validate("name")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent(
|
||||
f"Duplicate property '{self.tokens['name']}'",
|
||||
check=lambda child: child.tokens["name"] == self.tokens["name"]
|
||||
)
|
||||
|
||||
|
||||
@docs("name")
|
||||
def property_docs(self):
|
||||
if self.gir_property is not None:
|
||||
|
|
|
@ -139,6 +139,13 @@ class A11yProperty(BaseTypedAttribute):
|
|||
did_you_mean=(self.tokens["name"], types.keys()),
|
||||
)
|
||||
|
||||
@validate("name")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent(
|
||||
f"Duplicate accessibility attribute '{self.tokens['name']}'",
|
||||
check=lambda child: child.tokens["name"] == self.tokens["name"],
|
||||
)
|
||||
|
||||
@docs("name")
|
||||
def prop_docs(self):
|
||||
if self.tokens["name"] in get_types(self.root.gir):
|
||||
|
@ -156,6 +163,9 @@ class A11y(AstNode):
|
|||
def container_is_widget(self):
|
||||
validate_parent_type(self, "Gtk", "Widget", "accessibility properties")
|
||||
|
||||
@validate("accessibility")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate accessibility block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("accessibility")
|
||||
|
|
|
@ -56,6 +56,9 @@ class Items(AstNode):
|
|||
def container_is_combo_box_text(self):
|
||||
validate_parent_type(self, "Gtk", "ComboBoxText", "combo box items")
|
||||
|
||||
@validate("items")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate items block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("items")
|
||||
|
|
|
@ -27,6 +27,18 @@ class Filters(AstNode):
|
|||
def container_is_file_filter(self):
|
||||
validate_parent_type(self, "Gtk", "FileFilter", "file filter properties")
|
||||
|
||||
@validate()
|
||||
def unique_in_parent(self):
|
||||
# The token argument to validate() needs to be calculated based on
|
||||
# the instance, hence wrapping it like this.
|
||||
@validate(self.tokens["tag_name"])
|
||||
def wrapped_validator(self):
|
||||
self.validate_unique_in_parent(
|
||||
f"Duplicate {self.tokens['tag_name']} block",
|
||||
check=lambda child: child.tokens["tag_name"] == self.tokens["tag_name"],
|
||||
)
|
||||
wrapped_validator(self)
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag(self.tokens["tag_name"])
|
||||
for child in self.children:
|
||||
|
|
|
@ -31,6 +31,13 @@ class LayoutProperty(BaseAttribute):
|
|||
# there isn't really a way to validate these
|
||||
return None
|
||||
|
||||
@validate("name")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent(
|
||||
f"Duplicate layout property '{self.name}'",
|
||||
check=lambda child: child.name == self.name,
|
||||
)
|
||||
|
||||
|
||||
layout_prop = Group(
|
||||
LayoutProperty,
|
||||
|
@ -53,6 +60,9 @@ class Layout(AstNode):
|
|||
def container_is_widget(self):
|
||||
validate_parent_type(self, "Gtk", "Widget", "layout properties")
|
||||
|
||||
@validate("layout")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate layout block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("layout")
|
||||
|
|
|
@ -55,6 +55,10 @@ class Widgets(AstNode):
|
|||
def container_is_size_group(self):
|
||||
validate_parent_type(self, "Gtk", "SizeGroup", "size group properties")
|
||||
|
||||
@validate("widgets")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate widgets block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("widgets")
|
||||
for child in self.children:
|
||||
|
|
|
@ -51,6 +51,9 @@ class Strings(AstNode):
|
|||
def container_is_string_list(self):
|
||||
validate_parent_type(self, "Gtk", "StringList", "StringList items")
|
||||
|
||||
@validate("strings")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate strings block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("items")
|
||||
|
|
|
@ -41,6 +41,10 @@ class Styles(AstNode):
|
|||
def container_is_widget(self):
|
||||
validate_parent_type(self, "Gtk", "Widget", "style classes")
|
||||
|
||||
@validate("styles")
|
||||
def unique_in_parent(self):
|
||||
self.validate_unique_in_parent("Duplicate styles block")
|
||||
|
||||
def emit_xml(self, xml: XmlEmitter):
|
||||
xml.start_tag("style")
|
||||
for child in self.children:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue