mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
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:
parent
5bfed72674
commit
ac2a7d9282
8 changed files with 66 additions and 42 deletions
|
@ -37,6 +37,7 @@ from .values import (
|
|||
NumberLiteral,
|
||||
ObjectValue,
|
||||
QuotedLiteral,
|
||||
StringValue,
|
||||
Translated,
|
||||
TranslatedWithContext,
|
||||
TranslatedWithoutContext,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
],
|
||||
)
|
||||
|
||||
|
|
|
@ -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],
|
||||
),
|
||||
]
|
||||
),
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -78,7 +78,13 @@ class XmlOutput(OutputFormat):
|
|||
if isinstance(child, Menu):
|
||||
self._emit_menu(child, xml)
|
||||
elif isinstance(child, MenuAttribute):
|
||||
self._emit_attribute("attribute", "name", child.name, child.value, xml)
|
||||
xml.start_tag(
|
||||
"attribute",
|
||||
name=child.name,
|
||||
**self._translated_string_attrs(child.value.child),
|
||||
)
|
||||
xml.put_text(child.value.string)
|
||||
xml.end_tag()
|
||||
else:
|
||||
raise CompilerBugError()
|
||||
xml.end_tag()
|
||||
|
@ -235,7 +241,12 @@ class XmlOutput(OutputFormat):
|
|||
xml.end_tag()
|
||||
|
||||
def _emit_attribute(
|
||||
self, tag: str, attr: str, name: str, value: Value, xml: XmlEmitter
|
||||
self,
|
||||
tag: str,
|
||||
attr: str,
|
||||
name: str,
|
||||
value: T.Union[Value, StringValue],
|
||||
xml: XmlEmitter,
|
||||
):
|
||||
attrs = {attr: name}
|
||||
|
||||
|
@ -243,6 +254,10 @@ class XmlOutput(OutputFormat):
|
|||
xml.start_tag(tag, **attrs, **self._translated_string_attrs(value.child))
|
||||
xml.put_text(value.child.child.string)
|
||||
xml.end_tag()
|
||||
elif isinstance(value.child, QuotedLiteral):
|
||||
xml.start_tag(tag, **attrs)
|
||||
xml.put_text(value.child.value)
|
||||
xml.end_tag()
|
||||
else:
|
||||
xml.start_tag(tag, **attrs)
|
||||
self._emit_value(value, xml)
|
||||
|
@ -282,28 +297,20 @@ class XmlOutput(OutputFormat):
|
|||
xml.start_tag(
|
||||
"response",
|
||||
id=response.id,
|
||||
**self._translated_string_attrs(response.value),
|
||||
**self._translated_string_attrs(response.value.child),
|
||||
enabled=None if response.enabled else "false",
|
||||
appearance=response.appearance,
|
||||
)
|
||||
if isinstance(response.value, Translated):
|
||||
xml.put_text(response.value.child.string)
|
||||
else:
|
||||
xml.put_text(response.value.value)
|
||||
xml.put_text(response.value.string)
|
||||
xml.end_tag()
|
||||
xml.end_tag()
|
||||
|
||||
elif isinstance(extension, Strings):
|
||||
xml.start_tag("items")
|
||||
for prop in extension.children:
|
||||
value = prop.children[Value][0]
|
||||
if isinstance(value.child, Translated):
|
||||
xml.start_tag("item", **self._translated_string_attrs(value))
|
||||
xml.put_text(value.child.child.string)
|
||||
xml.end_tag()
|
||||
else:
|
||||
xml.start_tag("item")
|
||||
self._emit_value(value, xml)
|
||||
for string in extension.children:
|
||||
value = string.child
|
||||
xml.start_tag("item", **self._translated_string_attrs(value.child))
|
||||
xml.put_text(value.string)
|
||||
xml.end_tag()
|
||||
xml.end_tag()
|
||||
elif isinstance(extension, ListItemFactory):
|
||||
|
|
|
@ -1 +1 @@
|
|||
5,5,2,Cannot assign Gtk.Button to string
|
||||
4,3,22,Unexpected tokens
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue