I have the following string that I'm trying to parse with parsimonious
:
(,,"My","Cool",,"Array",,,)
This is a string array where it's possible for each entry to be empty (I want to represent them with None
).
I've taken a number of runs at it, the closest I've gotten to something that "works" is this:
string = ~'"[^\"]+"'
comma = ","
array = "(" (comma / string)* ")"
Then I would just use the visitor functions to assemble the array based on what nodes are encountered in the array values. This will work for correctly formatted files, but the following would still be "valid", according to the grammar:
("My""Cool""Array")
In my experience, PEG parsers are a little tricky to work with when parsing arrays like this. Is there a set of rules I could use that would handle this correctly? Ideally I could detect the error during the parse, and not during the AST traversal.
I don't know Parsimonious, but a PCRE regex that matches the format you want would look like this:
Try it on regex101.com.
A quick attempt to convert it to a Parsimonious grammar seems to work: