[antlr-interest] Parsing comments

David-Sarah Hopwood david-sarah at jacaranda.org
Thu Sep 17 11:33:57 PDT 2009


Soren Holstebroe wrote:
> Thank you for your answer,
> 
> As i understand it, your suggestion considers the whole template line as a
> single token, but I don't want the template token to eat up the rest of the
> line, since this is where my template wrapping grammar would be.
> 
> My grammar will have rules like this:
> 
> wrap_option
>     : WRAP_OPTION_BEGIN
>     (  'WRAP'
>     | 'TEMPLATE' '<' type_specifier (',' type_specifier)* '>'
>     )
>     ;
> 
> so I need something to match '//' WS* '@' as my WRAP_OPTION_BEGIN.

The tricky part here is that the parser should ideally report an error
if the template line does not end with a newline, but newline would
normally be a hidden token. Here's a possible solution (untested):


@lexer::members {
  private int newlineChannel = HIDDEN;
}

fragment WRAP_OPTION_BEGIN : ;

SLCOMMENT
  : '//' WS? ( '@' { $type = WRAP_OPTION_BEGIN;
                     newlineChannel = DEFAULT_CHANNEL; }
             | ( ~('@' | '\r' | '\n' | ' ' | '\t') (~('\r' | '\n'))* )? EOL
                   { $channel = HIDDEN; }
             )
  ;

NEWLINE
  : ('\r' '\n'? | '\n')
      { $channel = newlineChannel;
        newlineChannel = HIDDEN; }
  ;

fragment EOL
  : NEWLINE
  // allow single-line comment without newline at end of file
  | { input.LA(1) == EOF }?
  ;

WS : (' ' | '\t')+ { $channel = HIDDEN; } ;

// parser

wrap_option
  : WRAP_OPTION_BEGIN
  ( 'WRAP'
  | 'TEMPLATE' '<' type_specifier (',' type_specifier)* '>'
  )
  NEWLINE
  ;

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list