Java grammar definition completeness

While the Java grammar seems very accurately described in the JLS specifications, there are some specific cases that I cannot apply to the given definitions.

For example, if you accept the ClassInstanceCreationExpression rule in ClassInstanceCreationExpression Chapter 15.9, unskilled new expressions should look like this:

 new [TypeArguments] {Annotation} Identifier [TypeArgumentsOrDiamond] ( [ArgumentList] ) [ClassBody] 

Identifier is a standard Java identifier (mostly Java letters / numbers, no ).

How this definition applies to valid expressions, such as initializing static nested classes:

 new C1.C2(); 

or initialization of classes corresponding to packages:

 new java.lang.String("foo"); 

Given that points cannot be part of an Identifier ?

Note that in this definition from JLS7 to JLS8, the change was indicated, where JLS7 indicated, for unqualified new expressions:

 new [TypeArguments] TypeDeclSpecifier [TypeArgumentsOrDiamond]( [ArgumentList] ) [ClassBody] 

TypeDeclSpecifier defined as:

 TypeDeclSpecifier: TypeName ClassOrInterfaceType . Identifier 

allows unqualified new expressions for static nested classes and classes corresponding to the class.

+5
source share
1 answer

There seems to be a bug in the spec.

Quote from the bug report associated with the above (third paragraph in the description):

  1. Grammar does not define any products for the following expression: new java.security.Permissions ()

This is a side effect of removing TypeDeclSpecifier in 4.3, as it interacted poorly with type annotations. The JSR 308 Public Review noted: “TypeDeclSpecifier is one of the most obscure nonterminals in the Java Language Specification. It is used only in a few situations: extends and implements class declaration sentences (8.1.4, 8.1.5).), Extends the interface declaration statement (9.1.3) and the syntax for creating an instance of a class (15.9). The reason for using it is the prohibition of arguments of a substitution type (although 9.1.3 does not actually do this), but this can be achieved equally well without a special terminal. "

+5
source

Source: https://habr.com/ru/post/1204467/


All Articles