Skip to content

Commit

Permalink
CM KCL: add annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmundell committed Feb 7, 2025
1 parent 67e60cb commit fcfb8dc
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/codemirror-lang-kcl/src/kcl.grammar
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@precedence {
annotation
member
call
exp @left
Expand All @@ -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 { At AnnotationName AnnotationList? }
}

AnnotationList { !annotation "(" commaSep<AnnotationProperty> ")" }

ParamList { "(" commaSep<Parameter { VariableDefinition "?"? (":" type)? }> ")" }

Body { "{" statement* "}" }
Expand Down Expand Up @@ -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> ")" }
Expand All @@ -78,6 +88,8 @@ VariableName { identifier }

ArgumentLabel { identifier }

AnnotationName { identifier }

@skip { whitespace | LineComment | BlockComment }

kw<term> { @specialize[@name={term}]<identifier, term> }
Expand All @@ -92,6 +104,8 @@ commaSep1NoTrailingComma<term> { term ("," term)* }
Number { "." @digit+ | @digit+ ("." @digit+)? }
@precedence { Number, "." }

At { "@" }

AddOp { "+" | "-" }
MultOp { "/" | "*" | "\\" }
ExpOp { "^" }
Expand Down
140 changes: 140 additions & 0 deletions packages/codemirror-lang-kcl/test/annotation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# alone

@a

==>
Program(Annotation(At,
AnnotationName))

# empty

@ann()

==>
Program(Annotation(At,
AnnotationName,
AnnotationList))

# equals

@setting(a=1)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))

# operator

@ann(a*1)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))

# complex expr

@ann(a=(1+2+f('yes')))

==>
Program(Annotation(At,
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(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number),
AnnotationProperty(PropertyName,
Equals,
Number))))

# space around op

@ann(a / 1)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number))))

# space around sep

@ann(a/1 , b/2)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
MultOp,
Number),
AnnotationProperty(PropertyName,
MultOp,
Number))))

# trailing sep

@ann(a=1,)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))

# lone sep

@ann(,)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList))

# inside fn

fn f() {
@anno(b=2)
}

==>
Program(FunctionDeclaration(fn,
VariableDefinition,
ParamList,
Body(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))))

# laxer with space than the language parser is

@ anno (b=2)

==>
Program(Annotation(At,
AnnotationName,
AnnotationList(AnnotationProperty(PropertyName,
Equals,
Number))))

0 comments on commit fcfb8dc

Please sign in to comment.