antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.11k stars 3.28k forks source link

Javascript target and "start" rule element label #2617

Open mikecargal opened 5 years ago

mikecargal commented 5 years ago

We have a grammar with this parser rule:

expr    : OPEN_PAREN expr CLOSE_PAREN                               # parenExpr
        | str=expr OPEN_PAREN start=expr COLON len=expr CLOSE_PAREN # substringExpr
        ...

generated code includes the following:

             case 9:
                    localctx = new SubstringExprContext(this, new ExprContext(this, _parentctx, _parentState));
                    localctx.str = _prevctx;
                    this.pushNewRecursionContext(localctx, _startState, DeterminatorParser.RULE_expr);
                    this.state = 673;
                    if (!( this.precpred(this._ctx, 21))) {
                        throw new antlr4.error.FailedPredicateException(this, "this.precpred(this._ctx, 21)");
                    }
                    this.state = 674;
                    this.match(DeterminatorParser.OPEN_PAREN);
                    this.state = 675;
                    localctx.start = this.expr(0);   // <== problem here
                    this.state = 676;
                    this.match(DeterminatorParser.COLON);
                    this.state = 677;
                    localctx.len = this.expr(0);
                    this.state = 678;
                    this.match(DeterminatorParser.CLOSE_PAREN);
                    break;

The line with the "problem here" comment illustrates the problem.

All context objects have "start" and "stop" attributes that reference Tokens. When this statement executes, it replaces my start token with with my start expression.

We should be able to work around it with a rename in our grammar, but there should probably be an error (at least some warning) during code generation if there are naming conflicts like this (better yet, some sort of renaming (i.e. conflict on "start" in grammar results in "_start" attribute in generated code)).

ericvergnaud commented 1 year ago

agreed, but this is not specific to javascript