git.iko.soy/typeable/attoparsec
on master help sign in
Fix up a couple of subtly broken parsers.
commitebfdcc44beb1231397ca892f8373fea6bcff2bc5 authorBryan O'Sullivan <bos@serpentine.com> dateThu 4 Mar 2010, 21:59 (17 years ago) parent66ab088
1 file changed+36−21
Data/Attoparsec/Internal.hs+36−21
Data/Attoparsec/Internal.hs+36−21
@@ -183,18 +183,29 @@ ensure :: Int -> Parser ()
183183ensure n = Parser $ \st0@(S s0 _a0 _c0) kf ks ->
184184 if B.length s0 >= n
185185 then ks st0 ()
186− else runParser (requireInput >> ensure n) st0 kf ks
186+ else runParser (demandInput >> ensure n) st0 kf ks
187187
188−requireInput :: Parser ()
189−requireInput = Parser $ \st0@(S s0 a0 c0) kf ks ->
188+demandInput :: Parser ()
189+demandInput = Parser $ \st0@(S s0 a0 c0) kf ks ->
190190 if c0 == Complete
191− then kf st0 ["requireInput"] "not enough bytes"
191+ then kf st0 ["demandInput"] "not enough bytes"
192192 else Partial $ \s ->
193193 if B.null s
194− then kf (S s0 a0 Complete) ["requireInput"] "not enough bytes"
194+ then kf (S s0 a0 Complete) ["demandInput"] "not enough bytes"
195195 else let st1 = S (s0 +++ s) (a0 +++ s) Incomplete
196196 in ks st1 ()
197197
198+wantInput :: Parser Bool
199+wantInput = Parser $ \st0@(S s0 a0 c0) _kf ks ->
200+ case undefined of
201+ _ | not (B.null s0) -> ks st0 True
202+ | c0 == Complete -> ks st0 False
203+ | otherwise -> Partial $ \s ->
204+ if B.null s
205+ then ks st0 False
206+ else let st1 = S (s0 +++ s) (a0 +++ s) Incomplete
207+ in ks st1 True
208+
198209get :: Parser B.ByteString
199210get = Parser (\st0 _kf ks -> ks st0 (input st0))
200211
@@ -259,13 +270,14 @@ stringTransform f s = takeWith (B.length s) ((==s) . f)
259270{-# INLINE stringTransform #-}
260271
261272skipWhile :: (Word8 -> Bool) -> Parser ()
262−skipWhile p = do
263− (`when` requireInput) =<< B.null <$> get
264− t <- B8.dropWhile p <$> get
265− put t
266− if B.null t
267− then (skipWhile p <|> return ())
268− else return ()
273+skipWhile p = go
274+ where
275+ go = do
276+ input <- wantInput
277+ when input $ do
278+ t <- B8.dropWhile p <$> get
279+ put t
280+ when (B.null t) go
269281
270282takeTill :: (Word8 -> Bool) -> Parser B.ByteString
271283takeTill p = takeWhile (not . p)
@@ -275,21 +287,24 @@ takeWhile :: (Word8 -> Bool) -> Parser B.ByteString
275287takeWhile p = go
276288 where
277289 go = do
278− (`when` requireInput) =<< B.null <$> get
279− (h,t) <- B8.span p <$> get
280− put t
281− if B.null t
282− then (h+++) `fmapP` (go <|> return B.empty)
283− else return h
290+ input <- wantInput
291+ if input
292+ then do
293+ (h,t) <- B8.span p <$> get
294+ put t
295+ if B.null t
296+ then (h+++) `fmapP` go
297+ else return h
298+ else return B.empty
284299
285300takeWhile1 :: (Word8 -> Bool) -> Parser B.ByteString
286301takeWhile1 p = do
287− (`when` requireInput) =<< B.null <$> get
302+ (`when` demandInput) =<< B.null <$> get
288303 (h,t) <- B8.span p <$> get
289304 when (B.null h) $ failDesc "takeWhile1"
290305 put t
291306 if B.null t
292− then (h+++) `fmapP` (takeWhile p <|> return B.empty)
307+ then (h+++) `fmapP` takeWhile p
293308 else return h
294309
295310-- | Match any character in a set.
@@ -334,7 +349,7 @@ endOfInput = Parser $ \st0@S{..} kf ks ->
334349 then ks st0 ()
335350 else let kf' st1 _ _ = ks (mappend st0 st1) ()
336351 ks' st1 _ = kf (mappend st0 st1) [] "endOfInput"
337− in runParser requireInput st0 kf' ks'
352+ in runParser demandInput st0 kf' ks'
338353 else kf st0 [] "endOfInput"
339354
340355endOfLine :: Parser ()