on master help sign in
Make deintercalate to parse infix pattern
a5907e1Harendra Kumar
core/src/Streamly/Internal/Data/Parser/ParserD.hs+58−23
@@ -2521,18 +2521,34 @@ lookAhead (Parser step1 initial1 _) = Parser step initial extract
2521-- line number info.
2521-- line number info.
2522
2522
2523data DeintercalateState fs sp ss =
2523data DeintercalateState fs sp ss =
2524+ DeintercalateInitL !fs
2524− DeintercalateL !fs !sp
2525+ | DeintercalateL !fs !sp
2526+ | DeintercalateInitR !fs
2525 | DeintercalateR !fs !ss
2527 | DeintercalateR !fs !ss
2526
2528
2527-- XXX rename this to intercalate
2529-- 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.
2528
2538
2529-- | Apply two parsers alternately to an input stream. The input stream is
2539-- | Apply two parsers alternately to an input stream. The input stream is
2530-- considered an interleaving of two patterns. The two parsers represent the
2540-- 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.
2532--
2544--
2533-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
2545-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
2534-- >>> p2 = Parser.satisfy (== '+')
2546-- >>> p2 = Parser.satisfy (== '+')
2535-- >>> p = Parser.deintercalate p1 p2 Fold.toList
2547-- >>> 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"]
2536-- >>> Stream.parse p $ Stream.fromList "1+2+3"
2552-- >>> Stream.parse p $ Stream.fromList "1+2+3"
2537-- Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
2553-- Right [Left "1",Right '+',Left "2",Right '+',Left "3"]
2538--
2554--
@@ -2556,44 +2572,62 @@ deintercalate
2556 initial = do
2572 initial = do
2557 res <- finitial
2573 res <- finitial
2558 case res of
2574 case res of
2559− FL.Partial fs -> do
2575+ FL.Partial fs -> return $ IPartial $ (DeintercalateInitL fs)
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"
2565 FL.Done c -> return $ IDone c
2576 FL.Done c -> return $ IDone c
2566
2577
2567− {-# INLINE process #-}
2578+ {-# INLINE processL #-}
2568− process foldAction initAction pid n nextState = do
2579+ processL foldAction n nextState = do
2569 fres <- foldAction
2580 fres <- foldAction
2570 case fres of
2581 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)
2582+ FL.Partial fs1 -> return $ Partial n (nextState fs1)
2576− IDone _ -> errMsg pid "succeed"
2577− IError _ -> errMsg pid "fail"
2578 FL.Done c -> return $ Done n c
2583 FL.Done c -> return $ Done n c
2579
2584
2580− step (DeintercalateL fs sL) a = do
2585+ {-# INLINE runStepL #-}
2586+ runStepL fs sL a = do
2581 r <- stepL sL a
2587 r <- stepL sL a
2582 case r of
2588 case r of
2583 Partial n s -> return $ Partial n (DeintercalateL fs s)
2589 Partial n s -> return $ Partial n (DeintercalateL fs s)
2584 Continue n s -> return $ Continue n (DeintercalateL fs s)
2590 Continue n s -> return $ Continue n (DeintercalateL fs s)
2585 Done n b ->
2591 Done n b ->
2586− process (fstep fs (Left b)) initialR "right" n DeintercalateR
2592+ processL (fstep fs (Left b)) n DeintercalateInitR
2587 Error err -> return $ Error err
2593 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
2589 r <- stepR sR a
2609 r <- stepR sR a
2590 case r of
2610 case r of
2591 Partial n s -> return $ Partial n (DeintercalateR fs s)
2611 Partial n s -> return $ Partial n (DeintercalateR fs s)
2592 Continue n s -> return $ Continue n (DeintercalateR fs s)
2612 Continue n s -> return $ Continue n (DeintercalateR fs s)
2593− Done n b ->
2613+ Done n b -> processR (fstep fs (Right b)) n
2594− process (fstep fs (Right b)) initialL "left" n DeintercalateL
2595 Error err -> return $ Error err
2614 Error err -> return $ Error err
2596
2615
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+
2597 {-# INLINE extractResult #-}
2631 {-# INLINE extractResult #-}
2598 extractResult n fs r = do
2632 extractResult n fs r = do
2599 res <- fstep fs r
2633 res <- fstep fs r
@@ -2601,6 +2635,7 @@ deintercalate
2601 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2635 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2602 FL.Done c -> return (Done n c)
2636 FL.Done c -> return (Done n c)
2603
2637
2638+ extract (DeintercalateInitL fs) = fmap (Done 0) $ fextract fs
2604 extract (DeintercalateL fs sL) = do
2639 extract (DeintercalateL fs sL) = do
2605 r <- extractL sL
2640 r <- extractL sL
2606 case r of
2641 case r of
@@ -2608,7 +2643,7 @@ deintercalate
2608 Error err -> return $ Error err
2643 Error err -> return $ Error err
2609 Continue n s -> return $ Continue n (DeintercalateL fs s)
2644 Continue n s -> return $ Continue n (DeintercalateL fs s)
2610 Partial _ _ -> error "Partial in extract"
2645 Partial _ _ -> error "Partial in extract"
2611−
2646+ extract (DeintercalateInitR fs) = fmap (Done 0) $ fextract fs
2612 extract (DeintercalateR fs sR) = do
2647 extract (DeintercalateR fs sR) = do
2613 r <- extractR sR
2648 r <- extractR sR
2614 case r of
2649 case r of