mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
implement variant parsing
This commit is contained in:
parent
bc10ccee0c
commit
be667a2b9c
4 changed files with 139 additions and 16 deletions
|
@ -137,7 +137,10 @@ menu_attribute = Group(
|
||||||
[
|
[
|
||||||
UseIdent("name"),
|
UseIdent("name"),
|
||||||
":",
|
":",
|
||||||
Err(AnyOf(StringValue, VariantValue), "Expected string or translated string"),
|
Err(
|
||||||
|
AnyOf(StringValue, VariantValue),
|
||||||
|
"Expected string, translated string, or variant",
|
||||||
|
),
|
||||||
Match(";").expected(),
|
Match(";").expected(),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -26,6 +26,7 @@ from .common import *
|
||||||
from .contexts import ScopeCtx, ValueTypeCtx
|
from .contexts import ScopeCtx, ValueTypeCtx
|
||||||
from .gobject_object import Object
|
from .gobject_object import Object
|
||||||
from .types import TypeName
|
from .types import TypeName
|
||||||
|
from .variant import VarContent
|
||||||
|
|
||||||
|
|
||||||
class Translated(AstNode):
|
class Translated(AstNode):
|
||||||
|
@ -373,7 +374,7 @@ class IdentLiteral(AstNode):
|
||||||
|
|
||||||
|
|
||||||
class VariantValue(AstNode):
|
class VariantValue(AstNode):
|
||||||
grammar = ["variant", "<", UseQuoted("type"), ">", "(", UseQuoted("value"), ")"]
|
grammar = ["variant", "<", UseQuoted("type"), ">", "(", VarContent, ")"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def var_type(self) -> str:
|
def var_type(self) -> str:
|
||||||
|
@ -381,7 +382,7 @@ class VariantValue(AstNode):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def var_value(self) -> str:
|
def var_value(self) -> str:
|
||||||
return self.tokens["value"]
|
return self.children[0].content
|
||||||
|
|
||||||
@validate()
|
@validate()
|
||||||
def validate_for_type(self) -> None:
|
def validate_for_type(self) -> None:
|
||||||
|
|
119
blueprintcompiler/language/variant.py
Normal file
119
blueprintcompiler/language/variant.py
Normal 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,
|
||||||
|
]
|
|
@ -1,16 +1,16 @@
|
||||||
using Gtk 4.0;
|
using Gtk 4.0;
|
||||||
|
|
||||||
menu root {
|
$BlueprintTestObject {
|
||||||
submenu {
|
// test-one: variant<"s">("one");
|
||||||
name: "one";
|
test-zero: variant<"b">(true);
|
||||||
item {
|
test-one: variant<"s">("one");
|
||||||
action: "app.foo_bar";
|
test-two: variant<"i">(2);
|
||||||
target: variant<"s">("\"one\"");
|
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",
|
||||||
Button {
|
"Gtk": "4.16"
|
||||||
action-name: "app.shave_yak";
|
});
|
||||||
action-target: variant<"y">("8");
|
test-seven: variant<"ams">([just "2", nothing]);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue