cobalt icon indicating copy to clipboard operation
cobalt copied to clipboard

Annotation parameters

Open mlutze opened this issue 6 years ago • 8 comments

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.

mlutze avatar May 28 '18 23:05 mlutze

Are there any other formats we need to consider?

E,g,

@Retention(RetentionPolicy.RUNTIME)

Michael2109 avatar May 29 '18 13:05 Michael2109

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"})

mlutze avatar May 29 '18 15:05 mlutze

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) "," 

Michael2109 avatar May 29 '18 17:05 Michael2109

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.

mlutze avatar May 30 '18 04:05 mlutze

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.

Michael2109 avatar May 30 '18 13:05 Michael2109

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 )

mlutze avatar May 31 '18 02:05 mlutze

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.

Michael2109 avatar May 31 '18 09:05 Michael2109

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
}

mlutze avatar Jun 01 '18 23:06 mlutze