@@ -2521,18 +2521,34 @@ lookAhead (Parser step1 initial1 _) = Parser step initial extract
25212521-- line number info.
25222522
25232523data DeintercalateState fs sp ss =
2524− DeintercalateL !fs !sp
2524+ DeintercalateInitL !fs
2525+ | DeintercalateL !fs !sp
2526+ | DeintercalateInitR !fs
25252527 | DeintercalateR !fs !ss
25262528
25272529-- XXX rename this to intercalate
2530+-- XXX Need three versions of this:
2531+--
2532+-- * intercalate -- Stop at the first parser (default?)
2533+-- * intercalateSuffix/Snd -- Stop at the second parser
2534+-- * intercalateAny -- Stop at any parser
2535+--
2536+-- Make the naming such that the names of intercalate etc in stream module are
2537+-- duals of these.
25282538
25292539-- | Apply two parsers alternately to an input stream. The input stream is
25302540-- considered an interleaving of two patterns. The two parsers represent the
2531−-- two patterns.
2541+-- two patterns. Parsing starts at the first parser and stops at the first
2542+-- parser. It can be used to parse a infix style pattern e.g. p1 p2 p1 . Empty
2543+-- input or single parse of the first parser is accepted.
25322544--
25332545-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
25342546-- >>> p2 = Parser.satisfy (== '+')
25352547-- >>> p = Parser.deintercalate p1 p2 Fold.toList
2548+-- >>> Stream.parse p $ Stream.fromList ""
2549+-- Right []
2550+-- >>> Stream.parse p $ Stream.fromList "1"
2551+-- Right [Left "1"]
25362552-- >>> Stream.parse p $ Stream.fromList "1+2+3"
25372553-- Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
25382554--
@@ -2556,44 +2572,62 @@ deintercalate
25562572 initial = do
25572573 res <- finitial
25582574 case res of
2559− FL.Partial fs -> do
2560− resL <- initialL
2561− case resL of
2562− IPartial sL -> return $ IPartial $ (DeintercalateL fs sL)
2563− IDone _ -> errMsg "left" "succeed"
2564− IError _ -> errMsg "left" "fail"
2575+ FL.Partial fs -> return $ IPartial $ (DeintercalateInitL fs)
25652576 FL.Done c -> return $ IDone c
25662577
2567− {-# INLINE process #-}
2568− process foldAction initAction pid n nextState = do
2578+ {-# INLINE processL #-}
2579+ processL foldAction n nextState = do
25692580 fres <- foldAction
25702581 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"
2582+ FL.Partial fs1 -> return $ Partial n (nextState fs1)
25782583 FL.Done c -> return $ Done n c
25792584
2580− step (DeintercalateL fs sL) a = do
2585+ {-# INLINE runStepL #-}
2586+ runStepL fs sL a = do
25812587 r <- stepL sL a
25822588 case r of
25832589 Partial n s -> return $ Partial n (DeintercalateL fs s)
25842590 Continue n s -> return $ Continue n (DeintercalateL fs s)
25852591 Done n b ->
2586− process (fstep fs (Left b)) initialR "right" n DeintercalateR
2592+ processL (fstep fs (Left b)) n DeintercalateInitR
25872593 Error err -> return $ Error err
2588− step (DeintercalateR fs sR) a = do
2594+
2595+ {-# INLINE processR #-}
2596+ processR foldAction n = do
2597+ fres <- foldAction
2598+ case fres of
2599+ FL.Partial fs1 -> do
2600+ res <- initialL
2601+ case res of
2602+ IPartial ps -> return $ Partial n (DeintercalateL fs1 ps)
2603+ IDone _ -> errMsg "left" "succeed"
2604+ IError _ -> errMsg "left" "fail"
2605+ FL.Done c -> return $ Done n c
2606+
2607+ {-# INLINE runStepR #-}
2608+ runStepR fs sR a = do
25892609 r <- stepR sR a
25902610 case r of
25912611 Partial n s -> return $ Partial n (DeintercalateR fs s)
25922612 Continue n s -> return $ Continue n (DeintercalateR fs s)
2593− Done n b ->
2594− process (fstep fs (Right b)) initialL "left" n DeintercalateL
2613+ Done n b -> processR (fstep fs (Right b)) n
25952614 Error err -> return $ Error err
25962615
2616+ step (DeintercalateInitL fs) a = do
2617+ res <- initialL
2618+ case res of
2619+ IPartial s -> runStepL fs s a
2620+ IDone _ -> errMsg "left" "succeed"
2621+ IError _ -> errMsg "left" "fail"
2622+ step (DeintercalateL fs sL) a = runStepL fs sL a
2623+ step (DeintercalateInitR fs) a = do
2624+ res <- initialR
2625+ case res of
2626+ IPartial s -> runStepR fs s a
2627+ IDone _ -> errMsg "right" "succeed"
2628+ IError _ -> errMsg "right" "fail"
2629+ step (DeintercalateR fs sR) a = runStepR fs sR a
2630+
25972631 {-# INLINE extractResult #-}
25982632 extractResult n fs r = do
25992633 res <- fstep fs r
@@ -2601,6 +2635,7 @@ deintercalate
26012635 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
26022636 FL.Done c -> return (Done n c)
26032637
2638+ extract (DeintercalateInitL fs) = fmap (Done 0) $ fextract fs
26042639 extract (DeintercalateL fs sL) = do
26052640 r <- extractL sL
26062641 case r of
@@ -2608,7 +2643,7 @@ deintercalate
26082643 Error err -> return $ Error err
26092644 Continue n s -> return $ Continue n (DeintercalateL fs s)
26102645 Partial _ _ -> error "Partial in extract"
2611−
2646+ extract (DeintercalateInitR fs) = fmap (Done 0) $ fextract fs
26122647 extract (DeintercalateR fs sR) = do
26132648 r <- extractR sR
26142649 case r of