[core] Remove AntlrLexerBehavior
This commit is contained in:
parent
f90093c923
commit
9b20ec524a
@ -12,7 +12,6 @@ import org.antlr.v4.runtime.Lexer;
|
||||
|
||||
import net.sourceforge.pmd.cpd.CpdLexer;
|
||||
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.AntlrTokenManager;
|
||||
import net.sourceforge.pmd.lang.document.TextDocument;
|
||||
@ -24,15 +23,7 @@ public abstract class AntlrCpdLexer extends CpdLexerBase<AntlrToken> {
|
||||
@Override
|
||||
protected final TokenManager<AntlrToken> makeLexerImpl(TextDocument doc) throws IOException {
|
||||
CharStream charStream = CharStreams.fromReader(doc.newReader(), doc.getFileId().getAbsolutePath());
|
||||
return new AntlrTokenManager(getLexerForSource(charStream), doc, getLexerBehavior());
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to customize some aspects of the
|
||||
* lexer.
|
||||
*/
|
||||
protected AntlrLexerBehavior getLexerBehavior() {
|
||||
return new AntlrLexerBehavior();
|
||||
return new AntlrTokenManager(getLexerForSource(charStream), doc);
|
||||
}
|
||||
|
||||
protected abstract Lexer getLexerForSource(CharStream charStream);
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
@ -33,23 +33,18 @@ public class AntlrToken implements GenericToken<AntlrToken> {
|
||||
* @param token The antlr token implementation
|
||||
* @param previousComment The previous comment
|
||||
* @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
|
||||
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
|
||||
|
@ -20,20 +20,12 @@ public class AntlrTokenManager implements TokenManager<AntlrToken> {
|
||||
|
||||
private final Lexer lexer;
|
||||
private final TextDocument textDoc;
|
||||
private final AntlrLexerBehavior behavior;
|
||||
private AntlrToken previousToken;
|
||||
|
||||
|
||||
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.textDoc = textDocument;
|
||||
this.behavior = behavior;
|
||||
resetListeners();
|
||||
}
|
||||
|
||||
@ -48,7 +40,7 @@ public class AntlrTokenManager implements TokenManager<AntlrToken> {
|
||||
|
||||
private AntlrToken getNextTokenFromAnyChannel() {
|
||||
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) {
|
||||
previousToken.next = currentToken;
|
||||
}
|
||||
|
@ -8,10 +8,9 @@ import java.util.Locale;
|
||||
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -25,17 +24,12 @@ public class TSqlCpdLexer extends AntlrCpdLexer {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AntlrLexerBehavior getLexerBehavior() {
|
||||
return new AntlrLexerBehavior() {
|
||||
@Override
|
||||
protected String getTokenImage(Token token) {
|
||||
if (token.getType() == TSqlLexer.STRING) {
|
||||
// This path is for case-sensitive tokens
|
||||
return super.getTokenImage(token);
|
||||
}
|
||||
// normalize case sensitive tokens
|
||||
return token.getText().toUpperCase(Locale.ROOT);
|
||||
}
|
||||
};
|
||||
protected String getImage(AntlrToken token) {
|
||||
if (token.getKind() == TSqlLexer.STRING) {
|
||||
// This path is for case-sensitive tokens
|
||||
return token.getImage();
|
||||
}
|
||||
// normalize case-insensitive tokens
|
||||
return token.getImage().toUpperCase(Locale.ROOT);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user