naumcho
naumcho

Reputation: 19891

Specifying base listener/visitor implementation with antlr

I know you can define members for the antlr4 parser with the @members section. Is there something similar for defining default implementation of the BaseListener/BaseVisitor generated classes? The default implementations do - for visitor call visistChildren(), for listener do nothing.

The reason I am asking is because I am currently implementing both listener and visitor versions of the interface so I can compare performance and they both forward the call to a helper class that does the actual work in a very boilerplate way:

@Override
public Boolean visitExpression1(@NotNull FilterParser.Expression1Context ctx) {
    return evaluationHandler.eval(ctx);
}

@Override
public Boolean visitExpression2(@NotNull FilterParser.Expression2Context ctx) {
    return evaluationHandler.eval(ctx);
}
...

It would be nice if i can specify this in the grammar file somehow.

Thanks!

Upvotes: 0

Views: 1117

Answers (1)

Onur
Onur

Reputation: 5205

Create a class that inherits the BaseListener, call it MyBaseListener, add your stuff there and inherit this new class.

Upvotes: 1

Related Questions