Reputation: 119
I want to know which alternative rule antlr is current in .I read "the definitivie antlr4 reference",it use ctx.getChildCount() or ctx.ID()!=null to know which alternative rule antlr is currently in.
But I want to know is there a method which could give the specific index of the alternative rule.
I see this question How to know which alternative rule ANTLR parser is currently in during visit ,but it doesn't have the answer .
Upvotes: 2
Views: 556
Reputation: 170138
Either use alt labels to know in which alternative you are, or try to use options {contextSuperClass=org.antlr.v4.runtime.RuleContextWithAltNum;}
(link), but that is probably not what you want. The contextSuperClass
will only tell you in the child rule what the index in its parent is:
parent
: child_a
| child_b
| child_c
| child_d
| child_e
;
child_d
: ... // in a listener, you can now get index 3 from the parent context
;
so you cannot do:
parent
: child_a
| child_b
| child_c
| child_d // get index 3 here
| child_e
;
Note that the contextSuperClass
is only available in Java it seems by looking at the remark in the link: "I'm only putting into Java runtime as I'm certain I'm the only one that will really every use this."
Upvotes: 1