[antlr-interest] Passing list of tokens to StringTemplate

James gavilancomun at gmail.com
Fri Jul 25 05:10:18 PDT 2008


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