kmachstang
kmachstang

Reputation: 43

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.

Upvotes: 1

Views: 50

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12440

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;};

Upvotes: 2

Related Questions