From 2027d2f57fe4cb38e23a2cc988bed97a03995fa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 5 Nov 2022 17:05:46 +0100 Subject: [PATCH] Support generic ctors --- pmd-java/etc/grammar/Java.jjt | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 2072359aad..06f4d66829 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -736,6 +736,12 @@ public class JavaParser { token_source.setSuppressMarker(marker); } + /** + * Keeps track during tree construction, whether we are currently building an + * explicit constructor invocation. Then the PrimaryExpression that may prefix + * a qualified super constructor call may not consume "super" tokens. + */ + private boolean inExplicitConstructorInvoc = false; } PARSER_END(JavaParser) @@ -1559,13 +1565,14 @@ Token t;} } void ExplicitConstructorInvocation() : -{} -{ - LOOKAHEAD("this" Arguments() ";") "this" {jjtThis.setIsThis();} Arguments() ";" +{boolean prev = inExplicitConstructorInvoc; inExplicitConstructorInvoc = true;} +{ ( + LOOKAHEAD([TypeArguments()] "this" Arguments() ";") [TypeArguments()] "this" {jjtThis.setIsThis();} | - LOOKAHEAD(TypeArguments() "this" Arguments() ";") TypeArguments() "this" {jjtThis.setIsThis();} Arguments() ";" -| - [ LOOKAHEAD(PrimaryExpression() ".") PrimaryExpression() "." ] [ TypeArguments() ] "super" {jjtThis.setIsSuper();} Arguments() ";" + [ LOOKAHEAD(PrimaryExpression() "." [TypeArguments()] "super" ) PrimaryExpression() "." ] [ TypeArguments() ] "super" {jjtThis.setIsSuper();} + ) + {inExplicitConstructorInvoc = prev;} + Arguments() ";" } void Initializer() : @@ -1957,9 +1964,18 @@ void SwitchExpression() : void PrimaryExpression() : {} { - PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )* + PrimaryPrefix() ( LOOKAHEAD(SuffixLAhead()) PrimarySuffix() )* } +private void SuffixLAhead() #void: +{} +{ + "::" | "[" | "(" + | LOOKAHEAD({!inExplicitConstructorInvoc}) "." + | LOOKAHEAD({inExplicitConstructorInvoc}) "." ( | TypeArguments() | "new") // not super or this in this case +} + + void MemberSelector(): { Token t; @@ -2077,10 +2093,12 @@ void ArgumentList() : void AllocationExpression(): {} { - "new" (TypeAnnotation())* + "new" (LOOKAHEAD(2) PrimitiveType() ArrayDimsAndInits() | + [TypeArguments()] + (AnnotationNoNode())* ClassOrInterfaceType() ( ArrayDimsAndInits()