on master help sign in
Change argument order in parser combinators #969
4da398aRanjeet Kumar Ranjan
benchmark/Streamly/Benchmark/Data/Parser.hs+4−4
@@ -111,11 +111,11 @@ wordBy value = IP.parse (PR.wordBy (>= value) FL.drain)
111
111
112{-# INLINE manyWordByEven #-}
112{-# INLINE manyWordByEven #-}
113manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
113manyWordByEven :: 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)
115
115
116{-# INLINE many #-}
116{-# INLINE many #-}
117many :: MonadCatch m => SerialT m Int -> m Int
117many :: 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)
119
119
120{-# INLINE manyAlt #-}
120{-# INLINE manyAlt #-}
121manyAlt :: MonadCatch m => SerialT m Int -> m Int
121manyAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -125,7 +125,7 @@ manyAlt xs = do
125
125
126{-# INLINE some #-}
126{-# INLINE some #-}
127some :: MonadCatch m => SerialT m Int -> m Int
127some :: 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)
129
129
130{-# INLINE someAlt #-}
130{-# INLINE someAlt #-}
131someAlt :: MonadCatch m => SerialT m Int -> m Int
131someAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -136,7 +136,7 @@ someAlt xs = do
136{-# INLINE manyTill #-}
136{-# INLINE manyTill #-}
137manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
137manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
138manyTill value =
138manyTill 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)
140
140
141{-# INLINE splitAp #-}
141{-# INLINE splitAp #-}
142splitAp :: MonadCatch m
142splitAp :: MonadCatch m
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+3−3
@@ -94,11 +94,11 @@ wordBy value = IP.parseD (PR.wordBy (>= value) FL.drain)
94{-# INLINE manyWordByEven #-}
94{-# INLINE manyWordByEven #-}
95manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
95manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
96manyWordByEven =
96manyWordByEven =
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)
98
98
99{-# INLINE many #-}
99{-# INLINE many #-}
100many :: MonadCatch m => SerialT m Int -> m Int
100many :: 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)
102
102
103{-# INLINE manyAlt #-}
103{-# INLINE manyAlt #-}
104manyAlt :: MonadCatch m => SerialT m Int -> m Int
104manyAlt :: MonadCatch m => SerialT m Int -> m Int
@@ -108,7 +108,7 @@ manyAlt xs = do
108
108
109{-# INLINE some #-}
109{-# INLINE some #-}
110some :: MonadCatch m => SerialT m Int -> m Int
110some :: 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)
112
112
113{-# INLINE someAlt #-}
113{-# INLINE someAlt #-}
114someAlt :: MonadCatch m => SerialT m Int -> m Int
114someAlt :: MonadCatch m => SerialT m Int -> m Int
src/Streamly/Internal/Data/Parser.hs+25−25
@@ -953,8 +953,8 @@ choice _ps = undefined
953--
953--
954{-# INLINE manyP #-}
954{-# INLINE manyP #-}
955manyP :: -- MonadCatch m =>
955manyP :: -- MonadCatch m =>
956− Parser m b c -> Parser m a b -> Parser m a c
956+ Parser m a b -> Parser m b c -> Parser m a c
957−manyP _f _p = undefined -- K.toParserK $ D.manyP f (K.fromParserK p)
957+manyP _p _f = undefined -- K.toParserK $ D.manyP (K.fromParserK p) f
958
958
959-- | Collect zero or more parses. Apply the supplied parser repeatedly on the
959-- | Collect zero or more parses. Apply the supplied parser repeatedly on the
960-- input stream and push the parse results to a downstream fold.
960-- 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)
967-- /Pre-release/
967-- /Pre-release/
968--
968--
969{-# INLINE many #-}
969{-# INLINE many #-}
970−many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
970+many :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
971−many f p = K.toParserK $ D.many f (K.fromParserK p)
971+many p f = K.toParserK $ D.many (K.fromParserK p) f
972-- many = countBetween 0 maxBound
972-- many = countBetween 0 maxBound
973
973
974-- Note: many1 would perhaps be a better name for this and consistent with
974-- 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)
988-- /Pre-release/
988-- /Pre-release/
989--
989--
990{-# INLINE some #-}
990{-# INLINE some #-}
991−some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
991+some :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
992−some f p = K.toParserK $ D.some f (K.fromParserK p)
992+some p f = K.toParserK $ D.some (K.fromParserK p) f
993−-- some f p = manyP (takeGE 1 f) p
993+-- some p f = manyP p (takeGE 1 f)
994-- many = countBetween 1 maxBound
994-- many = countBetween 1 maxBound
995
995
996-- | @countBetween m n f p@ collects between @m@ and @n@ sequential parses of
996-- | @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)
1002{-# INLINE countBetween #-}
1002{-# INLINE countBetween #-}
1003countBetween ::
1003countBetween ::
1004 -- MonadCatch m =>
1004 -- MonadCatch m =>
1005− Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
1005+ Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
1006−countBetween _m _n _f = undefined
1006+countBetween _m _n _p = undefined
1007−-- countBetween m n f p = manyP (takeBetween m n f) p
1007+-- countBetween m n p f = manyP (takeBetween m n f) p
1008
1008
1009-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
1009-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
1010-- the fold @f@. Fails if the input ends or the parser fails before @n@
1010-- the fold @f@. Fails if the input ends or the parser fails before @n@
@@ -1015,9 +1015,9 @@ countBetween _m _n _f = undefined
1015{-# INLINE count #-}
1015{-# INLINE count #-}
1016count ::
1016count ::
1017 -- MonadCatch m =>
1017 -- 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
1019count n = countBetween n n
1019count n = countBetween n n
1020−-- count n f p = manyP (takeEQ n f) p
1020+-- count n p f = manyP (takeEQ n f) p
1021
1021
1022-- | Like 'manyTill' but uses a 'Parser' to collect the results instead of a
1022-- | Like 'manyTill' but uses a 'Parser' to collect the results instead of a
1023-- 'Fold'. Parsing stops or fails if the collecting parser stops or fails.
1023-- 'Fold'. Parsing stops or fails if the collecting parser stops or fails.
@@ -1032,9 +1032,9 @@ count n = countBetween n n
1032--
1032--
1033{-# INLINE manyTillP #-}
1033{-# INLINE manyTillP #-}
1034manyTillP :: -- MonadCatch m =>
1034manyTillP :: -- MonadCatch m =>
1035− Parser m b c -> Parser m a b -> Parser m a x -> Parser m a c
1035+ Parser m a b -> Parser m a x -> Parser m b c -> Parser m a c
1036−manyTillP _f _p1 _p2 = undefined
1036+manyTillP _p1 _p2 _f = undefined
1037− -- K.toParserK $ D.manyTillP f (K.fromParserK p1) (K.fromParserK p2)
1037+ -- K.toParserK $ D.manyTillP (K.fromParserK p1) (K.fromParserK p2) f
1038
1038
1039-- | @manyTill f collect test@ tries the parser @test@ on the input, if @test@
1039-- | @manyTill f collect test@ tries the parser @test@ on the input, if @test@
1040-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
1040-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
@@ -1048,8 +1048,8 @@ manyTillP _f _p1 _p2 = undefined
1048--
1048--
1049{-# INLINE manyTill #-}
1049{-# INLINE manyTill #-}
1050manyTill :: MonadCatch m
1050manyTill :: MonadCatch m
1051− => Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1051+ => Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1052−manyTill f p1 p2 =
1052+manyTill p1 p2 f =
1053 K.toParserK $ D.manyTill f (K.fromParserK p1) (K.fromParserK p2)
1053 K.toParserK $ D.manyTill f (K.fromParserK p1) (K.fromParserK p2)
1054
1054
1055-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
1055-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
@@ -1064,8 +1064,8 @@ manyTill f p1 p2 =
1064--
1064--
1065{-# INLINE manyThen #-}
1065{-# INLINE manyThen #-}
1066manyThen :: -- (Foldable t, MonadCatch m) =>
1066manyThen :: -- (Foldable t, MonadCatch m) =>
1067− Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1067+ Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1068−manyThen _f _parser _recover = undefined
1068+manyThen _parser _recover _f = undefined
1069
1069
1070-------------------------------------------------------------------------------
1070-------------------------------------------------------------------------------
1071-- Interleaving a collection of parsers
1071-- Interleaving a collection of parsers
@@ -1079,8 +1079,8 @@ manyThen _f _parser _recover = undefined
1079--
1079--
1080{-# INLINE roundRobin #-}
1080{-# INLINE roundRobin #-}
1081roundRobin :: -- (Foldable t, MonadCatch m) =>
1081roundRobin :: -- (Foldable t, MonadCatch m) =>
1082− Fold m b c -> t (Parser m a b) -> Parser m a c
1082+ t (Parser m a b) -> Fold m b c -> Parser m a c
1083−roundRobin _f _ps = undefined
1083+roundRobin _ps _f = undefined
1084
1084
1085-------------------------------------------------------------------------------
1085-------------------------------------------------------------------------------
1086-- Repeated Alternatives
1086-- Repeated Alternatives
@@ -1094,8 +1094,8 @@ roundRobin _f _ps = undefined
1094--
1094--
1095{-# INLINE retryMaxTotal #-}
1095{-# INLINE retryMaxTotal #-}
1096retryMaxTotal :: -- (MonadCatch m) =>
1096retryMaxTotal :: -- (MonadCatch m) =>
1097− Int -> Fold m b c -> Parser m a b -> Parser m a c
1097+ Int -> Parser m a b -> Fold m b c -> Parser m a c
1098−retryMaxTotal _n _f _p = undefined
1098+retryMaxTotal _n _p _f = undefined
1099
1099
1100-- | Like 'retryMaxTotal' but aborts after @n@ successive failures.
1100-- | Like 'retryMaxTotal' but aborts after @n@ successive failures.
1101--
1101--
@@ -1103,8 +1103,8 @@ retryMaxTotal _n _f _p = undefined
1103--
1103--
1104{-# INLINE retryMaxSuccessive #-}
1104{-# INLINE retryMaxSuccessive #-}
1105retryMaxSuccessive :: -- (MonadCatch m) =>
1105retryMaxSuccessive :: -- (MonadCatch m) =>
1106− Int -> Fold m b c -> Parser m a b -> Parser m a c
1106+ Int -> Parser m a b -> Fold m b c -> Parser m a c
1107−retryMaxSuccessive _n _f _p = undefined
1107+retryMaxSuccessive _n _p _f = undefined
1108
1108
1109-- | Keep trying a parser until it succeeds. When the parser fails the input
1109-- | Keep trying a parser until it succeeds. When the parser fails the input
1110-- consumed till now is dropped and the new instance is tried on the fresh
1110-- 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
902-- /Pre-release/
902-- /Pre-release/
903--
903--
904{-# INLINE many #-}
904{-# 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
906many = splitMany
906many = splitMany
907-- many = countBetween 0 maxBound
907-- many = countBetween 0 maxBound
908
908
@@ -911,7 +911,7 @@ many = splitMany
911-- /Pre-release/
911-- /Pre-release/
912--
912--
913{-# INLINE some #-}
913{-# 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
915some = splitSome
915some = splitSome
916-- some f p = many (takeGE 1 f) p
916-- some f p = many (takeGE 1 f) p
917-- many = countBetween 1 maxBound
917-- many = countBetween 1 maxBound
@@ -923,9 +923,9 @@ some = splitSome
923{-# INLINE countBetween #-}
923{-# INLINE countBetween #-}
924countBetween ::
924countBetween ::
925 -- MonadCatch m =>
925 -- MonadCatch m =>
926− Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
926+ Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
927−countBetween _m _n _f = undefined
927+countBetween _m _n _p = undefined
928−-- countBetween m n f p = many (takeBetween m n f) p
928+-- countBetween m n p f = many (takeBetween m n f) p
929
929
930-- | See 'Streamly.Internal.Data.Parser.count'.
930-- | See 'Streamly.Internal.Data.Parser.count'.
931--
931--
@@ -934,7 +934,7 @@ countBetween _m _n _f = undefined
934{-# INLINE count #-}
934{-# INLINE count #-}
935count ::
935count ::
936 -- MonadCatch m =>
936 -- 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
938count n = countBetween n n
938count n = countBetween n n
939-- count n f p = many (takeEQ n f) p
939-- count n f p = many (takeEQ n f) p
940
940
src/Streamly/Internal/Data/Parser/ParserD/Types.hs+6−6
@@ -601,8 +601,8 @@ alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
601-- /Pre-release/
601-- /Pre-release/
602--
602--
603{-# INLINE splitMany #-}
603{-# INLINE splitMany #-}
604−splitMany :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
604+splitMany :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
605−splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
605+splitMany (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
606 Parser step initial extract
606 Parser step initial extract
607
607
608 where
608 where
@@ -661,8 +661,8 @@ splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
661-- /Pre-release/
661-- /Pre-release/
662--
662--
663{-# INLINE splitSome #-}
663{-# INLINE splitSome #-}
664−splitSome :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
664+splitSome :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
665−splitSome (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
665+splitSome (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
666 Parser step initial extract
666 Parser step initial extract
667
667
668 where
668 where
@@ -789,10 +789,10 @@ instance MonadCatch m => Alternative (Parser m a) where
789 (<|>) = alt
789 (<|>) = alt
790
790
791 {-# INLINE many #-}
791 {-# INLINE many #-}
792− many = splitMany toList
792+ many = flip splitMany toList
793
793
794 {-# INLINE some #-}
794 {-# INLINE some #-}
795− some = splitSome toList
795+ some = flip splitSome toList
796
796
797{-# ANN type ConcatParseState Fuse #-}
797{-# ANN type ConcatParseState Fuse #-}
798data ConcatParseState sl m a b =
798data ConcatParseState sl m a b =
test/Streamly/Test/Data/Parser.hs+4−4
@@ -320,7 +320,7 @@ sliceSepByP =
320 Left _ -> property False
320 Left _ -> property False
321 where
321 where
322 predicate = (>= 100)
322 predicate = (>= 100)
323− prsr = P.many FL.toList (P.satisfy (const True))
323+ prsr = P.many (P.satisfy (const True)) FL.toList
324 tkwhl ls = Prelude.takeWhile (not . predicate) ls
324 tkwhl ls = Prelude.takeWhile (not . predicate) ls
325
325
326sliceBeginWith :: Property
326sliceBeginWith :: Property
@@ -400,7 +400,7 @@ wordBy =
400 where
400 where
401
401
402 predicate = (== ' ')
402 predicate = (== ' ')
403− parser = P.many FL.toList (P.wordBy predicate FL.toList)
403+ parser = P.many (P.wordBy predicate FL.toList) FL.toList
404 words' lst =
404 words' lst =
405 let wrds = words lst
405 let wrds = words lst
406 in if wrds == [] then [""] else wrds
406 in if wrds == [] then [""] else wrds
@@ -511,7 +511,7 @@ many =
511 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
511 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
512 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
512 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
513 prsr =
513 prsr =
514− P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
514+ flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
515 in
515 in
516 case S.parse prsr (S.fromList ls) of
516 case S.parse prsr (S.fromList ls) of
517 Right res_list -> checkListEqual res_list
517 Right res_list -> checkListEqual res_list
@@ -532,7 +532,7 @@ some =
532 fldstp conL currL = return $ FL.Partial $ conL ++ currL
532 fldstp conL currL = return $ FL.Partial $ conL ++ currL
533 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
533 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
534 prsr =
534 prsr =
535− P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
535+ flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
536 in
536 in
537 case S.parse prsr (S.fromList ls) of
537 case S.parse prsr (S.fromList ls) of
538 Right res_list -> res_list == Prelude.filter (== 0) ls
538 Right res_list -> res_list == Prelude.filter (== 0) ls
test/Streamly/Test/Data/Parser/ParserD.hs+5−5
@@ -377,7 +377,7 @@ wordBy =
377 where
377 where
378
378
379 predicate = (== ' ')
379 predicate = (== ' ')
380− parser = P.many FL.toList (P.wordBy predicate FL.toList)
380+ parser = P.many (P.wordBy predicate FL.toList) FL.toList
381 words' lst =
381 words' lst =
382 let wrds = words lst
382 let wrds = words lst
383 in if wrds == [] then [""] else wrds
383 in if wrds == [] then [""] else wrds
@@ -515,7 +515,7 @@ many =
515 concatFold =
515 concatFold =
516 FL.Fold fldstp (return (FL.Partial [])) return
516 FL.Fold fldstp (return (FL.Partial [])) return
517 prsr =
517 prsr =
518− P.many concatFold
518+ flip P.many concatFold
519 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
519 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
520 in case S.parseD prsr (S.fromList ls) of
520 in case S.parseD prsr (S.fromList ls) of
521 Right res_list ->
521 Right res_list ->
@@ -524,7 +524,7 @@ many =
524
524
525many_empty :: Property
525many_empty :: Property
526many_empty =
526many_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
528 Right res_list -> checkListEqual res_list ([] :: [Int])
528 Right res_list -> checkListEqual res_list ([] :: [Int])
529 Left _ -> property False)
529 Left _ -> property False)
530
530
@@ -535,7 +535,7 @@ some =
535 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
535 let fldstp conL currL = return $ FL.Partial $ conL ++ currL
536 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
536 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
537 prsr =
537 prsr =
538− P.some concatFold
538+ flip P.some concatFold
539 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
539 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
540 in case S.parseD prsr (S.fromList ls) of
540 in case S.parseD prsr (S.fromList ls) of
541 Right res_list -> res_list == Prelude.filter (== 0) ls
541 Right res_list -> res_list == Prelude.filter (== 0) ls
@@ -543,7 +543,7 @@ some =
543
543
544someFail :: Property
544someFail :: Property
545someFail =
545someFail =
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
547 Right _ -> False
547 Right _ -> False
548 Left _ -> True)
548 Left _ -> True)
549
549