Change the way values work

Change the parsing for values to make them more reusable, in particular
for when I implement extensions.
This commit is contained in:
James Westman 2023-01-12 13:19:15 -06:00
parent 6938267952
commit 1df46b5a06
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6
30 changed files with 707 additions and 291 deletions

View file

@ -1,4 +1,6 @@
from .attributes import BaseAttribute, BaseTypedAttribute
from .binding import Binding
from .contexts import ValueTypeCtx
from .expression import CastExpr, ClosureExpr, Expr, ExprChain, IdentExpr, LookupOp
from .gobject_object import Object, ObjectContent
from .gobject_property import Property
@ -14,16 +16,21 @@ from .gtk_styles import Styles
from .gtkbuilder_child import Child
from .gtkbuilder_template import Template
from .imports import GtkDirective, Import
from .property_binding import PropertyBinding
from .ui import UI
from .types import ClassName
from .values import (
TypeValue,
IdentValue,
TranslatedStringValue,
FlagsValue,
Flag,
QuotedValue,
NumberValue,
Flags,
IdentLiteral,
Literal,
NumberLiteral,
ObjectValue,
QuotedLiteral,
Translated,
TranslatedWithContext,
TranslatedWithoutContext,
TypeLiteral,
Value,
)
@ -43,12 +50,3 @@ OBJECT_CONTENT_HOOKS.children = [
Strings,
Child,
]
VALUE_HOOKS.children = [
TypeValue,
TranslatedStringValue,
FlagsValue,
IdentValue,
QuotedValue,
NumberValue,
]

View file

@ -18,7 +18,7 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
from .values import Value, TranslatedStringValue
from .values import Value, Translated
from .common import *

View file

@ -0,0 +1,55 @@
# binding.py
#
# Copyright 2023 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from dataclasses import dataclass
from .common import *
from .expression import ExprChain, LookupOp, IdentExpr
from .contexts import ValueTypeCtx
class Binding(AstNode):
grammar = [
Keyword("bind"),
ExprChain,
]
@property
def expression(self) -> ExprChain:
return self.children[ExprChain][0]
@property
def simple_binding(self) -> T.Optional["SimpleBinding"]:
if isinstance(self.expression.last, LookupOp):
if isinstance(self.expression.last.lhs, IdentExpr):
return SimpleBinding(
self.expression.last.lhs.ident, self.expression.last.property_name
)
return None
@validate("bind")
def not_bindable(self) -> None:
if binding_error := self.context[ValueTypeCtx].binding_error:
raise binding_error
@dataclass
class SimpleBinding:
source: str
property_name: str

View file

