From f894301571927e7c87bf8c9ffc134fccda3d203f Mon Sep 17 00:00:00 2001 From: jgcodes2020 Date: Sat, 14 Dec 2024 10:33:46 -0500 Subject: [PATCH 1/3] preliminary variant syntax --- .../language/gobject_property.py | 6 ++-- blueprintcompiler/language/values.py | 36 +++++++++++++++++++ blueprintcompiler/outputs/xml/__init__.py | 13 +++++++ variant-test.blp | 6 ++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 variant-test.blp diff --git a/blueprintcompiler/language/gobject_property.py b/blueprintcompiler/language/gobject_property.py index 5d0c867..b267083 100644 --- a/blueprintcompiler/language/gobject_property.py +++ b/blueprintcompiler/language/gobject_property.py @@ -22,12 +22,12 @@ from .binding import Binding from .common import * from .contexts import ValueTypeCtx from .gtkbuilder_template import Template -from .values import ArrayValue, ObjectValue, Value +from .values import ArrayValue, ObjectValue, Value, VariantValue class Property(AstNode): grammar = Statement( - UseIdent("name"), ":", AnyOf(Binding, ObjectValue, Value, ArrayValue) + UseIdent("name"), ":", AnyOf(Binding, VariantValue, ObjectValue, Value, ArrayValue) ) @property @@ -35,7 +35,7 @@ class Property(AstNode): return self.tokens["name"] @property - def value(self) -> T.Union[Binding, ObjectValue, Value, ArrayValue]: + def value(self) -> T.Union[Binding, VariantValue, ObjectValue, Value, ArrayValue]: return self.children[0] @property diff --git a/blueprintcompiler/language/values.py b/blueprintcompiler/language/values.py index 63cf4fc..9b8eb18 100644 --- a/blueprintcompiler/language/values.py +++ b/blueprintcompiler/language/values.py @@ -371,6 +371,42 @@ class IdentLiteral(AstNode): else: 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): grammar = AnyOf( diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index 5e43834..c3735eb 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -1,5 +1,7 @@ import typing as T +from blueprintcompiler.language.values import VariantValue + from ...language import * from .. import OutputFormat from .xml_emitter import XmlEmitter @@ -148,6 +150,11 @@ class XmlOutput(OutputFormat): self._emit_value(values[-1], xml) 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: raise CompilerBugError() @@ -205,6 +212,8 @@ class XmlOutput(OutputFormat): xml.put_text(self._object_id(value, value.ident)) elif isinstance(value, TypeLiteral): xml.put_text(value.type_name.glib_type_name) + elif isinstance(value, VariantValue): + xml.put_text(value.value) else: if isinstance(value.value, float) and value.value == int(value.value): xml.put_text(int(value.value)) @@ -284,6 +293,10 @@ class XmlOutput(OutputFormat): xml.start_tag(tag, **attrs) xml.put_text(value.child.value) 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: xml.start_tag(tag, **attrs) self._emit_value(value, xml) diff --git a/variant-test.blp b/variant-test.blp new file mode 100644 index 0000000..52a397d --- /dev/null +++ b/variant-test.blp @@ -0,0 +1,6 @@ +using Gtk 4.0; + +Button { + action-name: "app.shave_yak"; + action-target: variant<"y">("8"); +} \ No newline at end of file From a75adc7d1bc7780c7f4c31045a5955f365fa108d Mon Sep 17 00:00:00 2001 From: jgcodes2020 Date: Sat, 14 Dec 2024 14:00:09 -0500 Subject: [PATCH 2/3] add variant support for menus --- blueprintcompiler/language/gtk_menu.py | 12 ++++++++---- blueprintcompiler/language/values.py | 6 ++++-- blueprintcompiler/outputs/xml/__init__.py | 24 ++++++++++++++++------- variant-test.blp | 10 ++++++++++ 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/blueprintcompiler/language/gtk_menu.py b/blueprintcompiler/language/gtk_menu.py index c7ef5f2..9e76d61 100644 --- a/blueprintcompiler/language/gtk_menu.py +++ b/blueprintcompiler/language/gtk_menu.py @@ -19,7 +19,7 @@ import typing as T -from blueprintcompiler.language.values import StringValue +from blueprintcompiler.language.values import StringValue, VariantValue from .common import * from .contexts import ValueTypeCtx @@ -98,8 +98,12 @@ class MenuAttribute(AstNode): return self.tokens["name"] @property - def value(self) -> StringValue: - return self.children[StringValue][0] + def value(self) -> StringValue | VariantValue: + if len(self.children[StringValue]) > 0: + return self.children[StringValue][0] + elif len(self.children[VariantValue]) > 0: + return self.children[VariantValue][0] + raise CompilerBugError() @property def document_symbol(self) -> DocumentSymbol: @@ -133,7 +137,7 @@ menu_attribute = Group( [ UseIdent("name"), ":", - Err(StringValue, "Expected string or translated string"), + Err(AnyOf(StringValue, VariantValue), "Expected string or translated string"), Match(";").expected(), ], ) diff --git a/blueprintcompiler/language/values.py b/blueprintcompiler/language/values.py index 9b8eb18..41a4c5e 100644 --- a/blueprintcompiler/language/values.py +++ b/blueprintcompiler/language/values.py @@ -393,7 +393,9 @@ class VariantValue(AstNode): @validate() def validate_for_type(self) -> None: expected_type = self.context[ValueTypeCtx].value_type - if ( + if expected_type is None: + pass + elif ( isinstance(expected_type, gir.IntType) or isinstance(expected_type, gir.UIntType) or isinstance(expected_type, gir.FloatType) @@ -402,7 +404,7 @@ class VariantValue(AstNode): 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": + elif 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}") diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index c3735eb..4faeaff 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -85,13 +85,23 @@ class XmlOutput(OutputFormat): if isinstance(child, Menu): self._emit_menu(child, xml) elif isinstance(child, MenuAttribute): - xml.start_tag( - "attribute", - name=child.name, - **self._translated_string_attrs(child.value.child), - ) - xml.put_text(child.value.string) - xml.end_tag() + if isinstance(child.value, StringValue): + xml.start_tag( + "attribute", + name=child.name, + **self._translated_string_attrs(child.value.child), + ) + xml.put_text(child.value.string) + xml.end_tag() + elif isinstance(child.value, VariantValue): + xml.start_tag( + "attribute", + name=child.name, + type=child.value.var_type, + ) + xml.put_text(child.value.var_value) + xml.end_tag() + else: raise CompilerBugError() xml.end_tag() diff --git a/variant-test.blp b/variant-test.blp index 52a397d..f69522f 100644 --- a/variant-test.blp +++ b/variant-test.blp @@ -1,5 +1,15 @@ using Gtk 4.0; +menu root { + submenu { + name: "one"; + item { + action: "app.foo_bar"; + target: variant<"s">("\"one\""); + } + } +} + Button { action-name: "app.shave_yak"; action-target: variant<"y">("8"); From 3efb2f9f4ec8f949efee071248d49c25b50582e0 Mon Sep 17 00:00:00 2001 From: jgcodes2020 Date: Sat, 14 Dec 2024 14:01:14 -0500 Subject: [PATCH 3/3] remove weird kwarg syntax --- blueprintcompiler/outputs/xml/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blueprintcompiler/outputs/xml/__init__.py b/blueprintcompiler/outputs/xml/__init__.py index 4faeaff..4e4cc1c 100644 --- a/blueprintcompiler/outputs/xml/__init__.py +++ b/blueprintcompiler/outputs/xml/__init__.py @@ -161,7 +161,7 @@ class XmlOutput(OutputFormat): xml.end_tag() elif isinstance(value, VariantValue): - xml.start_tag("property", **props, **{"type": value.var_type}) + xml.start_tag("property", **props, type=value.var_type) xml.put_text(value.var_value) xml.end_tag()