tim*_*bod 13 haskell attoparsec
我对attoparsec的这种行为感到有点困惑.
$ ghci
> :m Data.Attoparsec.Text
> :m + Data.Text
> parse (string (pack "module")) (pack "mox")
Partial _
> parse (string (pack "module")) (pack "moxxxx")
Fail "moxxxx" [] "Failed reading: takeWith"
>
Run Code Online (Sandbox Code Playgroud)
为什么我需要添加字符才能触发失败?
一旦遇到第一个"x",它不应该失败吗?
Dan*_*her 13
这是一个实现细节,string解析器在知道是否有足够的输入可能成功之前没有完成.这是这些解析器的全有或全无行为的结果(我认为,这通常对性能有益).
string :: Text -> Parser Text
string s = takeWith (T.length s) (==s)
Run Code Online (Sandbox Code Playgroud)
string s尝试取length s单位Text,然后与之比较s.
takeWith :: Int -> (Text -> Bool) -> Parser Text
takeWith n p = do
s <- ensure n
let h = unsafeTake n s
t = unsafeDrop n s
if p h
then put t >> return h
else fail "takeWith"
Run Code Online (Sandbox Code Playgroud)
takeWith n p首先尝试确保n单位Text可用,并且
ensure :: Int -> Parser Text
ensure !n = T.Parser $ \i0 a0 m0 kf ks ->
if lengthAtLeast (unI i0) n
then ks i0 a0 m0 (unI i0)
else runParser (demandInput >> go n) i0 a0 m0 kf ks
where
go n' = T.Parser $ \i0 a0 m0 kf ks ->
if lengthAtLeast (unI i0) n'
then ks i0 a0 m0 (unI i0)
else runParser (demandInput >> go n') i0 a0 m0 kf ks
Run Code Online (Sandbox Code Playgroud)
ensure n 创造了一个要求更多的延续 粥输入(Partial结果),如果它没有立即找到足够的输入.
你可能会失败
Prelude Data.Attoparsec.Text Data.Text> parseOnly (string (pack "module")) (pack "mox")
Left "not enough input"
Run Code Online (Sandbox Code Playgroud)
预先告诉解析器它将不再获得任何输入(然后demandInputfrom ensure使其失败)或稍后
Prelude Data.Attoparsec.Text Data.Text> parse (string (pack "module")) (pack "mox")
Partial _
Prelude Data.Attoparsec.Text Data.Text> feed it (pack "")
Fail "mox" ["demandInput"] "not enough input"
Run Code Online (Sandbox Code Playgroud)
通过告诉Partial结果就是这样,喂它一空Text.
| 归档时间: |
|
| 查看次数: |
1984 次 |
| 最近记录: |