[antlr-interest] Conditional lexing

Mike Lischke mike at lischke-online.de
Thu Sep 13 09:39:02 PDT 2012


Hi list,

I'd like to consume input in the lexer based on a variable I pass into my lexer rule. How can I do  this?

fragment VERSION_COMMENT_TAIL[bool matches_version]:
	{!matches_version}? ( options { greedy = false; }: . )* '*' '/' { $type = VERSION_COMMENT; $channel = 98; }
;

This shall consume everything like a normal multiline comment if the parameter is true. Otherwise it should do simply nothing. Unfortunately, the generated code creates an exception if the param is false and in addition runs the loop. Any other ideas?

If all fails I could simply make this instead:

fragment VERSION_COMMENT_TAIL[bool matches_version]:
	{ if (matches_version) return } ( options { greedy = false; }: . )* '*' '/' { $type = VERSION_COMMENT; $channel = 98; }
;

but that seems so hack-ish...

Mike
-- 
www.soft-gems.net



More information about the antlr-interest mailing list