intellij-plugin-v4 icon indicating copy to clipboard operation
intellij-plugin-v4 copied to clipboard

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

Open Fancryer opened this issue 1 year ago • 1 comments

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.

Fancryer avatar Apr 01 '24 20:04 Fancryer

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.

bjansen avatar Apr 03 '24 08:04 bjansen