@ -19,7 +19,7 @@
from .. import gir
from ..ast_utils import AstNode, validate, docs
from ..ast_utils import AstNode, validate, docs, context
from ..errors import (
CompileError,
MultipleErrors,
@ -44,4 +44,3 @@ from ..parse_tree import *
OBJECT_CONTENT_HOOKS = AnyOf()
VALUE_HOOKS = AnyOf()

View file

@ -0,0 +1,28 @@
# contexts.py
#
# Copyright 2023 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
import typing as T
from dataclasses import dataclass
from .common import *
@dataclass
class ValueTypeCtx:
value_type: T.Optional[GirType]
binding_error: T.Optional[CompileError] = None

View file

@ -20,6 +20,7 @@
from .common import *
from .types import TypeName
from .gtkbuilder_template import Template
expr = Pratt()
@ -30,6 +31,10 @@ class Expr(AstNode):
def type(self) -> T.Optional[GirType]:
raise NotImplementedError()
@property
def type_complete(self) -> bool:
return True
@property
def rhs(self) -> T.Optional["Expr"]:
if isinstance(self.parent, ExprChain):
@ -53,6 +58,10 @@ class ExprChain(Expr):
def type(self) -> T.Optional[GirType]:
return self.last.type
@property
def type_complete(self) -> bool:
return self.last.type_complete
class InfixExpr(Expr):
@property
@ -83,6 +92,13 @@ class IdentExpr(Expr):
else:
return None
@property
def type_complete(self) -> bool:
if object := self.root.objects_by_id.get(self.ident):
return not isinstance(object, Template)
else:
return True
class LookupOp(InfixExpr):
grammar = [".", UseIdent("property")]
@ -103,17 +119,24 @@ class LookupOp(InfixExpr):
@validate("property")
def property_exists(self):
if self.lhs.type is None or isinstance(self.lhs.type, UncheckedType):
if (
self.lhs.type is None
or not self.lhs.type_complete
or isinstance(self.lhs.type, UncheckedType)
):
return
elif not isinstance(self.lhs.type, gir.Class) and not isinstance(
self.lhs.type, gir.Interface
):
raise CompileError(
f"Type {self.lhs.type.full_name} does not have properties"
)
elif self.lhs.type.properties.get(self.property_name) is None:
raise CompileError(
f"{self.lhs.type.full_name} does not have a property called {self.property_name}"
f"{self.lhs.type.full_name} does not have a property called {self.property_name}",
did_you_mean=(self.property_name, self.lhs.type.properties.keys()),
)
@ -124,9 +147,13 @@ class CastExpr(InfixExpr):
def type(self) -> T.Optional[GirType]:
return self.children[TypeName][0].gir_type
@property
def type_complete(self) -> bool:
return True
@validate()
def cast_makes_sense(self):
if self.lhs.type is None:
if self.type is None or self.lhs.type is None:
return
if not self.type.assignable_to(self.lhs.type):

View file

@ -17,51 +17,28 @@
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from dataclasses import dataclass
from .expression import ExprChain
from .gobject_object import Object
from .gtkbuilder_template import Template
from .values import Value, TranslatedStringValue
from .values import Value, Translated
from .common import *
from .contexts import ValueTypeCtx
from .property_binding import PropertyBinding
from .binding import Binding
class Property(AstNode):
grammar = AnyOf(
[
UseIdent("name"),
":",
Keyword("bind"),
UseIdent("bind_source"),
".",
UseIdent("bind_property"),
ZeroOrMore(
AnyOf(
["no-sync-create", UseLiteral("no_sync_create", True)],
["inverted", UseLiteral("inverted", True)],
["bidirectional", UseLiteral("bidirectional", True)],
Match("sync-create").warn(
"sync-create is deprecated in favor of no-sync-create"
),
)
),
";",
],
Statement(
UseIdent("name"),
UseLiteral("binding", True),
":",
"bind",
ExprChain,
),
Statement(
UseIdent("name"),
":",
AnyOf(
Object,
VALUE_HOOKS,
).expected("a value"),
),
)
grammar = [UseIdent("name"), ":", Value, ";"]
@property
def name(self) -> str:
return self.tokens["name"]
@property
def value(self) -> Value:
return self.children[0]
@property
def gir_class(self):
@ -72,10 +49,29 @@ class Property(AstNode):
if self.gir_class is not None and not isinstance(self.gir_class, UncheckedType):
return self.gir_class.properties.get(self.tokens["name"])
@property
def value_type(self):
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
if (
(
isinstance(self.value.child, PropertyBinding)
or isinstance(self.value.child, Binding)
)
and self.gir_property is not None
and self.gir_property.construct_only
):
binding_error = CompileError(
f"{self.gir_property.full_name} can't be bound because it is construct-only",
hints=["construct-only properties may only be set to a static value"],
)
else:
binding_error = None
if self.gir_property is not None:
return self.gir_property.type
type = self.gir_property.type
else:
type = None
return ValueTypeCtx(type, binding_error)
@validate("name")
def property_exists(self):
@ -95,40 +91,11 @@ class Property(AstNode):
did_you_mean=(self.tokens["name"], self.gir_class.properties.keys()),
)
@validate("bind")
def property_bindable(self):
if (
self.tokens["bind"]
and self.gir_property is not None
and self.gir_property.construct_only
):
raise CompileError(
f"{self.gir_property.full_name} can't be bound because it is construct-only",
hints=["construct-only properties may only be set to a static value"],
)
@validate("name")
def property_writable(self):
if self.gir_property is not None and not self.gir_property.writable:
raise CompileError(f"{self.gir_property.full_name} is not writable")
@validate()
def obj_property_type(self):
if len(self.children[Object]) == 0:
return
object = self.children[Object][0]
type = self.value_type
if (
object
and type
and object.gir_class
and not object.gir_class.assignable_to(type)
):
raise CompileError(
f"Cannot assign {object.gir_class.full_name} to {type.full_name}"
)
@validate("name")
def unique_in_parent(self):
self.validate_unique_in_parent(

View file

@ -82,10 +82,11 @@ class Signal(AstNode):
@validate("handler")
def old_extern(self):
if not self.tokens["extern"]:
raise UpgradeWarning(
"Use the '$' extern syntax introduced in blueprint 0.8.0",
actions=[CodeAction("Use '$' syntax", "$" + self.tokens["handler"])],
)
if self.handler is not None:
raise UpgradeWarning(
"Use the '$' extern syntax introduced in blueprint 0.8.0",
actions=[CodeAction("Use '$' syntax", "$" + self.handler)],
)
@validate("name")
def signal_exists(self):

View file

@ -21,6 +21,7 @@ from .gobject_object import ObjectContent, validate_parent_type
from .attributes import BaseTypedAttribute
from .values import Value
from .common import *
from .contexts import ValueTypeCtx
def get_property_types(gir):
@ -108,7 +109,7 @@ class A11yProperty(BaseTypedAttribute):
grammar = Statement(
UseIdent("name"),
":",
VALUE_HOOKS.expected("a value"),
Value,
)
@property
@ -129,8 +130,12 @@ class A11yProperty(BaseTypedAttribute):
return self.tokens["name"].replace("_", "-")
@property
def value_type(self) -> GirType:
return get_types(self.root.gir).get(self.tokens["name"])
def value(self) -> Value:
return self.children[0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(get_types(self.root.gir).get(self.tokens["name"]))
@validate("name")
def is_valid_property(self):
@ -161,6 +166,10 @@ class A11y(AstNode):
Until(A11yProperty, "}"),
]
@property
def properties(self) -> T.List[A11yProperty]:
return self.children[A11yProperty]
@validate("accessibility")
def container_is_widget(self):
validate_parent_type(self, "Gtk", "Widget", "accessibility properties")

View file

@ -21,15 +21,22 @@
from .attributes import BaseTypedAttribute
from .gobject_object import ObjectContent, validate_parent_type
from .common import *
from .contexts import ValueTypeCtx
from .values import Value
class Item(BaseTypedAttribute):
tag_name = "item"
attr_name = "id"
class Item(AstNode):
@property
def name(self) -> str:
return self.tokens["name"]
@property
def value_type(self):
return StringType()
def value(self) -> Value:
return self.children[Value][0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(StringType())
item = Group(
@ -41,7 +48,7 @@ item = Group(
":",
]
),
VALUE_HOOKS,
Value,
],
)

View file

@ -21,15 +21,25 @@
from .attributes import BaseAttribute
from .gobject_object import ObjectContent, validate_parent_type
from .common import *
from .contexts import ValueTypeCtx
from .values import Value
class LayoutProperty(BaseAttribute):
class LayoutProperty(AstNode):
tag_name = "property"
@property
def value_type(self):
def name(self) -> str:
return self.tokens["name"]
@property
def value(self) -> Value:
return self.children[Value][0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
# there isn't really a way to validate these
return None
return ValueTypeCtx(None)
@validate("name")
def unique_in_parent(self):
@ -41,11 +51,7 @@ class LayoutProperty(BaseAttribute):
layout_prop = Group(
LayoutProperty,
Statement(
UseIdent("name"),
":",
VALUE_HOOKS.expected("a value"),
),
Statement(UseIdent("name"), ":", Err(Value, "Expected a value")),
)

View file

@ -24,6 +24,7 @@ from blueprintcompiler.language.values import Value
from .attributes import BaseAttribute
from .gobject_object import Object, ObjectContent
from .common import *
from .contexts import ValueTypeCtx
class Menu(AstNode):
@ -49,17 +50,23 @@ class Menu(AstNode):
raise CompileError("Menu requires an ID")
class MenuAttribute(BaseAttribute):
class MenuAttribute(AstNode):
tag_name = "attribute"
@property
def value_type(self):
return None
def name(self) -> str:
return self.tokens["name"]
@property
def value(self) -> Value:
return self.children[Value][0]
@context(ValueTypeCtx)
def value_type(self) -> ValueTypeCtx:
return ValueTypeCtx(
None, binding_error=CompileError("Bindings are not permitted in menus")
)
menu_contents = Sequence()
@ -78,7 +85,7 @@ menu_attribute = Group(
[
UseIdent("name"),
":",
VALUE_HOOKS.expected("a value"),
Err(Value, "Expected a value"),
Match(";").expected(),
],
)
@ -102,7 +109,7 @@ menu_item_shorthand = Group(
"(",
Group(
MenuAttribute,
[UseLiteral("name", "label"), VALUE_HOOKS],
[UseLiteral("name", "label"), Value],
),
Optional(
[
@ -111,14 +118,14 @@ menu_item_shorthand = Group(
[
Group(
MenuAttribute,
[UseLiteral("name", "action"), VALUE_HOOKS],
[UseLiteral("name", "action"), Value],
),
Optional(
[
",",
Group(
MenuAttribute,
[UseLiteral("name", "icon"), VALUE_HOOKS],
[UseLiteral("name", "icon"), Value],
),
]
),

View file

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

View file

@ -64,6 +64,9 @@ class GtkDirective(AstNode):
self.gtk_version()
if self.tokens["version"] is not None:
return gir.get_namespace("Gtk", self.tokens["version"])
else:
# For better error handling, just assume it's 4.0
return gir.get_namespace("Gtk", "4.0")
class Import(AstNode):

View file

@ -0,0 +1,139 @@
# property_binding.py
#
# Copyright 2023 James Westman <james@jwestman.net>
#
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This file is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: LGPL-3.0-or-later
from .common import *
from .contexts import ValueTypeCtx
from .gobject_object import Object
from .gtkbuilder_template import Template
class PropertyBindingFlag(AstNode):
grammar = [
AnyOf(
UseExact("flag", "inverted"),
UseExact("flag", "bidirectional"),
UseExact("flag", "no-sync-create"),
UseExact("flag", "sync-create"),
)
]
@property
def flag(self) -> str:
return self.tokens["flag"]
@validate()
def sync_create(self):
if self.flag == "sync-create":
raise UpgradeWarning(
"'sync-create' is now the default. Use 'no-sync-create' if this is not wanted.",
actions=[CodeAction("remove 'sync-create'", "")],
)
class PropertyBinding(AstNode):
grammar = AnyOf(
[
Keyword("bind-property"),
UseIdent("source"),
".",
UseIdent("property"),
ZeroOrMore(PropertyBindingFlag),
],
[
Keyword("bind"),
UseIdent("source"),
".",
UseIdent("property"),
PropertyBindingFlag,
ZeroOrMore(PropertyBindingFlag),
],
)
@property
def source(self) -> str:
return self.tokens["source"]
@property
def source_obj(self) -> T.Optional[Object]:
return self.root.objects_by_id.get(self.source)
@property
def property_name(self) -> str:
return self.tokens["property"]
@property
def flags(self) -> T.List[PropertyBindingFlag]:
return self.children[PropertyBindingFlag]
@property
def inverted(self) -> bool:
return any([f.flag == "inverted" for f in self.flags])
@property
def bidirectional(self) -> bool:
return any([f.flag == "bidirectional" for f in self.flags])
@property
def no_sync_create(self) -> bool:
return any([f.flag == "no-sync-create" for f in self.flags])
@validate("source")
def source_object_exists(self) -> None:
if self.source_obj is None:
raise CompileError(
f"Could not find object with ID {self.source}",
did_you_mean=(self.source, self.root.objects_by_id.keys()),
)
@validate("property")
def property_exists(self) -> None:
if self.source_obj is None:
return
gir_class = self.source_obj.gir_class
if (
isinstance(self.source_obj, Template)
or gir_class is None
or isinstance(gir_class, UncheckedType)
):
# Objects that we have no gir data on should not be validated
# This happens for classes defined by the app itself
return
if (
isinstance(gir_class, gir.Class)
and gir_class.properties.get(self.property_name) is None
):
raise CompileError(
f"{gir_class.full_name} does not have a property called {self.property_name}"
)
@validate("bind-property")
def not_bindable(self) -> None:
if binding_error := self.context[ValueTypeCtx].binding_error:
raise binding_error
@validate("bind")
def old_bind(self):
if self.tokens["bind"]:
raise UpgradeWarning(
"Use 'bind-property', introduced in blueprint 0.8.0, to use binding flags",
actions=[CodeAction("Use 'bind-property'", "bind-property")],
)

View file

@ -94,7 +94,7 @@ class ClassName(TypeName):
@validate("namespace", "class_name")
def gir_class_exists(self):
if (
self.gir_type
self.gir_type is not None
and not isinstance(self.gir_type, UncheckedType)
and not isinstance(self.gir_type, Class)
):

View file

@ -21,41 +21,57 @@ import typing as T
from .common import *
from .types import TypeName
from .property_binding import PropertyBinding
from .binding import Binding
from .gobject_object import Object
from .contexts import ValueTypeCtx
class Value(AstNode):
pass
class TranslatedStringValue(Value):
grammar = AnyOf(
[
"_",
"(",
UseQuoted("value").expected("a quoted string"),
Match(")").expected(),
],
[
"C_",
"(",
UseQuoted("context").expected("a quoted string"),
",",
UseQuoted("value").expected("a quoted string"),
Optional(","),
Match(")").expected(),
],
)
class TranslatedWithoutContext(AstNode):
grammar = ["_", "(", UseQuoted("string"), Optional(","), ")"]
@property
def string(self) -> str:
return self.tokens["value"]
return self.tokens["string"]
class TranslatedWithContext(AstNode):
grammar = [
"C_",
"(",
UseQuoted("context"),
",",
UseQuoted("string"),
Optional(","),
")",
]
@property
def context(self) -> T.Optional[str]:
def string(self) -> str:
return self.tokens["string"]
@property
def context(self) -> str:
return self.tokens["context"]
class TypeValue(Value):
class Translated(AstNode):
grammar = AnyOf(TranslatedWithoutContext, TranslatedWithContext)
@property
def child(self) -> T.Union[TranslatedWithContext, TranslatedWithoutContext]:
return self.children[0]
@validate()
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if expected_type is not None and not expected_type.assignable_to(StringType()):
raise CompileError(
f"Cannot convert translated string to {expected_type.full_name}"
)
class TypeLiteral(AstNode):
grammar = [
"typeof",
"(",
@ -64,17 +80,17 @@ class TypeValue(Value):
]
@property
def type_name(self):
def type_name(self) -> TypeName:
return self.children[TypeName][0]
@validate()
def validate_for_type(self):
type = self.parent.value_type
if type is not None and not isinstance(type, gir.TypeType):
raise CompileError(f"Cannot convert GType to {type.full_name}")
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if expected_type is not None and not isinstance(expected_type, gir.TypeType):
raise CompileError(f"Cannot convert GType to {expected_type.full_name}")
class QuotedValue(Value):
class QuotedLiteral(AstNode):
grammar = UseQuoted("value")
@property
@ -82,22 +98,22 @@ class QuotedValue(Value):
return self.tokens["value"]
@validate()
def validate_for_type(self):
type = self.parent.value_type
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if (
isinstance(type, gir.IntType)
or isinstance(type, gir.UIntType)
or isinstance(type, gir.FloatType)
isinstance(expected_type, gir.IntType)
or isinstance(expected_type, gir.UIntType)
or isinstance(expected_type, gir.FloatType)
):
raise CompileError(f"Cannot convert string to number")
elif isinstance(type, gir.StringType):
elif isinstance(expected_type, gir.StringType):
pass
elif (
isinstance(type, gir.Class)
or isinstance(type, gir.Interface)
or isinstance(type, gir.Boxed)
isinstance(expected_type, gir.Class)
or isinstance(expected_type, gir.Interface)
or isinstance(expected_type, gir.Boxed)
):
parseable_types = [
"Gdk.Paintable",
@ -111,31 +127,32 @@ class QuotedValue(Value):
"Gsk.Transform",
"GLib.Variant",
]
if type.full_name not in parseable_types:
if expected_type.full_name not in parseable_types:
hints = []
if isinstance(type, gir.TypeType):
hints.append(
f"use the typeof operator: 'typeof({self.tokens('value')})'"
)
if isinstance(expected_type, gir.TypeType):
hints.append(f"use the typeof operator: 'typeof({self.value})'")
raise CompileError(
f"Cannot convert string to {type.full_name}", hints=hints
f"Cannot convert string to {expected_type.full_name}", hints=hints
)
elif type is not None:
raise CompileError(f"Cannot convert string to {type.full_name}")
elif expected_type is not None:
raise CompileError(f"Cannot convert string to {expected_type.full_name}")
class NumberValue(Value):
grammar = UseNumber("value")
class NumberLiteral(AstNode):
grammar = [
Optional(AnyOf(UseExact("sign", "-"), UseExact("sign", "+"))),
UseNumber("value"),
]
@property
def value(self) -> T.Union[int, float]:
return self.tokens["value"]
@validate()
def validate_for_type(self):
type = self.parent.value_type
if isinstance(type, gir.IntType):
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if isinstance(expected_type, gir.IntType):
try:
int(self.tokens["value"])
except:
@ -143,7 +160,7 @@ class NumberValue(Value):
f"Cannot convert {self.group.tokens['value']} to integer"
)
elif isinstance(type, gir.UIntType):
elif isinstance(expected_type, gir.UIntType):
try:
int(self.tokens["value"])
if int(self.tokens["value"]) < 0:
@ -153,7 +170,7 @@ class NumberValue(Value):
f"Cannot convert {self.group.tokens['value']} to unsigned integer"
)
elif isinstance(type, gir.FloatType):
elif isinstance(expected_type, gir.FloatType):
try:
float(self.tokens["value"])
except:
@ -161,8 +178,8 @@ class NumberValue(Value):
f"Cannot convert {self.group.tokens['value']} to float"
)
elif type is not None:
raise CompileError(f"Cannot convert number to {type.full_name}")
elif expected_type is not None:
raise CompileError(f"Cannot convert number to {expected_type.full_name}")
class Flag(AstNode):
@ -174,17 +191,17 @@ class Flag(AstNode):
@property
def value(self) -> T.Optional[int]:
type = self.parent.parent.value_type
type = self.context[ValueTypeCtx].value_type
if not isinstance(type, Enumeration):
return None
elif member := type.members.get(self.tokens["value"]):
elif member := type.members.get(self.name):
return member.value
else:
return None
@docs()
def docs(self):
type = self.parent.parent.value_type
type = self.context[ValueTypeCtx].value_type
if not isinstance(type, Enumeration):
return
if member := type.members.get(self.tokens["value"]):
@ -192,15 +209,18 @@ class Flag(AstNode):
@validate()
def validate_for_type(self):
type = self.parent.parent.value_type
if isinstance(type, gir.Bitfield) and self.tokens["value"] not in type.members:
expected_type = self.context[ValueTypeCtx].value_type
if (
isinstance(expected_type, gir.Bitfield)
and self.tokens["value"] not in expected_type.members
):
raise CompileError(
f"{self.tokens['value']} is not a member of {type.full_name}",
did_you_mean=(self.tokens["value"], type.members.keys()),
f"{self.tokens['value']} is not a member of {expected_type.full_name}",
did_you_mean=(self.tokens["value"], expected_type.members.keys()),
)
class FlagsValue(Value):
class Flags(AstNode):
grammar = [Flag, "|", Delimited(Flag, "|")]
@property
@ -208,57 +228,104 @@ class FlagsValue(Value):
return self.children
@validate()
def parent_is_bitfield(self):
type = self.parent.value_type
if type is not None and not isinstance(type, gir.Bitfield):
raise CompileError(f"{type.full_name} is not a bitfield type")
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if expected_type is not None and not isinstance(expected_type, gir.Bitfield):
raise CompileError(f"{expected_type.full_name} is not a bitfield type")
class IdentValue(Value):
class IdentLiteral(AstNode):
grammar = UseIdent("value")
@property
def ident(self) -> str:
return self.tokens["value"]
@validate()
def validate_for_type(self):
type = self.parent.value_type
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if isinstance(expected_type, gir.BoolType):
if self.ident not in ["true", "false"]:
raise CompileError(f"Expected 'true' or 'false' for boolean value")
if isinstance(type, gir.Enumeration):
if self.tokens["value"] not in type.members:
elif isinstance(expected_type, gir.Enumeration):
if self.ident not in expected_type.members:
raise CompileError(
f"{self.tokens['value']} is not a member of {type.full_name}",
did_you_mean=(self.tokens["value"], type.members.keys()),
f"{self.ident} is not a member of {expected_type.full_name}",
did_you_mean=(self.ident, list(expected_type.members.keys())),
)
elif isinstance(type, gir.BoolType):
if self.tokens["value"] not in ["true", "false"]:
raise CompileError(
f"Expected 'true' or 'false' for boolean value",
did_you_mean=(self.tokens["value"], ["true", "false"]),
)
elif type is not None:
object = self.root.objects_by_id.get(self.tokens["value"])
elif expected_type is not None:
object = self.root.objects_by_id.get(self.ident)
if object is None:
raise CompileError(
f"Could not find object with ID {self.tokens['value']}",
did_you_mean=(self.tokens["value"], self.root.objects_by_id.keys()),
f"Could not find object with ID {self.ident}",
did_you_mean=(self.ident, self.root.objects_by_id.keys()),
)
elif object.gir_class and not object.gir_class.assignable_to(type):
elif object.gir_class and not object.gir_class.assignable_to(expected_type):
raise CompileError(
f"Cannot assign {object.gir_class.full_name} to {type.full_name}"
f"Cannot assign {object.gir_class.full_name} to {expected_type.full_name}"
)
@docs()
def docs(self):
type = self.parent.value_type
def docs(self) -> T.Optional[str]:
type = self.context[ValueTypeCtx].value_type
if isinstance(type, gir.Enumeration):
if member := type.members.get(self.tokens["value"]):
if member := type.members.get(self.ident):
return member.doc
else:
return type.doc
elif isinstance(type, gir.GirNode):
return type.doc
else:
return None
def get_semantic_tokens(self) -> T.Iterator[SemanticToken]:
if isinstance(self.parent.value_type, gir.Enumeration):
token = self.group.tokens["value"]
yield SemanticToken(token.start, token.end, SemanticTokenType.EnumMember)
class Literal(AstNode):
grammar = AnyOf(
TypeLiteral,
QuotedLiteral,
NumberLiteral,
IdentLiteral,
)
@property
def value(
self,
) -> T.Union[TypeLiteral, QuotedLiteral, NumberLiteral, IdentLiteral]:
return self.children[0]
class ObjectValue(AstNode):
grammar = Object
@property
def object(self) -> Object:
return self.children[Object][0]
@validate()
def validate_for_type(self) -> None:
expected_type = self.context[ValueTypeCtx].value_type
if (
expected_type is not None
and self.object.gir_class is not None
and not self.object.gir_class.assignable_to(expected_type)
):
raise CompileError(
f"Cannot assign {self.object.gir_class.full_name} to {expected_type.full_name}"
)
class Value(AstNode):
grammar = AnyOf(PropertyBinding, Binding, Translated, ObjectValue, Flags, Literal)
@property
def child(
self,
) -> T.Union[PropertyBinding, Binding, Translated, ObjectValue, Flags, Literal,]:
return self.children[0]