From f1d6e22c4961b0382579527e5c867fe0af3a2df8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sat, 2 May 2020 23:11:16 +0200 Subject: [PATCH] Reintroduce AntlrNode --- .../lang/ast/impl/antlr4/AntlrBaseParser.java | 3 +-- .../lang/ast/impl/antlr4/AntlrBaseRule.java | 4 +++- .../impl/antlr4/AntlrGeneratedParserBase.java | 3 +-- .../ast/impl/antlr4/AntlrNameDictionary.java | 15 +++++++++------ .../pmd/lang/ast/impl/antlr4/AntlrNode.java | 18 ++++++++++++++++++ .../ast/impl/antlr4/BaseAntlrErrorNode.java | 4 +--- .../ast/impl/antlr4/BaseAntlrInnerNode.java | 5 ++--- .../lang/ast/impl/antlr4/BaseAntlrNode.java | 8 ++------ .../ast/impl/antlr4/BaseAntlrTerminalNode.java | 7 +++---- .../lang/ast/impl/antlr4/PmdAntlrVisitor.java | 6 +++++- .../pmd/lang/swift/AbstractSwiftRule.java | 2 +- .../lang/swift/ast/SwiftNameDictionary.java | 4 ++-- .../pmd/lang/swift/ast/SwiftNode.java | 7 ++----- 13 files changed, 50 insertions(+), 36 deletions(-) create mode 100644 pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNode.java diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java index 2fae75cfdd..0e84059e63 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseParser.java @@ -15,7 +15,6 @@ import net.sourceforge.pmd.lang.Parser; import net.sourceforge.pmd.lang.ParserOptions; import net.sourceforge.pmd.lang.ast.ParseException; import net.sourceforge.pmd.lang.ast.RootNode; -import net.sourceforge.pmd.lang.ast.impl.GenericNode; /** * Generic Antlr parser adapter for all Antlr parsers. This wraps a parser @@ -25,7 +24,7 @@ import net.sourceforge.pmd.lang.ast.impl.GenericNode; * @param Type of the root node */ public abstract class AntlrBaseParser< - N extends GenericNode, + N extends AntlrNode, R extends BaseAntlrInnerNode & RootNode > implements Parser { diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseRule.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseRule.java index 901441143d..19b0bf8c85 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseRule.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrBaseRule.java @@ -38,8 +38,10 @@ public abstract class AntlrBaseRule extends AbstractRule { * This visitor should explore the nodes it's interested in and report * violations on the given rule context. * + * @param ruleCtx Object that accumulates rule violations + * * @return A visitor bound to the given rule context */ - public abstract ParseTreeVisitor buildVisitor(RuleContext ctx); + public abstract ParseTreeVisitor buildVisitor(RuleContext ruleCtx); } 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 713cf70ca0..5571c21e70 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 @@ -13,7 +13,6 @@ import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.TerminalNode; import net.sourceforge.pmd.lang.ast.Node; -import net.sourceforge.pmd.lang.ast.impl.GenericNode; /** * This is the base class for antlr generated parsers. The implementation @@ -34,7 +33,7 @@ import net.sourceforge.pmd.lang.ast.impl.GenericNode; *

Additional members can be added to a parser with {@code @parser::members { ... }} * in the g4 file. */ -public abstract class AntlrGeneratedParserBase> extends Parser { +public abstract class AntlrGeneratedParserBase> extends Parser { public AntlrGeneratedParserBase(TokenStream input) { super(input); 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 d7ca519d66..1ca4c4f953 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 @@ -86,12 +86,15 @@ public class AntlrNameDictionary { return vocabulary; } - protected @Nullable String nonAlphaNumName(String s) { - // these are hardcoded, but it's overridable - // here we try to avoid semantic overtones, because - // a-priori the same terminal may mean several things - // in different contexts. - switch (s) { + /** + * Override this to customize the XPath name of tokes with no symbolic + * name and with an image that is non-alphanumeric. Return null to give + * up. The default just gives some name to common punctuation. Remember + * that the same token may mean several things in different contexts, so + * eg using {@code "not"} as the name of {@code "!"} is too specific. + */ + protected @Nullable String nonAlphaNumName(String name) { + switch (name) { case "!": return "bang"; case "!!": return "double-bang"; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNode.java new file mode 100644 index 0000000000..9f2292570b --- /dev/null +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/AntlrNode.java @@ -0,0 +1,18 @@ +/* + * 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.tree.ParseTreeVisitor; + +import net.sourceforge.pmd.lang.ast.impl.GenericNode; + +/** + * Base interface for all Antlr-based implementation of the Node interface. + */ +public interface AntlrNode> extends GenericNode { + + T accept(ParseTreeVisitor visitor); + +} diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrErrorNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrErrorNode.java index 44c52508b4..f3d54455d3 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrErrorNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrErrorNode.java @@ -8,9 +8,7 @@ import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.checkerframework.checker.nullness.qual.NonNull; -import net.sourceforge.pmd.lang.ast.impl.GenericNode; - -public abstract class BaseAntlrErrorNode> extends BaseAntlrTerminalNode { +public abstract class BaseAntlrErrorNode> extends BaseAntlrTerminalNode { protected BaseAntlrErrorNode(Token symbol) { super(symbol, true); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java index 7e53f88aff..bc0e50b142 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java @@ -16,7 +16,6 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.RuleNode; import org.antlr.v4.runtime.tree.TerminalNode; -import net.sourceforge.pmd.lang.ast.impl.GenericNode; import net.sourceforge.pmd.lang.ast.impl.antlr4.BaseAntlrInnerNode.PmdAsAntlrInnerNode; /** @@ -24,7 +23,7 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.BaseAntlrInnerNode.PmdAsAntlrInn * Use the {@code contextSuperClass} option to set this in the antlr g4 file, * eg {@code options { contextSuperClass = SwiftInnerNode; }}. */ -public abstract class BaseAntlrInnerNode> extends BaseAntlrNode, N> { +public abstract class BaseAntlrInnerNode> extends BaseAntlrNode, N> { public RecognitionException exception; @@ -109,7 +108,7 @@ public abstract class BaseAntlrInnerNode> extends BaseA return visitor.visitChildren(asAntlrNode()); } - protected static class PmdAsAntlrInnerNode> extends ParserRuleContext implements RuleNode, AntlrToPmdParseTreeAdapter { + protected static class PmdAsAntlrInnerNode> extends ParserRuleContext implements RuleNode, AntlrToPmdParseTreeAdapter { private final BaseAntlrInnerNode pmdNode; diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrNode.java index 75b2765d14..be9147822c 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrNode.java @@ -8,7 +8,6 @@ import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Token; import org.antlr.v4.runtime.tree.ErrorNode; import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.TerminalNode; import net.sourceforge.pmd.lang.ast.impl.GenericNode; @@ -34,7 +33,7 @@ import net.sourceforge.pmd.util.DataMap.DataKey; * @param Type of the underlying antlr node * @param Public interface (eg SwiftNode) */ -public abstract class BaseAntlrNode, N extends GenericNode> implements GenericNode { +public abstract class BaseAntlrNode, N extends AntlrNode> implements AntlrNode { private DataMap> userMap; private int indexInParent = -1; @@ -110,13 +109,10 @@ public abstract class BaseAntlrNode, N e return userMap; } - public abstract T accept(ParseTreeVisitor visitor); - - protected abstract A asAntlrNode(); - protected interface AntlrToPmdParseTreeAdapter> extends ParseTree { + protected interface AntlrToPmdParseTreeAdapter> extends ParseTree { BaseAntlrNode getPmdNode(); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrTerminalNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrTerminalNode.java index bb3c637cb9..3f53bde770 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrTerminalNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrTerminalNode.java @@ -12,13 +12,12 @@ import org.antlr.v4.runtime.tree.TerminalNode; import org.antlr.v4.runtime.tree.TerminalNodeImpl; import org.checkerframework.checker.nullness.qual.NonNull; -import net.sourceforge.pmd.lang.ast.impl.GenericNode; import net.sourceforge.pmd.lang.ast.impl.antlr4.BaseAntlrTerminalNode.AntlrTerminalPmdAdapter; /** * Base class for terminal nodes (they wrap a {@link TerminalNode}). */ -public abstract class BaseAntlrTerminalNode> +public abstract class BaseAntlrTerminalNode> extends BaseAntlrNode, N> { private final AntlrTerminalPmdAdapter antlrNode; @@ -77,7 +76,7 @@ public abstract class BaseAntlrTerminalNode> throw new IndexOutOfBoundsException("Index " + index + " for terminal node"); } - protected static class AntlrTerminalPmdAdapter> extends TerminalNodeImpl implements AntlrToPmdParseTreeAdapter { + protected static class AntlrTerminalPmdAdapter> extends TerminalNodeImpl implements AntlrToPmdParseTreeAdapter { private final BaseAntlrTerminalNode pmdNode; @@ -103,7 +102,7 @@ public abstract class BaseAntlrTerminalNode> } } - protected static class AntlrErrorPmdAdapter> extends AntlrTerminalPmdAdapter implements ErrorNode { + protected static class AntlrErrorPmdAdapter> extends AntlrTerminalPmdAdapter implements ErrorNode { public AntlrErrorPmdAdapter(BaseAntlrTerminalNode pmdNode, Token symbol) { super(pmdNode, symbol); diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/PmdAntlrVisitor.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/PmdAntlrVisitor.java index 5d01b8a62f..599c0f76a8 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/PmdAntlrVisitor.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/PmdAntlrVisitor.java @@ -8,7 +8,8 @@ import org.antlr.v4.runtime.tree.ParseTreeVisitor; import org.antlr.v4.runtime.tree.RuleNode; /** - * + * Interface for parse tree visitors that can also visit PMD nodes. The + * antlr rewrite makes the generated visitor interface extend this. */ public interface PmdAntlrVisitor extends ParseTreeVisitor { @@ -16,6 +17,9 @@ public interface PmdAntlrVisitor extends ParseTreeVisitor { T visitChildren(RuleNode node); + /** + * This is added for compatibility. + */ default T visitChildren(BaseAntlrInnerNode node) { return visitChildren(node.asAntlrNode()); } diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java index a0cc2a5467..b97644cd24 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/AbstractSwiftRule.java @@ -16,5 +16,5 @@ public abstract class AbstractSwiftRule extends AntlrBaseRule { } @Override - public abstract ParseTreeVisitor buildVisitor(RuleContext ctx); + public abstract ParseTreeVisitor buildVisitor(RuleContext ruleCtx); } diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNameDictionary.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNameDictionary.java index 7d1bcb6348..484090caf7 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNameDictionary.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNameDictionary.java @@ -13,13 +13,13 @@ import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrNameDictionary; final class SwiftNameDictionary extends AntlrNameDictionary { - public SwiftNameDictionary(Vocabulary vocab, String[] ruleNames) { + SwiftNameDictionary(Vocabulary vocab, String[] ruleNames) { super(vocab, ruleNames); } @Override protected @Nullable String nonAlphaNumName(String name) { - { + { // limit scope of 'sup', which would be null outside of here anyway String sup = super.nonAlphaNumName(name); if (sup != null) { return sup; diff --git a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNode.java b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNode.java index 7cf085693d..b2bfcecd3c 100644 --- a/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNode.java +++ b/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftNode.java @@ -4,16 +4,13 @@ package net.sourceforge.pmd.lang.swift.ast; -import org.antlr.v4.runtime.tree.ParseTreeVisitor; - -import net.sourceforge.pmd.lang.ast.impl.GenericNode; +import net.sourceforge.pmd.lang.ast.impl.antlr4.AntlrNode; /** * Supertype of all swift nodes. */ -public interface SwiftNode extends GenericNode { +public interface SwiftNode extends AntlrNode { - T accept(ParseTreeVisitor visitor); }