@@ -52,6 +52,7 @@ module Streamly.Internal.Data.Parser.ParserD
52 , takeGE -- takeBetween n maxBound
52 , takeGE -- takeBetween n maxBound
54 -- Grab a sequence of input elements by inspecting them
54 -- Grab a sequence of input elements by inspecting them
55 , lookAhead
56 , lookAhead
56 , takeWhile
57 , takeWhile
57 , takeWhile1
58 , takeWhile1
@@ -814,6 +815,56 @@ spanByRolling eq f1 f2 =
814-- nested parsers
815-- nested parsers
815-------------------------------------------------------------------------------
816-------------------------------------------------------------------------------
818+-- | See 'Streamly.Internal.Data.Parser.takeP'.
822+takeP :: Monad m => Int -> Parser m a b -> Parser m a b
823+takeP lim (Parser pstep pinitial pextract) = Parser step initial extract
832+ then return $ IPartial $ Tuple' 0 s
833+ else IDone <$> pextract s
834+ IDone b -> return $ IDone b
835+ IError e -> return $ IError e
837+ step (Tuple' cnt r) a = do
838+ assert (cnt < lim) (return ())
843+ assert (cnt1 >= 0) (return ())
845+ then return $ Partial 0 $ Tuple' cnt1 s
846+ else Done 0 <$> pextract s
848+ assert (cnt1 >= 0) (return ())
850+ then return $ Continue 0 $ Tuple' cnt1 s
851+ -- XXX This should error out?
852+ -- If designed properly, this will probably error out.
853+ -- "pextract" should error out
854+ else Done 0 <$> pextract s
856+ let taken = cnt1 - n
857+ assert (taken >= 0) (return ())
858+ return $ Partial n $ Tuple' taken s
860+ let taken = cnt1 - n
861+ assert (taken >= 0) (return ())
862+ return $ Continue n $ Tuple' taken s
863+ Done n b -> return $ Done n b
864+ Error str -> return $ Error str
866+ extract (Tuple' _ r) = pextract r
817-- | See 'Streamly.Internal.Data.Parser.lookahead'.
868-- | See 'Streamly.Internal.Data.Parser.lookahead'.
819-- /Pre-release/
870-- /Pre-release/