ANTLR 4 determines the behavior of predicates based on whether or not they are "context sensitive". Context sensitive predicates use the $ syntax to reference a parameter, label, local, or rule/token defined in the current rule. It appears in your case you are defining and initializing state information outside of the standard ANTLR syntax, so it has no way to know the predicate is context sensitive. There are two ways to address this issue:
- Define one or more of your state variables which are used in the predicate in a
locals
block for the rule instead of in a @members block. - Add a reference to
$ctx
inside of a comment in the predicates. For example, you could add/*$ctx*/
at the end of the predicate.
If a context sensitive predicate is encountered but no context information is available (as is the case for your code), the predicate is assumed to be true.