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

@ -159,7 +159,17 @@ class LookupOp(InfixExpr):
class CastExpr(InfixExpr):
grammar = ["as", "(", TypeName, ")"]
grammar = [
"as",
AnyOf(
["<", TypeName, Match(">").expected()],
[
UseExact("lparen", "("),
TypeName,
UseExact("rparen", ")").expected("')'"),
],
),
]
@context(ValueTypeCtx)
def value_type(self):
@ -183,6 +193,19 @@ class CastExpr(InfixExpr):
f"Invalid cast. No instance of {self.lhs.type.full_name} can be an instance of {self.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 ClosureArg(AstNode):
grammar = ExprChain