将attoparsec解析器转换为解析器,如果它消耗的字节数不是一定长度,则解析器失败

Cam*_*tin 1 haskell attoparsec

假设我有一个attoparsec解析器,x.

我正在寻找创建一个函数f :: Int -> Parser a -> Parser a,如果y = f n x,然后:

  • y如果x失败则失败
  • y如果x成功x则失败并且不消耗n字节
  • y 否则就会成功

我该怎么做呢?

Dan*_*ner 5

您可以使用匹配来实现它:

f n x = do
    (bs, res) <- match x
    guard (BS.length bs >= n)
    return res
Run Code Online (Sandbox Code Playgroud)

(<|>)在将其大量使用之前,您应该检查它是否以可接受的方式与之交互.