mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add properties to AST types
I want to have a cleaner API that relies less on the specifics of the grammar and parser.
This commit is contained in:
parent
0b7dbaf90d
commit
6938267952
4 changed files with 34 additions and 10 deletions
|
@ -39,6 +39,10 @@ class Menu(AstNode):
|
||||||
def tag(self) -> str:
|
def tag(self) -> str:
|
||||||
return self.tokens["tag"]
|
return self.tokens["tag"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def items(self) -> T.List[T.Union["Menu", "MenuAttribute"]]:
|
||||||
|
return self.children
|
||||||
|
|
||||||
@validate("menu")
|
@validate("menu")
|
||||||
def has_id(self):
|
def has_id(self):
|
||||||
if self.tokens["tag"] == "menu" and self.tokens["id"] is None:
|
if self.tokens["tag"] == "menu" and self.tokens["id"] is None:
|
||||||
|
|
|
@ -22,7 +22,7 @@ from .. import gir
|
||||||
from .imports import GtkDirective, Import
|
from .imports import GtkDirective, Import
|
||||||
from .gtkbuilder_template import Template
|
from .gtkbuilder_template import Template
|
||||||
from .gobject_object import Object
|
from .gobject_object import Object
|
||||||
from .gtk_menu import menu
|
from .gtk_menu import menu, Menu
|
||||||
from .common import *
|
from .common import *
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,6 +64,24 @@ class UI(AstNode):
|
||||||
|
|
||||||
return gir_ctx
|
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
|
@property
|
||||||
def objects_by_id(self):
|
def objects_by_id(self):
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -203,6 +203,10 @@ class Flag(AstNode):
|
||||||
class FlagsValue(Value):
|
class FlagsValue(Value):
|
||||||
grammar = [Flag, "|", Delimited(Flag, "|")]
|
grammar = [Flag, "|", Delimited(Flag, "|")]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def flags(self) -> T.List[Flag]:
|
||||||
|
return self.children
|
||||||
|
|
||||||
@validate()
|
@validate()
|
||||||
def parent_is_bitfield(self):
|
def parent_is_bitfield(self):
|
||||||
type = self.parent.value_type
|
type = self.parent.value_type
|
||||||
|
|
|
@ -14,12 +14,10 @@ class XmlOutput(OutputFormat):
|
||||||
def _emit_ui(self, ui: UI, xml: XmlEmitter):
|
def _emit_ui(self, ui: UI, xml: XmlEmitter):
|
||||||
xml.start_tag("interface")
|
xml.start_tag("interface")
|
||||||
|
|
||||||
for x in ui.children:
|
self._emit_gtk_directive(ui.gtk_decl, xml)
|
||||||
if isinstance(x, GtkDirective):
|
|
||||||
self._emit_gtk_directive(x, xml)
|
for x in ui.contents:
|
||||||
elif isinstance(x, Import):
|
if isinstance(x, Template):
|
||||||
pass
|
|
||||||
elif isinstance(x, Template):
|
|
||||||
self._emit_template(x, xml)
|
self._emit_template(x, xml)
|
||||||
elif isinstance(x, Object):
|
elif isinstance(x, Object):
|
||||||
self._emit_object(x, xml)
|
self._emit_object(x, xml)
|
||||||
|
@ -74,7 +72,7 @@ class XmlOutput(OutputFormat):
|
||||||
|
|
||||||
def _emit_menu(self, menu: Menu, xml: XmlEmitter):
|
def _emit_menu(self, menu: Menu, xml: XmlEmitter):
|
||||||
xml.start_tag(menu.tag, id=menu.id)
|
xml.start_tag(menu.tag, id=menu.id)
|
||||||
for child in menu.children:
|
for child in menu.items:
|
||||||
if isinstance(child, Menu):
|
if isinstance(child, Menu):
|
||||||
self._emit_menu(child, xml)
|
self._emit_menu(child, xml)
|
||||||
elif isinstance(child, MenuAttribute):
|
elif isinstance(child, MenuAttribute):
|
||||||
|
@ -167,7 +165,7 @@ class XmlOutput(OutputFormat):
|
||||||
xml.put_text(value.value)
|
xml.put_text(value.value)
|
||||||
elif isinstance(value, FlagsValue):
|
elif isinstance(value, FlagsValue):
|
||||||
xml.put_text(
|
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):
|
elif isinstance(value, TranslatedStringValue):
|
||||||
raise CompilerBugError("translated values must be handled in the parent")
|
raise CompilerBugError("translated values must be handled in the parent")
|
||||||
|
@ -177,7 +175,7 @@ class XmlOutput(OutputFormat):
|
||||||
raise CompilerBugError()
|
raise CompilerBugError()
|
||||||
|
|
||||||
def _emit_expression(self, expression: ExprChain, xml: XmlEmitter):
|
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):
|
def _emit_expression_part(self, expression: Expr, xml: XmlEmitter):
|
||||||
if isinstance(expression, IdentExpr):
|
if isinstance(expression, IdentExpr):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue