[antlr-interest] Escaping quotes in a lexer

Gavin Lambert antlr at mirality.co.nz
Tue Mar 17 11:50:35 PDT 2009


At 05:56 18/03/2009, Gesuato Emanuele wrote:
>I would like to use the ' character inside the string something 
>similar to:
>
>Invoice.customer='Tom L\'oreal'
>or (better)
>Invoice.customer="Tom L'oreal"
>
>I've got the String definition for such fields that is the 
>following:
>STRING : "'"((JOLLY)?(PAROLE|INTEGER)(JOLLY)?)"'";

Are you sure that *s can only appear at either end, never in the 
middle?

Anyway, if you want to permit a \' escape, then you have to 
explicitly spell it out.  Personally, though, I wouldn't try to be 
even slightly restrictive on the contents of the string -- leave 
that to later validation in the parser or external code:

STRING : '\'' ('\\' ('\'')? | ~('\\' | '\''))* '\'';



More information about the antlr-interest mailing list