开发者

Grammar for Java's annotations

开发者 https://www.devze.com 2022-12-23 12:28 出处:网络
Is there a BNF or EBNF that describes the grammar for Ja开发者_JAVA技巧va\'s annotations?The authoritative source for Java-related grammar, is, of course, the JLS.

Is there a BNF or EBNF that describes the grammar for Ja开发者_JAVA技巧va's annotations?


The authoritative source for Java-related grammar, is, of course, the JLS.

JLS 18.1 The Grammar of the Java Programming Language

Annotations:
        Annotation [Annotations]

Annotation:
        @ TypeName [( [Identifier =] ElementValue)]

ElementValue:
        ConditionalExpression
        Annotation
        ElementValueArrayInitializer

... rest ommitted


`/* Annotation syntax follows. */

Annotation ::= NormalAnnotation | SingleMemberAnnotation | MarkerAnnotation NormalAnnotation ::= "@" Name "(" ( MemberValuePairs )? ")" MarkerAnnotation ::= "@" Name SingleMemberAnnotation ::= "@" Name "(" MemberValue ")" MemberValuePairs ::= MemberValuePair ( "," MemberValuePair )* MemberValuePair ::= "=" MemberValue MemberValue ::= Annotation | MemberValueArrayInitializer | ConditionalExpression MemberValueArrayInitializer ::= "{" ( MemberValue ( "," MemberValue )* ( "," )? )? "}"

/* Annotation Types. */

AnnotationTypeDeclaration ::= "@" "interface" AnnotationTypeBody AnnotationTypeBody ::= "{" ( AnnotationTypeMemberDeclaration )* "}" AnnotationTypeMemberDeclaration ::= Modifiers ( Type "(" ")" ( DefaultValue )? ";" | ClassOrInterfaceDeclaration | EnumDeclaration | AnnotationTypeDeclaration | FieldDeclaration ) | ( ";" ) DefaultValue ::= "default" MemberValue` from here. Also see his blog post.


Java language grammar

  1. Any Type may be prefixed by [Annotations]:

    Type: [Annotations] Identifier [TypeArguments] {. Identifier [TypeArguments]} {[]} [Annotations] BasicType

  2. To permit annotations on levels of an array (in declarations, not constructors), change “{[]}” to “{[Annotations] []}”. (This was abstracted out as “BracketsOpt” in the 2nd edition of the JLS [GJSB00].) For example:

Type: [Annotations] Identifier [TypeArguments]{ . Identifier [TypeArguments]} {[Annotations] []} [Annotations] BasicType

Also permit annotations on varargs (...): FormalParameterDeclsRest: VariableDeclaratorId [, FormalParameterDecls] [Annotations] ... VariableDeclaratorId

  1. Annotations may appear on the receiver type by changing uses of “FormalParameters” (in all 5 places it appears in the grammar) to “FormalParameters [Annotations]”. For example:

    VoidMethodDeclaratorRest: FormalParameters [Annotations] [throws QualifiedIdentifierList] ( MethodBody | ; )


This is the ANTLRv4 grammar for Java Annotations (usage and definition) from the official ANTLR GitHub repository.

Annotation usage grammar:

altAnnotationQualifiedName
    : (IDENTIFIER DOT)* '@' IDENTIFIER
    ;


annotation
    : ('@' qualifiedName | altAnnotationQualifiedName) ('(' ( elementValuePairs | elementValue )? ')')?
    ;


elementValuePairs
    : elementValuePair (',' elementValuePair)*
    ;


elementValuePair
    : IDENTIFIER '=' elementValue
    ;


elementValue
    : expression
    | annotation
    | elementValueArrayInitializer
    ;


elementValueArrayInitializer
    : '{' (elementValue (',' elementValue)*)? (',')? '}'
    ;

Annotation declaration grammar:

annotationTypeDeclaration
    : '@' INTERFACE IDENTIFIER annotationTypeBody
    ;


annotationTypeBody
    : '{' (annotationTypeElementDeclaration)* '}'
    ;


annotationTypeElementDeclaration
    : modifier* annotationTypeElementRest
    | ';' // this is not allowed by the grammar, but apparently allowed by the actual compiler
    ;


annotationTypeElementRest
    : typeType annotationMethodOrConstantRest ';'
    | classDeclaration ';'?
    | interfaceDeclaration ';'?
    | enumDeclaration ';'?
    | annotationTypeDeclaration ';'?
    ;


annotationMethodOrConstantRest
    : annotationMethodRest
    | annotationConstantRest
    ;


annotationMethodRest
    : IDENTIFIER '(' ')' defaultValue?
    ;


annotationConstantRest
    : variableDeclarators
    ;


defaultValue
    : DEFAULT elementValue
    ;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号