Simplify Block::containsComment

Move logic out of the parser
This commit is contained in:
Clément Fournier
2020-02-07 23:23:54 +01:00
parent 91872dfe60
commit 1c09b28e56
3 changed files with 45 additions and 28 deletions

View File

@ -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() ]
<IDENTIFIER> {setLastTokenImage(jjtThis);} FormalParameters() [ ThrowsList() ]
("{"
[ LOOKAHEAD(ExplicitConstructorInvocation()) ExplicitConstructorInvocation() ]
( BlockStatement() )*
t = "}" { if (isPrecededByComment(t)) { jjtThis.setContainsComment(); } } ) #Block
[ TypeParameters() ]
<IDENTIFIER> {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:

View File

@ -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<ASTStatement>, ASTSwitchArrowRHS {
private boolean containsComment;
ASTBlock(int id) {
super(id);
}
@ -37,11 +37,15 @@ public final class ASTBlock extends AbstractStatement implements Iterable<ASTSta
public boolean containsComment() {
return this.containsComment;
}
JavaccToken t = jjtGetLastToken().getPreviousComment();
while (t != null) {
if (JavaTokenDocument.isComment(t)) {
return true;
}
t = t.getPreviousComment();
}
void setContainsComment() {
this.containsComment = true;
return false;
}
@Override

View File

@ -4,9 +4,12 @@
package net.sourceforge.pmd.lang.java.ast;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.FORMAL_COMMENT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.GT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.MULTI_LINE_COMMENT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.RSIGNEDSHIFT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.RUNSIGNEDSHIFT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.SINGLE_LINE_COMMENT;
import static net.sourceforge.pmd.lang.java.ast.JavaTokenKinds.WHITESPACE;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -24,6 +27,21 @@ final class JavaTokenDocument extends JavaccTokenDocument {
super(fullText);
}
/**
* Returns true if the given token is a Java comment.
*/
public static boolean isComment(JavaccToken t) {
switch (t.kind) {
case FORMAL_COMMENT:
case MULTI_LINE_COMMENT:
case SINGLE_LINE_COMMENT:
return true;
default:
return false;
}
}
@Override
protected @Nullable String describeKindImpl(int kind) {
return JavaTokenKinds.describe(kind);