antlrantlr3

ASN.1/SMI comment syntax on ANTLR 3


On ANTLR 2, the comment syntax is like this,

// Single-line comments
SL_COMMENT
    : (options {warnWhenFollowAmbig=false;} 
    : '--'(  { LA(2)!='-' }? '-'    |   ~('-'|'\n'|'\r'))*  ( (('\r')? '\n') { newline(); }| '--') )
        {$setType(Token.SKIP);  }
    ;

However, when porting this to ANTLR 3,

SL_COMMENT
    : (
    : '--'(  { input.LA(2)!='-' }? '-'  |   ~('-'|'\n'|'\r'))*  ( (('\r')? '\n') | '--') )
        {$channel = HIDDEN;}
    ;

because there is no more options {warnWhenFollowAmbig=false;}, the following comment cannot be parsed correctly,

-- some comment -- some not comment

Then, what is the possible way to define this SL_COMMENT rule for ANTLR 3?


Solution

  • I came across a solution finally,

    SL_COMMENT
        : COMMENT ( ({input.LA(2) != '-'}? '-') => '-'  |   ~('-'|'\n'|'\r'))*  ( (('\r')? '\n') | COMMENT) 
            { $channel = HIDDEN; }
        ;