将 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
,那么:
如果 x
失败 ,则 y
失败
如果 x
成功并且 x
不消耗 n
字节 ,则 y
失败
y
否则成功
我该怎么做?
你可以使用match来实现它:
f n x = do
(bs, res) <- match x
guard (BS.length bs >= n)
return res
在大量使用它之前,您应该检查它是否以可接受的方式与 (<|>)
交互。
假设我有一个 attoparsec 解析器,x
。
我想创建一个函数 f :: Int -> Parser a -> Parser a
,如果 y = f n x
,那么:
-
如果
y
失败 如果x
成功并且x
不消耗n
字节 ,则 y
失败y
否则成功
x
失败 ,则 我该怎么做?
你可以使用match来实现它:
f n x = do
(bs, res) <- match x
guard (BS.length bs >= n)
return res
在大量使用它之前,您应该检查它是否以可接受的方式与 (<|>)
交互。