简体   繁体   中英

ANTLR3 Exception to Comment Lexer rule

I am modifying an already existing ANTLR grammar file to allow a specific comment to be able to be parsed. Currently a Lexer rule exists to filter all comments to the hidden channel. The rule looks like:

 COMMENT : '//' .* ('\n' | '\r') {$channel = HIDDEN;};

I would like to modify this rule to allow a comment through if it is //@key . I attempted to play with negation statements to try and prevent the //@key statement from matching by using //~('@') which seemed to work, but there are other //@ tags that I would like to continue to filter to the hidden channel that this statement allowed through.

In lexer, if an input can match multiple rules, the topmost rule (in the order of the grammar file) is used.

So you could do:

SPECIAL_COMMENT : '//@key' .*  ('\n' | '\r');

COMMENT : '//' .* ('\n' | '\r') {$channel = HIDDEN;};

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