diff --git a/antlr4-wrapper.xml b/antlr4-wrapper.xml new file mode 100644 index 0000000000..7b4d83c6c1 --- /dev/null +++ b/antlr4-wrapper.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrGeneratedParserBase.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrGeneratedParserBase.java index 4b8d3efd3a..90261a5018 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrGeneratedParserBase.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrGeneratedParserBase.java @@ -6,10 +6,12 @@ package net.sourceforge.pmd.lang.ast.impl.antlr4; import org.antlr.v4.runtime.Parser; import org.antlr.v4.runtime.ParserRuleContext; +import org.antlr.v4.runtime.RuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.TokenStream; import org.antlr.v4.runtime.tree.TerminalNode; +import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.impl.GenericNode; /** @@ -30,6 +32,11 @@ public abstract class AntlrGeneratedParserBase> extends public abstract BaseAntlrTerminalNode createPmdTerminal(ParserRuleContext parent, Token t); + + protected Node asPmdNode(RuleContext ctx) { + return ((BaseAntlrNode.AntlrToPmdParseTreeAdapter) ctx).getPmdNode(); + } + protected void enterRule(BaseAntlrInnerNode ptree, int state, int alt) { enterRule(ptree.asAntlrNode(), state, alt); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java index 38c5755269..9be0648081 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNameDictionary.java @@ -23,8 +23,6 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public class AntlrNameDictionary { - public static final int ROOT_RULE_IDX = -1; - private final String[] terminalXpathNames; private final String[] nonTermXpathNames; private final Vocabulary vocabulary; @@ -155,12 +153,7 @@ public class AntlrNameDictionary { } public @NonNull String getXPathNameOfRule(int idx) { - if (idx >= 0 && idx < nonTermXpathNames.length) { - return nonTermXpathNames[idx]; - } else if (idx == ROOT_RULE_IDX) { - return "FileRoot"; - } - throw new IllegalArgumentException("I don't know rule type " + idx); + return nonTermXpathNames[idx]; } public int getMaxRuleIndex() { diff --git a/pmd-swift/pom.xml b/pmd-swift/pom.xml index aad103a03c..76e8c6ea6a 100644 --- a/pmd-swift/pom.xml +++ b/pmd-swift/pom.xml @@ -17,9 +17,33 @@ + org.antlr antlr4-maven-plugin + + org.apache.maven.plugins + maven-antrun-plugin + true + + + antlr-cleanup + generate-sources + + run + + + + + + + + + + + + + maven-resources-plugin diff --git a/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/ast/Swift.g4 b/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/ast/Swift.g4 index eec2717226..5265ff18e4 100644 --- a/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/ast/Swift.g4 +++ b/pmd-swift/src/main/antlr4/net/sourceforge/pmd/lang/swift/ast/Swift.g4 @@ -42,6 +42,7 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.*; static final AntlrNameDictionary DICO = new AntlrNameDictionary(VOCABULARY, ruleNames); + @Override public SwiftTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) { return new SwiftTerminalNode(t); } diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/PmdSwiftParser.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/PmdSwiftParser.java index c58a747f98..2c1f88b2b4 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/PmdSwiftParser.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/PmdSwiftParser.java @@ -10,20 +10,21 @@ import org.antlr.v4.runtime.Lexer; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrBaseParser; +import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext; /** * Adapter for the SwiftParser. */ -public final class PmdSwiftParser extends AntlrBaseParser { +public final class PmdSwiftParser extends AntlrBaseParser { public PmdSwiftParser(final ParserOptions parserOptions) { super(parserOptions); } @Override - protected SwiftFileNode parse(final Lexer lexer) { + protected TopLevelContext parse(final Lexer lexer) { SwiftParser parser = new SwiftParser(new CommonTokenStream(lexer)); - return new SwiftFileNode(parser.topLevel()); + return parser.topLevel(); } @Override diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftFileNode.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftFileNode.java deleted file mode 100644 index 463f26c9c8..0000000000 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftFileNode.java +++ /dev/null @@ -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 accept(ParseTreeVisitor visitor) { - return visitor.visitChildren(asAntlrNode()); - } -} diff --git a/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/BaseSwiftTreeDumpTest.java b/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/BaseSwiftTreeDumpTest.java index b41a4adc3d..caafefa012 100644 --- a/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/BaseSwiftTreeDumpTest.java +++ b/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/BaseSwiftTreeDumpTest.java @@ -4,7 +4,8 @@ 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.NodePrintersKt; @@ -18,7 +19,7 @@ public class BaseSwiftTreeDumpTest extends BaseTreeDumpTest { super(NodePrintersKt.getSimpleNodePrinter(), ".swift"); } - @NotNull + @NonNull @Override public SwiftParsingHelper getParser() { return SwiftParsingHelper.DEFAULT.withResourceContext(getClass(), "testdata"); diff --git a/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/SwiftParsingHelper.java b/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/SwiftParsingHelper.java index 1f790d9a46..1401f15a95 100644 --- a/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/SwiftParsingHelper.java +++ b/pmd-swift/src/test/java/net/sourceforge/pmd/lang/swift/ast/SwiftParsingHelper.java @@ -8,17 +8,18 @@ import org.jetbrains.annotations.NotNull; import net.sourceforge.pmd.lang.ast.test.BaseParsingHelper; import net.sourceforge.pmd.lang.swift.SwiftLanguageModule; +import net.sourceforge.pmd.lang.swift.ast.SwiftParser.TopLevelContext; /** * */ -public class SwiftParsingHelper extends BaseParsingHelper { +public class SwiftParsingHelper extends BaseParsingHelper { public static final SwiftParsingHelper DEFAULT = new SwiftParsingHelper(Params.getDefaultNoProcess()); public SwiftParsingHelper(@NotNull Params params) { - super(SwiftLanguageModule.NAME, SwiftFileNode.class, params); + super(SwiftLanguageModule.NAME, TopLevelContext.class, params); } @NotNull diff --git a/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/ast/testdata/Simple.txt b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/ast/testdata/Simple.txt index 029463f5bd..86885289da 100644 --- a/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/ast/testdata/Simple.txt +++ b/pmd-swift/src/test/resources/net/sourceforge/pmd/lang/swift/ast/testdata/Simple.txt @@ -151,14 +151,14 @@ | | | | | +- Expression | | | | | +- PrefixExpression | | | | | +- PostfixExpression - | | | | | | +- PrimaryExpression - | | | | | | +- Keyword - | | | | | | +- T-self - | | | | | +- PostfixExpression - | | | | | | +- T-dot - | | | | | | +- Identifier - | | | | | | +- T-Identifier - | | | | | +- PostfixExpression + | | | | | +- PostfixExpression + | | | | | | +- PostfixExpression + | | | | | | | +- PrimaryExpression + | | | | | | | +- Keyword + | | | | | | | +- T-self + | | | | | | +- T-dot + | | | | | | +- Identifier + | | | | | | +- T-Identifier | | | | | +- PostfixOperator | | | | | +- Operator | | | | | +- OperatorHead @@ -199,10 +199,10 @@ | | | | | +- Expression | | | | | +- PrefixExpression | | | | | +- PostfixExpression - | | | | | | +- PrimaryExpression - | | | | | | +- Identifier - | | | | | | +- T-Identifier - | | | | | +- PostfixExpression + | | | | | +- PostfixExpression + | | | | | | +- PrimaryExpression + | | | | | | +- Identifier + | | | | | | +- T-Identifier | | | | | +- T-dot | | | | | +- Identifier | | | | | +- T-Identifier @@ -215,14 +215,14 @@ | | | | | +- Expression | | | | | +- PrefixExpression | | | | | +- PostfixExpression - | | | | | | +- PrimaryExpression - | | | | | | +- Keyword - | | | | | | +- T-self - | | | | | +- PostfixExpression - | | | | | | +- T-dot - | | | | | | +- Identifier - | | | | | | +- T-Identifier - | | | | | +- PostfixExpression + | | | | | +- PostfixExpression + | | | | | | +- PostfixExpression + | | | | | | | +- PrimaryExpression + | | | | | | | +- Keyword + | | | | | | | +- T-self + | | | | | | +- T-dot + | | | | | | +- Identifier + | | | | | | +- T-Identifier | | | | | +- PostfixOperator | | | | | +- Operator | | | | | +- OperatorHead @@ -231,10 +231,10 @@ | | | | +- Expression | | | | +- PrefixExpression | | | | +- PostfixExpression - | | | | | +- PrimaryExpression - | | | | | +- Identifier - | | | | | +- T-Identifier - | | | | +- PostfixExpression + | | | | +- PostfixExpression + | | | | | +- PrimaryExpression + | | | | | +- Identifier + | | | | | +- T-Identifier | | | | +- FunctionCallArgumentClause | | | | +- T-lparen | | | | +- FunctionCallArgumentList @@ -280,10 +280,10 @@ | | | | +- Expression | | | | +- PrefixExpression | | | | +- PostfixExpression - | | | | | +- PrimaryExpression - | | | | | +- Identifier - | | | | | +- T-Identifier - | | | | +- PostfixExpression + | | | | +- PostfixExpression + | | | | | +- PrimaryExpression + | | | | | +- Identifier + | | | | | +- T-Identifier | | | | +- T-dot | | | | +- Identifier | | | | +- T-Identifier @@ -317,10 +317,10 @@ | | | +- Expression | | | +- PrefixExpression | | | +- PostfixExpression - | | | | +- PrimaryExpression - | | | | +- Identifier - | | | | +- T-Identifier - | | | +- PostfixExpression + | | | +- PostfixExpression + | | | | +- PrimaryExpression + | | | | +- Identifier + | | | | +- T-Identifier | | | +- T-dot | | | +- Identifier | | | +- T-Identifier diff --git a/pom.xml b/pom.xml index 5fe0d25eec..5d39647f85 100644 --- a/pom.xml +++ b/pom.xml @@ -108,6 +108,9 @@ ${settings.localRepository}/net/java/dev/javacc/javacc/${javacc.version}/javacc-${javacc.version}.jar ${project.build.directory}/generated-sources/javacc ${project.basedir}/../javacc-wrapper.xml + + ${project.build.directory}/generated-sources/antlr4 + ${project.basedir}/../antlr4-wrapper.xml