cobalt
cobalt copied to clipboard
Annotation parameters
Looking at the AST, it looks like annotations do not currently support parameters, whereas Java, for example does allow them:
@Foo(bar = false, baz = "foobar")
I don't know if this was an intentional decision, but if not, we should consider adding annotation parameters.
Are there any other formats we need to consider?
E,g,
@Retention(RetentionPolicy.RUNTIME)
I believe that when applying annotations, key-value pair and ordered parameters are the only ones. We should also keep in mind that arrays can also be instantiated here:
@Friend(connections = {"school", "neighborhood", "art club"})
If these are the only cases then we could use the statement parser. This is if we are happy to throw errors during semantic analysis instead if the wrong code is input there. E.g.
sepBy statementParser ","
The alternative is to specify the exact code that can be parsed there. E.g.
sepBy ((ExprAsStmt <$> nestedExpressionParser) <|> assignParser) ","
It looks like the parameters must be key-value pairs except in the unique case that there is a single parameter named value
(according to the Java tutorial).
This means the parser should accept either exactly one non-key-value parameter or any number of paired parameters.
They also have this?
@SuppressWarnings("unchecked")
So the current cases we know of are this.
@Annotation
@Annotation("value")
@Annotation(key = "value")
@Annotation(ClassName.varName)
I feel we need to find a list defining everything that can be put in it as we may miss some otherwise. It looks as though it will be best to use the statement parser for this to guarantee correct parsing. If there are any issues we can throw errors during analysis.
The Java specification provides the following grammar for annotations:
Annotation:
NormalAnnotation
MarkerAnnotation
SingleElementAnnotation
NormalAnnotation:
@ TypeName ( [ElementValuePairList] )
ElementValuePairList:
ElementValuePair {, ElementValuePair}
ElementValuePair:
Identifier = ElementValue
ElementValue:
ConditionalExpression
ElementValueArrayInitializer
Annotation
ElementValueArrayInitializer:
{ [ElementValueList] [,] }
ElementValueList:
ElementValue {, ElementValue}
MarkerAnnotation:
@ TypeName
SingleElementAnnotation:
@ TypeName ( ElementValue )
Great thank you! The next step is for us to decide the best format/syntax we would like. If you have any ideas let me know. I'll be looking into how Scala does it but it could be worth looking at other languages too to get some inspiration.
I think Scala is almost exactly the same, only slightly more relaxed, allowing the first ElementValuePair
in ElementValuePairList
to be ElementValue
, as in:
@Juice(Fruit.APPLE, fromConcentrate=true)
Python's decorators use a similar @
-syntax.
CLI languages apparently have metadata in this syntax:
[AttributeName(optional parameter, optional name=value pairs)]
Examples from Wikipedia:
[Custom]
[Custom(1)]
[Custom(1, Comment="yes")]
[Orange]
public int ExampleMethod(string input)
{
//method body goes here
}