[antlr-interest] Passing list of tokens to StringTemplate

Terence Parr parrt at cs.usfca.edu
Fri Jul 25 10:28:22 PDT 2008


Easier is to use propertyOf the token.

toks(v) ::= "<v:{x | <x.text>}>"

  for example.

Ter
On Jul 25, 2008, at 5:10 AM, James wrote:

> 2008/7/14 Gavilan <gavilancomun at gmail.com>:
>> I would like to pass a list of tokens to a template.
>>
>> grammar toks;
>>
>> options { output=template; }
>>
>> INT: ('+'|'-')? ('0'..'9')+;
>> WS: (' '|'\t'|'\r'|'\n')+ { skip(); };
>>
>> toks: t+=INT t+=INT t+=INT -> toks(v={$t});
>>
>>
>> On input:
>> -1 0 23
>>
>> I get:
>> <toks>[@0,0:1='-1',<4>,1:0], [@1,3:3='0',<4>,1:3],
>> [@2,5:6='23',<4>,1:5]</toks>
>
> I have fixed this by adding an attribute renderer:
>
> public class TokenRenderer implements AttributeRenderer {
>
> public String toString(Object o) {
> 	Token t = (Token) o;
> 	return t.getText();
> }
>
> public String toString(Object o, String formatName) {
> 	return toString(o);
> }
>
> Best Wishes,
> James



More information about the antlr-interest mailing list