Make modelica visitor generic

This commit is contained in:
Clément Fournier
2020-08-07 03:15:33 +02:00
parent 285e7f2e94
commit 9ed0da4ed5
25 changed files with 88 additions and 25 deletions

View File

@ -500,6 +500,15 @@ public interface" />
<replacefilter token="default R visit${node-name}(${node-name} node, P data) { for (int i = 0, len = node.getNumChildren(); i &lt; len; i++) node.getChild(i).jjtAccept(this, data); return data; }" <replacefilter token="default R visit${node-name}(${node-name} node, P data) { for (int i = 0, len = node.getNumChildren(); i &lt; len; i++) node.getChild(i).jjtAccept(this, data); return data; }"
value="default R visit${node-name}(${node-name} node, P data) { return visitNode(node, data); }" /> value="default R visit${node-name}(${node-name} node, P data) { return visitNode(node, data); }" />
</replace> </replace>
<replace>
<replacefilter token="public Object jjtAccept(${lang-name}ParserVisitor visitor, Object data)"
value="@Override protected &lt;P, R> R accept${lang-name}Visitor(${lang-name}Visitor&lt;? super P, ? extends R> visitor, P data)" />
<fileset dir="${target-package-dir}">
<include name="${ast-prefix}*" />
</fileset>
</replace>
</target> </target>
</project> </project>

View File

@ -40,7 +40,7 @@
<phase>generate-sources</phase> <phase>generate-sources</phase>
<configuration> <configuration>
<target> <target>
<ant antfile="${javacc.ant.wrapper}" target="alljavacc"> <ant antfile="${javacc.ant.wrapper}" target="alljavacc-visitor+">
<property name="javacc.jar" value="${javacc.jar}" /> <property name="javacc.jar" value="${javacc.jar}" />
<property name="lang-name" value="Modelica" /> <property name="lang-name" value="Modelica" />
<property name="lang-terse-name" value="modelica" /> <property name="lang-terse-name" value="modelica" />

View File

@ -68,7 +68,7 @@ public class ASTClassDefinition extends AbstractModelicaNode {
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }
} }

View File

@ -56,7 +56,7 @@ public final class ASTComponentReference extends AbstractModelicaNode implements
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -10,7 +10,7 @@ public final class ASTDerClassSpecifier extends AbstractModelicaClassSpecifierNo
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }
} }

View File

@ -20,7 +20,7 @@ public class ASTElementList extends AbstractModelicaNode {
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }
} }

View File

@ -10,7 +10,7 @@ public final class ASTEnumerationShortClassSpecifier extends AbstractModelicaCla
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }
} }

View File

@ -12,7 +12,7 @@ public final class ASTExtendingLongClassSpecifier extends AbstractModelicaClassS
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -25,7 +25,7 @@ public final class ASTMultipleDefinitionImportClause extends AbstractModelicaImp
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -19,7 +19,7 @@ public final class ASTName extends AbstractModelicaNode implements ResolvableMod
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -19,7 +19,7 @@ public final class ASTRenamingImportClause extends AbstractModelicaImportClause
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -12,7 +12,7 @@ public final class ASTSimpleLongClassSpecifier extends AbstractModelicaClassSpec
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -13,7 +13,7 @@ public final class ASTSimpleShortClassSpecifier extends AbstractModelicaClassSpe
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -19,7 +19,7 @@ public class ASTSingleDefinitionImportClause extends AbstractModelicaImportClaus
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -18,7 +18,7 @@ public class ASTStoredDefinition extends AbstractModelicaNode implements RootNod
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -21,7 +21,7 @@ public final class ASTUnqualifiedImportClause extends AbstractModelicaImportClau
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -10,7 +10,7 @@ public final class ASTWithinClause extends AbstractModelicaNode {
} }
@Override @Override
public Object jjtAccept(ModelicaParserVisitor visitor, Object data) { protected <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data) {
return visitor.visit(this, data); return visitor.visit(this, data);
} }

View File

