on master help sign in
Rename the parser Step constructors

Yield => Partial Skip => Continue Stop => Done The new names are hopefully more intuitive. They also decouple the names from the corresponding stream constructors so that there is no confusion about mixing the two intuitions. fixes #559

commit6bf8b4abd96816dfd5a446de88f125e2c015f9b6 authorHarendra Kumar <harendra@composewell.com> dateWed 24 Jun 2020, 01:09 (6 years ago) parent60d3228
5 files changed+257−245
src/Streamly/Internal/Data/Parser/ParserD.hs+40−40 src/Streamly/Internal/Data/Parser/ParserD/Tee.hs+55−55 src/Streamly/Internal/Data/Parser/ParserD/Types.hs+112−100 src/Streamly/Internal/Data/Stream/StreamD.hs+30−30 src/Streamly/Internal/Data/Zipper.hs+20−20
src/Streamly/Internal/Data/Parser/ParserD.hs+40−40
@@ -190,7 +190,7 @@ fromFold (Fold fstep finitial fextract) = Parser step finitial fextract
190190
191191 where
192192
193− step s a = Yield 0 <$> fstep s a
193+ step s a = Partial 0 <$> fstep s a
194194
195195-------------------------------------------------------------------------------
196196-- Convert to and from CPS style parser representation
@@ -241,11 +241,11 @@ any predicate = Parser step initial return
241241
242242 step s a = return $
243243 if s
244− then Stop 0 True
244+ then Done 0 True
245245 else
246246 if predicate a
247− then Stop 0 True
248− else Yield 0 False
247+ then Done 0 True
248+ else Partial 0 False
249249
250250{-# INLINABLE all #-}
251251all :: Monad m => (a -> Bool) -> Parser m a Bool
@@ -259,9 +259,9 @@ all predicate = Parser step initial return
259259 if s
260260 then
261261 if predicate a
262− then Yield 0 True
263− else Stop 0 False
264− else Stop 0 False
262+ then Partial 0 True
263+ else Done 0 False
264+ else Done 0 False
265265
266266-------------------------------------------------------------------------------
267267-- Failing Parsers
@@ -279,7 +279,7 @@ peek = Parser step initial extract
279279
280280 initial = return ()
281281
282− step () a = return $ Stop 1 a
282+ step () a = return $ Done 1 a
283283
284284 extract () = throwM $ ParseError "peek: end of input"
285285
@@ -311,7 +311,7 @@ satisfy predicate = Parser step initial extract
311311
312312 step () a = return $
313313 if predicate a
314− then Stop 0 a
314+ then Done 0 a
315315 else Error "satisfy: predicate failed"
316316
317317 extract _ = throwM $ ParseError "satisfy: end of input"
@@ -337,8 +337,8 @@ take n (Fold fstep finitial fextract) = Parser step initial extract
337337 let i1 = i + 1
338338 s1 = Tuple' i1 res
339339 if i1 < n
340− then return $ Yield 0 s1
341− else Stop 0 <$> fextract res
340+ then return $ Partial 0 s1
341+ else Done 0 <$> fextract res
342342
343343 extract (Tuple' _ r) = fextract r
344344
@@ -358,7 +358,7 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
358358 res <- fstep r a
359359 let i1 = i + 1
360360 s1 = Tuple' i1 res
361− if i1 < n then return (Skip 0 s1) else Stop 0 <$> fextract res
361+ if i1 < n then return (Continue 0 s1) else Done 0 <$> fextract res
362362
363363 extract (Tuple' i r) =
364364 if n == i
@@ -389,8 +389,8 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
389389 s1 = Tuple' i1 res
390390 return $
391391 if i1 < n
392− then Skip 0 s1
393− else Yield 0 s1
392+ then Continue 0 s1
393+ else Partial 0 s1
394394
395395 extract (Tuple' i r) = fextract r >>= f
396396
@@ -420,8 +420,8 @@ takeWhile predicate (Fold fstep finitial fextract) =
420420
421421 step s a =
422422 if predicate a
423− then Yield 0 <$> fstep s a
424− else Stop 1 <$> fextract s
423+ then Partial 0 <$> fstep s a
424+ else Done 1 <$> fextract s
425425
426426-- | See 'Streamly.Internal.Data.Parser.takeWhile1'.
427427--
@@ -441,16 +441,16 @@ takeWhile1 predicate (Fold fstep finitial fextract) =
441441 then do
442442 s <- finitial
443443 r <- fstep s a
444− return $ Yield 0 (Just r)
444+ return $ Partial 0 (Just r)
445445 else return $ Error "takeWhile1: empty"
446446 step (Just s) a =
447447 if predicate a
448448 then do
449449 r <- fstep s a
450− return $ Yield 0 (Just r)
450+ return $ Partial 0 (Just r)
451451 else do
452452 b <- fextract s
453− return $ Stop 1 b
453+ return $ Done 1 b
454454
455455 extract Nothing = throwM $ ParseError "takeWhile1: end of input"
456456 extract (Just s) = fextract s
@@ -469,8 +469,8 @@ sliceSepBy predicate (Fold fstep finitial fextract) =
469469 initial = finitial
470470 step s a =
471471 if not (predicate a)
472− then Yield 0 <$> fstep s a
473− else Stop 0 <$> fextract s
472+ then Partial 0 <$> fstep s a
473+ else Done 0 <$> fextract s
474474
475475-- | See 'Streamly.Internal.Data.Parser.sliceEndWith'.
476476--
@@ -511,11 +511,11 @@ sliceSepByMax predicate cnt (Fold fstep finitial fextract) =
511511 let i1 = i + 1
512512 s1 = Tuple' i1 res
513513 if i1 < cnt
514− then return $ Yield 0 s1
514+ then return $ Partial 0 s1
515515 else do
516516 b <- fextract res
517− return $ Stop 0 b
518− | otherwise = Stop 0 <$> fextract r
517+ return $ Done 0 b
518+ | otherwise = Done 0 <$> fextract r
519519 extract (Tuple' _ r) = fextract r
520520
521521-- | See 'Streamly.Internal.Data.Parser.wordBy'.
@@ -553,14 +553,14 @@ eqBy cmp str = Parser step initial extract
553553
554554 initial = return str
555555
556− step [] _ = return $ Stop 0 ()
556+ step [] _ = return $ Done 0 ()
557557 step [x] a = return $
558558 if x `cmp` a
559− then Stop 0 ()
559+ then Done 0 ()
560560 else Error "eqBy: failed, yet to match the last element"
561561 step (x:xs) a = return $
562562 if x `cmp` a
563− then Skip 0 xs
563+ then Continue 0 xs
564564 else Error $
565565 "eqBy: failed, yet to match " ++ show (length xs + 1) ++ " elements"
566566
@@ -588,9 +588,9 @@ lookAhead (Parser step1 initial1 _) =
588588 r <- step1 st a
589589 let cnt1 = cnt + 1
590590 return $ case r of
591− Yield n s -> Skip n (Tuple' (cnt1 - n) s)
592− Skip n s -> Skip n (Tuple' (cnt1 - n) s)
593− Stop _ b -> Stop cnt1 b
591+ Partial n s -> Continue n (Tuple' (cnt1 - n) s)
592+ Continue n s -> Continue n (Tuple' (cnt1 - n) s)
593+ Done _ b -> Done cnt1 b
594594 Error err -> Error err
595595
596596 -- XXX returning an error let's us backtrack. To implement it in a way so
@@ -711,26 +711,26 @@ manyTill (Fold fstep finitial fextract)
711711 step (ManyTillR cnt fs st) a = do
712712 r <- stepR st a
713713 case r of
714− Yield n s -> return $ Yield n (ManyTillR 0 fs s)
715− Skip n s -> do
714+ Partial n s -> return $ Partial n (ManyTillR 0 fs s)
715+ Continue n s -> do
716716 assert (cnt + 1 - n >= 0) (return ())
717− return $ Skip n (ManyTillR (cnt + 1 - n) fs s)
718− Stop n _ -> do
717+ return $ Continue n (ManyTillR (cnt + 1 - n) fs s)
718+ Done n _ -> do
719719 b <- fextract fs
720− return $ Stop n b
720+ return $ Done n b
721721 Error _ -> do
722722 rR <- initialL
723− return $ Skip (cnt + 1) (ManyTillL fs rR)
723+ return $ Continue (cnt + 1) (ManyTillL fs rR)
724724
725725 step (ManyTillL fs st) a = do
726726 r <- stepL st a
727727 case r of
728− Yield n s -> return $ Yield n (ManyTillL fs s)
729− Skip n s -> return $ Skip n (ManyTillL fs s)
730− Stop n b -> do
728+ Partial n s -> return $ Partial n (ManyTillL fs s)
729+ Continue n s -> return $ Continue n (ManyTillL fs s)
730+ Done n b -> do
731731 fs1 <- fstep fs b
732732 l <- initialR
733− return $ Yield n (ManyTillR 0 fs1 l)
733+ return $ Partial n (ManyTillR 0 fs1 l)
734734 Error err -> return $ Error err
735735
736736 extract (ManyTillL fs sR) = extractL sR >>= fstep fs >>= fextract
src/Streamly/Internal/Data/Parser/ParserD/Tee.hs+55−55
@@ -73,7 +73,7 @@ import Streamly.Internal.Data.Parser.ParserD.Types
7373-- source for individual parser backtracking. Problem arises when both the
7474-- parsers backtrack and they do not need any input from the driver rather they
7575-- must consume from their buffers. For such situation we may need a
76−-- "Skip/Continue" style driver command from the tee so that the driver runs
76+-- "Continue" style driver command from the tee so that the driver runs
7777-- the tee without providing it any input. Or we may need a local driver loop
7878-- until new input is to be demanded from the input stream.
7979--
@@ -137,19 +137,19 @@ teeWith zf (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
137137 useStream buf inp1 inp2 stp st y = do
138138 (buf1, r, inp11, inp21) <- consume buf inp1 inp2 stp st y
139139 case r of
140− Yield 0 s ->
140+ Partial 0 s ->
141141 let state = ([], StepState s, inp11, inp21)
142142 in return (state, Yld 0)
143− Yield n s ->
143+ Partial n s ->
144144 let src0 = Prelude.take n buf1
145145 src = Prelude.reverse src0
146146 state = ([], StepState s, src ++ inp11, inp21)
147147 in assert (n <= length buf1) (return (state, Yld n))
148− Stop n b ->
148+ Done n b ->
149149 let state = (Prelude.take n buf1, StepResult b, inp11, inp21)
150150 in assert (n <= length buf1) (return (state, Stp n))
151− -- Skip 0 s -> (buf1, Right s, inp11, inp21)
152− Skip n s ->
151+ -- Continue 0 s -> (buf1, Right s, inp11, inp21)
152+ Continue n s ->
153153 let (src0, buf2) = splitAt n buf1
154154 src = Prelude.reverse src0
155155 state = (buf2, StepState s, src ++ inp11, inp21)
@@ -163,18 +163,18 @@ teeWith zf (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
163163 (r,stR) <- useStream bufR inpR1 inpR2 stepR sR x
164164 let next = TeePair l r
165165 return $ case (stL,stR) of
166− (Yld n1, Yld n2) -> Yield (min n1 n2) next
167− (Yld n1, Stp n2) -> Yield (min n1 n2) next
168− (Stp n1, Yld n2) -> Yield (min n1 n2) next
166+ (Yld n1, Yld n2) -> Partial (min n1 n2) next
167+ (Yld n1, Stp n2) -> Partial (min n1 n2) next
168+ (Stp n1, Yld n2) -> Partial (min n1 n2) next
169169 (Stp n1, Stp n2) ->
170170 -- Uni-pattern match results in better optimized code compared
171171 -- to a case match.
172172 let (_, StepResult rL, _, _) = l
173173 (_, StepResult rR, _, _) = r
174− in Stop (min n1 n2) (zf rL rR)
174+ in Done (min n1 n2) (zf rL rR)
175175 (Err err, _) -> Error err
176176 (_, Err err) -> Error err
177− _ -> Skip 0 next
177+ _ -> Continue 0 next
178178
179179 step (TeePair (bufL, StepState sL, inpL1, inpL2)
180180 r@(_, StepResult rR, _, _)) x = do
@@ -185,11 +185,11 @@ teeWith zf (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
185185 -- to fix the other case. We need to keep incrementing the unused count
186186 -- of the stopped stream and take the min of the two.
187187 return $ case stL of
188− Yld n -> Yield n next
188+ Yld n -> Partial n next
189189 Stp n ->
190190 let (_, StepResult rL, _, _) = l
191− in Stop n (zf rL rR)
192− Skp -> Skip 0 next
191+ in Done n (zf rL rR)
192+ Skp -> Continue 0 next
193193 Err err -> Error err
194194
195195 step (TeePair l@(_, StepResult rL, _, _)
@@ -201,11 +201,11 @@ teeWith zf (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
201201 -- to fix the other case. We need to keep incrementing the unused count
202202 -- of the stopped stream and take the min of the two.
203203 return $ case stR of
204− Yld n -> Yield n next
204+ Yld n -> Partial n next
205205 Stp n ->
206206 let (_, StepResult rR, _, _) = r
207− in Stop n (zf rL rR)
208− Skp -> Skip 0 next
207+ in Done n (zf rL rR)
208+ Skp -> Continue 0 next
209209 Err err -> Error err
210210
211211 step _ _ = undefined
@@ -261,15 +261,15 @@ teeWithFst zf (Parser stepL initialL extractL)
261261 useStream buf inp1 inp2 stp st y = do
262262 (buf1, r, inp11, inp21) <- consume buf inp1 inp2 stp st y
263263 case r of
264− Yield 0 s ->
264+ Partial 0 s ->
265265 let state = ([], StepState s, inp11, inp21)
266266 in return (state, Yld 0)
267− Yield n _ -> return (undefined, Yld n) -- Not implemented
268− Stop n b ->
267+ Partial n _ -> return (undefined, Yld n) -- Not implemented
268+ Done n b ->
269269 let state = (Prelude.take n buf1, StepResult b, inp11, inp21)
270270 in assert (n <= length buf1) (return (state, Stp n))
271− -- Skip 0 s -> (buf1, Right s, inp11, inp21)
272− Skip n s ->
271+ -- Continue 0 s -> (buf1, Right s, inp11, inp21)
272+ Continue n s ->
273273 let (src0, buf2) = splitAt n buf1
274274 src = Prelude.reverse src0
275275 state = (buf2, StepState s, src ++ inp11, inp21)
@@ -293,18 +293,18 @@ teeWithFst zf (Parser stepL initialL extractL)
293293 -- to a case match.
294294 let (_, StepResult rL, _, _) = l
295295 (_, StepResult rR, _, _) = r
296− in return $ Stop n1 (zf rL rR)
296+ in return $ Done n1 (zf rL rR)
297297 (Stp n1, Yld _) ->
298298 let (_, StepResult rL, _, _) = l
299299 (_, StepState ssR, _, _) = r
300300 in do
301301 rR <- extractR ssR
302− return $ Stop n1 (zf rL rR)
303− (Yld n1, Yld n2) -> return $ Yield (min n1 n2) next
304− (Yld n1, Stp n2) -> return $ Yield (min n1 n2) next
302+ return $ Done n1 (zf rL rR)
303+ (Yld n1, Yld n2) -> return $ Partial (min n1 n2) next
304+ (Yld n1, Stp n2) -> return $ Partial (min n1 n2) next
305305 (Err err, _) -> return $ Error err
306306 (_, Err err) -> return $ Error err
307− _ -> return $ Skip 0 next
307+ _ -> return $ Continue 0 next
308308
309309 step (TeePair (bufL, StepState sL, inpL1, inpL2)
310310 r@(_, StepResult rR, _, _)) x = do
@@ -315,11 +315,11 @@ teeWithFst zf (Parser stepL initialL extractL)
315315 -- to fix the other case. We need to keep incrementing the unused count
316316 -- of the stopped stream and take the min of the two.
317317 return $ case stL of
318− Yld n -> Yield n next
318+ Yld n -> Partial n next
319319 Stp n ->
320320 let (_, StepResult rL, _, _) = l
321− in Stop n (zf rL rR)
322− Skp -> Skip 0 next
321+ in Done n (zf rL rR)
322+ Skp -> Continue 0 next
323323 Err err -> Error err
324324
325325 step _ _ = undefined
@@ -383,15 +383,15 @@ shortest (Parser stepL initialL extractL) (Parser stepR initialR _) =
383383 useStream buf inp1 inp2 stp st y = do
384384 (buf1, r, inp11, inp21) <- consume buf inp1 inp2 stp st y
385385 case r of
386− Yield 0 s ->
386+ Partial 0 s ->
387387 let state = ([], StepState s, inp11, inp21)
388388 in return (state, Yld 0)
389− Yield n _ -> return (undefined, Yld n) -- Not implemented
390− Stop n b ->
389+ Partial n _ -> return (undefined, Yld n) -- Not implemented
390+ Done n b ->
391391 let state = (Prelude.take n buf1, StepResult b, inp11, inp21)
392392 in assert (n <= length buf1) (return (state, Stp n))
393− -- Skip 0 s -> (buf1, Right s, inp11, inp21)
394− Skip n s ->
393+ -- Continue 0 s -> (buf1, Right s, inp11, inp21)
394+ Continue n s ->
395395 let (src0, buf2) = splitAt n buf1
396396 src = Prelude.reverse src0
397397 state = (buf2, StepState s, src ++ inp11, inp21)
@@ -410,14 +410,14 @@ shortest (Parser stepL initialL extractL) (Parser stepR initialR _) =
410410 return $ case (stL,stR) of
411411 (Stp n1, _) ->
412412 let (_, StepResult rL, _, _) = l
413− in Stop n1 rL
413+ in Done n1 rL
414414 (_, Stp n2) ->
415415 let (_, StepResult rR, _, _) = r
416− in Stop n2 rR
417− (Yld n1, Yld n2) -> Yield (min n1 n2) next
416+ in Done n2 rR
417+ (Yld n1, Yld n2) -> Partial (min n1 n2) next
418418 (Err err, _) -> Error err
419419 (_, Err err) -> Error err
420− _ -> Skip 0 next
420+ _ -> Continue 0 next
421421
422422 step _ _ = undefined
423423
@@ -460,15 +460,15 @@ longest (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
460460 useStream buf inp1 inp2 stp st y = do
461461 (buf1, r, inp11, inp21) <- consume buf inp1 inp2 stp st y
462462 case r of
463− Yield 0 s ->
463+ Partial 0 s ->
464464 let state = ([], StepState s, inp11, inp21)
465465 in return (state, Yld 0)
466− Yield n _ -> return (undefined, Yld n) -- Not implemented
467− Stop n b ->
466+ Partial n _ -> return (undefined, Yld n) -- Not implemented
467+ Done n b ->
468468 let state = (Prelude.take n buf1, StepResult b, inp11, inp21)
469469 in assert (n <= length buf1) (return (state, Stp n))
470− -- Skip 0 s -> (buf1, Right s, inp11, inp21)
471− Skip n s ->
470+ -- Continue 0 s -> (buf1, Right s, inp11, inp21)
471+ Continue n s ->
472472 let (src0, buf2) = splitAt n buf1
473473 src = Prelude.reverse src0
474474 state = (buf2, StepState s, src ++ inp11, inp21)
@@ -482,16 +482,16 @@ longest (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
482482 (r,stR) <- useStream bufR inpR1 inpR2 stepR sR x
483483 let next = TeePair l r
484484 return $ case (stL,stR) of
485− (Yld n1, Yld n2) -> Yield (min n1 n2) next
486− (Yld n1, Stp n2) -> Yield (min n1 n2) next
487− (Stp n1, Yld n2) -> Yield (min n1 n2) next
485+ (Yld n1, Yld n2) -> Partial (min n1 n2) next
486+ (Yld n1, Stp n2) -> Partial (min n1 n2) next
487+ (Stp n1, Yld n2) -> Partial (min n1 n2) next
488488 (Stp n1, Stp n2) ->
489489 let (_, StepResult rL, _, _) = l
490490 (_, StepResult rR, _, _) = r
491− in Stop (max n1 n2) (if n1 >= n2 then rL else rR)
491+ in Done (max n1 n2) (if n1 >= n2 then rL else rR)
492492 (Err err, _) -> Error err
493493 (_, Err err) -> Error err
494− _ -> Skip 0 next
494+ _ -> Continue 0 next
495495
496496 -- XXX the parser that finishes last may not be the longest because it may
497497 -- return a lot of unused input which makes it shorter. Our current
@@ -506,11 +506,11 @@ longest (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
506506 (l,stL) <- useStream bufL inpL1 inpL2 stepL sL x
507507 let next = TeePair l r
508508 return $ case stL of
509− Yld n -> Yield n next
509+ Yld n -> Partial n next
510510 Stp n ->
511511 let (_, StepResult rL, _, _) = l
512− in Stop n rL
513− Skp -> Skip 0 next
512+ in Done n rL
513+ Skp -> Continue 0 next
514514 Err err -> Error err
515515
516516 step (TeePair l@(_, StepResult _, _, _)
@@ -518,11 +518,11 @@ longest (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
518518 (r, stR) <- useStream bufR inpR1 inpR2 stepR sR x
519519 let next = TeePair l r
520520 return $ case stR of
521− Yld n -> Yield n next
521+ Yld n -> Partial n next
522522 Stp n ->
523523 let (_, StepResult rR, _, _) = r
524− in Stop n rR
525− Skp -> Skip 0 next
524+ in Done n rR
525+ Skp -> Continue 0 next
526526 Err err -> Error err
527527
528528 step _ _ = undefined
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+112−100
@@ -40,11 +40,11 @@
4040-- 'Step' functor provides the following commands to the fold driver
4141-- corresponding to the use cases outlined in the previous para:
4242--
43−-- 1. 'Skip': buffer the current input and go back to a previous
43+-- 1. 'Continue': buffer the current input and go back to a previous
4444-- position in the stream
45−-- 2. 'Yield': buffer the current input and go back to a previous
45+-- 2. 'Partial': buffer the current input and go back to a previous
4646-- position in the stream, drop the buffer before that position.
47−-- 3. 'Stop': parser succeeded, returns how much input was leftover
47+-- 3. 'Done': parser succeeded, returns how much input was leftover
4848-- 4. 'Error': indicates that the parser has failed without a result
4949--
5050-- = How a Parser Works?
@@ -55,19 +55,19 @@
5555--
5656-- The parser may build a new output value from multiple input items. When it
5757-- consumes an input item but needs more input to build a complete output item
58−-- it uses @Skip 0 s@, yielding the intermediate state @s@ and asking the
58+-- it uses @Continue 0 s@, yielding the intermediate state @s@ and asking the
5959-- driver to provide more input. When the parser determines that a new output
60−-- value is complete it can use a @Stop n b@ to terminate the parser with @n@
60+-- value is complete it can use a @Done n b@ to terminate the parser with @n@
6161-- items of input unused and the final value of the accumulator returned as
6262-- @b@. If at any time the parser determines that the parse has failed it can
6363-- return @Error err@.
6464--
65−-- A parser building a collection of values (e.g. a list) can use the @Yield@
65+-- A parser building a collection of values (e.g. a list) can use the @Partial@
6666-- constructor whenever a new item in the output collection is generated. If a
6767-- parser building a collection of values has yielded at least one value then
6868-- it considered successful and cannot fail after that. In the current
6969-- implementation, this is not automatically enforced, there is a rule that the
70−-- parser MUST use only @Stop@ for termination after the first @Yield@, it
70+-- parser MUST use only @Done@ for termination after the first @Partial@, it
7171-- cannot use @Error@. It may be possible to change the implementation so that
7272-- this rule is not required, but there may be some performance cost to it.
7373--
@@ -82,7 +82,7 @@
8282-- = Error Handling
8383--
8484-- When a parser's @step@ function is invoked it may terminate by either a
85−-- 'Stop' or an 'Error' return value. In an 'Alternative' composition an error
85+-- 'Done' or an 'Error' return value. In an 'Alternative' composition an error
8686-- return can make the composed parser backtrack and try another parser.
8787--
8888-- If the stream stops before a parser could terminate then we use the
@@ -145,58 +145,70 @@ import Streamly.Internal.Data.Strict (Tuple3'(..))
145145
146146-- | The return type of a 'Parser' step.
147147--
148−-- A parser is driven by a parse driver one step at a time, at any time the
149−-- driver may @extract@ the result of the parser. The parser may ask the driver
150−-- to backtrack at any point, therefore, the driver holds the input up to a
151−-- point of no return in a backtracking buffer. The buffer grows or shrinks
152−-- based on the return values of the parser step execution.
153−--
154−-- When a parser step is executed it generates a new intermediate state of the
155−-- parse result along with a command to the driver. The command tells the
156−-- driver whether to keep the input stream for a potential backtracking later
157−-- on or drop it, and how much to keep. The constructors of 'Step' represent
158−-- the commands to the driver.
148+-- The parse operation feeds the input stream to the parser one element at a
149+-- time, representing a parse 'Step'. The parser may or may not consume the
150+-- item and returns a result. If the result is 'Partial' we can either extract
151+-- the result or feed more input to the parser. If the result is 'Continue', we
152+-- must feed more input in order to get a result. If the parser returns 'Done'
153+-- then the parser can no longer take any more input.
154+--
155+-- If the result is 'Continue', the parse operation retains the input in a
156+-- backtracking buffer, in case the parser may ask to backtrack in future.
157+-- Whenever a 'Partial n' result is returned we first backtrack by @n@ elements
158+-- in the input and then release any remaining backtracking buffer. Similarly,
159+-- 'Continue n' backtracks to @n@ elements before the current position and
160+-- starts feeding the input from that point for future invocations of the
161+-- parser.
162+--
163+-- If parser is not yet done, we can use the @extract@ operation on the @state@
164+-- of the parser to extract a result. If the parser has not yet yielded a
165+-- result, the operation fails with a 'ParseError' exception. If the parser
166+-- yielded a 'Partial' result in the past the last partial result is returned.
167+-- Therefore, if a parser yields a partial result once it cannot fail later on.
168+--
169+-- The parser can never backtrack beyond the position where the last partial
170+-- result left it at. The parser must ensure that the backtrack position is
171+-- always after that.
159172--
160173-- /Internal/
161174--
162175{-# ANN type Step Fuse #-}
163176data Step s b =
164− Yield Int s
165− -- ^ Go back by @n@ items in the input buffer and drop the rest.
166− -- @Yield n state@ indicates that the parser has yielded a new result
167− -- which is a point of no return. The result can be extracted using
168− -- @extract@. The driver moves back the current position of the cursor by
169− -- @n@ elements and drops any buffer before that. The rule is that if a
170− -- parser has yielded at least once it cannot return a failure result.
171−
172− -- only rewind
173− | Skip Int s
174− -- ^ Go back by @n@ items in the input buffer.
175− -- @Skip n state@ indicates that the parser has consumed the current input
176− -- but no new result has been generated. A new @state@ is generated.
177− -- However, if we use @extract@ on @state@ it will generate a result from
178− -- the previous @Yield@. When @n@ is non-zero it is a backward offset from
179− -- the current position in the stream from which the driver will replay the
180− -- input to the parser. The offset cannot be beyond the latest point of no
181− -- return created by @Yield@.
182−
183− | Stop Int b
184− -- ^ Trim the backtracking buffer keeping only most recent @n@ items.
185− -- @Stop n result@ asks the driver to stop driving the parser because it
186− -- has reached a fixed point and further input will not change the result.
187− -- @n@ is the count of unused input elements which includes the element on
188− -- which 'Stop' occurred.
177+ Partial Int s
178+ -- ^ Partial result with an optional backtrack.
179+ --
180+ -- @Partial count state@ means a partial result is available which
181+ -- can be extracted successfully, @state@ is the opaque state of the
182+ -- parser to be supplied to the next invocation of the step operation.
183+ -- The current input position is reset to @count@ elements back and any
184+ -- input before that is dropped from the backtrack buffer.
185+
186+ | Continue Int s
187+ -- ^ Need more input with an optional backtrack.
188+ --
189+ -- @Continue count state@ means the parser has consumed the current input
190+ -- but no new result is generated, @state@ is the next state of the parser.
191+ -- The current input is retained in the backtrack buffer and the input
192+ -- position is reset to @count@ elements back.
193+
194+ | Done Int b
195+ -- ^ Done with leftover input count and result.
196+ --
197+ -- @Done count result@ means the parser has finished, it will accept no
198+ -- more input, last @count@ elements from the input are unused and the
199+ -- result of the parser is in @result@.
189200
190201 | Error String
191− -- ^ Go back to the start of the backtracking buffer.
192− -- An error makes the parser backtrack to the last checkpoint and try
193− -- another alternative.
202+ -- ^ Parser failed without generating any output.
203+ --
204+ -- The parsing operation may backtrack to the beginning and try another
205+ -- alternative.
194206
195207instance Functor (Step s) where
196208 {-# INLINE fmap #-}
197− fmap _ (Yield n s) = Yield n s
198− fmap _ (Skip n s) = Skip n s
199− fmap f (Stop n b) = Stop n (f b)
209+ fmap _ (Partial n s) = Partial n s
210+ fmap _ (Continue n s) = Continue n s
211+ fmap f (Done n b) = Done n (f b)
200212 fmap _ (Error err) = Error err
201213
202214-- | A parser is a fold that can fail and is represented as @Parser step
@@ -243,7 +255,7 @@ instance Functor m => Functor (Parser m a) where
243255--
244256{-# INLINE_NORMAL yield #-}
245257yield :: Monad m => b -> Parser m a b
246−yield b = Parser (\_ _ -> pure $ Stop 1 b) -- step
258+yield b = Parser (\_ _ -> pure $ Done 1 b) -- step
247259 (pure ()) -- initial
248260 (\_ -> pure b) -- extract
249261
@@ -253,7 +265,7 @@ yield b = Parser (\_ _ -> pure $ Stop 1 b) -- step
253265--
254266{-# INLINE yieldM #-}
255267yieldM :: Monad m => m b -> Parser m a b
256−yieldM b = Parser (\_ _ -> Stop 1 <$> b) -- step
268+yieldM b = Parser (\_ _ -> Done 1 <$> b) -- step
257269 (pure ()) -- initial
258270 (\_ -> b) -- extract
259271
@@ -287,7 +299,7 @@ splitWith func (Parser stepL initialL extractL)
287299 initial = SeqParseL <$> initialL
288300
289301 -- Note: For the composed parse to terminate, the left parser has to be
290− -- a terminating parser returning a Stop at some point.
302+ -- a terminating parser returning a Done at some point.
291303 step (SeqParseL st) a = do
292304 r <- stepL st a
293305 case r of
@@ -295,17 +307,17 @@ splitWith func (Parser stepL initialL extractL)
295307 -- e.g. in ((,) <$> p1 <*> p2) <|> p3, if p2 fails we have to
296308 -- backtrack and start running p3. So we need to keep the input
297309 -- buffered until we know that the applicative cannot fail.
298− Yield n s -> return $ Skip n (SeqParseL s)
299− Skip n s -> return $ Skip n (SeqParseL s)
300− Stop n b -> Skip n <$> (SeqParseR (func b) <$> initialR)
310+ Partial n s -> return $ Continue n (SeqParseL s)
311+ Continue n s -> return $ Continue n (SeqParseL s)
312+ Done n b -> Continue n <$> (SeqParseR (func b) <$> initialR)
301313 Error err -> return $ Error err
302314
303315 step (SeqParseR f st) a = do
304316 r <- stepR st a
305317 return $ case r of
306− Yield n s -> Yield n (SeqParseR f s)
307− Skip n s -> Skip n (SeqParseR f s)
308− Stop n b -> Stop n (f b)
318+ Partial n s -> Partial n (SeqParseR f s)
319+ Continue n s -> Continue n (SeqParseR f s)
320+ Done n b -> Done n (f b)
309321 Error err -> Error err
310322
311323 extract (SeqParseR f sR) = fmap f (extractR sR)
@@ -333,22 +345,22 @@ split_ (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
333345 initial = SeqAL <$> initialL
334346
335347 -- Note: For the composed parse to terminate, the left parser has to be
336− -- a terminating parser returning a Stop at some point.
348+ -- a terminating parser returning a Done at some point.
337349 step (SeqAL st) a = do
338350 (\r i -> case r of
339351 -- Note: this leads to buffering even if we are not in an
340352 -- Alternative composition.
341− Yield n s -> Skip n (SeqAL s)
342− Skip n s -> Skip n (SeqAL s)
343− Stop n _ -> Skip n (SeqAR i)
353+ Partial n s -> Continue n (SeqAL s)
354+ Continue n s -> Continue n (SeqAL s)
355+ Done n _ -> Continue n (SeqAR i)
344356 -- XXX should we sequence initialR monadically?
345357 Error err -> Error err) <$> stepL st a <*> initialR
346358
347359 step (SeqAR st) a = do
348360 (\r -> case r of
349− Yield n s -> Yield n (SeqAR s)
350− Skip n s -> Skip n (SeqAR s)
351− Stop n b -> Stop n b
361+ Partial n s -> Partial n (SeqAR s)
362+ Continue n s -> Continue n (SeqAR s)
363+ Done n b -> Done n b
352364 Error err -> Error err) <$> stepR st a
353365
354366 extract (SeqAR sR) = extractR sR
@@ -400,21 +412,21 @@ alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
400412 step (AltParseL cnt st) a = do
401413 r <- stepL st a
402414 case r of
403− Yield n s -> return $ Yield n (AltParseL 0 s)
404− Skip n s -> do
415+ Partial n s -> return $ Partial n (AltParseL 0 s)
416+ Continue n s -> do
405417 assert (cnt + 1 - n >= 0) (return ())
406− return $ Skip n (AltParseL (cnt + 1 - n) s)
407− Stop n b -> return $ Stop n b
418+ return $ Continue n (AltParseL (cnt + 1 - n) s)
419+ Done n b -> return $ Done n b
408420 Error _ -> do
409421 rR <- initialR
410− return $ Skip (cnt + 1) (AltParseR rR)
422+ return $ Continue (cnt + 1) (AltParseR rR)
411423
412424 step (AltParseR st) a = do
413425 r <- stepR st a
414426 return $ case r of
415− Yield n s -> Yield n (AltParseR s)
416− Skip n s -> Skip n (AltParseR s)
417− Stop n b -> Stop n b
427+ Partial n s -> Partial n (AltParseR s)
428+ Continue n s -> Continue n (AltParseR s)
429+ Done n b -> Done n b
418430 Error err -> Error err
419431
420432 extract (AltParseR sR) = extractR sR
@@ -441,19 +453,19 @@ splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
441453 r <- step1 st a
442454 let cnt1 = cnt + 1
443455 case r of
444− Yield n s -> do
456+ Partial n s -> do
445457 assert (cnt1 - n >= 0) (return ())
446− return $ Skip n (Tuple3' s (cnt1 - n) fs)
447− Skip n s -> do
458+ return $ Continue n (Tuple3' s (cnt1 - n) fs)
459+ Continue n s -> do
448460 assert (cnt1 - n >= 0) (return ())
449− return $ Skip n (Tuple3' s (cnt1 - n) fs)
450− Stop n b -> do
461+ return $ Continue n (Tuple3' s (cnt1 - n) fs)
462+ Done n b -> do
451463 s <- initial1
452464 fs1 <- fstep fs b
453− return $ Yield n (Tuple3' s 0 fs1)
465+ return $ Partial n (Tuple3' s 0 fs1)
454466 Error _ -> do
455467 xs <- fextract fs
456− return $ Stop cnt1 xs
468+ return $ Done cnt1 xs
457469
458470 -- XXX The "try" may impact performance if this parser is used as a scan
459471 extract (Tuple3' s _ fs) = do
@@ -482,28 +494,28 @@ splitSome (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
482494 step (Tuple3' st _ (Left fs)) a = do
483495 r <- step1 st a
484496 case r of
485− Yield n s -> return $ Skip n (Tuple3' s undefined (Left fs))
486− Skip n s -> return $ Skip n (Tuple3' s undefined (Left fs))
487− Stop n b -> do
497+ Partial n s -> return $ Continue n (Tuple3' s undefined (Left fs))
498+ Continue n s -> return $ Continue n (Tuple3' s undefined (Left fs))
499+ Done n b -> do
488500 s <- initial1
489501 fs1 <- fstep fs b
490− return $ Yield n (Tuple3' s 0 (Right fs1))
502+ return $ Partial n (Tuple3' s 0 (Right fs1))
491503 Error err -> return $ Error err
492504 step (Tuple3' st cnt (Right fs)) a = do
493505 r <- step1 st a
494506 let cnt1 = cnt + 1
495507 case r of
496− Yield n s -> do
508+ Partial n s -> do
497509 assert (cnt1 - n >= 0) (return ())
498− return $ Yield n (Tuple3' s (cnt1 - n) (Right fs))
499− Skip n s -> do
510+ return $ Partial n (Tuple3' s (cnt1 - n) (Right fs))
511+ Continue n s -> do
500512 assert (cnt1 - n >= 0) (return ())
501− return $ Skip n (Tuple3' s (cnt1 - n) (Right fs))
502− Stop n b -> do
513+ return $ Continue n (Tuple3' s (cnt1 - n) (Right fs))
514+ Done n b -> do
503515 s <- initial1
504516 fs1 <- fstep fs b
505− return $ Yield n (Tuple3' s 0 (Right fs1))
506− Error _ -> Stop cnt1 <$> fextract fs
517+ return $ Partial n (Tuple3' s 0 (Right fs1))
518+ Error _ -> Done cnt1 <$> fextract fs
507519
508520 -- XXX The "try" may impact performance if this parser is used as a scan
509521 extract (Tuple3' s _ (Left fs)) = extract1 s >>= fstep fs >>= fextract
@@ -581,20 +593,20 @@ concatMap func (Parser stepL initialL extractL) = Parser step initial extract
581593 step (ConcatParseL st) a = do
582594 r <- stepL st a
583595 return $ case r of
584− Yield n s -> Skip n (ConcatParseL s)
585− Skip n s -> Skip n (ConcatParseL s)
586− Stop n b -> Skip n (ConcatParseR (func b))
596+ Partial n s -> Continue n (ConcatParseL s)
597+ Continue n s -> Continue n (ConcatParseL s)
598+ Done n b -> Continue n (ConcatParseR (func b))
587599 Error err -> Error err
588600
589601 step (ConcatParseR (Parser stepR initialR extractR)) a = do
590602 st <- initialR
591603 r <- stepR st a
592604 return $ case r of
593− Yield n s ->
594− Yield n (ConcatParseR (Parser stepR (return s) extractR))
595− Skip n s ->
596− Skip n (ConcatParseR (Parser stepR (return s) extractR))
597− Stop n b -> Stop n b
605+ Partial n s ->
606+ Partial n (ConcatParseR (Parser stepR (return s) extractR))
607+ Continue n s ->
608+ Continue n (ConcatParseR (Parser stepR (return s) extractR))
609+ Done n b -> Done n b
598610 Error err -> Error err
599611
600612 extract (ConcatParseR (Parser _ initialR extractR)) =
src/Streamly/Internal/Data/Stream/StreamD.hs+30−30
@@ -998,19 +998,19 @@ parselMx' pstep initial extract (Stream step state) = do
998998 Yield x s -> do
999999 pRes <- pstep pst x
10001000 case pRes of
1001− PR.Yield 0 pst1 -> go SPEC s [] pst1
1002− PR.Yield n pst1 -> do
1001+ PR.Partial 0 pst1 -> go SPEC s [] pst1
1002+ PR.Partial n pst1 -> do
10031003 assert (n <= length (x:buf)) (return ())
10041004 let src0 = Prelude.take n (x:buf)
10051005 src = Prelude.reverse src0
10061006 gobuf SPEC s [] src pst1
1007− PR.Skip 0 pst1 -> go SPEC s (x:buf) pst1
1008− PR.Skip n pst1 -> do
1007+ PR.Continue 0 pst1 -> go SPEC s (x:buf) pst1
1008+ PR.Continue n pst1 -> do
10091009 assert (n <= length (x:buf)) (return ())
10101010 let (src0, buf1) = splitAt n (x:buf)
10111011 src = Prelude.reverse src0
10121012 gobuf SPEC s buf1 src pst1
1013− PR.Stop _ b -> return b
1013+ PR.Done _ b -> return b
10141014 PR.Error err -> throwM $ ParseError err
10151015 Skip s -> go SPEC s buf pst
10161016 Stop -> extract pst
@@ -1019,20 +1019,20 @@ parselMx' pstep initial extract (Stream step state) = do
10191019 gobuf !_ s buf (x:xs) !pst = do
10201020 pRes <- pstep pst x
10211021 case pRes of
1022− PR.Yield 0 pst1 ->
1022+ PR.Partial 0 pst1 ->
10231023 gobuf SPEC s [] xs pst1
1024− PR.Yield n pst1 -> do
1024+ PR.Partial n pst1 -> do
10251025 assert (n <= length (x:buf)) (return ())
10261026 let src0 = Prelude.take n (x:buf)
10271027 src = Prelude.reverse src0 ++ xs
10281028 gobuf SPEC s [] src pst1
1029− PR.Skip 0 pst1 -> gobuf SPEC s (x:buf) xs pst1
1030− PR.Skip n pst1 -> do
1029+ PR.Continue 0 pst1 -> gobuf SPEC s (x:buf) xs pst1
1030+ PR.Continue n pst1 -> do
10311031 assert (n <= length (x:buf)) (return ())
10321032 let (src0, buf1) = splitAt n (x:buf)
10331033 src = Prelude.reverse src0 ++ xs
10341034 gobuf SPEC s buf1 src pst1
1035− PR.Stop _ b -> return b
1035+ PR.Done _ b -> return b
10361036 PR.Error err -> throwM $ ParseError err
10371037
10381038------------------------------------------------------------------------------
@@ -1077,22 +1077,22 @@ parseMany (PRD.Parser pstep initial extract) (Stream step state) =
10771077 Yield x s -> do
10781078 pRes <- pstep pst x
10791079 case pRes of
1080− PR.Yield 0 pst1 ->
1080+ PR.Partial 0 pst1 ->
10811081 return $ Skip $ ParseChunksStream s [] pst1
1082− PR.Yield n pst1 -> do
1082+ PR.Partial n pst1 -> do
10831083 assert (n <= length (x:buf)) (return ())
10841084 let src0 = Prelude.take n (x:buf)
10851085 src = Prelude.reverse src0
10861086 return $ Skip $ ParseChunksBuf src s [] pst1
1087− -- PR.Skip 0 pst1 ->
1087+ -- PR.Continue 0 pst1 ->
10881088 -- return $ Skip $ ParseChunksStream s (x:buf) pst1
1089− PR.Skip n pst1 -> do
1089+ PR.Continue n pst1 -> do
10901090 assert (n <= length (x:buf)) (return ())
10911091 let (src0, buf1) = splitAt n (x:buf)
10921092 src = Prelude.reverse src0
10931093 return $ Skip $ ParseChunksBuf src s buf1 pst1
10941094 -- XXX Specialize for Stop 0 common case?
1095− PR.Stop n b -> do
1095+ PR.Done n b -> do
10961096 assert (n <= length (x:buf)) (return ())
10971097 let src = Prelude.reverse (Prelude.take n (x:buf))
10981098 return $ Skip $
@@ -1113,21 +1113,21 @@ parseMany (PRD.Parser pstep initial extract) (Stream step state) =
11131113 stepOuter _ (ParseChunksBuf (x:xs) s buf pst) = do
11141114 pRes <- pstep pst x
11151115 case pRes of
1116− PR.Yield 0 pst1 ->
1116+ PR.Partial 0 pst1 ->
11171117 return $ Skip $ ParseChunksBuf xs s [] pst1
1118− PR.Yield n pst1 -> do
1118+ PR.Partial n pst1 -> do
11191119 assert (n <= length (x:buf)) (return ())
11201120 let src0 = Prelude.take n (x:buf)
11211121 src = Prelude.reverse src0 ++ xs
11221122 return $ Skip $ ParseChunksBuf src s [] pst1
1123− -- PR.Skip 0 pst1 -> return $ Skip $ ParseChunksBuf xs s (x:buf) pst1
1124− PR.Skip n pst1 -> do
1123+ -- PR.Continue 0 pst1 -> return $ Skip $ ParseChunksBuf xs s (x:buf) pst1
1124+ PR.Continue n pst1 -> do
11251125 assert (n <= length (x:buf)) (return ())
11261126 let (src0, buf1) = splitAt n (x:buf)
11271127 src = Prelude.reverse src0 ++ xs
11281128 return $ Skip $ ParseChunksBuf src s buf1 pst1
11291129 -- XXX Specialize for Stop 0 common case?
1130− PR.Stop n b -> do
1130+ PR.Done n b -> do
11311131 assert (n <= length (x:buf)) (return ())
11321132 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
11331133 return $ Skip $ ParseChunksYield b (ParseChunksInit src s)
@@ -1179,25 +1179,25 @@ parseIterate func seed (Stream step state) =
11791179 pst <- initial
11801180 pRes <- pstep pst x
11811181 case pRes of
1182− PR.Yield 0 pst1 ->
1182+ PR.Partial 0 pst1 ->
11831183 return $ Skip $ ConcatParseStream s []
11841184 (PRD.Parser pstep (return pst1) extract)
1185− PR.Yield n pst1 -> do
1185+ PR.Partial n pst1 -> do
11861186 assert (n <= length (x:buf)) (return ())
11871187 let src0 = Prelude.take n (x:buf)
11881188 src = Prelude.reverse src0
11891189 return $ Skip $ ConcatParseBuf src s []
11901190 (PRD.Parser pstep (return pst1) extract)
1191− -- PR.Skip 0 pst1 ->
1191+ -- PR.Continue 0 pst1 ->
11921192 -- return $ Skip $ ConcatParseStream s (x:buf) pst1
1193− PR.Skip n pst1 -> do
1193+ PR.Continue n pst1 -> do
11941194 assert (n <= length (x:buf)) (return ())
11951195 let (src0, buf1) = splitAt n (x:buf)
11961196 src = Prelude.reverse src0
11971197 return $ Skip $ ConcatParseBuf src s buf1
11981198 (PRD.Parser pstep (return pst1) extract)
11991199 -- XXX Specialize for Stop 0 common case?
1200− PR.Stop n b -> do
1200+ PR.Done n b -> do
12011201 assert (n <= length (x:buf)) (return ())
12021202 let src = Prelude.reverse (Prelude.take n (x:buf))
12031203 return $ Skip $
@@ -1221,24 +1221,24 @@ parseIterate func seed (Stream step state) =
12211221 pst <- initial
12221222 pRes <- pstep pst x
12231223 case pRes of
1224− PR.Yield 0 pst1 ->
1224+ PR.Partial 0 pst1 ->
12251225 return $ Skip $ ConcatParseBuf xs s []
12261226 (PRD.Parser pstep (return pst1) extract)
1227− PR.Yield n pst1 -> do
1227+ PR.Partial n pst1 -> do
12281228 assert (n <= length (x:buf)) (return ())
12291229 let src0 = Prelude.take n (x:buf)
12301230 src = Prelude.reverse src0 ++ xs
12311231 return $ Skip $ ConcatParseBuf src s []
12321232 (PRD.Parser pstep (return pst1) extract)
1233− -- PR.Skip 0 pst1 -> return $ Skip $ ConcatParseBuf xs s (x:buf) pst1
1234− PR.Skip n pst1 -> do
1233+ -- PR.Continue 0 pst1 -> return $ Skip $ ConcatParseBuf xs s (x:buf) pst1
1234+ PR.Continue n pst1 -> do
12351235 assert (n <= length (x:buf)) (return ())
12361236 let (src0, buf1) = splitAt n (x:buf)
12371237 src = Prelude.reverse src0 ++ xs
12381238 return $ Skip $ ConcatParseBuf src s buf1
12391239 (PRD.Parser pstep (return pst1) extract)
12401240 -- XXX Specialize for Stop 0 common case?
1241− PR.Stop n b -> do
1241+ PR.Done n b -> do
12421242 assert (n <= length (x:buf)) (return ())
12431243 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
12441244 return $ Skip $ ConcatParseYield b
src/Streamly/Internal/Data/Zipper.hs+20−20
@@ -221,19 +221,19 @@ parse pstep initial extract (Zipper [] ls rs stream) =
221221 yieldk x r = do
222222 acc1 <- acc >>= \b -> pstep b x
223223 case acc1 of
224− PR.Yield 0 pst1 -> go r [] (return pst1)
225− PR.Yield n pst1 -> do
224+ PR.Partial 0 pst1 -> go r [] (return pst1)
225+ PR.Partial n pst1 -> do
226226 assert (n <= length (x:buf)) (return ())
227227 let src0 = Prelude.take n (x:buf)
228228 src = Prelude.reverse src0
229229 gobuf r [] src (return pst1)
230− PR.Skip 0 pst1 -> go r (x:buf) (return pst1)
231− PR.Skip n pst1 -> do
230+ PR.Continue 0 pst1 -> go r (x:buf) (return pst1)
231+ PR.Continue n pst1 -> do
232232 assert (n <= length (x:buf)) (return ())
233233 let (src0, buf1) = splitAt n (x:buf)
234234 src = Prelude.reverse src0
235235 gobuf r buf1 src (return pst1)
236− PR.Stop n b -> do
236+ PR.Done n b -> do
237237 assert (n <= length (x:buf)) (return ())
238238 let (src0, buf1) = splitAt n (x:buf)
239239 src = Prelude.reverse src0
@@ -247,20 +247,20 @@ parse pstep initial extract (Zipper [] ls rs stream) =
247247 r <- pst
248248 pRes <- pstep r x
249249 case pRes of
250− PR.Yield 0 pst1 ->
250+ PR.Partial 0 pst1 ->
251251 gobuf s [] xs (return pst1)
252− PR.Yield n pst1 -> do
252+ PR.Partial n pst1 -> do
253253 assert (n <= length (x:buf)) (return ())
254254 let src0 = Prelude.take n (x:buf)
255255 src = Prelude.reverse src0
256256 gobuf s [] (src ++ xs) (return pst1)
257− PR.Skip 0 pst1 -> gobuf s (x:buf) xs (return pst1)
258− PR.Skip n pst1 -> do
257+ PR.Continue 0 pst1 -> gobuf s (x:buf) xs (return pst1)
258+ PR.Continue n pst1 -> do
259259 assert (n <= length (x:buf)) (return ())
260260 let (src0, buf1) = splitAt n (x:buf)
261261 src = Prelude.reverse src0 ++ xs
262262 gobuf s buf1 src (return pst1)
263− PR.Stop n b -> do
263+ PR.Done n b -> do
264264 assert (n <= length (x:buf)) (return ())
265265 let (src0, buf1) = splitAt n (x:buf)
266266 src = Prelude.reverse src0 ++ xs
@@ -291,21 +291,21 @@ parse pstep initial extract (Zipper (cp:cps) ls rs stream) =
291291 acc1 <- acc >>= \b -> pstep b x
292292 let cnt1 = cnt + 1
293293 case acc1 of
294− PR.Yield 0 pst1 ->
294+ PR.Partial 0 pst1 ->
295295 go cnt1 r [] (return pst1)
296− PR.Yield n pst1 -> do
296+ PR.Partial n pst1 -> do
297297 assert (n <= length (x:buf)) (return ())
298298 let src0 = Prelude.take n (x:buf)
299299 src = Prelude.reverse src0
300300 gobuf (cnt1 - n) r [] src (return pst1)
301− PR.Skip 0 pst1 -> go cnt1 r (x:buf) (return pst1)
302− PR.Skip n pst1 -> do
301+ PR.Continue 0 pst1 -> go cnt1 r (x:buf) (return pst1)
302+ PR.Continue n pst1 -> do
303303 assert (n <= length (x:buf)) (return ())
304304 let (src0, buf1) = splitAt n (x:buf)
305305 src = Prelude.reverse src0
306306 assert (cnt1 - n >= 0) (return ())
307307 gobuf (cnt1 - n) r buf1 src (return pst1)
308− PR.Stop n b -> do
308+ PR.Done n b -> do
309309 assert (n <= length (x:buf)) (return ())
310310 let (src0, buf1) = splitAt n (x:buf)
311311 src = Prelude.reverse src0
@@ -325,21 +325,21 @@ parse pstep initial extract (Zipper (cp:cps) ls rs stream) =
325325 pRes <- pstep r x
326326 let cnt1 = cnt + 1
327327 case pRes of
328− PR.Yield 0 pst1 ->
328+ PR.Partial 0 pst1 ->
329329 gobuf cnt1 s [] xs (return pst1)
330− PR.Yield n pst1 -> do
330+ PR.Partial n pst1 -> do
331331 assert (n <= length (x:buf)) (return ())
332332 let src0 = Prelude.take n (x:buf)
333333 src = Prelude.reverse src0
334334 gobuf (cnt1 - n) s [] (src ++ xs) (return pst1)
335− PR.Skip 0 pst1 -> gobuf cnt1 s (x:buf) xs (return pst1)
336− PR.Skip n pst1 -> do
335+ PR.Continue 0 pst1 -> gobuf cnt1 s (x:buf) xs (return pst1)
336+ PR.Continue n pst1 -> do
337337 assert (n <= length (x:buf)) (return ())
338338 let (src0, buf1) = splitAt n (x:buf)
339339 src = Prelude.reverse src0 ++ xs
340340 assert (cnt1 - n >= 0) (return ())
341341 gobuf (cnt1 - n) s buf1 src (return pst1)
342− PR.Stop n b -> do
342+ PR.Done n b -> do
343343 assert (n <= length (x:buf)) (return ())
344344 let (src0, buf1) = splitAt n (x:buf)
345345 src = Prelude.reverse src0 ++ xs