tunnelvisionlabs / antlr4ts

Optimized TypeScript target for ANTLR 4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rule name collides with member of ParserRuleContext

arucil opened this issue · comments

If a grammar contains a rule named text,

grammar Test;
root : text ;
text : C ;
C : 'c' ;

A name conflict error is reported in the generated Parser file:

export class RootContext extends ParserRuleContext {
    public text(): TextContext {
//         ^^^^
// Property 'text' in type 'RootContext' is not assignable to the same property in base type 'ParserRuleContext'.
// Type '() => TextContext' is not assignable to type 'string'
        return this.getRuleContext(0, TextContext);
    }
    ...
}

text is a getter in RuleContext, which is the base class of ParserRuleContext.

Same problem here.

I had a rule named start which caused a name collision with StartContext. I added a prefix to all my rules as a workaround. Maybe the internal rules can be renamed to X_context, XCtx or XAntlr4tsContext so they won't collide anymore.

I'm not sure there's any real way to fix this. It would be good to update the code generator to be aware of the reserved identifiers and report a proper error message if a name conflict occurs.