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

Transform an attoparsec parser into a parser which fails if the number of bytes it consumes is not of a certain length

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

我想创建一个函数 f :: Int -> Parser a -> Parser a,如果 y = f n x,那么:

我该怎么做?

你可以使用match来实现它:

f n x = do
    (bs, res) <- match x
    guard (BS.length bs >= n)
    return res

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