antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

ActionTranslator - Reserved words escaping

Open dudemaga opened this issue 5 months ago • 0 comments
trafficstars

I’m not sure how applicable this is to targets other than Typescript.

If you use a label in a rule named as a reserved words, you get bad behaviour when using a token property reference.

Example:

Grammar.g4 rule: number=INT { (if $number.int==0) this.notifyErrorListeners("Error", $number, undefined);};

In the generated file you get: if ((localctx._number != null ? Number(localctx._number.text) : 0)==0) this.notifyErrorListeners("Error", localctx._number_, undefined);

You can see that the TokenRef which uses escapedName is correct and uses _number_ but that the TokenPropertyRef which uses label is incorrect and uses _number

dudemaga avatar Jun 13 '25 17:06 dudemaga