@@ -2800,23 +2800,33 @@ deintercalate
2800 return $ Done cnt xs
2800 return $ Done cnt xs
2802data SepByState fs sp ss =
2802data SepByState fs sp ss =
2803− SepByL !fs !sp
2804+ | SepByL !Int !fs !sp
2804− | SepByR !fs !ss
2806+ | SepByR !Int !fs !ss
2806−-- | Run the content parser first, when it is done, the separator parser is
2808+-- | Apply two parsers alternately to an input stream. Parsing starts at the
2807−-- run, when it is done content parser is run again and so on. If none of the
2809+-- first parser and stops at the first parser. The output of the first parser
2808−-- parsers consumes an input then parser returns a failure.
2810+-- is emiited and the output of the second parser is discarded. It can be used
2811+-- to parse a infix style pattern e.g. p1 p2 p1 . Empty input or single parse
2812+-- of the first parser is accepted.
2810-- Definitions:
2814-- Definitions:
2812−-- >>> sepBy p1 p2 sink = Parser.deintercalate p1 p2 (Fold.catLefts sink)
2816+-- >>> sepBy p1 p2 f = Parser.deintercalate p1 p2 (Fold.catLefts f)
2813−-- >>> sepBy content sep sink = Parser.sepBy1 content sep sink <|> return mempty
2817+-- >>> sepBy p1 p2 f = Parser.sepBy1 p1 p2 f <|> return mempty
2815-- Examples:
2819-- Examples:
2817-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
2821-- >>> p1 = Parser.takeWhile1 (not . (== '+')) Fold.toList
2818-- >>> p2 = Parser.satisfy (== '+')
2822-- >>> p2 = Parser.satisfy (== '+')
2819-- >>> p = Parser.sepBy p1 p2 Fold.toList
2823-- >>> p = Parser.sepBy p1 p2 Fold.toList
2824+-- >>> Stream.parse p $ Stream.fromList ""
2826+-- >>> Stream.parse p $ Stream.fromList "1"
2828+-- >>> Stream.parse p $ Stream.fromList "1+"
2820-- >>> Stream.parse p $ Stream.fromList "1+2+3"
2830-- >>> Stream.parse p $ Stream.fromList "1+2+3"
2821-- Right ["1","2","3"]
2831-- Right ["1","2","3"]
@@ -2837,48 +2847,63 @@ sepBy
2837 initial = do
2847 initial = do
2838 res <- finitial
2848 res <- finitial
2839 case res of
2849 case res of
2840− FL.Partial fs -> do
2843− IPartial sL -> return $ IPartial $ (SepByL fs sL)
2850+ FL.Partial fs -> return $ IPartial $ (SepByInitL fs)
2844− IDone _ -> errMsg "left" "succeed"
2845− IError _ -> errMsg "left" "fail"
2846 FL.Done c -> return $ IDone c
2851 FL.Done c -> return $ IDone c
2848− {-# INLINE process #-}
2853+ {-# INLINE processL #-}
2849− process foldAction initAction pid n nextState = do
2854+ processL foldAction n nextState = do
2850 fres <- foldAction
2855 fres <- foldAction
2851 case fres of
2856 case fres of
2852− FL.Partial fs1 -> do
2856− return $ Partial n (nextState fs1 ps)
2857+ FL.Partial fs1 -> return $ Partial n (nextState fs1)
2857− IDone _ -> errMsg pid "succeed"
2858− IError _ -> errMsg pid "fail"
2859 FL.Done c -> return $ Done n c
2858 FL.Done c -> return $ Done n c
2860+ {-# INLINE runStepL #-}
2861− step (SepByL fs sL) a = do
2861+ runStepL cnt fs sL a = do
2862 r <- stepL sL a
2863 r <- stepL sL a
2863 case r of
2864 case r of
2864− Partial n s -> return $ Partial n (SepByL fs s)
2865+ Partial n s -> return $ Continue n (SepByL cnt1 fs s)
2865− Continue n s -> return $ Continue n (SepByL fs s)
2866+ Continue n s -> return $ Continue n (SepByL cnt1 fs s)
2866 Done n b ->
2867 Done n b ->
2867− process (fstep fs b) initialR "right" n SepByR
2868+ processL (fstep fs b) n SepByInitR
2868− Error err -> return $ Error err
2869+ Error _ -> do
2871+ return $ Done cnt1 xs
2873+ {-# INLINE processR #-}
2874+ processR cnt fs n = do
2877+ IPartial ps -> return $ Continue n (SepByL cnt fs ps)
2878+ IDone _ -> errMsg "left" "succeed"
2879+ IError _ -> errMsg "left" "fail"
2881+ {-# INLINE runStepR #-}
2869− step (SepByR fs sR) a = do
2882+ runStepR cnt fs sR a = do
2870 r <- stepR sR a
2884 r <- stepR sR a
2871 case r of
2885 case r of
2872− Partial n s -> return $ Partial n (SepByR fs s)
2886+ Partial n s -> return $ Continue n (SepByR cnt1 fs s)
2873− Continue n s -> return $ Continue n (SepByR fs s)
2887+ Continue n s -> return $ Continue n (SepByR cnt1 fs s)
2874− Done n _ -> do
2888+ Done n _ -> processR cnt1 fs n
2891+ return $ Done cnt1 xs
2893+ step (SepByInitL fs) a = do
2875− res <- initialL
2894+ res <- initialL
2876− case res of
2895+ case res of
2877− IPartial ps ->
2896+ IPartial s -> runStepL 0 fs s a
2878− return $ Partial n (SepByL fs ps)
2897+ IDone _ -> errMsg "left" "succeed"
2898+ IError _ -> errMsg "left" "fail"
2899+ step (SepByL cnt fs sL) a = runStepL cnt fs sL a
2900+ step (SepByInitR fs) a = do
2903+ IPartial s -> runStepR 0 fs s a
2879− IDone _ -> errMsg "left" "succeed"
2904+ IDone _ -> errMsg "right" "succeed"
2880− IError _ -> errMsg "left" "fail"
2905+ IError _ -> errMsg "right" "fail"
2881− Error err -> return $ Error err
2906+ step (SepByR cnt fs sR) a = runStepR cnt fs sR a
2883 {-# INLINE extractResult #-}
2908 {-# INLINE extractResult #-}
2884 extractResult n fs r = do
2909 extractResult n fs r = do
@@ -2887,15 +2912,18 @@ sepBy
2887 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2912 FL.Partial fs1 -> fmap (Done n) $ fextract fs1
2888 FL.Done c -> return (Done n c)
2913 FL.Done c -> return (Done n c)
2915+ extract (SepByInitL fs) = fmap (Done 0) $ fextract fs
2890− extract (SepByL fs sL) = do
2916+ extract (SepByL cnt fs sL) = do
2891 r <- extractL sL
2917 r <- extractL sL
2892 case r of
2918 case r of
2893 Done n b -> extractResult n fs b
2919 Done n b -> extractResult n fs b
2894− Error err -> return $ Error err
2895− Continue n s -> return $ Continue n (SepByL fs s)
2920+ Continue n s -> return $ Continue n (SepByL cnt fs s)
2896 Partial _ _ -> error "Partial in extract"
2921 Partial _ _ -> error "Partial in extract"
2924+ return $ Done cnt xs
2898− extract (SepByR fs _) = fmap (Done 0) (fextract fs)
2925+ extract (SepByInitR fs) = fmap (Done 0) $ fextract fs
2926+ extract (SepByR cnt fs _) = fmap (Done cnt) $ fextract fs
2900-- XXX This can be implemented using refold, parse one and then continue
2928-- XXX This can be implemented using refold, parse one and then continue
2901-- collecting the rest in that.
2929-- collecting the rest in that.