antlr4ts icon indicating copy to clipboard operation
antlr4ts copied to clipboard

For labeled rules, the base rule event method is generated but not called during tree walk

Open cam-m opened this issue 5 years ago • 4 comments

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 avatar Sep 25 '19 05:09 cam-m

@cam-m @sharwell, any updates on it? I faced the same issue 😞

vepanimas avatar Dec 20 '19 17:12 vepanimas

Looks like the generated contexts derived from the ParserRuleContext don't override empty parent enterRule and exitRule methods. accept method isn't overridden too.

vepanimas avatar Dec 20 '19 18:12 vepanimas

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.

vepanimas avatar Dec 20 '19 19:12 vepanimas

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.

sharwell avatar Dec 20 '19 19:12 sharwell