Cleanup token manager errors
This commit is contained in:
@ -91,22 +91,6 @@
|
||||
<move overwrite="true"
|
||||
file="${target}/net/sourceforge/pmd/lang/ast/dummy/SimpleCharStream.java"
|
||||
tofile="${target}/net/sourceforge/pmd/lang/ast/SimpleCharStream.java" />
|
||||
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/TokenMgrError.java"
|
||||
token="net.sourceforge.pmd.lang.ast.dummy"
|
||||
value="net.sourceforge.pmd.lang.ast" />
|
||||
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/TokenMgrError.java"
|
||||
token="extends Error"
|
||||
value="extends RuntimeException" />
|
||||
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/TokenMgrError.java"
|
||||
token="static final int"
|
||||
value="public static final int" />
|
||||
<replace file="${target}/net/sourceforge/pmd/lang/ast/dummy/TokenMgrError.java">
|
||||
<replacetoken><![CDATA["Lexical error at line "]]></replacetoken>
|
||||
<replacevalue><![CDATA["Lexical error in file " + AbstractTokenManager.getFileName() + " at line "]]></replacevalue>
|
||||
</replace>
|
||||
<move overwrite="true"
|
||||
file="${target}/net/sourceforge/pmd/lang/ast/dummy/TokenMgrError.java"
|
||||
tofile="${target}/net/sourceforge/pmd/lang/ast/TokenMgrError.java" />
|
||||
|
||||
<delete dir="${target}/net/sourceforge/pmd/lang/ast/dummy" />
|
||||
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -11,5 +11,4 @@ public interface TokenManager {
|
||||
// TODO : Change the return to GenericToken in 7.0.0 - maybe even use generics TokenManager<T extends GenericToken>
|
||||
Object getNextToken();
|
||||
|
||||
void setFileName(String fileName);
|
||||
}
|
||||
|
@ -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<String> fileName = new ThreadLocal<>();
|
||||
|
||||
protected Map<Integer, String> 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<Integer, String> getSuppressMap() {
|
||||
return suppressMap;
|
||||
}
|
||||
}
|
@ -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 ? "<EOF> " : "\"" + 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);
|
||||
}
|
||||
}
|
@ -34,7 +34,7 @@ public abstract class AntlrBaseParser<T extends org.antlr.v4.runtime.Parser> 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);
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -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<? extends Node>... 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<? extends Node> 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();
|
||||
}
|
||||
|
||||
}
|
@ -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<Integer, String> suppressMap = new HashMap<>();
|
||||
protected String suppressMarker = PMD.SUPPRESS_MARKER;
|
||||
|
||||
public void setSuppressMarker(String marker) {
|
||||
this.suppressMarker = marker;
|
||||
}
|
||||
|
||||
public Map<Integer, String> getSuppressMap() {
|
||||
return suppressMap;
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -36,7 +36,7 @@
|
||||
javacchome="${javacc-home.path}" />
|
||||
<replace file="${target}/net/sourceforge/pmd/lang/cpp/ast/CppParserTokenManager.java"
|
||||
token="class CppParserTokenManager"
|
||||
value="class CppParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
|
||||
value="class CppParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" />
|
||||
<delete file="${target}/net/sourceforge/pmd/lang/cpp/ast/CharStream.java" />
|
||||
<delete file="${target}/net/sourceforge/pmd/lang/cpp/ast/ParseException.java" />
|
||||
<delete file="${target}/net/sourceforge/pmd/lang/cpp/ast/TokenMgrError.java" />
|
||||
@ -92,5 +92,5 @@ public class Token implements GenericToken, java.io.Serializable]]></replacevalu
|
||||
</replace>
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
</project>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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 <iostream>\n" + "#include <string>\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());
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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() :
|
||||
<EOF>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -178,7 +178,7 @@
|
||||
|
||||
<replace file="${target-package-dir}/JavaParserTokenManager.java"
|
||||
token="public class JavaParserTokenManager"
|
||||
value="import net.sourceforge.pmd.lang.ast.impl.JavaccToken; public class JavaParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
|
||||
value="import net.sourceforge.pmd.lang.ast.impl.javacc.JavaccToken; public class JavaParserTokenManager extends net.sourceforge.pmd.lang.ast.impl.javacc.AbstractTokenManager" />
|
||||
<replace file="${target-package-dir}/JavaParser.java"
|
||||
token="throw new Error"
|
||||
value="throw new RuntimeException" />
|
||||
|
@ -25,8 +25,4 @@ public class JavaTokenManager implements TokenManager {
|
||||
return tokenManager.getNextToken();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFileName(String fileName) {
|
||||
tokenManager.setFileName(fileName);
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,6 @@ public final class ASTCompilationUnit extends AbstractJavaTypeNode implements Ro
|
||||
private ClassTypeResolver classTypeResolver;
|
||||
private List<Comment> comments;
|
||||
private Map<Integer, String> noPmdComments = Collections.emptyMap();
|
||||
private CharSequence fileText;
|
||||
private TokenDocument tokenDocument;
|
||||
|
||||
ASTCompilationUnit(int id) {
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user