[antlr-interest] How to handle first non whitespace commentcharacters?

Micheal J open.zone at virgin.net
Wed Jun 13 02:50:18 PDT 2007


> -----Original Message-----
> From: antlr-interest-bounces at antlr.org 
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Loverde
> Sent: 13 June 2007 09:20
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How to handle first non whitespace 
> commentcharacters?
> 
> 
> Hello,
> 
> I was hoping someone could help me out, as I've been 
> struggling with this for a bit.  Basically, how do you handle 
> something like a COBOL asterisk comment character, where it 
> is only a valid line comment indicator if it is the first non 
> whitespace character on a line (but if it's part of an 
> expression such as 5 * 7 it shouldn't be treated as a line 
> comment character).
> 
> For example:
> 
> * This is a line comment
> IF 5 * 5 = 20 THEN
>     * this is also a line comment
>     NOTHING
> END-IF.
> 
> Ideally I'd like to have the line comments channeled to the 
> hidden channel similar to the "normal" line comment such as:
> 
> LINE_COMMENT
>     :    '!' ~('\n'|'\r')*  '\r'? '\n' {$channel=HIDDEN;}

How about something like the following:

WHITESPACE
	:	lcmt=LINE_COMMENT	
		{	if ( $lcmt1.pos == 1)
			{
				$type=LINE_COMMENT;
				$channel=HIDDEN;
			}
		} 
	|	nnw=NON_NEWLINE_WHITESPACE (NON_NEWLINE_WHITESPACE)*
		(	{ ($nnw.pos == 1) }? LINE_COMMENT 
			{	
				$type=LINE_COMMENT;
                        $channel=HIDDEN;
			}
		)?
	;

fragment LINE_COMMENT
     :    '*' ~('\n'|'\r')*  '\r'? '\n'


This is a rough translation of what CSharpLexer.g (from the
KCSParse/csharp_v1 sample for V2.7.7) does for preprocessor directives. You
may want to look at the original file.

Micheal

-----------------------
The best way to contact me is via the list/forum. My time is very limited.



More information about the antlr-interest mailing list