pointfreeco/swift-parsing

Can `Many` be made to accept an optional separator?

Mozahler opened this issue · 1 comments

If this is the wrong place to ask a question about how to work with the parsing package, please let me know where to redirect.

I've set up a parser (everything has been simplified for reproducibility) that depends upon an item:

public enum Item {
   case item1(Substring)
   case item2(Substring)
   case item3(Substring, Substring)
}

I've written a handful of parsers in this format:

let parseAnItemTwo = Parse(Item.item2) {
   "@@ "
   Prefix { $0 != "\n" }
   "\n"

So far so good. I can hand roll the parsing of a given string when I specify them in the correct order.

But I want to use something similar to OneOf (or an equivalent conditional) to parse a stream of tokens from the input (resulting in Array)

let fullSuite =  OneOf {
      parseAnItemOne
      parseAnItemTwo
      parseAnItemThree
   }
}

I would then run this on my input, which works fine as long as I have a newline between each token

let iterateForTokens = Many {
   fullSuite
} separator: {
   "\n"
}

I need the separator to be optional (there might be newlines between records/tokens, but it's not a requirement).

I tried revising your zeroOrMoreSpaces:

let zeroOrMoreNewlines = Prefix { $0 == "\n" }

let iterateForTokens = Many {
   fullSuite
} separator: {
   zeroOrMoreNewlines
}

This compiles, but produces a run-time error:

The operation couldn’t be completed. (Parsing.ParsingError error 0.)

Again: All tokens in the input stream have unique starting sequences, so explicit delimiters aren't required in the data stream.

Hi @Mozahler! We use GitHub discussions for questions about library usage. I'll convert it for you.