@ -30,8 +30,8 @@ abstract class AbstractModelicaNode extends AbstractJjtreeNode<AbstractModelicaN
super.setImage(image); super.setImage(image);
} }
@Override
public abstract Object jjtAccept(ModelicaParserVisitor visitor, Object data); protected abstract <P, R> R acceptModelicaVisitor(ModelicaVisitor<? super P, ? extends R> visitor, P data);
@Override @Override
public String getXPathNodeName() { public String getXPathNodeName() {

View File

@ -4,6 +4,8 @@
package net.sourceforge.pmd.lang.modelica.ast; package net.sourceforge.pmd.lang.modelica.ast;
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
import net.sourceforge.pmd.lang.ast.AstVisitor;
import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode; import net.sourceforge.pmd.lang.ast.impl.javacc.JjtreeNode;
import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope; import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope;
@ -12,6 +14,17 @@ import net.sourceforge.pmd.lang.modelica.resolver.ModelicaScope;
*/ */
public interface ModelicaNode extends JjtreeNode<ModelicaNode> { public interface ModelicaNode extends JjtreeNode<ModelicaNode> {
/**
* @deprecated Use {@link #acceptVisitor(AstVisitor, Object)}
*/
@DeprecatedUntil700
@Deprecated
default Object jjtAccept(ModelicaParserVisitor visitor, Object data) {
return acceptVisitor(visitor, data);
}
/** /**
* Returns the lexical scope this node is contained in. * Returns the lexical scope this node is contained in.
*/ */
@ -24,5 +37,4 @@ public interface ModelicaNode extends JjtreeNode<ModelicaNode> {
*/ */
ModelicaScope getMostSpecificScope(); ModelicaScope getMostSpecificScope();
Object jjtAccept(ModelicaParserVisitor visitor, Object data);
} }

View File

@ -0,0 +1,25 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.modelica.ast;
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
import net.sourceforge.pmd.lang.ast.Node;
/**
* Backwards-compatibility only.
*
* @deprecated Use {@link ModelicaVisitor}
*/
@Deprecated
@DeprecatedUntil700
public interface ModelicaParserVisitor extends ModelicaVisitor<Object, Object> {
@Override
default Object visitNode(Node node, Object param) {
node.children().forEach(c -> c.acceptVisitor(this, param));
return param;
}
}

View File

@ -4,5 +4,10 @@
package net.sourceforge.pmd.lang.modelica.ast; package net.sourceforge.pmd.lang.modelica.ast;
public class ModelicaParserVisitorAdapter implements ModelicaParserVisitor { import net.sourceforge.pmd.annotation.DeprecatedUntil700;
@Deprecated
@DeprecatedUntil700
public class ModelicaParserVisitorAdapter extends ModelicaVisitorBase<Object, Object> implements ModelicaParserVisitor {
} }

View File

@ -0,0 +1,14 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.modelica.ast;
import net.sourceforge.pmd.lang.ast.AstVisitorBase;
/**
* Base implementation of {@link ModelicaVisitor}.
*/
public class ModelicaVisitorBase<P, R> extends AstVisitorBase<P, R> implements ModelicaVisitor<P, R> {
}

View File

@ -9,6 +9,6 @@ import net.sourceforge.pmd.lang.modelica.ast.ASTStoredDefinition;
public class ModelicaSymbolFacade { public class ModelicaSymbolFacade {
public void initializeWith(ASTStoredDefinition node) { public void initializeWith(ASTStoredDefinition node) {
ScopeAndDeclarationFinder sc = new ScopeAndDeclarationFinder(); ScopeAndDeclarationFinder sc = new ScopeAndDeclarationFinder();
node.jjtAccept(sc, null); node.acceptVisitor(sc, null);
} }
} }

View File

@ -9,7 +9,6 @@ import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.ast.AstProcessingStage; import net.sourceforge.pmd.lang.ast.AstProcessingStage;
import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.modelica.ModelicaLanguageModule; import net.sourceforge.pmd.lang.modelica.ModelicaLanguageModule;
import net.sourceforge.pmd.lang.modelica.ast.ModelicaNode;
import net.sourceforge.pmd.lang.modelica.ast.ModelicaParserVisitor; import net.sourceforge.pmd.lang.modelica.ast.ModelicaParserVisitor;
import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage; import net.sourceforge.pmd.lang.modelica.internal.ModelicaProcessingStage;
import net.sourceforge.pmd.lang.rule.AbstractRule; import net.sourceforge.pmd.lang.rule.AbstractRule;
@ -25,10 +24,9 @@ public abstract class AbstractModelicaRule extends AbstractRule implements Model
@Override @Override
public void apply(Node target, RuleContext ctx) { public void apply(Node target, RuleContext ctx) {
((ModelicaNode) target).jjtAccept(this, ctx); target.acceptVisitor(this, ctx);
} }
@Override @Override
public boolean dependsOn(AstProcessingStage<?> stage) { public boolean dependsOn(AstProcessingStage<?> stage) {
if (!(stage instanceof ModelicaProcessingStage)) { if (!(stage instanceof ModelicaProcessingStage)) {

View File

@ -46,7 +46,7 @@ public class ModelicaResolverTest {
private ModelicaNode findNodeByClassAndImage(ASTStoredDefinition ast, Class<?> clazz, String image) { private ModelicaNode findNodeByClassAndImage(ASTStoredDefinition ast, Class<?> clazz, String image) {
NodeFinder vis = new NodeFinder(clazz, image); NodeFinder vis = new NodeFinder(clazz, image);
ast.jjtAccept(vis, null); ast.acceptVisitor(vis, null);
return vis.getResult(); return vis.getResult();
} }