ANTLR 4 $channel = HIDDEN and options

后端 未结 1 456
时光取名叫无心
时光取名叫无心 2020-12-28 13:01

I need help with my ANTLR 4 grammar after deciding to switch to v4 from v3. I am not very experienced with ANTLR so I am really sorry if my question is dumb ;)

In v3

相关标签:
1条回答
  • 2020-12-28 13:56

    The v4 equivalent would look like:

    COMMENT
        :   ( '//' ~[\r\n]* '\r'? '\n'
            | '/*' .*? '*/'
            ) -> channel(HIDDEN)
        ;
    

    which will put all single- and multi line comment on the HIDDEN channel. However, if you're not doing anything with these HIDDEN-tokens, you could also skip these tokens, which would look like this:

    COMMENT
        :   ( '//' ~[\r\n]* '\r'? '\n'
            | '/*' .*? '*/'
            ) -> skip
        ;
    

    Note that to tell the lexer or parser to match ungreedy, you don't use options {greedy=false;} anymore, but append a ?, similar to many regex implementations.

    0 讨论(0)
提交回复
热议问题