Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Fancryer opened this issue Apr 2, 2024 · 0 comments

Comments

@Fancryer
Copy link

Fancryer commented Apr 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant