on master help sign in
Change argument order in parser combinators #969

Keep the collecting fold as the last argument for ease of composition. Data flows from one fold to the next, so the next fold in the chain should be the last argument.

commit4da398aa00984b03b99ef1badbd94aba28cc12bc authorRanjeet Kumar Ranjan <ranjeetr@composewell.com> dateMon 8 Mar 2021, 17:42 (6 years ago) parentbfebe94
7 files changed+53−53
benchmark/Streamly/Benchmark/Data/Parser.hs+4−4 benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+3−3 src/Streamly/Internal/Data/Parser.hs+25−25 src/Streamly/Internal/Data/Parser/ParserD.hs+6−6 src/Streamly/Internal/Data/Parser/ParserD/Types.hs+6−6 test/Streamly/Test/Data/Parser.hs+4−4 test/Streamly/Test/Data/Parser/ParserD.hs+5−5
benchmark/Streamly/Benchmark/Data/Parser.hs+4−4
@@ -111,11 +111,11 @@ wordBy value = IP.parse (PR.wordBy (>= value) FL.drain)
111111
112112{-# INLINE manyWordByEven #-}
113113manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
114−manyWordByEven = IP.parse (PR.many FL.drain (PR.wordBy (Prelude.even) FL.drain))
114+manyWordByEven = IP.parse (PR.many (PR.wordBy (Prelude.even) FL.drain) FL.drain)
115115
116116{-# INLINE many #-}
117117many :: MonadCatch m => SerialT m Int -> m Int
118−many = IP.parse (PR.many FL.length (PR.satisfy (> 0)))
118+many = IP.parse (PR.many (PR.satisfy (> 0)) FL.length)
119119
120120{-# INLINE manyAlt #-}
121121manyAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -125,7 +125,7 @@ manyAlt xs = do
125125
126126{-# INLINE some #-}
127127some :: MonadCatch m => SerialT m Int -> m Int
128−some = IP.parse (PR.some FL.length (PR.satisfy (> 0)))
128+some = IP.parse (PR.some (PR.satisfy (> 0)) FL.length)
129129
130130{-# INLINE someAlt #-}
131131someAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -136,7 +136,7 @@ someAlt xs = do
136136{-# INLINE manyTill #-}
137137manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
138138manyTill value =
139− IP.parse (PR.manyTill FL.length (PR.satisfy (> 0)) (PR.satisfy (== value)))
139+ IP.parse (PR.manyTill (PR.satisfy (> 0)) (PR.satisfy (== value)) FL.length)
140140
141141{-# INLINE splitAp #-}
142142splitAp :: MonadCatch m
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+3−3
@@ -94,11 +94,11 @@ wordBy value = IP.parseD (PR.wordBy (>= value) FL.drain)
9494{-# INLINE manyWordByEven #-}
9595manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
9696manyWordByEven =
97− IP.parseD (PR.many FL.drain (PR.wordBy (Prelude.even) FL.drain))
97+ IP.parseD (PR.many (PR.wordBy (Prelude.even) FL.drain) FL.drain)
9898
9999{-# INLINE many #-}
100100many :: MonadCatch m => SerialT m Int -> m Int
101−many = IP.parseD (PR.many FL.length (PR.satisfy (> 0)))
101+many = IP.parseD (PR.many (PR.satisfy (> 0)) FL.length)
102102
103103{-# INLINE manyAlt #-}
104104manyAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -108,7 +108,7 @@ manyAlt xs = do
108108
109109{-# INLINE some #-}
110110some :: MonadCatch m => SerialT m Int -> m Int
111−some = IP.parseD (PR.some FL.length (PR.satisfy (> 0)))
111+some = IP.parseD (PR.some (PR.satisfy (> 0)) FL.length)
112112
113113{-# INLINE someAlt #-}
114114someAlt :: MonadCatch m => SerialT m Int -> m Int
src/Streamly/Internal/Data/Parser.hs+25−25
@@ -953,8 +953,8 @@ choice _ps = undefined
953953--
954954{-# INLINE manyP #-}
955955manyP :: -- MonadCatch m =>
956− Parser m b c -> Parser m a b -> Parser m a c
957−manyP _f _p = undefined -- K.toParserK $ D.manyP f (K.fromParserK p)
956+ Parser m a b -> Parser m b c -> Parser m a c
957+manyP _p _f = undefined -- K.toParserK $ D.manyP (K.fromParserK p) f
958958
959959-- | Collect zero or more parses. Apply the supplied parser repeatedly on the
960960-- input stream and push the parse results to a downstream fold.
@@ -967,8 +967,8 @@ manyP _f _p = undefined -- K.toParserK $ D.manyP f (K.fromParserK p)
967967-- /Pre-release/
968968--
969969{-# INLINE many #-}
970−many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
971−many f p = K.toParserK $ D.many f (K.fromParserK p)
970+many :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
971+many p f = K.toParserK $ D.many (K.fromParserK p) f
972972-- many = countBetween 0 maxBound
973973
974974-- Note: many1 would perhaps be a better name for this and consistent with
@@ -988,9 +988,9 @@ many f p = K.toParserK $ D.many f (K.fromParserK p)
988988-- /Pre-release/
989989--
990990{-# INLINE some #-}
991−some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
992−some f p = K.toParserK $ D.some f (K.fromParserK p)
993−-- some f p = manyP (takeGE 1 f) p
991+some :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
992+some p f = K.toParserK $ D.some (K.fromParserK p) f
993+-- some p f = manyP p (takeGE 1 f)
994994-- many = countBetween 1 maxBound
995995
996996-- | @countBetween m n f p@ collects between @m@ and @n@ sequential parses of
@@ -1002,9 +1002,9 @@ some f p = K.toParserK $ D.some f (K.fromParserK p)
10021002{-# INLINE countBetween #-}
10031003countBetween ::
10041004 -- MonadCatch m =>
1005− Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
1006−countBetween _m _n _f = undefined
1007−-- countBetween m n f p = manyP (takeBetween m n f) p
1005+ Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
1006+countBetween _m _n _p = undefined
1007+-- countBetween m n p f = manyP (takeBetween m n f) p
10081008
10091009-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
10101010-- the fold @f@. Fails if the input ends or the parser fails before @n@
@@ -1015,9 +1015,9 @@ countBetween _m _n _f = undefined
10151015{-# INLINE count #-}
10161016count ::
10171017 -- MonadCatch m =>
1018− Int -> Fold m b c -> Parser m a b -> Parser m a c
1018+ Int -> Parser m a b -> Fold m b c -> Parser m a c
10191019count n = countBetween n n
1020−-- count n f p = manyP (takeEQ n f) p
1020+-- count n p f = manyP (takeEQ n f) p
10211021
10221022-- | Like 'manyTill' but uses a 'Parser' to collect the results instead of a
10231023-- 'Fold'. Parsing stops or fails if the collecting parser stops or fails.
@@ -1032,9 +1032,9 @@ count n = countBetween n n
10321032--
10331033{-# INLINE manyTillP #-}
10341034manyTillP :: -- MonadCatch m =>
1035− Parser m b c -> Parser m a b -> Parser m a x -> Parser m a c
1036−manyTillP _f _p1 _p2 = undefined
1037− -- K.toParserK $ D.manyTillP f (K.fromParserK p1) (K.fromParserK p2)
1035+ Parser m a b -> Parser m a x -> Parser m b c -> Parser m a c
1036+manyTillP _p1 _p2 _f = undefined
1037+ -- K.toParserK $ D.manyTillP (K.fromParserK p1) (K.fromParserK p2) f
10381038
10391039-- | @manyTill f collect test@ tries the parser @test@ on the input, if @test@
10401040-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
@@ -1048,8 +1048,8 @@ manyTillP _f _p1 _p2 = undefined
10481048--
10491049{-# INLINE manyTill #-}
10501050manyTill :: MonadCatch m
1051− => Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1052−manyTill f p1 p2 =
1051+ => Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1052+manyTill p1 p2 f =
10531053 K.toParserK $ D.manyTill f (K.fromParserK p1) (K.fromParserK p2)
10541054
10551055-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
@@ -1064,8 +1064,8 @@ manyTill f p1 p2 =
10641064--
10651065{-# INLINE manyThen #-}
10661066manyThen :: -- (Foldable t, MonadCatch m) =>
1067− Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1068−manyThen _f _parser _recover = undefined
1067+ Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1068+manyThen _parser _recover _f = undefined
10691069
10701070-------------------------------------------------------------------------------
10711071-- Interleaving a collection of parsers
@@ -1079,8 +1079,8 @@ manyThen _f _parser _recover = undefined
10791079--
10801080{-# INLINE roundRobin #-}
10811081roundRobin :: -- (Foldable t, MonadCatch m) =>
1082− Fold m b c -> t (Parser m a b) -> Parser m a c
1083−roundRobin _f _ps = undefined
1082+ t (Parser m a b) -> Fold m b c -> Parser m a c
1083+roundRobin _ps _f = undefined
10841084
10851085-------------------------------------------------------------------------------
10861086-- Repeated Alternatives
@@ -1094,8 +1094,8 @@ roundRobin _f _ps = undefined
10941094--
10951095{-# INLINE retryMaxTotal #-}
10961096retryMaxTotal :: -- (MonadCatch m) =>
1097− Int -> Fold m b c -> Parser m a b -> Parser m a c
1098−retryMaxTotal _n _f _p = undefined
1097+ Int -> Parser m a b -> Fold m b c -> Parser m a c
1098+retryMaxTotal _n _p _f = undefined
10991099
11001100-- | Like 'retryMaxTotal' but aborts after @n@ successive failures.
11011101--
@@ -1103,8 +1103,8 @@ retryMaxTotal _n _f _p = undefined
11031103--
11041104{-# INLINE retryMaxSuccessive #-}
11051105retryMaxSuccessive :: -- (MonadCatch m) =>
1106− Int -> Fold m b c -> Parser m a b -> Parser m a c
1107−retryMaxSuccessive _n _f _p = undefined
1106+ Int -> Parser m a b -> Fold m b c -> Parser m a c
1107+retryMaxSuccessive _n _p _f = undefined
11081108
11091109-- | Keep trying a parser until it succeeds. When the parser fails the input
11101110-- consumed till now is dropped and the new instance is tried on the fresh
src/Streamly/Internal/Data/Parser/ParserD.hs+6−6
@@ -902,7 +902,7 @@ choice _ps = undefined
902902-- /Pre-release/
903903--
904904{-# INLINE many #-}
905−many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
905+many :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
906906many = splitMany
907907-- many = countBetween 0 maxBound
908908
@@ -911,7 +911,7 @@ many = splitMany
911911-- /Pre-release/
912912--
913913{-# INLINE some #-}
914−some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
914+some :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
915915some = splitSome
916916-- some f p = many (takeGE 1 f) p
917917-- many = countBetween 1 maxBound
@@ -923,9 +923,9 @@ some = splitSome
923923{-# INLINE countBetween #-}
924924countBetween ::
925925 -- MonadCatch m =>
926− Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
927−countBetween _m _n _f = undefined
928−-- countBetween m n f p = many (takeBetween m n f) p
926+ Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
927+countBetween _m _n _p = undefined
928+-- countBetween m n p f = many (takeBetween m n f) p
929929
930930-- | See 'Streamly.Internal.Data.Parser.count'.
931931--
@@ -934,7 +934,7 @@ countBetween _m _n _f = undefined
934934{-# INLINE count #-}
935935count ::
936936 -- MonadCatch m =>
937− Int -> Fold m b c -> Parser m a b -> Parser m a c
937+ Int -> Parser m a b -> Fold m b c -> Parser m a c
938938count n = countBetween n n
939939-- count n f p = many (takeEQ n f) p
940940
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+6−6
@@ -601,8 +601,8 @@ alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
601601-- /Pre-release/
602602--
603603{-# INLINE splitMany #-}
604−splitMany :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
605−splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
604+splitMany :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
605+splitMany (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
606606 Parser step initial extract
607607
608608 where
@@ -661,8 +661,8 @@ splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
661661-- /Pre-release/
662662--
663663{-# INLINE splitSome #-}
664−splitSome :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
665−splitSome (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
664+splitSome :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
665+splitSome (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
666666 Parser step initial extract
667667
668668 where
@@ -789,10 +789,10 @@ instance MonadCatch m => Alternative (Parser m a) where
789789 (<|>) = alt
790790
791791 {-# INLINE many #-}
792− many = splitMany toList
792+ many = flip splitMany toList
793793
794794 {-# INLINE some #-}
795− some = splitSome toList
795+ some = flip splitSome toList
796796
797797{-# ANN type ConcatParseState Fuse #-}
798798data ConcatParseState sl m a b =
test/Streamly/Test/Data/Parser.hs+4−4
@@ -320,7 +320,7 @@ sliceSepByP =
320320 Left _ -> property False
321321 where
322322 predicate = (>= 100)
323− prsr = P.many FL.toList (P.satisfy (const True))
323+ prsr = P.many (P.satisfy (const True)) FL.toList
324324 tkwhl ls = Prelude.takeWhile (not . predicate) ls
325325
326326sliceBeginWith :: Property
@@ -400,7 +400,7 @@ wordBy =
400400 where
401401
402402 predicate = (== ' ')
403− parser = P.many FL.toList (P.wordBy predicate FL.toList)
403+ parser = P.many (P.wordBy predicate FL.toList) FL.toList
404404 words' lst =
405405 let wrds = words lst
406406 in if wrds == [] then [""] else wrds
@@ -511,7 +511,7 @@ many =
511511 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
512512 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
513513 prsr =
514− P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
514+ flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
515515 in
516516 case S.parse prsr (S.fromList ls) of
517517 Right res_list -> checkListEqual res_list
@@ -532,7 +532,7 @@ some =
532532 fldstp conL currL = return $ FL.Partial $ conL ++ currL
533533 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
534534 prsr =
535− P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
535+ flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
536536 in
537537 case S.parse prsr (S.fromList ls) of
538538 Right res_list -> res_list == Prelude.filter (== 0) ls
test/Streamly/Test/Data/Parser/ParserD.hs+5−5
@@ -377,7 +377,7 @@ wordBy =
377377 where
378378
379379 predicate = (== ' ')
380− parser = P.many FL.toList (P.wordBy predicate FL.toList)
380+ parser = P.many (P.wordBy predicate FL.toList) FL.toList
381381 words' lst =
382382 let wrds = words lst
383383 in if wrds == [] then [""] else wrds
@@ -515,7 +515,7 @@ many =
515515 concatFold =
516516 FL.Fold fldstp (return (FL.Partial [])) return
517517 prsr =
518− P.many concatFold
518+ flip P.many concatFold
519519 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
520520 in case S.parseD prsr (S.fromList ls) of
521521 Right res_list ->
@@ -524,7 +524,7 @@ many =
524524
525525many_empty :: Property
526526many_empty =
527− property (case S.parseD (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
527+ property (case S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
528528 Right res_list -> checkListEqual res_list ([] :: [Int])
529529 Left _ -> property False)
530530
@@ -535,7 +535,7 @@ some =
535535 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
536536 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
537537 prsr =
538− P.some concatFold
538+ flip P.some concatFold
539539 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
540540 in case S.parseD prsr (S.fromList ls) of
541541 Right res_list -> res_list == Prelude.filter (== 0) ls
@@ -543,7 +543,7 @@ some =
543543
544544someFail :: Property
545545someFail =
546− property (case S.parseD (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
546+ property (case S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
547547 Right _ -> False
548548 Left _ -> True)
549549