on master help sign in
Rename some parser APIs, reorg the export list
6ed0c93Harendra Kumar
benchmark/Streamly/Benchmark/Data/Parser.hs+9−9
@@ -92,12 +92,12 @@ takeEQ value = IP.parse (PR.takeEQ value FL.drain)
92dropWhile :: MonadCatch m => Int -> SerialT m Int -> m ()
92dropWhile :: MonadCatch m => Int -> SerialT m Int -> m ()
93dropWhile value = IP.parse (PR.dropWhile (<= value))
93dropWhile value = IP.parse (PR.dropWhile (<= value))
94
94
95−{-# INLINE sliceBeginWith #-}
95+{-# INLINE takeStartBy #-}
96−sliceBeginWith :: MonadCatch m => Int -> SerialT m Int -> m ()
96+takeStartBy :: MonadCatch m => Int -> SerialT m Int -> m ()
97−sliceBeginWith value stream = do
97+takeStartBy value stream = do
98 stream1 <- return . fromMaybe (S.fromPure (value + 1)) =<< S.tail stream
98 stream1 <- return . fromMaybe (S.fromPure (value + 1)) =<< S.tail stream
99 let stream2 = value `S.cons` stream1
99 let stream2 = value `S.cons` stream1
100− IP.parse (PR.sliceBeginWith (== value) FL.drain) stream2
100+ IP.parse (PR.takeStartBy (== value) FL.drain) stream2
101
101
102{-# INLINE takeWhile #-}
102{-# INLINE takeWhile #-}
103takeWhile :: MonadCatch m => Int -> SerialT m Int -> m ()
103takeWhile :: MonadCatch m => Int -> SerialT m Int -> m ()
@@ -212,10 +212,10 @@ split_ value =
212 (PR.dropWhile (<= value))
212 (PR.dropWhile (<= value))
213 )
213 )
214
214
215−{-# INLINE sliceSepByP #-}
215+{-# INLINE takeEndBy_ #-}
216−sliceSepByP :: MonadCatch m
216+takeEndBy_ :: MonadCatch m
217 => Int -> SerialT m Int -> m()
217 => Int -> SerialT m Int -> m()
218−sliceSepByP value = IP.parse (PR.sliceSepByP (>= value) (PR.fromFold FL.drain))
218+takeEndBy_ value = IP.parse (PR.takeEndBy_ (>= value) (PR.fromFold FL.drain))
219
219
220{-# INLINE teeAllAny #-}
220{-# INLINE teeAllAny #-}
221teeAllAny :: MonadCatch m
221teeAllAny :: MonadCatch m
@@ -362,7 +362,7 @@ o_1_space_serial value =
362 , benchIOSink value "takeWhile" $ takeWhile value
362 , benchIOSink value "takeWhile" $ takeWhile value
363 , benchIOSink value "takeP" $ takeP value
363 , benchIOSink value "takeP" $ takeP value
364 , benchIOSink value "dropWhile" $ dropWhile value
364 , benchIOSink value "dropWhile" $ dropWhile value
365− , benchIOSink value "sliceBeginWith" $ sliceBeginWith value
365+ , benchIOSink value "takeStartBy" $ takeStartBy value
366 , benchIOSink value "groupBy" $ groupBy
366 , benchIOSink value "groupBy" $ groupBy
367 , benchIOSink value "groupByRolling" $ groupByRolling
367 , benchIOSink value "groupByRolling" $ groupByRolling
368 , benchIOSink value "wordBy" $ wordBy value
368 , benchIOSink value "wordBy" $ wordBy value
@@ -372,7 +372,7 @@ o_1_space_serial value =
372 , benchIOSink value "splitApBefore" $ splitApBefore value
372 , benchIOSink value "splitApBefore" $ splitApBefore value
373 , benchIOSink value "splitApAfter" $ splitApAfter value
373 , benchIOSink value "splitApAfter" $ splitApAfter value
374 , benchIOSink value "serialWith" $ serialWith value
374 , benchIOSink value "serialWith" $ serialWith value
375− , benchIOSink value "sliceSepByP" $ sliceSepByP value
375+ , benchIOSink value "takeEndBy_" $ takeEndBy_ value
376 , benchIOSink value "many" many
376 , benchIOSink value "many" many
377 , benchIOSink value "many (wordBy even)" $ manyWordByEven
377 , benchIOSink value "many (wordBy even)" $ manyWordByEven
378 , benchIOSink value "some" some
378 , benchIOSink value "some" some
benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs+9−9
@@ -79,12 +79,12 @@ benchIOSinkRandom value name f =
79drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
79drainWhile :: MonadThrow m => (a -> Bool) -> PR.Parser m a ()
80drainWhile p = PR.takeWhile p FL.drain
80drainWhile p = PR.takeWhile p FL.drain
81
81
82−{-# INLINE sliceBeginWith #-}
82+{-# INLINE takeStartBy #-}
83−sliceBeginWith :: MonadCatch m => Int -> SerialT m Int -> m ()
83+takeStartBy :: MonadCatch m => Int -> SerialT m Int -> m ()
84−sliceBeginWith value stream = do
84+takeStartBy value stream = do
85 stream1 <- return . fromMaybe (S.fromPure (value + 1)) =<< S.tail stream
85 stream1 <- return . fromMaybe (S.fromPure (value + 1)) =<< S.tail stream
86 let stream2 = value `S.cons` stream1
86 let stream2 = value `S.cons` stream1
87− IP.parseD (PR.sliceBeginWith (== value) FL.drain) stream2
87+ IP.parseD (PR.takeStartBy (== value) FL.drain) stream2
88
88
89{-# INLINE takeWhile #-}
89{-# INLINE takeWhile #-}
90takeWhile :: MonadThrow m => Int -> SerialT m Int -> m ()
90takeWhile :: MonadThrow m => Int -> SerialT m Int -> m ()
@@ -135,9 +135,9 @@ someAlt xs = do
135 x <- IP.parseD (AP.some (PR.satisfy (> 0))) xs
135 x <- IP.parseD (AP.some (PR.satisfy (> 0))) xs
136 return $ Prelude.length x
136 return $ Prelude.length x
137
137
138−{-#INLINE sliceSepByP #-}
138+{-#INLINE takeEndBy_ #-}
139−sliceSepByP :: MonadCatch m => Int -> SerialT m Int -> m ()
139+takeEndBy_ :: MonadCatch m => Int -> SerialT m Int -> m ()
140−sliceSepByP value = IP.parseD (PR.sliceSepByP (>= value) (PR.fromFold FL.drain))
140+takeEndBy_ value = IP.parseD (PR.takeEndBy_ (>= value) (PR.fromFold FL.drain))
141
141
142{-# INLINE manyTill #-}
142{-# INLINE manyTill #-}
143manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
143manyTill :: MonadCatch m => Int -> SerialT m Int -> m Int
@@ -323,7 +323,7 @@ o_1_space_serial value =
323 [ benchIOSink value "takeWhile" $ takeWhile value
323 [ benchIOSink value "takeWhile" $ takeWhile value
324 , benchIOSink value "takeP" $ takeP value
324 , benchIOSink value "takeP" $ takeP value
325 , benchIOSink value "takeBetween" $ takeBetween value
325 , benchIOSink value "takeBetween" $ takeBetween value
326− , benchIOSink value "sliceBeginWith" $ sliceBeginWith value
326+ , benchIOSink value "takeStartBy" $ takeStartBy value
327 , benchIOSink value "groupBy" $ groupBy
327 , benchIOSink value "groupBy" $ groupBy
328 , benchIOSink value "groupByRolling" $ groupByRolling
328 , benchIOSink value "groupByRolling" $ groupByRolling
329 , benchIOSink value "wordBy" $ wordBy value
329 , benchIOSink value "wordBy" $ wordBy value
@@ -331,7 +331,7 @@ o_1_space_serial value =
331 , benchIOSink value "many" many
331 , benchIOSink value "many" many
332 , benchIOSink value "many (wordBy even)" $ manyWordByEven
332 , benchIOSink value "many (wordBy even)" $ manyWordByEven
333 , benchIOSink value "some" some
333 , benchIOSink value "some" some
334− , benchIOSink value "sliceSepByP" $ sliceSepByP value
334+ , benchIOSink value "takeEndBy_" $ takeEndBy_ value
335 , benchIOSink value "manyTill" $ manyTill value
335 , benchIOSink value "manyTill" $ manyTill value
336 , benchIOSink value "tee (all,any)" $ teeAllAny value
336 , benchIOSink value "tee (all,any)" $ teeAllAny value
337 , benchIOSink value "teeFst (all,any)" $ teeFstAllAny value
337 , benchIOSink value "teeFst (all,any)" $ teeFstAllAny value
core/src/Streamly/Internal/Data/Parser.hs+95−101
@@ -80,11 +80,14 @@ module Streamly.Internal.Data.Parser
80 , maybe
80 , maybe
81 , either
81 , either
82
82
83− -- * Sequence parsers
83+ -- * Sequence parsers (tokenizers)
84 --
84 --
85 -- | Parsers chained in series, if one parser terminates the composition
85 -- | Parsers chained in series, if one parser terminates the composition
86 -- terminates.
86 -- terminates.
87
87
88+ , lookAhead
89+
90+ -- ** By length
88 -- | Grab a sequence of input elements without inspecting them
91 -- | Grab a sequence of input elements without inspecting them
89 , takeBetween
92 , takeBetween
90 -- , take -- takeBetween 0 n
93 -- , take -- takeBetween 0 n
@@ -93,30 +96,40 @@ module Streamly.Internal.Data.Parser
93 , takeP
96 , takeP
94
97
95 -- Grab a sequence of input elements by inspecting them
98 -- Grab a sequence of input elements by inspecting them
99+ -- ** Exact match
96 , eqBy
100 , eqBy
97 , list
101 , list
98− , lookAhead
102+
103+ -- ** By predicate
99 , takeWhileP
104 , takeWhileP
100 , takeWhile
105 , takeWhile
101 -- $takeWhile
106 -- $takeWhile
102 , takeWhile1
107 , takeWhile1
103 , dropWhile
108 , dropWhile
104
109
105− -- Separators
110+ -- ** Separators
106− , sliceSepByP
111+ -- , takeEndBy
107− , sliceBeginWith
112+ , takeEndBy_
108− , sliceSepWith
113+ , takeEndByEsc
109−
114+ -- , takeEndByEsc_
110− -- Quoting and Escaping
115+ , takeStartBy
111− , escapedSliceSepBy
116+ -- , takeStartBy_
112− , escapedFrameBy
117+ , takeEitherSepBy
113−
114− -- Words and grouping
115 , wordBy
118 , wordBy
119+ -- , wordByEsc
120+
121+ -- ** By comparing
116 , groupBy
122 , groupBy
117 , groupByRolling
123 , groupByRolling
118 , groupByRollingEither
124 , groupByRollingEither
119
125
126+ -- ** Framing
127+ -- , takeFramedBy
128+ -- , takeFramedBy_
129+ , takeFramedByEsc
130+ -- , wordByQuoted
131+ -- , wordByQuotedEsc
132+
120 -- | Unimplemented
133 -- | Unimplemented
121 --
134 --
122 -- @
135 -- @
@@ -143,6 +156,24 @@ module Streamly.Internal.Data.Parser
143 -- secondary parser.
156 -- secondary parser.
144 , deintercalate
157 , deintercalate
145
158
159+ -- *** Special cases
160+ -- | TODO: traditional implmentations of these may be of limited use. For
161+ -- example, consider parsing lines separated by @\\r\\n@. The main parser
162+ -- will have to detect and exclude the sequence @\\r\\n@ anyway so that we
163+ -- can apply the "sep" parser.
164+ --
165+ -- We can instead implement these as special cases of deintercalate.
166+ --
167+ -- @
168+ -- , endBy
169+ -- , sepEndBy
170+ -- , beginBy
171+ -- , sepBeginBy
172+ -- , sepAroundBy
173+ -- @
174+ , sepBy1
175+ , sepBy
176+
146 -- ** Sequential Alternative
177 -- ** Sequential Alternative
147 , alt
178 , alt
148
179
@@ -167,24 +198,6 @@ module Streamly.Internal.Data.Parser
167 , manyTill
198 , manyTill
168 , manyThen
199 , manyThen
169
200
170− -- ** Special cases
171− -- | TODO: traditional implmentations of these may be of limited use. For
172− -- example, consider parsing lines separated by @\\r\\n@. The main parser
173− -- will have to detect and exclude the sequence @\\r\\n@ anyway so that we
174− -- can apply the "sep" parser.
175− --
176− -- We can instead implement these as special cases of deintercalate.
177− --
178− -- @
179− -- , endBy
180− , sepBy1
181− , sepBy
182− -- , sepEndBy
183− -- , beginBy
184− -- , sepBeginBy
185− -- , sepAroundBy
186− -- @
187−
188 -- * Distribution
201 -- * Distribution
189 --
202 --
190 -- | A simple and stupid impl would be to just convert the stream to an
203 -- | A simple and stupid impl would be to just convert the stream to an
@@ -524,12 +537,9 @@ either = D.toParserK . D.either
524-- @takeBetween@ is the most general take operation, other take operations can
537-- @takeBetween@ is the most general take operation, other take operations can
525-- be defined in terms of takeBetween. For example:
538-- be defined in terms of takeBetween. For example:
526--
539--
527−-- @
528−-- take = takeBetween 0 n -- equivalent of take
540+-- >>> take n = Parser.takeBetween 0 n
529−-- take1 = takeBetween 1 n -- equivalent of takeLE1
530−-- takeEQ = takeBetween n n
541+-- >>> takeEQ n = Parser.takeBetween n n
531−-- takeGE = takeBetween n maxBound
542+-- >>> takeGE n = Parser.takeBetween n maxBound
532−-- @
533--
543--
534-- /Pre-release/
544-- /Pre-release/
535--
545--
@@ -584,10 +594,10 @@ takeGE n = D.toParserK . D.takeGE n
584-- input. The combinator stops when the condition fails or if the collecting
594-- input. The combinator stops when the condition fails or if the collecting
585-- parser stops.
595-- parser stops.
586--
596--
587−-- This is a generalized version of takeWhile, for example 'takeWhile1' can be
588−-- implemented in terms of this:
597+-- Other interesting parsers can be implemented in terms of this parser:
589--
598--
590−-- >>> takeWhile1 cond p = takeWhile cond (takeBetween 1 maxBound p)
599+-- >>> takeWhile1 cond p = Parser.takeWhileP cond (Parser.takeBetween 1 maxBound p)
600+-- >>> takeWhileBetween cond m n p = Parser.takeWhileP cond (Parser.takeBetween m n p)
591--
601--
592-- Stops: when the condition fails or the collecting parser stops.
602-- Stops: when the condition fails or the collecting parser stops.
593-- Fails: when the collecting parser fails.
603-- Fails: when the collecting parser fails.
@@ -650,89 +660,73 @@ dropWhile p = takeWhile p FL.drain
650-- Separators
660-- Separators
651-------------------------------------------------------------------------------
661-------------------------------------------------------------------------------
652
662
653−-- | @sliceSepByP cond parser@ parses a slice of the input using @parser@ until
663+-- | @takeEndBy_ cond parser@ parses a token that ends by a separator chosen by
654−-- @cond@ succeeds or the parser stops.
664+-- the supplied predicate. The separator is dropped.
655--
665--
656−-- This is a generalized slicing parser which can be used to implement other
666+-- This can be combined with other parsers to implement other interesting
657−-- parsers e.g.:
667+-- parsers as follows:
658--
668--
659−-- @
669+-- >>> takeEndByOrMax cond n p = Parser.takeEndBy_ cond (Parser.fromFold $ Fold.take n p)
660−-- sliceSepByMax cond n p = sliceSepByP cond (take n p)
661−-- sliceSepByBetween cond m n p = sliceSepByP cond (takeBetween m n p)
670+-- >>> takeEndByBetween cond m n p = Parser.takeEndBy_ cond (Parser.takeBetween m n p)
662−-- @
663--
671--
664-- /Pre-release/
672-- /Pre-release/
665--
673--
666−{-# INLINE sliceSepByP #-}
674+{-# INLINE takeEndBy_ #-}
667−sliceSepByP ::
668− MonadCatch m =>
669− (a -> Bool) -> Parser m a b -> Parser m a b
675+takeEndBy_ :: MonadCatch m => (a -> Bool) -> Parser m a b -> Parser m a b
670−sliceSepByP cond = D.toParserK . D.sliceSepByP cond . D.fromParserK
676+takeEndBy_ cond = D.toParserK . D.takeEndBy_ cond . D.fromParserK
671
677
672−-- | Like 'sliceSepBy' but does not drop the separator element, instead
678+-- | Take either the separator or the token. Separator is a Left value and
673−-- separator is emitted as a separate element in the output.
679+-- token is Right value.
674--
680--
675-- /Unimplemented/
681-- /Unimplemented/
676−{-# INLINE sliceSepWith #-}
682+{-# INLINE takeEitherSepBy #-}
677−sliceSepWith :: -- MonadCatch m =>
683+takeEitherSepBy :: -- MonadCatch m =>
678− (a -> Bool) -> Fold m a b -> Parser m a b
684+ (a -> Bool) -> Fold m (Either a b) c -> Parser m a c
679−sliceSepWith _cond = undefined -- D.toParserK . D.sliceSepBy cond
685+takeEitherSepBy _cond = undefined -- D.toParserK . D.takeEitherSepBy cond
680
686
681−-- | Collect stream elements until an elements passes the predicate, return the
687+-- | Parse a token that starts with an element chosen by the predicate. The
682−-- last element on which the predicate succeeded back to the input stream. If
688+-- parser fails if the input does not start with the selected element.
683−-- the predicate succeeds on the first element itself then the parser does not
684−-- terminate there. The succeeding element in the leading position
685−-- is treated as a prefix separator which is kept in the output segment.
686--
689--
687-- * Stops - when the predicate succeeds in non-leading position.
690-- * Stops - when the predicate succeeds in non-leading position.
688−-- * Fails - never.
691+-- * Fails - when the predicate fails in the leading position.
689--
692--
690−-- S.splitWithPrefix pred f = S.parseMany (PR.sliceBeginWith pred f)
693+-- >>> splitWithPrefix p f = Stream.parseMany (Parser.takeStartBy p f)
691--
694--
692-- Examples: -
695-- Examples: -
693--
696--
694−-- >>> :{
697+-- >>> p = Parser.takeStartBy (== ',') Fold.toList
695−-- sliceBeginWithOdd ls = Stream.parse prsr (Stream.fromList ls)
698+-- >>> leadingComma = Stream.parse p . Stream.fromList
696−-- where prsr = Parser.sliceBeginWith odd Fold.toList
699+-- >>> leadingComma "a,b,c,d"
697−-- :}
698−--
699−--
700−-- >>> sliceBeginWithOdd [2, 4, 6, 3]
701−-- *** Exception: sliceBeginWith : slice begins with an element which fails the predicate
700+-- *** Exception: takeStartBy : token begins with an element which fails the predicate
702-- ...
701-- ...
703−--
702+-- >>> leadingComma ",,"
704−-- >>> sliceBeginWithOdd [3, 5, 7, 4]
705−-- [3]
703+-- ","
706−--
704+-- >>> leadingComma ",a,b,c,d"
707−-- >>> sliceBeginWithOdd [3, 4, 6, 8, 5]
705+-- ",a"
708−-- [3,4,6,8]
706+-- >>> leadingComma ""
709−--
710−-- >>> sliceBeginWithOdd []
711−-- []
707+-- ""
712--
708--
713-- /Pre-release/
709-- /Pre-release/
714--
710--
715−{-# INLINE sliceBeginWith #-}
711+{-# INLINE takeStartBy #-}
716−sliceBeginWith ::
717− MonadCatch m =>
718− (a -> Bool) -> Fold m a b -> Parser m a b
712+takeStartBy :: MonadCatch m => (a -> Bool) -> Fold m a b -> Parser m a b
719−sliceBeginWith cond = D.toParserK . D.sliceBeginWith cond
713+takeStartBy cond = D.toParserK . D.takeStartBy cond
720
714
721-------------------------------------------------------------------------------
715-------------------------------------------------------------------------------
722-- Quoting and Escaping
716-- Quoting and Escaping
723-------------------------------------------------------------------------------
717-------------------------------------------------------------------------------
724
718
725−-- | Like 'sliceSepBy' but the separator elements can be escaped using an
719+-- | Like 'takeEndBy' but the separator elements can be escaped using an
726-- escape char determined by the second predicate.
720-- escape char determined by the second predicate.
727--
721--
728-- /Unimplemented/
722-- /Unimplemented/
729−{-# INLINE escapedSliceSepBy #-}
723+{-# INLINE takeEndByEsc #-}
730−escapedSliceSepBy :: -- MonadCatch m =>
724+takeEndByEsc :: -- MonadCatch m =>
731 (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
725 (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
732−escapedSliceSepBy _cond _esc = undefined
726+takeEndByEsc _cond _esc = undefined
733− -- D.toParserK . D.escapedSliceSepBy cond esc
727+ -- D.toParserK . D.takeEndByEsc cond esc
734
728
735−-- | @escapedFrameBy begin end escape@ parses a string framed using @begin@ and
729+-- | @takeFramedByEsc begin end escape@ parses a string framed using @begin@ and
736-- @end@ as the frame begin and end marker elements and @escape@ as an escaping
730-- @end@ as the frame begin and end marker elements and @escape@ as an escaping
737-- element to escape the occurrence of the framing elements within the frame.
731-- element to escape the occurrence of the framing elements within the frame.
738-- Nested frames are allowed, but nesting is removed when parsing.
732-- Nested frames are allowed, but nesting is removed when parsing.
@@ -740,26 +734,26 @@ escapedSliceSepBy _cond _esc = undefined
740-- For example,
734-- For example,
741--
735--
742-- @
736-- @
743−-- > Stream.parse (Parser.escapedFrameBy (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello}"
737+-- > Stream.parse (Parser.takeFramedByEsc (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello}"
744-- "hello"
738-- "hello"
745--
739--
746−-- > Stream.parse (Parser.escapedFrameBy (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello {world}}"
740+-- > Stream.parse (Parser.takeFramedByEsc (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello {world}}"
747-- "hello world"
741-- "hello world"
748--
742--
749−-- > Stream.parse (Parser.escapedFrameBy (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello \\{world\\}}"
743+-- > Stream.parse (Parser.takeFramedByEsc (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello \\{world\\}}"
750-- "hello {world}"
744-- "hello {world}"
751--
745--
752−-- > Stream.parse (Parser.escapedFrameBy (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello {world}"
746+-- > Stream.parse (Parser.takeFramedByEsc (== '{') (== '}') (== '\\') Fold.toList) $ Stream.fromList "{hello {world}"
753-- ParseError "Unterminated '{'"
747-- ParseError "Unterminated '{'"
754--
748--
755-- @
749-- @
756--
750--
757-- /Unimplemented/
751-- /Unimplemented/
758−{-# INLINE escapedFrameBy #-}
752+{-# INLINE takeFramedByEsc #-}
759−escapedFrameBy :: -- MonadCatch m =>
753+takeFramedByEsc :: -- MonadCatch m =>
760 (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
754 (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
761−escapedFrameBy _begin _end _escape _p = undefined
755+takeFramedByEsc _begin _end _escape _p = undefined
762− -- D.toParserK . D.frameBy begin end escape p
756+ -- D.toParserK . D.takeFramedByEsc begin end escape p
763
757
764-------------------------------------------------------------------------------
758-------------------------------------------------------------------------------
765-- Grouping and words
759-- Grouping and words
core/src/Streamly/Internal/Data/Parser/ParserD.hs+14−16
@@ -53,16 +53,16 @@ module Streamly.Internal.Data.Parser.ParserD
53 -- parsers but we can use Parsers instead of folds to make the composition
53 -- parsers but we can use Parsers instead of folds to make the composition
54 -- more powerful. For example, we can do:
54 -- more powerful. For example, we can do:
55 --
55 --
56− -- sliceSepByMax cond n p = sliceBy cond (take n p)
56+ -- takeEndByOrMax cond n p = takeEndBy cond (take n p)
57− -- sliceSepByBetween cond m n p = sliceBy cond (takeBetween m n p)
57+ -- takeEndByBetween cond m n p = takeEndBy cond (takeBetween m n p)
58 -- takeWhileBetween cond m n p = takeWhile cond (takeBetween m n p)
58 -- takeWhileBetween cond m n p = takeWhile cond (takeBetween m n p)
59
59
60 -- Grab a sequence of input elements without inspecting them
60 -- Grab a sequence of input elements without inspecting them
61 , takeBetween
61 , takeBetween
62 -- , take -- take -- takeBetween 0 n
62 -- , take -- take -- takeBetween 0 n
63− -- , takeLE1 -- take1 -- takeBetween 1 n
64 , takeEQ -- takeBetween n n
63 , takeEQ -- takeBetween n n
65 , takeGE -- takeBetween n maxBound
64 , takeGE -- takeBetween n maxBound
65+ -- , takeGE1 -- take1 -- takeBetween 1 n
66 , takeP
66 , takeP
67
67
68 -- Grab a sequence of input elements by inspecting them
68 -- Grab a sequence of input elements by inspecting them
@@ -72,10 +72,8 @@ module Streamly.Internal.Data.Parser.ParserD
72 , takeWhile1
72 , takeWhile1
73
73
74 -- Separators
74 -- Separators
75− , sliceSepByP
75+ , takeEndBy_
76− -- , sliceSepByBetween
76+ , takeStartBy
77− , sliceBeginWith
78− -- , sliceSepWith
79
77
80 -- Words and grouping
78 -- Words and grouping
81 , wordBy
79 , wordBy
@@ -670,13 +668,13 @@ takeWhile1 predicate (Fold fstep finitial fextract) =
670-- Separators
668-- Separators
671-------------------------------------------------------------------------------
669-------------------------------------------------------------------------------
672
670
673−-- | See 'Streamly.Internal.Data.Parser.sliceSepByP'.
671+-- | See 'Streamly.Internal.Data.Parser.takeEndBy_'.
674--
672--
675-- /Pre-release/
673-- /Pre-release/
676--
674--
677−sliceSepByP :: MonadCatch m =>
675+takeEndBy_ :: MonadCatch m =>
678 (a -> Bool) -> Parser m a b -> Parser m a b
676 (a -> Bool) -> Parser m a b -> Parser m a b
679−sliceSepByP cond (Parser pstep pinitial pextract) =
677+takeEndBy_ cond (Parser pstep pinitial pextract) =
680
678
681 Parser step initial pextract
679 Parser step initial pextract
682
680
@@ -691,15 +689,15 @@ sliceSepByP cond (Parser pstep pinitial pextract) =
691 return $ Done 0 res
689 return $ Done 0 res
692 else pstep s a
690 else pstep s a
693
691
694−-- | See 'Streamly.Internal.Data.Parser.sliceBeginWith'.
692+-- | See 'Streamly.Internal.Data.Parser.takeStartBy'.
695--
693--
696-- /Pre-release/
694-- /Pre-release/
697--
695--
698data SliceBeginWithState s = Left' s | Right' s
696data SliceBeginWithState s = Left' s | Right' s
699
697
700−{-# INLINE sliceBeginWith #-}
698+{-# INLINE takeStartBy #-}
701−sliceBeginWith :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
699+takeStartBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
702−sliceBeginWith cond (Fold fstep finitial fextract) =
700+takeStartBy cond (Fold fstep finitial fextract) =
703
701
704 Parser step initial extract
702 Parser step initial extract
705
703
@@ -710,7 +708,7 @@ sliceBeginWith cond (Fold fstep finitial fextract) =
710 return $
708 return $
711 case res of
709 case res of
712 FL.Partial s -> IPartial (Left' s)
710 FL.Partial s -> IPartial (Left' s)
713− FL.Done _ -> IError "sliceBeginWith : bad finitial"
711+ FL.Done _ -> IError "takeStartBy : bad finitial"
714
712
715 {-# INLINE process #-}
713 {-# INLINE process #-}
716 process s a = do
714 process s a = do
@@ -723,7 +721,7 @@ sliceBeginWith cond (Fold fstep finitial fextract) =
723 step (Left' s) a =
721 step (Left' s) a =
724 if cond a
722 if cond a
725 then process s a
723 then process s a
726− else error $ "sliceBeginWith : slice begins with an element which "
724+ else error $ "takeStartBy : token begins with an element which "
727 ++ "fails the predicate"
725 ++ "fails the predicate"
728 step (Right' s) a =
726 step (Right' s) a =
729 if not (cond a)
727 if not (cond a)
test/Streamly/Test/Data/Parser.hs+9−9
@@ -324,10 +324,10 @@ takeProperties =
324-- where
324-- where
325-- list_length = Prelude.length ls
325-- list_length = Prelude.length ls
326
326
327−sliceSepByP :: Property
327+takeEndBy_ :: Property
328−sliceSepByP =
328+takeEndBy_ =
329 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
329 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
330− case S.parse (P.sliceSepByP predicate prsr) (S.fromList ls) of
330+ case S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
331 Right parsed_list ->
331 Right parsed_list ->
332 checkListEqual parsed_list (tkwhl ls)
332 checkListEqual parsed_list (tkwhl ls)
333 Left _ -> property False
333 Left _ -> property False
@@ -336,8 +336,8 @@ sliceSepByP =
336 prsr = P.many (P.satisfy (const True)) FL.toList
336 prsr = P.many (P.satisfy (const True)) FL.toList
337 tkwhl ls = Prelude.takeWhile (not . predicate) ls
337 tkwhl ls = Prelude.takeWhile (not . predicate) ls
338
338
339−sliceBeginWith :: Property
339+takeStartBy :: Property
340−sliceBeginWith =
340+takeStartBy =
341 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
341 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
342 let ls1 = 1:ls
342 let ls1 = 1:ls
343 in
343 in
@@ -354,7 +354,7 @@ sliceBeginWith =
354 Left _ -> property False
354 Left _ -> property False
355 where
355 where
356 predicate = odd
356 predicate = odd
357− parser = P.sliceBeginWith predicate FL.toList
357+ parser = P.takeStartBy predicate FL.toList
358
358
359takeWhile :: Property
359takeWhile :: Property
360takeWhile =
360takeWhile =
@@ -779,9 +779,9 @@ main =
779 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
779 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
780 -- prop "Fail when stream length exceeded" lookAheadFail
780 -- prop "Fail when stream length exceeded" lookAheadFail
781 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
781 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
782− prop "P.sliceSepByP test" Main.sliceSepByP
782+ prop "P.takeEndBy_ test" Main.takeEndBy_
783− prop ("P.sliceBeginWith pred = head : Prelude.takeWhile (not . pred)"
783+ prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
784− ++ " tail") sliceBeginWith
784+ ++ " tail") takeStartBy
785 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
785 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
786 prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
786 prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
787 ++ " else check why failed") takeWhile1
787 ++ " else check why failed") takeWhile1