on master help sign in
Use exceptions instead of Either return in extract
f6327e5Harendra Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+9−9
@@ -57,22 +57,22 @@ benchIOSink value name f =
57-------------------------------------------------------------------------------
57-------------------------------------------------------------------------------
58
58
59{-# INLINE any #-}
59{-# INLINE any #-}
60−any :: (Monad m, Ord a) => a -> SerialT m a -> m (Either String Bool)
60+any :: (Monad m, Ord a) => a -> SerialT m a -> m Bool
61any value = IP.parse (PR.any (> value))
61any value = IP.parse (PR.any (> value))
62
62
63{-# INLINE all #-}
63{-# INLINE all #-}
64−all :: (Monad m, Ord a) => a -> SerialT m a -> m (Either String Bool)
64+all :: (Monad m, Ord a) => a -> SerialT m a -> m Bool
65all value = IP.parse (PR.all (<= value))
65all value = IP.parse (PR.all (<= value))
66
66
67−{-# INLINE zipAllAny #-}
68−zipAllAny :: (Monad m, Ord a)
69− => a -> SerialT m a -> m (Either String (Bool, Bool))
70−zipAllAny value = IP.parse ((,) <$> PR.all (<= value) <*> PR.any (> value))
71−
72{-# INLINE take #-}
67{-# INLINE take #-}
73−take :: Monad m => Int -> SerialT m a -> m (Either String ())
68+take :: Monad m => Int -> SerialT m a -> m ()
74take value = IP.parse (FL.take value FL.drain)
69take value = IP.parse (FL.take value FL.drain)
75
70
71+{-# INLINE splitAllAny #-}
72+splitAllAny :: (Monad m, Ord a)
73+ => a -> SerialT m a -> m (Bool, Bool)
74+splitAllAny value = IP.parse ((,) <$> PR.all (<= value) <*> PR.any (> value))
75+
76-------------------------------------------------------------------------------
76-------------------------------------------------------------------------------
77-- Benchmarks
77-- Benchmarks
78-------------------------------------------------------------------------------
78-------------------------------------------------------------------------------
@@ -81,8 +81,8 @@ o_1_space_serial_parse :: Int -> [Benchmark]
81o_1_space_serial_parse value =
81o_1_space_serial_parse value =
82 [ benchIOSink value "any" $ any value
82 [ benchIOSink value "any" $ any value
83 , benchIOSink value "all" $ all value
83 , benchIOSink value "all" $ all value
84− , benchIOSink value "(all,any)" $ zipAllAny value
85 , benchIOSink value "take" $ take value
84 , benchIOSink value "take" $ take value
85+ , benchIOSink value "split (all,any)" $ splitAllAny value
86 ]
86 ]
87
87
88-------------------------------------------------------------------------------
88-------------------------------------------------------------------------------
src/Streamly/Internal/Data/Fold.hs+2−2
@@ -856,9 +856,9 @@ take n (Fold fstep finitial fextract) = Parser step initial extract
856 s1 = Tuple' i1 res
856 s1 = Tuple' i1 res
857 if i1 < n
857 if i1 < n
858 then return $ Yield 0 s1
858 then return $ Yield 0 s1
859− else return $ Stop 0 s1
859+ else Stop 0 <$> fextract res
860
860
861− extract (Tuple' _ r) = fmap Right (fextract r)
861+ extract (Tuple' _ r) = fextract r
862
862
863------------------------------------------------------------------------------
863------------------------------------------------------------------------------
864-- Binary APIs
864-- Binary APIs
src/Streamly/Internal/Data/Parser.hs+41−44
@@ -1,7 +1,5 @@
1−{-# LANGUAGE BangPatterns #-}
2{-# LANGUAGE CPP #-}
1{-# LANGUAGE CPP #-}
3{-# LANGUAGE RankNTypes #-}
2{-# LANGUAGE RankNTypes #-}
4−{-# LANGUAGE RecordWildCards #-}
5{-# LANGUAGE ScopedTypeVariables #-}
3{-# LANGUAGE ScopedTypeVariables #-}
6
4
7-- |
5-- |
@@ -44,8 +42,8 @@ where
44import Prelude
42import Prelude
45 hiding (any, all, takeWhile)
43 hiding (any, all, takeWhile)
46
44
47−import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
48import Streamly.Internal.Data.Fold.Types (Fold(..))
45import Streamly.Internal.Data.Fold.Types (Fold(..))
46+import Streamly.Internal.Data.Parser.Types (Parser(..), Step(..))
49
47
50import Streamly.Internal.Data.Strict
48import Streamly.Internal.Data.Strict
51
49
@@ -57,17 +55,14 @@ import Streamly.Internal.Data.Strict
57--
55--
58{-# INLINE fromFold #-}
56{-# INLINE fromFold #-}
59fromFold :: Monad m => Fold m a b -> Parser m a b
57fromFold :: Monad m => Fold m a b -> Parser m a b
60−fromFold (Fold fstep finitial fextract) = Parser step initial extract
58+fromFold (Fold fstep finitial fextract) = Parser step finitial fextract
59+
61 where
60 where
62
61
63− initial = finitial
64 step s a = Yield 0 <$> fstep s a
62 step s a = Yield 0 <$> fstep s a
65− extract s = do
66− r <- fextract s
67− return $ Right r
68
63
69-------------------------------------------------------------------------------
64-------------------------------------------------------------------------------
70−-- Terminating folds
65+-- Terminating but not failing folds
71-------------------------------------------------------------------------------
66-------------------------------------------------------------------------------
72--
67--
73-- |
68-- |
@@ -76,9 +71,12 @@ fromFold (Fold fstep finitial fextract) = Parser step initial extract
76--
71--
77{-# INLINABLE any #-}
72{-# INLINABLE any #-}
78any :: Monad m => (a -> Bool) -> Parser m a Bool
73any :: Monad m => (a -> Bool) -> Parser m a Bool
79−any predicate = Parser step initial (return . Right)
74+any predicate = Parser step initial return
75+
80 where
76 where
77+
81 initial = return False
78 initial = return False
79+
82 step s a = return $
80 step s a = return $
83 if s
81 if s
84 then Stop 0 True
82 then Stop 0 True
@@ -93,9 +91,12 @@ any predicate = Parser step initial (return . Right)
93--
91--
94{-# INLINABLE all #-}
92{-# INLINABLE all #-}
95all :: Monad m => (a -> Bool) -> Parser m a Bool
93all :: Monad m => (a -> Bool) -> Parser m a Bool
96−all predicate = Parser step initial (return . Right)
94+all predicate = Parser step initial return
95+
97 where
96 where
97+
98 initial = return True
98 initial = return True
99+
99 step s a = return $
100 step s a = return $
100 if s
101 if s
101 then
102 then
@@ -108,6 +109,8 @@ all predicate = Parser step initial (return . Right)
108-- Taking elements
109-- Taking elements
109-------------------------------------------------------------------------------
110-------------------------------------------------------------------------------
110--
111--
112+-- XXX can we use a "cmp" operation in a common implementation?
113+--
111-- | Stops after taking exactly @n@ input elements.
114-- | Stops after taking exactly @n@ input elements.
112--
115--
113-- * Stops - after @n@ elements.
116-- * Stops - after @n@ elements.
@@ -118,21 +121,24 @@ all predicate = Parser step initial (return . Right)
118--
121--
119-- /Internal/
122-- /Internal/
120--
123--
121−{-# INLINABLE takeEQ #-}
124+{-# INLINE takeEQ #-}
122takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
125takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
123takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
126takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
124
127
125 where
128 where
126
129
127− initial = (Tuple' 0) <$> finitial
130+ initial = Tuple' 0 <$> finitial
128
131
129 step (Tuple' i r) a = do
132 step (Tuple' i r) a = do
130 res <- fstep r a
133 res <- fstep r a
131 let i1 = i + 1
134 let i1 = i + 1
132 s1 = Tuple' i1 res
135 s1 = Tuple' i1 res
133− return $ if i1 < n then Skip 0 s1 else Stop 0 s1
136+ if i1 < n then return (Skip 0 s1) else Stop 0 <$> fextract res
134
137
135− extract (Tuple' i r) = fmap f (fextract r)
138+ extract (Tuple' i r) =
139+ if n == i
140+ then fextract r
141+ else error err
136
142
137 where
143 where
138
144
@@ -140,11 +146,6 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
140 "takeEQ: Expecting exactly " ++ show n
146 "takeEQ: Expecting exactly " ++ show n
141 ++ " elements, got " ++ show i
147 ++ " elements, got " ++ show i
142
148
143− f x =
144− if n == i
145− then Right x
146− else Left err
147−
148-- | Take at least @n@ input elements, but can collect more.
149-- | Take at least @n@ input elements, but can collect more.
149--
150--
150-- * Stops - never.
151-- * Stops - never.
@@ -158,21 +159,22 @@ takeEQ n (Fold fstep finitial fextract) = Parser step initial extract
158--
159--
159-- /Internal/
160-- /Internal/
160--
161--
161−{-# INLINABLE takeGE #-}
162+{-# INLINE takeGE #-}
162takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
163takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
163takeGE n (Fold fstep finitial fextract) = Parser step initial extract
164takeGE n (Fold fstep finitial fextract) = Parser step initial extract
164
165
165 where
166 where
166
167
167− initial = (Tuple' 0) <$> finitial
168+ initial = Tuple' 0 <$> finitial
168
169
169 step (Tuple' i r) a = do
170 step (Tuple' i r) a = do
170 res <- fstep r a
171 res <- fstep r a
171 let i1 = i + 1
172 let i1 = i + 1
172 s1 = Tuple' i1 res
173 s1 = Tuple' i1 res
174+ return $
173− if i1 < n
175+ if i1 < n
174− then return $ Skip 0 s1
176+ then Skip 0 s1
175− else return $ Yield 0 s1
177+ else Yield 0 s1
176
178
177 extract (Tuple' i r) = fmap f (fextract r)
179 extract (Tuple' i r) = fmap f (fextract r)
178
180
@@ -184,8 +186,8 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
184
186
185 f x =
187 f x =
186 if i >= n
188 if i >= n
187− then Right x
189+ then x
188− else Left err
190+ else error err
189
191
190-- | Take until the predicate fails. The element on which the predicate fails
192-- | Take until the predicate fails. The element on which the predicate fails
191-- is returned back to the input stream.
193-- is returned back to the input stream.
@@ -202,21 +204,19 @@ takeGE n (Fold fstep finitial fextract) = Parser step initial extract
202--
204--
203-- /Internal/
205-- /Internal/
204--
206--
205−{-# INLINABLE takeWhile #-}
207+{-# INLINE takeWhile #-}
206takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
208takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
207takeWhile predicate (Fold fstep finitial fextract) =
209takeWhile predicate (Fold fstep finitial fextract) =
208− Parser step initial extract
210+ Parser step initial fextract
209
211
210 where
212 where
211
213
212 initial = finitial
214 initial = finitial
215+
213− step s a = do
216+ step s a =
214 if predicate a
217 if predicate a
215 then Yield 0 <$> fstep s a
218 then Yield 0 <$> fstep s a
216− else return $ Stop 1 s
217− extract s = do
218− b <- fextract s
219+ else Stop 1 <$> fextract s
219− return $ Right b
220
220
221-- | Keep taking elements until the predicate succeeds. Drop the succeeding
221-- | Keep taking elements until the predicate succeeds. Drop the succeeding
222-- element.
222-- element.
@@ -237,18 +237,15 @@ takeWhile predicate (Fold fstep finitial fextract) =
237{-# INLINABLE sepBy #-}
237{-# INLINABLE sepBy #-}
238sepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
238sepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
239sepBy predicate (Fold fstep finitial fextract) =
239sepBy predicate (Fold fstep finitial fextract) =
240− Parser step initial extract
240+ Parser step initial fextract
241
241
242 where
242 where
243
243
244 initial = finitial
244 initial = finitial
245− step s a = do
245+ step s a =
246 if not (predicate a)
246 if not (predicate a)
247 then Yield 0 <$> fstep s a
247 then Yield 0 <$> fstep s a
248− else return $ Stop 0 s
249− extract s = do
250− b <- fextract s
248+ else Stop 0 <$> fextract s
251− return $ Right b
252
249
253-- | Keep taking elements until the predicate succeeds. Take the succeeding
250-- | Keep taking elements until the predicate succeeds. Take the succeeding
254-- element as well.
251-- element as well.
@@ -303,10 +300,10 @@ sepByMax predicate count (Fold fstep finitial fextract) =
303 s1 = Tuple' i1 res
300 s1 = Tuple' i1 res
304 if not (predicate a) && i1 < count
301 if not (predicate a) && i1 < count
305 then return $ Yield 0 s1
302 then return $ Yield 0 s1
303+ else do
304+ b <- fextract res
306− else return $ Stop 0 s1
305+ return $ Stop 0 b
307− extract (Tuple' _ r) = do
306+ extract (Tuple' _ r) = fextract r
308− b <- fextract r
309− return $ Right b
310
307
311-- | Like 'splitOn' after stripping leading, trailing, and repeated separators.
308-- | Like 'splitOn' after stripping leading, trailing, and repeated separators.
312-- Therefore, @".a..b."@ with '.' as the separator would be parsed as
309-- Therefore, @".a..b."@ with '.' as the separator would be parsed as
src/Streamly/Internal/Data/Parser/Types.hs+39−36
@@ -1,8 +1,4 @@
1{-# LANGUAGE ExistentialQuantification #-}
1{-# LANGUAGE ExistentialQuantification #-}
2−{-# LANGUAGE BangPatterns #-}
3−{-# LANGUAGE DeriveFunctor #-}
4−{-# LANGUAGE DeriveAnyClass #-}
5−{-# LANGUAGE DeriveGeneric #-}
6
2
7-- |
3-- |
8-- Module : Streamly.Parser.Types
4-- Module : Streamly.Parser.Types
@@ -70,7 +66,7 @@ import Fusion.Plugin.Types (Fuse(..))
70-- /Internal/
66-- /Internal/
71--
67--
72{-# ANN type Step Fuse #-}
68{-# ANN type Step Fuse #-}
73−data Step s =
69+data Step s b =
74 Yield Int s
70 Yield Int s
75 -- ^ @Yield offset state@ indicates that the parser has yielded a new
71 -- ^ @Yield offset state@ indicates that the parser has yielded a new
76 -- result which is a point of no return. The result can be extracted
72 -- result which is a point of no return. The result can be extracted
@@ -87,13 +83,19 @@ data Step s =
87 -- the next input to the parser. The offset cannot be beyond the latest
83 -- the next input to the parser. The offset cannot be beyond the latest
88 -- point of no return created by @Yield@.
84 -- point of no return created by @Yield@.
89
85
90− | Stop Int s
86+ | Stop Int b
91 -- ^ @Stop offset state@ asks the driver to stop driving the parser because
87 -- ^ @Stop offset state@ asks the driver to stop driving the parser because
92 -- it has reached a fixed point and further input will not change the
88 -- it has reached a fixed point and further input will not change the
93 -- result. @offset@ is the count of unused elements which includes the
89 -- result. @offset@ is the count of unused elements which includes the
94 -- element on which 'Stop' occurred. Once a fold stops, driving it further
90 -- element on which 'Stop' occurred. Once a fold stops, driving it further
95 -- may produce undefined behavior.
91 -- may produce undefined behavior.
96
92
93+instance Functor (Step s) where
94+ {-# INLINE fmap #-}
95+ fmap _ (Yield n s) = Yield n s
96+ fmap _ (Skip n s) = Skip n s
97+ fmap f (Stop n b) = Stop n (f b)
98+
97-- | A parsing fold is represented as @Parser step initial extract@. Before we
99-- | A parsing fold is represented as @Parser step initial extract@. Before we
98-- drive a parser we call the @initial@ action to retrieve the initial state of
100-- drive a parser we call the @initial@ action to retrieve the initial state of
99-- the fold. The driver invokes @step@ with the state returned by the previous
101-- the fold. The driver invokes @step@ with the state returned by the previous
@@ -103,39 +105,43 @@ data Step s =
103-- error or an output value.
105-- error or an output value.
104--
106--
105data Parser m a b =
107data Parser m a b =
106− forall s. Parser (s -> a -> m (Step s)) (m s) (s -> m (Either String b))
108+ forall s. Parser (s -> a -> m (Step s b)) (m s) (s -> m b)
107
109
108-- | Maps a function over the output of the fold.
110-- | Maps a function over the output of the fold.
109--
111--
110instance Functor m => Functor (Parser m a) where
112instance Functor m => Functor (Parser m a) where
111 {-# INLINE fmap #-}
113 {-# INLINE fmap #-}
112− fmap f (Parser step initial extract) =
114+ fmap f (Parser step1 initial extract) =
113− Parser step initial (fmap3 f extract)
115+ Parser step initial (fmap2 f extract)
114
116
115 where
117 where
116
118
119+ step s b = fmap2 f (step1 s b)
117− fmap3 g = fmap (fmap (fmap g))
120+ fmap2 g = fmap (fmap g)
118
121
119{-# INLINE wrap #-}
122{-# INLINE wrap #-}
120wrap :: Monad m => b -> Parser m a b
123wrap :: Monad m => b -> Parser m a b
121−wrap b = Parser (\_ _ -> pure $ Stop 0 ()) -- step
124+wrap b = Parser (\_ _ -> pure $ Stop 0 b) -- step
122− (pure ()) -- initial
125+ (pure ()) -- initial
123− (\_ -> pure $ Right b) -- extract
126+ (\_ -> pure b) -- extract
124
127
125−{-# ANN type SeqParseState Fuse #-}
128+-------------------------------------------------------------------------------
126−data SeqParseState sl f sr =
129+-- Sequential applicative
127− SeqParseL sl | SeqParseR f sr | SeqParseLErr String
130+-------------------------------------------------------------------------------
128
131
129−-- XXX implement interleaved variant, parse using the first parser and then
132+{-# ANN type SeqParseState Fuse #-}
130−-- using the second parser alternately.
133+data SeqParseState sl f sr = SeqParseL sl | SeqParseR f sr
131
134
132−-- | Apply two parsers sequentially to an input stream. The input is provided
135+-- | Sequential application. Apply two parsers sequentially to an input stream.
133−-- to the first parser, when it is done the remaining input is provided to the
136+-- The input is provided to the first parser, when it is done the remaining
134−-- second parser. If both the parsers succeed there outputs are combined using
137+-- input is provided to the second parser. If both the parsers succeed their
135−-- the supplied function. The operation fails if any of the parsers fail.
138+-- outputs are combined using the supplied function. The operation fails if any
139+-- of the parsers fail.
136--
140--
137-- This undoes an "append" of two streams, it splits the streams.
141-- This undoes an "append" of two streams, it splits the streams.
138--
142--
143+-- /Internal/
144+--
139{-# INLINE splitWith #-}
145{-# INLINE splitWith #-}
140splitWith :: Monad m
146splitWith :: Monad m
141 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
147 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
@@ -152,28 +158,25 @@ splitWith func (Parser stepL initialL extractL)
152 step (SeqParseL st) a = do
158 step (SeqParseL st) a = do
153 r <- stepL st a
159 r <- stepL st a
154 case r of
160 case r of
161+ -- Note: this leads to buffering even if we are not in an
162+ -- Alternative composition.
155 Yield _ s -> return $ Skip 0 (SeqParseL s)
163 Yield _ s -> return $ Skip 0 (SeqParseL s)
156 Skip n s -> return $ Skip n (SeqParseL s)
164 Skip n s -> return $ Skip n (SeqParseL s)
157− Stop n s -> do
158− res <- extractL s
159− case res of
160− Left err -> return $ Stop n (SeqParseLErr err)
161− Right x -> Skip n <$> (SeqParseR (func x) <$> initialR)
165+ Stop n b -> Skip n <$> (SeqParseR (func b) <$> initialR)
162
166
163 step (SeqParseR f st) a = do
167 step (SeqParseR f st) a = do
164 r <- stepR st a
168 r <- stepR st a
165 return $ case r of
169 return $ case r of
166 Yield n s -> Yield n (SeqParseR f s)
170 Yield n s -> Yield n (SeqParseR f s)
167 Skip n s -> Skip n (SeqParseR f s)
171 Skip n s -> Skip n (SeqParseR f s)
168− Stop n s -> Stop n (SeqParseR f s)
172+ Stop n b -> Stop n (f b)
169−
173+
170− step (SeqParseLErr _) _ = error "step called in SeqParseLErr"
171−
172− -- XXX bimap to add "<*>: Right parse failed" to the error message
173− extract (SeqParseR f s) = fmap (fmap f) (extractR s)
174+ extract (SeqParseR f sR) = fmap f (extractR sR)
174− extract (SeqParseL _) = return $ Left "<*>: Incomplete left parse"
175+ extract (SeqParseL sL) = do
175− extract (SeqParseLErr err) =
176+ rL <- extractL sL
176− return $ Left $ "<*>: Left parse failed\n" ++ err
177+ sR <- initialR
178+ rR <- extractR sR
179+ return $ func rL rR
177
180
178-- | 'Applicative' form of 'splitWith'.
181-- | 'Applicative' form of 'splitWith'.
179instance Monad m => Applicative (Parser m a) where
182instance Monad m => Applicative (Parser m a) where
src/Streamly/Internal/Data/Stream/Prelude.hs+3−3
@@ -162,11 +162,11 @@ foldlMx' step begin done m = S.foldlMx' step begin done $ toStreamS m
162{-# INLINE parselMx' #-}
162{-# INLINE parselMx' #-}
163parselMx'
163parselMx'
164 :: (IsStream t, Monad m)
164 :: (IsStream t, Monad m)
165− => (s -> a -> m (Step s))
165+ => (s -> a -> m (Step s b))
166 -> m s
166 -> m s
167− -> (s -> m (Either String b))
167+ -> (s -> m b)
168 -> t m a
168 -> t m a
169− -> m (Either String b)
169+ -> m b
170parselMx' step initial extract m =
170parselMx' step initial extract m =
171 D.parselMx' step initial extract $ D.toStreamD m
171 D.parselMx' step initial extract $ D.toStreamD m
172
172
src/Streamly/Internal/Data/Stream/StreamD.hs+18−27
@@ -972,11 +972,11 @@ splitAt n ls
972{-# INLINE_NORMAL parselMx' #-}
972{-# INLINE_NORMAL parselMx' #-}
973parselMx'
973parselMx'
974 :: Monad m
974 :: Monad m
975− => (s -> a -> m (PR.Step s))
975+ => (s -> a -> m (PR.Step s b))
976 -> m s
976 -> m s
977− -> (s -> m (Either String b))
977+ -> (s -> m b)
978 -> Stream m a
978 -> Stream m a
979− -> m (Either String b)
979+ -> m b
980parselMx' pstep initial extract (Stream step state) = do
980parselMx' pstep initial extract (Stream step state) = do
981 initial >>= go SPEC state []
981 initial >>= go SPEC state []
982
982
@@ -1002,7 +1002,7 @@ parselMx' pstep initial extract (Stream step state) = do
1002 let (src0, buf1) = splitAt n (x:buf)
1002 let (src0, buf1) = splitAt n (x:buf)
1003 src = Prelude.reverse src0
1003 src = Prelude.reverse src0
1004 gobuf SPEC s buf1 src pst1
1004 gobuf SPEC s buf1 src pst1
1005− PR.Stop _ pst1 -> extract pst1
1005+ PR.Stop _ b -> return b
1006 Skip s -> go SPEC s buf pst
1006 Skip s -> go SPEC s buf pst
1007 Stop -> extract pst
1007 Stop -> extract pst
1008
1008
@@ -1020,7 +1020,7 @@ parselMx' pstep initial extract (Stream step state) = do
1020 let (src0, buf1) = splitAt n (x:buf)
1020 let (src0, buf1) = splitAt n (x:buf)
1021 src = Prelude.reverse src0 ++ xs
1021 src = Prelude.reverse src0 ++ xs
1022 gobuf SPEC s buf1 src pst1
1022 gobuf SPEC s buf1 src pst1
1023− PR.Stop _ pst1 -> extract pst1
1023+ PR.Stop _ b -> return b
1024
1024
1025------------------------------------------------------------------------------
1025------------------------------------------------------------------------------
1026-- Repeated parsing
1026-- Repeated parsing
@@ -1077,22 +1077,17 @@ splitParse (Parser pstep initial extract) (Stream step state) =
1077 src = Prelude.reverse src0
1077 src = Prelude.reverse src0
1078 return $ Skip $ ParseChunksBuf src s buf1 pst1
1078 return $ Skip $ ParseChunksBuf src s buf1 pst1
1079 -- XXX Halt 0 common case?
1079 -- XXX Halt 0 common case?
1080− PR.Stop n pst1 -> do
1080+ PR.Stop n b -> do
1081− res <- extract pst1
1082− case res of
1083− Left err -> error err
1084− Right a -> do
1085− assert (n <= length (x:buf)) (return ())
1081+ assert (n <= length (x:buf)) (return ())
1086− let src = Prelude.reverse (Prelude.take n (x:buf))
1082+ let src = Prelude.reverse (Prelude.take n (x:buf))
1083+ return $ Skip $
1087− return $ Skip $ ParseChunksYield a (ParseChunksInit src s)
1084+ ParseChunksYield b (ParseChunksInit src s)
1088 Skip s -> return $ Skip $ ParseChunksStream s buf pst
1085 Skip s -> return $ Skip $ ParseChunksStream s buf pst
1089 Stop -> do
1086 Stop -> do
1090− res <- extract pst
1087+ b <- extract pst
1091− case res of
1092− Left err -> error err
1093− Right a -> do
1094− let src = Prelude.reverse buf
1088+ let src = Prelude.reverse buf
1089+ return $ Skip $
1095− return $ Skip $ ParseChunksYield a (ParseChunksInitLeftOver src)
1090+ ParseChunksYield b (ParseChunksInitLeftOver src)
1096
1091
1097 -- go back to stream processing mode
1092 -- go back to stream processing mode
1098 stepOuter _ (ParseChunksBuf [] s buf pst) =
1093 stepOuter _ (ParseChunksBuf [] s buf pst) =
@@ -1114,14 +1109,10 @@ splitParse (Parser pstep initial extract) (Stream step state) =
1114 src = Prelude.reverse src0 ++ xs
1109 src = Prelude.reverse src0 ++ xs
1115 return $ Skip $ ParseChunksBuf src s buf1 pst1
1110 return $ Skip $ ParseChunksBuf src s buf1 pst1
1116 -- XXX have 0 case
1111 -- XXX have 0 case
1117− PR.Stop n pst1 -> do
1112+ PR.Stop n b -> do
1118− res <- extract pst1
1119− case res of
1120− Left err -> error err
1121− Right a -> do
1122− assert (n <= length (x:buf)) (return ())
1113+ assert (n <= length (x:buf)) (return ())
1123− let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1114+ let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1124− return $ Skip $ ParseChunksYield a (ParseChunksInit src s)
1115+ return $ Skip $ ParseChunksYield b (ParseChunksInit src s)
1125
1116
1126 stepOuter _ (ParseChunksYield a next) = return $ Yield a next
1117 stepOuter _ (ParseChunksYield a next) = return $ Yield a next
1127
1118
src/Streamly/Internal/Prelude.hs+1−1
@@ -1202,7 +1202,7 @@ runSink = fold . toFold
1202-- /Internal/
1202-- /Internal/
1203--
1203--
1204{-# INLINE parse #-}
1204{-# INLINE parse #-}
1205−parse :: Monad m => Parser m a b -> SerialT m a -> m (Either String b)
1205+parse :: Monad m => Parser m a b -> SerialT m a -> m b
1206parse (Parser step initial extract) = P.parselMx' step initial extract
1206parse (Parser step initial extract) = P.parselMx' step initial extract
1207
1207
1208------------------------------------------------------------------------------
1208------------------------------------------------------------------------------