on master help sign in
Rename the CPS Parser type to ParserK (#2270)
commitfa71024febd02dc8d6d59b279e97cd7ec7a72bf4 authorRanjeet Ranjan <ranjeetr@composewell.com> dateThu 2 Mar 2023, 13:59 (4 years ago) parent7252323
4 files changed+19−22
benchmark/Streamly/Benchmark/Data/Parser/ParserK.hs+2−2 core/src/Streamly/Data/Parser/ParserK.hs−1 core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+14−17 core/src/Streamly/Internal/Data/Stream/StreamK.hs+3−2
benchmark/Streamly/Benchmark/Data/Parser/ParserK.hs+2−2
@@ -84,11 +84,11 @@ one value = StreamK.parseChunks p
8484 Nothing -> pure Nothing
8585
8686{-# INLINE satisfy #-}
87−satisfy :: (MonadIO m, Unbox a) => (a -> Bool) -> PR.Parser a m a
87+satisfy :: (MonadIO m, Unbox a) => (a -> Bool) -> PR.ParserK a m a
8888satisfy = PR.fromParser . PRD.satisfy
8989
9090{-# INLINE takeWhile #-}
91−takeWhile :: (MonadIO m, Unbox a) => (a -> Bool) -> PR.Parser a m ()
91+takeWhile :: (MonadIO m, Unbox a) => (a -> Bool) -> PR.ParserK a m ()
9292takeWhile p = PR.fromParser $ PRD.takeWhile p FL.drain
9393
9494{-# INLINE takeWhileK #-}
core/src/Streamly/Data/Parser/ParserK.hs−1
@@ -15,7 +15,6 @@ module Streamly.Data.Parser.ParserK
1515 (
1616 -- * Parser Type
1717 ParserK
18− , Parser
1918
2019 -- * Parsers
2120 -- ** Conversions
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+14−17
@@ -21,8 +21,7 @@ module Streamly.Internal.Data.Parser.ParserK.Type
2121 Step (..)
2222 , Input (..)
2323 , ParseResult (..)
24− , Parser (..)
25− , ParserK
24+ , ParserK (..)
2625 , fromParser
2726 -- , toParser
2827 , fromPure
@@ -111,7 +110,7 @@ instance Functor ParseResult where
111110-- input without a result or return a result with no further input to be
112111-- consumed.
113112--
114−newtype Parser a m b = MkParser
113+newtype ParserK a m b = MkParser
115114 { runParser :: forall r.
116115 -- Using "Input" in runParser is not necessary but it avoids making
117116 -- one more function call to get the input. This could be helpful
@@ -143,8 +142,6 @@ newtype Parser a m b = MkParser
143142 -> m (Step a m r)
144143 }
145144
146−type ParserK = Parser
147−
148145-------------------------------------------------------------------------------
149146-- Functor
150147-------------------------------------------------------------------------------
@@ -152,7 +149,7 @@ type ParserK = Parser
152149-- XXX rewrite this using ParserD, expose rmapM from ParserD.
153150-- | Maps a function over the output of the parser.
154151--
155−instance Functor m => Functor (Parser a m) where
152+instance Functor m => Functor (ParserK a m) where
156153 {-# INLINE fmap #-}
157154 fmap f parser = MkParser $ \k n st arr ->
158155 let k1 res = k (fmap f res)
@@ -169,7 +166,7 @@ instance Functor m => Functor (Parser a m) where
169166-- /Pre-release/
170167--
171168{-# INLINE fromPure #-}
172−fromPure :: b -> Parser a m b
169+fromPure :: b -> ParserK a m b
173170fromPure b = MkParser $ \k n st arr -> k (Success n b) st arr
174171
175172-- | See 'Streamly.Internal.Data.Parser.fromEffect'.
@@ -177,7 +174,7 @@ fromPure b = MkParser $ \k n st arr -> k (Success n b) st arr
177174-- /Pre-release/
178175--
179176{-# INLINE fromEffect #-}
180−fromEffect :: Monad m => m b -> Parser a m b
177+fromEffect :: Monad m => m b -> ParserK a m b
181178fromEffect eff =
182179 MkParser $ \k n st arr -> eff >>= \b -> k (Success n b) st arr
183180
@@ -185,7 +182,7 @@ fromEffect eff =
185182-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
186183-- when fusion is important.
187184--
188−instance Monad m => Applicative (Parser a m) where
185+instance Monad m => Applicative (ParserK a m) where
189186 {-# INLINE pure #-}
190187 pure = fromPure
191188
@@ -222,7 +219,7 @@ instance Monad m => Applicative (Parser a m) where
222219-- /Pre-release/
223220--
224221{-# INLINE die #-}
225−die :: String -> Parser a m b
222+die :: String -> ParserK a m b
226223die err = MkParser (\k n st arr -> k (Failure n err) st arr)
227224
228225-- | Monad composition can be used for lookbehind parsers, we can make the
@@ -267,7 +264,7 @@ die err = MkParser (\k n st arr -> k (Failure n err) st arr)
267264-- does not fuse, use 'Streamly.Internal.Data.Parser.concatMap' when you need
268265-- fusion.
269266--
270−instance Monad m => Monad (Parser a m) where
267+instance Monad m => Monad (ParserK a m) where
271268 {-# INLINE return #-}
272269 return = pure
273270
@@ -286,11 +283,11 @@ instance Monad m => Monad (Parser a m) where
286283 {-# INLINE fail #-}
287284 fail = die
288285#endif
289−instance Monad m => Fail.MonadFail (Parser a m) where
286+instance Monad m => Fail.MonadFail (ParserK a m) where
290287 {-# INLINE fail #-}
291288 fail = die
292289
293−instance MonadIO m => MonadIO (Parser a m) where
290+instance MonadIO m => MonadIO (ParserK a m) where
294291 {-# INLINE liftIO #-}
295292 liftIO = fromEffect . liftIO
296293
@@ -311,7 +308,7 @@ instance MonadIO m => MonadIO (Parser a m) where
311308-- does not fuse, use 'Streamly.Internal.Data.Parser.alt' when you need
312309-- fusion.
313310--
314−instance Monad m => Alternative (Parser a m) where
311+instance Monad m => Alternative (ParserK a m) where
315312 {-# INLINE empty #-}
316313 empty = die "empty"
317314
@@ -344,7 +341,7 @@ instance Monad m => Alternative (Parser a m) where
344341-- | 'mzero' is same as 'empty', it aborts the parser. 'mplus' is same as
345342-- '<|>', it selects the first succeeding parser.
346343--
347−instance Monad m => MonadPlus (Parser a m) where
344+instance Monad m => MonadPlus (ParserK a m) where
348345 {-# INLINE mzero #-}
349346 mzero = die "mzero"
350347
@@ -352,7 +349,7 @@ instance Monad m => MonadPlus (Parser a m) where
352349 mplus = (<|>)
353350
354351{-
355−instance MonadTrans (Parser a) where
352+instance MonadTrans (ParserK a) where
356353 {-# INLINE lift #-}
357354 lift = fromEffect
358355-}
@@ -521,7 +518,7 @@ parserDone _ _ _ = error "Bug: toParser: called with input"
521518-- /Pre-release/
522519--
523520{-# INLINE_LATE toParser #-}
524−toParser :: Monad m => Parser a m b -> ParserD.Parser a m b
521+toParser :: Monad m => ParserK a m b -> ParserD.Parser a m b
525522toParser parser = ParserD.Parser step initial extract
526523
527524 where
core/src/Streamly/Internal/Data/Stream/StreamK.hs+3−2
@@ -233,6 +233,7 @@ import Streamly.Internal.Data.Fold.Type (Fold(..))
233233import Streamly.Internal.Data.Producer.Type (Producer(..))
234234import Streamly.Internal.Data.SVar.Type (adaptState, defState)
235235import Streamly.Internal.Data.Unboxed (sizeOf, Unbox)
236+import Streamly.Internal.Data.Parser.ParserK.Type (ParserK)
236237
237238import qualified Streamly.Internal.Data.Array.Type as Array
238239import qualified Streamly.Internal.Data.Fold.Type as FL
@@ -1283,7 +1284,7 @@ parserDone (ParserK.Failure n e) _ _ = pure $ ParserK.Error n e
12831284{-# INLINE_NORMAL parseBreakChunks #-}
12841285parseBreakChunks
12851286 :: (Monad m, Unbox a)
1286− => ParserK.Parser a m b
1287+ => ParserK a m b
12871288 -> StreamK m (Array a)
12881289 -> m (Either ParseError b, StreamK m (Array a))
12891290parseBreakChunks parser input = do
@@ -1373,7 +1374,7 @@ parseBreakChunks parser input = do
13731374
13741375{-# INLINE parseChunks #-}
13751376parseChunks :: (Monad m, Unbox a) =>
1376− ParserK.Parser a m b -> StreamK m (Array a) -> m (Either ParseError b)
1377+ ParserK a m b -> StreamK m (Array a) -> m (Either ParseError b)
13771378parseChunks f = fmap fst . parseBreakChunks f
13781379
13791380-------------------------------------------------------------------------------