antlr4ts
antlr4ts copied to clipboard
For labeled rules, the base rule event method is generated but not called during tree walk
I have a rule like this:
expression
: IDENTIFIER LPAREN expressionList? RPAREN # functionExpression
| LPAREN expression RPAREN # parenthesisExpression
| ( OP_NOT | OP_HAS | MINUS ) expression # unaryExpression
// and lots of other expression types...
| MINUS expression # negativeExpression
;
The generated parse tree listener does include a method enterExpression
but my generated parser never calls it during a parsetree walk. Is this intended or am i missing something?
antlr4ts version: 0.5.0-alpha.3
PS> thanks for this port Sam and contributors - love it!
@cam-m @sharwell, any updates on it? I faced the same issue 😞
Looks like the generated contexts derived from the ParserRuleContext
don't override empty parent enterRule
and exitRule
methods. accept
method isn't overridden too.
Ok, I even found a commit where this behavior was changed (that line). Any ideas why this change was made? I can only imagine one reason why this could have been done. Maybe this is to prevent traversing the same node multiple times.
If the alternatives are labeled, the listeners and visitors should call the methods for the specific alternatives and not the ones for the base type. For the example by @cam-m, the expected behavior is enterExpression
does not exist and is never called. Currently the interface defines it as an optional method, but it is never called so it's easy enough to ignore it. We would accept a pull request to modify the code generator to stop emitting these unused method definitions.