[antlr-interest] Re: nested comments

Richard Spence rkmspence at yahoo.co.uk
Mon Jul 4 13:30:52 PDT 2005


Here is a complete, working, nested C-style comment lexer fragment. The 
NEWLINE rule allows the 3 different EOL combinations of '\n' and '\r'. 
C++ target.

CCOMMENT
	:   "/*"
         (   CCOMMENT
         |   '*'
             (   NEWLINE
             |   ~('/' | '\n' | '\r')
             )
		|	NEWLINE
		|	~('/' | '*' | '\n' | '\r')
         )*
         "*/"
         {$setType(antlr::Token::SKIP);}
	;

protected NEWLINE
{
     unsigned int mask = 0;
}
     :   (   options {greedy=true;}:

             '\n' | '\r'
             {
                 unsigned int bit = 1 << $getText[0];

                 if(mask & bit) {
                     mask = bit;
                     newline();
                 }
                 else {
                     mask |= bit;
                 }
             }
         )+
         {
             if(mask) {
                 newline();
             }
         }
     ;

--
-rkms



More information about the antlr-interest mailing list