When I get the token with these rules
STRINGA : '"' (options {greedy=false;}: ESC | .)* '"';
STRINGB : '\'' (options {greedy=false;}: ESC | .)* '\'';
it ends up grabbing 'text' instead of just text. I can easily remove the ' and ' myself but was wondering how I can get ANTLR to remove it?
You will need some custom code for that. Also, you shouldn’t be using a
.(dot) inside the rule: you should explicitly define you want to match everything except a backslash (assuming that is what yourESQstarts with), a quote and line break chars probably.Something like this would do it:
If you now parse the input
"tabs:'\t\t\t'\nquote:\"\nbackslash:\\", the following will be printed to the console:To keep the grammar clean, you could of course move the code in a custom method: