pythonstringparsingpegparsimonious

Escaped Strings in Parsing Expression Grammars


I am attempting to write a grammar for a small language utility using the python library parsimonious, but I am struggling with writing a part, which covers strings, especially strings with escaped quotes and other special characters.

I have the following:

string         = doubleString / singleString
doubleString   = "\"" escapedString "\""
singleString   = "'" escapedString "'"

escapedString is as if yet undefined, but should accept anything one would reasonably expect a string in a programming language to accept. I cannot think of where to begin. Does anyone have any suggestions?


Solution

  • I don't know parsimonious's syntax, but in a regex-style one I'd do something like:

    string         = doubleString / singleString
    doubleString   = ~'"([^"]|(\"))*"'
    singleString   = ~"'([^']|(\'))*'"
    

    i.e. you'd need a different escaped string for each kind of string, each made of a possibly empty sequence of either characters that are not the end quote char or escaped end quote chars.