2
說我有一些文字:FParsec - 解析器序列
a = "foobarbaz"
b = "foobar"
c = "foo"
d = "rubbish"
e = "foobazbar"
和三個解析器富,酒吧和巴茲爲字符串「富」,分別爲「酒吧」和「巴茲」。
我將如何創建一個解析器,這將使我的結果:
a = ["foo", "bar", "baz"]
b = ["foo", "bar"]
c = ["foo"]
d = []
e = ["foo"]
時對輸入上面跑?基本上嘗試每種可能性,直到構建列表失敗。我可以使用用戶狀態,但我希望儘可能避免。 (我想保持獨立的解析器自己無知的用戶狀態的)
我得到的最接近的是類似下面fooseq:
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)
在我看來,必須有這樣做的更好的辦法?