Abstract ant wrapper for antlr

This commit is contained in:
Clément Fournier
2020-05-02 02:57:14 +02:00
parent 3aadb253a3
commit 1359f792c2
11 changed files with 110 additions and 75 deletions

32
antlr4-wrapper.xml Normal file
View File

@ -0,0 +1,32 @@
<!--
~ BSD-style license; for more info see http://pmd.sourceforge.net/license.html
-->
<project name="pmd" default="adapt-antlr-sources" basedir="../../../../">
<property name="target-package-dir" value="${antlr4.outputDirectory}/net/sourceforge/pmd/lang/${lang-terse-name}/ast"/>
<target name="adapt-antlr-sources" description="Adapt antlr sources to the PMD codebase">
<replace file="${target-package-dir}/${lang-name}Parser.java"
token="TopLevelContext extends ${lang-name}InnerNode"
value="TopLevelContext extends ${lang-name}InnerNode implements net.sourceforge.pmd.lang.ast.RootNode"/>
<replace file="${target-package-dir}/${lang-name}Parser.java"
token="_ctx = _localctx;"
value="_ctx = _localctx.asAntlrNode();"/>
<replace file="${target-package-dir}/${lang-name}Parser.java"
token="visitor.visitChildren(this)"
value="visitor.visitChildren(asAntlrNode())"/>
<replaceregexp flags="g" file="${target-package-dir}/${lang-name}Parser.java">
<regexp pattern="_sempred\(\((\w+)\)_localctx,"/>
<substitution expression="_sempred\((\1) asPmdNode(_localctx),"/>
</replaceregexp>
<replace file="${target-package-dir}/${lang-name}Visitor.java"
token="extends ParseTreeVisitor"
value="extends net.sourceforge.pmd.lang.ast.impl.antlr4.PmdAntlrVisitor"/>
</target>
</project>

View File

@ -6,10 +6,12 @@ package net.sourceforge.pmd.lang.ast.impl.antlr4;
import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNode;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.ast.impl.GenericNode; import net.sourceforge.pmd.lang.ast.impl.GenericNode;
/** /**
@ -30,6 +32,11 @@ public abstract class AntlrGeneratedParserBase<N extends GenericNode<N>> extends
public abstract BaseAntlrTerminalNode<N> createPmdTerminal(ParserRuleContext parent, Token t); public abstract BaseAntlrTerminalNode<N> createPmdTerminal(ParserRuleContext parent, Token t);
protected Node asPmdNode(RuleContext ctx) {
return ((BaseAntlrNode.AntlrToPmdParseTreeAdapter<?>) ctx).getPmdNode();
}
protected void enterRule(BaseAntlrInnerNode<N> ptree, int state, int alt) { protected void enterRule(BaseAntlrInnerNode<N> ptree, int state, int alt) {
enterRule(ptree.asAntlrNode(), state, alt); enterRule(ptree.asAntlrNode(), state, alt);
} }

View File

@ -23,8 +23,6 @@ import org.checkerframework.checker.nullness.qual.Nullable;
*/ */
public class AntlrNameDictionary { public class AntlrNameDictionary {
public static final int ROOT_RULE_IDX = -1;
private final String[] terminalXpathNames; private final String[] terminalXpathNames;
private final String[] nonTermXpathNames; private final String[] nonTermXpathNames;
private final Vocabulary vocabulary; private final Vocabulary vocabulary;
@ -155,12 +153,7 @@ public class AntlrNameDictionary {
} }
public @NonNull String getXPathNameOfRule(int idx) { public @NonNull String getXPathNameOfRule(int idx) {
if (idx >= 0 && idx < nonTermXpathNames.length) { return nonTermXpathNames[idx];
return nonTermXpathNames[idx];
} else if (idx == ROOT_RULE_IDX) {
return "FileRoot";
}
throw new IllegalArgumentException("I don't know rule type " + idx);
} }
public int getMaxRuleIndex() { public int getMaxRuleIndex() {

View File

@ -17,9 +17,33 @@
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<!-- this needs to run before the ant task -->
<groupId>org.antlr</groupId> <groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId> <artifactId>antlr4-maven-plugin</artifactId>
</plugin> </plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<inherited>true</inherited>
<executions>
<execution>
<id>antlr-cleanup</id>
<phase>generate-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<ant antfile="${antlr4.ant.wrapper}">
<property name="lang-name" value="Swift" />
<property name="lang-terse-name" value="swift" />
</ant>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin> <plugin>
<artifactId>maven-resources-plugin</artifactId> <artifactId>maven-resources-plugin</artifactId>
<configuration> <configuration>

View File

@ -42,6 +42,7 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.*;
static final AntlrNameDictionary DICO = new AntlrNameDictionary(VOCABULARY, ruleNames); static final AntlrNameDictionary DICO = new AntlrNameDictionary(VOCABULARY, ruleNames);
@Override
public SwiftTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) { public SwiftTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
return new SwiftTerminalNode(t); return new SwiftTerminalNode(t);
} }

View File

@ -10,20 +10,21 @@ import org.antlr.v4.runtime.Lexer;
import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.ParserOptions;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser; import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser;
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext;
/** /**
* Adapter for the SwiftParser. * Adapter for the SwiftParser.
*/ */
public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, SwiftFileNode> { public final class PmdSwiftParser extends AntlrBaseParser<SwiftNode, TopLevelContext> {
public PmdSwiftParser(final ParserOptions parserOptions) { public PmdSwiftParser(final ParserOptions parserOptions) {
super(parserOptions); super(parserOptions);
} }
@Override @Override
protected SwiftFileNode parse(final Lexer lexer) { protected TopLevelContext parse(final Lexer lexer) {
SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer)); SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer));
return new SwiftFileNode(parser.topLevel()); return parser.topLevel();
} }
@Override @Override

View File

@ -1,28 +0,0 @@
/*
* 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.tree.ParseTreeVisitor;
import net.sourceforge.pmd.lang.ast.RootNode;
import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrNameDictionary;
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext;
public final class SwiftFileNode extends SwiftInnerNode implements RootNode {
SwiftFileNode(TopLevelContext root) {
asAntlrNode().addChild(root.asAntlrNode());
}
@Override
protected int getRuleIndex() {
return AntlrNameDictionary.ROOT_RULE_IDX;
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitChildren(asAntlrNode());
}
}

View File

@ -4,7 +4,8 @@
package net.sourceforge.pmd.lang.swift.ast; package net.sourceforge.pmd.lang.swift.ast;
import org.jetbrains.annotations.NotNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import net.sourceforge.pmd.lang.ast.test.BaseTreeDumpTest; import net.sourceforge.pmd.lang.ast.test.BaseTreeDumpTest;
import net.sourceforge.pmd.lang.ast.test.NodePrintersKt; import net.sourceforge.pmd.lang.ast.test.NodePrintersKt;
@ -18,7 +19,7 @@ public class BaseSwiftTreeDumpTest extends BaseTreeDumpTest {
super(NodePrintersKt.getSimpleNodePrinter(), ".swift"); super(NodePrintersKt.getSimpleNodePrinter(), ".swift");
} }
@NotNull @NonNull
@Override @Override
public SwiftParsingHelper getParser() { public SwiftParsingHelper getParser() {
return SwiftParsingHelper.DEFAULT.withResourceContext(getClass(), "testdata"); return SwiftParsingHelper.DEFAULT.withResourceContext(getClass(), "testdata");

View File

@ -8,17 +8,18 @@ import org.jetbrains.annotations.NotNull;
import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper; import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper;
import net.sourceforge.pmd.lang.swift.SwiftLanguageModule; import net.sourceforge.pmd.lang.swift.SwiftLanguageModule;
import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext;
/** /**
* *
*/ */
public class SwiftParsingHelper extends BaseParsingHelper<SwiftParsingHelper, SwiftFileNode> { public class SwiftParsingHelper extends BaseParsingHelper<SwiftParsingHelper, TopLevelContext> {
public static final SwiftParsingHelper DEFAULT = new SwiftParsingHelper(Params.getDefaultNoProcess()); public static final SwiftParsingHelper DEFAULT = new SwiftParsingHelper(Params.getDefaultNoProcess());
public SwiftParsingHelper(@NotNull Params params) { public SwiftParsingHelper(@NotNull Params params) {
super(SwiftLanguageModule.NAME, SwiftFileNode.class, params); super(SwiftLanguageModule.NAME, TopLevelContext.class, params);
} }
@NotNull @NotNull

View File

@ -151,14 +151,14 @@
| | | | | +- Expression | | | | | +- Expression
| | | | | +- PrefixExpression | | | | | +- PrefixExpression
| | | | | +- PostfixExpression | | | | | +- PostfixExpression
| | | | | | +- PrimaryExpression | | | | | +- PostfixExpression
| | | | | | +- Keyword | | | | | | +- PostfixExpression
| | | | | | +- T-self | | | | | | | +- PrimaryExpression
| | | | | +- PostfixExpression | | | | | | | +- Keyword
| | | | | | +- T-dot | | | | | | | +- T-self
| | | | | | +- Identifier | | | | | | +- T-dot
| | | | | | +- T-Identifier | | | | | | +- Identifier
| | | | | +- PostfixExpression | | | | | | +- T-Identifier
| | | | | +- PostfixOperator | | | | | +- PostfixOperator
| | | | | +- Operator | | | | | +- Operator
| | | | | +- OperatorHead | | | | | +- OperatorHead
@ -199,10 +199,10 @@
| | | | | +- Expression | | | | | +- Expression
| | | | | +- PrefixExpression | | | | | +- PrefixExpression
| | | | | +- PostfixExpression | | | | | +- PostfixExpression
| | | | | | +- PrimaryExpression | | | | | +- PostfixExpression
| | | | | | +- Identifier | | | | | | +- PrimaryExpression
| | | | | | +- T-Identifier | | | | | | +- Identifier
| | | | | +- PostfixExpression | | | | | | +- T-Identifier
| | | | | +- T-dot | | | | | +- T-dot
| | | | | +- Identifier | | | | | +- Identifier
| | | | | +- T-Identifier | | | | | +- T-Identifier
@ -215,14 +215,14 @@
| | | | | +- Expression | | | | | +- Expression
| | | | | +- PrefixExpression | | | | | +- PrefixExpression
| | | | | +- PostfixExpression | | | | | +- PostfixExpression
| | | | | | +- PrimaryExpression | | | | | +- PostfixExpression
| | | | | | +- Keyword | | | | | | +- PostfixExpression
| | | | | | +- T-self | | | | | | | +- PrimaryExpression
| | | | | +- PostfixExpression | | | | | | | +- Keyword
| | | | | | +- T-dot | | | | | | | +- T-self
| | | | | | +- Identifier | | | | | | +- T-dot
| | | | | | +- T-Identifier | | | | | | +- Identifier
| | | | | +- PostfixExpression | | | | | | +- T-Identifier
| | | | | +- PostfixOperator | | | | | +- PostfixOperator
| | | | | +- Operator | | | | | +- Operator
| | | | | +- OperatorHead | | | | | +- OperatorHead
@ -231,10 +231,10 @@
| | | | +- Expression | | | | +- Expression
| | | | +- PrefixExpression | | | | +- PrefixExpression
| | | | +- PostfixExpression | | | | +- PostfixExpression
| | | | | +- PrimaryExpression | | | | +- PostfixExpression
| | | | | +- Identifier | | | | | +- PrimaryExpression
| | | | | +- T-Identifier | | | | | +- Identifier
| | | | +- PostfixExpression | | | | | +- T-Identifier
| | | | +- FunctionCallArgumentClause | | | | +- FunctionCallArgumentClause
| | | | +- T-lparen | | | | +- T-lparen
| | | | +- FunctionCallArgumentList | | | | +- FunctionCallArgumentList
@ -280,10 +280,10 @@
| | | | +- Expression | | | | +- Expression
| | | | +- PrefixExpression | | | | +- PrefixExpression
| | | | +- PostfixExpression | | | | +- PostfixExpression
| | | | | +- PrimaryExpression | | | | +- PostfixExpression
| | | | | +- Identifier | | | | | +- PrimaryExpression
| | | | | +- T-Identifier | | | | | +- Identifier
| | | | +- PostfixExpression | | | | | +- T-Identifier
| | | | +- T-dot | | | | +- T-dot
| | | | +- Identifier | | | | +- Identifier
| | | | +- T-Identifier | | | | +- T-Identifier
@ -317,10 +317,10 @@
| | | +- Expression | | | +- Expression
| | | +- PrefixExpression | | | +- PrefixExpression
| | | +- PostfixExpression | | | +- PostfixExpression
| | | | +- PrimaryExpression | | | +- PostfixExpression
| | | | +- Identifier | | | | +- PrimaryExpression
| | | | +- T-Identifier | | | | +- Identifier
| | | +- PostfixExpression | | | | +- T-Identifier
| | | +- T-dot | | | +- T-dot
| | | +- Identifier | | | +- Identifier
| | | +- T-Identifier | | | +- T-Identifier

View File

@ -108,6 +108,9 @@
<javacc.jar>${settings.localRepository}/net/java/dev/javacc/javacc/${javacc.version}/javacc-${javacc.version}.jar</javacc.jar> <javacc.jar>${settings.localRepository}/net/java/dev/javacc/javacc/${javacc.version}/javacc-${javacc.version}.jar</javacc.jar>
<javacc.outputDirectory>${project.build.directory}/generated-sources/javacc</javacc.outputDirectory> <javacc.outputDirectory>${project.build.directory}/generated-sources/javacc</javacc.outputDirectory>
<javacc.ant.wrapper>${project.basedir}/../javacc-wrapper.xml</javacc.ant.wrapper> <javacc.ant.wrapper>${project.basedir}/../javacc-wrapper.xml</javacc.ant.wrapper>
<antlr4.outputDirectory>${project.build.directory}/generated-sources/antlr4</antlr4.outputDirectory>
<antlr4.ant.wrapper>${project.basedir}/../antlr4-wrapper.xml</antlr4.ant.wrapper>
</properties> </properties>
<build> <build>