on master help sign in
Fix formatting for parsers
05613cdAnurag Hooda
src/Streamly/Internal/Data/Parser/ParserD.hs+11−10
@@ -296,31 +296,34 @@ takeBetween low high (Fold fstep finitial fextract) =
296 initial = Tuple' 0 <$> finitial
296 initial = Tuple' 0 <$> finitial
297
297
298 step (Tuple' i s) a
298 step (Tuple' i s) a
299+ | low > high =
300+ throwM
299− | low > high = throwM $ ParseError
301+ $ ParseError
300− $ "takeBetween: lower bound - " ++ show low
302+ $ "takeBetween: lower bound - " ++ show low
301− ++ " is greater than higher bound - " ++ show high
303+ ++ " is greater than higher bound - " ++ show high
302 | high <= 0 = Done 1 <$> fextract s
304 | high <= 0 = Done 1 <$> fextract s
303 | i1 < low = do
305 | i1 < low = do
304 res <- fstep s a
306 res <- fstep s a
305 return
307 return
306 $ case res of
308 $ case res of
307− FL.Partial s' -> Continue 0 $ Tuple' i1 s'
309+ FL.Partial s1 -> Continue 0 $ Tuple' i1 s1
308 FL.Done _ ->
310 FL.Done _ ->
309 Error
311 Error
310 $ "takeBetween: the collecting fold terminated after"
312 $ "takeBetween: the collecting fold terminated after"
311 ++ " consuming" ++ show i1 ++ " elements"
313 ++ " consuming" ++ show i1 ++ " elements"
314+ ++ " minimum" ++ show low ++ " elements needed"
312 | otherwise = do
315 | otherwise = do
313 res <- fstep s a
316 res <- fstep s a
314 case res of
317 case res of
315− FL.Partial s' ->
318+ FL.Partial s1 ->
316 if i1 >= high
319 if i1 >= high
317− then Done 0 <$> fextract s'
320+ then Done 0 <$> fextract s1
318− else return $ Partial 0 $ Tuple' i1 s'
321+ else return $ Partial 0 $ Tuple' i1 s1
319 FL.Done b -> return $ Done 0 b
322 FL.Done b -> return $ Done 0 b
320
323
321 where
324 where
322
325
323− i1 = i +1
326+ i1 = i + 1
324
327
325 extract (Tuple' i s)
328 extract (Tuple' i s)
326 | i >= low && i <= high = fextract s
329 | i >= low && i <= high = fextract s
@@ -332,8 +335,6 @@ takeBetween low high (Fold fstep finitial fextract) =
332 "takeBetween: Expecting alteast " ++ show low
335 "takeBetween: Expecting alteast " ++ show low
333 ++ " elements, got " ++ show i
336 ++ " elements, got " ++ show i
334
337
335−
336−
337-- | See 'Streamly.Internal.Data.Parser.takeEQ'.
338-- | See 'Streamly.Internal.Data.Parser.takeEQ'.
338--
339--
339-- /Internal/
340-- /Internal/
test/Streamly/Test/Data/Parser.hs+64−62
@@ -56,7 +56,8 @@ max_length = 1000
56fromFold :: Property
56fromFold :: Property
57fromFold =
57fromFold =
58 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
58 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
59− case (==) <$> (S.parse (P.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
59+ case (==) <$> S.parse (P.fromFold FL.sum) (S.fromList ls)
60+ <*> S.fold FL.sum (S.fromList ls) of
60 Right is_equal -> is_equal
61 Right is_equal -> is_equal
61 Left _ -> False
62 Left _ -> False
62
63
@@ -160,57 +161,45 @@ takeBetweenPass =
160 forAll (chooseInt (min_value, max_value)) $ \m ->
161 forAll (chooseInt (min_value, max_value)) $ \m ->
161 forAll (chooseInt (m, max_value)) $ \n ->
162 forAll (chooseInt (m, max_value)) $ \n ->
162 forAll (chooseInt (m, max_value)) $ \list_length ->
163 forAll (chooseInt (m, max_value)) $ \list_length ->
163− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
164+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
165+ $ \ls ->
164− case S.parse (P.takeBetween m n FL.toList) (S.fromList ls) of
166+ case S.parse (P.takeBetween m n FL.toList)
167+ (S.fromList ls) of
165− Right parsed_list ->
168+ Right parsed_list ->
166− let lpl = Prelude.length parsed_list
169+ let lpl = Prelude.length parsed_list
167− in checkListEqual parsed_list (Prelude.take lpl ls)
170+ in checkListEqual parsed_list
171+ $ Prelude.take lpl ls
168− Left _ -> property False
172+ Left _ -> property False
169
173
170takeBetween :: Property
174takeBetween :: Property
171takeBetween =
175takeBetween =
172 forAll (chooseInt (min_value, max_value)) $ \m ->
176 forAll (chooseInt (min_value, max_value)) $ \m ->
173 forAll (chooseInt (min_value, max_value)) $ \n ->
177 forAll (chooseInt (min_value, max_value)) $ \n ->
174− forAll (listOf(chooseInt (min_value, max_value))) $ \ls ->
178+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
175 let
179 let
176 list_length = Prelude.length ls
180 list_length = Prelude.length ls
177 in
181 in
178− case S.parse (P.takeBetween m n FL.toList) (S.fromList ls) of
182+ case S.parse (P.takeBetween m n FL.toList)
183+ (S.fromList ls) of
179 Right parsed_list ->
184 Right parsed_list ->
180 if m <= list_length && n >= list_length
185 if m <= list_length && n >= list_length
181 then
186 then
182 let lpl = Prelude.length parsed_list
187 let lpl = Prelude.length parsed_list
183− in checkListEqual parsed_list (Prelude.take
188+ in checkListEqual parsed_list
184− lpl ls)
189+ $ Prelude.take lpl ls
185 else property False
190 else property False
186 Left _ -> property (m > n || list_length < m)
191 Left _ -> property (m > n || list_length < m)
187
192
188
193
189−-- takeBetween :: Property
190−-- takeBetween =
191−-- forAll (chooseInt (min_value, max_value)) $ \m ->
192−-- forAll (chooseInt (min_value, max_value)) $ \n ->
193−-- forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
194−-- let
195−-- list_length = Prelude.length ls
196−-- in
197−-- case S.parseD (P.takeBetween m n FL.toList) (S.fromList ls) of
198−-- Right parsed_list ->
199−-- if m <= list_length && n >= list_length
200−-- then
201−-- let lpl = Prelude.length parsed_list
202−-- in checkListEqual parsed_list (Prelude.take
203−-- lpl ls)
204−-- else property False
205−-- Left _ -> property (m > n || list_length < m)
206−
207takeEQPass :: Property
194takeEQPass :: Property
208takeEQPass =
195takeEQPass =
209 forAll (chooseInt (min_value, max_value)) $ \n ->
196 forAll (chooseInt (min_value, max_value)) $ \n ->
210 forAll (chooseInt (n, max_value)) $ \list_length ->
197 forAll (chooseInt (n, max_value)) $ \list_length ->
198+ forAll (vectorOf list_length
211− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
199+ (chooseInt (min_value, max_value))) $ \ls ->
212 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
200 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
201+ Right parsed_list ->
213− Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
202+ checkListEqual parsed_list (Prelude.take n ls)
214 Left _ -> property False
203 Left _ -> property False
215
204
216takeEQ :: Property
205takeEQ :: Property
@@ -222,20 +211,20 @@ takeEQ =
222 in
211 in
223 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
212 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
224 Right parsed_list ->
213 Right parsed_list ->
225− if (n <= list_length) then
214+ if n <= list_length
226− checkListEqual parsed_list (Prelude.take n ls)
215+ then checkListEqual parsed_list (Prelude.take n ls)
227− else
228− property False
216+ else property False
229 Left _ -> property (n > list_length)
217 Left _ -> property (n > list_length)
230
218
231takeGEPass :: Property
219takeGEPass :: Property
232takeGEPass =
220takeGEPass =
233 forAll (chooseInt (min_value, max_value)) $ \n ->
221 forAll (chooseInt (min_value, max_value)) $ \n ->
234 forAll (chooseInt (n, max_value)) $ \list_length ->
222 forAll (chooseInt (n, max_value)) $ \list_length ->
235− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
223+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
224+ $ \ls ->
236− case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
225+ case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
237− Right parsed_list -> checkListEqual parsed_list ls
226+ Right parsed_list -> checkListEqual parsed_list ls
238− Left _ -> property False
227+ Left _ -> property False
239
228
240takeGE :: Property
229takeGE :: Property
241takeGE =
230takeGE =
@@ -246,10 +235,9 @@ takeGE =
246 in
235 in
247 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
236 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
248 Right parsed_list ->
237 Right parsed_list ->
249− if (n <= list_length) then
238+ if n <= list_length
250− checkListEqual parsed_list ls
239+ then checkListEqual parsed_list ls
251− else
252− property False
240+ else property False
253 Left _ -> property (n > list_length)
241 Left _ -> property (n > list_length)
254
242
255nLessThanEqual0 ::
243nLessThanEqual0 ::
@@ -327,7 +315,8 @@ takeWhile :: Property
327takeWhile =
315takeWhile =
328 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
316 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
329 case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
317 case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
318+ Right parsed_list ->
330− Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
319+ checkListEqual parsed_list (Prelude.takeWhile predicate ls)
331 Left _ -> property False
320 Left _ -> property False
332 where
321 where
333 predicate = (== 0)
322 predicate = (== 0)
@@ -339,8 +328,10 @@ takeWhile1 =
339 Right parsed_list -> case ls of
328 Right parsed_list -> case ls of
340 [] -> property False
329 [] -> property False
341 (x : _) ->
330 (x : _) ->
342− if predicate x then
331+ if predicate x
332+ then
333+ checkListEqual parsed_list
343− checkListEqual parsed_list (Prelude.takeWhile predicate ls)
334+ $ Prelude.takeWhile predicate ls
344 else
335 else
345 property False
336 property False
346 Left _ -> case ls of
337 Left _ -> case ls of
@@ -491,7 +482,8 @@ many =
491 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
482 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
492 in
483 in
493 case S.parse prsr (S.fromList ls) of
484 case S.parse prsr (S.fromList ls) of
494− Right res_list -> checkListEqual res_list (Prelude.filter (== 0) ls)
485+ Right res_list -> checkListEqual res_list
486+ $ Prelude.filter (== 0) ls
495 Left _ -> property False
487 Left _ -> property False
496
488
497-- many_empty :: Property
489-- many_empty :: Property
@@ -505,7 +497,11 @@ some =
505 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
497 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
506 let
498 let
507 ls = 0 : genLs
499 ls = 0 : genLs
500+ concatFold = FL.Fold
501+ (\concatList curr_list ->
508− concatFold = FL.Fold (\concatList curr_list -> return $ FL.Partial $ concatList ++ curr_list) (return []) return
502+ return $ FL.Partial $ concatList ++ curr_list)
503+ (return [])
504+ return
509 prsr = P.some concatFold
505 prsr = P.some concatFold
510 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
506 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
511 in
507 in
@@ -543,9 +539,10 @@ applicative =
543-- fixed.
539-- fixed.
544sequence :: Property
540sequence :: Property
545sequence =
541sequence =
546− forAll (vectorOf 11 (listOf (chooseAny :: Gen Int) `suchThat` (\x -> length x > 0))) $ \ ins ->
542+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int) `suchThat`
543+ (\x -> length x > 0))) $ \ ins ->
547− let p xs = P.fromFold (FL.ltake (length xs) FL.toList)
544+ let p xs = P.fromFold (FL.ltake (length xs) FL.toList)
548− in monadicIO $ do
545+ in monadicIO $ do
549 outs <- run $
546 outs <- run $
550 S.parse
547 S.parse
551 (Prelude.sequence $ fmap p ins)
548 (Prelude.sequence $ fmap p ins)
@@ -556,13 +553,14 @@ sequence =
556-- fixed.
553-- fixed.
557monad :: Property
554monad :: Property
558monad =
555monad =
559− forAll (listOf (chooseAny :: Gen Int) `suchThat` (\x -> length x > 0)) $ \ list1 ->
556+ forAll (listOf (chooseAny :: Gen Int) `suchThat`
560− forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
557+ (\x -> length x > 0)) $ \ list1 ->
558+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
561− let parser = do
559+ let parser = do
562− olist1 <- P.fromFold (FL.ltake (length list1) FL.toList)
560+ olist1 <- P.fromFold (FL.ltake (length list1) FL.toList)
563− olist2 <- P.fromFold (FL.ltake (length list2) FL.toList)
561+ olist2 <- P.fromFold (FL.ltake (length list2) FL.toList)
564− return (olist1, olist2)
562+ return (olist1, olist2)
565− in monadicIO $ do
563+ in monadicIO $ do
566 (olist1, olist2) <-
564 (olist1, olist2) <-
567 run $ S.parse parser (S.fromList $ list1 ++ list2)
565 run $ S.parse parser (S.fromList $ list1 ++ list2)
568 listEquals (==) olist1 list1
566 listEquals (==) olist1 list1
@@ -688,14 +686,16 @@ main =
688 ++ "otherwise fail") Main.takeBetween
686 ++ "otherwise fail") Main.takeBetween
689
687
690 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
688 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
691− prop "P.takeEQ = Prelude.take when len >= n and fail otherwise" Main.takeEQ
689+ prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
690+ Main.takeEQ
692 prop "P.takeGE n ls = ls when len >= n" takeGEPass
691 prop "P.takeGE n ls = ls when len >= n" takeGEPass
693 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
692 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
694 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
693 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
695 -- prop "Fail when stream length exceeded" lookAheadFail
694 -- prop "Fail when stream length exceeded" lookAheadFail
696 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
695 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
697 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
696 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
698− prop "P.takeWhile1 = Prelude.takeWhile if taken something, else check why failed" takeWhile1
697+ prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
698+ ++ " else check why failed") takeWhile1
699 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
699 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
700 prop "many (P.wordBy ' ') = words'" wordBy
700 prop "many (P.wordBy ' ') = words'" wordBy
701 -- prop "" splitWithPass
701 -- prop "" splitWithPass
@@ -711,8 +711,10 @@ main =
711 -- prop "" shortestFailLeft
711 -- prop "" shortestFailLeft
712 -- prop "" shortestFailRight
712 -- prop "" shortestFailRight
713 -- prop "" shortestFailBoth
713 -- prop "" shortestFailBoth
714− prop "P.many concatFold $ P.sliceSepBy (== 1) FL.toList = Prelude.filter (== 0)" many
714+ prop ("P.many concatFold $ P.sliceSepBy (== 1) FL.toList ="
715+ ++ "Prelude.filter (== 0)") many
715 -- prop "[] due to parser being die" many_empty
716 -- prop "[] due to parser being die" many_empty
716− prop "P.some concatFold $ P.sliceSepBy (== 1) FL.toList = Prelude.filter (== 0)" some
717+ prop ("P.some concatFold $ P.sliceSepBy (== 1) FL.toList ="
718+ ++ "Prelude.filter (== 0)") some
717 -- prop "fail due to parser being die" someFail
719 -- prop "fail due to parser being die" someFail
718 takeProperties
720 takeProperties