[core] Remove AntlrLexerBehavior

This commit is contained in:
Andreas Dangel 2024-06-27 21:15:10 +02:00
parent f90093c923
commit 9b20ec524a
No known key found for this signature in database
GPG Key ID: 93450DF2DF9A3FA3
5 changed files with 18 additions and 78 deletions

View File

@ -12,7 +12,6 @@ import org.antlr.v4.runtime.Lexer;
import net.sourceforge.pmd.cpd.CpdLexer; import net.sourceforge.pmd.cpd.CpdLexer;
import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.TokenManager;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrLexerBehavior;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrToken; import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrToken;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrTokenManager; import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrTokenManager;
import net.sourceforge.pmd.lang.document.TextDocument; import net.sourceforge.pmd.lang.document.TextDocument;
@ -24,15 +23,7 @@ public abstract class AntlrCpdLexer extends CpdLexerBase<AntlrToken> {
@Override @Override
protected final TokenManager<AntlrToken> makeLexerImpl(TextDocument doc) throws IOException { protected final TokenManager<AntlrToken> makeLexerImpl(TextDocument doc) throws IOException {
CharStream charStream = CharStreams.fromReader(doc.newReader(), doc.getFileId().getAbsolutePath()); CharStream charStream = CharStreams.fromReader(doc.newReader(), doc.getFileId().getAbsolutePath());
return new AntlrTokenManager(getLexerForSource(charStream), doc, getLexerBehavior()); return new AntlrTokenManager(getLexerForSource(charStream), doc);
}
/**
* Override this method to customize some aspects of the
* lexer.
*/
protected AntlrLexerBehavior getLexerBehavior() {
return new AntlrLexerBehavior();
} }
protected abstract Lexer getLexerForSource(CharStream charStream); protected abstract Lexer getLexerForSource(CharStream charStream);

View File

@ -1,32 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.ast.impl.antlr4;
import org.antlr.v4.runtime.Token;
import net.sourceforge.pmd.cpd.CpdLanguageProperties;
/**
* Strategy to customize some aspects of the mapping
* from Antlr tokens to PMD/CPD tokens.
*/
public class AntlrLexerBehavior {
/**
* Return the image that the token should have, possibly applying a transformation.
* The default just returns {@link Token#getText()}.
* Transformations here are usually normalizations, for instance, mapping
* the image of all keywords to uppercase/lowercase to implement case-insensitivity,
* or replacing the image of literals by a placeholder to implement {@link CpdLanguageProperties#CPD_ANONYMIZE_LITERALS}.
*
* @param token A token from the Antlr Lexer
*
* @return The image
*/
protected String getTokenImage(Token token) {
return token.getText();
}
}

View File

@ -33,23 +33,18 @@ public class AntlrToken implements GenericToken<AntlrToken> {
* @param token The antlr token implementation * @param token The antlr token implementation
* @param previousComment The previous comment * @param previousComment The previous comment
* @param textDoc The text document * @param textDoc The text document
*/ *
AntlrToken(final Token token, final AntlrToken previousComment, TextDocument textDoc, AntlrLexerBehavior behavior) {
this.previousComment = previousComment;
this.textDoc = textDoc;
this.image = behavior.getTokenImage(token);
this.startOffset = token.getStartIndex();
this.endOffset = token.getStopIndex() + 1; // exclusive
this.channel = token.getChannel();
this.kind = token.getType();
}
/**
* @deprecated Don't create antlr tokens directly, use an {@link AntlrTokenManager} * @deprecated Don't create antlr tokens directly, use an {@link AntlrTokenManager}
*/ */
@Deprecated @Deprecated
public AntlrToken(final Token token, final AntlrToken previousComment, TextDocument textDoc) { public AntlrToken(final Token token, final AntlrToken previousComment, TextDocument textDoc) {
this(token, previousComment, textDoc, new AntlrLexerBehavior()); this.previousComment = previousComment;
this.textDoc = textDoc;
this.image = token.getText();
this.startOffset = token.getStartIndex();
this.endOffset = token.getStopIndex() + 1; // exclusive
this.channel = token.getChannel();
this.kind = token.getType();
} }
@Override @Override

View File

@ -20,20 +20,12 @@ public class AntlrTokenManager implements TokenManager<AntlrToken> {
private final Lexer lexer; private final Lexer lexer;
private final TextDocument textDoc; private final TextDocument textDoc;
private final AntlrLexerBehavior behavior;
private AntlrToken previousToken; private AntlrToken previousToken;
public AntlrTokenManager(final Lexer lexer, final TextDocument textDocument) { public AntlrTokenManager(final Lexer lexer, final TextDocument textDocument) {
this(lexer, textDocument, new AntlrLexerBehavior());
}
public AntlrTokenManager(final Lexer lexer,
final TextDocument textDocument,
final AntlrLexerBehavior behavior) {
this.lexer = lexer; this.lexer = lexer;
this.textDoc = textDocument; this.textDoc = textDocument;
this.behavior = behavior;
resetListeners(); resetListeners();
} }
@ -48,7 +40,7 @@ public class AntlrTokenManager implements TokenManager<AntlrToken> {
private AntlrToken getNextTokenFromAnyChannel() { private AntlrToken getNextTokenFromAnyChannel() {
final AntlrToken previousComment = previousToken != null && previousToken.isHidden() ? previousToken : null; final AntlrToken previousComment = previousToken != null && previousToken.isHidden() ? previousToken : null;
final AntlrToken currentToken = new AntlrToken(lexer.nextToken(), previousComment, textDoc, this.behavior); final AntlrToken currentToken = new AntlrToken(lexer.nextToken(), previousComment, textDoc);
if (previousToken != null) { if (previousToken != null) {
previousToken.next = currentToken; previousToken.next = currentToken;
} }

View File

@ -8,10 +8,9 @@ import java.util.Locale;
import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.Lexer; import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Token;
import net.sourceforge.pmd.cpd.impl.AntlrCpdLexer; import net.sourceforge.pmd.cpd.impl.AntlrCpdLexer;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrLexerBehavior; import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrToken;
import net.sourceforge.pmd.lang.tsql.ast.TSqlLexer; import net.sourceforge.pmd.lang.tsql.ast.TSqlLexer;
/** /**
@ -25,17 +24,12 @@ public class TSqlCpdLexer extends AntlrCpdLexer {
} }
@Override @Override
protected AntlrLexerBehavior getLexerBehavior() { protected String getImage(AntlrToken token) {
return new AntlrLexerBehavior() { if (token.getKind() == TSqlLexer.STRING) {
@Override // This path is for case-sensitive tokens
protected String getTokenImage(Token token) { return token.getImage();
if (token.getType() == TSqlLexer.STRING) { }
// This path is for case-sensitive tokens // normalize case-insensitive tokens
return super.getTokenImage(token); return token.getImage().toUpperCase(Locale.ROOT);
}
// normalize case sensitive tokens
return token.getText().toUpperCase(Locale.ROOT);
}
};
} }
} }