Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing string literals with escaped quotes in ANTLR

I am a bit perplexed about how to capture a quoted string in ANTLR4.

Currently, this lexer rule is not tokenizing the way I expect.

The sample string is "=\"". I've tried lots of different ways to capture this, but I am at a loss about what I am doing incorrectly. I'd really appreciate some insights on best practices for this. Thank you so much!

ESCAPED_QUOTE : '\"';
QUOTED_STRING :   '"' ( ESCAPED_QUOTE | ~('\n'|'\r') )*? '"';
like image 883
Steve H. Avatar asked Oct 07 '13 23:10

Steve H.


1 Answers

There are two problems with the above rules.

  1. You didn't actually escape your quote like you thought. You meant to use '\\"'.
  2. Your ESCAPED_QUOTE rule doesn't form a token all by itself, so it should be a fragment rule.

The result of these two changes would be the following:

fragment ESCAPED_QUOTE : '\\"';
QUOTED_STRING :   '"' ( ESCAPED_QUOTE | ~('\n'|'\r') )*? '"';
like image 83
Sam Harwell Avatar answered Oct 19 '22 11:10

Sam Harwell