Reputation: 168
Say I have some text:
a = "foobarbaz"
b = "foobar"
c = "foo"
d = "rubbish"
e = "foobazbar"
and three parsers foo, bar, and baz for the strings 'foo', 'bar' and 'baz' respectively.
How would I create a parser that would give me the results:
a = ["foo", "bar", "baz"]
b = ["foo", "bar"]
c = ["foo"]
d = []
e = ["foo"]
when run against the inputs above? Basically attempt each possibility until failure whilst constructing a list. I could use user state but I would like to avoid if possible. (I would like to keep the individual parsers themselves ignorant of user state)
the closest I have gotten is something like fooseq below:
let foo = pstring "foo"
let bar = pstring "bar"
let baz = pstring "baz"
let foobar = pipe2 foo bar Seq.of2
let foobarbaz = pipe3 foo bar baz Seq.of3
let fooseq = choice (Seq.map attempt [foobarbaz; foobar; foo |>> Seq.of1 ;])
//(the Seq.ofx functions just take arguments and create a sequence of them)
It seems to me there must be a better way of doing this?
Upvotes: 2
Views: 189
Reputation: 3072
FParsec has no built-in sequence combinator that does exactly what you're looking for, but you could implement one yourself like in the following example:
let mySeq (parsers: seq<Parser<'t,'u>>) : Parser<'t[],'u> =
let ps = Array.ofSeq parsers
if ps.Length = 0 then preturn [||]
else
fun stream ->
let mutable stateTag = stream.StateTag
let mutable reply = ps.[0] stream
let mutable error = reply.Error
let mutable myReply = Reply()
if reply.Status <> Ok then myReply.Result <- [||]
else
// create array to hold results
let mutable xs = Array.zeroCreate ps.Length
xs.[0] <- reply.Result
let mutable i = 1
while i < ps.Length do
stateTag <- stream.StateTag
reply <- ps.[i] stream
error <- if stateTag <> stream.StateTag then reply.Error
else mergeErrors error reply.Error
if reply.Status = Ok then
xs.[i] <- reply.Result
i <- i + 1
else // truncate array and break loop
xs <- Array.sub xs 0 i
i <- ps.Length
myReply.Result <- xs
myReply.Status <- if reply.Status = Error && stateTag = stream.StateTag
then Ok
else reply.Status
myReply.Error <- error
myReply
With the mySeq
combinator, you can express your fooSeq
parser as
let fooSeq = mySeq [foo; bar; baz]
Upvotes: 6