javaantlrantlr4

Can I assume ctx is always non-null when overriding methods in ANTLR's BaseListener?


I'm currently working with ANTLR and overriding methods in the generated BaseListener class. Specifically, I want to know if I can safely assume that the ctx (context) parameter passed to these overridden methods is always non-null.

For example, if I override a listener method like this:

@Override
public void enterExpr(MyGrammarParser.ExprContext ctx) {
    // Can I assume 'ctx' is always non-null here?
}

Are there any edge cases (like certain parsing errors or optional rules) where ctx could be null, or is it always guaranteed to be a valid context object when the method is called?


Solution

  • Yes you can. The context is allways created, and if you check the generated code, you can see the context as uses 'this' when the correspoding listener function is called, so it must be exist!