mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-06 16:19:07 -04:00
Merge branch 'variant-literal' into 'main'
Draft: Syntax for variants See merge request jwestman/blueprint-compiler!224
This commit is contained in:
commit
abfa977742
5 changed files with 95 additions and 14 deletions
|
@ -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
|
||||
|
|
|
@ -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(),
|
||||
],
|
||||
)
|
||||
|
|
|
@ -371,6 +371,44 @@ 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 expected_type is None:
|
||||
pass
|
||||
elif (
|
||||
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")
|
||||
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}")
|
||||
pass
|
||||
|
||||
class Literal(AstNode):
|
||||
grammar = AnyOf(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue