mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Add lookup expressions
This commit is contained in:
parent
c094743e84
commit
4fefa0bd73
8 changed files with 177 additions and 5 deletions
63
blueprintcompiler/language/expression.py
Normal file
63
blueprintcompiler/language/expression.py
Normal 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),
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue