mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
119 lines
2.6 KiB
Python
119 lines
2.6 KiB
Python
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,
|
|
]
|