From 1c09b28e56fd75cf4c1d32ee0dd2595a3aed0308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Fri, 7 Feb 2020 23:23:54 +0100 Subject: [PATCH] Simplify Block::containsComment Move logic out of the parser --- pmd-java/etc/grammar/Java.jjt | 39 ++++++++----------- .../pmd/lang/java/ast/ASTBlock.java | 16 +++++--- .../pmd/lang/java/ast/JavaTokenDocument.java | 18 +++++++++ 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 716fb96d19..443e1daff8 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -299,17 +299,6 @@ class JavaParserImpl { return getToken(1).image.equals("assert"); } - private boolean isPrecededByComment(JavaccToken tok) { - boolean res = false; - while (!res && tok.specialToken != null) { - tok = tok.specialToken; - res = tok.kind == SINGLE_LINE_COMMENT || - tok.kind == FORMAL_COMMENT || - tok.kind == MULTI_LINE_COMMENT; - } - return res; - } - /** * Semantic lookahead to check if the next identifier is a * specific restricted keyword. @@ -1064,14 +1053,22 @@ void VarargsDim() #ArrayTypeDim: void ConstructorDeclaration() : -{JavaccToken t;} +{} { - [ TypeParameters() ] - {setLastTokenImage(jjtThis);} FormalParameters() [ ThrowsList() ] - ("{" - [ LOOKAHEAD(ExplicitConstructorInvocation()) ExplicitConstructorInvocation() ] - ( BlockStatement() )* - t = "}" { if (isPrecededByComment(t)) { jjtThis.setContainsComment(); } } ) #Block + [ TypeParameters() ] + {setLastTokenImage(jjtThis);} + FormalParameters() + [ ThrowsList() ] + ConstructorBlock() +} + +private void ConstructorBlock() #Block: +{} +{ + "{" + [ LOOKAHEAD(ExplicitConstructorInvocation()) ExplicitConstructorInvocation() ] + ( BlockStatement() )* + "}" } void ExplicitConstructorInvocation() : @@ -1954,11 +1951,9 @@ void LabeledStatement() : } void Block() : -{JavaccToken t;} +{} { - "{" - - ( BlockStatement() )* t = "}" { if (isPrecededByComment(t)) { jjtThis.setContainsComment(); } } + "{" ( BlockStatement() )* "}" } void BlockStatement() #void: diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java index 20106d123a..ca5334e590 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTBlock.java @@ -6,6 +6,8 @@ package net.sourceforge.pmd.lang.java.ast; import java.util.Iterator; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; + /** * A block of code. This is a {@linkplain ASTStatement statement} that * contains other statements. @@ -18,8 +20,6 @@ import java.util.Iterator; */ public final class ASTBlock extends AbstractStatement implements Iterable, ASTSwitchArrowRHS { - private boolean containsComment; - ASTBlock(int id) { super(id); } @@ -37,11 +37,15 @@ public final class ASTBlock extends AbstractStatement implements Iterable