[core] Antlr - make more methods protected

The abstract methods createPmdTerminal and createPmdError in AntlrGeneratedParserBase don't need to be public. They are just implementation.

The language specific inner nodes (KotlinInnerNode, SwiftInnerNode)
can be package private. Only the concrete subclasses are considered
public API.
This commit is contained in:
Andreas Dangel
2023-04-14 09:41:52 +02:00
parent ee227b1397
commit 0d5ecebd4e
9 changed files with 30 additions and 22 deletions

View File

@ -75,20 +75,24 @@ definitely don't come for free. It is much effort and requires perseverance to i
* a language specific inner node - these nodes represent the production rules from the grammar.
In Antlr, they are called "ParserRuleContext". We call them "InnerNode". Use the
base class from pmd-core
[`BaseAntlrInnerNode`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java)
. And example is [`SwiftInnerNode`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftInnerNode.java).
[`BaseAntlrInnerNode`](https://github.com/pmd/pmd/blob/master/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/impl/antlr4/BaseAntlrInnerNode.java)
. And example is [`SwiftInnerNode`](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftInnerNode.java).
Note that this language specific inner node is package-private, as it is only the base class for the concrete
nodes generated by ANLTR.
* a language specific root node - this provides the root of the AST and our parser will return
subtypes of this node. The root node itself is a "InnerNode".
See [`SwiftRootNode`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftRootNode.java).
See [`SwiftRootNode`](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftRootNode.java).
Note that this language specific root node is package-private, as it is only the base class for the concrete
node generated by ANLTR.
* a language specific terminal node.
See [`SwiftTerminalNode`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftTerminalNode.java).
See [`SwiftTerminalNode`](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftTerminalNode.java).
* a language specific error node.
See [`SwiftErrorNode`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftErrorNode.java).
See [`SwiftErrorNode`](https://github.com/pmd/pmd/blob/master/pmd-swift/src/main/java/net/sourceforge/pmd/lang/swift/ast/SwiftErrorNode.java).
* In order for the generated code to match and use our custom classes, we have a common ant script, that fiddles with
the generated code. The ant script is [`antlr4-wrapper.xml`](https://github.com/pmd/pmd/blob/pmd/7.0.x/antlr4-wrapper.xml) and
the generated code. The ant script is [`antlr4-wrapper.xml`](https://github.com/pmd/pmd/blob/master/antlr4-wrapper.xml) and
does not need to be adjusted - it has plenty of parameters to set. The ant script is added in the
language module's `pom.xml` where the parameters are set (e.g. name of root name class). Have a look at
Swift's example: [`pmd-swift/pom.xml`](https://github.com/pmd/pmd/blob/pmd/7.0.x/pmd-swift/pom.xml).
Swift's example: [`pmd-swift/pom.xml`](https://github.com/pmd/pmd/blob/master/pmd-swift/pom.xml).
* You can add additional methods in your "InnerNode" (e.g. `SwiftInnerNode`) that are available on all nodes.
But on most cases you won't need to do anything.

View File

@ -43,7 +43,9 @@ The remaining section describe the complete release notes for 7.0.0.
* Moved the two classes {% jdoc core::cpd.impl.AntlrTokenizer %} and {% jdoc core::cpd.impl.JavaCCTokenizer %} from
`internal` package into package {% jdoc_package core::cpd.impl %}. These two classes are part of the API and
are base classes for CPD language implementations.
* `AntlrBaseRule` is gone in favor of {% jdoc core::pmd.lang.rule.AbstractVisitorRule %}.
* `AntlrBaseRule` is gone in favor of {% jdoc core::lang.rule.AbstractVisitorRule %}.
* The classes {% jdoc kotlin::lang.kotlin.ast.KotlinInnerNode %} and {% jdoc swift::lang.swift.ast.SwiftInnerNode %}
are package-private now.
#### Fixed Issues:
* java-codestyle

View File

@ -753,8 +753,9 @@ until the next major release, but it is recommended to stop using them.
* Moved the two classes {% jdoc core::cpd.impl.AntlrTokenizer %} and {% jdoc core::cpd.impl.JavaCCTokenizer %} from
`internal` package into package {% jdoc_package core::cpd.impl %}. These two classes are part of the API and
are base classes for CPD language implementations. Since 7.0.0-rc2.
* `AntlrBaseRule` is gone in favor of {% jdoc core::pmd.lang.rule.AbstractVisitorRule %}. Since 7.0.0-rc2.
* `AntlrBaseRule` is gone in favor of {% jdoc core::lang.rule.AbstractVisitorRule %}. Since 7.0.0-rc2.
* The classes {% jdoc kotlin::lang.kotlin.ast.KotlinInnerNode %} and {% jdoc swift::lang.swift.ast.SwiftInnerNode %}
are package-private now. Since 7.0.0-rc2.
### XPath 3.1 support

View File

@ -31,7 +31,8 @@ import net.sourceforge.pmd.lang.ast.Node;
* which stores the XPath names of the generated nodes (and terminals).
*
* <p>Additional members can be added to a parser with {@code @parser::members { ... }}
* in the g4 file.
* in the g4 file. That's how the implementations for {@link #createPmdTerminal(ParserRuleContext, Token)}
* and {@link #createPmdError(ParserRuleContext, Token)} can be added.
*/
public abstract class AntlrGeneratedParserBase<N extends AntlrNode<N>> extends Parser {
@ -52,9 +53,9 @@ public abstract class AntlrGeneratedParserBase<N extends AntlrNode<N>> extends P
// Those two need to return a node that implements eg SwiftNode
public abstract BaseAntlrTerminalNode<N> createPmdTerminal(ParserRuleContext parent, Token t);
protected abstract BaseAntlrTerminalNode<N> createPmdTerminal(ParserRuleContext parent, Token t);
public abstract BaseAntlrErrorNode<N> createPmdError(ParserRuleContext parent, Token t);
protected abstract BaseAntlrErrorNode<N> createPmdError(ParserRuleContext parent, Token t);
protected Node asPmdNode(RuleContext ctx) {

View File

@ -15,12 +15,12 @@ import net.sourceforge.pmd.lang.ast.AstVisitor;
static final AntlrNameDictionary DICO = new KotlinNameDictionary(VOCABULARY, ruleNames);
@Override
public KotlinTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
protected KotlinTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
return new KotlinTerminalNode(t);
}
@Override
public KotlinErrorNode createPmdError(ParserRuleContext parent, Token t) {
protected KotlinErrorNode createPmdError(ParserRuleContext parent, Token t) {
return new KotlinErrorNode(t);
}
}

View File

@ -50,12 +50,12 @@ import net.sourceforge.pmd.lang.ast.AstVisitor;
static final AntlrNameDictionary DICO = new KotlinNameDictionary(VOCABULARY, ruleNames);
@Override
public KotlinTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
protected KotlinTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
return new KotlinTerminalNode(t);
}
@Override
public KotlinErrorNode createPmdError(ParserRuleContext parent, Token t) {
protected KotlinErrorNode createPmdError(ParserRuleContext parent, Token t) {
return new KotlinErrorNode(t);
}
}

View File

@ -9,8 +9,7 @@ import org.antlr.v4.runtime.ParserRuleContext;
import net.sourceforge.pmd.lang.ast.AstVisitor;
import net.sourceforge.pmd.lang.ast.impl.antlr4.BaseAntlrInnerNode;
public abstract class KotlinInnerNode
extends BaseAntlrInnerNode<KotlinNode> implements KotlinNode {
abstract class KotlinInnerNode extends BaseAntlrInnerNode<KotlinNode> implements KotlinNode {
KotlinInnerNode(ParserRuleContext parent, int invokingStateNumber) {
super(parent, invokingStateNumber);

View File

@ -44,12 +44,12 @@ import net.sourceforge.pmd.lang.ast.AstVisitor;
static final AntlrNameDictionary DICO = new SwiftNameDictionary(VOCABULARY, ruleNames);
@Override
public SwiftTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
protected SwiftTerminalNode createPmdTerminal(ParserRuleContext parent, Token t) {
return new SwiftTerminalNode(t);
}
@Override
public SwiftErrorNode createPmdError(ParserRuleContext parent, Token t) {
protected SwiftErrorNode createPmdError(ParserRuleContext parent, Token t) {
return new SwiftErrorNode(t);
}
}

View File

@ -9,7 +9,8 @@ import org.antlr.v4.runtime.ParserRuleContext;
import net.sourceforge.pmd.lang.ast.AstVisitor;
import net.sourceforge.pmd.lang.ast.impl.antlr4.BaseAntlrInnerNode;
public abstract class SwiftInnerNode
// package private base class
abstract class SwiftInnerNode
extends BaseAntlrInnerNode<SwiftNode> implements SwiftNode {
SwiftInnerNode() {