on master help sign in
Rename the CPS Parser type to ParserK (#2270)
fa71024Ranjeet Ranjan
benchmark/Streamly/Benchmark/Data/Parser/ParserK.hs+2−2
@@ -84,11 +84,11 @@ one value = StreamK.parseChunks p
84 Nothing -> pure Nothing
84 Nothing -> pure Nothing
85
85
86{-# INLINE satisfy #-}
86{-# 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
88satisfy = PR.fromParser . PRD.satisfy
88satisfy = PR.fromParser . PRD.satisfy
89
89
90{-# INLINE takeWhile #-}
90{-# 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 ()
92takeWhile p = PR.fromParser $ PRD.takeWhile p FL.drain
92takeWhile p = PR.fromParser $ PRD.takeWhile p FL.drain
93
93
94{-# INLINE takeWhileK #-}
94{-# INLINE takeWhileK #-}
core/src/Streamly/Data/Parser/ParserK.hs−1
@@ -15,7 +15,6 @@ module Streamly.Data.Parser.ParserK
15 (
15 (
16 -- * Parser Type
16 -- * Parser Type
17 ParserK
17 ParserK
18− , Parser
19
18
20 -- * Parsers
19 -- * Parsers
21 -- ** Conversions
20 -- ** Conversions
core/src/Streamly/Internal/Data/Parser/ParserK/Type.hs+14−17
@@ -21,8 +21,7 @@ module Streamly.Internal.Data.Parser.ParserK.Type
21 Step (..)
21 Step (..)
22 , Input (..)
22 , Input (..)
23 , ParseResult (..)
23 , ParseResult (..)
24− , Parser (..)
24+ , ParserK (..)
25− , ParserK
26 , fromParser
25 , fromParser
27 -- , toParser
26 -- , toParser
28 , fromPure
27 , fromPure
@@ -111,7 +110,7 @@ instance Functor ParseResult where
111-- input without a result or return a result with no further input to be
110-- input without a result or return a result with no further input to be
112-- consumed.
111-- consumed.
113--
112--
114−newtype Parser a m b = MkParser
113+newtype ParserK a m b = MkParser
115 { runParser :: forall r.
114 { runParser :: forall r.
116 -- Using "Input" in runParser is not necessary but it avoids making
115 -- Using "Input" in runParser is not necessary but it avoids making
117 -- one more function call to get the input. This could be helpful
116 -- one more function call to get the input. This could be helpful
@@ -143,8 +142,6 @@ newtype Parser a m b = MkParser
143 -> m (Step a m r)
142 -> m (Step a m r)
144 }
143 }
145
144
146−type ParserK = Parser
147−
148-------------------------------------------------------------------------------
145-------------------------------------------------------------------------------
149-- Functor
146-- Functor
150-------------------------------------------------------------------------------
147-------------------------------------------------------------------------------
@@ -152,7 +149,7 @@ type ParserK = Parser
152-- XXX rewrite this using ParserD, expose rmapM from ParserD.
149-- XXX rewrite this using ParserD, expose rmapM from ParserD.
153-- | Maps a function over the output of the parser.
150-- | Maps a function over the output of the parser.
154--
151--
155−instance Functor m => Functor (Parser a m) where
152+instance Functor m => Functor (ParserK a m) where
156 {-# INLINE fmap #-}
153 {-# INLINE fmap #-}
157 fmap f parser = MkParser $ \k n st arr ->
154 fmap f parser = MkParser $ \k n st arr ->
158 let k1 res = k (fmap f res)
155 let k1 res = k (fmap f res)
@@ -169,7 +166,7 @@ instance Functor m => Functor (Parser a m) where
169-- /Pre-release/
166-- /Pre-release/
170--
167--
171{-# INLINE fromPure #-}
168{-# INLINE fromPure #-}
172−fromPure :: b -> Parser a m b
169+fromPure :: b -> ParserK a m b
173fromPure b = MkParser $ \k n st arr -> k (Success n b) st arr
170fromPure b = MkParser $ \k n st arr -> k (Success n b) st arr
174
171
175-- | See 'Streamly.Internal.Data.Parser.fromEffect'.
172-- | See 'Streamly.Internal.Data.Parser.fromEffect'.
@@ -177,7 +174,7 @@ fromPure b = MkParser $ \k n st arr -> k (Success n b) st arr
177-- /Pre-release/
174-- /Pre-release/
178--
175--
179{-# INLINE fromEffect #-}
176{-# INLINE fromEffect #-}
180−fromEffect :: Monad m => m b -> Parser a m b
177+fromEffect :: Monad m => m b -> ParserK a m b
181fromEffect eff =
178fromEffect eff =
182 MkParser $ \k n st arr -> eff >>= \b -> k (Success n b) st arr
179 MkParser $ \k n st arr -> eff >>= \b -> k (Success n b) st arr
183
180
@@ -185,7 +182,7 @@ fromEffect eff =
185-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
182-- this operation does not fuse, use 'Streamly.Internal.Data.Parser.splitWith'
186-- when fusion is important.
183-- when fusion is important.
187--
184--
188−instance Monad m => Applicative (Parser a m) where
185+instance Monad m => Applicative (ParserK a m) where
189 {-# INLINE pure #-}
186 {-# INLINE pure #-}
190 pure = fromPure
187 pure = fromPure
191
188
@@ -222,7 +219,7 @@ instance Monad m => Applicative (Parser a m) where
222-- /Pre-release/
219-- /Pre-release/
223--
220--
224{-# INLINE die #-}
221{-# INLINE die #-}
225−die :: String -> Parser a m b
222+die :: String -> ParserK a m b
226die err = MkParser (\k n st arr -> k (Failure n err) st arr)
223die err = MkParser (\k n st arr -> k (Failure n err) st arr)
227
224
228-- | Monad composition can be used for lookbehind parsers, we can make the
225-- | 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)
267-- does not fuse, use 'Streamly.Internal.Data.Parser.concatMap' when you need
264-- does not fuse, use 'Streamly.Internal.Data.Parser.concatMap' when you need
268-- fusion.
265-- fusion.
269--
266--
270−instance Monad m => Monad (Parser a m) where
267+instance Monad m => Monad (ParserK a m) where
271 {-# INLINE return #-}
268 {-# INLINE return #-}
272 return = pure
269 return = pure
273
270
@@ -286,11 +283,11 @@ instance Monad m => Monad (Parser a m) where
286 {-# INLINE fail #-}
283 {-# INLINE fail #-}
287 fail = die
284 fail = die
288#endif
285#endif
289−instance Monad m => Fail.MonadFail (Parser a m) where
286+instance Monad m => Fail.MonadFail (ParserK a m) where
290 {-# INLINE fail #-}
287 {-# INLINE fail #-}
291 fail = die
288 fail = die
292
289
293−instance MonadIO m => MonadIO (Parser a m) where
290+instance MonadIO m => MonadIO (ParserK a m) where
294 {-# INLINE liftIO #-}
291 {-# INLINE liftIO #-}
295 liftIO = fromEffect . liftIO
292 liftIO = fromEffect . liftIO
296
293
@@ -311,7 +308,7 @@ instance MonadIO m => MonadIO (Parser a m) where
311-- does not fuse, use 'Streamly.Internal.Data.Parser.alt' when you need
308-- does not fuse, use 'Streamly.Internal.Data.Parser.alt' when you need
312-- fusion.
309-- fusion.
313--
310--
314−instance Monad m => Alternative (Parser a m) where
311+instance Monad m => Alternative (ParserK a m) where
315 {-# INLINE empty #-}
312 {-# INLINE empty #-}
316 empty = die "empty"
313 empty = die "empty"
317
314
@@ -344,7 +341,7 @@ instance Monad m => Alternative (Parser a m) where
344-- | 'mzero' is same as 'empty', it aborts the parser. 'mplus' is same as
341-- | 'mzero' is same as 'empty', it aborts the parser. 'mplus' is same as
345-- '<|>', it selects the first succeeding parser.
342-- '<|>', it selects the first succeeding parser.
346--
343--
347−instance Monad m => MonadPlus (Parser a m) where
344+instance Monad m => MonadPlus (ParserK a m) where
348 {-# INLINE mzero #-}
345 {-# INLINE mzero #-}
349 mzero = die "mzero"
346 mzero = die "mzero"
350
347
@@ -352,7 +349,7 @@ instance Monad m => MonadPlus (Parser a m) where
352 mplus = (<|>)
349 mplus = (<|>)
353
350
354{-
351{-
355−instance MonadTrans (Parser a) where
352+instance MonadTrans (ParserK a) where
356 {-# INLINE lift #-}
353 {-# INLINE lift #-}
357 lift = fromEffect
354 lift = fromEffect
358-}
355-}
@@ -521,7 +518,7 @@ parserDone _ _ _ = error "Bug: toParser: called with input"
521-- /Pre-release/
518-- /Pre-release/
522--
519--
523{-# INLINE_LATE toParser #-}
520{-# 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
525toParser parser = ParserD.Parser step initial extract
522toParser parser = ParserD.Parser step initial extract
526
523
527 where
524 where
core/src/Streamly/Internal/Data/Stream/StreamK.hs+3−2
@@ -233,6 +233,7 @@ import Streamly.Internal.Data.Fold.Type (Fold(..))
233import Streamly.Internal.Data.Producer.Type (Producer(..))
233import Streamly.Internal.Data.Producer.Type (Producer(..))
234import Streamly.Internal.Data.SVar.Type (adaptState, defState)
234import Streamly.Internal.Data.SVar.Type (adaptState, defState)
235import Streamly.Internal.Data.Unboxed (sizeOf, Unbox)
235import Streamly.Internal.Data.Unboxed (sizeOf, Unbox)
236+import Streamly.Internal.Data.Parser.ParserK.Type (ParserK)
236
237
237import qualified Streamly.Internal.Data.Array.Type as Array
238import qualified Streamly.Internal.Data.Array.Type as Array
238import qualified Streamly.Internal.Data.Fold.Type as FL
239import qualified Streamly.Internal.Data.Fold.Type as FL
@@ -1283,7 +1284,7 @@ parserDone (ParserK.Failure n e) _ _ = pure $ ParserK.Error n e
1283{-# INLINE_NORMAL parseBreakChunks #-}
1284{-# INLINE_NORMAL parseBreakChunks #-}
1284parseBreakChunks
1285parseBreakChunks
1285 :: (Monad m, Unbox a)
1286 :: (Monad m, Unbox a)
1286− => ParserK.Parser a m b
1287+ => ParserK a m b
1287 -> StreamK m (Array a)
1288 -> StreamK m (Array a)
1288 -> m (Either ParseError b, StreamK m (Array a))
1289 -> m (Either ParseError b, StreamK m (Array a))
1289parseBreakChunks parser input = do
1290parseBreakChunks parser input = do
@@ -1373,7 +1374,7 @@ parseBreakChunks parser input = do
1373
1374
1374{-# INLINE parseChunks #-}
1375{-# INLINE parseChunks #-}
1375parseChunks :: (Monad m, Unbox a) =>
1376parseChunks :: (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)
1377parseChunks f = fmap fst . parseBreakChunks f
1378parseChunks f = fmap fst . parseBreakChunks f
1378
1379
1379-------------------------------------------------------------------------------
1380-------------------------------------------------------------------------------