mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
implement variant parsing
This commit is contained in:
parent
bc10ccee0c
commit
be667a2b9c
4 changed files with 139 additions and 16 deletions
119
blueprintcompiler/language/variant.py
Normal file
119
blueprintcompiler/language/variant.py
Normal file
|
@ -0,0 +1,119 @@
|
|||
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,
|
||||
]
|
Loading…
Add table
Add a link
Reference in a new issue