yhirose / cpp-peglib

A single file C++ header-only PEG (Parsing Expression Grammars) library
MIT License
916 stars 113 forks source link

Proper way to handle double quoted escape sequence? #259

Closed gamagan closed 1 year ago

gamagan commented 1 year ago

What's the way to handle double quoted escape sequences?

I found a similar question: https://github.com/yhirose/cpp-peglib/issues/4 but that answer doesn't properly create C/C++ type of string escape sequences. Specifically, escaping a double quote requires an odd number of backslashes. An even number escapes the backslash itself.

"\""     =   "
"\\"     =   \
"\\\""   =   \"
"\\\\"   =   \\
"\\\\\"" =   \\"

The answer in the linked post would consider the following a complete string, but it should be a syntax error: "abc\\"d"

yhirose commented 1 year ago

Hope this helps.

image
gamagan commented 1 year ago

Thank you, at @yhirose. For posterity: I ended up boiling my version down to the following, for a string that accepts anything inside, plus the escaped sequences:

String    <- '"' < ('\\\\' / '\\"' / (!["] .))* > '"'