[antlr-interest] Concatenating variable number of tokens into a single string in an action?
Jim Idle
jimi at temporal-wave.com
Mon Jul 18 14:41:04 PDT 2011
But use a StringBuilder if there are lots.
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of John B. Brodie
> Sent: Monday, July 18, 2011 1:53 PM
> To: Dejas Ninethousand
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Concatenating variable number of tokens
> into a single string in an action?
>
> 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
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
More information about the antlr-interest
mailing list