Add supertype to swift nodes

This commit is contained in:
Clément Fournier
2020-04-23 21:24:54 +02:00
parent e61f03aafa
commit 164c1361a6
3 changed files with 40 additions and 10 deletions

View File

@@ -5,19 +5,19 @@
package net.sourceforge.pmd.lang.ast.impl.antlr4;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import net.sourceforge.pmd.util.DataMap;
import net.sourceforge.pmd.util.DataMap.DataKey;
public abstract class AntlrBaseNode extends ParserRuleContext implements AntlrNode {
public abstract class AntlrBaseNode<I extends AntlrBaseNode<I>> extends ParserRuleContext implements AntlrNode {
private final DataMap<DataKey<?, ?>> userData = DataMap.newDataMap();
/**
* Constructor required by {@link ParserRuleContext}
*/
@SuppressWarnings("unused")
public AntlrBaseNode() {
protected AntlrBaseNode() {
// Nothing to be done
}
@@ -27,8 +27,7 @@ public abstract class AntlrBaseNode extends ParserRuleContext implements AntlrNo
* @param parent The parent
* @param invokingStateNumber the invokingState defined by {@link org.antlr.v4.runtime.RuleContext} parent
*/
@SuppressWarnings("unused")
public AntlrBaseNode(final ParserRuleContext parent, final int invokingStateNumber) {
protected AntlrBaseNode(final ParserRuleContext parent, final int invokingStateNumber) {
super(parent, invokingStateNumber);
}
@@ -70,15 +69,17 @@ public abstract class AntlrBaseNode extends ParserRuleContext implements AntlrNo
}
@Override
public AntlrNode getChild(int i) {
return (AntlrNode) super.getChild(i);
public I getChild(int i) {
return cast(super.getChild(i));
}
@Override
public AntlrBaseNode getParent() {
return (AntlrBaseNode) super.getParent();
public I getParent() {
return cast(super.getParent());
}
protected abstract I cast(ParseTree o);
@Override
public int getNumChildren() {
return getChildCount();

View File

@@ -54,7 +54,7 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.*;
}
options {
contextSuperClass = AntlrBaseNode;
contextSuperClass = SwiftNode;
superClass = PmdAntlrParserBase;
}

View File

@@ -0,0 +1,29 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.swift.ast;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.tree.ParseTree;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseNode;
/**
* Supertype of all swift nodes.
*/
public abstract class SwiftNode extends AntlrBaseNode<SwiftNode> {
protected SwiftNode() {
super();
}
protected SwiftNode(final ParserRuleContext parent, final int invokingStateNumber) {
super(parent, invokingStateNumber);
}
@Override
protected SwiftNode cast(ParseTree o) {
return (SwiftNode) o;
}
}