on master help sign in
Make parser result type either everywhere
3a21b44Ranjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Parser.hs+67−59
@@ -10,6 +10,7 @@
10{-# LANGUAGE CPP #-}
10{-# LANGUAGE CPP #-}
11{-# LANGUAGE FlexibleContexts #-}
11{-# LANGUAGE FlexibleContexts #-}
12{-# LANGUAGE ScopedTypeVariables #-}
12{-# LANGUAGE ScopedTypeVariables #-}
13+{-# OPTIONS_GHC -Wno-orphans #-}
13
14
14module Main
15module Main
15 (
16 (
@@ -17,7 +18,6 @@ module Main
17 ) where
18 ) where
18
19
19import Control.DeepSeq (NFData(..))
20import Control.DeepSeq (NFData(..))
20−import Control.Monad.Catch (MonadCatch, MonadThrow, try, SomeException)
21import Data.Foldable (asum)
21import Data.Foldable (asum)
22import Data.Functor (($>))
22import Data.Functor (($>))
23import Data.Maybe (fromMaybe)
23import Data.Maybe (fromMaybe)
@@ -26,6 +26,7 @@ import GHC.Magic (inline)
26import GHC.Magic (noinline)
26import GHC.Magic (noinline)
27import System.IO (Handle)
27import System.IO (Handle)
28import System.Random (randomRIO)
28import System.Random (randomRIO)
29+import Streamly.Internal.Data.Parser (ParseError(..))
29import Prelude hiding
30import Prelude hiding
30 (any, all, take, sequence, sequence_, sequenceA, takeWhile, dropWhile)
31 (any, all, take, sequence, sequence_, sequenceA, takeWhile, dropWhile)
31
32
@@ -114,7 +115,7 @@ benchIOSink value name f =
114-------------------------------------------------------------------------------
115-------------------------------------------------------------------------------
115
116
116{-# INLINE one #-}
117{-# INLINE one #-}
117−one :: MonadThrow m => Int -> Stream m Int -> m (Maybe Int)
118+one :: Monad m => Int -> Stream m Int -> m (Either ParseError (Maybe Int))
118one value = Stream.parse p
119one value = Stream.parse p
119
120
120 where
121 where
@@ -126,25 +127,25 @@ one value = Stream.parse p
126 Nothing -> pure Nothing
127 Nothing -> pure Nothing
127
128
128{-# INLINE takeBetween #-}
129{-# INLINE takeBetween #-}
129−takeBetween :: MonadThrow m => Int -> Stream m a -> m ()
130+takeBetween :: Monad m => Int -> Stream m a -> m (Either ParseError ())
130−takeBetween value = Stream.parse (PR.takeBetween 0 value Fold.drain)
131+takeBetween value = Stream.parse (PR.takeBetween 0 value Fold.drain)
131
132
132{-# INLINE takeEQ #-}
133{-# INLINE takeEQ #-}
133−takeEQ :: MonadThrow m => Int -> Stream m a -> m ()
134+takeEQ :: Monad m => Int -> Stream m a -> m (Either ParseError ())
134takeEQ value = Stream.parse (PR.takeEQ value Fold.drain)
135takeEQ value = Stream.parse (PR.takeEQ value Fold.drain)
135
136
136{-# INLINE dropWhile #-}
137{-# INLINE dropWhile #-}
137−dropWhile :: MonadThrow m => Int -> Stream m Int -> m ()
138+dropWhile :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
138dropWhile value = Stream.parse (PR.dropWhile (<= value))
139dropWhile value = Stream.parse (PR.dropWhile (<= value))
139
140
140{-# INLINE takeStartBy #-}
141{-# INLINE takeStartBy #-}
141−takeStartBy :: MonadThrow m => Int -> Stream m Int -> m ()
142+takeStartBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
142takeStartBy value stream = do
143takeStartBy value stream = do
143 stream1 <- return . fromMaybe (Stream.fromPure (value + 1)) =<< IsStream.tail stream
144 stream1 <- return . fromMaybe (Stream.fromPure (value + 1)) =<< IsStream.tail stream
144 let stream2 = value `Stream.cons` stream1
145 let stream2 = value `Stream.cons` stream1
145 Stream.parse (PR.takeStartBy (== value) Fold.drain) stream2
146 Stream.parse (PR.takeStartBy (== value) Fold.drain) stream2
146
147
147−takeFramedByEsc_ :: MonadThrow m => Int -> Stream m Char -> m ()
148+takeFramedByEsc_ :: Monad m => Int -> Stream m Char -> m (Either ParseError ())
148takeFramedByEsc_ _ = Stream.parse parser
149takeFramedByEsc_ _ = Stream.parse parser
149
150
150 where
151 where
@@ -156,45 +157,45 @@ takeFramedByEsc_ _ = Stream.parse parser
156 parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
157 parser = PR.takeFramedByEsc_ isEsc isBegin isEnd Fold.drain
157
158
158{-# INLINE listEqBy #-}
159{-# INLINE listEqBy #-}
159−listEqBy :: Int -> Stream IO Int -> IO [Int]
160+listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
160listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
161listEqBy len = Stream.parse (PR.listEqBy (==) [1 .. len])
161
162
162{-# INLINE eqBy #-}
163{-# INLINE eqBy #-}
163−eqBy :: Int -> Stream IO Int -> IO ()
164+eqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
164eqBy len = Stream.parse (PR.eqBy (==) (Stream.enumerateFromTo 1 len))
165eqBy len = Stream.parse (PR.eqBy (==) (Stream.enumerateFromTo 1 len))
165
166
166{-# INLINE takeWhile #-}
167{-# INLINE takeWhile #-}
167−takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
168+takeWhile :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
168takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
169takeWhile value = Stream.parse (PR.takeWhile (<= value) Fold.drain)
169
170
170−takeWhileP :: MonadThrow m => Int -> Stream m Int -> m ()
171+takeWhileP :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
171takeWhileP value =
172takeWhileP value =
172 Stream.parse (PR.takeWhileP (<= value) (PR.takeWhile (<= value - 1) Fold.drain))
173 Stream.parse (PR.takeWhileP (<= value) (PR.takeWhile (<= value - 1) Fold.drain))
173
174
174{-# INLINE takeP #-}
175{-# INLINE takeP #-}
175−takeP :: MonadThrow m => Int -> Stream m a -> m ()
176+takeP :: Monad m => Int -> Stream m a -> m (Either ParseError ())
176takeP value = Stream.parse (PR.takeP value (PR.fromFold Fold.drain))
177takeP value = Stream.parse (PR.takeP value (PR.fromFold Fold.drain))
177
178
178{-# INLINE groupBy #-}
179{-# INLINE groupBy #-}
179−groupBy :: MonadThrow m => Stream m Int -> m ()
180+groupBy :: Monad m => Stream m Int -> m (Either ParseError ())
180groupBy = Stream.parse (PR.groupBy (<=) Fold.drain)
181groupBy = Stream.parse (PR.groupBy (<=) Fold.drain)
181
182
182{-# INLINE groupByRolling #-}
183{-# INLINE groupByRolling #-}
183−groupByRolling :: MonadThrow m => Stream m Int -> m ()
184+groupByRolling :: Monad m => Stream m Int -> m (Either ParseError ())
184groupByRolling = Stream.parse (PR.groupByRolling (<=) Fold.drain)
185groupByRolling = Stream.parse (PR.groupByRolling (<=) Fold.drain)
185
186
186{-# INLINE wordBy #-}
187{-# INLINE wordBy #-}
187−wordBy :: MonadThrow m => Int -> Stream m Int -> m ()
188+wordBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
188wordBy value = Stream.parse (PR.wordBy (>= value) Fold.drain)
189wordBy value = Stream.parse (PR.wordBy (>= value) Fold.drain)
189
190
190{-# INLINE sepByWords #-}
191{-# INLINE sepByWords #-}
191−sepByWords :: MonadThrow m => Int -> Stream m Int -> m ()
192+sepByWords :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
192sepByWords _ = Stream.parse (wrds even Fold.drain)
193sepByWords _ = Stream.parse (wrds even Fold.drain)
193 where
194 where
194 wrds p = PR.sepBy (PR.takeWhile (not . p) Fold.drain) (PR.dropWhile p)
195 wrds p = PR.sepBy (PR.takeWhile (not . p) Fold.drain) (PR.dropWhile p)
195
196
196{-# INLINE deintercalate #-}
197{-# INLINE deintercalate #-}
197−deintercalate :: MonadThrow m => Int -> Stream m Int -> m ()
198+deintercalate :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
198deintercalate _ = Stream.parse (partition even)
199deintercalate _ = Stream.parse (partition even)
199
200
200 where
201 where
@@ -204,37 +205,37 @@ deintercalate _ = Stream.parse (partition even)
204 (PR.takeWhile (not . p) Fold.sum) (PR.takeWhile p Fold.sum) Fold.drain
205 (PR.takeWhile (not . p) Fold.sum) (PR.takeWhile p Fold.sum) Fold.drain
205
206
206{-# INLINE manyWordByEven #-}
207{-# INLINE manyWordByEven #-}
207−manyWordByEven :: MonadThrow m => Stream m Int -> m ()
208+manyWordByEven :: Monad m => Stream m Int -> m (Either ParseError ())
208manyWordByEven = Stream.parse (PR.many (PR.wordBy even Fold.drain) Fold.drain)
209manyWordByEven = Stream.parse (PR.many (PR.wordBy even Fold.drain) Fold.drain)
209
210
210{-# INLINE many #-}
211{-# INLINE many #-}
211−many :: MonadThrow m => Stream m Int -> m Int
212+many :: Monad m => Stream m Int -> m (Either ParseError Int)
212many = Stream.parse (PR.many (PR.satisfy (> 0)) Fold.length)
213many = Stream.parse (PR.many (PR.satisfy (> 0)) Fold.length)
213
214
214{-# INLINE manyAlt #-}
215{-# INLINE manyAlt #-}
215−manyAlt :: MonadThrow m => Stream m Int -> m Int
216+manyAlt :: Monad m => Stream m Int -> m Int
216manyAlt xs = do
217manyAlt xs = do
217 x <- Stream.parse (AP.many (PR.satisfy (> 0))) xs
218 x <- Stream.parse (AP.many (PR.satisfy (> 0))) xs
218 return $ Prelude.length x
219 return $ Prelude.length x
219
220
220{-# INLINE some #-}
221{-# INLINE some #-}
221−some :: MonadThrow m => Stream m Int -> m Int
222+some :: Monad m => Stream m Int -> m (Either ParseError Int)
222some = Stream.parse (PR.some (PR.satisfy (> 0)) Fold.length)
223some = Stream.parse (PR.some (PR.satisfy (> 0)) Fold.length)
223
224
224{-# INLINE someAlt #-}
225{-# INLINE someAlt #-}
225−someAlt :: MonadThrow m => Stream m Int -> m Int
226+someAlt :: Monad m => Stream m Int -> m Int
226someAlt xs = do
227someAlt xs = do
227 x <- Stream.parse (AP.some (PR.satisfy (> 0))) xs
228 x <- Stream.parse (AP.some (PR.satisfy (> 0))) xs
228 return $ Prelude.length x
229 return $ Prelude.length x
229
230
230{-# INLINE manyTill #-}
231{-# INLINE manyTill #-}
231−manyTill :: MonadThrow m => Int -> Stream m Int -> m Int
232+manyTill :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
232manyTill value =
233manyTill value =
233 Stream.parse (PR.manyTill (PR.satisfy (> 0)) (PR.satisfy (== value)) Fold.length)
234 Stream.parse (PR.manyTill (PR.satisfy (> 0)) (PR.satisfy (== value)) Fold.length)
234
235
235{-# INLINE splitAp #-}
236{-# INLINE splitAp #-}
236−splitAp :: MonadThrow m
237+splitAp :: Monad m
237− => Int -> Stream m Int -> m ((), ())
238+ => Int -> Stream m Int -> m (Either ParseError ((), ()))
238splitAp value =
239splitAp value =
239 Stream.parse
240 Stream.parse
240 ((,)
241 ((,)
@@ -243,8 +244,8 @@ splitAp value =
243 )
244 )
244
245
245{-# INLINE splitApBefore #-}
246{-# INLINE splitApBefore #-}
246−splitApBefore :: MonadThrow m
247+splitApBefore :: Monad m
247− => Int -> Stream m Int -> m ()
248+ => Int -> Stream m Int -> m (Either ParseError ())
248splitApBefore value =
249splitApBefore value =
249 Stream.parse
250 Stream.parse
250 ( PR.dropWhile (<= (value `div` 2))
251 ( PR.dropWhile (<= (value `div` 2))
@@ -252,8 +253,8 @@ splitApBefore value =
252 )
253 )
253
254
254{-# INLINE splitApAfter #-}
255{-# INLINE splitApAfter #-}
255−splitApAfter :: MonadThrow m
256+splitApAfter :: Monad m
256− => Int -> Stream m Int -> m ()
257+ => Int -> Stream m Int -> m (Either ParseError ())
257splitApAfter value =
258splitApAfter value =
258 Stream.parse
259 Stream.parse
259 ( PR.dropWhile (<= (value `div` 2))
260 ( PR.dropWhile (<= (value `div` 2))
@@ -261,8 +262,8 @@ splitApAfter value =
261 )
262 )
262
263
263{-# INLINE splitWith #-}
264{-# INLINE splitWith #-}
264−splitWith :: MonadThrow m
265+splitWith :: Monad m
265− => Int -> Stream m Int -> m ((), ())
266+ => Int -> Stream m Int -> m (Either ParseError ((), ()))
266splitWith value =
267splitWith value =
267 Stream.parse
268 Stream.parse
268 (PR.splitWith (,)
269 (PR.splitWith (,)
@@ -271,8 +272,8 @@ splitWith value =
271 )
272 )
272
273
273{-# INLINE split_ #-}
274{-# INLINE split_ #-}
274−split_ :: MonadThrow m
275+split_ :: Monad m
275− => Int -> Stream m Int -> m ()
276+ => Int -> Stream m Int -> m (Either ParseError ())
276split_ value =
277split_ value =
277 Stream.parse
278 Stream.parse
278 (PR.split_
279 (PR.split_
@@ -281,13 +282,13 @@ split_ value =
281 )
282 )
282
283
283{-# INLINE takeEndBy_ #-}
284{-# INLINE takeEndBy_ #-}
284−takeEndBy_ :: MonadThrow m
285+takeEndBy_ :: Monad m
285− => Int -> Stream m Int -> m()
286+ => Int -> Stream m Int -> m (Either ParseError ())
286takeEndBy_ value = Stream.parse (PR.takeEndBy_ (>= value) (PR.fromFold Fold.drain))
287takeEndBy_ value = Stream.parse (PR.takeEndBy_ (>= value) (PR.fromFold Fold.drain))
287
288
288{-
289{-
289{-# INLINE teeAllAny #-}
290{-# INLINE teeAllAny #-}
290−teeAllAny :: MonadThrow m
291+teeAllAny :: Monad m
291 => Int -> Stream m Int -> m ((), ())
292 => Int -> Stream m Int -> m ((), ())
292teeAllAny value =
293teeAllAny value =
293 Stream.parse
294 Stream.parse
@@ -297,7 +298,7 @@ teeAllAny value =
297 )
298 )
298
299
299{-# INLINE teeFstAllAny #-}
300{-# INLINE teeFstAllAny #-}
300−teeFstAllAny :: MonadThrow m
301+teeFstAllAny :: Monad m
301 => Int -> Stream m Int -> m ((), ())
302 => Int -> Stream m Int -> m ((), ())
302teeFstAllAny value =
303teeFstAllAny value =
303 Stream.parse
304 Stream.parse
@@ -307,8 +308,8 @@ teeFstAllAny value =
307 )
308 )
308
309
309{-# INLINE shortestAllAny #-}
310{-# INLINE shortestAllAny #-}
310−shortestAllAny :: MonadThrow m
311+shortestAllAny :: Monad m
311− => Int -> Stream m Int -> m ()
312+ => Int -> Stream m Int -> m (Either ParseError ())
312shortestAllAny value =
313shortestAllAny value =
313 Stream.parse
314 Stream.parse
314 (PR.shortest
315 (PR.shortest
@@ -317,8 +318,8 @@ shortestAllAny value =
317 )
318 )
318
319
319{-# INLINE longestAllAny #-}
320{-# INLINE longestAllAny #-}
320−longestAllAny :: MonadThrow m
321+longestAllAny :: Monad m
321− => Int -> Stream m Int -> m ()
322+ => Int -> Stream m Int -> m (Either ParseError ())
322longestAllAny value =
323longestAllAny value =
323 Stream.parse
324 Stream.parse
324 (PR.longest
325 (PR.longest
@@ -359,41 +360,41 @@ parseManyUnfoldArrays count arrays = do
359-- not have to rely on it.
360-- not have to rely on it.
360--
361--
361{-# INLINE lookAhead #-}
362{-# INLINE lookAhead #-}
362−lookAhead :: MonadThrow m => Int -> Stream m Int -> m ()
363+lookAhead :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
363lookAhead value =
364lookAhead value =
364 Stream.parse (PR.lookAhead (PR.takeWhile (<= value) Fold.drain) $> ())
365 Stream.parse (PR.lookAhead (PR.takeWhile (<= value) Fold.drain) $> ())
365
366
366{-# INLINE sequenceA #-}
367{-# INLINE sequenceA #-}
367−sequenceA :: MonadThrow m => Int -> Stream m Int -> m Int
368+sequenceA :: Monad m => Int -> Stream m Int -> m Int
368sequenceA value xs = do
369sequenceA value xs = do
369 x <- Stream.parse (TR.sequenceA (replicate value (PR.satisfy (> 0)))) xs
370 x <- Stream.parse (TR.sequenceA (replicate value (PR.satisfy (> 0)))) xs
370 return $ length x
371 return $ length x
371
372
372{-# INLINE sequenceA_ #-}
373{-# INLINE sequenceA_ #-}
373−sequenceA_ :: MonadThrow m => Int -> Stream m Int -> m ()
374+sequenceA_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
374sequenceA_ value =
375sequenceA_ value =
375 Stream.parse (F.sequenceA_ $ replicate value (PR.satisfy (> 0)))
376 Stream.parse (F.sequenceA_ $ replicate value (PR.satisfy (> 0)))
376
377
377{-# INLINE sequence #-}
378{-# INLINE sequence #-}
378−sequence :: MonadThrow m => Int -> Stream m Int -> m Int
379+sequence :: Monad m => Int -> Stream m Int -> m Int
379sequence value xs = do
380sequence value xs = do
380 x <- Stream.parse (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
381 x <- Stream.parse (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
381 return $ length x
382 return $ length x
382
383
383{-# INLINE sequence_ #-}
384{-# INLINE sequence_ #-}
384−sequence_ :: MonadThrow m => Int -> Stream m Int -> m ()
385+sequence_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
385sequence_ value =
386sequence_ value =
386 Stream.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
387 Stream.parse (F.sequence_ $ replicate value (PR.satisfy (> 0)))
387
388
388{-# INLINE choiceAsum #-}
389{-# INLINE choiceAsum #-}
389−choiceAsum :: MonadThrow m => Int -> Stream m Int -> m Int
390+choiceAsum :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
390choiceAsum value =
391choiceAsum value =
391 Stream.parse (asum (replicate value (PR.satisfy (< 0)))
392 Stream.parse (asum (replicate value (PR.satisfy (< 0)))
392 AP.<|> PR.satisfy (> 0))
393 AP.<|> PR.satisfy (> 0))
393
394
394{-
395{-
395{-# INLINE choice #-}
396{-# INLINE choice #-}
396−choice :: MonadThrow m => Int -> Stream m Int -> m Int
397+choice :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
397choice value =
398choice value =
398 Stream.parse
399 Stream.parse
399 (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
400 (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
@@ -404,35 +405,38 @@ choice value =
404-------------------------------------------------------------------------------
405-------------------------------------------------------------------------------
405
406
406{-# INLINE parseMany #-}
407{-# INLINE parseMany #-}
407−parseMany :: MonadThrow m => Int -> Stream m Int -> m ()
408+parseMany :: Monad m => Int -> Stream m Int -> m ()
408parseMany n =
409parseMany n =
409 Stream.fold Fold.drain
410 Stream.fold Fold.drain
410 . fmap getSum
411 . fmap getSum
411− . Stream.parseMany (PR.fromFold $ Fold.take n Fold.mconcat)
412+ . Stream.rights . Stream.parseMany (PR.fromFold $ Fold.take n Fold.mconcat)
412 . fmap Sum
413 . fmap Sum
413
414
414{-# INLINE parseIterate #-}
415{-# INLINE parseIterate #-}
415−parseIterate :: MonadThrow m => Int -> Stream m Int -> m ()
416+parseIterate :: Monad m => Int -> Stream m Int -> m ()
416parseIterate n =
417parseIterate n =
417 Stream.fold Fold.drain
418 Stream.fold Fold.drain
418 . fmap getSum
419 . fmap getSum
420+ . Stream.rights
421+ . Stream.parseIterate
419− . Stream.parseIterate (PR.fromFold . Fold.take n . Fold.sconcat) (Sum 0)
422+ (PR.fromFold . Fold.take n . Fold.sconcat)
423+ (Sum 0)
420 . fmap Sum
424 . fmap Sum
421
425
422{-# INLINE parseBreak #-}
426{-# INLINE parseBreak #-}
423−parseBreak :: MonadCatch m => Stream m Int -> m ()
427+parseBreak :: Monad m => Stream m Int -> m ()
424parseBreak s = do
428parseBreak s = do
425− r <- try $ Stream.parseBreak PR.one s
429+ r <- Stream.parseBreak PR.one s
426 case r of
430 case r of
427− Left (_ :: SomeException) -> return ()
431+ (Left _, _) -> return ()
428− Right (_, s1) -> parseBreak s1
432+ (Right _, s1) -> parseBreak s1
429
433
430{-# INLINE concatSequence #-}
434{-# INLINE concatSequence #-}
431−concatSequence :: MonadThrow m => Stream m Int -> m ()
435+concatSequence :: Monad m => Stream m Int -> m (Either ParseError ())
432concatSequence = Stream.parse $ PR.concatSequence Fold.drain $ Stream.repeat PR.one
436concatSequence = Stream.parse $ PR.concatSequence Fold.drain $ Stream.repeat PR.one
433
437
434{-# INLINE parseManyGroupBy #-}
438{-# INLINE parseManyGroupBy #-}
435−parseManyGroupBy :: MonadThrow m => (Int -> Int -> Bool) -> Stream m Int -> m ()
439+parseManyGroupBy :: Monad m => (Int -> Int -> Bool) -> Stream m Int -> m ()
436parseManyGroupBy cmp =
440parseManyGroupBy cmp =
437 Stream.fold Fold.drain . Stream.parseMany (PR.groupBy cmp Fold.drain)
441 Stream.fold Fold.drain . Stream.parseMany (PR.groupBy cmp Fold.drain)
438
442
@@ -443,6 +447,10 @@ parseManyGroupBy cmp =
443moduleName :: String
447moduleName :: String
444moduleName = "Data.Parser"
448moduleName = "Data.Parser"
445
449
450+instance NFData ParseError where
451+ {-# INLINE rnf #-}
452+ rnf (ParseError x) = rnf x
453+
446o_1_space_serial :: Int -> [Benchmark]
454o_1_space_serial :: Int -> [Benchmark]
447o_1_space_serial value =
455o_1_space_serial value =
448 [ benchIOSink value "one (fold)" $ one value
456 [ benchIOSink value "one (fold)" $ one value
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+45−40
@@ -10,6 +10,7 @@
10{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE FlexibleContexts #-}
11{-# LANGUAGE ScopedTypeVariables #-}
11{-# LANGUAGE ScopedTypeVariables #-}
12{-# OPTIONS_GHC -fspec-constr-recursive=4 #-}
12{-# OPTIONS_GHC -fspec-constr-recursive=4 #-}
13+{-# OPTIONS_GHC -Wno-orphans #-}
13
14
14module Main
15module Main
15 (
16 (
@@ -17,12 +18,12 @@ module Main
17 ) where
18 ) where
18
19
19import Control.DeepSeq (NFData(..))
20import Control.DeepSeq (NFData(..))
20−import Control.Monad.Catch (MonadThrow)
21import Data.Foldable (asum)
21import Data.Foldable (asum)
22import Data.Function ((&))
22import Data.Function ((&))
23import Data.Functor (($>))
23import Data.Functor (($>))
24import Data.Maybe (fromMaybe)
24import Data.Maybe (fromMaybe)
25import Streamly.Internal.Data.Stream (Stream)
25import Streamly.Internal.Data.Stream (Stream)
26+import Streamly.Internal.Data.Parser (ParseError(..))
26import System.Random (randomRIO)
27import System.Random (randomRIO)
27import Prelude hiding (any, all, take, sequence, sequenceA, sequence_, takeWhile, span)
28import Prelude hiding (any, all, take, sequence, sequenceA, sequence_, takeWhile, span)
28
29
@@ -69,87 +70,87 @@ benchIOSink value name f =
69-------------------------------------------------------------------------------
70-------------------------------------------------------------------------------
70
71
71{-# INLINE listEqBy #-}
72{-# INLINE listEqBy #-}
72−listEqBy :: Int -> Stream IO Int -> IO [Int]
73+listEqBy :: Int -> Stream IO Int -> IO (Either ParseError [Int])
73listEqBy len = Stream.parseD (PR.listEqBy (==) [1 .. len])
74listEqBy len = Stream.parseD (PR.listEqBy (==) [1 .. len])
74
75
75{-# INLINE eqBy #-}
76{-# INLINE eqBy #-}
76−eqBy :: Int -> Stream IO Int -> IO ()
77+eqBy :: Int -> Stream IO Int -> IO (Either ParseError ())
77eqBy len = Stream.parseD (PR.eqBy (==) (D.enumerateFromToIntegral 1 len))
78eqBy len = Stream.parseD (PR.eqBy (==) (D.enumerateFromToIntegral 1 len))
78
79
79{-# INLINE drainWhile #-}
80{-# INLINE drainWhile #-}
80−drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser a m ()
81+drainWhile :: Monad m => (a -> Bool) -> PR.Parser a m ()
81drainWhile p = PR.takeWhile p Fold.drain
82drainWhile p = PR.takeWhile p Fold.drain
82
83
83{-# INLINE takeStartBy #-}
84{-# INLINE takeStartBy #-}
84−takeStartBy :: MonadThrow m => Int -> Stream m Int -> m ()
85+takeStartBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
85takeStartBy value stream = do
86takeStartBy value stream = do
86 stream1 <- return . fromMaybe (Stream.fromPure (value + 1)) =<< IsStream.tail stream
87 stream1 <- return . fromMaybe (Stream.fromPure (value + 1)) =<< IsStream.tail stream
87 let stream2 = value `Stream.cons` stream1
88 let stream2 = value `Stream.cons` stream1
88 Stream.parseD (PR.takeStartBy (== value) Fold.drain) stream2
89 Stream.parseD (PR.takeStartBy (== value) Fold.drain) stream2
89
90
90{-# INLINE takeWhile #-}
91{-# INLINE takeWhile #-}
91−takeWhile :: MonadThrow m => Int -> Stream m Int -> m ()
92+takeWhile :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
92takeWhile value = Stream.parseD (drainWhile (<= value))
93takeWhile value = Stream.parseD (drainWhile (<= value))
93
94
94{-# INLINE takeP #-}
95{-# INLINE takeP #-}
95−takeP :: MonadThrow m => Int -> Stream m a -> m ()
96+takeP :: Monad m => Int -> Stream m a -> m (Either ParseError ())
96takeP value = Stream.parseD (PR.takeP value (PR.fromFold Fold.drain))
97takeP value = Stream.parseD (PR.takeP value (PR.fromFold Fold.drain))
97
98
98{-# INLINE takeBetween #-}
99{-# INLINE takeBetween #-}
99−takeBetween :: MonadThrow m => Int -> Stream m a -> m ()
100+takeBetween :: Monad m => Int -> Stream m a -> m (Either ParseError ())
100takeBetween value = Stream.parseD (PR.takeBetween 0 value Fold.drain)
101takeBetween value = Stream.parseD (PR.takeBetween 0 value Fold.drain)
101
102
102{-# INLINE groupBy #-}
103{-# INLINE groupBy #-}
103−groupBy :: MonadThrow m => Stream m Int -> m ()
104+groupBy :: Monad m => Stream m Int -> m (Either ParseError ())
104groupBy = Stream.parseD (PR.groupBy (<=) Fold.drain)
105groupBy = Stream.parseD (PR.groupBy (<=) Fold.drain)
105
106
106{-# INLINE groupByRolling #-}
107{-# INLINE groupByRolling #-}
107−groupByRolling :: MonadThrow m => Stream m Int -> m ()
108+groupByRolling :: Monad m => Stream m Int -> m (Either ParseError ())
108groupByRolling = Stream.parseD (PR.groupByRolling (<=) Fold.drain)
109groupByRolling = Stream.parseD (PR.groupByRolling (<=) Fold.drain)
109
110
110{-# INLINE wordBy #-}
111{-# INLINE wordBy #-}
111−wordBy :: MonadThrow m => Int -> Stream m Int -> m ()
112+wordBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
112wordBy value = Stream.parseD (PR.wordBy (>= value) Fold.drain)
113wordBy value = Stream.parseD (PR.wordBy (>= value) Fold.drain)
113
114
114{-# INLINE manyWordByEven #-}
115{-# INLINE manyWordByEven #-}
115−manyWordByEven :: MonadThrow m => Stream m Int -> m ()
116+manyWordByEven :: Monad m => Stream m Int -> m (Either ParseError ())
116manyWordByEven =
117manyWordByEven =
117 Stream.parseD (PR.many (PR.wordBy (Prelude.even) Fold.drain) Fold.drain)
118 Stream.parseD (PR.many (PR.wordBy (Prelude.even) Fold.drain) Fold.drain)
118
119
119{-# INLINE many #-}
120{-# INLINE many #-}
120−many :: MonadThrow m => Stream m Int -> m Int
121+many :: Monad m => Stream m Int -> m (Either ParseError Int)
121many = Stream.parseD (PR.many (PR.satisfy (> 0)) Fold.length)
122many = Stream.parseD (PR.many (PR.satisfy (> 0)) Fold.length)
122
123
123{-# INLINE manyAlt #-}
124{-# INLINE manyAlt #-}
124−manyAlt :: MonadThrow m => Stream m Int -> m Int
125+manyAlt :: Monad m => Stream m Int -> m Int
125manyAlt xs = do
126manyAlt xs = do
126 x <- Stream.parseD (AP.many (PR.satisfy (> 0))) xs
127 x <- Stream.parseD (AP.many (PR.satisfy (> 0))) xs
127 return $ Prelude.length x
128 return $ Prelude.length x
128
129
129{-# INLINE some #-}
130{-# INLINE some #-}
130−some :: MonadThrow m => Stream m Int -> m Int
131+some :: Monad m => Stream m Int -> m (Either ParseError Int)
131some = Stream.parseD (PR.some (PR.satisfy (> 0)) Fold.length)
132some = Stream.parseD (PR.some (PR.satisfy (> 0)) Fold.length)
132
133
133{-# INLINE someAlt #-}
134{-# INLINE someAlt #-}
134−someAlt :: MonadThrow m => Stream m Int -> m Int
135+someAlt :: Monad m => Stream m Int -> m Int
135someAlt xs = do
136someAlt xs = do
136 x <- Stream.parseD (AP.some (PR.satisfy (> 0))) xs
137 x <- Stream.parseD (AP.some (PR.satisfy (> 0))) xs
137 return $ Prelude.length x
138 return $ Prelude.length x
138
139
139{-#INLINE takeEndBy_ #-}
140{-#INLINE takeEndBy_ #-}
140−takeEndBy_ :: MonadThrow m => Int -> Stream m Int -> m ()
141+takeEndBy_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
141takeEndBy_ value = Stream.parseD (PR.takeEndBy_ (>= value) (PR.fromFold Fold.drain))
142takeEndBy_ value = Stream.parseD (PR.takeEndBy_ (>= value) (PR.fromFold Fold.drain))
142
143
143{-# INLINE manyTill #-}
144{-# INLINE manyTill #-}
144−manyTill :: MonadThrow m => Int -> Stream m Int -> m Int
145+manyTill :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
145manyTill value =
146manyTill value =
146 let p = PR.satisfy (> 0)
147 let p = PR.satisfy (> 0)
147 pcond = PR.satisfy (== value)
148 pcond = PR.satisfy (== value)
148 in Stream.parseD (PR.manyTill p pcond Fold.length)
149 in Stream.parseD (PR.manyTill p pcond Fold.length)
149
150
150{-# INLINE splitWith #-}
151{-# INLINE splitWith #-}
151−splitWith :: MonadThrow m
152+splitWith :: Monad m
152− => Int -> Stream m Int -> m ((), ())
153+ => Int -> Stream m Int -> m (Either ParseError ((), ()))
153splitWith value =
154splitWith value =
154 Stream.parseD
155 Stream.parseD
155 ((,)
156 ((,)
@@ -159,7 +160,7 @@ splitWith value =
159
160
160{-
161{-
161{-# INLINE teeAllAny #-}
162{-# INLINE teeAllAny #-}
162−teeAllAny :: MonadThrow m
163+teeAllAny :: Monad m
163 => Int -> Stream m Int -> m ((), ())
164 => Int -> Stream m Int -> m ((), ())
164teeAllAny value =
165teeAllAny value =
165 Stream.parseD
166 Stream.parseD
@@ -169,7 +170,7 @@ teeAllAny value =
169 )
170 )
170
171
171{-# INLINE teeFstAllAny #-}
172{-# INLINE teeFstAllAny #-}
172−teeFstAllAny :: MonadThrow m
173+teeFstAllAny :: Monad m
173 => Int -> Stream m Int -> m ((), ())
174 => Int -> Stream m Int -> m ((), ())
174teeFstAllAny value =
175teeFstAllAny value =
175 Stream.parseD
176 Stream.parseD
@@ -179,7 +180,7 @@ teeFstAllAny value =
179 )
180 )
180
181
181{-# INLINE shortestAllAny #-}
182{-# INLINE shortestAllAny #-}
182−shortestAllAny :: MonadThrow m
183+shortestAllAny :: Monad m
183 => Int -> Stream m Int -> m ()
184 => Int -> Stream m Int -> m ()
184shortestAllAny value =
185shortestAllAny value =
185 Stream.parseD
186 Stream.parseD
@@ -189,7 +190,7 @@ shortestAllAny value =
189 )
190 )
190
191
191{-# INLINE longestAllAny #-}
192{-# INLINE longestAllAny #-}
192−longestAllAny :: MonadThrow m
193+longestAllAny :: Monad m
193 => Int -> Stream m Int -> m ()
194 => Int -> Stream m Int -> m ()
194longestAllAny value =
195longestAllAny value =
195 Stream.parseD
196 Stream.parseD
@@ -200,7 +201,7 @@ longestAllAny value =
200-}
201-}
201
202
202{-# INLINE sequenceParser #-}
203{-# INLINE sequenceParser #-}
203−sequenceParser :: MonadThrow m => Stream m Int -> m ()
204+sequenceParser :: Monad m => Stream m Int -> m (Either ParseError ())
204sequenceParser = Stream.parseD (PR.sequence (D.repeat (PR.satisfy $ const True)) Fold.drain)
205sequenceParser = Stream.parseD (PR.sequence (D.repeat (PR.satisfy $ const True)) Fold.drain)
205
206
206-------------------------------------------------------------------------------
207-------------------------------------------------------------------------------
@@ -208,16 +209,16 @@ sequenceParser = Stream.parseD (PR.sequence (D.repeat (PR.satisfy $ const True))
208-------------------------------------------------------------------------------
209-------------------------------------------------------------------------------
209
210
210{-# INLINE span #-}
211{-# INLINE span #-}
211−span :: MonadThrow m => Int -> Stream m Int -> m ((), ())
212+span :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
212span value = Stream.parseD (PR.span (<= (value `div` 2)) Fold.drain Fold.drain)
213span value = Stream.parseD (PR.span (<= (value `div` 2)) Fold.drain Fold.drain)
213
214
214{-# INLINE spanBy #-}
215{-# INLINE spanBy #-}
215−spanBy :: MonadThrow m => Int -> Stream m Int -> m ((), ())
216+spanBy :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
216spanBy value =
217spanBy value =
217 Stream.parseD (PR.spanBy (\_ i -> i <= (value `div` 2)) Fold.drain Fold.drain)
218 Stream.parseD (PR.spanBy (\_ i -> i <= (value `div` 2)) Fold.drain Fold.drain)
218
219
219{-# INLINE spanByRolling #-}
220{-# INLINE spanByRolling #-}
220−spanByRolling :: MonadThrow m => Int -> Stream m Int -> m ((), ())
221+spanByRolling :: Monad m => Int -> Stream m Int -> m (Either ParseError ((), ()))
221spanByRolling value =
222spanByRolling value =
222 Stream.parseD (PR.spanByRolling (\_ i -> i <= value `div` 2) Fold.drain Fold.drain)
223 Stream.parseD (PR.spanByRolling (\_ i -> i <= value `div` 2) Fold.drain Fold.drain)
223
224
@@ -226,20 +227,20 @@ spanByRolling value =
226-------------------------------------------------------------------------------
227-------------------------------------------------------------------------------
227
228
228{-# INLINE parseMany #-}
229{-# INLINE parseMany #-}
229−parseMany :: MonadThrow m => Int -> Stream m Int -> m ()
230+parseMany :: Monad m => Int -> Stream m Int -> m ()
230parseMany value = Stream.fold Fold.drain . Stream.parseManyD (drainWhile (<= value + 1))
231parseMany value = Stream.fold Fold.drain . Stream.parseManyD (drainWhile (<= value + 1))
231
232
232{-# INLINE parseManyGroups #-}
233{-# INLINE parseManyGroups #-}
233−parseManyGroups :: MonadThrow m => Bool -> Stream m Int -> m ()
234+parseManyGroups :: Monad m => Bool -> Stream m Int -> m ()
234parseManyGroups b = Stream.fold Fold.drain . Stream.parseManyD (PR.groupBy (\_ _ -> b) Fold.drain)
235parseManyGroups b = Stream.fold Fold.drain . Stream.parseManyD (PR.groupBy (\_ _ -> b) Fold.drain)
235
236
236{-# INLINE parseManyGroupsRolling #-}
237{-# INLINE parseManyGroupsRolling #-}
237−parseManyGroupsRolling :: MonadThrow m => Bool -> Stream m Int -> m ()
238+parseManyGroupsRolling :: Monad m => Bool -> Stream m Int -> m ()
238parseManyGroupsRolling b =
239parseManyGroupsRolling b =
239 Stream.fold Fold.drain . Stream.parseManyD (PR.groupByRolling (\_ _ -> b) Fold.drain)
240 Stream.fold Fold.drain . Stream.parseManyD (PR.groupByRolling (\_ _ -> b) Fold.drain)
240
241
241{-# INLINE parseManyGroupsRollingEither #-}
242{-# INLINE parseManyGroupsRollingEither #-}
242−parseManyGroupsRollingEither :: MonadThrow m =>
243+parseManyGroupsRollingEither :: Monad m =>
243 (Int -> Int -> Bool) -> Int -> m ()
244 (Int -> Int -> Bool) -> Int -> m ()
244parseManyGroupsRollingEither cmp value = do
245parseManyGroupsRollingEither cmp value = do
245 sourceUnfoldrM value 1
246 sourceUnfoldrM value 1
@@ -247,7 +248,7 @@ parseManyGroupsRollingEither cmp value = do
247 & Stream.fold Fold.drain
248 & Stream.fold Fold.drain
248
249
249{-# INLINE parseManyGroupsRollingEitherAlt #-}
250{-# INLINE parseManyGroupsRollingEitherAlt #-}
250−parseManyGroupsRollingEitherAlt :: MonadThrow m =>
251+parseManyGroupsRollingEitherAlt :: Monad m =>
251 (Int -> Int -> Bool) -> Int -> m ()
252 (Int -> Int -> Bool) -> Int -> m ()
252parseManyGroupsRollingEitherAlt cmp value = do
253parseManyGroupsRollingEitherAlt cmp value = do
253 sourceUnfoldrM value 1
254 sourceUnfoldrM value 1
@@ -281,32 +282,32 @@ parseManyUnfoldArrays count arrays = do
281-- not have to rely on it.
282-- not have to rely on it.
282--
283--
283{-# INLINE lookAhead #-}
284{-# INLINE lookAhead #-}
284−lookAhead :: MonadThrow m => Int -> Stream m Int -> m ()
285+lookAhead :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
285lookAhead value =
286lookAhead value =
286 Stream.parseD (PR.lookAhead (PR.takeWhile (<= value) Fold.drain) $> ())
287 Stream.parseD (PR.lookAhead (PR.takeWhile (<= value) Fold.drain) $> ())
287
288
288-- XXX The timing of this increased 3x after the stepify extract changes.
289-- XXX The timing of this increased 3x after the stepify extract changes.
289{-# INLINE sequenceA_ #-}
290{-# INLINE sequenceA_ #-}
290−sequenceA_ :: MonadThrow m => Int -> Stream m Int -> m ()
291+sequenceA_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
291sequenceA_ value =
292sequenceA_ value =
292 Stream.parseD (F.sequenceA_ $ replicate value (PR.satisfy (> 0)))
293 Stream.parseD (F.sequenceA_ $ replicate value (PR.satisfy (> 0)))
293
294
294-- quadratic complexity
295-- quadratic complexity
295{-# INLINE sequenceA #-}
296{-# INLINE sequenceA #-}
296−sequenceA :: MonadThrow m => Int -> Stream m Int -> m Int
297+sequenceA :: Monad m => Int -> Stream m Int -> m Int
297sequenceA value xs = do
298sequenceA value xs = do
298 x <- Stream.parseD (TR.sequenceA (replicate value (PR.satisfy (> 0)))) xs
299 x <- Stream.parseD (TR.sequenceA (replicate value (PR.satisfy (> 0)))) xs
299 return $ length x
300 return $ length x
300
301
301-- quadratic complexity
302-- quadratic complexity
302{-# INLINE sequence #-}
303{-# INLINE sequence #-}
303−sequence :: MonadThrow m => Int -> Stream m Int -> m Int
304+sequence :: Monad m => Int -> Stream m Int -> m Int
304sequence value xs = do
305sequence value xs = do
305 x <- Stream.parseD (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
306 x <- Stream.parseD (TR.sequence (replicate value (PR.satisfy (> 0)))) xs
306 return $ length x
307 return $ length x
307
308
308{-# INLINE sequence_ #-}
309{-# INLINE sequence_ #-}
309−sequence_ :: MonadThrow m => Int -> Stream m Int -> m ()
310+sequence_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
310sequence_ value xs =
311sequence_ value xs =
311 Stream.parseD (foldr f (return ()) (replicate value (PR.takeBetween 0 1 Fold.drain))) xs
312 Stream.parseD (foldr f (return ()) (replicate value (PR.takeBetween 0 1 Fold.drain))) xs
312
313
@@ -319,14 +320,14 @@ sequence_ value xs =
319-- quadratic performance complexity.
320-- quadratic performance complexity.
320--
321--
321{-# INLINE choiceAsum #-}
322{-# INLINE choiceAsum #-}
322−choiceAsum :: MonadThrow m => Int -> Stream m Int -> m Int
323+choiceAsum :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
323choiceAsum value =
324choiceAsum value =
324 Stream.parseD (asum (replicate value (PR.satisfy (< 0)))
325 Stream.parseD (asum (replicate value (PR.satisfy (< 0)))
325 AP.<|> PR.satisfy (> 0))
326 AP.<|> PR.satisfy (> 0))
326
327
327{-
328{-
328{-# INLINE choice #-}
329{-# INLINE choice #-}
329−choice :: MonadThrow m => Int -> Stream m Int -> m Int
330+choice :: Monad m => Int -> Stream m Int -> m Int
330choice value =
331choice value =
331 Stream.parseD
332 Stream.parseD
332 (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
333 (PR.choice (replicate value (PR.satisfy (< 0))) AP.<|> PR.satisfy (> 0))
@@ -339,6 +340,10 @@ choice value =
339moduleName :: String
340moduleName :: String
340moduleName = "Data.Parser.ParserD"
341moduleName = "Data.Parser.ParserD"
341
342
343+instance NFData ParseError where
344+ {-# INLINE rnf #-}
345+ rnf (ParseError x) = rnf x
346+
342o_1_space_serial :: Int -> [Benchmark]
347o_1_space_serial :: Int -> [Benchmark]
343o_1_space_serial value =
348o_1_space_serial value =
344 [ benchIOSink value "takeWhile" $ takeWhile value
349 [ benchIOSink value "takeWhile" $ takeWhile value
benchmark/Streamly/Benchmark/Data/Parser/ParserK.hs+15−10
@@ -7,6 +7,7 @@
7
7
8{-# LANGUAGE CPP #-}
8{-# LANGUAGE CPP #-}
9{-# LANGUAGE FlexibleContexts #-}
9{-# LANGUAGE FlexibleContexts #-}
10+{-# OPTIONS_GHC -Wno-orphans #-}
10
11
11module Main
12module Main
12 (
13 (
@@ -14,8 +15,8 @@ module Main
14 ) where
15 ) where
15
16
16import Control.DeepSeq (NFData(..))
17import Control.DeepSeq (NFData(..))
17−import Control.Monad.Catch (MonadThrow)
18import Data.Foldable (asum)
18import Data.Foldable (asum)
19+import Streamly.Internal.Data.Parser (ParseError(..))
19import Streamly.Internal.Data.Stream (Stream)
20import Streamly.Internal.Data.Stream (Stream)
20import System.Random (randomRIO)
21import System.Random (randomRIO)
21import Prelude hiding (any, all, take, sequence, sequenceA, takeWhile)
22import Prelude hiding (any, all, take, sequence, sequenceA, takeWhile)
@@ -73,17 +74,17 @@ takeWhile :: Monad m => (a -> Bool) -> PR.Parser a m ()
73takeWhile p = PRD.toParserK $ PRD.takeWhile p FL.drain
74takeWhile p = PRD.toParserK $ PRD.takeWhile p FL.drain
74
75
75{-# INLINE takeWhileK #-}
76{-# INLINE takeWhileK #-}
76−takeWhileK :: MonadThrow m => Int -> Stream m Int -> m ()
77+takeWhileK :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
77takeWhileK value = PARSE_OP (takeWhile (<= value))
78takeWhileK value = PARSE_OP (takeWhile (<= value))
78
79
79{-# INLINE splitApp #-}
80{-# INLINE splitApp #-}
80−splitApp :: MonadThrow m
81+splitApp :: Monad m
81− => Int -> Stream m Int -> m ((), ())
82+ => Int -> Stream m Int -> m (Either ParseError ((), ()))
82splitApp value =
83splitApp value =
83 PARSE_OP ((,) <$> takeWhile (<= (value `div` 2)) <*> takeWhile (<= value))
84 PARSE_OP ((,) <$> takeWhile (<= (value `div` 2)) <*> takeWhile (<= value))
84
85
85{-# INLINE sequenceA #-}
86{-# INLINE sequenceA #-}
86−sequenceA :: MonadThrow m => Int -> Stream m Int -> m Int
87+sequenceA :: Monad m => Int -> Stream m Int -> m Int
87sequenceA value xs = do
88sequenceA value xs = do
88 let parser = satisfy (> 0)
89 let parser = satisfy (> 0)
89 list = Prelude.replicate value parser
90 list = Prelude.replicate value parser
@@ -91,14 +92,14 @@ sequenceA value xs = do
91 return $ Prelude.length x
92 return $ Prelude.length x
92
93
93{-# INLINE sequenceA_ #-}
94{-# INLINE sequenceA_ #-}
94−sequenceA_ :: MonadThrow m => Int -> Stream m Int -> m ()
95+sequenceA_ :: Monad m => Int -> Stream m Int -> m (Either ParseError ())
95sequenceA_ value xs = do
96sequenceA_ value xs = do
96 let parser = satisfy (> 0)
97 let parser = satisfy (> 0)
97 list = Prelude.replicate value parser
98 list = Prelude.replicate value parser
98 PARSE_OP (F.sequenceA_ list) xs
99 PARSE_OP (F.sequenceA_ list) xs
99
100
100{-# INLINE sequence #-}
101{-# INLINE sequence #-}
101−sequence :: MonadThrow m => Int -> Stream m Int -> m Int
102+sequence :: Monad m => Int -> Stream m Int -> m Int
102sequence value xs = do
103sequence value xs = do
103 let parser = satisfy (> 0)
104 let parser = satisfy (> 0)
104 list = Prelude.replicate value parser
105 list = Prelude.replicate value parser
@@ -106,19 +107,19 @@ sequence value xs = do
106 return $ Prelude.length x
107 return $ Prelude.length x
107
108
108{-# INLINE manyAlt #-}
109{-# INLINE manyAlt #-}
109−manyAlt :: MonadThrow m => Stream m Int -> m Int
110+manyAlt :: Monad m => Stream m Int -> m Int
110manyAlt xs = do
111manyAlt xs = do
111 x <- PARSE_OP (AP.many (satisfy (> 0))) xs
112 x <- PARSE_OP (AP.many (satisfy (> 0))) xs
112 return $ Prelude.length x
113 return $ Prelude.length x
113
114
114{-# INLINE someAlt #-}
115{-# INLINE someAlt #-}
115−someAlt :: MonadThrow m => Stream m Int -> m Int
116+someAlt :: Monad m => Stream m Int -> m Int
116someAlt xs = do
117someAlt xs = do
117 x <- PARSE_OP (AP.some (satisfy (> 0))) xs
118 x <- PARSE_OP (AP.some (satisfy (> 0))) xs
118 return $ Prelude.length x
119 return $ Prelude.length x
119
120
120{-# INLINE choice #-}
121{-# INLINE choice #-}
121−choice :: MonadThrow m => Int -> Stream m Int -> m Int
122+choice :: Monad m => Int -> Stream m Int -> m (Either ParseError Int)
122choice value =
123choice value =
123 PARSE_OP (asum (replicate value (satisfy (< 0)))
124 PARSE_OP (asum (replicate value (satisfy (< 0)))
124 AP.<|> satisfy (> 0))
125 AP.<|> satisfy (> 0))
@@ -130,6 +131,10 @@ choice value =
130moduleName :: String
131moduleName :: String
131moduleName = "Data.Parser.ParserK"
132moduleName = "Data.Parser.ParserK"
132
133
134+instance NFData ParseError where
135+ {-# INLINE rnf #-}
136+ rnf (ParseError x) = rnf x
137+
133o_1_space_serial :: Int -> [Benchmark]
138o_1_space_serial :: Int -> [Benchmark]
134o_1_space_serial value =
139o_1_space_serial value =
135 [ benchIOSink value "takeWhile" $ takeWhileK value
140 [ benchIOSink value "takeWhile" $ takeWhileK value
benchmark/Streamly/Benchmark/Unicode/Stream.hs+1−1
@@ -280,7 +280,7 @@ _copyStreamUtf8Parser :: Handle -> Handle -> IO ()
280_copyStreamUtf8Parser inh outh =
280_copyStreamUtf8Parser inh outh =
281 Stream.fold (Handle.write outh)
281 Stream.fold (Handle.write outh)
282 $ Unicode.encodeUtf8
282 $ Unicode.encodeUtf8
283− $ Stream.parseMany
283+ $ Stream.rights $ Stream.parseMany
284 (Unicode.parseCharUtf8With Unicode.TransliterateCodingFailure)
284 (Unicode.parseCharUtf8With Unicode.TransliterateCodingFailure)
285 $ Stream.unfold Handle.reader inh
285 $ Stream.unfold Handle.reader inh
286
286
core/src/Streamly/Internal/Data/Array/Mut/Stream.hs+3−3
@@ -29,11 +29,11 @@ where
29
29
30import Control.Monad.IO.Class (MonadIO(..))
30import Control.Monad.IO.Class (MonadIO(..))
31import Control.Monad (when)
31import Control.Monad (when)
32−import Control.Monad.Catch (MonadThrow)
33import Data.Bifunctor (first)
32import Data.Bifunctor (first)
34import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
33import Streamly.Internal.Data.Unboxed (Unbox, sizeOf)
35import Streamly.Internal.Data.Array.Mut.Type (Array(..))
34import Streamly.Internal.Data.Array.Mut.Type (Array(..))
36import Streamly.Internal.Data.Fold.Type (Fold(..))
35import Streamly.Internal.Data.Fold.Type (Fold(..))
36+import Streamly.Internal.Data.Parser (ParseError)
37import Streamly.Internal.Data.Stream.Type (Stream)
37import Streamly.Internal.Data.Stream.Type (Stream)
38import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
38import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
39
39
@@ -298,8 +298,8 @@ compactGEFold n = Fold step initial extract
298-- maximum specified size in bytes.
298-- maximum specified size in bytes.
299--
299--
300-- /Internal/
300-- /Internal/
301−compactLE :: (MonadThrow m, MonadIO m, Unbox a) =>
301+compactLE :: (Monad m, MonadIO m, Unbox a) =>
302− Int -> Stream m (Array a) -> Stream m (Array a)
302+ Int -> Stream m (Array a) -> Stream m (Either ParseError (Array a))
303compactLE n =
303compactLE n =
304 Stream.fromStreamD . D.parseMany (compactLEParserD n) . Stream.toStreamD
304 Stream.fromStreamD . D.parseMany (compactLEParserD n) . Stream.toStreamD
305
305
core/src/Streamly/Internal/Data/Parser.hs+38−38
@@ -365,7 +365,7 @@ dieM = D.toParserK . D.dieM
365-- | @lmap f parser@ maps the function @f@ on the input of the parser.
365-- | @lmap f parser@ maps the function @f@ on the input of the parser.
366--
366--
367-- >>> Stream.parse (Parser.lmap (\x -> x * x) (Parser.fromFold Fold.sum)) (Stream.enumerateFromTo 1 100)
367-- >>> Stream.parse (Parser.lmap (\x -> x * x) (Parser.fromFold Fold.sum)) (Stream.enumerateFromTo 1 100)
368−-- 338350
368+-- Right 338350
369--
369--
370-- > lmap = Parser.lmapM return
370-- > lmap = Parser.lmapM return
371--
371--
@@ -389,7 +389,7 @@ rmapM f p = D.toParserK $ D.rmapM f $ D.fromParserK p
389-- | Include only those elements that pass a predicate.
389-- | Include only those elements that pass a predicate.
390--
390--
391-- >>> Stream.parse (Parser.filter (> 5) (Parser.fromFold Fold.sum)) $ Stream.fromList [1..10]
391-- >>> Stream.parse (Parser.filter (> 5) (Parser.fromFold Fold.sum)) $ Stream.fromList [1..10]
392−-- 40
392+-- Right 40
393--
393--
394{-# INLINE filter #-}
394{-# INLINE filter #-}
395filter :: Monad m => (a -> Bool) -> Parser a m b -> Parser a m b
395filter :: Monad m => (a -> Bool) -> Parser a m b -> Parser a m b
@@ -403,7 +403,7 @@ filter f p = D.toParserK $ D.filter f $ D.fromParserK p
403-- encounters end of input.
403-- encounters end of input.
404--
404--
405-- >>> Stream.parse ((,) <$> Parser.peek <*> Parser.satisfy (> 0)) $ Stream.fromList [1]
405-- >>> Stream.parse ((,) <$> Parser.peek <*> Parser.satisfy (> 0)) $ Stream.fromList [1]
406−-- (1,1)
406+-- Right (1,1)
407--
407--
408-- @
408-- @
409-- peek = lookAhead (satisfy True)
409-- peek = lookAhead (satisfy True)
@@ -416,7 +416,7 @@ peek = D.toParserK D.peek
416-- | Succeeds if we are at the end of input, fails otherwise.
416-- | Succeeds if we are at the end of input, fails otherwise.
417--
417--
418-- >>> Stream.parse ((,) <$> Parser.satisfy (> 0) <*> Parser.eof) $ Stream.fromList [1]
418-- >>> Stream.parse ((,) <$> Parser.satisfy (> 0) <*> Parser.eof) $ Stream.fromList [1]
419−-- (1,())
419+-- Right (1,())
420--
420--
421{-# INLINE eof #-}
421{-# INLINE eof #-}
422eof :: Monad m => Parser a m ()
422eof :: Monad m => Parser a m ()
@@ -425,7 +425,7 @@ eof = D.toParserK D.eof
425-- | Returns the next element if it passes the predicate, fails otherwise.
425-- | Returns the next element if it passes the predicate, fails otherwise.
426--
426--
427-- >>> Stream.parse (Parser.satisfy (== 1)) $ Stream.fromList [1,0,1]
427-- >>> Stream.parse (Parser.satisfy (== 1)) $ Stream.fromList [1,0,1]
428−-- 1
428+-- Right 1
429--
429--
430-- >>> toMaybe f x = if f x then Just x else Nothing
430-- >>> toMaybe f x = if f x then Just x else Nothing
431-- >>> satisfy f = Parser.maybe (toMaybe f)
431-- >>> satisfy f = Parser.maybe (toMaybe f)
@@ -551,19 +551,19 @@ either = D.toParserK . D.either
551-- @
551-- @
552--
552--
553-- >>> takeBetween' 2 4 [1, 2, 3, 4, 5]
553-- >>> takeBetween' 2 4 [1, 2, 3, 4, 5]
554−-- [1,2,3,4]
554+-- Right [1,2,3,4]
555--
555--
556-- >>> takeBetween' 2 4 [1, 2]
556-- >>> takeBetween' 2 4 [1, 2]
557−-- [1,2]
557+-- Right [1,2]
558--
558--
559-- >>> takeBetween' 2 4 [1]
559-- >>> takeBetween' 2 4 [1]
560−-- *** Exception: ParseError "takeBetween: Expecting alteast 2 elements, got 1"
560+-- Left (ParseError "takeBetween: Expecting alteast 2 elements, got 1")
561--
561--
562-- >>> takeBetween' 0 0 [1, 2]
562-- >>> takeBetween' 0 0 [1, 2]
563−-- []
563+-- Right []
564--
564--
565-- >>> takeBetween' 0 1 []
565-- >>> takeBetween' 0 1 []
566−-- []
566+-- Right []
567--
567--
568-- @takeBetween@ is the most general take operation, other take operations can
568-- @takeBetween@ is the most general take operation, other take operations can
569-- be defined in terms of takeBetween. For example:
569-- be defined in terms of takeBetween. For example:
@@ -586,7 +586,7 @@ takeBetween m n = D.toParserK . D.takeBetween m n
586-- exactly @n@ elements.
586-- exactly @n@ elements.
587--
587--
588-- >>> Stream.parse (Parser.takeEQ 4 Fold.toList) $ Stream.fromList [1,0,1]
588-- >>> Stream.parse (Parser.takeEQ 4 Fold.toList) $ Stream.fromList [1,0,1]
589−-- *** Exception: ParseError "takeEQ: Expecting exactly 4 elements, input terminated on 3"
589+-- Left (ParseError "takeEQ: Expecting exactly 4 elements, input terminated on 3")
590--
590--
591{-# INLINE takeEQ #-}
591{-# INLINE takeEQ #-}
592takeEQ :: Monad m => Int -> Fold m a b -> Parser a m b
592takeEQ :: Monad m => Int -> Fold m a b -> Parser a m b
@@ -599,10 +599,10 @@ takeEQ n = D.toParserK . D.takeEQ n
599-- elements.
599-- elements.
600--
600--
601-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1]
601-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1]
602−-- *** Exception: ParseError "takeGE: Expecting at least 4 elements, input terminated on 3"
602+-- Left (ParseError "takeGE: Expecting at least 4 elements, input terminated on 3")
603--
603--
604-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1,0,1]
604-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1,0,1]
605−-- [1,0,1,0,1]
605+-- Right [1,0,1,0,1]
606--
606--
607-- /Pre-release/
607-- /Pre-release/
608--
608--
@@ -644,7 +644,7 @@ takeWhileP cond p = D.toParserK $ D.takeWhileP cond (D.fromParserK p)
644-- * Fails - never.
644-- * Fails - never.
645--
645--
646-- >>> Stream.parse (Parser.takeWhile (== 0) Fold.toList) $ Stream.fromList [0,0,1,0,1]
646-- >>> Stream.parse (Parser.takeWhile (== 0) Fold.toList) $ Stream.fromList [0,0,1,0,1]
647−-- [0,0]
647+-- Right [0,0]
648--
648--
649-- >>> takeWhile cond f = Parser.takeWhileP cond (Parser.fromFold f)
649-- >>> takeWhile cond f = Parser.takeWhileP cond (Parser.fromFold f)
650--
650--
@@ -752,14 +752,14 @@ takeEitherSepBy _cond = undefined -- D.toParserK . D.takeEitherSepBy cond
752-- >>> p = Parser.takeStartBy (== ',') Fold.toList
752-- >>> p = Parser.takeStartBy (== ',') Fold.toList
753-- >>> leadingComma = Stream.parse p . Stream.fromList
753-- >>> leadingComma = Stream.parse p . Stream.fromList
754-- >>> leadingComma "a,b"
754-- >>> leadingComma "a,b"
755−-- *** Exception: ParseError "takeStartBy: missing frame start"
755+-- Left (ParseError "takeStartBy: missing frame start")
756-- ...
756-- ...
757-- >>> leadingComma ",,"
757-- >>> leadingComma ",,"
758−-- ","
758+-- Right ","
759-- >>> leadingComma ",a,b"
759-- >>> leadingComma ",a,b"
760−-- ",a"
760+-- Right ",a"
761-- >>> leadingComma ""
761-- >>> leadingComma ""
762−-- ""
762+-- Right ""
763--
763--
764-- /Pre-release/
764-- /Pre-release/
765--
765--
@@ -802,13 +802,13 @@ takeEndByEsc isEsc isEnd p =
802--
802--
803-- >>> p = Parser.takeFramedByEsc_ (== '\\') (== '{') (== '}') Fold.toList
803-- >>> p = Parser.takeFramedByEsc_ (== '\\') (== '{') (== '}') Fold.toList
804-- >>> Stream.parse p $ Stream.fromList "{hello}"
804-- >>> Stream.parse p $ Stream.fromList "{hello}"
805−-- "hello"
805+-- Right "hello"
806-- >>> Stream.parse p $ Stream.fromList "{hello {world}}"
806-- >>> Stream.parse p $ Stream.fromList "{hello {world}}"
807−-- "hello {world}"
807+-- Right "hello {world}"
808-- >>> Stream.parse p $ Stream.fromList "{hello \\{world}"
808-- >>> Stream.parse p $ Stream.fromList "{hello \\{world}"
809−-- "hello {world"
809+-- Right "hello {world"
810-- >>> Stream.parse p $ Stream.fromList "{hello {world}"
810-- >>> Stream.parse p $ Stream.fromList "{hello {world}"
811−-- *** Exception: ParseError "takeFramedByEsc_: missing frame end"
811+-- Left (ParseError "takeFramedByEsc_: missing frame end")
812--
812--
813-- /Pre-release/
813-- /Pre-release/
814{-# INLINE takeFramedByEsc_ #-}
814{-# INLINE takeFramedByEsc_ #-}
@@ -865,17 +865,17 @@ wordBy f = D.toParserK . D.wordBy f
865--
865--
866-- >>> braces = Parser.wordFramedBy (== '\\') (== '{') (== '}') isSpace Fold.toList
866-- >>> braces = Parser.wordFramedBy (== '\\') (== '{') (== '}') isSpace Fold.toList
867-- >>> Stream.parse braces $ Stream.fromList "{ab} cd"
867-- >>> Stream.parse braces $ Stream.fromList "{ab} cd"
868−-- "ab"
868+-- Right "ab"
869-- >>> Stream.parse braces $ Stream.fromList "{ab}{cd}"
869-- >>> Stream.parse braces $ Stream.fromList "{ab}{cd}"
870−-- "abcd"
870+-- Right "abcd"
871-- >>> Stream.parse braces $ Stream.fromList "a{b} cd"
871-- >>> Stream.parse braces $ Stream.fromList "a{b} cd"
872−-- "ab"
872+-- Right "ab"
873-- >>> Stream.parse braces $ Stream.fromList "a{{b}} cd"
873-- >>> Stream.parse braces $ Stream.fromList "a{{b}} cd"
874−-- "a{b}"
874+-- Right "a{b}"
875--
875--
876-- >>> quotes = Parser.wordFramedBy (== '\\') (== '"') (== '"') isSpace Fold.toList
876-- >>> quotes = Parser.wordFramedBy (== '\\') (== '"') (== '"') isSpace Fold.toList
877-- >>> Stream.parse quotes $ Stream.fromList "\"a\"\"b\""
877-- >>> Stream.parse quotes $ Stream.fromList "\"a\"\"b\""
878−-- "ab"
878+-- Right "ab"
879--
879--
880{-# INLINE wordFramedBy #-}
880{-# INLINE wordFramedBy #-}
881wordFramedBy :: Monad m =>
881wordFramedBy :: Monad m =>
@@ -903,10 +903,10 @@ wordFramedBy isEsc isBegin isEnd isSpc =
903-- >>> p kQ = Parser.wordQuotedBy kQ (== '\\') q q id isSpace Fold.toList
903-- >>> p kQ = Parser.wordQuotedBy kQ (== '\\') q q id isSpace Fold.toList
904--
904--
905-- >>> Stream.parse (p False) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
905-- >>> Stream.parse (p False) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
906−-- "ab'c;d\"ef"
906+-- Right "ab'c;d\"ef"
907--
907--
908-- >>> Stream.parse (p True) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
908-- >>> Stream.parse (p True) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
909−-- "a\"b'c\";'d\"e'f"
909+-- Right "a\"b'c\";'d\"e'f"
910--
910--
911{-# INLINE wordQuotedBy #-}
911{-# INLINE wordQuotedBy #-}
912wordQuotedBy :: (Monad m, Eq a) =>
912wordQuotedBy :: (Monad m, Eq a) =>
@@ -942,10 +942,10 @@ wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSpc =
942-- []
942-- []
943--
943--
944-- >>> runGroupsBy (<) [1]
944-- >>> runGroupsBy (<) [1]
945−-- [[1]]
945+-- [Right [1]]
946--
946--
947-- >>> runGroupsBy (<) [3, 5, 4, 1, 2, 0]
947-- >>> runGroupsBy (<) [3, 5, 4, 1, 2, 0]
948−-- [[3,5,4],[1,2],[0]]
948+-- [Right [3,5,4],Right [1,2],Right [0]]
949--
949--
950{-# INLINE groupBy #-}
950{-# INLINE groupBy #-}
951groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser a m b
951groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser a m b
@@ -974,10 +974,10 @@ groupBy eq = D.toParserK . D.groupBy eq
974-- []
974-- []
975--
975--
976-- >>> runGroupsByRolling (<) [1]
976-- >>> runGroupsByRolling (<) [1]
977−-- [[1]]
977+-- [Right [1]]
978--
978--
979-- >>> runGroupsByRolling (<) [3, 5, 4, 1, 2, 0]
979-- >>> runGroupsByRolling (<) [3, 5, 4, 1, 2, 0]
980−-- [[3,5],[4],[1,2],[0]]
980+-- [Right [3,5],Right [4],Right [1,2],Right [0]]
981--
981--
982-- /Pre-release/
982-- /Pre-release/
983--
983--
@@ -1023,10 +1023,10 @@ eqBy cmp = D.toParserK . D.eqBy cmp . Stream.toStreamD
1023-- Examples:
1023-- Examples:
1024--
1024--
1025-- >>> Stream.parse (Parser.listEqBy (==) "string") $ Stream.fromList "string"
1025-- >>> Stream.parse (Parser.listEqBy (==) "string") $ Stream.fromList "string"
1026−-- "string"
1026+-- Right "string"
1027--
1027--
1028-- >>> Stream.parse (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
1028-- >>> Stream.parse (Parser.listEqBy (==) "mismatch") $ Stream.fromList "match"
1029−-- *** Exception: ParseError "eqBy: mismtach occurred"
1029+-- Left (ParseError "eqBy: mismtach occurred")
1030--
1030--
1031{-# INLINE listEqBy #-}
1031{-# INLINE listEqBy #-}
1032listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser a m [a]
1032listEqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser a m [a]
@@ -1186,7 +1186,7 @@ teeWithMin f p1 p2 =
1186-- will fail:
1186-- will fail:
1187--
1187--
1188-- >>> Stream.parse (Parser.satisfy (> 0) `Parser.alt` undefined) $ Stream.fromList [1..10]
1188-- >>> Stream.parse (Parser.satisfy (> 0) `Parser.alt` undefined) $ Stream.fromList [1..10]
1189−-- 1
1189+-- Right 1
1190--
1190--
1191-- Compare with 'Alternative' instance method '<|>'. This implementation allows
1191-- Compare with 'Alternative' instance method '<|>'. This implementation allows
1192-- stream fusion but has quadratic complexity. This can fuse with other
1192-- stream fusion but has quadratic complexity. This can fuse with other
@@ -1235,10 +1235,10 @@ lookAhead p = D.toParserK $ D.lookAhead $ D.fromParserK p
1235-- * Fails - when the collecting parser fails.
1235-- * Fails - when the collecting parser fails.
1236--
1236--
1237-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 2 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1237-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 2 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1238−-- [1,2]
1238+-- Right [1,2]
1239--
1239--
1240-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 5 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1240-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 5 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1241−-- *** Exception: ParseError "takeEQ: Expecting exactly 5 elements, input terminated on 4"
1241+-- Left (ParseError "takeEQ: Expecting exactly 5 elements, input terminated on 4")
1242--
1242--
1243-- /Internal/
1243-- /Internal/
1244{-# INLINE takeP #-}
1244{-# INLINE takeP #-}
core/src/Streamly/Internal/Data/Parser/Chunked.hs+8−8
@@ -26,7 +26,6 @@ where
26#include "assert.hs"
26#include "assert.hs"
27#include "inline.hs"
27#include "inline.hs"
28
28
29−import Control.Monad.Catch (MonadThrow, throwM)
30import Control.Monad.IO.Class (MonadIO(..))
29import Control.Monad.IO.Class (MonadIO(..))
31import GHC.Types (SPEC(..))
30import GHC.Types (SPEC(..))
32import Streamly.Internal.Data.Array.Type (Array(..))
31import Streamly.Internal.Data.Array.Type (Array(..))
@@ -42,6 +41,7 @@ import qualified Streamly.Internal.Data.Parser.ParserD as D
42 hiding (fromParserK, toParserK)
41 hiding (fromParserK, toParserK)
43import qualified Streamly.Internal.Data.Stream.StreamK as StreamK
42import qualified Streamly.Internal.Data.Stream.StreamK as StreamK
44import qualified Streamly.Internal.Data.Stream.Type as Stream
43import qualified Streamly.Internal.Data.Stream.Type as Stream
44+import Streamly.Internal.Data.Parser.ParserD (ParseError(..))
45
45
46-------------------------------------------------------------------------------
46-------------------------------------------------------------------------------
47-- Driver
47-- Driver
@@ -84,10 +84,10 @@ parserDone (K.Failure n e) _ _ = return $ K.Error n e
84-- | Run a 'Parser' over a stream and return rest of the Stream.
84-- | Run a 'Parser' over a stream and return rest of the Stream.
85{-# INLINE_NORMAL parseBreak #-}
85{-# INLINE_NORMAL parseBreak #-}
86parseBreak
86parseBreak
87− :: (MonadThrow m, Unbox a)
87+ :: (Monad m, Unbox a)
88 => ParserChunked a m b
88 => ParserChunked a m b
89 -> Stream m (Array a)
89 -> Stream m (Array a)
90− -> m (b, Stream m (Array a))
90+ -> m (Either ParseError b, Stream m (Array a))
91parseBreak parser input = do
91parseBreak parser input = do
92 let parserk = \arr -> K.runParser parser 0 0 arr parserDone
92 let parserk = \arr -> K.runParser parser 0 0 arr parserDone
93 in go [] parserk (Stream.toStreamK input)
93 in go [] parserk (Stream.toStreamK input)
@@ -116,13 +116,13 @@ parseBreak parser input = do
116 let (s1, backBuf1) = backTrack n1 backBuf StreamK.nil
116 let (s1, backBuf1) = backTrack n1 backBuf StreamK.nil
117 in go backBuf1 cont1 s1
117 in go backBuf1 cont1 s1
118 K.Done 0 b ->
118 K.Done 0 b ->
119− return (b, Stream.nil)
119+ return (Right b, Stream.nil)
120 K.Done n b -> do
120 K.Done n b -> do
121 let n1 = negate n
121 let n1 = negate n
122 assertM(n1 >= 0 && n1 <= sum (map Array.length backBuf))
122 assertM(n1 >= 0 && n1 <= sum (map Array.length backBuf))
123 let (s1, _) = backTrack n1 backBuf StreamK.nil
123 let (s1, _) = backTrack n1 backBuf StreamK.nil
124− in return (b, Stream.fromStreamK s1)
124+ in return (Right b, Stream.fromStreamK s1)
125− K.Error _ err -> throwM $ D.ParseError err
125+ K.Error _ err -> return (Left (ParseError err), Stream.nil)
126
126
127 seekErr n len =
127 seekErr n len =
128 error $ "parseBreak: Partial: forward seek not implemented n = "
128 error $ "parseBreak: Partial: forward seek not implemented n = "
@@ -164,8 +164,8 @@ parseBreak parser input = do
164 let n1 = len - n
164 let n1 = len - n
165 assertM(n1 <= sum (map Array.length (arr:backBuf)))
165 assertM(n1 <= sum (map Array.length (arr:backBuf)))
166 let (s1, _) = backTrack n1 (arr:backBuf) stream
166 let (s1, _) = backTrack n1 (arr:backBuf) stream
167− in return (b, Stream.fromStreamK s1)
167+ in return (Right b, Stream.fromStreamK s1)
168− K.Error _ err -> throwM $ D.ParseError err
168+ K.Error _ err -> return (Left (ParseError err), Stream.nil)
169
169
170 go backBuf parserk stream = do
170 go backBuf parserk stream = do
171 let stop = goStop backBuf parserk
171 let stop = goStop backBuf parserk
core/src/Streamly/Internal/Data/Parser/ParserD/Type.hs+1−1
@@ -1295,7 +1295,7 @@ dieM err = Parser undefined (IError <$> err) undefined
1295-- argument. The following code will fail:
1295-- argument. The following code will fail:
1296--
1296--
1297-- >>> Stream.parse (Parser.satisfy (> 0) <|> undefined) $ Stream.fromList [1..10]
1297-- >>> Stream.parse (Parser.satisfy (> 0) <|> undefined) $ Stream.fromList [1..10]
1298−-- 1
1298+-- Right 1
1299--
1299--
1300instance Monad m => Alternative (Parser a m) where
1300instance Monad m => Alternative (Parser a m) where
1301 {-# INLINE empty #-}
1301 {-# INLINE empty #-}
core/src/Streamly/Internal/Data/Stream/Eliminate.hs+8−9
@@ -77,9 +77,8 @@ where
77
77
78#include "inline.hs"
78#include "inline.hs"
79
79
80−import Control.Monad.Catch (MonadThrow)
81import Control.Monad.IO.Class (MonadIO(..))
80import Control.Monad.IO.Class (MonadIO(..))
82−import Streamly.Internal.Data.Parser (Parser (..))
81+import Streamly.Internal.Data.Parser (Parser (..), ParseError (..))
83import Streamly.Internal.Data.Unboxed (Unbox)
82import Streamly.Internal.Data.Unboxed (Unbox)
84
83
85import qualified Streamly.Internal.Data.Array.Type as Array
84import qualified Streamly.Internal.Data.Array.Type as Array
@@ -216,7 +215,7 @@ foldlS f z =
216-- /Internal/
215-- /Internal/
217--
216--
218{-# INLINE_NORMAL parseD #-}
217{-# INLINE_NORMAL parseD #-}
219−parseD :: MonadThrow m => PRD.Parser a m b -> Stream m a -> m b
218+parseD :: Monad m => PRD.Parser a m b -> Stream m a -> m (Either ParseError b)
220parseD p = D.parse p . toStreamD
219parseD p = D.parse p . toStreamD
221
220
222-- XXX Drive directly as parserK rather than converting to parserD first.
221-- XXX Drive directly as parserK rather than converting to parserD first.
@@ -225,7 +224,7 @@ parseD p = D.parse p . toStreamD
225--
224--
226-- /Internal/
225-- /Internal/
227{-# INLINE parseK #-}
226{-# INLINE parseK #-}
228−parseK :: MonadThrow m => PRK.Parser a m b -> Stream m a -> m b
227+parseK :: Monad m => PRK.Parser a m b -> Stream m a -> m (Either ParseError b)
229parseK = parse
228parseK = parse
230
229
231-- | Parse a stream using the supplied 'Parser'.
230-- | Parse a stream using the supplied 'Parser'.
@@ -236,17 +235,17 @@ parseK = parse
236-- error. For example:
235-- error. For example:
237--
236--
238-- >>> Stream.parse (Parser.takeEQ 1 Fold.drain) Stream.nil
237-- >>> Stream.parse (Parser.takeEQ 1 Fold.drain) Stream.nil
239−-- *** Exception: ParseError "takeEQ: Expecting exactly 1 elements, input terminated on 0"
238+-- Left (ParseError "takeEQ: Expecting exactly 1 elements, input terminated on 0")
240--
239--
241-- Note: @parse p@ is not the same as @head . parseMany p@ on an empty stream.
240-- Note: @parse p@ is not the same as @head . parseMany p@ on an empty stream.
242--
241--
243{-# INLINE [3] parse #-}
242{-# INLINE [3] parse #-}
244−parse :: MonadThrow m => Parser a m b -> Stream m a -> m b
243+parse :: Monad m => Parser a m b -> Stream m a -> m (Either ParseError b)
245parse = parseD . PRD.fromParserK
244parse = parseD . PRD.fromParserK
246
245
247{-# INLINE_NORMAL parseBreakD #-}
246{-# INLINE_NORMAL parseBreakD #-}
248−parseBreakD :: MonadThrow m =>
247+parseBreakD :: Monad m =>
249− PRD.Parser a m b -> Stream m a -> m (b, Stream m a)
248+ PRD.Parser a m b -> Stream m a -> m (Either ParseError b, Stream m a)
250parseBreakD parser strm = do
249parseBreakD parser strm = do
251 (b, strmD) <- D.parseBreak parser (toStreamD strm)
250 (b, strmD) <- D.parseBreak parser (toStreamD strm)
252 return $! (b, fromStreamD strmD)
251 return $! (b, fromStreamD strmD)
@@ -256,7 +255,7 @@ parseBreakD parser strm = do
256-- /Not fused/
255-- /Not fused/
257--
256--
258{-# INLINE parseBreak #-}
257{-# INLINE parseBreak #-}
259−parseBreak :: MonadThrow m => Parser a m b -> Stream m a -> m (b, Stream m a)
258+parseBreak :: Monad m => Parser a m b -> Stream m a -> m (Either ParseError b, Stream m a)
260parseBreak p strm = fmap f $ K.parseBreak (PRD.fromParserK p) (toStreamK strm)
259parseBreak p strm = fmap f $ K.parseBreak (PRD.fromParserK p) (toStreamK strm)
261
260
262 where
261 where
core/src/Streamly/Internal/Data/Stream/Reduce.hs+9−9
@@ -56,11 +56,11 @@ module Streamly.Internal.Data.Stream.Reduce
56 )
56 )
57where
57where
58
58
59−import Control.Monad.Catch (MonadThrow)
60import Control.Monad.IO.Class (MonadIO(..))
59import Control.Monad.IO.Class (MonadIO(..))
61import Streamly.Internal.Data.Array.Type (Array)
60import Streamly.Internal.Data.Array.Type (Array)
62import Streamly.Internal.Data.Fold.Type (Fold (..))
61import Streamly.Internal.Data.Fold.Type (Fold (..))
63import Streamly.Internal.Data.Parser (Parser (..))
62import Streamly.Internal.Data.Parser (Parser (..))
63+import Streamly.Internal.Data.Parser.ParserD (ParseError)
64import Streamly.Internal.Data.Refold.Type (Refold (..))
64import Streamly.Internal.Data.Refold.Type (Refold (..))
65import Streamly.Internal.Data.Stream.Bottom (foldManyPost)
65import Streamly.Internal.Data.Stream.Bottom (foldManyPost)
66import Streamly.Internal.Data.Stream.Type (Stream, fromStreamD, toStreamD)
66import Streamly.Internal.Data.Stream.Type (Stream, fromStreamD, toStreamD)
@@ -331,7 +331,7 @@ splitOnAny _subseq _f _m =
331-- >>> s = Stream.fromList [1..10]
331-- >>> s = Stream.fromList [1..10]
332-- >>> parser = Parser.takeBetween 0 2 Fold.sum
332-- >>> parser = Parser.takeBetween 0 2 Fold.sum
333-- >>> Stream.fold Fold.toList $ Stream.parseMany parser s
333-- >>> Stream.fold Fold.toList $ Stream.parseMany parser s
334−-- [3,7,11,15,19]
334+-- [Right 3,Right 7,Right 11,Right 15,Right 19]
335--
335--
336-- foldMany f = Stream.parseMany (Parser.fromFold f)
336-- foldMany f = Stream.parseMany (Parser.fromFold f)
337--
337--
@@ -340,10 +340,10 @@ splitOnAny _subseq _f _m =
340--
340--
341{-# INLINE parseMany #-}
341{-# INLINE parseMany #-}
342parseMany
342parseMany
343− :: MonadThrow m
343+ :: Monad m
344 => Parser a m b
344 => Parser a m b
345 -> Stream m a
345 -> Stream m a
346− -> Stream m b
346+ -> Stream m (Either ParseError b)
347parseMany p m =
347parseMany p m =
348 fromStreamD $ D.parseMany (ParserD.fromParserK p) (toStreamD m)
348 fromStreamD $ D.parseMany (ParserD.fromParserK p) (toStreamD m)
349
349
@@ -353,10 +353,10 @@ parseMany p m =
353--
353--
354{-# INLINE parseManyD #-}
354{-# INLINE parseManyD #-}
355parseManyD
355parseManyD
356− :: MonadThrow m
356+ :: Monad m
357 => ParserD.Parser a m b
357 => ParserD.Parser a m b
358 -> Stream m a
358 -> Stream m a
359− -> Stream m b
359+ -> Stream m (Either ParseError b)
360parseManyD p m =
360parseManyD p m =
361 fromStreamD $ D.parseMany p (toStreamD m)
361 fromStreamD $ D.parseMany p (toStreamD m)
362
362
@@ -398,7 +398,7 @@ parseManyTill = undefined
398--
398--
399-- >>> import Data.Monoid (Sum(..))
399-- >>> import Data.Monoid (Sum(..))
400-- >>> s = Stream.fromList [1..10]
400-- >>> s = Stream.fromList [1..10]
401−-- >>> Stream.fold Fold.toList $ fmap getSum $ Stream.parseIterate (\b -> Parser.takeBetween 0 2 (Fold.sconcat b)) 0 $ fmap Sum s
401+-- >>> Stream.fold Fold.toList $ fmap getSum $ Stream.rights $ Stream.parseIterate (\b -> Parser.takeBetween 0 2 (Fold.sconcat b)) (Sum 0) $ fmap Sum s
402-- [3,10,21,36,55,55]
402-- [3,10,21,36,55,55]
403--
403--
404-- This is the streaming equivalent of monad like sequenced application of
404-- This is the streaming equivalent of monad like sequenced application of
@@ -408,11 +408,11 @@ parseManyTill = undefined
408--
408--
409{-# INLINE parseIterate #-}
409{-# INLINE parseIterate #-}
410parseIterate
410parseIterate
411− :: MonadThrow m
411+ :: Monad m
412 => (b -> Parser a m b)
412 => (b -> Parser a m b)
413 -> b
413 -> b
414 -> Stream m a
414 -> Stream m a
415− -> Stream m b
415+ -> Stream m (Either ParseError b)
416parseIterate f i m = fromStreamD $
416parseIterate f i m = fromStreamD $
417 D.parseIterate (ParserD.fromParserK . f) i (toStreamD m)
417 D.parseIterate (ParserD.fromParserK . f) i (toStreamD m)
418
418
core/src/Streamly/Internal/Data/Stream/StreamD/Eliminate.hs+1−1
@@ -189,7 +189,7 @@ parseBreak (PRD.Parser pstep initial extract) stream@(Stream step state) = do
189 let (src0, buf1) = splitAt n (x:getList buf)
189 let (src0, buf1) = splitAt n (x:getList buf)
190 src = Prelude.reverse src0
190 src = Prelude.reverse src0
191 gobuf SPEC s (List buf1) (List src) pst1
191 gobuf SPEC s (List buf1) (List src) pst1
192− PR.Done 0 b -> return $ (Right b, Stream step s)
192+ PR.Done 0 b -> return (Right b, Stream step s)
193 PR.Done n b -> do
193 PR.Done n b -> do
194 assert (n <= length (x:getList buf)) (return ())
194 assert (n <= length (x:getList buf)) (return ())
195 let src0 = Prelude.take n (x:getList buf)
195 let src0 = Prelude.take n (x:getList buf)
core/src/Streamly/Internal/Data/Stream/StreamD/Nesting.hs+55−21
@@ -150,7 +150,6 @@ where
150#include "ArrayMacros.h"
150#include "ArrayMacros.h"
151
151
152import Control.Exception (assert)
152import Control.Exception (assert)
153−import Control.Monad.Catch (MonadThrow, throwM)
154import Control.Monad.IO.Class (MonadIO(..))
153import Control.Monad.IO.Class (MonadIO(..))
155import Data.Bits (shiftR, shiftL, (.|.), (.&.))
154import Data.Bits (shiftR, shiftL, (.|.), (.&.))
156import Data.Functor.Identity ( Identity )
155import Data.Functor.Identity ( Identity )
@@ -1363,7 +1362,7 @@ parseMany (PRD.Parser pstep initial extract) (Stream step state) =
1363 stepOuter _ (ParseChunksYield a next) = return $ Yield a next
1362 stepOuter _ (ParseChunksYield a next) = return $ Yield a next
1364
1363
1365{-# ANN type ConcatParseState Fuse #-}
1364{-# ANN type ConcatParseState Fuse #-}
1366−data ConcatParseState b inpBuf st p m a =
1365+data ConcatParseState c b inpBuf st p m a =
1367 ConcatParseInit inpBuf st p
1366 ConcatParseInit inpBuf st p
1368 | ConcatParseInitBuf inpBuf p
1367 | ConcatParseInitBuf inpBuf p
1369 | ConcatParseInitLeftOver inpBuf
1368 | ConcatParseInitLeftOver inpBuf
@@ -1375,16 +1374,16 @@ data ConcatParseState b inpBuf st p m a =
1375 inpBuf st inpBuf (s -> a -> m (PRD.Step s b)) s (s -> m (PRD.Step s b))
1374 inpBuf st inpBuf (s -> a -> m (PRD.Step s b)) s (s -> m (PRD.Step s b))
1376 | forall s. ConcatParseExtract
1375 | forall s. ConcatParseExtract
1377 inpBuf inpBuf (s -> a -> m (PRD.Step s b)) s (s -> m (PRD.Step s b))
1376 inpBuf inpBuf (s -> a -> m (PRD.Step s b)) s (s -> m (PRD.Step s b))
1378− | ConcatParseYield b (ConcatParseState b inpBuf st p m a)
1377+ | ConcatParseYield c (ConcatParseState c b inpBuf st p m a)
1379
1378
1380-- XXX Review the changes
1379-- XXX Review the changes
1381{-# INLINE_NORMAL parseIterate #-}
1380{-# INLINE_NORMAL parseIterate #-}
1382parseIterate
1381parseIterate
1383− :: MonadThrow m
1382+ :: Monad m
1384 => (b -> PRD.Parser a m b)
1383 => (b -> PRD.Parser a m b)
1385 -> b
1384 -> b
1386 -> Stream m a
1385 -> Stream m a
1387− -> Stream m b
1386+ -> Stream m (Either ParseError b)
1388parseIterate func seed (Stream step state) =
1387parseIterate func seed (Stream step state) =
1389 Stream stepOuter (ConcatParseInit [] state (func seed))
1388 Stream stepOuter (ConcatParseInit [] state (func seed))
1390
1389
@@ -1399,8 +1398,13 @@ parseIterate func seed (Stream step state) =
1399 return $ Skip $ ConcatParseStream st [] pstep ps extract
1398 return $ Skip $ ConcatParseStream st [] pstep ps extract
1400 PRD.IDone pb ->
1399 PRD.IDone pb ->
1401 let next = ConcatParseInit [] st (func pb)
1400 let next = ConcatParseInit [] st (func pb)
1402− in return $ Skip $ ConcatParseYield pb next
1401+ in return $ Skip $ ConcatParseYield (Right pb) next
1403− PRD.IError err -> throwM $ ParseError err
1402+ PRD.IError err ->
1403+ return
1404+ $ Skip
1405+ $ ConcatParseYield
1406+ (Left (ParseError err))
1407+ (ConcatParseInitLeftOver [])
1404
1408
1405 -- Buffer is not empty, go to buffered processing loop
1409 -- Buffer is not empty, go to buffered processing loop
1406 stepOuter _ (ConcatParseInit src st
1410 stepOuter _ (ConcatParseInit src st
@@ -1411,8 +1415,13 @@ parseIterate func seed (Stream step state) =
1411 return $ Skip $ ConcatParseBuf src st [] pstep ps extract
1415 return $ Skip $ ConcatParseBuf src st [] pstep ps extract
1412 PRD.IDone pb ->
1416 PRD.IDone pb ->
1413 let next = ConcatParseInit src st (func pb)
1417 let next = ConcatParseInit src st (func pb)
1414− in return $ Skip $ ConcatParseYield pb next
1418+ in return $ Skip $ ConcatParseYield (Right pb) next
1415− PRD.IError err -> throwM $ ParseError err
1419+ PRD.IError err ->
1420+ return
1421+ $ Skip
1422+ $ ConcatParseYield
1423+ (Left (ParseError err))
1424+ (ConcatParseInitLeftOver [])
1416
1425
1417 -- This is simplified ConcatParseInit
1426 -- This is simplified ConcatParseInit
1418 stepOuter _ (ConcatParseInitBuf src
1427 stepOuter _ (ConcatParseInitBuf src
@@ -1423,8 +1432,13 @@ parseIterate func seed (Stream step state) =
1423 return $ Skip $ ConcatParseExtract src [] pstep ps extract
1432 return $ Skip $ ConcatParseExtract src [] pstep ps extract
1424 PRD.IDone pb ->
1433 PRD.IDone pb ->
1425 let next = ConcatParseInitBuf src (func pb)
1434 let next = ConcatParseInitBuf src (func pb)
1426− in return $ Skip $ ConcatParseYield pb next
1435+ in return $ Skip $ ConcatParseYield (Right pb) next
1427− PRD.IError err -> throwM $ ParseError err
1436+ PRD.IError err ->
1437+ return
1438+ $ Skip
1439+ $ ConcatParseYield
1440+ (Left (ParseError err))
1441+ (ConcatParseInitLeftOver [])
1428
1442
1429 -- XXX we just discard any leftover input at the end
1443 -- XXX we just discard any leftover input at the end
1430 stepOuter _ (ConcatParseInitLeftOver _) = return Stop
1444 stepOuter _ (ConcatParseInitLeftOver _) = return Stop
@@ -1455,8 +1469,13 @@ parseIterate func seed (Stream step state) =
1455 assert (n <= length (x:buf)) (return ())
1469 assert (n <= length (x:buf)) (return ())
1456 let src = Prelude.reverse (Prelude.take n (x:buf))
1470 let src = Prelude.reverse (Prelude.take n (x:buf))
1457 return $ Skip $
1471 return $ Skip $
1458− ConcatParseYield b (ConcatParseInit src s (func b))
1472+ ConcatParseYield (Right b) (ConcatParseInit src s (func b))
1473+ PR.Error err ->
1474+ return
1475+ $ Skip
1476+ $ ConcatParseYield
1459− PR.Error err -> throwM $ ParseError err
1477+ (Left (ParseError err))
1478+ (ConcatParseInitLeftOver [])
1460 Skip s -> return $ Skip $ ConcatParseStream s buf pstep pst extract
1479 Skip s -> return $ Skip $ ConcatParseStream s buf pstep pst extract
1461 Stop -> return $ Skip $ ConcatParseStop buf pstep pst extract
1480 Stop -> return $ Skip $ ConcatParseStop buf pstep pst extract
1462
1481
@@ -1485,9 +1504,14 @@ parseIterate func seed (Stream step state) =
1485 PR.Done n b -> do
1504 PR.Done n b -> do
1486 assert (n <= length (x:buf)) (return ())
1505 assert (n <= length (x:buf)) (return ())
1487 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1506 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1488− return $ Skip $ ConcatParseYield b
1507+ return $ Skip $ ConcatParseYield (Right b)
1489 (ConcatParseInit src s (func b))
1508 (ConcatParseInit src s (func b))
1509+ PR.Error err ->
1510+ return
1511+ $ Skip
1512+ $ ConcatParseYield
1490− PR.Error err -> throwM $ ParseError err
1513+ (Left (ParseError err))
1514+ (ConcatParseInitLeftOver [])
1491
1515
1492 -- This is simplified ConcatParseBuf
1516 -- This is simplified ConcatParseBuf
1493 stepOuter _ (ConcatParseExtract [] buf pstep pst extract) =
1517 stepOuter _ (ConcatParseExtract [] buf pstep pst extract) =
@@ -1511,12 +1535,17 @@ parseIterate func seed (Stream step state) =
1511 src = Prelude.reverse src0 ++ xs
1535 src = Prelude.reverse src0 ++ xs
1512 return $ Skip $ ConcatParseExtract src buf1 pstep pst1 extract
1536 return $ Skip $ ConcatParseExtract src buf1 pstep pst1 extract
1513 PR.Done 0 b ->
1537 PR.Done 0 b ->
1514− return $ Skip $ ConcatParseYield b (ConcatParseInitBuf xs (func b))
1538+ return $ Skip $ ConcatParseYield (Right b) (ConcatParseInitBuf xs (func b))
1515 PR.Done n b -> do
1539 PR.Done n b -> do
1516 assert (n <= length (x:buf)) (return ())
1540 assert (n <= length (x:buf)) (return ())
1517 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1541 let src = Prelude.reverse (Prelude.take n (x:buf)) ++ xs
1518− return $ Skip $ ConcatParseYield b (ConcatParseInitBuf src (func b))
1542+ return $ Skip $ ConcatParseYield (Right b) (ConcatParseInitBuf src (func b))
1543+ PR.Error err ->
1544+ return
1545+ $ Skip
1546+ $ ConcatParseYield
1519− PR.Error err -> throwM $ ParseError err
1547+ (Left (ParseError err))
1548+ (ConcatParseInitLeftOver [])
1520
1549
1521 -- This is simplified ConcatParseExtract
1550 -- This is simplified ConcatParseExtract
1522 stepOuter _ (ConcatParseStop buf pstep pst extract) = do
1551 stepOuter _ (ConcatParseStop buf pstep pst extract) = do
@@ -1532,13 +1561,18 @@ parseIterate func seed (Stream step state) =
1532 return $ Skip $ ConcatParseExtract src buf1 pstep pst1 extract
1561 return $ Skip $ ConcatParseExtract src buf1 pstep pst1 extract
1533 PR.Done 0 b -> do
1562 PR.Done 0 b -> do
1534 return $ Skip $
1563 return $ Skip $
1535− ConcatParseYield b (ConcatParseInitLeftOver [])
1564+ ConcatParseYield (Right b) (ConcatParseInitLeftOver [])
1536 PR.Done n b -> do
1565 PR.Done n b -> do
1537 assert (n <= length buf) (return ())
1566 assert (n <= length buf) (return ())
1538 let src = Prelude.reverse (Prelude.take n buf)
1567 let src = Prelude.reverse (Prelude.take n buf)
1539 return $ Skip $
1568 return $ Skip $
1540− ConcatParseYield b (ConcatParseInitBuf src (func b))
1569+ ConcatParseYield (Right b) (ConcatParseInitBuf src (func b))
1570+ PR.Error err ->
1571+ return
1572+ $ Skip
1573+ $ ConcatParseYield
1541− PR.Error err -> throwM $ ParseError err
1574+ (Left (ParseError err))
1575+ (ConcatParseInitLeftOver [])
1542
1576
1543 stepOuter _ (ConcatParseYield a next) = return $ Yield a next
1577 stepOuter _ (ConcatParseYield a next) = return $ Yield a next
1544
1578
core/src/Streamly/Internal/Data/Stream/StreamK.hs+12−12
@@ -177,7 +177,6 @@ where
177
177
178#include "assert.hs"
178#include "assert.hs"
179
179
180−import Control.Monad.Catch (MonadThrow, throwM)
181import Control.Monad (void, join)
180import Control.Monad (void, join)
182import Streamly.Internal.Data.Fold.Type (Fold(..))
181import Streamly.Internal.Data.Fold.Type (Fold(..))
183import Streamly.Internal.Data.SVar.Type (adaptState, defState)
182import Streamly.Internal.Data.SVar.Type (adaptState, defState)
@@ -193,6 +192,7 @@ import Prelude
193 foldr1, (!!), replicate, reverse, concatMap, iterate, splitAt)
192 foldr1, (!!), replicate, reverse, concatMap, iterate, splitAt)
194
193
195import Streamly.Internal.Data.Stream.StreamK.Type
194import Streamly.Internal.Data.Stream.StreamK.Type
195+import Streamly.Internal.Data.Parser.ParserD (ParseError(..))
196
196
197-- $setup
197-- $setup
198-- >>> :m
198-- >>> :m
@@ -998,16 +998,16 @@ splitAt n ls
998-- | Run a 'Parser' over a stream and return rest of the Stream.
998-- | Run a 'Parser' over a stream and return rest of the Stream.
999{-# INLINE_NORMAL parseBreak #-}
999{-# INLINE_NORMAL parseBreak #-}
1000parseBreak
1000parseBreak
1001− :: MonadThrow m
1001+ :: Monad m
1002 => PR.Parser a m b
1002 => PR.Parser a m b
1003 -> Stream m a
1003 -> Stream m a
1004− -> m (b, Stream m a)
1004+ -> m (Either ParseError b, Stream m a)
1005parseBreak (PR.Parser pstep initial extract) stream = do
1005parseBreak (PR.Parser pstep initial extract) stream = do
1006 res <- initial
1006 res <- initial
1007 case res of
1007 case res of
1008 PR.IPartial s -> goStream stream [] s
1008 PR.IPartial s -> goStream stream [] s
1009− PR.IDone b -> return (b, stream)
1009+ PR.IDone b -> return (Right b, stream)
1010− PR.IError err -> throwM $ PR.ParseError err
1010+ PR.IError err -> return (Left (ParseError err), stream)
1011
1011
1012 where
1012 where
1013
1013
@@ -1021,12 +1021,12 @@ parseBreak (PR.Parser pstep initial extract) stream = do
1021 let stop = do
1021 let stop = do
1022 r <- extract pst
1022 r <- extract pst
1023 case r of
1023 case r of
1024− PR.Error err -> throwM $ PR.ParseError err
1024+ PR.Error err -> return (Left (ParseError err), nil)
1025 PR.Done n b -> do
1025 PR.Done n b -> do
1026 assertM(n <= length buf)
1026 assertM(n <= length buf)
1027 let src0 = Prelude.take n buf
1027 let src0 = Prelude.take n buf
1028 src = Prelude.reverse src0
1028 src = Prelude.reverse src0
1029− return (b, fromList src)
1029+ return (Right b, fromList src)
1030 PR.Partial _ _ -> error "Bug: parseBreak: Partial in extract"
1030 PR.Partial _ _ -> error "Bug: parseBreak: Partial in extract"
1031 PR.Continue 0 s -> goStream nil buf s
1031 PR.Continue 0 s -> goStream nil buf s
1032 PR.Continue n s -> do
1032 PR.Continue n s -> do
@@ -1050,13 +1050,13 @@ parseBreak (PR.Parser pstep initial extract) stream = do
1050 let (src0, buf1) = splitAt n (x:buf)
1050 let (src0, buf1) = splitAt n (x:buf)
1051 src = Prelude.reverse src0
1051 src = Prelude.reverse src0
1052 goBuf r buf1 src s
1052 goBuf r buf1 src s
1053− PR.Done 0 b -> return (b, r)
1053+ PR.Done 0 b -> return (Right b, r)
1054 PR.Done n b -> do
1054 PR.Done n b -> do
1055 assertM(n <= length (x:buf))
1055 assertM(n <= length (x:buf))
1056 let src0 = Prelude.take n (x:buf)
1056 let src0 = Prelude.take n (x:buf)
1057 src = Prelude.reverse src0
1057 src = Prelude.reverse src0
1058− return (b, serial (fromList src) r)
1058+ return (Right b, serial (fromList src) r)
1059− PR.Error err -> throwM $ PR.ParseError err
1059+ PR.Error err -> return (Left (ParseError err), r)
1060 in foldStream defState yieldk single stop st
1060 in foldStream defState yieldk single stop st
1061
1061
1062 goBuf st buf [] !pst = goStream st buf pst
1062 goBuf st buf [] !pst = goStream st buf pst
@@ -1079,5 +1079,5 @@ parseBreak (PR.Parser pstep initial extract) stream = do
1079 assert (n <= length (x:buf)) (return ())
1079 assert (n <= length (x:buf)) (return ())
1080 let src0 = Prelude.take n (x:buf)
1080 let src0 = Prelude.take n (x:buf)
1081 src = Prelude.reverse src0
1081 src = Prelude.reverse src0
1082− return (b, serial (fromList src) st)
1082+ return (Right b, serial (fromList src) st)
1083− PR.Error err -> throwM $ PR.ParseError err
1083+ PR.Error err -> return (Left (ParseError err), nil)
core/src/Streamly/Internal/Data/Stream/Top.hs+2−3
@@ -42,7 +42,6 @@ where
42
42
43#include "inline.hs"
43#include "inline.hs"
44
44
45−import Control.Monad.Catch (MonadThrow)
46import Control.Monad.IO.Class (MonadIO(..))
45import Control.Monad.IO.Class (MonadIO(..))
47import Control.Monad.Trans.State.Strict (get, put)
46import Control.Monad.Trans.State.Strict (get, put)
48import Data.Function ((&))
47import Data.Function ((&))
@@ -111,7 +110,7 @@ sampleFromThen offset stride =
111-- /Pre-release/
110-- /Pre-release/
112--
111--
113{-# INLINE sortBy #-}
112{-# INLINE sortBy #-}
114−sortBy :: MonadThrow m => (a -> a -> Ordering) -> Stream m a -> Stream m a
113+sortBy :: Monad m => (a -> a -> Ordering) -> Stream m a -> Stream m a
115-- sortBy f = Stream.concatPairsWith (Stream.mergeBy f) Stream.fromPure
114-- sortBy f = Stream.concatPairsWith (Stream.mergeBy f) Stream.fromPure
116sortBy cmp =
115sortBy cmp =
117 let p =
116 let p =
@@ -120,7 +119,7 @@ sortBy cmp =
120 Fold.toStreamRev
119 Fold.toStreamRev
121 Fold.toStream
120 Fold.toStream
122 in Stream.concatPairsWith (Stream.mergeBy cmp) id
121 in Stream.concatPairsWith (Stream.mergeBy cmp) id
123− . Stream.parseMany (fmap (either id id) p)
122+ . Stream.rights . Stream.parseMany (fmap (either id id) p)
124
123
125------------------------------------------------------------------------------
124------------------------------------------------------------------------------
126-- SQL Joins
125-- SQL Joins
core/src/Streamly/Internal/Unicode/String.hs+8−3
@@ -34,7 +34,10 @@ module Streamly.Internal.Unicode.String
34 ( str
34 ( str
35 ) where
35 ) where
36
36
37+
37import Control.Applicative (Alternative(..))
38import Control.Applicative (Alternative(..))
39+import Control.Exception (displayException)
40+import Data.Functor.Identity (runIdentity)
38import Streamly.Internal.Data.Parser (Parser)
41import Streamly.Internal.Data.Parser (Parser)
39
42
40import Language.Haskell.TH
43import Language.Haskell.TH
@@ -100,9 +103,11 @@ strExp xs = appE [| concat |] $ listE $ map strSegmentExp xs
100
103
101expandVars :: String -> Q Exp
104expandVars :: String -> Q Exp
102expandVars ln =
105expandVars ln =
103− case Stream.parse strParser (Stream.fromList ln) of
106+ case runIdentity $ Stream.parse strParser (Stream.fromList ln) of
104− Left _ -> fail "Parsing of str quoted string failed."
107+ Left e ->
108+ fail $ "str QuasiQuoter parse error: " ++ displayException e
109+ Right x ->
105− Right xs -> strExp xs
110+ strExp x
106
111
107-- | A QuasiQuoter that treats the input as a string literal:
112-- | A QuasiQuoter that treats the input as a string literal:
108--
113--
src/Streamly/Internal/Data/Fold/Concurrent.hs+1
@@ -44,6 +44,7 @@ import Streamly.Internal.Data.Fold.Concurrent.Channel
44-- Imports for example snippets in this module.
44-- Imports for example snippets in this module.
45--
45--
46-- >>> :m
46-- >>> :m
47+-- >>> :set -XFlexibleContexts
47-- >>> import Control.Concurrent (threadDelay)
48-- >>> import Control.Concurrent (threadDelay)
48-- >>> import qualified Streamly.Data.Array as Array
49-- >>> import qualified Streamly.Data.Array as Array
49-- >>> import qualified Streamly.Data.Fold as Fold
50-- >>> import qualified Streamly.Data.Fold as Fold
src/Streamly/Internal/Data/Stream/IsStream/Reduce.hs+9−10
@@ -152,7 +152,6 @@ where
152#include "inline.hs"
152#include "inline.hs"
153
153
154import Control.Exception (assert)
154import Control.Exception (assert)
155−import Control.Monad.Catch (MonadThrow)
156import Control.Monad.IO.Class (MonadIO(..))
155import Control.Monad.IO.Class (MonadIO(..))
157import Data.Heap (Entry(..))
156import Data.Heap (Entry(..))
158import Data.Kind (Type)
157import Data.Kind (Type)
@@ -163,7 +162,7 @@ import Streamly.Internal.Control.Concurrent (MonadAsync)
163import Streamly.Internal.Data.Fold.Type (Fold (..))
162import Streamly.Internal.Data.Fold.Type (Fold (..))
164import Streamly.Internal.Data.IsMap (IsMap(..))
163import Streamly.Internal.Data.IsMap (IsMap(..))
165import Streamly.Internal.Data.Refold.Type (Refold (..))
164import Streamly.Internal.Data.Refold.Type (Refold (..))
166−import Streamly.Internal.Data.Parser (Parser (..))
165+import Streamly.Internal.Data.Parser (Parser (..), ParseError)
167import Streamly.Internal.Data.Array.Type (Array)
166import Streamly.Internal.Data.Array.Type (Array)
168import Streamly.Internal.Data.Stream.IsStream.Common
167import Streamly.Internal.Data.Stream.IsStream.Common
169 ( fold
168 ( fold
@@ -363,7 +362,7 @@ refoldIterateM c i m = fromStreamD $ D.refoldIterateM c i (toStreamD m)
363-- parse combinator.
362-- parse combinator.
364--
363--
365-- >>> Stream.toList $ Stream.parseMany (Parser.takeBetween 0 2 Fold.sum) $ Stream.fromList [1..10]
364-- >>> Stream.toList $ Stream.parseMany (Parser.takeBetween 0 2 Fold.sum) $ Stream.fromList [1..10]
366−-- [3,7,11,15,19]
365+-- [Right 3,Right 7,Right 11,Right 15,Right 19]
367--
366--
368-- @
367-- @
369-- > Stream.toList $ Stream.parseMany (Parser.line Fold.toList) $ Stream.fromList "hello\\nworld"
368-- > Stream.toList $ Stream.parseMany (Parser.line Fold.toList) $ Stream.fromList "hello\\nworld"
@@ -382,10 +381,10 @@ refoldIterateM c i m = fromStreamD $ D.refoldIterateM c i (toStreamD m)
382--
381--
383{-# INLINE parseMany #-}
382{-# INLINE parseMany #-}
384parseMany
383parseMany
385− :: (IsStream t, MonadThrow m)
384+ :: (IsStream t, Monad m)
386 => Parser a m b
385 => Parser a m b
387 -> t m a
386 -> t m a
388− -> t m b
387+ -> t m (Either ParseError b)
389parseMany p m =
388parseMany p m =
390 fromStreamD $ D.parseMany (PRD.fromParserK p) (toStreamD m)
389 fromStreamD $ D.parseMany (PRD.fromParserK p) (toStreamD m)
391
390
@@ -395,10 +394,10 @@ parseMany p m =
395--
394--
396{-# INLINE parseManyD #-}
395{-# INLINE parseManyD #-}
397parseManyD
396parseManyD
398− :: (IsStream t, MonadThrow m)
397+ :: (IsStream t, Monad m)
399 => PRD.Parser a m b
398 => PRD.Parser a m b
400 -> t m a
399 -> t m a
401− -> t m b
400+ -> t m (Either ParseError b)
402parseManyD p m =
401parseManyD p m =
403 fromStreamD $ D.parseMany p (toStreamD m)
402 fromStreamD $ D.parseMany p (toStreamD m)
404
403
@@ -439,7 +438,7 @@ parseManyTill = undefined
439-- the result is used to generate the next parser and so on.
438-- the result is used to generate the next parser and so on.
440--
439--
441-- >>> import Data.Monoid (Sum(..))
440-- >>> import Data.Monoid (Sum(..))
442−-- >>> Stream.toList $ Stream.map getSum $ Stream.parseIterate (\b -> Parser.takeBetween 0 2 (Fold.sconcat b)) 0 $ Stream.map Sum $ Stream.fromList [1..10]
441+-- >>> Stream.toList $ fmap getSum $ Stream.rights $ Stream.parseIterate (\b -> Parser.takeBetween 0 2 (Fold.sconcat b)) (Sum 0) $ fmap Sum $ Stream.fromList [1..10]
443-- [3,10,21,36,55,55]
442-- [3,10,21,36,55,55]
444--
443--
445-- This is the streaming equivalent of monad like sequenced application of
444-- This is the streaming equivalent of monad like sequenced application of
@@ -449,11 +448,11 @@ parseManyTill = undefined
449--
448--
450{-# INLINE parseIterate #-}
449{-# INLINE parseIterate #-}
451parseIterate
450parseIterate
452− :: (IsStream t, MonadThrow m)
451+ :: (IsStream t, Monad m)
453 => (b -> Parser a m b)
452 => (b -> Parser a m b)
454 -> b
453 -> b
455 -> t m a
454 -> t m a
456− -> t m b
455+ -> t m (Either ParseError b)
457parseIterate f i m = fromStreamD $
456parseIterate f i m = fromStreamD $
458 D.parseIterate (PRD.fromParserK . f) i (toStreamD m)
457 D.parseIterate (PRD.fromParserK . f) i (toStreamD m)
459
458
src/Streamly/Internal/Data/Stream/IsStream/Top.hs+2−1
@@ -250,7 +250,8 @@ sortBy cmp =
250 Fold.toStreamRev
250 Fold.toStreamRev
251 Fold.toStream
251 Fold.toStream
252 in Stream.concatPairsWith (Stream.mergeBy cmp) id
252 in Stream.concatPairsWith (Stream.mergeBy cmp) id
253− . Stream.parseMany (fmap (either id id) p)
253+ . Stream.rights . Stream.parseMany (fmap (either id id) p)
254+
254
255
255------------------------------------------------------------------------------
256------------------------------------------------------------------------------
256-- SQL Joins
257-- SQL Joins
src/Streamly/Internal/FileSystem/Event/Darwin.hs+1−1
@@ -505,7 +505,7 @@ readOneEvent = do
505
505
506watchToStream :: Watch -> Stream IO Event
506watchToStream :: Watch -> Stream IO Event
507watchToStream (Watch handle _ _) =
507watchToStream (Watch handle _ _) =
508− S.parseMany readOneEvent $ S.unfold FH.reader handle
508+ S.rights $ S.parseMany readOneEvent $ S.unfold FH.reader handle
509
509
510-- XXX Write tests for all the points in macOS specific behavior.
510-- XXX Write tests for all the points in macOS specific behavior.
511--
511--
src/Streamly/Internal/FileSystem/Event/Linux.hs+1−1
@@ -868,7 +868,7 @@ watchToStream cfg wt@(Watch handle _) = do
868 -- sizeof(struct inotify_event) + NAME_MAX + 1
868 -- sizeof(struct inotify_event) + NAME_MAX + 1
869 --
869 --
870 -- will be sufficient to read at least one event.
870 -- will be sufficient to read at least one event.
871− S.parseMany (readOneEvent cfg wt) $ S.unfold FH.reader handle
871+ S.rights $ S.parseMany (readOneEvent cfg wt) $ S.unfold FH.reader handle
872
872
873-- XXX We should not go across the mount points of network file systems or file
873-- XXX We should not go across the mount points of network file systems or file
874-- systems that are known to not generate any events.
874-- systems that are known to not generate any events.
test/Streamly/Test/Data/Parser.hs+87−69
@@ -1,6 +1,6 @@
1module Main (main) where
1module Main (main) where
2
2
3−import Control.Exception (SomeException(..), displayException, try)
3+import Control.Exception (displayException)
4import Data.Foldable (for_)
4import Data.Foldable (for_)
5import Data.Word (Word8, Word32, Word64)
5import Data.Word (Word8, Word32, Word64)
6import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
6import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
@@ -24,6 +24,8 @@ import qualified Test.Hspec as H
24#if MIN_VERSION_QuickCheck(2,14,0)
24#if MIN_VERSION_QuickCheck(2,14,0)
25
25
26import Test.QuickCheck (chooseAny)
26import Test.QuickCheck (chooseAny)
27+import Control.Monad.Identity (Identity(runIdentity, Identity))
28+import Streamly.Internal.Data.Parser.ParserD (ParseError(..))
27
29
28#else
30#else
29
31
@@ -56,46 +58,49 @@ max_length = 1000
56fromFold :: Property
58fromFold :: Property
57fromFold =
59fromFold =
58 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
60 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
61+ monadicIO $ do
59− case (==) <$> S.parse (P.fromFold FL.sum) (S.fromList ls)
62+ s1 <- S.parse (P.fromFold FL.sum) (S.fromList ls)
60− <*> S.fold FL.sum (S.fromList ls) of
63+ o2 <- S.fold FL.sum (S.fromList ls)
61− Right is_equal -> is_equal
64+ return $
65+ case s1 of
66+ Right o1 -> o1 == o2
62− Left _ -> False
67+ Left _ -> False
63
68
64fromPure :: Property
69fromPure :: Property
65fromPure =
70fromPure =
66 forAll (chooseInt (min_value, max_value)) $ \x ->
71 forAll (chooseInt (min_value, max_value)) $ \x ->
67− case S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
72+ case runIdentity $ S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
68 Right r -> r == x
73 Right r -> r == x
69 Left _ -> False
74 Left _ -> False
70
75
71fromEffect :: Property
76fromEffect :: Property
72fromEffect =
77fromEffect =
73 forAll (chooseInt (min_value, max_value)) $ \x ->
78 forAll (chooseInt (min_value, max_value)) $ \x ->
74− case S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
79+ case runIdentity $ S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
75 Right r -> r == x
80 Right r -> r == x
76 Left _ -> False
81 Left _ -> False
77
82
78die :: Property
83die :: Property
79die =
84die =
80 property $
85 property $
81− case S.parse (P.die "die test") (S.fromList [0 :: Int]) of
86+ case runIdentity $ S.parse (P.die "die test") (S.fromList [0 :: Int]) of
82 Right _ -> False
87 Right _ -> False
83 Left _ -> True
88 Left _ -> True
84
89
85dieM :: Property
90dieM :: Property
86dieM =
91dieM =
87 property $
92 property $
88− case S.parse (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
93+ case runIdentity $ S.parse (P.dieM (Identity "die test")) (S.fromList [0 :: Int]) of
89 Right _ -> False
94 Right _ -> False
90 Left _ -> True
95 Left _ -> True
91
96
92parserFail :: Property
97parserFail :: Property
93parserFail =
98parserFail =
94 property $
99 property $
95− case S.parse (fail err) (S.fromList [0 :: Int]) of
100+ case runIdentity $ S.parse (fail err) (S.fromList [0 :: Int]) of
96− Right _ -> False
101+ Right _ -> False
97− Left (SomeException e) -> err == displayException e
102+ Left (ParseError e) -> err == e
98− where
103+ where
99 err = "Testing MonadFail.fail."
104 err = "Testing MonadFail.fail."
100
105
101-- Element Parser Tests
106-- Element Parser Tests
@@ -104,7 +109,7 @@ peekPass :: Property
104peekPass =
109peekPass =
105 forAll (chooseInt (1, max_length)) $ \list_length ->
110 forAll (chooseInt (1, max_length)) $ \list_length ->
106 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
111 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
107− case S.parse P.peek (S.fromList ls) of
112+ case runIdentity $ S.parse P.peek (S.fromList ls) of
108 Right head_value -> case ls of
113 Right head_value -> case ls of
109 head_ls : _ -> head_value == head_ls
114 head_ls : _ -> head_value == head_ls
110 _ -> False
115 _ -> False
@@ -112,13 +117,13 @@ peekPass =
112
117
113peekFail :: Property
118peekFail :: Property
114peekFail =
119peekFail =
115− property (case S.parse P.peek (S.fromList []) of
120+ property (case runIdentity $ S.parse P.peek (S.fromList []) of
116 Right _ -> False
121 Right _ -> False
117 Left _ -> True)
122 Left _ -> True)
118
123
119eofPass :: Property
124eofPass :: Property
120eofPass =
125eofPass =
121− property (case S.parse P.eof (S.fromList []) of
126+ property (case runIdentity $ S.parse P.eof (S.fromList []) of
122 Right _ -> True
127 Right _ -> True
123 Left _ -> False)
128 Left _ -> False)
124
129
@@ -126,7 +131,7 @@ eofFail :: Property
126eofFail =
131eofFail =
127 forAll (chooseInt (1, max_length)) $ \list_length ->
132 forAll (chooseInt (1, max_length)) $ \list_length ->
128 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
133 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
129− case S.parse P.eof (S.fromList ls) of
134+ case runIdentity $ S.parse P.eof (S.fromList ls) of
130 Right _ -> False
135 Right _ -> False
131 Left _ -> True
136 Left _ -> True
132
137
@@ -138,14 +143,14 @@ satisfyPass =
138 ls = first_element : ls_tail
143 ls = first_element : ls_tail
139 predicate = (>= mid_value)
144 predicate = (>= mid_value)
140 in
145 in
141− case S.parse (P.satisfy predicate) (S.fromList ls) of
146+ case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
142 Right r -> r == first_element
147 Right r -> r == first_element
143 Left _ -> False
148 Left _ -> False
144
149
145satisfy :: Property
150satisfy :: Property
146satisfy =
151satisfy =
147 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
152 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
148− case S.parse (P.satisfy predicate) (S.fromList ls) of
153+ case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
149 Right r -> case ls of
154 Right r -> case ls of
150 [] -> False
155 [] -> False
151 (x : _) -> predicate x && (r == x)
156 (x : _) -> predicate x && (r == x)
@@ -158,15 +163,16 @@ satisfy =
158onePass :: Property
163onePass :: Property
159onePass =
164onePass =
160 forAll (chooseInt (1, max_value)) $ \int ->
165 forAll (chooseInt (1, max_value)) $ \int ->
161− property (case S.parse P.one (S.fromList [int]) of
166+ property (case runIdentity $ S.parse P.one (S.fromList [int]) of
162 Right i -> i == int
167 Right i -> i == int
163 Left _ -> False)
168 Left _ -> False)
164
169
165one :: Property
170one :: Property
166one =
171one =
172+ property $
167− property (case S.parse P.one (S.fromList []) of
173+ case runIdentity $ S.parse P.one (S.fromList []) of
168− Left _ -> True
174+ Left _ -> True
169− Right _ -> False)
175+ Right _ -> False
170
176
171-- Sequence Parsers Tests
177-- Sequence Parsers Tests
172takeBetweenPass :: Property
178takeBetweenPass :: Property
@@ -176,7 +182,7 @@ takeBetweenPass =
176 forAll (chooseInt (m, max_value)) $ \list_length ->
182 forAll (chooseInt (m, max_value)) $ \list_length ->
177 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
183 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
178 $ \ls ->
184 $ \ls ->
179− case S.parse (P.takeBetween m n FL.toList)
185+ case runIdentity $ S.parse (P.takeBetween m n FL.toList)
180 (S.fromList ls) of
186 (S.fromList ls) of
181 Right parsed_list ->
187 Right parsed_list ->
182 let lpl = Prelude.length parsed_list
188 let lpl = Prelude.length parsed_list
@@ -195,25 +201,25 @@ takeBetween =
195
201
196 go m n ls =
202 go m n ls =
197 let inputLen = Prelude.length ls
203 let inputLen = Prelude.length ls
198− in monadicIO $ do
204+ in do
199 let p = P.takeBetween m n FL.toList
205 let p = P.takeBetween m n FL.toList
200− r <- run $ try $ S.parse p (S.fromList ls)
206+ case runIdentity $ S.parse p (S.fromList ls) of
201− return $ case r of
202 Right xs ->
207 Right xs ->
203 let parsedLen = Prelude.length xs
208 let parsedLen = Prelude.length xs
204− in if inputLen >= m && parsedLen >= m && parsedLen <= n
209+ in if inputLen >= m && parsedLen >= m && parsedLen <= n
205 then checkListEqual xs $ Prelude.take parsedLen ls
210 then checkListEqual xs $ Prelude.take parsedLen ls
206 else property False
211 else property False
207− Left (_ :: SomeException) ->
212+ Left _ ->
208 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
213 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
209
214
215+
210takeEQPass :: Property
216takeEQPass :: Property
211takeEQPass =
217takeEQPass =
212 forAll (chooseInt (min_value, max_value)) $ \n ->
218 forAll (chooseInt (min_value, max_value)) $ \n ->
213 forAll (chooseInt (n, max_value)) $ \list_length ->
219 forAll (chooseInt (n, max_value)) $ \list_length ->
214 forAll (vectorOf list_length
220 forAll (vectorOf list_length
215 (chooseInt (min_value, max_value))) $ \ls ->
221 (chooseInt (min_value, max_value))) $ \ls ->
216− case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
222+ case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
217 Right parsed_list ->
223 Right parsed_list ->
218 checkListEqual parsed_list (Prelude.take n ls)
224 checkListEqual parsed_list (Prelude.take n ls)
219 Left _ -> property False
225 Left _ -> property False
@@ -225,7 +231,7 @@ takeEQ =
225 let
231 let
226 list_length = Prelude.length ls
232 list_length = Prelude.length ls
227 in
233 in
228− case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
234+ case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
229 Right parsed_list ->
235 Right parsed_list ->
230 if n <= list_length
236 if n <= list_length
231 then checkListEqual parsed_list (Prelude.take n ls)
237 then checkListEqual parsed_list (Prelude.take n ls)
@@ -238,7 +244,7 @@ takeGEPass =
238 forAll (chooseInt (n, max_value)) $ \list_length ->
244 forAll (chooseInt (n, max_value)) $ \list_length ->
239 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
245 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
240 $ \ls ->
246 $ \ls ->
241− case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
247+ case runIdentity $ S.parse (P.takeGE n FL.toList) (S.fromList ls) of
242 Right parsed_list -> checkListEqual parsed_list ls
248 Right parsed_list -> checkListEqual parsed_list ls
243 Left _ -> property False
249 Left _ -> property False
244
250
@@ -249,24 +255,25 @@ takeGE =
249 let
255 let
250 list_length = Prelude.length ls
256 list_length = Prelude.length ls
251 in
257 in
252− case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
258+ case runIdentity $S.parse (P.takeGE n FL.toList) (S.fromList ls) of
253 Right parsed_list ->
259 Right parsed_list ->
254 if n <= list_length
260 if n <= list_length
255 then checkListEqual parsed_list ls
261 then checkListEqual parsed_list ls
256 else property False
262 else property False
257 Left _ -> property (n > list_length)
263 Left _ -> property (n > list_length)
258
264
265+
259nLessThanEqual0 ::
266nLessThanEqual0 ::
260 ( Int
267 ( Int
261− -> FL.Fold (Either SomeException) Int [Int]
268+ -> FL.Fold Identity Int [Int]
262− -> P.Parser Int (Either SomeException) [Int]
269+ -> P.Parser Int Identity [Int]
263 )
270 )
264 -> (Int -> [Int] -> [Int])
271 -> (Int -> [Int] -> [Int])
265 -> Property
272 -> Property
266nLessThanEqual0 tk ltk =
273nLessThanEqual0 tk ltk =
267 forAll (elements [0, (-1)]) $ \n ->
274 forAll (elements [0, (-1)]) $ \n ->
268 forAll (listOf arbitrary) $ \ls ->
275 forAll (listOf arbitrary) $ \ls ->
269− case S.parse (tk n FL.toList) (S.fromList ls) of
276+ case runIdentity $ S.parse (tk n FL.toList) (S.fromList ls) of
270 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
277 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
271 Left _ -> property False
278 Left _ -> property False
272
279
@@ -330,7 +337,7 @@ takeProperties =
330takeEndBy_ :: Property
337takeEndBy_ :: Property
331takeEndBy_ =
338takeEndBy_ =
332 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
339 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
333− case S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
340+ case runIdentity $ S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
334 Right parsed_list ->
341 Right parsed_list ->
335 checkListEqual parsed_list (tkwhl ls)
342 checkListEqual parsed_list (tkwhl ls)
336 Left _ -> property False
343 Left _ -> property False
@@ -344,7 +351,7 @@ takeStartBy =
344 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
351 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
345 let ls1 = 1:ls
352 let ls1 = 1:ls
346 in
353 in
347− case S.parse parser (S.fromList ls1) of
354+ case runIdentity $ S.parse parser (S.fromList ls1) of
348 Right parsed_list ->
355 Right parsed_list ->
349 if not $ Prelude.null ls1
356 if not $ Prelude.null ls1
350 then
357 then
@@ -362,7 +369,7 @@ takeStartBy =
362takeWhile :: Property
369takeWhile :: Property
363takeWhile =
370takeWhile =
364 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
371 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
365− case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
372+ case runIdentity $ S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
366 Right parsed_list ->
373 Right parsed_list ->
367 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
374 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
368 Left _ -> property False
375 Left _ -> property False
@@ -375,7 +382,7 @@ takeP =
375 ((,) <$> chooseInt (min_value, max_value)
382 ((,) <$> chooseInt (min_value, max_value)
376 <*> listOf (chooseInt (0, 1)))
383 <*> listOf (chooseInt (0, 1)))
377 $ \(takeNum, ls) ->
384 $ \(takeNum, ls) ->
378− case S.parse
385+ case runIdentity $ S.parse
379 (P.takeP takeNum (P.fromFold FL.toList))
386 (P.takeP takeNum (P.fromFold FL.toList))
380 (S.fromList ls) of
387 (S.fromList ls) of
381 Right parsed_list ->
388 Right parsed_list ->
@@ -385,7 +392,7 @@ takeP =
385takeWhile1 :: Property
392takeWhile1 :: Property
386takeWhile1 =
393takeWhile1 =
387 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
394 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
388− case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
395+ case runIdentity $ S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
389 Right parsed_list -> case ls of
396 Right parsed_list -> case ls of
390 [] -> property False
397 [] -> property False
391 (x : _) ->
398 (x : _) ->
@@ -415,7 +422,7 @@ takeWhileP =
415 takeWhileTillLen maxLen prd list =
422 takeWhileTillLen maxLen prd list =
416 Prelude.take maxLen $ Prelude.takeWhile prd list
423 Prelude.take maxLen $ Prelude.takeWhile prd list
417 in
424 in
418− case S.parse prsr (S.fromList ls) of
425+ case runIdentity $ S.parse prsr (S.fromList ls) of
419 Right parsed_list ->
426 Right parsed_list ->
420 checkListEqual
427 checkListEqual
421 parsed_list
428 parsed_list
@@ -444,7 +451,7 @@ groupBy :: Property
444groupBy =
451groupBy =
445 forAll (listOf (chooseInt (0, 1)))
452 forAll (listOf (chooseInt (0, 1)))
446 $ \ls ->
453 $ \ls ->
447− case S.parse parser (S.fromList ls) of
454+ case runIdentity $ S.parse parser (S.fromList ls) of
448 Right parsed -> checkListEqual parsed (groupByLF ls)
455 Right parsed -> checkListEqual parsed (groupByLF ls)
449 Left _ -> property False
456 Left _ -> property False
450
457
@@ -460,7 +467,7 @@ wordBy :: Property
460wordBy =
467wordBy =
461 forAll (listOf (elements [' ', 's']))
468 forAll (listOf (elements [' ', 's']))
462 $ \ls ->
469 $ \ls ->
463− case S.parse parser (S.fromList ls) of
470+ case runIdentity $ S.parse parser (S.fromList ls) of
464 Right parsed -> checkListEqual parsed (words' ls)
471 Right parsed -> checkListEqual parsed (words' ls)
465 Left _ -> property False
472 Left _ -> property False
466
473
@@ -486,7 +493,7 @@ parseManyWordQuotedBy =
486 spc _ = False
493 spc _ = False
487
494
488 parser = P.wordQuotedBy kQ esc lQ rQ fromLQ spc FL.toList
495 parser = P.wordQuotedBy kQ esc lQ rQ fromLQ spc FL.toList
489− result <- H.runIO $ S.fold FL.toList $ S.parseMany parser inpStrm
496+ result <- H.runIO $ S.fold FL.toList $ S.rights $ S.parseMany parser inpStrm
490 H.it (showCase c) $ result `H.shouldBe` expected
497 H.it (showCase c) $ result `H.shouldBe` expected
491
498
492 where
499 where
@@ -595,7 +602,7 @@ parseManyWordQuotedBy =
595deintercalate :: Property
602deintercalate :: Property
596deintercalate =
603deintercalate =
597 forAll (listOf (chooseAny :: Gen Int)) $ \ls ->
604 forAll (listOf (chooseAny :: Gen Int)) $ \ls ->
598− case S.parse p (S.fromList ls) of
605+ case runIdentity $ S.parse p (S.fromList ls) of
599 Right evenOdd -> evenOdd == List.partition even ls
606 Right evenOdd -> evenOdd == List.partition even ls
600 Left _ -> False
607 Left _ -> False
601
608
@@ -645,7 +652,7 @@ many =
645 prsr =
652 prsr =
646 flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
653 flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
647 in
654 in
648− case S.parse prsr (S.fromList ls) of
655+ case runIdentity $ S.parse prsr (S.fromList ls) of
649 Right res_list -> checkListEqual res_list
656 Right res_list -> checkListEqual res_list
650 $ Prelude.filter (== 0) ls
657 $ Prelude.filter (== 0) ls
651 Left _ -> property False
658 Left _ -> property False
@@ -666,7 +673,7 @@ some =
666 prsr =
673 prsr =
667 flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
674 flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
668 in
675 in
669− case S.parse prsr (S.fromList ls) of
676+ case runIdentity $ S.parse prsr (S.fromList ls) of
670 Right res_list -> res_list == Prelude.filter (== 0) ls
677 Right res_list -> res_list == Prelude.filter (== 0) ls
671 Left _ -> False
678 Left _ -> False
672
679
@@ -689,10 +696,11 @@ applicative =
689 <$> P.fromFold (FL.take (length list1) FL.toList)
696 <$> P.fromFold (FL.take (length list1) FL.toList)
690 <*> P.fromFold (FL.take (length list2) FL.toList)
697 <*> P.fromFold (FL.take (length list2) FL.toList)
691 in monadicIO $ do
698 in monadicIO $ do
692− (olist1, olist2) <-
693− run $ S.parse parser (S.fromList $ list1 ++ list2)
699+ s <- S.parse parser (S.fromList $ list1 ++ list2)
694− listEquals (==) olist1 list1
700+ return $
695− listEquals (==) olist2 list2
701+ case s of
702+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
703+ Left _ -> False
696
704
697sequence :: Property
705sequence :: Property
698sequence =
706sequence =
@@ -703,7 +711,10 @@ sequence =
703 S.parse
711 S.parse
704 (Prelude.sequence $ fmap p ins)
712 (Prelude.sequence $ fmap p ins)
705 (S.fromList $ concat ins)
713 (S.fromList $ concat ins)
706− listEquals (==) outs ins
714+ return $
715+ case outs of
716+ Right ls -> ls == ins
717+ Left _ -> False
707
718
708monad :: Property
719monad :: Property
709monad =
720monad =
@@ -714,10 +725,11 @@ monad =
714 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
725 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
715 return (olist1, olist2)
726 return (olist1, olist2)
716 in monadicIO $ do
727 in monadicIO $ do
717− (olist1, olist2) <-
718− run $ S.parse parser (S.fromList $ list1 ++ list2)
728+ s <- S.parse parser (S.fromList $ list1 ++ list2)
719− listEquals (==) olist1 list1
729+ return $
720− listEquals (==) olist2 list2
730+ case s of
731+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
732+ Left _ -> False
721
733
722-------------------------------------------------------------------------------
734-------------------------------------------------------------------------------
723-- Stream parsing
735-- Stream parsing
@@ -732,6 +744,7 @@ parseMany =
732 let p = P.fromFold $ FL.take len FL.toList
744 let p = P.fromFold $ FL.take len FL.toList
733 run
745 run
734 $ S.fold FL.toList
746 $ S.fold FL.toList
747+ $ S.rights
735 $ S.parseMany p (S.fromList $ concat ins)
748 $ S.parseMany p (S.fromList $ concat ins)
736 listEquals (==) outs ins
749 listEquals (==) outs ins
737
750
@@ -786,6 +799,7 @@ parseMany2Events =
786 xs <-
799 xs <-
787 ( run
800 ( run
788 $ S.fold FL.toList
801 $ S.fold FL.toList
802+ $ S.rights
789 $ S.parseMany readOneEvent
803 $ S.parseMany readOneEvent
790 $ S.fromList (concat (replicate 2 event))
804 $ S.fromList (concat (replicate 2 event))
791 )
805 )
@@ -805,8 +819,11 @@ manyEqParseMany =
805 monadicIO $ do
819 monadicIO $ do
806 let strm = S.fromList lst
820 let strm = S.fromList lst
807 r1 <- run $ S.parse (P.many (split i) FL.toList) strm
821 r1 <- run $ S.parse (P.many (split i) FL.toList) strm
808− r2 <- run $ S.fold FL.toList $ S.parseMany (split i) strm
822+ r2 <- run $ S.fold FL.toList $ S.rights $ S.parseMany (split i) strm
809− assert $ r1 == r2
823+ return $
824+ case r1 of
825+ Right o1 -> o1 == r2
826+ Left _ -> False
810
827
811 where
828 where
812
829
@@ -816,7 +833,7 @@ manyEqParseMany =
816takeEndBy1 :: Property
833takeEndBy1 :: Property
817takeEndBy1 =
834takeEndBy1 =
818 forAll (listOf (chooseInt (0, 1))) $ \ls ->
835 forAll (listOf (chooseInt (0, 1))) $ \ls ->
819− case S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
836+ case runIdentity $ S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
820 Right parsed_list ->
837 Right parsed_list ->
821 checkListEqual
838 checkListEqual
822 parsed_list
839 parsed_list
@@ -847,8 +864,9 @@ takeEndBy2 =
847 predicate = (==0)
864 predicate = (==0)
848
865
849 eitherParsedList =
866 eitherParsedList =
850− S.fold FL.toList $
867+ S.fold FL.toList
868+ $ S.rights
851− S.parseMany (P.takeEndBy predicate prsr) strm
869+ $ S.parseMany (P.takeEndBy predicate prsr) strm
852
870
853 where
871 where
854
872
@@ -905,7 +923,7 @@ takeEndByEsc =
905 Just _ ->
923 Just _ ->
906 x : escapeSep Nothing xs
924 x : escapeSep Nothing xs
907 in
925 in
908− case S.parse prsr (S.fromList ls) of
926+ case runIdentity $ S.parse prsr (S.fromList ls) of
909 Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
927 Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
910 Left err -> property (displayException err == msg)
928 Left err -> property (displayException err == msg)
911
929
@@ -981,7 +999,7 @@ takeFramedByEsc_ =
981 in
999 in
982 helper l Nothing (0 :: Int)
1000 helper l Nothing (0 :: Int)
983 in
1001 in
984− case S.parse prsr (S.fromList ls) of
1002+ case runIdentity $ S.parse prsr (S.fromList ls) of
985 Right parsed_list ->
1003 Right parsed_list ->
986 if checkPassBeg ls
1004 if checkPassBeg ls
987 then checkListEqual parsed_list $
1005 then checkListEqual parsed_list $
@@ -1038,7 +1056,7 @@ takeFramedByEsc_Pass =
1038 in
1056 in
1039 helper l Nothing (0 :: Int)
1057 helper l Nothing (0 :: Int)
1040 in
1058 in
1041− case S.parse prsr (S.fromList ls) of
1059+ case runIdentity $ S.parse prsr (S.fromList ls) of
1042 Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
1060 Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
1043 _ -> property False
1061 _ -> property False
1044
1062
@@ -1057,7 +1075,7 @@ takeFramedByEsc_Fail1 =
1057
1075
1058 ls = [0 :: Int]
1076 ls = [0 :: Int]
1059 in
1077 in
1060− case S.parse prsr (S.fromList ls) of
1078+ case runIdentity $ S.parse prsr (S.fromList ls) of
1061 Right _ -> property False
1079 Right _ -> property False
1062 Left err -> property (displayException err == msg)
1080 Left err -> property (displayException err == msg)
1063
1081
@@ -1076,7 +1094,7 @@ takeFramedByEsc_Fail2 =
1076
1094
1077 ls = [1 :: Int]
1095 ls = [1 :: Int]
1078 in
1096 in
1079− case S.parse prsr (S.fromList ls) of
1097+ case runIdentity $ S.parse prsr (S.fromList ls) of
1080 Right _ -> property False
1098 Right _ -> property False
1081 Left err -> property (displayException err == msg)
1099 Left err -> property (displayException err == msg)
1082
1100
@@ -1095,7 +1113,7 @@ takeFramedByEsc_Fail3 =
1095
1113
1096 ls = [2 :: Int]
1114 ls = [2 :: Int]
1097 in
1115 in
1098− case S.parse prsr (S.fromList ls) of
1116+ case runIdentity $ S.parse prsr (S.fromList ls) of
1099 Right _ -> property False
1117 Right _ -> property False
1100 Left err -> property $ (displayException err == msg)
1118 Left err -> property $ (displayException err == msg)
1101
1119
@@ -1105,7 +1123,7 @@ takeStartBy_ =
1105 let ls1 = 1:ls
1123 let ls1 = 1:ls
1106 msg = "takeFramedByGeneric: empty token"
1124 msg = "takeFramedByGeneric: empty token"
1107 in
1125 in
1108− case S.parse parser (S.fromList ls1) of
1126+ case runIdentity $ S.parse parser (S.fromList ls1) of
1109 Right parsed_list ->
1127 Right parsed_list ->
1110 if not $ Prelude.null ls1
1128 if not $ Prelude.null ls1
1111 then
1129 then
test/Streamly/Test/Data/Parser/Chunked.hs+15−13
@@ -1,17 +1,16 @@
1module Main (main) where
1module Main (main) where
2
2
3−import Control.Monad.Catch (MonadThrow)
4-- import Control.Exception (SomeException(..), displayException, try)
3-- import Control.Exception (SomeException(..), displayException, try)
5-- import Data.Foldable (for_)
4-- import Data.Foldable (for_)
6-- import Data.Word (Word8, Word32, Word64)
5-- import Data.Word (Word8, Word32, Word64)
6+import Streamly.Internal.Data.Parser (ParseError)
7import Streamly.Internal.Data.Unboxed (Unbox)
7import Streamly.Internal.Data.Unboxed (Unbox)
8−import Streamly.Test.Common (listEquals)
9import Test.Hspec (hspec, describe)
8import Test.Hspec (hspec, describe)
10import Test.Hspec.QuickCheck
9import Test.Hspec.QuickCheck
11import Test.QuickCheck
10import Test.QuickCheck
12 (forAll, Property, listOf,
11 (forAll, Property, listOf,
13 Gen)
12 Gen)
14−import Test.QuickCheck.Monadic (monadicIO, run)
13+import Test.QuickCheck.Monadic (monadicIO)
15
14
16import Prelude hiding (sequence)
15import Prelude hiding (sequence)
17
16
@@ -685,8 +684,8 @@ some =
685-- Instances
684-- Instances
686-------------------------------------------------------------------------------
685-------------------------------------------------------------------------------
687
686
688−parse :: (MonadThrow f, Unbox a) =>
687+parse :: (Monad f, Unbox a) =>
689− P.ParserChunked a f b -> S.Stream f (A.Array a) -> f b
688+ P.ParserChunked a f b -> S.Stream f (A.Array a) -> f (Either ParseError b)
690parse parser stream = fmap fst (P.parseBreak parser stream)
689parse parser stream = fmap fst (P.parseBreak parser stream)
691
690
692applicative :: Property
691applicative :: Property
@@ -699,10 +698,12 @@ applicative =
699 <*> ParserD.fromFold (FL.take (length list2) FL.toList)
698 <*> ParserD.fromFold (FL.take (length list2) FL.toList)
700 in monadicIO $ do
699 in monadicIO $ do
701 let arrays = [A.fromList list1, A.fromList list2]
700 let arrays = [A.fromList list1, A.fromList list2]
702− (olist1, olist2) <-
703− run $ parse (P.fromParserD parser) (S.fromList arrays)
701+ s1 <- parse (P.fromParserD parser) (S.fromList arrays)
704− listEquals (==) olist1 list1
702+ return $
705− listEquals (==) olist2 list2
703+ case s1 of
704+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
705+ Left _ -> False
706+
706
707
707{-
708{-
708sequence :: Property
709sequence :: Property
@@ -727,10 +728,11 @@ monad =
727 return (olist1, olist2)
728 return (olist1, olist2)
728 in monadicIO $ do
729 in monadicIO $ do
729 let arrays = [A.fromList list1, A.fromList list2]
730 let arrays = [A.fromList list1, A.fromList list2]
730− (olist1, olist2) <-
731− run $ parse (P.fromParserD parser) (S.fromList arrays)
731+ s1 <- parse (P.fromParserD parser) (S.fromList arrays)
732− listEquals (==) olist1 list1
732+ return $
733− listEquals (==) olist2 list2
733+ case s1 of
734+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
735+ Left _ -> False
734
736
735{-
737{-
736-------------------------------------------------------------------------------
738-------------------------------------------------------------------------------
test/Streamly/Test/Data/Parser/ParserD.hs+100−84
@@ -3,8 +3,8 @@ module Main (main) where
3import Control.Applicative ((<|>))
3import Control.Applicative ((<|>))
4import Control.Exception (SomeException(..), try)
4import Control.Exception (SomeException(..), try)
5import Data.Word (Word8, Word32, Word64)
5import Data.Word (Word8, Word32, Word64)
6−import Streamly.Test.Common (listEquals, checkListEqual, chooseInt, equals)
6+import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
7−import Test.Hspec (Spec, hspec, describe, Expectation, it, shouldReturn)
7+import Test.Hspec (Spec, hspec, describe)
8import Test.Hspec.QuickCheck
8import Test.Hspec.QuickCheck
9import Test.QuickCheck
9import Test.QuickCheck
10 (arbitrary, forAll, elements, Property,
10 (arbitrary, forAll, elements, Property,
@@ -28,7 +28,7 @@ import Prelude hiding (sequence)
28#if MIN_VERSION_QuickCheck(2,14,0)
28#if MIN_VERSION_QuickCheck(2,14,0)
29
29
30import Test.QuickCheck (chooseAny)
30import Test.QuickCheck (chooseAny)
31−
31+import Control.Monad.Identity (runIdentity, Identity (Identity))
32
32
33#else
33#else
34
34
@@ -65,7 +65,7 @@ fromFold :: Property
65fromFold =
65fromFold =
66 forAll (listOf $ chooseInt (min_value, max_value))
66 forAll (listOf $ chooseInt (min_value, max_value))
67 $ \ls ->
67 $ \ls ->
68− case (==) <$> (S.parseD (P.fromFold FL.sum) (S.fromList ls))
68+ case (==) <$> runIdentity (S.parseD (P.fromFold FL.sum) (S.fromList ls))
69 <*> (S.fold FL.sum (S.fromList ls)) of
69 <*> (S.fold FL.sum (S.fromList ls)) of
70 Right is_equal -> is_equal
70 Right is_equal -> is_equal
71 Left _ -> False
71 Left _ -> False
@@ -73,28 +73,28 @@ fromFold =
73fromPure :: Property
73fromPure :: Property
74fromPure =
74fromPure =
75 forAll (chooseInt (min_value, max_value)) $ \x ->
75 forAll (chooseInt (min_value, max_value)) $ \x ->
76− case S.parseD (P.fromPure x) (S.fromList [1 :: Int]) of
76+ case runIdentity $ S.parseD (P.fromPure x) (S.fromList [1 :: Int]) of
77 Right r -> r == x
77 Right r -> r == x
78 Left _ -> False
78 Left _ -> False
79
79
80fromEffect :: Property
80fromEffect :: Property
81fromEffect =
81fromEffect =
82 forAll (chooseInt (min_value, max_value)) $ \x ->
82 forAll (chooseInt (min_value, max_value)) $ \x ->
83− case S.parseD (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
83+ case runIdentity $ S.parseD (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
84 Right r -> r == x
84 Right r -> r == x
85 Left _ -> False
85 Left _ -> False
86
86
87die :: Property
87die :: Property
88die =
88die =
89 property $
89 property $
90− case S.parseD (P.die "die test") (S.fromList [0 :: Int]) of
90+ case runIdentity (S.parseD (P.die "die test") (S.fromList [0 :: Int])) of
91 Right _ -> False
91 Right _ -> False
92 Left _ -> True
92 Left _ -> True
93
93
94dieM :: Property
94dieM :: Property
95dieM =
95dieM =
96 property $
96 property $
97− case S.parseD (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
97+ case runIdentity (S.parseD (P.dieM (Identity "die test")) (S.fromList [0 :: Int])) of
98 Right _ -> False
98 Right _ -> False
99 Left _ -> True
99 Left _ -> True
100
100
@@ -104,7 +104,7 @@ peekPass :: Property
104peekPass =
104peekPass =
105 forAll (chooseInt (1, max_length)) $ \list_length ->
105 forAll (chooseInt (1, max_length)) $ \list_length ->
106 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
106 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
107− case S.parseD P.peek (S.fromList ls) of
107+ case runIdentity $ S.parseD P.peek (S.fromList ls) of
108 Right head_value -> case ls of
108 Right head_value -> case ls of
109 head_ls : _ -> head_value == head_ls
109 head_ls : _ -> head_value == head_ls
110 _ -> False
110 _ -> False
@@ -112,7 +112,7 @@ peekPass =
112
112
113peekFail :: Property
113peekFail :: Property
114peekFail =
114peekFail =
115− property (case S.parseD P.peek (S.fromList []) of
115+ property (case runIdentity $ S.parseD P.peek (S.fromList []) of
116 Right _ -> False
116 Right _ -> False
117 Left _ -> True)
117 Left _ -> True)
118
118
@@ -126,7 +126,7 @@ eofFail :: Property
126eofFail =
126eofFail =
127 forAll (chooseInt (1, max_length)) $ \list_length ->
127 forAll (chooseInt (1, max_length)) $ \list_length ->
128 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
128 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
129− case S.parseD P.eof (S.fromList ls) of
129+ case runIdentity $S.parseD P.eof (S.fromList ls) of
130 Right _ -> False
130 Right _ -> False
131 Left _ -> True
131 Left _ -> True
132
132
@@ -138,14 +138,14 @@ satisfyPass =
138 ls = first_element : ls_tail
138 ls = first_element : ls_tail
139 predicate = (>= mid_value)
139 predicate = (>= mid_value)
140 in
140 in
141− case S.parseD (P.satisfy predicate) (S.fromList ls) of
141+ case runIdentity $ S.parseD (P.satisfy predicate) (S.fromList ls) of
142 Right r -> r == first_element
142 Right r -> r == first_element
143 Left _ -> False
143 Left _ -> False
144
144
145satisfy :: Property
145satisfy :: Property
146satisfy =
146satisfy =
147 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
147 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
148− case S.parseD (P.satisfy predicate) (S.fromList ls) of
148+ case runIdentity $ S.parseD (P.satisfy predicate) (S.fromList ls) of
149 Right r -> case ls of
149 Right r -> case ls of
150 [] -> False
150 [] -> False
151 (x : _) -> predicate x && (r == x)
151 (x : _) -> predicate x && (r == x)
@@ -162,7 +162,7 @@ takeBetweenPass =
162 forAll (chooseInt (m, max_value)) $ \n ->
162 forAll (chooseInt (m, max_value)) $ \n ->
163 forAll (chooseInt (m, max_value)) $ \list_length ->
163 forAll (chooseInt (m, max_value)) $ \list_length ->
164 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
164 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
165− case S.parseD (P.takeBetween m n FL.toList) (S.fromList ls) of
165+ case runIdentity $ S.parseD (P.takeBetween m n FL.toList) (S.fromList ls) of
166 Right parsed_list ->
166 Right parsed_list ->
167 let lpl = Prelude.length parsed_list
167 let lpl = Prelude.length parsed_list
168 in checkListEqual parsed_list (Prelude.take lpl ls)
168 in checkListEqual parsed_list (Prelude.take lpl ls)
@@ -180,13 +180,16 @@ takeBetween =
180 let p = P.takeBetween m n FL.toList
180 let p = P.takeBetween m n FL.toList
181 r <- run $ try $ S.parseD p (S.fromList ls)
181 r <- run $ try $ S.parseD p (S.fromList ls)
182 return $ case r of
182 return $ case r of
183− Right parsed_list ->
183+ Right x -> case x of
184+ Right parsed_list ->
184− if m <= list_length && n >= m
185+ if m <= list_length && n >= m
185− then
186+ then
186− let len = Prelude.length parsed_list
187+ let len = Prelude.length parsed_list
187− in checkListEqual
188+ in checkListEqual
188− parsed_list (Prelude.take len ls)
189+ parsed_list (Prelude.take len ls)
189− else property False
190+ else property False
191+ Left _ ->
192+ property (m > n || list_length < m)
190 Left (_ :: SomeException) ->
193 Left (_ :: SomeException) ->
191 property (m > n || list_length < m)
194 property (m > n || list_length < m)
192
195
@@ -194,7 +197,7 @@ take :: Property
194take =
197take =
195 forAll (chooseInt (min_value, max_value)) $ \n ->
198 forAll (chooseInt (min_value, max_value)) $ \n ->
196 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
199 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
197− case S.parseD (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
200+ case runIdentity $ S.parseD (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
198 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
201 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
199 Left _ -> property False
202 Left _ -> property False
200
203
@@ -203,7 +206,7 @@ takeEQPass =
203 forAll (chooseInt (min_value, max_value)) $ \n ->
206 forAll (chooseInt (min_value, max_value)) $ \n ->
204 forAll (chooseInt (n, max_value)) $ \list_length ->
207 forAll (chooseInt (n, max_value)) $ \list_length ->
205 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
208 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
206− case S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
209+ case runIdentity $ S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
207 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
210 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
208 Left _ -> property False
211 Left _ -> property False
209
212
@@ -214,7 +217,7 @@ takeEQ =
214 let
217 let
215 list_length = Prelude.length ls
218 list_length = Prelude.length ls
216 in
219 in
217− case S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
220+ case runIdentity $ S.parseD (P.takeEQ n FL.toList) (S.fromList ls) of
218 Right parsed_list ->
221 Right parsed_list ->
219 if (n <= list_length) then
222 if (n <= list_length) then
220 checkListEqual parsed_list (Prelude.take n ls)
223 checkListEqual parsed_list (Prelude.take n ls)
@@ -227,7 +230,7 @@ takeGEPass =
227 forAll (chooseInt (min_value, max_value)) $ \n ->
230 forAll (chooseInt (min_value, max_value)) $ \n ->
228 forAll (chooseInt (n, max_value)) $ \list_length ->
231 forAll (chooseInt (n, max_value)) $ \list_length ->
229 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
232 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
230− case S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
233+ case runIdentity $ S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
231 Right parsed_list -> checkListEqual parsed_list ls
234 Right parsed_list -> checkListEqual parsed_list ls
232 Left _ -> property False
235 Left _ -> property False
233
236
@@ -238,7 +241,7 @@ takeGE =
238 let
241 let
239 list_length = Prelude.length ls
242 list_length = Prelude.length ls
240 in
243 in
241− case S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
244+ case runIdentity $ S.parseD (P.takeGE n FL.toList) (S.fromList ls) of
242 Right parsed_list ->
245 Right parsed_list ->
243 if (n <= list_length) then
246 if (n <= list_length) then
244 checkListEqual parsed_list ls
247 checkListEqual parsed_list ls
@@ -248,15 +251,15 @@ takeGE =
248
251
249nLessThanEqual0 ::
252nLessThanEqual0 ::
250 ( Int
253 ( Int
251− -> FL.Fold (Either SomeException) Int [Int]
254+ -> FL.Fold Identity Int [Int]
252− -> P.Parser Int (Either SomeException) [Int]
255+ -> P.Parser Int Identity [Int]
253 )
256 )
254 -> (Int -> [Int] -> [Int])
257 -> (Int -> [Int] -> [Int])
255 -> Property
258 -> Property
256nLessThanEqual0 tk ltk =
259nLessThanEqual0 tk ltk =
257 forAll (elements [0, (-1)]) $ \n ->
260 forAll (elements [0, (-1)]) $ \n ->
258 forAll (listOf arbitrary) $ \ls ->
261 forAll (listOf arbitrary) $ \ls ->
259− case S.parseD (tk n FL.toList) (S.fromList ls) of
262+ case runIdentity $ S.parseD (tk n FL.toList) (S.fromList ls) of
260 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
263 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
261 Left _ -> property False
264 Left _ -> property False
262
265
@@ -283,7 +286,7 @@ lookAheadPass =
283 in
286 in
284 forAll (chooseInt (n+1, max_value)) $ \list_length ->
287 forAll (chooseInt (n+1, max_value)) $ \list_length ->
285 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
288 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
286− case S.parseD parseTwice (S.fromList ls) of
289+ case runIdentity $ S.parseD parseTwice (S.fromList ls) of
287 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
290 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
288 Left _ -> property $ False
291 Left _ -> property $ False
289
292
@@ -298,7 +301,7 @@ lookAhead =
298 return (parsed_list_1, parsed_list_2)
301 return (parsed_list_1, parsed_list_2)
299 in
302 in
300 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
303 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
301− case S.parseD parseTwice (S.fromList ls) of
304+ case runIdentity $ S.parseD parseTwice (S.fromList ls) of
302 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
305 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
303 Left _ -> property ((list_length < n) || (list_length == n && n == 0))
306 Left _ -> property ((list_length < n) || (list_length == n && n == 0))
304 where
307 where
@@ -307,7 +310,7 @@ lookAhead =
307takeWhile :: Property
310takeWhile :: Property
308takeWhile =
311takeWhile =
309 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
312 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
310− case S.parseD (P.takeWhile predicate FL.toList) (S.fromList ls) of
313+ case runIdentity $ S.parseD (P.takeWhile predicate FL.toList) (S.fromList ls) of
311 Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
314 Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
312 Left _ -> property False
315 Left _ -> property False
313 where
316 where
@@ -316,7 +319,7 @@ takeWhile =
316takeWhile1 :: Property
319takeWhile1 :: Property
317takeWhile1 =
320takeWhile1 =
318 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
321 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
319− case S.parseD (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
322+ case runIdentity $ S.parseD (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
320 Right parsed_list -> case ls of
323 Right parsed_list -> case ls of
321 [] -> property False
324 [] -> property False
322 (x : _) ->
325 (x : _) ->
@@ -334,7 +337,7 @@ groupBy :: Property
334groupBy =
337groupBy =
335 forAll (listOf (chooseInt (0, 1)))
338 forAll (listOf (chooseInt (0, 1)))
336 $ \ls ->
339 $ \ls ->
337− case S.parseD parser (S.fromList ls) of
340+ case runIdentity $ S.parseD parser (S.fromList ls) of
338 Right parsed -> checkListEqual parsed (groupByLF ls)
341 Right parsed -> checkListEqual parsed (groupByLF ls)
339 Left _ -> property False
342 Left _ -> property False
340
343
@@ -350,7 +353,7 @@ groupByRolling :: Property
350groupByRolling =
353groupByRolling =
351 forAll (listOf (chooseInt (0, 1)))
354 forAll (listOf (chooseInt (0, 1)))
352 $ \ls ->
355 $ \ls ->
353− case S.parseD parser (S.fromList ls) of
356+ case runIdentity $ S.parseD parser (S.fromList ls) of
354 Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
357 Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
355 Left _ -> property False
358 Left _ -> property False
356
359
@@ -369,7 +372,7 @@ takeEndByOrMax :: Property
369takeEndByOrMax =
372takeEndByOrMax =
370 forAll (chooseInt (min_value, max_value)) $ \n ->
373 forAll (chooseInt (min_value, max_value)) $ \n ->
371 forAll (listOf (chooseInt (0, 1))) $ \ls ->
374 forAll (listOf (chooseInt (0, 1))) $ \ls ->
372− case S.parseD (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
375+ case runIdentity $ S.parseD (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
373 Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
376 Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
374 Left _ -> property False
377 Left _ -> property False
375 where
378 where
@@ -379,7 +382,7 @@ wordBy :: Property
379wordBy =
382wordBy =
380 forAll (listOf (elements [' ', 's']))
383 forAll (listOf (elements [' ', 's']))
381 $ \ls ->
384 $ \ls ->
382− case S.parseD parser (S.fromList ls) of
385+ case runIdentity $ S.parseD parser (S.fromList ls) of
383 Right parsed -> checkListEqual parsed (words' ls)
386 Right parsed -> checkListEqual parsed (words' ls)
384 Left _ -> property False
387 Left _ -> property False
385
388
@@ -395,7 +398,7 @@ wordBy =
395splitWith :: Property
398splitWith :: Property
396splitWith =
399splitWith =
397 forAll (listOf (chooseInt (0, 1))) $ \ls ->
400 forAll (listOf (chooseInt (0, 1))) $ \ls ->
398− case S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
401+ case runIdentity $ S.parseD (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
399 Right (result_first, result_second) -> case ls of
402 Right (result_first, result_second) -> case ls of
400 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
403 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
401 _ -> False
404 _ -> False
@@ -405,19 +408,19 @@ splitWith =
405
408
406splitWithFailLeft :: Property
409splitWithFailLeft :: Property
407splitWithFailLeft =
410splitWithFailLeft =
408− property (case S.parseD (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
411+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
409 Right _ -> False
412 Right _ -> False
410 Left _ -> True)
413 Left _ -> True)
411
414
412splitWithFailRight :: Property
415splitWithFailRight :: Property
413splitWithFailRight =
416splitWithFailRight =
414− property (case S.parseD (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
417+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
415 Right _ -> False
418 Right _ -> False
416 Left _ -> True)
419 Left _ -> True)
417
420
418splitWithFailBoth :: Property
421splitWithFailBoth :: Property
419splitWithFailBoth =
422splitWithFailBoth =
420− property (case S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
423+ property (case runIdentity $ S.parseD (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
421 Right _ -> False
424 Right _ -> False
422 Left _ -> True)
425 Left _ -> True)
423
426
@@ -528,14 +531,14 @@ many =
528 prsr =
531 prsr =
529 flip P.many concatFold
532 flip P.many concatFold
530 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
533 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
531− in case S.parseD prsr (S.fromList ls) of
534+ in case runIdentity $ S.parseD prsr (S.fromList ls) of
532 Right res_list ->
535 Right res_list ->
533 checkListEqual res_list (Prelude.filter (== 0) ls)
536 checkListEqual res_list (Prelude.filter (== 0) ls)
534 Left _ -> property False
537 Left _ -> property False
535
538
536many_empty :: Property
539many_empty :: Property
537many_empty =
540many_empty =
538− property (case S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
541+ property (case runIdentity $ S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
539 Right res_list -> checkListEqual res_list ([] :: [Int])
542 Right res_list -> checkListEqual res_list ([] :: [Int])
540 Left _ -> property False)
543 Left _ -> property False)
541
544
@@ -548,13 +551,13 @@ some =
548 prsr =
551 prsr =
549 flip P.some concatFold
552 flip P.some concatFold
550 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
553 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
551− in case S.parseD prsr (S.fromList ls) of
554+ in case runIdentity $ S.parseD prsr (S.fromList ls) of
552 Right res_list -> res_list == Prelude.filter (== 0) ls
555 Right res_list -> res_list == Prelude.filter (== 0) ls
553 Left _ -> False
556 Left _ -> False
554
557
555someFail :: Property
558someFail :: Property
556someFail =
559someFail =
557− property (case S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
560+ property (case runIdentity $ S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
558 Right _ -> False
561 Right _ -> False
559 Left _ -> True)
562 Left _ -> True)
560
563
@@ -566,54 +569,66 @@ applicative :: Property
566applicative =
569applicative =
567 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
570 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
568 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
571 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
572+ monadicIO $ do
569 let parser =
573 let parser =
570 (,)
574 (,)
571 <$> P.fromFold (FL.take (length list1) FL.toList)
575 <$> P.fromFold (FL.take (length list1) FL.toList)
572 <*> P.fromFold (FL.take (length list2) FL.toList)
576 <*> P.fromFold (FL.take (length list2) FL.toList)
573− in monadicIO $ do
577+
574− (olist1, olist2) <-
578+ return $
575− run $ S.parseD parser (S.fromList $ list1 ++ list2)
579+ case runIdentity $ S.parseD parser (S.fromList $ list1 ++ list2) of
576− listEquals (==) olist1 list1
580+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
577− listEquals (==) olist2 list2
581+ Left _ -> False
578
582
579sequence :: Property
583sequence :: Property
580sequence =
584sequence =
581 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
585 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
586+ monadicIO $ do
582 let parsers = fmap (\xs -> P.fromFold $ FL.take (length xs) FL.toList) ins
587 let parsers = fmap (\xs -> P.fromFold $ FL.take (length xs) FL.toList) ins
583− in monadicIO $ do
584− outs <- run $
585− S.parseD
588+ outs <- S.parseD
586− (Prelude.sequence parsers)
589+ (Prelude.sequence parsers)
587− (S.fromList $ concat ins)
590+ (S.fromList $ concat ins)
588− listEquals (==) outs ins
591+ return $
589−
592+ case outs of
593+ Right x -> x == ins
594+ Left _ -> False
595+
590−altEOF1 :: Expectation
596+altEOF1 :: Property
591altEOF1 =
597altEOF1 =
598+ monadicIO $ do
592− S.parseD
599+ s1 <- S.parseD
593 (P.satisfy (> 0) <|> return 66)
600 (P.satisfy (> 0) <|> return 66)
594 (S.fromList ([]::[Int]))
601 (S.fromList ([]::[Int]))
595− `shouldReturn` 66
602+ return $
603+ case s1 of
604+ Right x -> x == 66
605+ Left _ -> False
596
606
597−altEOF2 :: Expectation
607+altEOF2 :: Property
598altEOF2 =
608altEOF2 =
609+ monadicIO $ do
599− S.parseD
610+ s1 <- S.parseD
600 ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
611 ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
601 (S.fromList ([51]::[Int]))
612 (S.fromList ([51]::[Int]))
602− `shouldReturn` [51]
613+ return $
614+ case s1 of
615+ Right x -> x == [51]
616+ Left _ -> False
603
617
604monad :: Property
618monad :: Property
605monad =
619monad =
606 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
620 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
607 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
621 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
622+ monadicIO $ do
608 let parser = do
623 let parser = do
609 olist1 <- P.fromFold (FL.take (length list1) FL.toList)
624 olist1 <- P.fromFold (FL.take (length list1) FL.toList)
610 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
625 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
611 return (olist1, olist2)
626 return (olist1, olist2)
612− in monadicIO $ do
613− (olist1, olist2) <-
614− run $ S.parseD parser (S.fromList $ list1 ++ list2)
627+ s1 <- S.parseD parser (S.fromList $ list1 ++ list2)
615− listEquals (==) olist1 list1
628+ return $
616− listEquals (==) olist2 list2
629+ case s1 of
630+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
631+ Left _ -> False
617
632
618-------------------------------------------------------------------------------
633-------------------------------------------------------------------------------
619-- Stream parsing
634-- Stream parsing
@@ -624,13 +639,11 @@ parseMany =
624 forAll (chooseInt (1,100)) $ \len ->
639 forAll (chooseInt (1,100)) $ \len ->
625 forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
640 forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
626 monadicIO $ do
641 monadicIO $ do
627− outs <-
642+ outs <-
628− ( run
629− $ toList
630− $ S.parseManyD
643+ (toList $ S.rights $ S.parseManyD
631− (P.fromFold $ FL.take len FL.toList) (S.fromList $ concat ins)
644+ (P.fromFold $ FL.take len FL.toList) (S.fromList $ concat ins)
632− )
645+ )
633− listEquals (==) outs ins
646+ return $ outs == ins
634
647
635-- basic sanity test for parsing from arrays
648-- basic sanity test for parsing from arrays
636parseUnfold :: Property
649parseUnfold :: Property
@@ -664,13 +677,15 @@ parserSequence :: Property
664parserSequence =
677parserSequence =
665 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
678 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
666 monadicIO $ do
679 monadicIO $ do
667− let parsers = D.fromList
680+ let parsers = D.fromList
668− $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
681+ $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
669− let sequencedParser = P.sequence parsers FL.sum
682+ let sequencedParser = P.sequence parsers FL.sum
670− outs <-
683+ outs <-
671− run $
672− S.parseD sequencedParser $ S.concatMap S.fromList (S.fromList ins)
684+ S.parseD sequencedParser $ S.concatMap S.fromList (S.fromList ins)
685+ return $
686+ case outs of
673− equals (==) outs (sum $ map sum ins)
687+ Right x -> x == sum (map sum ins)
688+ Left _ -> False
674
689
675-------------------------------------------------------------------------------
690-------------------------------------------------------------------------------
676-- Test for a particular case hit during fs events testing
691-- Test for a particular case hit during fs events testing
@@ -722,6 +737,7 @@ parseMany2Events =
722 xs <-
737 xs <-
723 ( run
738 ( run
724 $ toList
739 $ toList
740+ $ S.rights
725 $ S.parseManyD readOneEvent
741 $ S.parseManyD readOneEvent
726 $ S.fromList (concat (replicate 2 event))
742 $ S.fromList (concat (replicate 2 event))
727 )
743 )
@@ -750,8 +766,8 @@ main =
750
766
751 describe "Instances" $ do
767 describe "Instances" $ do
752 prop "applicative" applicative
768 prop "applicative" applicative
753− it "Alternative: end of input 1" altEOF1
769+ prop "Alternative: end of input 1" altEOF1
754− it "Alternative: end of input 2" altEOF2
770+ prop "Alternative: end of input 2" altEOF2
755 prop "monad" monad
771 prop "monad" monad
756 prop "sequence" sequence
772 prop "sequence" sequence
757
773