Update menu syntax

Sections and submenus can have IDs. Also, change the code to better
reflect the documented grammar.
This commit is contained in:
James Westman 2023-04-09 15:26:07 -05:00
parent 75055ac967
commit dd3c75d2c7
6 changed files with 44 additions and 33 deletions

View file

@ -24,6 +24,7 @@ from blueprintcompiler.language.values import StringValue
from .attributes import BaseAttribute from .attributes import BaseAttribute
from .gobject_object import Object, ObjectContent from .gobject_object import Object, ObjectContent
from .common import * from .common import *
from .values import Translated, QuotedLiteral
from .contexts import ValueTypeCtx from .contexts import ValueTypeCtx
@ -50,6 +51,14 @@ class Menu(AstNode):
raise CompileError("Menu requires an ID") 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): class MenuAttribute(AstNode):
tag_name = "attribute" tag_name = "attribute"
@ -63,22 +72,10 @@ class MenuAttribute(AstNode):
@context(ValueTypeCtx) @context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx: def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx( return ValueTypeCtx(None)
None, binding_error=CompileError("Bindings are not permitted in menus")
)
menu_contents = Sequence() menu_child = AnyOf()
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_attribute = Group( menu_attribute = Group(
MenuAttribute, 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 = Group(
Menu, Menu,
[ [
"item", "item",
UseLiteral("tag", "item"), UseLiteral("tag", "item"),
Optional(UseIdent("id")),
Match("{").expected(), Match("{").expected(),
Until(menu_attribute, "}"), Until(menu_attribute, "}"),
], ],
@ -137,18 +155,11 @@ menu_item_shorthand = Group(
], ],
) )
menu_contents.children = [ menu_child.children = [
Match("{"),
Until(
AnyOf(
menu_section, menu_section,
menu_submenu, menu_submenu,
menu_item_shorthand, menu_item_shorthand,
menu_item, menu_item,
menu_attribute,
),
"}",
),
] ]
menu: Group = Group( menu: Group = Group(

View file

@ -1,5 +1,5 @@
using Gtk 4.0; using Gtk 4.0;
menu menu { menu menu {
not-allowed: true; not-allowed: "true";
} }

View file

@ -1,2 +1,2 @@
4,5,19,Attributes are not permitted at the top level of a menu 4,5,21,Attributes are not permitted at the top level of a menu
4,16,8,Unexpected tokens 4,16,10,Unexpected tokens

View file

@ -7,7 +7,7 @@ menu my-menu {
} }
item { item {
label: "test item"; label: C_("context", "test translated item");
} }
item ("test item shorthand 1") item ("test item shorthand 1")

View file

@ -7,7 +7,7 @@
<attribute name="label">test section</attribute> <attribute name="label">test section</attribute>
</section> </section>
<item> <item>
<attribute name="label">test item</attribute> <attribute name="label" translatable="true" context="context">test translated item</attribute>
</item> </item>
<item> <item>
<attribute name="label">test item shorthand 1</attribute> <attribute name="label">test item shorthand 1</attribute>

View file

@ -7,7 +7,7 @@ menu my-menu {
} }
item { item {
label: "test item"; label: C_("context", "test translated item");
} }
item { item {