antlr / intellij-plugin-v4

An IntelliJ plugin for ANTLR v4
https://plugins.jetbrains.com/plugin/7358-antlr-v4
BSD 3-Clause "New" or "Revised" License
461 stars 103 forks source link

Generate an abstract class/interface for rule containing only other parser rules as unnamed subrules #699

Open Fancryer opened 4 months ago

Fancryer commented 4 months ago

For example, I have this rule.

atom: fstring | string | number | nil | logic;

And generated class is:

@SuppressWarnings("CheckReturnValue")
public static class AtomContext extends ParserRuleContext {
    public FstringContext fstring() {
        return getRuleContext(FstringContext.class,0);
    }
    public StringContext string() {
        return getRuleContext(StringContext.class,0);
    }
    public NumberContext number() {
        return getRuleContext(NumberContext.class,0);
    }
    public NilContext nil() {
        return getRuleContext(NilContext.class,0);
    }
    public LogicContext logic() {
        return getRuleContext(LogicContext.class,0);
    }
    public AtomContext(ParserRuleContext parent, int invokingState) {
        super(parent, invokingState);
    }
    @Override public int getRuleIndex() { return RULE_atom; }
    @Override
    public void enterRule(ParseTreeListener listener) {
        if ( listener instanceof NuttParserListener ) ((NuttParserListener)listener).enterAtom(this);
    }
    @Override
    public void exitRule(ParseTreeListener listener) {
        if ( listener instanceof NuttParserListener ) ((NuttParserListener)listener).exitAtom(this);
    }
    @Override
    public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
        if ( visitor instanceof NuttParserVisitor ) return ((NuttParserVisitor<? extends T>)visitor).visitAtom(this);
        else return visitor.visitChildren(this);
    }
}

Is it possible to provide common interface or abstract class that would describe content (unnamed subrules) of such rule when (I think only) it has only references to other parser rules? It is very boring to check every subrule if it is null, it would be wonderful if I could just use pattern matching and its "default" branch.

bjansen commented 4 months ago

Hi, I think this is a question for https://github.com/antlr/antlr4, this class is not generated by the IntelliJ plugin itself but by ANTLR.