[antlr-interest] skipping whitespaces in code and avoiding it in comments

Maciej Gawinecki mgawinecki at gmail.com
Sun Mar 8 12:44:25 PDT 2009


Hello,

Lexer can /skip/ or send to /hidden channel/ the tokens that are 
whitespaces.

However I would like it not to skip them when the parser recognizes a 
comment fragment, because I want to buffer comments including their 
whitespaces.

It is trivial to pass some information inside of parser instance via 
instance variables but how can I pass informatiom from a parser to a 
lexer to temporary avoid skipping whitespaces? Or there is some 
workaround for my goal?

Thanks for your time,
Maciej

ps. Below is the grammar that accept statements with comments like:

   x = 5; // this is my x

Together with trial (of course not working) to passing information about 
comments turned on/off.

------------------------
grammar sqltest;

@members {
   boolean isComment = false;
}

file 	:	statement EOF ;

statement
	:	id '=' value ';' ('//' comment)?
	
	{
		System.out.println($id.text + ':' + $comment.text) ;	
	};


value 	:	DIGIT+ ;
id 	:	LETTER+ ;
	
comment
@init{ isComment = true; }
@after{ isComment = false; }
	:	(LETTER|DIGIT)* ;
	
LETTER 	:	'a'..'z'|'A'..'Z' ;

DIGIT
	:	'0'..'9' ;

WS
     :   (
              ' '
         |    '\r'
         |    '\t'
         |    '\n'
         )
             {
             	if (!isComment)               	
                 	skip();
             }
     ;		



More information about the antlr-interest mailing list