From f6d25ccf3380b395932f7dd06ba430930512d6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Mon, 16 Dec 2019 07:36:47 +0100 Subject: [PATCH] Cleanup token manager errors --- pmd-core/src/main/ant/alljavacc.xml | 16 -- .../sourceforge/pmd/SourceCodeProcessor.java | 8 +- .../pmd/cpd/internal/AntlrTokenizer.java | 6 +- .../pmd/cpd/internal/JavaCCTokenizer.java | 6 +- .../sourceforge/pmd/lang/AbstractParser.java | 4 +- .../sourceforge/pmd/lang/TokenManager.java | 1 - .../pmd/lang/ast/AbstractTokenManager.java | 38 --- .../pmd/lang/ast/TokenMgrError.java | 114 +++++++++ .../lang/ast/impl/antlr4/AntlrBaseParser.java | 2 +- .../ast/impl/antlr4/AntlrTokenManager.java | 14 +- .../ast/impl/javacc/AbstractJjtreeNode.java | 220 ------------------ .../ast/impl/javacc/AbstractTokenManager.java | 24 ++ .../AncestorOrSelfIterator.java | 0 .../ast/{impl => internal}/AxisStream.java | 0 .../DescendantOrSelfIterator.java | 0 .../ast/{impl => internal}/Filtermap.java | 0 .../IteratorBasedNStream.java | 0 .../SingletonNodeStream.java | 0 .../ast/{impl => internal}/StreamImpl.java | 4 + .../{impl => internal}/TraversalUtils.java | 0 pmd-cpp/src/main/ant/alljavacc.xml | 4 +- .../pmd/lang/cpp/CppTokenManager.java | 6 +- .../sourceforge/pmd/cpd/CPPTokenizerTest.java | 4 +- .../sourceforge/pmd/cpd/DartTokenizer.java | 2 +- .../net/sourceforge/pmd/cpd/GoTokenizer.java | 2 +- pmd-java/etc/grammar/Java.jjt | 4 +- pmd-java/src/main/ant/alljavacc.xml | 2 +- .../pmd/lang/java/JavaTokenManager.java | 4 - .../pmd/lang/java/ast/ASTCompilationUnit.java | 1 - .../pmd/lang/java/ast/AbstractJavaNode.java | 54 ++--- .../pmd/lang/java/ast/InternalApiBridge.java | 2 - .../pmd/lang/java/ast/JavaNode.java | 5 +- .../pmd/lang/java/ast/JavaTokenFactory.java | 4 +- .../documentation/CommentRequiredRule.java | 2 +- .../pmd/lang/java/ast/TestExtensions.kt | 4 +- .../pmd/lang/java/ast/TokenUtilsTest.kt | 2 +- pmd-javascript/src/main/ant/alljavacc.xml | 2 +- .../ecmascript5/Ecmascript5TokenManager.java | 4 - pmd-jsp/src/main/ant/alljavacc.xml | 2 +- .../sourceforge/pmd/lang/jsp/JspParser.java | 3 +- .../pmd/lang/jsp/JspTokenManager.java | 4 - .../pmd/lang/jsp/ast/AbstractJspNodesTst.java | 2 +- .../sourceforge/pmd/cpd/KotlinTokenizer.java | 2 +- .../net/sourceforge/pmd/cpd/LuaTokenizer.java | 2 +- pmd-matlab/src/main/ant/alljavacc.xml | 2 +- .../pmd/lang/matlab/MatlabTokenManager.java | 4 - pmd-objectivec/src/main/ant/alljavacc.xml | 2 +- .../objectivec/ObjectiveCTokenManager.java | 4 - pmd-plsql/src/main/ant/alljavacc.xml | 2 +- .../pmd/lang/plsql/PLSQLParser.java | 3 +- .../pmd/lang/plsql/PLSQLTokenManager.java | 4 - pmd-python/src/main/ant/alljavacc.xml | 2 +- .../pmd/lang/python/PythonTokenManager.java | 4 - .../sourceforge/pmd/cpd/SwiftTokenizer.java | 2 +- pmd-visualforce/src/main/ant/alljavacc.xml | 4 +- .../net/sourceforge/pmd/lang/vf/VfParser.java | 3 +- .../pmd/lang/vf/VfTokenManager.java | 4 - .../pmd/lang/vf/ast/AbstractVfNodesTest.java | 2 +- pmd-vm/src/main/ant/alljavacc.xml | 2 +- .../net/sourceforge/pmd/lang/vm/VmParser.java | 3 +- .../pmd/lang/vm/VmTokenManager.java | 6 - 61 files changed, 222 insertions(+), 411 deletions(-) delete mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractTokenManager.java create mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java delete mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java create mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractTokenManager.java rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/AncestorOrSelfIterator.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/AxisStream.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/DescendantOrSelfIterator.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/Filtermap.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/IteratorBasedNStream.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/SingletonNodeStream.java (100%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/StreamImpl.java (98%) rename pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/{impl => internal}/TraversalUtils.java (100%) diff --git a/pmd-core/src/main/ant/alljavacc.xml b/pmd-core/src/main/ant/alljavacc.xml index 623d10ed98..0ddea773d0 100644 --- a/pmd-core/src/main/ant/alljavacc.xml +++ b/pmd-core/src/main/ant/alljavacc.xml @@ -91,22 +91,6 @@ - - - - - - - - diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java b/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java index 99c192d018..cdcaabbbd5 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/SourceCodeProcessor.java @@ -20,6 +20,7 @@ import net.sourceforge.pmd.lang.Parser; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.ast.RootNode; +import net.sourceforge.pmd.lang.ast.TokenMgrError; import net.sourceforge.pmd.lang.xpath.Initializer; public class SourceCodeProcessor { @@ -159,7 +160,12 @@ public class SourceCodeProcessor { Parser parser = PMD.parserFor(languageVersion, configuration); - RootNode rootNode = (RootNode) parse(ctx, sourceCode, parser); + RootNode rootNode; + try { + rootNode = (RootNode) parse(ctx, sourceCode, parser); + } catch (TokenMgrError tmgrError) { + throw TokenMgrError.withFileName(ctx.getSourceCodeFile().toString(), tmgrError); + } dependencyHelper.runLanguageSpecificStages(ruleSets, languageVersion, rootNode); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java index 3310732cb0..23a2ef3e7c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/AntlrTokenizer.java @@ -27,20 +27,18 @@ public abstract class AntlrTokenizer implements Tokenizer { public void tokenize(final SourceCode sourceCode, final Tokens tokenEntries) { final AntlrTokenManager tokenManager = getLexerForSource(sourceCode); - tokenManager.setFileName(sourceCode.getFileName()); - final AntlrTokenFilter tokenFilter = getTokenFilter(tokenManager); try { AntlrToken currentToken = tokenFilter.getNextToken(); while (currentToken != null) { - processToken(tokenEntries, tokenManager.getFileName(), currentToken); + processToken(tokenEntries, sourceCode.getFileName(), currentToken); currentToken = tokenFilter.getNextToken(); } } catch (final AntlrTokenManager.ANTLRSyntaxError err) { // Wrap exceptions of the ANTLR tokenizer in a TokenMgrError, so they are correctly handled // when CPD is executed with the '--skipLexicalErrors' command line option - throw new TokenMgrError("Lexical error in file " + tokenManager.getFileName() + " at line " + throw new TokenMgrError("Lexical error in file " + sourceCode.getFileName() + " at line " + err.getLine() + ", column " + err.getColumn() + ". Encountered: " + err.getMessage(), TokenMgrError.LEXICAL_ERROR); } finally { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/JavaCCTokenizer.java b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/JavaCCTokenizer.java index 03adab05c5..bbb29fe1ff 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/JavaCCTokenizer.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/cpd/internal/JavaCCTokenizer.java @@ -14,6 +14,7 @@ import net.sourceforge.pmd.cpd.token.JavaCCTokenFilter; import net.sourceforge.pmd.cpd.token.TokenFilter; import net.sourceforge.pmd.lang.TokenManager; import net.sourceforge.pmd.lang.ast.GenericToken; +import net.sourceforge.pmd.lang.ast.TokenMgrError; public abstract class JavaCCTokenizer implements Tokenizer { @@ -26,11 +27,10 @@ public abstract class JavaCCTokenizer implements Tokenizer { protected TokenEntry processToken(Tokens tokenEntries, GenericToken currentToken, String filename) { return new TokenEntry(currentToken.getImage(), filename, currentToken.getBeginLine()); } - + @Override public void tokenize(SourceCode sourceCode, Tokens tokenEntries) throws IOException { TokenManager tokenManager = getLexerForSource(sourceCode); - tokenManager.setFileName(sourceCode.getFileName()); try { final TokenFilter tokenFilter = getTokenFilter(tokenManager); @@ -39,6 +39,8 @@ public abstract class JavaCCTokenizer implements Tokenizer { tokenEntries.add(processToken(tokenEntries, currentToken, sourceCode.getFileName())); currentToken = tokenFilter.getNextToken(); } + } catch (TokenMgrError e) { + throw TokenMgrError.withFileName(sourceCode.getFileName(), e); } finally { tokenEntries.add(TokenEntry.getEOF()); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/AbstractParser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/AbstractParser.java index 870fb72440..7036e9965c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/AbstractParser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/AbstractParser.java @@ -25,9 +25,7 @@ public abstract class AbstractParser implements Parser { @Override public TokenManager getTokenManager(String fileName, Reader source) { - TokenManager tokenManager = createTokenManager(source); - tokenManager.setFileName(fileName); - return tokenManager; + return createTokenManager(source); } protected abstract TokenManager createTokenManager(Reader source); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/TokenManager.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/TokenManager.java index e0e67c78cc..1184508ba6 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/TokenManager.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/TokenManager.java @@ -11,5 +11,4 @@ public interface TokenManager { // TODO : Change the return to GenericToken in 7.0.0 - maybe even use generics TokenManager Object getNextToken(); - void setFileName(String fileName); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractTokenManager.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractTokenManager.java deleted file mode 100644 index 96c8e60c3d..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractTokenManager.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.ast; - -import java.util.HashMap; -import java.util.Map; - -import net.sourceforge.pmd.PMD; - -public abstract class AbstractTokenManager { - - // Because the TokenMgrError class does not have access to the TokenManager - // instance, we - // cannot store the file name as an instance field, but must use a static. - private static ThreadLocal fileName = new ThreadLocal<>(); - - protected Map suppressMap = new HashMap<>(); - protected String suppressMarker = PMD.SUPPRESS_MARKER; - - public static void setFileName(String fileName) { - AbstractTokenManager.fileName.set(fileName); - } - - public static String getFileName() { - String fileName = AbstractTokenManager.fileName.get(); - return fileName == null ? "(no file name provided)" : fileName; - } - - public void setSuppressMarker(String marker) { - this.suppressMarker = marker; - } - - public Map getSuppressMap() { - return suppressMap; - } -} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java new file mode 100644 index 0000000000..62279b1feb --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/TokenMgrError.java @@ -0,0 +1,114 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast; + +/** + * An error thrown during lexical analysis of a file. + */ +public final class TokenMgrError extends RuntimeException { + + /** + * Lexical error occurred. + */ + public static final int LEXICAL_ERROR = 0; + + /** + * Tried to change to an invalid lexical state. + */ + public static final int INVALID_LEXICAL_STATE = 2; + + /** + * Detected (and bailed out of) an infinite loop in the token manager. + */ + public static final int LOOP_DETECTED = 3; + + /** Constructor with message. */ + private TokenMgrError(String message, Throwable cause) { + super(message, cause); + } + + /** Constructor with message and reason. */ + @SuppressWarnings("PMD.UnusedFormalParameter") + public TokenMgrError(String message, int reason) { + super(message); + } + + /** Full Constructor. */ + @SuppressWarnings("PMD.UnusedFormalParameter") + public TokenMgrError(boolean eofSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) { + this(makeMessage(eofSeen, errorLine, errorColumn, errorAfter, curChar), reason); + } + + /** + * Replaces unprintable characters by their escaped (or unicode escaped) + * equivalents in the given string + */ + private static String addEscapes(String str) { + StringBuilder retval = new StringBuilder(); + for (int i = 0; i < str.length(); i++) { + final char ch = str.charAt(i); + switch (ch) { + case 0: + break; + case '\b': + retval.append("\\b"); + break; + case '\t': + retval.append("\\t"); + break; + case '\n': + retval.append("\\n"); + break; + case '\f': + retval.append("\\f"); + break; + case '\r': + retval.append("\\r"); + break; + case '\"': + retval.append("\\\""); + break; + case '\'': + retval.append("\\'"); + break; + case '\\': + retval.append("\\\\"); + break; + default: + if (ch < 0x20 || ch > 0x7e) { + String s = "0000" + Integer.toString(ch, 16); + retval.append("\\u").append(s.substring(s.length() - 4)); + } else { + retval.append(ch); + } + break; + } + } + return retval.toString(); + } + + /** + * Returns a detailed message for the Error when it is thrown by the + * token manager to indicate a lexical error. + * Parameters : + * eofseen : indicates if EOF caused the lexical error + * curLexState : lexical state in which this error occurred + * errorLine : line number when the error occurred + * errorColumn : column number when the error occurred + * errorAfter : prefix that was seen before this error occurred + * curchar : the offending character + * Note: You can customize the lexical error message by modifying this method. + */ + private static String makeMessage(boolean eofseen, int errorLine, int errorColumn, String errorAfter, char curChar) { + return "Lexical error at line " + errorLine + ", column " + errorColumn + ". Encountered: " + + (eofseen ? " " : "\"" + addEscapes(String.valueOf(curChar)) + "\"" + " (" + (int) curChar + "), ") + + "after : \"" + addEscapes(errorAfter) + "\""; + } + + public static TokenMgrError withFileName(String filename, TokenMgrError error) { + String newMessage = "Lexical error in file " + filename + error.getMessage().substring("Lexical error ".length()); + return new TokenMgrError(newMessage, error); + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java index 7a92e0d9ea..e1a1585d45 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java @@ -34,7 +34,7 @@ public abstract class AntlrBaseParser imp @Override public TokenManager getTokenManager(final String fileName, final Reader source) { try { - return new AntlrTokenManager(getLexer(source), fileName); + return new AntlrTokenManager(getLexer(source)); } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrTokenManager.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrTokenManager.java index 19905322e3..1e0b25999c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrTokenManager.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrTokenManager.java @@ -17,18 +17,15 @@ import net.sourceforge.pmd.lang.TokenManager; */ public class AntlrTokenManager implements TokenManager { private final Lexer lexer; - private String fileName; private AntlrToken previousToken; /** * Constructor * * @param lexer The lexer - * @param fileName The file name */ - public AntlrTokenManager(final Lexer lexer, final String fileName) { + public AntlrTokenManager(final Lexer lexer) { this.lexer = lexer; - this.fileName = fileName; resetListeners(); } @@ -49,15 +46,6 @@ public class AntlrTokenManager implements TokenManager { return currentToken; } - @Override - public void setFileName(String fileName) { - this.fileName = fileName; - } - - public String getFileName() { - return fileName; - } - private void resetListeners() { lexer.removeErrorListeners(); lexer.addErrorListener(new ErrorHandler()); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java deleted file mode 100644 index 6c7e01aed9..0000000000 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractJjtreeNode.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.ast.impl.javacc; - -import java.util.Objects; - -import org.apache.commons.lang3.ArrayUtils; - -import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.dfa.DataFlowNode; - -/** - * Base class for implementations of the {@link Node} interface - * that use JJTree. - */ -public abstract class AbstractJjtreeNode implements Node { - - private static final Node[] EMPTY_ARRAY = new Node[0]; - - protected Node parent; - // never null, never contains null elements - protected Node[] children = EMPTY_ARRAY; - protected int childIndex; - - private String image; - private DataFlowNode dataFlowNode; - private Object userData; - protected JavaccToken firstToken; - protected JavaccToken lastToken; - - protected AbstractJjtreeNode() { - - } - - @Override - public void jjtOpen() { - // to be overridden by subclasses - } - - @Override - public void jjtClose() { - // to be overridden by subclasses - } - - @Override - public void jjtSetParent(final Node parent) { - this.parent = parent; - } - - @Override - public Node jjtGetParent() { - return parent; - } - - @Override - public void jjtAddChild(final Node child, final int index) { - if (index >= children.length) { - final Node[] newChildren = new Node[index + 1]; - System.arraycopy(children, 0, newChildren, 0, children.length); - children = newChildren; - } - children[index] = child; - child.jjtSetChildIndex(index); - child.jjtSetParent(this); - } - - @Override - public void jjtSetChildIndex(final int index) { - childIndex = index; - } - - @Override - public int jjtGetChildIndex() { - return childIndex; - } - - @Override - public Node jjtGetChild(final int index) { - return children[index]; - } - - @Override - public int jjtGetNumChildren() { - return children.length; - } - - @Override - public int jjtGetId() { - return 0; - } - - @Override - public String getImage() { - return image; - } - - @Override - public void setImage(final String image) { - this.image = image; - } - - @Override - public boolean hasImageEqualTo(final String image) { - return Objects.equals(this.getImage(), image); - } - - @Override - public int getBeginLine() { - return firstToken.getBeginLine(); - } - - @Override - public int getBeginColumn() { - return firstToken.getBeginColumn(); - } - - @Override - public int getEndLine() { - return lastToken.getEndLine(); - } - - @Override - public int getEndColumn() { - return lastToken.getEndColumn(); - } - - - @Override - public DataFlowNode getDataFlowNode() { - if (this.dataFlowNode == null) { - if (this.parent != null) { - return parent.getDataFlowNode(); - } - return null; // TODO wise? - } - return dataFlowNode; - } - - @Override - public void setDataFlowNode(final DataFlowNode dataFlowNode) { - this.dataFlowNode = dataFlowNode; - } - - /** - * Returns true if this node has a descendant of any type among the provided types. - * - * @param types Types to test - */ - public final boolean hasDescendantOfAnyType(final Class... types) { - // TODO consider implementing that with a single traversal! - // hasDescendantOfType could then be a special case of this one - // But to really share implementations, getFirstDescendantOfType's - // internal helper could have to give up some type safety to rely - // instead on a getFirstDescendantOfAnyType, then cast to the correct type - for (final Class type : types) { - if (hasDescendantOfType(type)) { - return true; - } - } - return false; - } - - @Override - public Object getUserData() { - return userData; - } - - @Override - public void setUserData(final Object userData) { - this.userData = userData; - } - - public final JavaccToken getFirstToken() { - return firstToken; - } - - public final JavaccToken getLastToken() { - return lastToken; - } - - protected void setFirstToken(final JavaccToken token) { - this.firstToken = token; - } - - protected void setLastToken(final JavaccToken token) { - this.lastToken = token; - } - - @Override - public void remove() { - // Detach current node of its parent, if any - final Node parent = jjtGetParent(); - if (parent != null) { - parent.removeChildAtIndex(jjtGetChildIndex()); - jjtSetParent(null); - } - - // TODO [autofix]: Notify action for handling text edition - } - - @Override - public void removeChildAtIndex(final int childIndex) { - if (0 <= childIndex && childIndex < jjtGetNumChildren()) { - // Remove the child at the given index - children = ArrayUtils.remove(children, childIndex); - // Update the remaining & left-shifted children indexes - for (int i = childIndex; i < jjtGetNumChildren(); i++) { - jjtGetChild(i).jjtSetChildIndex(i); - } - } - } - - @Override - public String toString() { - return getXPathNodeName(); - } - -} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractTokenManager.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractTokenManager.java new file mode 100644 index 0000000000..b4ab4cfc05 --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/javacc/AbstractTokenManager.java @@ -0,0 +1,24 @@ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + +package net.sourceforge.pmd.lang.ast.impl.javacc; + +import java.util.HashMap; +import java.util.Map; + +import net.sourceforge.pmd.PMD; + +public abstract class AbstractTokenManager { + + protected Map suppressMap = new HashMap<>(); + protected String suppressMarker = PMD.SUPPRESS_MARKER; + + public void setSuppressMarker(String marker) { + this.suppressMarker = marker; + } + + public Map getSuppressMap() { + return suppressMap; + } +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AncestorOrSelfIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AncestorOrSelfIterator.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AncestorOrSelfIterator.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AncestorOrSelfIterator.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AxisStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/AxisStream.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/AxisStream.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/DescendantOrSelfIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/DescendantOrSelfIterator.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/DescendantOrSelfIterator.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/DescendantOrSelfIterator.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/Filtermap.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/Filtermap.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/Filtermap.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/IteratorBasedNStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/IteratorBasedNStream.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/IteratorBasedNStream.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SingletonNodeStream.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/SingletonNodeStream.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/SingletonNodeStream.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/StreamImpl.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java similarity index 98% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/StreamImpl.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java index 660fe11061..1830690e76 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/StreamImpl.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/StreamImpl.java @@ -2,6 +2,10 @@ * BSD-style license; for more info see http://pmd.sourceforge.net/license.html */ +/* + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ + package net.sourceforge.pmd.lang.ast.internal; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TraversalUtils.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java similarity index 100% rename from pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/TraversalUtils.java rename to pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/internal/TraversalUtils.java diff --git a/pmd-cpp/src/main/ant/alljavacc.xml b/pmd-cpp/src/main/ant/alljavacc.xml index 9347d126e4..13a825f14a 100644 --- a/pmd-cpp/src/main/ant/alljavacc.xml +++ b/pmd-cpp/src/main/ant/alljavacc.xml @@ -36,7 +36,7 @@ javacchome="${javacc-home.path}" /> + value="class CppParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> @@ -92,5 +92,5 @@ public class Token implements GenericToken, java.io.Serializable]]> - + diff --git a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java index 83aa097c39..3ec9de934e 100644 --- a/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java +++ b/pmd-cpp/src/main/java/net/sourceforge/pmd/lang/cpp/CppTokenManager.java @@ -17,7 +17,7 @@ public class CppTokenManager implements TokenManager { /** * Creates a new C++ Token Manager from the given source code. - * + * * @param source * the source code */ @@ -30,8 +30,4 @@ public class CppTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - CppParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java index c8caeb354e..21a224367a 100644 --- a/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java +++ b/pmd-cpp/src/test/java/net/sourceforge/pmd/cpd/CPPTokenizerTest.java @@ -41,7 +41,7 @@ public class CPPTokenizerTest { assertNotSame(TokenEntry.getEOF(), tokens.getTokens().get(0)); assertEquals(24, tokens.size()); } - + @Test public void testIgnoreBetweenSpecialComments() { String code = "#include \n" + "#include \n" + "\n" + "// CPD-OFF\n" @@ -155,7 +155,7 @@ public class CPPTokenizerTest { tokenizer.setProperties(properties); expectedException.expect(TokenMgrError.class); - expectedException.expectMessage("Lexical error in file issue-1559.cpp at"); + expectedException.expectMessage("Lexical error in file issue-1559.cpp"); tokenizer.tokenize(code, new Tokens()); } diff --git a/pmd-dart/src/main/java/net/sourceforge/pmd/cpd/DartTokenizer.java b/pmd-dart/src/main/java/net/sourceforge/pmd/cpd/DartTokenizer.java index 9afb02b1dd..8eba7904a9 100644 --- a/pmd-dart/src/main/java/net/sourceforge/pmd/cpd/DartTokenizer.java +++ b/pmd-dart/src/main/java/net/sourceforge/pmd/cpd/DartTokenizer.java @@ -20,7 +20,7 @@ public class DartTokenizer extends AntlrTokenizer { @Override protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) { CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode); - return new AntlrTokenManager(new Dart2Lexer(charStream), sourceCode.getFileName()); + return new AntlrTokenManager(new Dart2Lexer(charStream)); } @Override diff --git a/pmd-go/src/main/java/net/sourceforge/pmd/cpd/GoTokenizer.java b/pmd-go/src/main/java/net/sourceforge/pmd/cpd/GoTokenizer.java index ff99a98129..c45fb1479d 100644 --- a/pmd-go/src/main/java/net/sourceforge/pmd/cpd/GoTokenizer.java +++ b/pmd-go/src/main/java/net/sourceforge/pmd/cpd/GoTokenizer.java @@ -18,6 +18,6 @@ public class GoTokenizer extends AntlrTokenizer { @Override protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) { CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode); - return new AntlrTokenManager(new GolangLexer(charStream), sourceCode.getFileName()); + return new AntlrTokenManager(new GolangLexer(charStream)); } } diff --git a/pmd-java/etc/grammar/Java.jjt b/pmd-java/etc/grammar/Java.jjt index 0ea73f7c3e..bc5c227947 100644 --- a/pmd-java/etc/grammar/Java.jjt +++ b/pmd-java/etc/grammar/Java.jjt @@ -252,7 +252,7 @@ import java.util.List; import java.util.Map; import net.sourceforge.pmd.lang.ast.CharStream; import net.sourceforge.pmd.lang.ast.GenericToken; -import net.sourceforge.pmd.lang.ast.impl.JavaccToken; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; import net.sourceforge.pmd.lang.ast.TokenMgrError; import net.sourceforge.pmd.lang.ast.Node; @@ -1551,7 +1551,7 @@ ASTCompilationUnit CompilationUnit() : { jjtThis.setComments(token_source.comments); - jjtThis.setTokenDocument(((net.sourceforge.pmd.lang.ast.impl.JavaCharStream) token_source.input_stream).getTokenDocument()); + jjtThis.setTokenDocument(((net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream) token_source.input_stream).getTokenDocument()); return jjtThis; } } diff --git a/pmd-java/src/main/ant/alljavacc.xml b/pmd-java/src/main/ant/alljavacc.xml index 5d4d3e64e9..e16d618d9a 100644 --- a/pmd-java/src/main/ant/alljavacc.xml +++ b/pmd-java/src/main/ant/alljavacc.xml @@ -178,7 +178,7 @@ + value="import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; public class JavaParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java index ca7297c43a..68fc2a600d 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/JavaTokenManager.java @@ -25,8 +25,4 @@ public class JavaTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - tokenManager.setFileName(fileName); - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java index 6f34b25ff8..163a6e821c 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/ASTCompilationUnit.java @@ -23,7 +23,6 @@ public final class ASTCompilationUnit extends AbstractJavaTypeNode implements Ro private ClassTypeResolver classTypeResolver; private List comments; private Map noPmdComments = Collections.emptyMap(); - private CharSequence fileText; private TokenDocument tokenDocument; ASTCompilationUnit(int id) { diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java index 3717d2d389..4bb2ab6cce 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/AbstractJavaNode.java @@ -7,14 +7,13 @@ package net.sourceforge.pmd.lang.java.ast; import org.apache.commons.lang3.ArrayUtils; import org.checkerframework.checker.nullness.qual.NonNull; -import net.sourceforge.pmd.lang.ast.GenericToken; +import net.sourceforge.pmd.lang.ast.AbstractNode; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.impl.javacc.AbstractJjtreeNode; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable; import net.sourceforge.pmd.lang.symboltable.Scope; -abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { +abstract class AbstractJavaNode extends AbstractNode implements JavaNode { protected JavaParser parser; private Scope scope; @@ -22,14 +21,13 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { private Comment comment; private ASTCompilationUnit root; private CharSequence text; - private final int id; AbstractJavaNode(int id) { - this.id = id; + super(id); } AbstractJavaNode(JavaParser parser, int id) { - this.id = id; + super(id); this.parser = parser; } @@ -122,6 +120,16 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { return root; } + @Override + public JavaccToken jjtGetFirstToken() { + return (JavaccToken) firstToken; + } + + @Override + public JavaccToken jjtGetLastToken() { + return (JavaccToken) lastToken; + } + /** * Replaces the child at index idx with its own children. */ @@ -188,20 +196,20 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { } private void enlargeLeft(AbstractJavaNode child) { - JavaccToken thisFst = this.getFirstToken(); - JavaccToken childFst = child.getFirstToken(); + JavaccToken thisFst = this.jjtGetFirstToken(); + JavaccToken childFst = child.jjtGetFirstToken(); if (TokenUtils.isBefore(childFst, thisFst)) { - setFirstToken(childFst); + this.jjtSetFirstToken(childFst); } } private void enlargeRight(AbstractJavaNode child) { - JavaccToken thisLast = this.getLastToken(); - JavaccToken childLast = child.getLastToken(); + JavaccToken thisLast = this.jjtGetLastToken(); + JavaccToken childLast = child.jjtGetLastToken(); if (TokenUtils.isAfter(childLast, thisLast)) { - setLastToken(childLast); + this.jjtSetLastToken(childLast); } } @@ -227,20 +235,14 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { */ void shiftTokens(int leftShift, int rightShift) { if (leftShift != 0) { - jjtSetFirstToken(findTokenSiblingInThisNode(this.getFirstToken(), leftShift)); + jjtSetFirstToken(findTokenSiblingInThisNode(this.jjtGetFirstToken(), leftShift)); } if (rightShift != 0) { - jjtSetLastToken(findTokenSiblingInThisNode(this.getLastToken(), rightShift)); + jjtSetLastToken(findTokenSiblingInThisNode(this.jjtGetLastToken(), rightShift)); } } - void jjtSetFirstToken(JavaccToken token) { - super.setFirstToken(token); - } - - void jjtSetLastToken(JavaccToken token) { - super.setLastToken(token); - } + // these make the setter visible to the parser private JavaccToken findTokenSiblingInThisNode(JavaccToken token, int shift) { assert token != null : "Null token"; @@ -248,7 +250,7 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { return token; } else if (shift < 0) { // expects a positive shift - return TokenUtils.nthPrevious(this.getFirstToken(), token, -shift); + return TokenUtils.nthPrevious(this.jjtGetFirstToken(), token, -shift); } else { return TokenUtils.nthFollower(token, shift); } @@ -256,8 +258,8 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { void copyTextCoordinates(AbstractJavaNode copy) { - setFirstToken(copy.getFirstToken()); - setLastToken(copy.getLastToken()); + this.jjtSetFirstToken(copy.jjtGetFirstToken()); + this.jjtSetLastToken(copy.jjtGetLastToken()); } @@ -292,11 +294,11 @@ abstract class AbstractJavaNode extends AbstractJjtreeNode implements JavaNode { } private int getStartOffset() { - return this.getFirstToken().getStartInDocument(); + return this.jjtGetFirstToken().getStartInDocument(); } private int getEndOffset() { - return this.getLastToken().getEndInDocument(); + return this.jjtGetLastToken().getEndInDocument(); } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java index ee44483b59..d881f6c061 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/InternalApiBridge.java @@ -8,7 +8,6 @@ import java.io.Reader; import net.sourceforge.pmd.annotation.InternalApi; import net.sourceforge.pmd.lang.ParserOptions; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; import net.sourceforge.pmd.lang.java.ast.internal.LanguageLevelChecker; import net.sourceforge.pmd.lang.java.qname.JavaOperationQualifiedName; @@ -73,7 +72,6 @@ public final class InternalApiBridge { parser.setJdkVersion(checker.getJdkVersion()); parser.setPreview(checker.isPreviewEnabled()); - AbstractTokenManager.setFileName(fileName); ASTCompilationUnit acu = parser.CompilationUnit(); acu.setNoPmdComments(parser.getSuppressMap()); checker.check(acu); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java index 733233e9fd..5b5effa582 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaNode.java @@ -8,7 +8,6 @@ package net.sourceforge.pmd.lang.java.ast; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; -import net.sourceforge.pmd.lang.ast.GenericToken; import net.sourceforge.pmd.lang.ast.TextAvailableNode; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; import net.sourceforge.pmd.lang.java.symbols.table.JSymbolTable; @@ -87,10 +86,10 @@ public interface JavaNode extends ScopedNode, TextAvailableNode { JavaNode jjtGetParent(); - JavaccToken getFirstToken(); + JavaccToken jjtGetFirstToken(); - JavaccToken getLastToken(); + JavaccToken jjtGetLastToken(); /** diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java index 7bb5b4c6e0..a21d3e2feb 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/ast/JavaTokenFactory.java @@ -5,9 +5,9 @@ package net.sourceforge.pmd.lang.java.ast; import net.sourceforge.pmd.lang.ast.CharStream; +import net.sourceforge.pmd.lang.ast.impl.TokenDocument; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; -import net.sourceforge.pmd.lang.ast.impl.TokenDocument; final class JavaTokenFactory { @@ -65,7 +65,7 @@ final class JavaTokenFactory { private static final class LazyImageToken extends JavaccToken { - public LazyImageToken(int kind, int startInclusive, int endExclusive, TokenDocument document) { + LazyImageToken(int kind, int startInclusive, int endExclusive, TokenDocument document) { super(kind, null, startInclusive, endExclusive, document); } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java index f730f79d0d..0aa317d2c8 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/documentation/CommentRequiredRule.java @@ -225,7 +225,7 @@ public class CommentRequiredRule extends AbstractCommentRule { && field.isStatic() && field.isFinal() && field.isArray() - && "ObjectStreamField".equals(field.getFirstToken().getImage()); // .getType() returns null + && "ObjectStreamField".equals(field.jjtGetFirstToken().getImage()); // .getType() returns null } @Override diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt index 2b9e2d7b54..5380f95778 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TestExtensions.kt @@ -31,9 +31,9 @@ infix fun KCallable>.shouldBePresent(any: U) = this shoul fun JavaNode.tokenList(): List { val lst = mutableListOf() - var t = firstToken + var t = jjtGetFirstToken() lst += t - while (t != lastToken) { + while (t != jjtGetLastToken()) { t = t.next lst += t } diff --git a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TokenUtilsTest.kt b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TokenUtilsTest.kt index d950316852..0efda1d4af 100644 --- a/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TokenUtilsTest.kt +++ b/pmd-java/src/test/kotlin/net/sourceforge/pmd/lang/java/ast/TokenUtilsTest.kt @@ -23,7 +23,7 @@ class TokenUtilsTest : FunSpec({ class Foo { /* wassup */ abstract void bar(); } """.trimIndent()) - val fileTokens = generateSequence(decl.root.firstToken) { it.next }.toList() + val fileTokens = generateSequence(decl.root.jjtGetFirstToken()) { it.next }.toList() fileTokens.map { it.image } shouldBe listOf( // for some reason there's 2 EOF tokens but that's not the point of this test diff --git a/pmd-javascript/src/main/ant/alljavacc.xml b/pmd-javascript/src/main/ant/alljavacc.xml index 6f7a0a7bc4..8cbcad70bd 100644 --- a/pmd-javascript/src/main/ant/alljavacc.xml +++ b/pmd-javascript/src/main/ant/alljavacc.xml @@ -36,7 +36,7 @@ javacchome="${javacc-home.path}" /> + value="class Ecmascript5ParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript5/Ecmascript5TokenManager.java b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript5/Ecmascript5TokenManager.java index bdfea305bc..89addddb05 100644 --- a/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript5/Ecmascript5TokenManager.java +++ b/pmd-javascript/src/main/java/net/sourceforge/pmd/lang/ecmascript5/Ecmascript5TokenManager.java @@ -31,8 +31,4 @@ public class Ecmascript5TokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - Ecmascript5ParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-jsp/src/main/ant/alljavacc.xml b/pmd-jsp/src/main/ant/alljavacc.xml index 34207aa4a6..87aaa7524e 100644 --- a/pmd-jsp/src/main/ant/alljavacc.xml +++ b/pmd-jsp/src/main/ant/alljavacc.xml @@ -45,7 +45,7 @@ + value="class JspParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspParser.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspParser.java index 2906daa765..47b703832a 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspParser.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspParser.java @@ -9,7 +9,6 @@ import java.io.Reader; import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.ast.SimpleCharStream; @@ -30,7 +29,7 @@ public class JspParser extends AbstractParser { @Override public Node parse(String fileName, Reader source) throws ParseException { - AbstractTokenManager.setFileName(fileName); + return new net.sourceforge.pmd.lang.jsp.ast.JspParser(new SimpleCharStream(source)).CompilationUnit(); } diff --git a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspTokenManager.java b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspTokenManager.java index 1342951e4a..0e60d5d50f 100644 --- a/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspTokenManager.java +++ b/pmd-jsp/src/main/java/net/sourceforge/pmd/lang/jsp/JspTokenManager.java @@ -25,8 +25,4 @@ public class JspTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - JspParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java index 7149f28d96..52d3dbc75d 100644 --- a/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java +++ b/pmd-jsp/src/test/java/net/sourceforge/pmd/lang/jsp/ast/AbstractJspNodesTst.java @@ -10,8 +10,8 @@ import java.io.StringReader; import java.util.HashSet; import java.util.Set; -import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; public abstract class AbstractJspNodesTst { diff --git a/pmd-kotlin/src/main/java/net/sourceforge/pmd/cpd/KotlinTokenizer.java b/pmd-kotlin/src/main/java/net/sourceforge/pmd/cpd/KotlinTokenizer.java index 963ea09c3c..5c6ee52299 100644 --- a/pmd-kotlin/src/main/java/net/sourceforge/pmd/cpd/KotlinTokenizer.java +++ b/pmd-kotlin/src/main/java/net/sourceforge/pmd/cpd/KotlinTokenizer.java @@ -20,7 +20,7 @@ public class KotlinTokenizer extends AntlrTokenizer { @Override protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) { CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode); - return new AntlrTokenManager(new Kotlin(charStream), sourceCode.getFileName()); + return new AntlrTokenManager(new Kotlin(charStream)); } @Override diff --git a/pmd-lua/src/main/java/net/sourceforge/pmd/cpd/LuaTokenizer.java b/pmd-lua/src/main/java/net/sourceforge/pmd/cpd/LuaTokenizer.java index effae4f797..86bae21668 100644 --- a/pmd-lua/src/main/java/net/sourceforge/pmd/cpd/LuaTokenizer.java +++ b/pmd-lua/src/main/java/net/sourceforge/pmd/cpd/LuaTokenizer.java @@ -19,7 +19,7 @@ public class LuaTokenizer extends AntlrTokenizer { @Override protected AntlrTokenManager getLexerForSource(SourceCode sourceCode) { CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode); - return new AntlrTokenManager(new LuaLexer(charStream), sourceCode.getFileName()); + return new AntlrTokenManager(new LuaLexer(charStream)); } @Override diff --git a/pmd-matlab/src/main/ant/alljavacc.xml b/pmd-matlab/src/main/ant/alljavacc.xml index ecf431f7b6..a9bd18eeca 100644 --- a/pmd-matlab/src/main/ant/alljavacc.xml +++ b/pmd-matlab/src/main/ant/alljavacc.xml @@ -36,7 +36,7 @@ javacchome="${javacc-home.path}" /> + value="class MatlabParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-matlab/src/main/java/net/sourceforge/pmd/lang/matlab/MatlabTokenManager.java b/pmd-matlab/src/main/java/net/sourceforge/pmd/lang/matlab/MatlabTokenManager.java index 5b620f7664..8077cdeeef 100644 --- a/pmd-matlab/src/main/java/net/sourceforge/pmd/lang/matlab/MatlabTokenManager.java +++ b/pmd-matlab/src/main/java/net/sourceforge/pmd/lang/matlab/MatlabTokenManager.java @@ -31,8 +31,4 @@ public class MatlabTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - MatlabParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-objectivec/src/main/ant/alljavacc.xml b/pmd-objectivec/src/main/ant/alljavacc.xml index 26ab8cd3d9..3e6cae2ad5 100644 --- a/pmd-objectivec/src/main/ant/alljavacc.xml +++ b/pmd-objectivec/src/main/ant/alljavacc.xml @@ -36,7 +36,7 @@ javacchome="${javacc-home.path}" /> + value="class ObjectiveCParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-objectivec/src/main/java/net/sourceforge/pmd/lang/objectivec/ObjectiveCTokenManager.java b/pmd-objectivec/src/main/java/net/sourceforge/pmd/lang/objectivec/ObjectiveCTokenManager.java index ec573e5d96..59c1fecaee 100644 --- a/pmd-objectivec/src/main/java/net/sourceforge/pmd/lang/objectivec/ObjectiveCTokenManager.java +++ b/pmd-objectivec/src/main/java/net/sourceforge/pmd/lang/objectivec/ObjectiveCTokenManager.java @@ -31,8 +31,4 @@ public class ObjectiveCTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - ObjectiveCParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-plsql/src/main/ant/alljavacc.xml b/pmd-plsql/src/main/ant/alljavacc.xml index c161ad16ca..3636d10959 100644 --- a/pmd-plsql/src/main/ant/alljavacc.xml +++ b/pmd-plsql/src/main/ant/alljavacc.xml @@ -80,7 +80,7 @@ value="PLSQLNode" /> + value="class PLSQLParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> public class + value="class PythonParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-python/src/main/java/net/sourceforge/pmd/lang/python/PythonTokenManager.java b/pmd-python/src/main/java/net/sourceforge/pmd/lang/python/PythonTokenManager.java index 30a3aa72fc..cd43684c26 100644 --- a/pmd-python/src/main/java/net/sourceforge/pmd/lang/python/PythonTokenManager.java +++ b/pmd-python/src/main/java/net/sourceforge/pmd/lang/python/PythonTokenManager.java @@ -31,8 +31,4 @@ public class PythonTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - PythonParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/cpd/SwiftTokenizer.java b/pmd-swift/src/main/java/net/sourceforge/pmd/cpd/SwiftTokenizer.java index d18cd28793..9088673de5 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/cpd/SwiftTokenizer.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/cpd/SwiftTokenizer.java @@ -18,6 +18,6 @@ public class SwiftTokenizer extends AntlrTokenizer { @Override protected AntlrTokenManager getLexerForSource(final SourceCode sourceCode) { CharStream charStream = AntlrTokenizer.getCharStreamFromSourceCode(sourceCode); - return new AntlrTokenManager(new SwiftLexer(charStream), sourceCode.getFileName()); + return new AntlrTokenManager(new SwiftLexer(charStream)); } } diff --git a/pmd-visualforce/src/main/ant/alljavacc.xml b/pmd-visualforce/src/main/ant/alljavacc.xml index b2463ff956..f3e8198e2f 100644 --- a/pmd-visualforce/src/main/ant/alljavacc.xml +++ b/pmd-visualforce/src/main/ant/alljavacc.xml @@ -33,7 +33,7 @@ javacchome="${javacc-home.path}" /> - + value="class VfParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfParser.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfParser.java index 9a3dd235ab..565c547f3f 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfParser.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfParser.java @@ -9,7 +9,6 @@ import java.io.Reader; import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; @@ -29,7 +28,7 @@ public class VfParser extends AbstractParser { @Override public Node parse(String fileName, Reader source) throws ParseException { - AbstractTokenManager.setFileName(fileName); + return new net.sourceforge.pmd.lang.vf.ast.VfParser(new VfSimpleCharStream(source)).CompilationUnit(); } diff --git a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfTokenManager.java b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfTokenManager.java index 9d531d4f1c..5078a59fd0 100644 --- a/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfTokenManager.java +++ b/pmd-visualforce/src/main/java/net/sourceforge/pmd/lang/vf/VfTokenManager.java @@ -25,8 +25,4 @@ public class VfTokenManager implements TokenManager { return tokenManager.getNextToken(); } - @Override - public void setFileName(String fileName) { - VfParserTokenManager.setFileName(fileName); - } } diff --git a/pmd-visualforce/src/test/java/net/sourceforge/pmd/lang/vf/ast/AbstractVfNodesTest.java b/pmd-visualforce/src/test/java/net/sourceforge/pmd/lang/vf/ast/AbstractVfNodesTest.java index 375624aaef..e846ceeb05 100644 --- a/pmd-visualforce/src/test/java/net/sourceforge/pmd/lang/vf/ast/AbstractVfNodesTest.java +++ b/pmd-visualforce/src/test/java/net/sourceforge/pmd/lang/vf/ast/AbstractVfNodesTest.java @@ -10,8 +10,8 @@ import java.io.StringReader; import java.util.HashSet; import java.util.Set; -import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; import net.sourceforge.pmd.lang.ast.Node; +import net.sourceforge.pmd.lang.ast.impl.javacc.JavaCharStream; public abstract class AbstractVfNodesTest { diff --git a/pmd-vm/src/main/ant/alljavacc.xml b/pmd-vm/src/main/ant/alljavacc.xml index 845e8da6f7..d71ec6bbfb 100644 --- a/pmd-vm/src/main/ant/alljavacc.xml +++ b/pmd-vm/src/main/ant/alljavacc.xml @@ -46,7 +46,7 @@ + value="class VmParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" /> diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmParser.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmParser.java index 812d924b86..e4a06d0595 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmParser.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmParser.java @@ -9,7 +9,6 @@ import java.io.Reader; import net.sourceforge.pmd.lang.AbstractParser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.vm.util.VelocityCharStream; @@ -30,7 +29,7 @@ public class VmParser extends AbstractParser { @Override public Node parse(final String fileName, final Reader source) throws ParseException { - AbstractTokenManager.setFileName(fileName); + return new net.sourceforge.pmd.lang.vm.ast.VmParser(new VelocityCharStream(source, 1, 1)).process(); } diff --git a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmTokenManager.java b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmTokenManager.java index 4d8f01e46c..e43e3345e8 100644 --- a/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmTokenManager.java +++ b/pmd-vm/src/main/java/net/sourceforge/pmd/lang/vm/VmTokenManager.java @@ -7,7 +7,6 @@ package net.sourceforge.pmd.lang.vm; import java.io.Reader; import net.sourceforge.pmd.lang.TokenManager; -import net.sourceforge.pmd.lang.ast.AbstractTokenManager; import net.sourceforge.pmd.lang.vm.ast.VmParserTokenManager; import net.sourceforge.pmd.lang.vm.util.VelocityCharStream; @@ -24,9 +23,4 @@ public class VmTokenManager implements TokenManager { return vmParserTokenManager.getNextToken(); } - @Override - public void setFileName(final String fileName) { - AbstractTokenManager.setFileName(fileName); - } - }