Reputation: 253
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') )*? '"';
Upvotes: 6
Views: 7718
Reputation: 99859
There are two problems with the above rules.
'\\"'
.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') )*? '"';
Upvotes: 9