preliminary variant syntax

This commit is contained in:
jgcodes2020 2024-12-14 10:33:46 -05:00
parent 778a979714
commit f894301571
4 changed files with 58 additions and 3 deletions

View file

@ -22,12 +22,12 @@ from .binding import Binding
from .common import * from .common import *
from .contexts import ValueTypeCtx from .contexts import ValueTypeCtx
from .gtkbuilder_template import Template from .gtkbuilder_template import Template
from .values import ArrayValue, ObjectValue, Value from .values import ArrayValue, ObjectValue, Value, VariantValue
class Property(AstNode): class Property(AstNode):
grammar = Statement( grammar = Statement(
UseIdent("name"), ":", AnyOf(Binding, ObjectValue, Value, ArrayValue) UseIdent("name"), ":", AnyOf(Binding, VariantValue, ObjectValue, Value, ArrayValue)
) )
@property @property
@ -35,7 +35,7 @@ class Property(AstNode):
return self.tokens["name"] return self.tokens["name"]
@property @property
def value(self) -> T.Union[Binding, ObjectValue, Value, ArrayValue]: def value(self) -> T.Union[Binding, VariantValue, ObjectValue, Value, ArrayValue]:
return self.children[0] return self.children[0]
@property @property

View file

@ -371,6 +371,42 @@ class IdentLiteral(AstNode):
else: else:
return None return None
class VariantValue(AstNode):
grammar = [
"variant",
"<",
UseQuoted("type"),
">",
"(",
UseQuoted("value"),
")"
]
@property
def var_type(self) -> str:
return self.tokens["type"]
@property
def var_value(self) -> str:
return self.tokens["value"]
@validate()
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if (
isinstance(expected_type, gir.IntType)
or isinstance(expected_type, gir.UIntType)
or isinstance(expected_type, gir.FloatType)
or isinstance(expected_type, gir.FloatType)
):
raise CompileError(f"Cannot convert variant to number")
elif isinstance(expected_type, gir.StringType):
raise CompileError("Cannot convert variant to string")
if isinstance(expected_type, gir.Boxed) and expected_type.full_name == "GLib.Variant":
pass
else:
raise CompileError(f"Cannot convert variant into {expected_type.full_name}")
pass
class Literal(AstNode): class Literal(AstNode):
grammar = AnyOf( grammar = AnyOf(

View file

@ -1,5 +1,7 @@
import typing as T import typing as T
from blueprintcompiler.language.values import VariantValue
from ...language import * from ...language import *
from .. import OutputFormat from .. import OutputFormat
from .xml_emitter import XmlEmitter from .xml_emitter import XmlEmitter
@ -148,6 +150,11 @@ class XmlOutput(OutputFormat):
self._emit_value(values[-1], xml) self._emit_value(values[-1], xml)
xml.end_tag() xml.end_tag()
elif isinstance(value, VariantValue):
xml.start_tag("property", **props, **{"type": value.var_type})
xml.put_text(value.var_value)
xml.end_tag()
else: else:
raise CompilerBugError() raise CompilerBugError()
@ -205,6 +212,8 @@ class XmlOutput(OutputFormat):
xml.put_text(self._object_id(value, value.ident)) xml.put_text(self._object_id(value, value.ident))
elif isinstance(value, TypeLiteral): elif isinstance(value, TypeLiteral):
xml.put_text(value.type_name.glib_type_name) xml.put_text(value.type_name.glib_type_name)
elif isinstance(value, VariantValue):
xml.put_text(value.value)
else: else:
if isinstance(value.value, float) and value.value == int(value.value): if isinstance(value.value, float) and value.value == int(value.value):
xml.put_text(int(value.value)) xml.put_text(int(value.value))
@ -284,6 +293,10 @@ class XmlOutput(OutputFormat):
xml.start_tag(tag, **attrs) xml.start_tag(tag, **attrs)
xml.put_text(value.child.value) xml.put_text(value.child.value)
xml.end_tag() xml.end_tag()
elif isinstance(value.child, VariantValue):
xml.start_tag(tag, **attrs, type=value.child.var_type)
xml.put_text(value.child.var_value)
xml.end_tag()
else: else:
xml.start_tag(tag, **attrs) xml.start_tag(tag, **attrs)
self._emit_value(value, xml) self._emit_value(value, xml)

6
variant-test.blp Normal file
View file

@ -0,0 +1,6 @@
using Gtk 4.0;
Button {
action-name: "app.shave_yak";
action-target: variant<"y">("8");
}