parser: Shorter code for groups

Add a "grammar" property on AstNode types so they can be used in grammar
expressions as groups
This commit is contained in:
James Westman 2022-01-17 00:04:26 -06:00
parent 8d587b62a0
commit 76f7befd68
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
10 changed files with 119 additions and 159 deletions

View file

@ -1,18 +1,18 @@
""" Contains all the syntax beyond basic objects, properties, signal, and
templates. """
from .gtk_a11y import a11y
from .gtk_combo_box_text import items
from .gtk_a11y import A11y
from .gtk_combo_box_text import Items
from .gtk_file_filter import mime_types, patterns, suffixes
from .gtk_layout import layout
from .gtk_layout import Layout
from .gtk_menu import menu
from .gtk_size_group import widgets
from .gtk_string_list import strings
from .gtk_styles import styles
from .gtk_size_group import Widgets
from .gtk_string_list import Strings
from .gtk_styles import Styles
OBJECT_HOOKS = [menu]
OBJECT_CONTENT_HOOKS = [
a11y, styles, layout, mime_types, patterns, suffixes, widgets, items,
strings,
A11y, Styles, Layout, mime_types, patterns, suffixes, Widgets, Items,
Strings,
]

View file

@ -105,20 +105,13 @@ def _get_docs(gir, name):
).doc
class A11y(AstNode):
@validate("accessibility")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "accessibility properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("accessibility")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class A11yProperty(BaseTypedAttribute):
grammar = Statement(
UseIdent("name"),
":",
value.expected("a value"),
)
@property
def tag_name(self):
name = self.tokens["name"]
@ -151,23 +144,23 @@ class A11yProperty(BaseTypedAttribute):
return _get_docs(self.root.gir, self.tokens["name"])
a11y_prop = Group(
A11yProperty,
Statement(
UseIdent("name"),
":",
value.expected("a value"),
)
)
a11y = Group(
A11y,
[
class A11y(AstNode):
grammar = [
Keyword("accessibility"),
"{",
Until(a11y_prop, "}"),
Until(A11yProperty, "}"),
]
)
@validate("accessibility")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "accessibility properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("accessibility")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@completer(

View file

@ -28,19 +28,6 @@ from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Items(AstNode):
@validate("items")
def container_is_combo_box_text(self):
self.validate_parent_type("Gtk", "ComboBoxText", "combo box items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Item(BaseTypedAttribute):
tag_name = "item"
attr_name = "id"
@ -61,15 +48,25 @@ item = Group(
]
)
items = Group(
Items,
[
class Items(AstNode):
grammar = [
Keyword("items"),
"[",
Delimited(item, ","),
"]",
]
)
@validate("items")
def container_is_combo_box_text(self):
self.validate_parent_type("Gtk", "ComboBoxText", "combo box items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@completer(

View file

@ -27,19 +27,6 @@ from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Layout(AstNode):
@validate("layout")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "layout properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("layout")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class LayoutProperty(BaseAttribute):
tag_name = "property"
@ -58,14 +45,24 @@ layout_prop = Group(
)
)
layout = Group(
Layout,
Sequence(
class Layout(AstNode):
grammar = Sequence(
Keyword("layout"),
"{",
Until(layout_prop, "}"),
)
)
@validate("layout")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "layout properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("layout")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@completer(

View file

@ -27,19 +27,9 @@ from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Widgets(AstNode):
@validate("widgets")
def container_is_size_group(self):
self.validate_parent_type("Gtk", "SizeGroup", "size group properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("widgets")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Widget(AstNode):
grammar = UseIdent("name")
@validate("name")
def obj_widget(self):
object = self.root.objects_by_id.get(self.tokens["name"])
@ -58,21 +48,23 @@ class Widget(AstNode):
xml.put_self_closing("widget", name=self.tokens["name"])
widgets = Group(
Widgets,
[
class Widgets(AstNode):
grammar = [
Keyword("widgets"),
"[",
Delimited(
Group(
Widget,
UseIdent("name"),
),
",",
),
Delimited(Widget, ","),
"]",
]
)
@validate("widgets")
def container_is_size_group(self):
self.validate_parent_type("Gtk", "SizeGroup", "size group properties")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("widgets")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@completer(

View file

@ -28,20 +28,9 @@ from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class Items(AstNode):
@validate("items")
def container_is_string_list(self):
self.validate_parent_type("Gtk", "StringList", "StringList items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
class Item(AstNode):
grammar = value
@property
def value_type(self):
return StringType()
@ -54,20 +43,24 @@ class Item(AstNode):
xml.end_tag()
item = Group(
Item,
value,
)
strings = Group(
Items,
[
class Strings(AstNode):
grammar = [
Keyword("strings"),
"[",
Delimited(item, ","),
Delimited(Item, ","),
"]",
]
)
@validate("items")
def container_is_string_list(self):
self.validate_parent_type("Gtk", "StringList", "StringList items")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("items")
for child in self.children:
child.emit_xml(xml)
xml.end_tag()
@completer(

View file

@ -27,7 +27,21 @@ from ..parser_utils import *
from ..xml_emitter import XmlEmitter
class StyleClass(AstNode):
grammar = UseQuoted("name")
def emit_xml(self, xml):
xml.put_self_closing("class", name=self.tokens["name"])
class Styles(AstNode):
grammar = [
Keyword("styles"),
"[",
Delimited(StyleClass, ","),
"]",
]
@validate("styles")
def container_is_widget(self):
self.validate_parent_type("Gtk", "Widget", "style classes")
@ -39,28 +53,6 @@ class Styles(AstNode):
xml.end_tag()
class StyleClass(AstNode):
def emit_xml(self, xml):
xml.put_self_closing("class", name=self.tokens["name"])
styles = Group(
Styles,
[
Keyword("styles"),
"[",
Delimited(
Group(
StyleClass,
UseQuoted("name")
),
",",
),
"]",
]
)
@completer(
applies_in=[ast.ObjectContent],
applies_in_subclass=("Gtk", "Widget"),