I am trying to write an ANTLR parser rule that matches a list of things, and I want to write a parser action that can process each item in a list independently.
Some sample input for these rules:
$(A1 A2 A3)
I would like this to lead to an evaluation that contains a list of three MyIdentEvaluator objects — one for each of A1, A2, and A3.
Here is a snippet of my grammar:
my_list returns [IEvaluator e] : { $e = new MyListEvaluator(); } '$' LPAREN op=my_ident+ { $e.Add($op.e); } RPAREN ; my_ident returns [IEvaluator e] : IDENT { $e = new MyIdentEvaluator($IDENT.text); } ;
I think my_ident defined correctly because I see three MyIdentEvaluators created, created as expected for my input line, but only the last my_ident ever added to the list (A3 in my input example).
How can I best process each of these elements independently, either by changing the grammar or by changing the parsing?
It also occurred to me that my vocabulary for these concepts is not what it should be, so if it looks like I'm misusing the term, I probably have one.
EDIT in response to Wayne's comment:
I tried using op+=my_ident+ . In this case, $op in my action becomes an IList (in C #), which contains instances of Antlr.Runtime.Tree.CommonTree . This gives me one entry for a matching token in $op , so I see three of my matches, but I don't have the MyIdentEvaluator instances that I really want. I was hoping that now I could find the rule attribute in the ANTLR docs that could help with this, but nothing helped me get rid of this IList .
Result...
Based on chollida's answer, I ended up with this, which works well:
my_list returns [IEvaluator e] : { $e = new MyListEvaluator(); } '$' LPAREN (op=my_ident { $e.Add($op.e); } )+ RPAREN ;
The Add method is called for each match my_ident.