diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 364d997e99..7a845820cc 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1911,6 +1911,34 @@ void Initializer() : } + + +/* JLS: https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3 + +ReferenceType: + ClassOrInterfaceType + TypeVariable + ArrayType +ClassOrInterfaceType: + ClassType + InterfaceType +ClassType: + {Annotation} Identifier [TypeArguments] + ClassOrInterfaceType . {Annotation} Identifier [TypeArguments] +InterfaceType: + ClassType +TypeVariable: + {Annotation} Identifier +ArrayType: + PrimitiveType Dims + ClassOrInterfaceType Dims + TypeVariable Dims +Dims: + {Annotation} [ ] {{Annotation} [ ]} + +*/ + + /* * Type, name and expression syntax follows. * Type is the same as "UnannType" in JLS @@ -1922,18 +1950,48 @@ void Type(): Token t; } { - LOOKAHEAD(2) ReferenceType() + LOOKAHEAD(PrimitiveType() Dims()) ReferenceType() | PrimitiveType() } +void Dims() #ArrayTypeDims: +{} +{ + +/* @A [] @B [] + node { + it.dimSize shouldBe 2 + + child { + child(ignoreChildren=true) { + it.nameImage shouldBe "A" + } + } + + child { + child(ignoreChildren=true) { + it.nameImage shouldBe "B" + } + } + } +*/ + + + ( // LOOKAHEAD((TypeAnnotation())* "[" "]") + ((TypeAnnotation())* "[" "]") #ArrayTypeDim + )+ +} + void ReferenceType(): {} { - // The grammar here is mildly wrong, the annotations can be before each [] - // This will wait for #997 - PrimitiveType() (TypeAnnotation())* ( LOOKAHEAD(2) "[" "]" { jjtThis.bumpArrayDepth(); })+ - | - ( ClassOrInterfaceType()) (TypeAnnotation())* ( LOOKAHEAD(2) "[" "]" { jjtThis.bumpArrayDepth(); })* + + // TypeVariable is ambiguous with ClassOrInterfaceType + // Unless! we add a small symbol table to the parser, + // keeping track of the in-scope type variables (they're all + // necessarily lexically accessible) + ( PrimitiveType() Dims() ) #ArrayType + | ( ClassOrInterfaceType() [ LOOKAHEAD(2) Dims() ] ) #ArrayType(>1) } void ClassOrInterfaceType(): diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java new file mode 100644 index 0000000000..c056b5bb39 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayType.java @@ -0,0 +1,24 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +/* Generated By:JJTree: Do not edit this line. ASTNullLiteral.java */ + +package net.sourceforge.pmd.lang.java.ast; + +public class ASTArrayType extends AbstractJavaTypeNode { + public ASTArrayType(int id) { + super(id); + } + + public ASTArrayType(JavaParser p, int id) { + super(p, id); + } + + /** + * Accept the visitor. * + */ + @Override + public Object jjtAccept(JavaParserVisitor visitor, Object data) { + return visitor.visit(this, data); + } +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java new file mode 100644 index 0000000000..628a894d38 --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDim.java @@ -0,0 +1,24 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +/* Generated By:JJTree: Do not edit this line. ASTNullLiteral.java */ + +package net.sourceforge.pmd.lang.java.ast; + +public class ASTArrayTypeDim extends AbstractJavaTypeNode { + public ASTArrayTypeDim(int id) { + super(id); + } + + public ASTArrayTypeDim(JavaParser p, int id) { + super(p, id); + } + + /** + * Accept the visitor. * + */ + @Override + public Object jjtAccept(JavaParserVisitor visitor, Object data) { + return visitor.visit(this, data); + } +} diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDims.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDims.java new file mode 100644 index 0000000000..0d357bdf6e --- /dev/null +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTArrayTypeDims.java @@ -0,0 +1,24 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +/* Generated By:JJTree: Do not edit this line. ASTNullLiteral.java */ + +package net.sourceforge.pmd.lang.java.ast; + +public class ASTArrayTypeDims extends AbstractJavaTypeNode { + public ASTArrayTypeDims(int id) { + super(id); + } + + public ASTArrayTypeDims(JavaParser p, int id) { + super(p, id); + } + + /** + * Accept the visitor. * + */ + @Override + public Object jjtAccept(JavaParserVisitor visitor, Object data) { + return visitor.visit(this, data); + } +}