[antlr-interest] Conditional lexing

Jim Idle jimi at temporal-wave.com
Thu Sep 13 09:56:15 PDT 2012


You want a gated semantic predicate and will likely need a true and false
alt.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Mike Lischke
> Sent: Thursday, September 13, 2012 9:39 AM
> To: ANTLR Mailing List
> Subject: [antlr-interest] Conditional lexing
>
> 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
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list