implement variant parsing

This commit is contained in:
jgcodes2020 2024-12-24 08:50:37 -05:00
parent bc10ccee0c
commit be667a2b9c
4 changed files with 139 additions and 16 deletions

View file

@ -137,7 +137,10 @@ menu_attribute = Group(
[
UseIdent("name"),
":",
Err(AnyOf(StringValue, VariantValue), "Expected string or translated string"),
Err(
AnyOf(StringValue, VariantValue),
"Expected string, translated string, or variant",
),
Match(";").expected(),
],
)

View file

@ -26,6 +26,7 @@ from .common import *
from .contexts import ScopeCtx, ValueTypeCtx
from .gobject_object import Object
from .types import TypeName
from .variant import VarContent
class Translated(AstNode):
@ -373,7 +374,7 @@ class IdentLiteral(AstNode):
class VariantValue(AstNode):
grammar = ["variant", "<", UseQuoted("type"), ">", "(", UseQuoted("value"), ")"]
grammar = ["variant", "<", UseQuoted("type"), ">", "(", VarContent, ")"]
@property
def var_type(self) -> str:
@ -381,7 +382,7 @@ class VariantValue(AstNode):
@property
def var_value(self) -> str:
return self.tokens["value"]
return self.children[0].content
@validate()
def validate_for_type(self) -> None:

View file

@ -0,0 +1,119 @@
import typing as T
from blueprintcompiler.gir import ArrayType
from blueprintcompiler.lsp_utils import SemanticToken
from .common import *
from .contexts import ScopeCtx, ValueTypeCtx
from .gobject_object import Object
from .types import TypeName
VAR_CONTENT_HOOKS = []
class VarContent(AstNode):
grammar = AnyOf(*VAR_CONTENT_HOOKS)
@property
def content(self) -> str:
return self.children[0].content
class VarContentBool(AstNode):
grammar = AnyOf(
[Keyword("true"), UseLiteral("value", True)],
[Keyword("false"), UseLiteral("value", False)],
)
@property
def content(self) -> str:
if self.tokens["value"]:
return "true"
else:
return "false"
class VarContentString(AstNode):
grammar = UseQuoted("value")
@property
def content(self) -> str:
return self.tokens["value"]
class VarContentNumber(AstNode):
grammar = UseNumberText("value")
@property
def content(self) -> str:
return self.tokens["value"]
class VarContentTuple(AstNode):
grammar = ["(", Delimited(VarContent, ","), ")"]
@property
def content(self) -> str:
inner = ", ".join(child.content for child in self.children)
return f"({inner})"
class VarContentArray(AstNode):
grammar = ["[", Delimited(VarContent, ","), "]"]
@property
def content(self) -> str:
inner = ", ".join(child.content for child in self.children)
return f"[{inner}]"
class VarContentDictEntry(AstNode):
grammar = ["{", VarContent, ",", VarContent, "}"]
@property
def content(self):
return f"{{{self.children[0].content}, {self.children[1].content}}}"
class VarContentDict(AstNode):
grammar = ["{", Delimited([VarContent, ":", VarContent], ","), "}"]
@property
def content(self) -> str:
inner = ", ".join(child.content for child in self.children)
return f"{{{inner}}}"
class VarContentVariant(AstNode):
grammar = ["<", VarContent, ">"]
@property
def content(self) -> str:
return f"<{self.children[0].content}>"
class VarContentMaybe(AstNode):
grammar = AnyOf(
[Keyword("just"), VarContent],
[Keyword("nothing")],
)
@property
def content(self) -> str:
if self.children[0] is not None:
return f"just {self.children[0].content}"
else:
return "nothing"
VarContent.grammar.children = [
VarContentString,
VarContentNumber,
VarContentBool,
VarContentMaybe,
VarContentTuple,
VarContentDict,
VarContentDictEntry,
VarContentArray,
VarContentVariant,
]

View file

@ -1,16 +1,16 @@
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");
$BlueprintTestObject {
// test-one: variant<"s">("one");
test-zero: variant<"b">(true);
test-one: variant<"s">("one");
test-two: variant<"i">(2);
test-three: variant<"(ii)">((3, 4));
test-four: variant<"ai">([5, 6]);
test-five: variant<"{sv}">({"key", <"value">});
test-six: variant<"a{ss}">({
"GLib": "2.24",
"Gtk": "4.16"
});
test-seven: variant<"ams">([just "2", nothing]);
}