diff --git a/blueprintcompiler/language/gtk_menu.py b/blueprintcompiler/language/gtk_menu.py index 2d3b65b..83b5e63 100644 --- a/blueprintcompiler/language/gtk_menu.py +++ b/blueprintcompiler/language/gtk_menu.py @@ -24,6 +24,7 @@ from blueprintcompiler.language.values import StringValue from .attributes import BaseAttribute from .gobject_object import Object, ObjectContent from .common import * +from .values import Translated, QuotedLiteral from .contexts import ValueTypeCtx @@ -50,6 +51,14 @@ class Menu(AstNode): raise CompileError("Menu requires an ID") +class MenuValue(AstNode): + grammar = AnyOf(QuotedLiteral, Translated) + + @property + def child(self) -> T.Union[QuotedLiteral, Translated]: + return self.children[0] + + class MenuAttribute(AstNode): tag_name = "attribute" @@ -63,22 +72,10 @@ class MenuAttribute(AstNode): @context(ValueTypeCtx) def value_type(self) -> ValueTypeCtx: - return ValueTypeCtx( - None, binding_error=CompileError("Bindings are not permitted in menus") - ) + return ValueTypeCtx(None) -menu_contents = Sequence() - -menu_section = Group( - Menu, - ["section", UseLiteral("tag", "section"), Optional(UseIdent("id")), menu_contents], -) - -menu_submenu = Group( - Menu, - ["submenu", UseLiteral("tag", "submenu"), Optional(UseIdent("id")), menu_contents], -) +menu_child = AnyOf() menu_attribute = Group( MenuAttribute, @@ -90,12 +87,33 @@ menu_attribute = Group( ], ) +menu_section = Group( + Menu, + [ + "section", + UseLiteral("tag", "section"), + Optional(UseIdent("id")), + Match("{").expected(), + Until(AnyOf(menu_child, menu_attribute), "}"), + ], +) + +menu_submenu = Group( + Menu, + [ + "submenu", + UseLiteral("tag", "submenu"), + Optional(UseIdent("id")), + Match("{").expected(), + Until(AnyOf(menu_child, menu_attribute), "}"), + ], +) + menu_item = Group( Menu, [ "item", UseLiteral("tag", "item"), - Optional(UseIdent("id")), Match("{").expected(), Until(menu_attribute, "}"), ], @@ -137,18 +155,11 @@ menu_item_shorthand = Group( ], ) -menu_contents.children = [ - Match("{"), - Until( - AnyOf( - menu_section, - menu_submenu, - menu_item_shorthand, - menu_item, - menu_attribute, - ), - "}", - ), +menu_child.children = [ + menu_section, + menu_submenu, + menu_item_shorthand, + menu_item, ] menu: Group = Group( diff --git a/tests/sample_errors/menu_toplevel_attribute.blp b/tests/sample_errors/menu_toplevel_attribute.blp index e9923c2..05ae13e 100644 --- a/tests/sample_errors/menu_toplevel_attribute.blp +++ b/tests/sample_errors/menu_toplevel_attribute.blp @@ -1,5 +1,5 @@ using Gtk 4.0; menu menu { - not-allowed: true; + not-allowed: "true"; } \ No newline at end of file diff --git a/tests/sample_errors/menu_toplevel_attribute.err b/tests/sample_errors/menu_toplevel_attribute.err index 45713cf..8f3ef26 100644 --- a/tests/sample_errors/menu_toplevel_attribute.err +++ b/tests/sample_errors/menu_toplevel_attribute.err @@ -1,2 +1,2 @@ -4,5,19,Attributes are not permitted at the top level of a menu -4,16,8,Unexpected tokens \ No newline at end of file +4,5,21,Attributes are not permitted at the top level of a menu +4,16,10,Unexpected tokens \ No newline at end of file diff --git a/tests/samples/menu.blp b/tests/samples/menu.blp index 4c52bab..4d4b14d 100644 --- a/tests/samples/menu.blp +++ b/tests/samples/menu.blp @@ -7,7 +7,7 @@ menu my-menu { } item { - label: "test item"; + label: C_("context", "test translated item"); } item ("test item shorthand 1") diff --git a/tests/samples/menu.ui b/tests/samples/menu.ui index 3ba1fed..3b19896 100644 --- a/tests/samples/menu.ui +++ b/tests/samples/menu.ui @@ -7,7 +7,7 @@ test section - test item + test translated item test item shorthand 1 diff --git a/tests/samples/menu_dec.blp b/tests/samples/menu_dec.blp index 64a6021..14b4df3 100644 --- a/tests/samples/menu_dec.blp +++ b/tests/samples/menu_dec.blp @@ -7,7 +7,7 @@ menu my-menu { } item { - label: "test item"; + label: C_("context", "test translated item"); } item {