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.
This commit is contained in:
Ranjeet Kumar Ranjan 2021-03-08 17:42:25 +05:30 committed by Harendra Kumar
parent bfebe943c0
commit 4da398aa00
7 changed files with 53 additions and 53 deletions

View File

@ -111,11 +111,11 @@ wordBy value = IP.parse (PR.wordBy (>= value) FL.drain)
{-# INLINE manyWordByEven #-}
manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
manyWordByEven = IP.parse (PR.many FL.drain (PR.wordBy (Prelude.even) FL.drain))
manyWordByEven = IP.parse (PR.many (PR.wordBy (Prelude.even) FL.drain) FL.drain)
{-# INLINE many #-}
many :: MonadCatch m => SerialT m Int -> m Int
many = IP.parse (PR.many FL.length (PR.satisfy (> 0)))
many = IP.parse (PR.many (PR.satisfy (> 0)) FL.length)
{-# INLINE manyAlt #-}
manyAlt :: MonadCatch m => SerialT m Int -> m Int
@ -125,7 +125,7 @@ manyAlt xs = do
{-# INLINE some #-}
some :: MonadCatch m => SerialT m Int -> m Int
some = IP.parse (PR.some FL.length (PR.satisfy (> 0)))
some = IP.parse (PR.some (PR.satisfy (> 0)) FL.length)
{-# INLINE someAlt #-}
someAlt :: MonadCatch m => SerialT m Int -> m Int
@ -136,7 +136,7 @@ someAlt xs = do
{-# INLINE manyTill #-}
manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
manyTill value =
IP.parse (PR.manyTill FL.length (PR.satisfy (> 0)) (PR.satisfy (== value)))
IP.parse (PR.manyTill (PR.satisfy (> 0)) (PR.satisfy (== value)) FL.length)
{-# INLINE splitAp #-}
splitAp :: MonadCatch m

View File

@ -94,11 +94,11 @@ wordBy value = IP.parseD (PR.wordBy (>= value) FL.drain)
{-# INLINE manyWordByEven #-}
manyWordByEven :: MonadCatch m => SerialT m Int -> m ()
manyWordByEven =
IP.parseD (PR.many FL.drain (PR.wordBy (Prelude.even) FL.drain))
IP.parseD (PR.many (PR.wordBy (Prelude.even) FL.drain) FL.drain)
{-# INLINE many #-}
many :: MonadCatch m => SerialT m Int -> m Int
many = IP.parseD (PR.many FL.length (PR.satisfy (> 0)))
many = IP.parseD (PR.many (PR.satisfy (> 0)) FL.length)
{-# INLINE manyAlt #-}
manyAlt :: MonadCatch m => SerialT m Int -> m Int
@ -108,7 +108,7 @@ manyAlt xs = do
{-# INLINE some #-}
some :: MonadCatch m => SerialT m Int -> m Int
some = IP.parseD (PR.some FL.length (PR.satisfy (> 0)))
some = IP.parseD (PR.some (PR.satisfy (> 0)) FL.length)
{-# INLINE someAlt #-}
someAlt :: MonadCatch m => SerialT m Int -> m Int

View File

@ -953,8 +953,8 @@ choice _ps = undefined
--
{-# INLINE manyP #-}
manyP :: -- MonadCatch m =>
Parser m b c -> Parser m a b -> Parser m a c
manyP _f _p = undefined -- K.toParserK $ D.manyP f (K.fromParserK p)
Parser m a b -> Parser m b c -> Parser m a c
manyP _p _f = undefined -- K.toParserK $ D.manyP (K.fromParserK p) f
-- | Collect zero or more parses. Apply the supplied parser repeatedly on the
-- 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)
-- /Pre-release/
--
{-# INLINE many #-}
many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
many f p = K.toParserK $ D.many f (K.fromParserK p)
many :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
many p f = K.toParserK $ D.many (K.fromParserK p) f
-- many = countBetween 0 maxBound
-- 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)
-- /Pre-release/
--
{-# INLINE some #-}
some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
some f p = K.toParserK $ D.some f (K.fromParserK p)
-- some f p = manyP (takeGE 1 f) p
some :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
some p f = K.toParserK $ D.some (K.fromParserK p) f
-- some p f = manyP p (takeGE 1 f)
-- many = countBetween 1 maxBound
-- | @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)
{-# INLINE countBetween #-}
countBetween ::
-- MonadCatch m =>
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween _m _n _f = undefined
-- countBetween m n f p = manyP (takeBetween m n f) p
Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
countBetween _m _n _p = undefined
-- countBetween m n p f = manyP (takeBetween m n f) p
-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
-- the fold @f@. Fails if the input ends or the parser fails before @n@
@ -1015,9 +1015,9 @@ countBetween _m _n _f = undefined
{-# INLINE count #-}
count ::
-- MonadCatch m =>
Int -> Fold m b c -> Parser m a b -> Parser m a c
Int -> Parser m a b -> Fold m b c -> Parser m a c
count n = countBetween n n
-- count n f p = manyP (takeEQ n f) p
-- count n p f = manyP (takeEQ n f) p
-- | Like 'manyTill' but uses a 'Parser' to collect the results instead of a
-- 'Fold'. Parsing stops or fails if the collecting parser stops or fails.
@ -1032,9 +1032,9 @@ count n = countBetween n n
--
{-# INLINE manyTillP #-}
manyTillP :: -- MonadCatch m =>
Parser m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTillP _f _p1 _p2 = undefined
-- K.toParserK $ D.manyTillP f (K.fromParserK p1) (K.fromParserK p2)
Parser m a b -> Parser m a x -> Parser m b c -> Parser m a c
manyTillP _p1 _p2 _f = undefined
-- K.toParserK $ D.manyTillP (K.fromParserK p1) (K.fromParserK p2) f
-- | @manyTill f collect test@ tries the parser @test@ on the input, if @test@
-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
@ -1048,8 +1048,8 @@ manyTillP _f _p1 _p2 = undefined
--
{-# INLINE manyTill #-}
manyTill :: MonadCatch m
=> Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill f p1 p2 =
=> Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
manyTill p1 p2 f =
K.toParserK $ D.manyTill f (K.fromParserK p1) (K.fromParserK p2)
-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
@ -1064,8 +1064,8 @@ manyTill f p1 p2 =
--
{-# INLINE manyThen #-}
manyThen :: -- (Foldable t, MonadCatch m) =>
Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyThen _f _parser _recover = undefined
Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
manyThen _parser _recover _f = undefined
-------------------------------------------------------------------------------
-- Interleaving a collection of parsers
@ -1079,8 +1079,8 @@ manyThen _f _parser _recover = undefined
--
{-# INLINE roundRobin #-}
roundRobin :: -- (Foldable t, MonadCatch m) =>
Fold m b c -> t (Parser m a b) -> Parser m a c
roundRobin _f _ps = undefined
t (Parser m a b) -> Fold m b c -> Parser m a c
roundRobin _ps _f = undefined
-------------------------------------------------------------------------------
-- Repeated Alternatives
@ -1094,8 +1094,8 @@ roundRobin _f _ps = undefined
--
{-# INLINE retryMaxTotal #-}
retryMaxTotal :: -- (MonadCatch m) =>
Int -> Fold m b c -> Parser m a b -> Parser m a c
retryMaxTotal _n _f _p = undefined
Int -> Parser m a b -> Fold m b c -> Parser m a c
retryMaxTotal _n _p _f = undefined
-- | Like 'retryMaxTotal' but aborts after @n@ successive failures.
--
@ -1103,8 +1103,8 @@ retryMaxTotal _n _f _p = undefined
--
{-# INLINE retryMaxSuccessive #-}
retryMaxSuccessive :: -- (MonadCatch m) =>
Int -> Fold m b c -> Parser m a b -> Parser m a c
retryMaxSuccessive _n _f _p = undefined
Int -> Parser m a b -> Fold m b c -> Parser m a c
retryMaxSuccessive _n _p _f = undefined
-- | Keep trying a parser until it succeeds. When the parser fails the input
-- consumed till now is dropped and the new instance is tried on the fresh

View File

@ -902,7 +902,7 @@ choice _ps = undefined
-- /Pre-release/
--
{-# INLINE many #-}
many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
many :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
many = splitMany
-- many = countBetween 0 maxBound
@ -911,7 +911,7 @@ many = splitMany
-- /Pre-release/
--
{-# INLINE some #-}
some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
some :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
some = splitSome
-- some f p = many (takeGE 1 f) p
-- many = countBetween 1 maxBound
@ -923,9 +923,9 @@ some = splitSome
{-# INLINE countBetween #-}
countBetween ::
-- MonadCatch m =>
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween _m _n _f = undefined
-- countBetween m n f p = many (takeBetween m n f) p
Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
countBetween _m _n _p = undefined
-- countBetween m n p f = many (takeBetween m n f) p
-- | See 'Streamly.Internal.Data.Parser.count'.
--
@ -934,7 +934,7 @@ countBetween _m _n _f = undefined
{-# INLINE count #-}
count ::
-- MonadCatch m =>
Int -> Fold m b c -> Parser m a b -> Parser m a c
Int -> Parser m a b -> Fold m b c -> Parser m a c
count n = countBetween n n
-- count n f p = many (takeEQ n f) p

View File

@ -601,8 +601,8 @@ alt (Parser stepL initialL extractL) (Parser stepR initialR extractR) =
-- /Pre-release/
--
{-# INLINE splitMany #-}
splitMany :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
splitMany :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
splitMany (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
Parser step initial extract
where
@ -661,8 +661,8 @@ splitMany (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
-- /Pre-release/
--
{-# INLINE splitSome #-}
splitSome :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
splitSome (Fold fstep finitial fextract) (Parser step1 initial1 extract1) =
splitSome :: MonadCatch m => Parser m a b -> Fold m b c -> Parser m a c
splitSome (Parser step1 initial1 extract1) (Fold fstep finitial fextract) =
Parser step initial extract
where
@ -789,10 +789,10 @@ instance MonadCatch m => Alternative (Parser m a) where
(<|>) = alt
{-# INLINE many #-}
many = splitMany toList
many = flip splitMany toList
{-# INLINE some #-}
some = splitSome toList
some = flip splitSome toList
{-# ANN type ConcatParseState Fuse #-}
data ConcatParseState sl m a b =

View File

@ -320,7 +320,7 @@ sliceSepByP =
Left _ -> property False
where
predicate = (>= 100)
prsr = P.many FL.toList (P.satisfy (const True))
prsr = P.many (P.satisfy (const True)) FL.toList
tkwhl ls = Prelude.takeWhile (not . predicate) ls
sliceBeginWith :: Property
@ -400,7 +400,7 @@ wordBy =
where
predicate = (== ' ')
parser = P.many FL.toList (P.wordBy predicate FL.toList)
parser = P.many (P.wordBy predicate FL.toList) FL.toList
words' lst =
let wrds = words lst
in if wrds == [] then [""] else wrds
@ -511,7 +511,7 @@ many =
let fldstp conL currL = return $ FL.Partial $ conL ++ currL
concatFold = FL.Fold fldstp (return (FL.Partial [])) return
prsr =
P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
in
case S.parse prsr (S.fromList ls) of
Right res_list -> checkListEqual res_list
@ -532,7 +532,7 @@ some =
fldstp conL currL = return $ FL.Partial $ conL ++ currL
concatFold = FL.Fold fldstp (return (FL.Partial [])) return
prsr =
P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
in
case S.parse prsr (S.fromList ls) of
Right res_list -> res_list == Prelude.filter (== 0) ls

View File

@ -377,7 +377,7 @@ wordBy =
where
predicate = (== ' ')
parser = P.many FL.toList (P.wordBy predicate FL.toList)
parser = P.many (P.wordBy predicate FL.toList) FL.toList
words' lst =
let wrds = words lst
in if wrds == [] then [""] else wrds
@ -515,7 +515,7 @@ many =
concatFold =
FL.Fold fldstp (return (FL.Partial [])) return
prsr =
P.many concatFold
flip P.many concatFold
$ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
in case S.parseD prsr (S.fromList ls) of
Right res_list ->
@ -524,7 +524,7 @@ many =
many_empty :: Property
many_empty =
property (case S.parseD (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
property (case S.parseD (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
Right res_list -> checkListEqual res_list ([] :: [Int])
Left _ -> property False)
@ -535,7 +535,7 @@ some =
let fldstp conL currL = return $ FL.Partial $ conL ++ currL
concatFold = FL.Fold fldstp (return (FL.Partial [])) return
prsr =
P.some concatFold
flip P.some concatFold
$ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
in case S.parseD prsr (S.fromList ls) of
Right res_list -> res_list == Prelude.filter (== 0) ls
@ -543,7 +543,7 @@ some =
someFail :: Property
someFail =
property (case S.parseD (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
property (case S.parseD (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
Right _ -> False
Left _ -> True)