diff --git a/blueprintcompiler/ast.py b/blueprintcompiler/ast.py index bb80dfd..934ca11 100644 --- a/blueprintcompiler/ast.py +++ b/blueprintcompiler/ast.py @@ -143,7 +143,7 @@ class Object(AstNode): @validate("class_name") def gir_class_exists(self): - if not self.tokens["ignore_gir"] and self.gir_ns is not None: + if self.tokens["class_name"] and not self.tokens["ignore_gir"] and self.gir_ns is not None: self.root.gir.validate_class(self.tokens["class_name"], self.tokens["namespace"]) @property @@ -153,7 +153,7 @@ class Object(AstNode): @property def gir_class(self): - if not self.tokens["ignore_gir"]: + if self.tokens["class_name"] and not self.tokens["ignore_gir"]: return self.root.gir.get_class(self.tokens["class_name"], self.tokens["namespace"]) @@ -181,10 +181,13 @@ class Object(AstNode): class Template(Object): def emit_xml(self, xml: XmlEmitter): - xml.start_tag("template", **{ - "class": self.tokens["name"], - "parent": self.gir_class.glib_type_name if self.gir_class else self.tokens["class_name"], - }) + if self.gir_class: + parent = self.gir_class.glib_type_name + elif self.tokens["class_name"]: + parent = self.tokens["class_name"] + else: + parent = None + xml.start_tag("template", **{"class": self.tokens["name"]}, parent=parent) for child in self.children: child.emit_xml(xml) xml.end_tag() diff --git a/blueprintcompiler/parser.py b/blueprintcompiler/parser.py index a802df0..480b605 100644 --- a/blueprintcompiler/parser.py +++ b/blueprintcompiler/parser.py @@ -140,8 +140,12 @@ def parse(tokens) -> T.Tuple[ast.UI, T.Optional[MultipleErrors]]: Sequence( Keyword("template"), UseIdent("name").expected("template class name"), - Op(":").expected("`:`"), - class_name.expected("parent class"), + Optional( + Sequence( + Op(":"), + class_name.expected("parent class"), + ) + ), object_content.expected("block"), ) ) diff --git a/tests/samples/template_no_parent.blp b/tests/samples/template_no_parent.blp new file mode 100644 index 0000000..2e3defc --- /dev/null +++ b/tests/samples/template_no_parent.blp @@ -0,0 +1,3 @@ +using Gtk 4.0; + +template GtkListItem {} diff --git a/tests/samples/template_no_parent.ui b/tests/samples/template_no_parent.ui new file mode 100644 index 0000000..ea2263e --- /dev/null +++ b/tests/samples/template_no_parent.ui @@ -0,0 +1,5 @@ + + + + + diff --git a/tests/test_samples.py b/tests/test_samples.py index 94d0d99..e5bf8b9 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -131,6 +131,7 @@ class TestSamples(unittest.TestCase): self.assert_sample("strings") self.assert_sample("style") self.assert_sample("template") + self.assert_sample("template_no_parent") self.assert_sample("translated") self.assert_sample("uint") self.assert_sample("using")