3

I want to parse a GraphQL document using Dart PetitParser. To be able to support BlockString (multi-line string) I'm looking for a way to get

from

"""
abc
\"""
def
"""

this part out

abc
\"""
def

Full syntax https://facebook.github.io/graphql/draft/#sec-String-Value

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567

1 Answers1

2

I am on a mobile Phone and I don't have a computer to test, but something along these lines should work:

string('"""') & (string(r'\"""') | any()).starLazy(string('"""')) & string('"""')

This parses the triple quotes, followed by any sequence of the escaped triple quotes or other characters, until we reach the ending triple quotes. Possibly you want to also add a .flatten() to the inner part to get a plain string as return value.

Lukas Renggli
  • 8,754
  • 23
  • 46
  • Thanks for your support! That didn't work though, but I had accidentally left some other line from previous experiments that made it work `string('"""') & (string(r'\"""') | any()).starLazy(string('"""')) & ref(token, char(r'\').not().seq(string('"""')));` Does this make sense? Can this be improved or simplified further? – Günter Zöchbauer Nov 01 '18 at 08:47
  • Actually I used `ref(sourceCharacter)` instead of `any()` with `Parser sourceCharacter() => pattern('$tabChar$newLineChar$carriageReturnChar$spaceChar-\uFFFF');` but `any()` worked as well. – Günter Zöchbauer Nov 01 '18 at 08:52
  • Strange, I've [added my solution as a test to the library](https://github.com/petitparser/dart-petitparser/blob/master/petitparser/test/petitparser_test.dart#L1050) and it seems to work for my examples. In your code, it is not clear to me what the `.not()` part is supposed to do or why it should be necessary? – Lukas Renggli Nov 03 '18 at 17:46
  • Thanks for checking! I don't understand at all why my code above worked. Only on the phone now. I'll investigate more tomorrow. – Günter Zöchbauer Nov 03 '18 at 18:03
  • I changed my tests like you have it in your project, and now it's working. Looks like there was an issue with my test. Thanks a lot for your support! Should the [.pick(1)](https://github.com/petitparser/dart-petitparser/blob/master/petitparser/test/petitparser_test.dart#L966) be added to the answer above as well? It doesn't seem to make a difference though. – Günter Zöchbauer Nov 05 '18 at 09:37