Use <> instead of () for casts & typeof

This makes it clearer that they aren't functions, and it eliminates
syntactic ambiguity with closure expressions.
This commit is contained in:
James Westman 2023-04-10 09:38:56 -05:00
parent d6bd282e58
commit 02796fd830
10 changed files with 66 additions and 11 deletions

View file

@ -74,9 +74,18 @@ class Translated(AstNode):
class TypeLiteral(AstNode):
grammar = [
"typeof",
"(",
to_parse_node(TypeName).expected("type name"),
Match(")").expected(),
AnyOf(
[
"<",
to_parse_node(TypeName).expected("type name"),
Match(">").expected(),
],
[
UseExact("lparen", "("),
to_parse_node(TypeName).expected("type name"),
UseExact("rparen", ")").expected("')'"),
],
),
]
@property
@ -93,6 +102,19 @@ class TypeLiteral(AstNode):
if expected_type is not None and not isinstance(expected_type, gir.TypeType):
raise CompileError(f"Cannot convert GType to {expected_type.full_name}")
@validate("lparen", "rparen")
def upgrade_to_angle_brackets(self):
if self.tokens["lparen"]:
raise UpgradeWarning(
"Use angle bracket syntax introduced in blueprint 0.8.0",
actions=[
CodeAction(
"Use <> instead of ()",
f"<{self.children[TypeName][0].as_string}>",
)
],
)
class QuotedLiteral(AstNode):
grammar = UseQuoted("value")