[antlr-interest] Request for Change regarding Lexer (?)

Gavin Lambert antlr at mirality.co.nz
Tue Feb 16 12:27:11 PST 2010


At 08:13 17/02/2010, Terence Parr wrote:
 >} and what I need is loop escaping dependent on previous match, 
so
 >some constrained context passing would be nice.
 >
 >try predicates. :)

While it's usually possible to frame the contents of a looping 
construct in a positive sense (which is what ANTLR currently 
requires), it'd be nice if there were a language construct that 
would let you do specific negative matches too (maybe a feature 
request for v4?).

For (a completely made up) example, consider a case where you 
might want to match any identifier except one starting with 
"foo".  In current ANTLR, you'd have to do one of these:
   FOOLIST: 'foo[' NON_FOO_ID+ ']';
   FOOLIST: 'foo[' ({!next_id_starts_with_foo()}? => ID)+ ']';
   FOOLIST: 'foo[' ((~'f' | 'f' ~'o' | 'fo' ~'o') => ID)+ ']';

It'd be nice if there was some way to express a negative match via 
a syntactic predicate, eg:
   FOOLIST: 'foo[' (('foo') => ~ | ID)+ ']';
(where '~' in an alt basically means "break", ie. match nothing 
and terminate the innermost loop.)
Or, perhaps better:
   FOOLIST: 'foo[' (('foo') ~=> ID)+ ']';
(where '~=>' means "only take this path if the predicate *fails*")

Granted, this sort of requirement doesn't come up often, but when 
it does it'd be nice to have a tidier way of expressing it; and 
it'd be fairly simple to implement... :)



More information about the antlr-interest mailing list