1

I want to write a parser rule to parse a valid String, my rule goes like this:

STRING: '"' [\s\S]+ '"';

But it gives me a warning saying invalid escape sequence \s. I tried other escape sequence like \t, \n... they are all fine.

Can anyone tell me what's going on? \s\S is not supported? What alternatives can I find for parse a valid String?

Thanks!

paranoider
  • 27
  • 2

1 Answers1

2

ANTLR doesn't use Regex, even if it looks like that sometimes. It doesn't know escaped entities like \s or \w. You should instead use:

STRING: '"' .*? '"';

This is a non-greedy scan of any char between double quotes. ANTLR4 is clever enough to exclude the finishing quote from the any-char match. An often used variation of that is:

STRING: '"' ~["]* '"';

I recommend to look at existing grammars to learn how to write rules, e.g. in this Java grammar. Also read the ANTLR4 documentation about lexer rules on Github.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181