Add lookup expressions

This commit is contained in:
James Westman 2022-01-29 21:21:04 -06:00
parent c094743e84
commit 4fefa0bd73
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
8 changed files with 177 additions and 5 deletions

View file

@ -2,6 +2,7 @@
templates. """
from .attributes import BaseAttribute, BaseTypedAttribute
from .expression import Expr
from .gobject_object import Object, ObjectContent
from .gobject_property import Property
from .gobject_signal import Signal

View file

@ -0,0 +1,63 @@
# expressions.py
#
# Copyright 2022 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .common import *
expr = Pratt()
class Expr(AstNode):
grammar = expr
def emit_xml(self, xml: XmlEmitter):
self.children[-1].emit_xml(xml)
class InfixExpr(AstNode):
@property
def lhs(self):
children = list(self.parent_by_type(Expr).children)
return children[children.index(self) - 1]
class IdentExpr(AstNode):
grammar = UseIdent("ident")
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("constant")
xml.put_text(self.tokens["ident"])
xml.end_tag()
class LookupOp(InfixExpr):
grammar = [".", UseIdent("property")]
def emit_xml(self, xml: XmlEmitter):
xml.start_tag("lookup", name=self.tokens["property"])
self.lhs.emit_xml(xml)
xml.end_tag()
expr.children = [
Prefix(IdentExpr),
Prefix(["(", Expr, ")"]),
Infix(10, LookupOp),
]

View file

@ -18,6 +18,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .expression import Expr
from .gobject_object import Object
from .gtkbuilder_template import Template
from .values import Value, TranslatedStringValue
@ -26,19 +27,27 @@ from .common import *
class Property(AstNode):
grammar = AnyOf(
Statement(
[
UseIdent("name"),
":",
Keyword("bind"),
UseIdent("bind_source").expected("the ID of a source object to bind from"),
UseIdent("bind_source"),
".",
UseIdent("bind_property").expected("a property name to bind from"),
UseIdent("bind_property"),
ZeroOrMore(AnyOf(
["no-sync-create", UseLiteral("no_sync_create", True)],
["inverted", UseLiteral("inverted", True)],
["bidirectional", UseLiteral("bidirectional", True)],
Match("sync-create").warn("sync-create is deprecated in favor of no-sync-create"),
)),
";",
],
Statement(
UseIdent("name"),
UseLiteral("binding", True),
":",
"bind",
Expr,
),
Statement(
UseIdent("name"),
@ -154,7 +163,13 @@ class Property(AstNode):
self.children[Object][0].emit_xml(xml)
xml.end_tag()
elif value is None:
xml.put_self_closing("property", **props)
if self.tokens["binding"]:
xml.start_tag("binding", **props)
for x in self.children:
x.emit_xml(xml)
xml.end_tag()
else:
xml.put_self_closing("property", **props);
else:
xml.start_tag("property", **props)
value.emit_xml(xml)