CM KCL: add annotations (#5374)

* CM KCL: add annotations

* Make AnnotationName a token that includes the @

* The text of AnnotationName is now optional (#5324)

---------

Co-authored-by: Matt Mundell <matt@mundell.me>
This commit is contained in:
Jonathan Tran
2025-02-13 14:28:19 -05:00
committed by GitHub
parent 49d52ce94b
commit 5d02a27122
2 changed files with 165 additions and 1 deletions

View File

@ -1,4 +1,5 @@
@precedence { @precedence {
annotation
member member
call call
exp @left exp @left
@ -20,9 +21,12 @@ statement[@isGroup=Statement] {
FunctionDeclaration { kw<"export">? kw<"fn"> VariableDefinition Equals? ParamList Arrow? Body } | FunctionDeclaration { kw<"export">? kw<"fn"> VariableDefinition Equals? ParamList Arrow? Body } |
VariableDeclaration { kw<"export">? (kw<"var"> | kw<"let"> | kw<"const">)? VariableDefinition Equals expression } | VariableDeclaration { kw<"export">? (kw<"var"> | kw<"let"> | kw<"const">)? VariableDefinition Equals expression } |
ReturnStatement { kw<"return"> expression } | ReturnStatement { kw<"return"> expression } |
ExpressionStatement { expression } ExpressionStatement { expression } |
Annotation { AnnotationName AnnotationList? }
} }
AnnotationList { !annotation "(" commaSep<AnnotationProperty> ")" }
ParamList { "(" commaSep<Parameter { VariableDefinition "?"? (":" type)? }> ")" } ParamList { "(" commaSep<Parameter { VariableDefinition "?"? (":" type)? }> ")" }
Body { "{" statement* "}" } Body { "{" statement* "}" }
@ -59,6 +63,12 @@ UnaryOp { AddOp | BangOp }
ObjectProperty { PropertyName (":" | Equals) expression } ObjectProperty { PropertyName (":" | Equals) expression }
AnnotationProperty {
PropertyName
( AddOp | MultOp | ExpOp | LogicOp | BangOp | CompOp | Equals | Arrow | PipeOperator | PipeSubstitution )
expression
}
LabeledArgument { ArgumentLabel Equals expression } LabeledArgument { ArgumentLabel Equals expression }
ArgumentList { "(" commaSep<LabeledArgument | expression> ")" } ArgumentList { "(" commaSep<LabeledArgument | expression> ")" }
@ -105,6 +115,7 @@ commaSep1NoTrailingComma<term> { term ("," term)* }
PipeSubstitution { "%" } PipeSubstitution { "%" }
identifier { (@asciiLetter | "_") (@asciiLetter | @digit | "_")* } identifier { (@asciiLetter | "_") (@asciiLetter | @digit | "_")* }
AnnotationName { "@" identifier? }
PropertyName { identifier } PropertyName { identifier }
TagDeclarator { "$" identifier } TagDeclarator { "$" identifier }

View File

@ -0,0 +1,153 @@
# alone
@a
==>
Program(Annotation(AnnotationName))
# alone and anonymous
@
==>
Program(Annotation(AnnotationName))
# empty
@ann()
==>
Program(Annotation(AnnotationName,
AnnotationList))
# empty and anonymous
@()
==>
Program(Annotation(AnnotationName,
AnnotationList))
# equals
@setting(a=1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# operator
@ann(a*1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))
# anonymous
@(a=1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# complex expr
@ann(a=(1+2+f('yes')))
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
ParenthesizedExpression(BinaryExpression(BinaryExpression(Number,
AddOp,
Number),
AddOp,
CallExpression(VariableName,
ArgumentList(String))))))))
# many args
@ann(a=1, b=2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number),
AnnotationProperty(PropertyName,
Equals,
Number))))
# space around op
@ann(a / 1)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))
# space around sep
@ann(a/1 , b/2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number),
AnnotationProperty(PropertyName,
MultOp,
Number))))
# trailing sep
@ann(a=1,)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))
# lone sep
@ann(,)
==>
Program(Annotation(AnnotationName,
AnnotationList))
# inside fn
fn f() {
@anno(b=2)
}
==>
Program(FunctionDeclaration(fn,
VariableDefinition,
ParamList,
Body(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))))
# laxer with space than the language parser is
@anno (b=2)
==>
Program(Annotation(AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))