[antlr-interest] Preserving whitespaces when rewriting with StringTemplate

Robert.Klaus at innovations.de Robert.Klaus at innovations.de
Mon Oct 22 06:14:57 PDT 2007


Hi all,

I'm rewriting a language using ANTLR 3.0.1.

One of the tasks is to rewrite certain operator symbols to keywords,
e.g. '&' becomes 'intersect'.


In the parser I've defined a rule such as

r:
	unary (AMPERSAND^ unary)?
;

Where AMPERSAND is defined in the tokens section as AMPERSAND = '&';

The rewriting itself is done in the treeparser, where I have a matching
rule:

r:
	^(AMPERSAND a=unary b=unary) ->
template(left={$a.text},right={$b.text}) "<left> intersect <right>"
;

So far everything works out quite nicely. When I'm using the
StringTemplate this way, I loose all whitespaces, which should be
preserved if possible.
For example: "a\n\n &         b" now becomes "a intersect b".

It would suffice for this case to replace the token or it's text in the
TokenRewriteStream. I couldn't figure out how to do this in an easy way.
Does ANTLR support this or is there a smarter way to preserve the
whitespaces?


Thanks in advance,

Robert

P.S.
Whitespaces are kept on a different channel than the normal tokens


-------------------------------------------------------------
Robert Klaus
Diplom Informatiker
Product Development
-------------------------------------------------------------
Innovations Softwaretechnologie GmbH
Ziegelei 7
88090 Immenstaad
Germany
Phone: +49(0)7545-202-339
robert.klaus at innovations.de
www.visual-rules.com
-------------------------------------------------------------
Executives: 
Achim Berger, Thomas Cotic, Walter Pitz
Register Court Ulm HRB 631622
-------------------------------------------------------------


More information about the antlr-interest mailing list