[antlr-interest] Unquoting strings

Gavin Lambert antlr at mirality.co.nz
Wed May 14 13:26:15 PDT 2008


At 07:29 15/05/2008, Daniel Danciu wrote:
>Browsing through some tutorials, I was left with the impression 
>that appending an exclamation mark to a character would remove 
>that character from the parsed token, e.g.:
>
>fragment
>SingleQuotedString
>   :
>   '\''! // or single quoted string
>   ( '\\'! '\''
>   | ~('\''|'\n'|'\r')
>   )*
>    '\''!;

That works in ANTLR v2, but not v3.  In v3 the ! operator only 
works at the parser level (to leave a token out from the 
automatically-generated AST).

>STRING
>: (DoubleQuotedString | SingleQuotedString)
>   {
>           // Strip the surrounding quotes
>           String txt = getText();
>           setText(txt.substring(1, txt.length() -1));
>   };

That's basically just "what you have to do" in v3.

See: <http://www.antlr.org/wiki/pages/viewpage.action?pageId=1461>

Though, you might also want to handle escape characters at the 
same time.  You will need to do a manual search/replace for those.



More information about the antlr-interest mailing list