[antlr-interest] comments, newlines, and emit()

Andy Tripp antlr at jazillian.com
Fri Aug 3 13:06:04 PDT 2007


I'm parsing a language (visual basic), in which newlines are significant,
and it also allows single line comments. My comment rule is the standard
one that matches a single quote up to and including the next newline:

LINE_COMMENT
    : '\'' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    ;

The problem is that need to keep the '\n' on the DEFAULT channel, and
put the rest on the HIDDEN channel.

According to the "Emitting More Than One Token per Lexer Rule" section 
of the book,
it seems like I should be able to emit the newline on the DEFAULT 
channel like this:

LINE_COMMENT
    : '\'' ~('\n'|'\r')* '\r'? newline='\n' {emit(newline); 
$channel=HIDDEN;}
    ;

Yet that generates an emit() call that takes an int, which doesn't compile.

There must be an easy way to either emit an extra newline token here.
Anyone?

Andy



More information about the antlr-interest mailing list