@@ -2522,7 +2522,7 @@ lookAhead (Parser step1 initial1 _) = Parser step initial extract
25222522
25232523data DeintercalateState fs sp ss =
25242524 DeintercalateL !fs !sp
2525− | DeintercalateR !fs !ss !Bool
2525+ | DeintercalateR !fs !ss
25262526
25272527-- XXX rename this to intercalate
25282528
@@ -2530,6 +2530,12 @@ data DeintercalateState fs sp ss =
25302530-- considered an interleaving of two patterns. The two parsers represent the
25312531-- two patterns.
25322532--
2533+-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
2534+-- >>> p2 = Parser.satisfy (== '+')
2535+-- >>> p = Parser.deintercalate p1 p2 Fold.toList
2536+-- >>> Stream.parse p $ Stream.fromList "1+2+3"
2537+-- Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
2538+--
25332539{-# INLINE deintercalate #-}
25342540deintercalate :: Monad m =>
25352541 Parser a m x
@@ -2553,52 +2559,43 @@ deintercalate
25532559 FL.Partial fs -> do
25542560 resL <- initialL
25552561 case resL of
2556− IPartial sL -> return $ IPartial $ DeintercalateL fs sL
2562+ IPartial sL -> return $ IPartial $ (DeintercalateL fs sL)
25572563 IDone _ -> errMsg "left" "succeed"
25582564 IError _ -> errMsg "left" "fail"
25592565 FL.Done c -> return $ IDone c
25602566
2567+ {-# INLINE process #-}
2568+ process foldAction initAction pid n nextState = do
2569+ fres <- foldAction
2570+ case fres of
2571+ FL.Partial fs1 -> do
2572+ res <- initAction
2573+ case res of
2574+ IPartial ps ->
2575+ return $ Partial n (nextState fs1 ps)
2576+ IDone _ -> errMsg pid "succeed"
2577+ IError _ -> errMsg pid "fail"
2578+ FL.Done c -> return $ Done n c
2579+
25612580 step (DeintercalateL fs sL) a = do
25622581 r <- stepL sL a
25632582 case r of
25642583 Partial n s -> return $ Partial n (DeintercalateL fs s)
25652584 Continue n s -> return $ Continue n (DeintercalateL fs s)
2566− Done n b -> do
2567− fres <- fstep fs (Left b)
2568− case fres of
2569− FL.Partial fs1 -> do
2570− resR <- initialR
2571− case resR of
2572− IPartial sR ->
2573− return
2574− $ Partial n (DeintercalateR fs1 sR False)
2575− IDone _ -> errMsg "right" "succeed"
2576− IError _ -> errMsg "right" "fail"
2577− FL.Done c -> return $ Done n c
2585+ Done n b ->
2586+ process (fstep fs (Left b)) initialR "right" n DeintercalateR
25782587 Error err -> return $ Error err
2579− step (DeintercalateR fs sR consumed) a = do
2588+ step (DeintercalateR fs sR) a = do
25802589 r <- stepR sR a
25812590 case r of
2582− Partial n s -> return $ Partial n (DeintercalateR fs s True)
2583− Continue n s -> return $ Continue n (DeintercalateR fs s True)
2591+ Partial n s -> return $ Partial n (DeintercalateR fs s)
2592+ Continue n s -> return $ Continue n (DeintercalateR fs s)
25842593 Done n b ->
2585− if consumed
2586− then do
2587− fres <- fstep fs (Right b)
2588− case fres of
2589− FL.Partial fs1 -> do
2590− resL <- initialL
2591− case resL of
2592− IPartial sL ->
2593− return $ Partial n $ DeintercalateL fs1 sL
2594− IDone _ -> errMsg "left" "succeed"
2595− IError _ -> errMsg "left" "fail"
2596− FL.Done c -> return $ Done n c
2597− else error "deintercalate: infinite loop"
2594+ process (fstep fs (Right b)) initialL "left" n DeintercalateL
25982595 Error err -> return $ Error err
25992596
2600− {-# INLINE processResult #-}
2601− processResult n fs r = do
2597+ {-# INLINE extractResult #-}
2598+ extractResult n fs r = do
26022599 res <- fstep fs r
26032600 case res of
26042601 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
@@ -2607,18 +2604,17 @@ deintercalate
26072604 extract (DeintercalateL fs sL) = do
26082605 r <- extractL sL
26092606 case r of
2610− Done n b -> processResult n fs (Left b)
2607+ Done n b -> extractResult n fs (Left b)
26112608 Error err -> return $ Error err
26122609 Continue n s -> return $ Continue n (DeintercalateL fs s)
26132610 Partial _ _ -> error "Partial in extract"
26142611
2615− extract (DeintercalateR fs sR consumed) = do
2612+ extract (DeintercalateR fs sR) = do
26162613 r <- extractR sR
26172614 case r of
2618− Done n b -> processResult n fs (Right b)
2615+ Done n b -> extractResult n fs (Right b)
26192616 Error err -> return $ Error err
2620− -- XXX Review consumed
2621− Continue n s -> return $ Continue n (DeintercalateR fs s consumed)
2617+ Continue n s -> return $ Continue n (DeintercalateR fs s)
26222618 Partial _ _ -> error "Partial in extract"
26232619
26242620data SepByState fs sp ss =