Add StringValue

Makes the grammar more specific in a few places that take only a string
literal or translated string.
This commit is contained in:
James Westman 2023-04-11 21:26:37 -05:00
parent 5bfed72674
commit ac2a7d9282
8 changed files with 66 additions and 42 deletions

View file

@ -37,6 +37,7 @@ from .values import (
NumberLiteral,
ObjectValue,
QuotedLiteral,
StringValue,
Translated,
TranslatedWithContext,
TranslatedWithoutContext,

View file

@ -22,14 +22,14 @@ from ..decompiler import truthy, decompile_translatable
from .common import *
from .contexts import ValueTypeCtx
from .gobject_object import ObjectContent, validate_parent_type
from .values import QuotedLiteral, Translated
from .values import StringValue
class Response(AstNode):
grammar = [
UseIdent("id"),
Match(":").expected(),
AnyOf(QuotedLiteral, Translated).expected("a value"),
to_parse_node(StringValue).expected("a string or translatable string"),
ZeroOrMore(
AnyOf(Keyword("destructive"), Keyword("suggested"), Keyword("disabled"))
),
@ -52,7 +52,7 @@ class Response(AstNode):
return "disabled" not in self.tokens
@property
def value(self) -> T.Union[QuotedLiteral, Translated]:
def value(self) -> StringValue:
return self.children[0]
@context(ValueTypeCtx)

View file

@ -22,7 +22,7 @@ from .attributes import BaseTypedAttribute
from .gobject_object import ObjectContent, validate_parent_type
from .common import *
from .contexts import ValueTypeCtx
from .values import Value
from .values import StringValue
class Item(AstNode):
@ -31,12 +31,8 @@ class Item(AstNode):
return self.tokens["name"]
@property
def value(self) -> Value:
return self.children[Value][0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(StringType())
def value(self) -> StringValue:
return self.children[StringValue][0]
item = Group(
@ -48,7 +44,7 @@ item = Group(
":",
]
),
Value,
StringValue,
],
)

View file

@ -19,7 +19,7 @@
import typing as T
from blueprintcompiler.language.values import Value
from blueprintcompiler.language.values import StringValue
from .attributes import BaseAttribute
from .gobject_object import Object, ObjectContent
@ -58,8 +58,8 @@ class MenuAttribute(AstNode):
return self.tokens["name"]
@property
def value(self) -> Value:
return self.children[Value][0]
def value(self) -> StringValue:
return self.children[StringValue][0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
@ -85,7 +85,7 @@ menu_attribute = Group(
[
UseIdent("name"),
":",
Err(Value, "Expected a value"),
Err(StringValue, "Expected string or translated string"),
Match(";").expected(),
],
)
@ -109,7 +109,7 @@ menu_item_shorthand = Group(
"(",
Group(
MenuAttribute,
[UseLiteral("name", "label"), Value],
[UseLiteral("name", "label"), StringValue],
),
Optional(
[
@ -118,14 +118,14 @@ menu_item_shorthand = Group(
[
Group(
MenuAttribute,
[UseLiteral("name", "action"), Value],
[UseLiteral("name", "action"), StringValue],
),
Optional(
[
",",
Group(
MenuAttribute,
[UseLiteral("name", "icon"), Value],
[UseLiteral("name", "icon"), StringValue],
),
]
),

View file

@ -20,17 +20,16 @@
from .attributes import BaseTypedAttribute
from .gobject_object import ObjectContent, validate_parent_type
from .values import Value, Translated
from .values import StringValue
from .common import *
from .contexts import ValueTypeCtx
class Item(AstNode):
grammar = Value
grammar = StringValue
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(StringType())
@property
def child(self) -> StringValue:
return self.children[StringValue][0]
class Strings(AstNode):

View file

@ -368,3 +368,24 @@ class Value(AstNode):
self,
) -> T.Union[PropertyBinding, Binding, Translated, ObjectValue, Flags, Literal]:
return self.children[0]
class StringValue(AstNode):
grammar = AnyOf(Translated, QuotedLiteral)
@property
def child(
self,
) -> T.Union[Translated, QuotedLiteral]:
return self.children[0]
@property
def string(self) -> str:
if isinstance(self.child, Translated):
return self.child.child.string
else:
return self.child.value
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(StringType())