[antlr-interest] Whitespace is significant sometimes

Daniel Lidström daniel.lidstrom at gpsgate.com
Mon Sep 27 08:16:02 PDT 2010


Hello,

I am trying to parse a simple templating language that may look like this:

"a  aa [T * 2]a a"

Here, the part inside [] should be calculated (T has an integer value) and 
replaced. The other part
is supposed to be left as-is. With those rules, the output should be "a aa 
30a a" (when T is 15).
I want to be able to have whitespace surrounding the '*', so I am sending ' 
' to the hidden channel.
But that breaks the parts outside [] into pieces. Is there a way to capture 
the part outside [] as-is,
with whitespace exactly like written, and the part inside [] is relaxed 
where whitespace to go hidden
channel?

Daniel

Here's my grammar, btw:

parse
	: (IDENT | variable)+ EOF
	;

variable
	: '[' mult ']'
	;

// the multiplication is optional
mult
	: IDENT ('*' INTEGER)?
	;

fragment DIGIT : '0'..'9' ;
INTEGER : DIGIT+ ;

fragment LETTER : 'a'..'z' | 'A'..'Z';
IDENT : LETTER+ ;

WS : ' '+ { $channel = HIDDEN; } ;
 



More information about the antlr-interest mailing list