@@ -76,9 +76,12 @@ module Streamly.Internal.Data.Parser.ParserD
76 , takeEndBy_
76 , takeEndBy_
77 , takeEndByEsc
77 , takeEndByEsc
78 , takeStartBy
78 , takeStartBy
80 -- Words and grouping
82 -- Words and grouping
83 , groupByRolling
86 , groupByRolling
84 , groupByRollingEither
87 , groupByRollingEither
@@ -787,6 +790,101 @@ takeStartBy cond (Fold fstep finitial fextract) =
787 extract (Left' s) = fextract s
790 extract (Left' s) = fextract s
788 extract (Right' s) = fextract s
791 extract (Right' s) = fextract s
793+data FramedEscState s =
794+ FrameEscInit !s | FrameEscGo !s !Int | FrameEscEsc !s !Int
796+{-# INLINE takeFramedByEsc_ #-}
797+takeFramedByEsc_ :: MonadThrow m =>
798+ (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
799+takeFramedByEsc_ isEsc isBegin isEnd (Fold fstep finitial fextract) =
801+ Parser step initial extract
809+ FL.Partial s -> IPartial (FrameEscInit s)
811+ error "takeFramedByEsc_: fold done without input"
813+ {-# INLINE process #-}
818+ FL.Partial s1 -> Continue 0 (FrameEscGo s1 n)
819+ FL.Done b -> Done 0 b
821+ step (FrameEscInit s) a =
823+ then return $ Partial 0 (FrameEscGo s 0)
824+ else return $ Error "takeFramedByEsc_: missing frame start"
825+ step (FrameEscGo s n) a =
827+ then return $ Partial 0 $ FrameEscEsc s n
831+ let n1 = if isBegin a then n + 1 else n
835+ then Done 0 <$> fextract s
836+ else process s a (n - 1)
837+ step (FrameEscEsc s n) a = process s a n
839+ err = throwM . ParseError
841+ extract (FrameEscInit _) = err "takeFramedByEsc_: empty token"
842+ extract (FrameEscGo _ _) = err "takeFramedByEsc_: missing frame end"
843+ extract (FrameEscEsc _ _) = err "takeFramedByEsc_: trailing escape"
845+data FramedState s = FrameInit !s | FrameGo !s Int
847+{-# INLINE takeFramedBy_ #-}
848+takeFramedBy_ :: MonadThrow m =>
849+ (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
850+takeFramedBy_ isBegin isEnd (Fold fstep finitial fextract) =
852+ Parser step initial extract
860+ FL.Partial s -> IPartial (FrameInit s)
862+ error "takeFramedBy_: fold done without input"
864+ {-# INLINE process #-}
869+ FL.Partial s1 -> Continue 0 (FrameGo s1 n)
870+ FL.Done b -> Done 0 b
872+ step (FrameInit s) a =
874+ then return $ Continue 0 (FrameGo s 0)
875+ else return $ Error "takeFramedBy_: missing frame start"
876+ step (FrameGo s n) a
878+ let n1 = if isBegin a then n + 1 else n
880+ | n == 0 = Done 0 <$> fextract s
881+ | otherwise = process s a (n - 1)
883+ err = throwM . ParseError
885+ extract (FrameInit _) = err "takeFramedBy_: empty token"
886+ extract (FrameGo _ _) = err "takeFramedBy_: missing frame end"
790-------------------------------------------------------------------------------
888-------------------------------------------------------------------------------
791-- Grouping and words
889-- Grouping and words
792-------------------------------------------------------------------------------
890-------------------------------------------------------------------------------