From 693826795203863eba3973fedf5d23d5505438f8 Mon Sep 17 00:00:00 2001 From: James Westman Date: Mon, 9 Jan 2023 21:55:14 -0600 Subject: [PATCH] Add properties to AST types I want to have a cleaner API that relies less on the specifics of the grammar and parser. --- blueprintcompiler/language/gtk_menu.py | 4 ++++ blueprintcompiler/language/ui.py | 20 +++++++++++++++++++- blueprintcompiler/language/values.py | 4 ++++ blueprintcompiler/outputs/xml/__init__.py | 16 +++++++--------- 4 files changed, 34 insertions(+), 10 deletions(-) diff --git a/blueprintcompiler/language/gtk_menu.py b/blueprintcompiler/language/gtk_menu.py index 73aa039..dedf6e3 100644 --- a/blueprintcompiler/language/gtk_menu.py +++ b/blueprintcompiler/language/gtk_menu.py @@ -39,6 +39,10 @@ class Menu(AstNode): def tag(self) -> str: return self.tokens["tag"] + @property + def items(self) -> T.List[T.Union["Menu", "MenuAttribute"]]: + return self.children + @validate("menu") def has_id(self): if self.tokens["tag"] == "menu" and self.tokens["id"] is None: diff --git a/blueprintcompiler/language/ui.py b/blueprintcompiler/language/ui.py index c277c56..d45fe4c 100644 --- a/blueprintcompiler/language/ui.py +++ b/blueprintcompiler/language/ui.py @@ -22,7 +22,7 @@ from .. import gir from .imports import GtkDirective, Import from .gtkbuilder_template import Template from .gobject_object import Object -from .gtk_menu import menu +from .gtk_menu import menu, Menu from .common import * @@ -64,6 +64,24 @@ class UI(AstNode): return gir_ctx + @property + def using(self) -> T.List[Import]: + return self.children[Import] + + @property + def gtk_decl(self) -> GtkDirective: + return self.children[GtkDirective][0] + + @property + def contents(self) -> T.List[T.Union[Object, Template, Menu]]: + return [ + child + for child in self.children + if isinstance(child, Object) + or isinstance(child, Template) + or isinstance(child, Menu) + ] + @property def objects_by_id(self): return { diff --git a/blueprintcompiler/language/values.py b/blueprintcompiler/language/values.py index 4a71247..8447267 100644 --- a/blueprintcompiler/language/values.py +++ b/blueprintcompiler/language/values.py @@ -203,6 +203,10 @@ class Flag(AstNode): class FlagsValue(Value): grammar = [Flag, "|", Delimited(Flag, "|")] + @property + def flags(self) -> T.List[Flag]: + return self.children + @validate() def parent_is_bitfield(self): type = self.parent.value_type diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index ac2aea8..48a18ac 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -14,12 +14,10 @@ class XmlOutput(OutputFormat): def _emit_ui(self, ui: UI, xml: XmlEmitter): xml.start_tag("interface") - for x in ui.children: - if isinstance(x, GtkDirective): - self._emit_gtk_directive(x, xml) - elif isinstance(x, Import): - pass - elif isinstance(x, Template): + self._emit_gtk_directive(ui.gtk_decl, xml) + + for x in ui.contents: + if isinstance(x, Template): self._emit_template(x, xml) elif isinstance(x, Object): self._emit_object(x, xml) @@ -74,7 +72,7 @@ class XmlOutput(OutputFormat): def _emit_menu(self, menu: Menu, xml: XmlEmitter): xml.start_tag(menu.tag, id=menu.id) - for child in menu.children: + for child in menu.items: if isinstance(child, Menu): self._emit_menu(child, xml) elif isinstance(child, MenuAttribute): @@ -167,7 +165,7 @@ class XmlOutput(OutputFormat): xml.put_text(value.value) elif isinstance(value, FlagsValue): xml.put_text( - "|".join([str(flag.value or flag.name) for flag in value.children]) + "|".join([str(flag.value or flag.name) for flag in value.flags]) ) elif isinstance(value, TranslatedStringValue): raise CompilerBugError("translated values must be handled in the parent") @@ -177,7 +175,7 @@ class XmlOutput(OutputFormat): raise CompilerBugError() def _emit_expression(self, expression: ExprChain, xml: XmlEmitter): - self._emit_expression_part(expression.children[-1], xml) + self._emit_expression_part(expression.last, xml) def _emit_expression_part(self, expression: Expr, xml: XmlEmitter): if isinstance(expression, IdentExpr):