[antlr-interest] Concatenating variable number of tokens into a single string in an action?
John B. Brodie
jbb at acm.org
Mon Jul 18 13:52:33 PDT 2011
Greetings!
On Mon, 2011-07-18 at 15:35 -0500, Dejas Ninethousand wrote:
> Hello,
>
> If I have the rule:
>
> text : t=(UNQUOTED_ALPHA_TEXT | DECIMAL_NUMBER)+ {
> stack.pushUnquotedText(...); };
>
> Is there any way for me to gather the text of all the ALPHA_TEXT and
> DECIMAL_NUMBER tokens in this production into a single string and shove that
> string as an argument in my action?
>
Basically accumulate the text into a variable using an action inside the
(...)+ loop and then push the accumulation. Something like (UNTESTED):
text
@init{ String str = ""; } :
( t=(UNQUOTED_ALPHA_TEXT | DECIMAL_NUMBER) { str+=$t.text; } )+
{ stack.pushUnquotedText(str); }
;
Note that the t=(...) only works because all of the stuff inside the
(...) are Tokens (i think).
Note that any tokens that were put on the HIDDEN channel (or skip();d)
by the Lexer will not be accumulated/pushed.
Hope this helps...
-jbb
More information about the antlr-interest
mailing list