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:
Also permit annotations on varargs (...):
FormalParameterDeclsRest:
VariableDeclaratorId [, FormalParameterDecls]
[Annotations] ... VariableDeclaratorId
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:
发布评论
评论(4)
Java相关语法的权威来源当然是JLS。
JLS 18.1 Java 编程语言语法
The authoritative source for Java-related grammar, is, of course, the JLS.
JLS 18.1 The Grammar of the Java Programming Language
`/* 注释语法如下。 */
注解 ::= NormalAnnotation
|单成员注释
|标记注释
NormalAnnotation ::= "@" 名称 "(" ( MemberValuePairs )? ")"
MarkerAnnotation ::= "@" 名称
SingleMemberAnnotation ::= "@" 名称 "(" MemberValue ")"
MemberValuePairs ::= MemberValuePair ( "," MemberValuePair )*
会员值对 ::= "= 会员值
成员值 ::= 注释
|成员值数组初始化器
|条件表达式
MemberValueArrayInitializer ::= "{" ( MemberValue ( "," MemberValue )* ( "," )? )? "}"
/* 注释类型。 */
AnnotationTypeDeclaration ::= "@" "interface" AnnotationTypeBody
AnnotationTypeBody ::= "{" ( AnnotationTypeMemberDeclaration )* "}"
AnnotationTypeMemberDeclaration ::= 修饰符 ( 类型 "(" ")" ( DefaultValue )? ";" | ClassOrInterfaceDeclaration | EnumDeclaration | AnnotationTypeDeclaration | FieldDeclaration )
| (“;”)
DefaultValue ::= "default" MemberValue` 从这里。另请参阅他的 博客发布。
`/* 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语言语法
任何类型都可以以[Annotations]为前缀:
<代码>类型:
[注释] 标识符 [TypeArguments] {.标识符 [TypeArguments]} {[]}
[Annotations] BasicType
要允许在数组级别上进行注释(在声明中,而不是构造函数中),请将“{[]}”更改为“{[Annotations] []}”。 (在 JLS [GJSB00] 第二版中,这被抽象为“BracketsOpt”。)例如:
类型:
[注释] 标识符 [TypeArguments]{ .标识符 [TypeArguments]} {[注释] []}
[注释] BasicType
还允许对可变参数进行注释 (...):
形式参数声明剩余:
VariableDeclaratorId [, FormalParameterDecls]
[Annotations] ... VariableDeclaratorId
通过将“FormalParameters”(它出现在语法中的所有 5 个位置)的使用更改为“FormalParameters [Annotations]”,注释可能会出现在接收器类型上。例如:
VoidMethodDeclaratorRest:
FormalParameters [Annotations] [throws QualifiedIdentifierList] ( MethodBody | ; )
Java language grammar
Any Type may be prefixed by [Annotations]:
Type:
[Annotations] Identifier [TypeArguments] {. Identifier [TypeArguments]} {[]}
[Annotations] BasicType
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
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 | ; )
这是来自官方的 Java 注解的 ANTLRv4 语法(用法和定义)ANTLR GitHub 存储库。
注解使用语法:
注解声明语法:
This is the ANTLRv4 grammar for Java Annotations (usage and definition) from the official ANTLR GitHub repository.
Annotation usage grammar:
Annotation declaration grammar: