on master help sign in
Fix formatting for parsers
commit05613cd6cfd35f442c62116a435bd80fa51ae69c authorAnurag Hooda <anuraghooda@composewell.com> dateThu 31 Dec 2020, 00:06 (6 years ago) parent656bfc6
2 files changed+75−72
src/Streamly/Internal/Data/Parser/ParserD.hs+11−10 test/Streamly/Test/Data/Parser.hs+64−62
src/Streamly/Internal/Data/Parser/ParserD.hs+11−10
@@ -296,31 +296,34 @@ takeBetween low high (Fold fstep finitial fextract) =
296296 initial = Tuple' 0 <$> finitial
297297
298298 step (Tuple' i s) a
299− | low > high = throwM $ ParseError
300− $ "takeBetween: lower bound - " ++ show low
301− ++ " is greater than higher bound - " ++ show high
299+ | low > high =
300+ throwM
301+ $ ParseError
302+ $ "takeBetween: lower bound - " ++ show low
303+ ++ " is greater than higher bound - " ++ show high
302304 | high <= 0 = Done 1 <$> fextract s
303305 | i1 < low = do
304306 res <- fstep s a
305307 return
306308 $ case res of
307− FL.Partial s' -> Continue 0 $ Tuple' i1 s'
309+ FL.Partial s1 -> Continue 0 $ Tuple' i1 s1
308310 FL.Done _ ->
309311 Error
310312 $ "takeBetween: the collecting fold terminated after"
311313 ++ " consuming" ++ show i1 ++ " elements"
314+ ++ " minimum" ++ show low ++ " elements needed"
312315 | otherwise = do
313316 res <- fstep s a
314317 case res of
315− FL.Partial s' ->
318+ FL.Partial s1 ->
316319 if i1 >= high
317− then Done 0 <$> fextract s'
318− else return $ Partial 0 $ Tuple' i1 s'
320+ then Done 0 <$> fextract s1
321+ else return $ Partial 0 $ Tuple' i1 s1
319322 FL.Done b -> return $ Done 0 b
320323
321324 where
322325
323− i1 = i +1
326+ i1 = i + 1
324327
325328 extract (Tuple' i s)
326329 | i >= low && i <= high = fextract s
@@ -332,8 +335,6 @@ takeBetween low high (Fold fstep finitial fextract) =
332335 "takeBetween: Expecting alteast " ++ show low
333336 ++ " elements, got " ++ show i
334337
335−
336−
337338-- | See 'Streamly.Internal.Data.Parser.takeEQ'.
338339--
339340-- /Internal/
test/Streamly/Test/Data/Parser.hs+64−62
@@ -56,7 +56,8 @@ max_length = 1000
5656fromFold :: Property
5757fromFold =
5858 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
6061 Right is_equal -> is_equal
6162 Left _ -> False
6263
@@ -160,57 +161,45 @@ takeBetweenPass =
160161 forAll (chooseInt (min_value, max_value)) $ \m ->
161162 forAll (chooseInt (m, max_value)) $ \n ->
162163 forAll (chooseInt (m, max_value)) $ \list_length ->
163− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
164− case S.parse (P.takeBetween m n FL.toList) (S.fromList ls) of
165− Right parsed_list ->
166− let lpl = Prelude.length parsed_list
167− in checkListEqual parsed_list (Prelude.take lpl ls)
168− Left _ -> property False
164+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
165+ $ \ls ->
166+ case S.parse (P.takeBetween m n FL.toList)
167+ (S.fromList ls) of
168+ Right parsed_list ->
169+ let lpl = Prelude.length parsed_list
170+ in checkListEqual parsed_list
171+ $ Prelude.take lpl ls
172+ Left _ -> property False
169173
170174takeBetween :: Property
171175takeBetween =
172176 forAll (chooseInt (min_value, max_value)) $ \m ->
173177 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 ->
175179 let
176180 list_length = Prelude.length ls
177181 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
179184 Right parsed_list ->
180185 if m <= list_length && n >= list_length
181186 then
182187 let lpl = Prelude.length parsed_list
183− in checkListEqual parsed_list (Prelude.take
184− lpl ls)
188+ in checkListEqual parsed_list
189+ $ Prelude.take lpl ls
185190 else property False
186191 Left _ -> property (m > n || list_length < m)
187192
188193
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−
207194takeEQPass :: Property
208195takeEQPass =
209196 forAll (chooseInt (min_value, max_value)) $ \n ->
210197 forAll (chooseInt (n, max_value)) $ \list_length ->
211− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
198+ forAll (vectorOf list_length
199+ (chooseInt (min_value, max_value))) $ \ls ->
212200 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
213− Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
201+ Right parsed_list ->
202+ checkListEqual parsed_list (Prelude.take n ls)
214203 Left _ -> property False
215204
216205takeEQ :: Property
@@ -222,20 +211,20 @@ takeEQ =
222211 in
223212 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
224213 Right parsed_list ->
225− if (n <= list_length) then
226− checkListEqual parsed_list (Prelude.take n ls)
227− else
228− property False
214+ if n <= list_length
215+ then checkListEqual parsed_list (Prelude.take n ls)
216+ else property False
229217 Left _ -> property (n > list_length)
230218
231219takeGEPass :: Property
232220takeGEPass =
233221 forAll (chooseInt (min_value, max_value)) $ \n ->
234222 forAll (chooseInt (n, max_value)) $ \list_length ->
235− forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
236− case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
237− Right parsed_list -> checkListEqual parsed_list ls
238− Left _ -> property False
223+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
224+ $ \ls ->
225+ case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
226+ Right parsed_list -> checkListEqual parsed_list ls
227+ Left _ -> property False
239228
240229takeGE :: Property
241230takeGE =
@@ -246,10 +235,9 @@ takeGE =
246235 in
247236 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
248237 Right parsed_list ->
249− if (n <= list_length) then
250− checkListEqual parsed_list ls
251− else
252− property False
238+ if n <= list_length
239+ then checkListEqual parsed_list ls
240+ else property False
253241 Left _ -> property (n > list_length)
254242
255243nLessThanEqual0 ::
@@ -327,7 +315,8 @@ takeWhile :: Property
327315takeWhile =
328316 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
329317 case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
330− Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
318+ Right parsed_list ->
319+ checkListEqual parsed_list (Prelude.takeWhile predicate ls)
331320 Left _ -> property False
332321 where
333322 predicate = (== 0)
@@ -339,8 +328,10 @@ takeWhile1 =
339328 Right parsed_list -> case ls of
340329 [] -> property False
341330 (x : _) ->
342− if predicate x then
343− checkListEqual parsed_list (Prelude.takeWhile predicate ls)
331+ if predicate x
332+ then
333+ checkListEqual parsed_list
334+ $ Prelude.takeWhile predicate ls
344335 else
345336 property False
346337 Left _ -> case ls of
@@ -491,7 +482,8 @@ many =
491482 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
492483 in
493484 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
495487 Left _ -> property False
496488
497489-- many_empty :: Property
@@ -505,7 +497,11 @@ some =
505497 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
506498 let
507499 ls = 0 : genLs
508− concatFold = FL.Fold (\concatList curr_list -> return $ FL.Partial $ concatList ++ curr_list) (return []) return
500+ concatFold = FL.Fold
501+ (\concatList curr_list ->
502+ return $ FL.Partial $ concatList ++ curr_list)
503+ (return [])
504+ return
509505 prsr = P.some concatFold
510506 $ P.fromFold $ FL.sliceSepBy (== 1) FL.toList
511507 in
@@ -543,9 +539,10 @@ applicative =
543539-- fixed.
544540sequence :: Property
545541sequence =
546− forAll (vectorOf 11 (listOf (chooseAny :: Gen Int) `suchThat` (\x -> length x > 0))) $ \ ins ->
547− let p xs = P.fromFold (FL.ltake (length xs) FL.toList)
548− in monadicIO $ do
542+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int) `suchThat`
543+ (\x -> length x > 0))) $ \ ins ->
544+ let p xs = P.fromFold (FL.ltake (length xs) FL.toList)
545+ in monadicIO $ do
549546 outs <- run $
550547 S.parse
551548 (Prelude.sequence $ fmap p ins)
@@ -556,13 +553,14 @@ sequence =
556553-- fixed.
557554monad :: Property
558555monad =
559− forAll (listOf (chooseAny :: Gen Int) `suchThat` (\x -> length x > 0)) $ \ list1 ->
560− forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
561− let parser = do
562− olist1 <- P.fromFold (FL.ltake (length list1) FL.toList)
563− olist2 <- P.fromFold (FL.ltake (length list2) FL.toList)
564− return (olist1, olist2)
565− in monadicIO $ do
556+ forAll (listOf (chooseAny :: Gen Int) `suchThat`
557+ (\x -> length x > 0)) $ \ list1 ->
558+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
559+ let parser = do
560+ olist1 <- P.fromFold (FL.ltake (length list1) FL.toList)
561+ olist2 <- P.fromFold (FL.ltake (length list2) FL.toList)
562+ return (olist1, olist2)
563+ in monadicIO $ do
566564 (olist1, olist2) <-
567565 run $ S.parse parser (S.fromList $ list1 ++ list2)
568566 listEquals (==) olist1 list1
@@ -688,14 +686,16 @@ main =
688686 ++ "otherwise fail") Main.takeBetween
689687
690688 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
692691 prop "P.takeGE n ls = ls when len >= n" takeGEPass
693692 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
694693 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
695694 -- prop "Fail when stream length exceeded" lookAheadFail
696695 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
697696 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
699699 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
700700 prop "many (P.wordBy ' ') = words'" wordBy
701701 -- prop "" splitWithPass
@@ -711,8 +711,10 @@ main =
711711 -- prop "" shortestFailLeft
712712 -- prop "" shortestFailRight
713713 -- 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
715716 -- 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
717719 -- prop "fail due to parser being die" someFail
718720 takeProperties