Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

antlr match any character except

I have the following deffinition of fragment:

fragment CHAR   :'a'..'z'|'A'..'Z'|'\n'|'\t'|'\\'|EOF;  

Now I have to define a lexer rule for string. I did the following :

STRING   : '"'(CHAR)*'"'

However in string I want to match all of my characters except the new line '\n'. Any ideas how I can achieve that?

like image 561
Andrey Avatar asked Feb 14 '13 19:02

Andrey


1 Answers

You'll also need to exclude " besides line breaks. Try this:

STRING : '"' ~('\r' | '\n' | '"')* '"' ;

The ~ negates char-sets.

ut I want to negate only the new line from my CHAR set

No other way than this AFAIK:

STRING : '"' CHAR_NO_NL* '"' ;

fragment CHAR_NO_NL : 'a'..'z'|'A'..'Z'|'\t'|'\\'|EOF;  
like image 121
Bart Kiers Avatar answered Sep 24 '22 10:09

Bart Kiers