From 69ad3a70878b65cec2bb2a1bbbaab419f191c086 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 25 Jan 2020 02:40:35 +0100 Subject: [PATCH] Simplify some jjtree aspects --- .../lang/ast/impl/javacc/JjtreeBuilder.java | 18 ------------------ pmd-java/etc/grammar/Java.jjt | 18 +++++++++--------- .../lang/java/ast/ASTMethodReferenceTest.kt | 18 ++++++++++++++++++ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java index 9b6e3fc502..ada584a9fe 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/JjtreeBuilder.java @@ -57,24 +57,6 @@ public final class JjtreeBuilder> { return nodes.get(0); } - /*** - * Extend the number of children of the current node of one to the left. - * If the node is closed, one additional node from the stack will be popped - * and added to its children. This allows mimicking "left-recursive" nodes, - * while keeping the parsing iterative. - * - *

Note that when the total number of children is definitely known, you - * can use "definite nodes", ie write the expected number of children (including - * the ones to the left) in the JJTree annotation (eg {@code #AdditiveExpression(2)}). - * So this is only useful when the number of children of the current node is not certain. - * - *

This method does not affect the stack unless the current jjtThis is - * closed in the future. - */ - public void extendLeft() { - mk--; - } - /*** * Peek the nth node from the top of the stack. * peekNode(0) == peekNode() diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 08b0b92ef8..716fb96d19 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -1285,13 +1285,13 @@ void ClassOrInterfaceType() #void: { forceTypeContext(); } } -private void ClassTypeSegment() #ClassOrInterfaceType: +private void ClassTypeSegment() #ClassOrInterfaceType(jjtree.nodeArity() + 1): {} { TypeAnnotListNoInject() // We'll enclose the previous segment - { setLastTokenImage(jjtThis); jjtree.extendLeft();} + { setLastTokenImage(jjtThis); } [ TypeArguments() ] } @@ -1345,7 +1345,7 @@ void PrimitiveType() : | "float" | "double" ) - {jjtThis.setImage(getToken(0).getImage());} + {setLastTokenImage(jjtThis);} } @@ -1628,7 +1628,8 @@ void PrimaryPrefix() #void : ("." MemberSelector() | MethodReference()) | UnqualifiedAllocationExpr() | ("void" "." "class") #ClassLiteral -| (PrimitiveType() [ Dims() ] ) #ArrayType(>1) +| LOOKAHEAD(1) // suppress the warning here. + (PrimitiveType() [ Dims() ] ) #ArrayType(>1) ( MethodReference() | "." "class" #ClassLiteral(1) @@ -1636,7 +1637,7 @@ void PrimaryPrefix() #void : // If annotations start the expression, it's necessarily a method or ctor reference // This is because types in class literals or in qualified super/this may not be annotated -| LOOKAHEAD({getToken(1).kind == AT}) AnnotatedRefType() MethodReference() +| LOOKAHEAD("@") AnnotatedRefType() MethodReference() | LOOKAHEAD(LambdaLahead()) LambdaExpression() @@ -1738,10 +1739,10 @@ void MemberSelector() #void : | ( {setLastTokenImage(jjtThis);}) #FieldAccess(1) } -void MethodReference(): // LHS is injected +void MethodReference() #MethodReference(jjtree.nodeArity() + 1): // LHS is injected {} { - "::" {jjtree.extendLeft();} + "::" [TypeArguments()] ( "new" | ) {setLastTokenImage(jjtThis);} {/* empty, to set the image before jjtClose */} @@ -1847,11 +1848,10 @@ void ArgumentList() : // more straightforward because can't be an array creation expr -void QualifiedAllocationExpr() #ConstructorCall: +void QualifiedAllocationExpr() #ConstructorCall(jjtree.nodeArity() + 1): {} { "new" - {jjtree.extendLeft();} [ TypeArguments() ] AnnotatedClassOrInterfaceType() ArgumentList() diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTMethodReferenceTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTMethodReferenceTest.kt index 9471a72fd7..92b107bd23 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTMethodReferenceTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/ASTMethodReferenceTest.kt @@ -1,6 +1,7 @@ package net.sourceforge.pmd.lang.java.ast import net.sourceforge.pmd.lang.ast.test.shouldBe +import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType.PrimitiveType.BOOLEAN import net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType.PrimitiveType.INT /** @@ -157,6 +158,23 @@ class ASTMethodReferenceTest : ParserTestSpec({ } } + "boolean @A []::new" should parseAs { + constructorRef { + it::getTypeArguments shouldBe null + + typeExpr { + arrayType { + primitiveType(BOOLEAN) + it::getDimensions shouldBe child { + arrayDim { + annotation("A") + } + } + } + } + } + } + "Class[]::new" should parseAs { constructorRef { it::getTypeArguments shouldBe null