mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
language: Add cast expressions
This commit is contained in:
parent
2033bd9e16
commit
5cf9b63547
15 changed files with 122 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
||||||
from .attributes import BaseAttribute, BaseTypedAttribute
|
from .attributes import BaseAttribute, BaseTypedAttribute
|
||||||
from .expression import IdentExpr, LookupOp, Expr
|
from .expression import CastExpr, IdentExpr, LookupOp, ExprChain
|
||||||
from .gobject_object import Object, ObjectContent
|
from .gobject_object import Object, ObjectContent
|
||||||
from .gobject_property import Property
|
from .gobject_property import Property
|
||||||
from .gobject_signal import Signal
|
from .gobject_signal import Signal
|
||||||
|
|
|
@ -19,29 +19,59 @@
|
||||||
|
|
||||||
|
|
||||||
from .common import *
|
from .common import *
|
||||||
|
from .types import TypeName
|
||||||
|
|
||||||
|
|
||||||
expr = Pratt()
|
expr = Pratt()
|
||||||
|
|
||||||
|
|
||||||
class Expr(AstNode):
|
class Expr:
|
||||||
|
@property
|
||||||
|
def type(self) -> T.Optional[GirType]:
|
||||||
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
|
class ExprChain(Expr, AstNode):
|
||||||
grammar = expr
|
grammar = expr
|
||||||
|
|
||||||
|
@property
|
||||||
|
def last(self) -> Expr:
|
||||||
|
return self.children[-1]
|
||||||
|
|
||||||
class InfixExpr(AstNode):
|
@property
|
||||||
|
def type(self) -> T.Optional[GirType]:
|
||||||
|
return self.last.type
|
||||||
|
|
||||||
|
|
||||||
|
class InfixExpr(Expr, AstNode):
|
||||||
@property
|
@property
|
||||||
def lhs(self):
|
def lhs(self):
|
||||||
children = list(self.parent_by_type(Expr).children)
|
children = list(self.parent_by_type(ExprChain).children)
|
||||||
return children[children.index(self) - 1]
|
return children[children.index(self) - 1]
|
||||||
|
|
||||||
|
|
||||||
class IdentExpr(AstNode):
|
class IdentExpr(Expr, AstNode):
|
||||||
grammar = UseIdent("ident")
|
grammar = UseIdent("ident")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ident(self) -> str:
|
def ident(self) -> str:
|
||||||
return self.tokens["ident"]
|
return self.tokens["ident"]
|
||||||
|
|
||||||
|
@validate()
|
||||||
|
def exists(self):
|
||||||
|
if self.root.objects_by_id.get(self.ident) is None:
|
||||||
|
raise CompileError(
|
||||||
|
f"Could not find object with ID {self.ident}",
|
||||||
|
did_you_mean=(self.ident, self.root.objects_by_id.keys()),
|
||||||
|
)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self) -> T.Optional[GirType]:
|
||||||
|
if object := self.root.objects_by_id.get(self.ident):
|
||||||
|
return object.gir_class
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
class LookupOp(InfixExpr):
|
class LookupOp(InfixExpr):
|
||||||
grammar = [".", UseIdent("property")]
|
grammar = [".", UseIdent("property")]
|
||||||
|
@ -50,9 +80,53 @@ class LookupOp(InfixExpr):
|
||||||
def property_name(self) -> str:
|
def property_name(self) -> str:
|
||||||
return self.tokens["property"]
|
return self.tokens["property"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self) -> T.Optional[GirType]:
|
||||||
|
if isinstance(self.lhs.type, gir.Class) or isinstance(
|
||||||
|
self.lhs.type, gir.Interface
|
||||||
|
):
|
||||||
|
if property := self.lhs.type.properties.get(self.property_name):
|
||||||
|
return property.type
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
@validate("property")
|
||||||
|
def property_exists(self):
|
||||||
|
if self.lhs.type is None or isinstance(self.lhs.type, UncheckedType):
|
||||||
|
return
|
||||||
|
elif not isinstance(self.lhs.type, gir.Class) and not isinstance(
|
||||||
|
self.lhs.type, gir.Interface
|
||||||
|
):
|
||||||
|
raise CompileError(
|
||||||
|
f"Type {self.lhs.type.full_name} does not have properties"
|
||||||
|
)
|
||||||
|
elif self.lhs.type.properties.get(self.property_name) is None:
|
||||||
|
raise CompileError(
|
||||||
|
f"{self.lhs.type.full_name} does not have a property called {self.property_name}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class CastExpr(InfixExpr):
|
||||||
|
grammar = ["as", "(", TypeName, ")"]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def type(self) -> T.Optional[GirType]:
|
||||||
|
return self.children[TypeName][0].gir_type
|
||||||
|
|
||||||
|
@validate()
|
||||||
|
def cast_makes_sense(self):
|
||||||
|
if self.lhs.type is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not self.type.assignable_to(self.lhs.type):
|
||||||
|
raise CompileError(
|
||||||
|
f"Invalid cast. No instance of {self.lhs.type.full_name} can be an instance of {self.type.full_name}."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
expr.children = [
|
expr.children = [
|
||||||
Prefix(IdentExpr),
|
Prefix(IdentExpr),
|
||||||
Prefix(["(", Expr, ")"]),
|
Prefix(["(", ExprChain, ")"]),
|
||||||
Infix(10, LookupOp),
|
Infix(10, LookupOp),
|
||||||
|
Infix(10, CastExpr),
|
||||||
]
|
]
|
||||||
|
|
|
@ -54,7 +54,9 @@ class Object(AstNode):
|
||||||
return self.children[ObjectContent][0]
|
return self.children[ObjectContent][0]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def gir_class(self):
|
def gir_class(self) -> GirType:
|
||||||
|
if self.class_name is None:
|
||||||
|
raise CompilerBugError()
|
||||||
return self.class_name.gir_type
|
return self.class_name.gir_type
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
|
|
|
@ -18,7 +18,7 @@
|
||||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
|
|
||||||
from .expression import Expr
|
from .expression import ExprChain
|
||||||
from .gobject_object import Object
|
from .gobject_object import Object
|
||||||
from .gtkbuilder_template import Template
|
from .gtkbuilder_template import Template
|
||||||
from .values import Value, TranslatedStringValue
|
from .values import Value, TranslatedStringValue
|
||||||
|
@ -51,7 +51,7 @@ class Property(AstNode):
|
||||||
UseLiteral("binding", True),
|
UseLiteral("binding", True),
|
||||||
":",
|
":",
|
||||||
"bind",
|
"bind",
|
||||||
Expr,
|
ExprChain,
|
||||||
),
|
),
|
||||||
Statement(
|
Statement(
|
||||||
UseIdent("name"),
|
UseIdent("name"),
|
||||||
|
@ -91,7 +91,7 @@ class Property(AstNode):
|
||||||
|
|
||||||
if self.gir_property is None:
|
if self.gir_property is None:
|
||||||
raise CompileError(
|
raise CompileError(
|
||||||
f"Class {self.gir_class.full_name} does not contain a property called {self.tokens['name']}",
|
f"Class {self.gir_class.full_name} does not have a property called {self.tokens['name']}",
|
||||||
did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()),
|
did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -114,7 +114,7 @@ class XmlOutput(OutputFormat):
|
||||||
elif value is None:
|
elif value is None:
|
||||||
if property.tokens["binding"]:
|
if property.tokens["binding"]:
|
||||||
xml.start_tag("binding", **props)
|
xml.start_tag("binding", **props)
|
||||||
self._emit_expression(property.children[Expr][0], xml)
|
self._emit_expression(property.children[ExprChain][0], xml)
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
else:
|
else:
|
||||||
xml.put_self_closing("property", **props)
|
xml.put_self_closing("property", **props)
|
||||||
|
@ -176,7 +176,7 @@ class XmlOutput(OutputFormat):
|
||||||
else:
|
else:
|
||||||
raise CompilerBugError()
|
raise CompilerBugError()
|
||||||
|
|
||||||
def _emit_expression(self, expression: Expr, xml: XmlEmitter):
|
def _emit_expression(self, expression: ExprChain, xml: XmlEmitter):
|
||||||
self._emit_expression_part(expression.children[-1], xml)
|
self._emit_expression_part(expression.children[-1], xml)
|
||||||
|
|
||||||
def _emit_expression_part(self, expression, xml: XmlEmitter):
|
def _emit_expression_part(self, expression, xml: XmlEmitter):
|
||||||
|
@ -184,8 +184,10 @@ class XmlOutput(OutputFormat):
|
||||||
self._emit_ident_expr(expression, xml)
|
self._emit_ident_expr(expression, xml)
|
||||||
elif isinstance(expression, LookupOp):
|
elif isinstance(expression, LookupOp):
|
||||||
self._emit_lookup_op(expression, xml)
|
self._emit_lookup_op(expression, xml)
|
||||||
elif isinstance(expression, Expr):
|
elif isinstance(expression, ExprChain):
|
||||||
self._emit_expression(expression, xml)
|
self._emit_expression(expression, xml)
|
||||||
|
elif isinstance(expression, CastExpr):
|
||||||
|
self._emit_cast_expr(expression, xml)
|
||||||
else:
|
else:
|
||||||
raise CompilerBugError()
|
raise CompilerBugError()
|
||||||
|
|
||||||
|
@ -195,10 +197,13 @@ class XmlOutput(OutputFormat):
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
def _emit_lookup_op(self, expr: LookupOp, xml: XmlEmitter):
|
def _emit_lookup_op(self, expr: LookupOp, xml: XmlEmitter):
|
||||||
xml.start_tag("lookup", name=expr.property_name)
|
xml.start_tag("lookup", name=expr.property_name, type=expr.lhs.type)
|
||||||
self._emit_expression_part(expr.lhs, xml)
|
self._emit_expression_part(expr.lhs, xml)
|
||||||
xml.end_tag()
|
xml.end_tag()
|
||||||
|
|
||||||
|
def _emit_cast_expr(self, expr: CastExpr, xml: XmlEmitter):
|
||||||
|
self._emit_expression_part(expr.lhs, xml)
|
||||||
|
|
||||||
def _emit_attribute(
|
def _emit_attribute(
|
||||||
self, tag: str, attr: str, name: str, value: Value, xml: XmlEmitter
|
self, tag: str, attr: str, name: str, value: Value, xml: XmlEmitter
|
||||||
):
|
):
|
||||||
|
|
5
tests/sample_errors/expr_cast_conversion.blp
Normal file
5
tests/sample_errors/expr_cast_conversion.blp
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
Overlay overlay {
|
||||||
|
margin-bottom: bind overlay.child as (Adjustment).value;
|
||||||
|
}
|
1
tests/sample_errors/expr_cast_conversion.err
Normal file
1
tests/sample_errors/expr_cast_conversion.err
Normal file
|
@ -0,0 +1 @@
|
||||||
|
4,37,15,Invalid cast. No instance of Gtk.Widget can be an instance of Gtk.Adjustment.
|
5
tests/sample_errors/expr_lookup_dne.blp
Normal file
5
tests/sample_errors/expr_lookup_dne.blp
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
Overlay overlay {
|
||||||
|
margin-bottom: bind overlay.child as (Label).not-a-property;
|
||||||
|
}
|
1
tests/sample_errors/expr_lookup_dne.err
Normal file
1
tests/sample_errors/expr_lookup_dne.err
Normal file
|
@ -0,0 +1 @@
|
||||||
|
4,48,14,Gtk.Label does not have a property called not-a-property
|
5
tests/sample_errors/expr_lookup_no_properties.blp
Normal file
5
tests/sample_errors/expr_lookup_no_properties.blp
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
using Gtk 4.0;
|
||||||
|
|
||||||
|
Overlay overlay {
|
||||||
|
margin-bottom: bind overlay.margin-bottom.what;
|
||||||
|
}
|
1
tests/sample_errors/expr_lookup_no_properties.err
Normal file
1
tests/sample_errors/expr_lookup_no_properties.err
Normal file
|
@ -0,0 +1 @@
|
||||||
|
4,45,4,Type int does not have properties
|
|
@ -1 +1 @@
|
||||||
4,3,19,Class Gtk.Label does not contain a property called not-a-real-property
|
4,3,19,Class Gtk.Label does not have a property called not-a-real-property
|
||||||
|
|
|
@ -5,5 +5,5 @@ Overlay {
|
||||||
}
|
}
|
||||||
|
|
||||||
Label {
|
Label {
|
||||||
label: bind (label.parent).child.label;
|
label: bind (label.parent) as (Overlay).child as (Label).label;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,9 +8,9 @@
|
||||||
</object>
|
</object>
|
||||||
<object class="GtkLabel">
|
<object class="GtkLabel">
|
||||||
<binding name="label">
|
<binding name="label">
|
||||||
<lookup name="label">
|
<lookup name="label" type="GtkLabel">
|
||||||
<lookup name="child">
|
<lookup name="child" type="GtkOverlay">
|
||||||
<lookup name="parent">
|
<lookup name="parent" type="GtkLabel">
|
||||||
<constant>label</constant>
|
<constant>label</constant>
|
||||||
</lookup>
|
</lookup>
|
||||||
</lookup>
|
</lookup>
|
||||||
|
|
|
@ -151,7 +151,7 @@ class TestSamples(unittest.TestCase):
|
||||||
self.assert_sample("combo_box_text")
|
self.assert_sample("combo_box_text")
|
||||||
self.assert_sample("comments")
|
self.assert_sample("comments")
|
||||||
self.assert_sample("enum")
|
self.assert_sample("enum")
|
||||||
self.assert_sample("expr_lookup", skip_run=True) # TODO: Fix
|
self.assert_sample("expr_lookup")
|
||||||
self.assert_sample("file_filter")
|
self.assert_sample("file_filter")
|
||||||
self.assert_sample("flags")
|
self.assert_sample("flags")
|
||||||
self.assert_sample("id_prop")
|
self.assert_sample("id_prop")
|
||||||
|
@ -207,6 +207,9 @@ class TestSamples(unittest.TestCase):
|
||||||
self.assert_sample_error("duplicates")
|
self.assert_sample_error("duplicates")
|
||||||
self.assert_sample_error("empty")
|
self.assert_sample_error("empty")
|
||||||
self.assert_sample_error("enum_member_dne")
|
self.assert_sample_error("enum_member_dne")
|
||||||
|
self.assert_sample_error("expr_cast_conversion")
|
||||||
|
self.assert_sample_error("expr_lookup_dne")
|
||||||
|
self.assert_sample_error("expr_lookup_no_properties")
|
||||||
self.assert_sample_error("filters_in_non_file_filter")
|
self.assert_sample_error("filters_in_non_file_filter")
|
||||||
self.assert_sample_error("gtk_3")
|
self.assert_sample_error("gtk_3")
|
||||||
self.assert_sample_error("gtk_exact_version")
|
self.assert_sample_error("gtk_exact_version")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue