Java注解语法

发布于 2024-08-26 05:13:13 字数 36 浏览 9 评论 0 原文

是否有 BNF 或 EBNF 描述 Java 注释的语法?

Is there a BNF or EBNF that describes the grammar for Java's annotations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

音栖息无 2024-09-02 05:13:13

Java相关语法的权威来源当然是JLS。

JLS 18.1 Java 编程语言语法

Annotations:
        Annotation [Annotations]

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

ElementValue:
        ConditionalExpression
        Annotation
        ElementValueArrayInitializer

... rest ommitted

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
素手挽清风 2024-09-02 05:13:13

`/* 注释语法如下。 */

注解 ::= 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.

葬心 2024-09-02 05:13:13

Java语言语法

  1. 任何类型都可以以[Annotations]为前缀:

    <代码>类型:
    [注释] 标识符 [TypeArguments] {.标识符 [TypeArguments]} {[]}
    [Annotations] BasicType

  2. 要允许在数组级别上进行注释(在声明中,而不是构造函数中),请将“{[]}”更改为“{[Annotations] []}”。 (在 JLS [GJSB00] 第二版中,这被抽象为“BracketsOpt”。)例如:

类型:
[注释] 标识符 [TypeArguments]{ .标识符 [TypeArguments]} {[注释] []}
[注释] BasicType

还允许对可变参数进行注释 (...):
形式参数声明剩余:
VariableDeclaratorId [, FormalParameterDecls]
[Annotations] ... VariableDeclaratorId

  1. 通过将“FormalParameters”(它出现在语法中的所有 5 个位置)的使用更改为“FormalParameters [Annotations]”,注释可能会出现在接收器类型上。例如:

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

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 | ; )

嘿哥们儿 2024-09-02 05:13:13

这是来自官方的 Java 注解的 ANTLRv4 语法(用法和定义)ANTLR GitHub 存储库

注解使用语法:

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


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


elementValuePairs
    : elementValuePair (',' elementValuePair)*
    ;


elementValuePair
    : IDENTIFIER '=' elementValue
    ;


elementValue
    : expression
    | annotation
    | elementValueArrayInitializer
    ;


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

注解声明语法:

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
    ;

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
    ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文