简体   繁体   中英

ANTLR: handle nested line comments

Usually, (for syntax coloring) I use following ANTLR lines (eg for Java or C++ sources) to detect block comments ( /* this is a comment */ ):

BlockComment
    :
      '/*' BlockCommentChar* '*/'
      -> skip
    ;

fragment BlockCommentChar
    : '*'  ~'/'
    | ~'*'
    ;

But in the V language block comments are allowed to be nested. I could imagine to somehow increase a counter at a detected /* and decrease it on */ , but how to do that and how to report everything between the /* fdksjfldjlf /* fjdsjfkds f */ jfdjf jd */ as one lexer token?

Something like this should work:

BlockComment
 : '/*' ( BlockComment | '/' ~'*' | ~'/' )*? '*/'
 ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM