Fix error nodes not extending SwiftNode

This commit is contained in:
Clément Fournier 2020-05-02 03:12:56 +02:00
parent cc97807d41
commit 4820e812e3
13 changed files with 7098 additions and 3955 deletions

View File

@ -17,8 +17,10 @@
value="_ctx = _localctx.asAntlrNode();"/>
<replace file="${target-package-dir}/${lang-name}Parser.java"
token="visitor.visitChildren(this)"
value="visitor.visitChildren(asAntlrNode())"/>
token="else return visitor.visitChildren(this);"
value="
else if (visitor instanceof PmdAntlrVisitor) return ((PmdAntlrVisitor&lt;? extends T>) visitor).visitChildren(this);
else return visitor.visitChildren(asAntlrNode());"/>
<replaceregexp flags="g" file="${target-package-dir}/${lang-name}Parser.java">
<regexp pattern="_sempred\(\((\w+)\)_localctx,"/>

View File

@ -39,7 +39,6 @@ public abstract class AntlrBaseRule extends AbstractRule {
* violations on the given rule context.
*
* @return A visitor bound to the given rule context
* @param ctx
*/
public abstract ParseTreeVisitor<Void> buildVisitor(RuleContext ctx);

View File

@ -66,8 +66,8 @@ public abstract class AntlrGeneratedParserBase<N extends GenericNode<N>> extends
}
protected boolean sempred(BaseAntlrInnerNode<N> _localctx, int ruleIndex, int predIndex) {
return sempred(_localctx.asAntlrNode(), ruleIndex, predIndex);
protected boolean sempred(BaseAntlrInnerNode<N> localctx, int ruleIndex, int predIndex) {
return sempred(localctx.asAntlrNode(), ruleIndex, predIndex);
}
}

View File

@ -136,8 +136,10 @@ public class AntlrNameDictionary {
case "->": return "rarrow";
case "<-": return "larrow";
default:
return null;
}
return null;
}
public @NonNull String getXPathNameOfToken(int tokenType) {

View File

@ -1,15 +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;
final class AntlrUtils {
private AntlrUtils() {
// utility class
}
}

View File

@ -59,12 +59,12 @@ public abstract class BaseAntlrInnerNode<N extends GenericNode<N>> extends BaseA
@Override
protected Token getFirstToken() {
public Token getFirstAntlrToken() {
return asAntlrNode().start;
}
@Override
protected Token getLastToken() {
public Token getLastAntlrToken() {
return asAntlrNode().stop;
}

View File

@ -23,12 +23,29 @@ public abstract class BaseAntlrNode<A extends AntlrToPmdParseTreeAdapter<N>, N e
private int indexInParent = -1;
protected BaseAntlrNode() {
// protected
}
protected abstract Token getFirstToken();
/**
* Recurses over the text of all terminal descendants to build the
* text of this node (without spaces). This is extremely inefficient
* and should not be used to write rules. The antlr impl doesn't even
* use a single stringbuilder.
*
* @deprecated Some rules depend on it and have not been rewritten
*/
@Deprecated
public String joinTokenText() {
return asAntlrNode().getText();
}
// these are an implementation detail, meant as a crutch while some
// rules depend on it
// Should be made protected
protected abstract Token getLastToken();
public abstract Token getFirstAntlrToken();
public abstract Token getLastAntlrToken();
@Override
public N getParent() {
@ -37,23 +54,23 @@ public abstract class BaseAntlrNode<A extends AntlrToPmdParseTreeAdapter<N>, N e
@Override
public int getBeginLine() {
return getFirstToken().getLine(); // This goes from 1 to n
return getFirstAntlrToken().getLine(); // This goes from 1 to n
}
@Override
public int getEndLine() {
// FIXME this is not the end line if the stop token spans several lines
return getLastToken().getLine();
return getLastAntlrToken().getLine();
}
@Override
public int getBeginColumn() {
return getFirstToken().getCharPositionInLine() + 1;
return getFirstAntlrToken().getCharPositionInLine() + 1;
}
@Override
public int getEndColumn() {
Token tok = getLastToken();
Token tok = getLastAntlrToken();
return tok.getCharPositionInLine() + tok.getStopIndex() - tok.getStartIndex() + 1;
}

View File

@ -27,9 +27,9 @@ public abstract class BaseAntlrTerminalNode<N extends GenericNode<N>>
BaseAntlrTerminalNode(Token symbol, boolean isError) {
if (isError) {
this.antlrNode = new AntlrTerminalPmdAdapter<>(this, symbol);
} else {
this.antlrNode = new AntlrErrorPmdAdapter<>(this, symbol);
} else {
this.antlrNode = new AntlrTerminalPmdAdapter<>(this, symbol);
}
}
@ -39,12 +39,12 @@ public abstract class BaseAntlrTerminalNode<N extends GenericNode<N>>
}
@Override
protected Token getFirstToken() {
public Token getFirstAntlrToken() {
return antlrNode.symbol;
}
@Override
protected Token getLastToken() {
public Token getLastAntlrToken() {
return antlrNode.symbol;
}

View File

@ -48,7 +48,7 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.*;
}
@Override
public SwiftTerminalNode createPmdError(ParserRuleContext parent, Token t) {
public SwiftErrorNode createPmdError(ParserRuleContext parent, Token t) {
return new SwiftErrorNode(t);
}
}

View File

@ -16,14 +16,4 @@ public interface SwiftNode extends GenericNode<SwiftNode> {
<T> T accept(ParseTreeVisitor<? extends T> visitor);
//
//
// @Override
// default <P, R> R acceptVisitor(AntlrTreeVisitor<P, R, ?> visitor, P data) {
// if (visitor instanceof SwiftVisitor) {
// return acceptVisitor((SwiftVisitor<P, R>) visitor, data);
// }
// throw new IllegalArgumentException("Cannot accept visitor " + visitor);
// }
}

View File

@ -17,7 +17,7 @@ public final class SwiftTerminalNode extends BaseAntlrTerminalNode<SwiftNode> im
@Override
public String getXPathNodeName() {
return SwiftParser.DICO.getXPathNameOfToken(getFirstToken().getType());
return SwiftParser.DICO.getXPathNameOfToken(getFirstAntlrToken().getType());
}
@Override

View File

@ -67,13 +67,11 @@ public class UnavailableFunctionRule extends AbstractSwiftRule {
final List<SwiftParser.StatementContext> statements = ctx.statements().statement();
return false;
// return statements.size() == 1 && FATAL_ERROR.equals(statements.get(0).getFirstToken().getText());
return statements.size() == 1 && FATAL_ERROR.equals(statements.get(0).getFirstAntlrToken().getText());
}
private boolean hasUnavailableModifier(final List<SwiftParser.AttributeContext> attributes) {
return false;
// return attributes.stream().anyMatch(atr -> AVAILABLE_UNAVAILABLE.equals(atr.getText()));
return attributes.stream().anyMatch(atr -> AVAILABLE_UNAVAILABLE.equals(atr.joinTokenText()));
}
};
}