Jan,
My general rule of thumb is to have the parser pull out the data I
ultimately want to keep and have the transform to turn it into a more
usable structure.
You could make it ignore the \ by naming (.as()) everything else. That
would end up with a structure like :string => [{}, {}, {}], an array
of hashes of data you want to keep. Then you could write a transform
rule to combine the data back into one string.
That would be the technically correct way to do it using parslet,
however that seems a little overkill for your case. I might honestly
just write a transform rule that does the unescaping rather than
getting fancy with the parser. Either way, you'd be using transform to
do something.
-mj
On Thu, Aug 25, 2011 at 7:42 PM, Jan-Paul Bultmann
<[email protected]> wrote:
> Hey,
> I was just wondering how to do this properly,
> say I want to parse a String and collect its contents while doing so. ' " '
> are not permitted unless they are escaped by a ' \ '.
> A rule could look something like this:
>
> rule(:string) { str('"') >> (str('\\"') | str('"').absent? >>
> any).repeat.as(:string) >> str('"') }
>
> While collecting everything will get collected, but it would be handy to do
> something like this:
>
> rule(:string) { str('"') >> (str('\\').ignore >> str('"') |
> str('"').absent? >> any).repeat.as(:string) >> str('"') }
>
> so that the ' \ ' won't get collected with the rest, which results in the
> escape automagically being resolved :)
>
> Is something like this possible? Is it even a good idea, or should it be done
> in the transform step?
>
> Cheers,
> Jan
>