antlr / antlr/antlr4

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

Open
#4,573 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the grammar rule atom: fstring | string | number | nil | logic; and the generated Java AtomContext class shown in the issue. Investigate how generated parser-rule context types represent unnamed subrules, then define the desired common interface or abstract type so callers can handle alternatives with pattern matching and a default branch.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.