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?