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