1{-# OPTIONS_GHC -Wno-orphans #-}
4-- Module : Streamly.Internal.Data.Parser
5-- Copyright : (c) 2020 Composewell Technologies
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : pre-release
11-- Fast backtracking parsers with stream fusion and native streaming
14-- 'Applicative' and 'Control.Applicative.Alternative' type class based
15-- combinators from the
16-- <http://hackage.haskell.org/package/parser-combinators parser-combinators>
17-- package can also be used with the 'Parser' type. However, there are two
18-- important differences between @parser-combinators@ and the equivalent ones
19-- provided in this module in terms of performance:
21-- 1) @parser-combinators@ use plain Haskell lists to collect the results, in a
22-- strict Monad like IO, the results are necessarily buffered before they can
23-- be consumed. This may not perform optimally in streaming applications
24-- processing large amounts of data. Equivalent combinators in this module can
25-- consume the results of parsing using a 'Fold', thus providing a scalability
26-- and a composable consumer.
28-- 2) Several combinators in this module can be many times faster because of
29-- stream fusion. For example, 'Streamly.Internal.Data.Parser.many' combinator
30-- in this module is much faster than the 'Control.Applicative.many' combinator
31-- of 'Control.Applicative.Alternative' type class.
35-- Failing parsers in this module throw the 'D.ParseError' exception.
39-- As far as possible, try that the names of the combinators in this module are
42-- * <https://hackage.haskell.org/package/base/docs/Text-ParserCombinators-ReadP.html base/Text.ParserCombinators.ReadP>
43-- * <http://hackage.haskell.org/package/parser-combinators parser-combinators>
44-- * <http://hackage.haskell.org/package/megaparsec megaparsec>
45-- * <http://hackage.haskell.org/package/attoparsec attoparsec>
46-- * <http://hackage.haskell.org/package/parsec parsec>
48module Streamly.Internal.Data.Parser
54 -- * Downgrade to Fold
57 -- First order parsers
77 -- All of these can be expressed in terms of either
88 -- * Sequence parsers (tokenizers)
90 -- | Parsers chained in series, if one parser terminates the composition
96 -- | Grab a sequence of input elements without inspecting them
98 -- , take -- takeBetween 0 n
99 , takeEQ -- takeBetween n n
100 , takeGE -- takeBetween n maxBound
103 -- Grab a sequence of input elements by inspecting them
129 , groupByRollingEither
135 , takeFramedByGeneric
142 -- , prefixOf -- match any prefix of a given string
143 -- , suffixOf -- match any suffix of a given string
144 -- , infixOf -- match any substring of a given string
147 -- Second order parsers (parsers using parsers)
148 -- * Binary Combinators
150 -- ** Sequential Applicative
155 -- ** Parallel Applicatives
159 -- , teeTill -- like manyTill but parallel
162 -- ** Sequential Interleaving
163 -- Use two folds, run a primary parser, its rejected values go to the
166 -- , deintercalatePrefix
167 -- , deintercalateSuffix
170 -- | TODO: traditional implmentations of these may be of limited use. For
171 -- example, consider parsing lines separated by @\\r\\n@. The main parser
172 -- will have to detect and exclude the sequence @\\r\\n@ anyway so that we
173 -- can apply the "sep" parser.
175 -- We can instead implement these as special cases of deintercalate.
187 -- ** Sequential Alternative
191 -- ** Parallel Alternatives
197 -- * N-ary Combinators
198 -- ** Sequential Collection
202 -- ** Sequential Repetition
215 -- | A simple and stupid impl would be to just convert the stream to an
216 -- array and give the array reference to all consumers. The array can be
217 -- grown on demand by any consumer and truncated when nonbody needs it.
219 -- ** Distribute to collection
220 -- ** Distribute to repetition
222 -- ** Interleaved collection
229 -- ** Collection of Alternatives
235 -- , fastestN -- first N successful in time
236 -- , choiceN -- first N successful in position
238 -- , choice -- first successful in position
240 -- ** Repeated Alternatives
250import Data.Functor (($>))
252 ( any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either
255import Streamly.Internal.Data.Fold.Type (Fold(..))
256import Streamly.Internal.Data.Parser.ParserK.Type (Parser)
257import Streamly.Internal.Data.Stream.Type (Stream)
259import qualified Streamly.Internal.Data.Fold.Type as FL
260import qualified Streamly.Internal.Data.Parser.ParserD as D
261import qualified Streamly.Internal.Data.Parser.ParserK.Type as K
262import qualified Streamly.Internal.Data.Stream.Type as Stream
267-- >>> :set -package streamly
268-- >>> import Prelude hiding (any, all, dropWhile, take, takeWhile, sequence, concatMap, maybe, either, filter)
269-- >>> import Control.Applicative ((<|>))
270-- >>> import Data.Char (isSpace)
271-- >>> import qualified Data.Maybe as Maybe
272-- >>> import qualified Streamly.Data.Stream as Stream
273-- >>> import qualified Streamly.Internal.Data.Stream as Stream (parse, parseMany)
274-- >>> import qualified Streamly.Internal.Data.Fold as Fold
275-- >>> import qualified Streamly.Internal.Data.Parser as Parser
277-------------------------------------------------------------------------------
278-- Downgrade a parser to a Fold
279-------------------------------------------------------------------------------
281-- | Make a 'Fold' from a 'Parser'. The fold just throws an exception if the
282-- parser fails or tries to backtrack.
284-- This can be useful in combinators that accept a Fold and we know that a
285-- Parser cannot fail or failure exception is acceptable as there is no way to
291toFold :: Monad m => Parser m a b -> Fold m a b
292toFold p = D.toFold $ D.fromParserK p
294-------------------------------------------------------------------------------
295-- Upgrade folds to parses
296-------------------------------------------------------------------------------
298-- | Make a 'Parser' from a 'Fold'.
302{-# INLINE fromFold #-}
303fromFold :: Monad m => Fold m a b -> Parser m a b
304fromFold = D.toParserK . D.fromFold
306-- | Convert a Maybe returning fold to an error returning parser. The first
307-- argument is the error message that the parser would return when the fold
312{-# INLINE fromFoldMaybe #-}
313fromFoldMaybe :: Monad m => String -> Fold m a (Maybe b) -> Parser m a b
314fromFoldMaybe err = D.toParserK . D.fromFoldMaybe err
316-------------------------------------------------------------------------------
317-- Terminating but not failing folds
318-------------------------------------------------------------------------------
320-- This is the dual of stream "fromPure".
322-- | A parser that always yields a pure value without consuming any input.
326{-# INLINE [3] fromPure #-}
327fromPure :: Monad m => b -> Parser m a b
328fromPure = D.toParserK . D.fromPure
329{-# RULES "fromPure fallback to CPS" [2]
330 forall a. D.toParserK (D.fromPure a) = K.fromPure a #-}
332-- This is the dual of stream "fromEffect".
334-- | A parser that always yields the result of an effectful action without
335-- consuming any input.
339{-# INLINE fromEffect #-}
340fromEffect :: Monad m => m b -> Parser m a b
341fromEffect = K.fromEffect -- D.toParserK . D.fromEffect
343-- This is the dual of "nil".
345-- | A parser that always fails with an error message without consuming
350{-# INLINE [3] die #-}
351die :: Monad m => String -> Parser m a b
352die = D.toParserK . D.die
353{-# RULES "die fallback to CPS" [2]
354 forall a. D.toParserK (D.die a) = K.die a #-}
356-- This is the dual of "nilM".
358-- | A parser that always fails with an effectful error message and without
359-- consuming any input.
364dieM :: Monad m => m String -> Parser m a b
365dieM = D.toParserK . D.dieM
367-------------------------------------------------------------------------------
369-------------------------------------------------------------------------------
371-- | @lmap f parser@ maps the function @f@ on the input of the parser.
373-- >>> Stream.parse (Parser.lmap (\x -> x * x) (Parser.fromFold Fold.sum)) (Stream.enumerateFromTo 1 100)
376-- > lmap = Parser.lmapM return
380lmap :: Monad m => (a -> b) -> Parser m b r -> Parser m a r
381lmap f p = D.toParserK $ D.lmap f $ D.fromParserK p
383-- | @lmapM f parser@ maps the monadic function @f@ on the input of the parser.
387lmapM :: Monad m => (a -> m b) -> Parser m b r -> Parser m a r
388lmapM f p = D.toParserK $ D.lmapM f $ D.fromParserK p
390-- | @rmapM f parser@ maps the monadic function @f@ on the output of the parser.
394rmapM :: Monad m => (b -> m c) -> Parser m a b -> Parser m a c
395rmapM f p = D.toParserK $ D.rmapM f $ D.fromParserK p
397-- | Include only those elements that pass a predicate.
399-- >>> Stream.parse (Parser.filter (> 5) (Parser.fromFold Fold.sum)) $ Stream.fromList [1..10]
404filter :: Monad m => (a -> Bool) -> Parser m a b -> Parser m a b
405filter f p = D.toParserK $ D.filter f $ D.fromParserK p
407-------------------------------------------------------------------------------
409-------------------------------------------------------------------------------
411-- | Peek the head element of a stream, without consuming it. Fails if it
412-- encounters end of input.
414-- >>> Stream.parse ((,) <$> Parser.peek <*> Parser.satisfy (> 0)) $ Stream.fromList [1]
418-- peek = lookAhead (satisfy True)
424peek :: Monad m => Parser m a a
425peek = D.toParserK D.peek
427-- | Succeeds if we are at the end of input, fails otherwise.
429-- >>> Stream.parse ((,) <$> Parser.satisfy (> 0) <*> Parser.eof) $ Stream.fromList [1]
435eof :: Monad m => Parser m a ()
436eof = D.toParserK D.eof
438-- | Returns the next element if it passes the predicate, fails otherwise.
440-- >>> Stream.parse (Parser.satisfy (== 1)) $ Stream.fromList [1,0,1]
443-- >>> toMaybe f x = if f x then Just x else Nothing
444-- >>> satisfy f = Parser.maybe (toMaybe f)
448{-# INLINE satisfy #-}
449satisfy :: Monad m => (a -> Bool) -> Parser m a a
450satisfy = D.toParserK . D.satisfy
452-- | Consume one element from the head of the stream. Fails if it encounters
455-- >>> one = Parser.satisfy $ const True
460one :: Monad m => Parser m a a
461one = satisfy $ const True
463-- | Match a specific element.
465-- >>> element x = Parser.satisfy (== x)
467{-# INLINE element #-}
468element :: (Monad m, Eq a) => a -> Parser m a a
469element x = satisfy (== x)
471-- | Match anything other than the supplied element.
473-- >>> except x = Parser.satisfy (/= x)
476except :: (Monad m, Eq a) => a -> Parser m a a
477except x = satisfy (/= x)
479-- | Match any one of the elements in the supplied list.
481-- >>> oneOf xs = Parser.satisfy (`elem` xs)
483-- When performance matters a pattern matching predicate could be more
484-- efficient than a list:
495-- GHC may use a binary search instead of linear search in the list.
496-- Alternatively, you can also use an array instead of list for storage and
500oneOf :: (Monad m, Eq a) => [a] -> Parser m a a
501oneOf xs = satisfy (`elem` xs)
503-- | See performance notes in 'oneOf'.
505-- >>> noneOf xs = Parser.satisfy (`notElem` xs)
508noneOf :: (Monad m, Eq a) => [a] -> Parser m a a
509noneOf xs = satisfy (`notElem` xs)
511-- | Return the next element of the input. Returns 'Nothing'
512-- on end of input. Also known as 'head'.
516{-# DEPRECATED next "Please use \"fromFold Fold.one\" instead" #-}
518next :: Monad m => Parser m a (Maybe a)
519next = D.toParserK D.next
521-- | Map a 'Maybe' returning function on the next element in the stream. The
522-- parser fails if the function returns 'Nothing' otherwise returns the 'Just'
525-- >>> toEither = Maybe.maybe (Left "maybe: predicate failed") Right
526-- >>> maybe f = Parser.either (toEither . f)
528-- >>> maybe f = Parser.fromFoldMaybe "maybe: predicate failed" (Fold.maybe f)
533maybe :: Monad m => (a -> Maybe b) -> Parser m a b
534maybe = D.toParserK . D.maybe
536-- | Map an 'Either' returning function on the next element in the stream. If
537-- the function returns 'Left err', the parser fails with the error message
538-- @err@ otherwise returns the 'Right' value.
543either :: Monad m => (a -> Either String b) -> Parser m a b
544either = D.toParserK . D.either
546-------------------------------------------------------------------------------
548-------------------------------------------------------------------------------
550-- | @takeBetween m n@ takes a minimum of @m@ and a maximum of @n@ input
551-- elements and folds them using the supplied fold.
553-- Stops after @n@ elements.
554-- Fails if the stream ends before @m@ elements could be taken.
560-- takeBetween' low high ls = Stream.parse prsr (Stream.fromList ls)
561-- where prsr = Parser.takeBetween low high Fold.toList
566-- >>> takeBetween' 2 4 [1, 2, 3, 4, 5]
569-- >>> takeBetween' 2 4 [1, 2]
572-- >>> takeBetween' 2 4 [1]
573-- *** Exception: ParseError "takeBetween: Expecting alteast 2 elements, got 1"
575-- >>> takeBetween' 0 0 [1, 2]
578-- >>> takeBetween' 0 1 []
581-- @takeBetween@ is the most general take operation, other take operations can
582-- be defined in terms of takeBetween. For example:
584-- >>> take n = Parser.takeBetween 0 n
585-- >>> takeEQ n = Parser.takeBetween n n
586-- >>> takeGE n = Parser.takeBetween n maxBound
590{-# INLINE takeBetween #-}
591takeBetween :: Monad m =>
592 Int -> Int -> Fold m a b -> Parser m a b
593takeBetween m n = D.toParserK . D.takeBetween m n
595-- | Stops after taking exactly @n@ input elements.
597-- * Stops - after consuming @n@ elements.
598-- * Fails - if the stream or the collecting fold ends before it can collect
599-- exactly @n@ elements.
601-- >>> Stream.parse (Parser.takeEQ 4 Fold.toList) $ Stream.fromList [1,0,1]
602-- *** Exception: ParseError "takeEQ: Expecting exactly 4 elements, input terminated on 3"
607takeEQ :: Monad m => Int -> Fold m a b -> Parser m a b
608takeEQ n = D.toParserK . D.takeEQ n
610-- | Take at least @n@ input elements, but can collect more.
612-- * Stops - when the collecting fold stops.
613-- * Fails - if the stream or the collecting fold ends before producing @n@
616-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1]
617-- *** Exception: ParseError "takeGE: Expecting at least 4 elements, input terminated on 3"
619-- >>> Stream.parse (Parser.takeGE 4 Fold.toList) $ Stream.fromList [1,0,1,0,1]
625takeGE :: Monad m => Int -> Fold m a b -> Parser m a b
626takeGE n = D.toParserK . D.takeGE n
628-------------------------------------------------------------------------------
629-- Take until a condition
630-------------------------------------------------------------------------------
633-- Note: This is called @takeWhileP@ and @munch@ in some parser libraries.
635-- XXX We should perhaps use only takeWhileP and rename it to takeWhile.
637-- | Like 'takeWhile' but uses a 'Parser' instead of a 'Fold' to collect the
638-- input. The combinator stops when the condition fails or if the collecting
641-- Other interesting parsers can be implemented in terms of this parser:
643-- >>> takeWhile1 cond p = Parser.takeWhileP cond (Parser.takeBetween 1 maxBound p)
644-- >>> takeWhileBetween cond m n p = Parser.takeWhileP cond (Parser.takeBetween m n p)
646-- Stops: when the condition fails or the collecting parser stops.
647-- Fails: when the collecting parser fails.
651{-# INLINE takeWhileP #-}
652takeWhileP :: Monad m => (a -> Bool) -> Parser m a b -> Parser m a b
653takeWhileP cond p = D.toParserK $ D.takeWhileP cond (D.fromParserK p)
655-- | Collect stream elements until an element fails the predicate. The element
656-- on which the predicate fails is returned back to the input stream.
658-- * Stops - when the predicate fails or the collecting fold stops.
661-- >>> Stream.parse (Parser.takeWhile (== 0) Fold.toList) $ Stream.fromList [0,0,1,0,1]
664-- >>> takeWhile cond f = Parser.takeWhileP cond (Parser.fromFold f)
666-- We can implement a @breakOn@ using 'takeWhile':
669-- breakOn p = takeWhile (not p)
674{-# INLINE takeWhile #-}
675takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
676takeWhile cond = D.toParserK . D.takeWhile cond
677-- takeWhile cond f = takeWhileP cond (fromFold f)
679-- | Like 'takeWhile' but takes at least one element otherwise fails.
681-- >>> takeWhile1 cond p = Parser.takeWhileP cond (Parser.takeBetween 1 maxBound p)
685{-# INLINE takeWhile1 #-}
686takeWhile1 :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
687takeWhile1 cond = D.toParserK . D.takeWhile1 cond
688-- takeWhile1 cond f = takeWhileP cond (takeBetween 1 maxBound f)
690-- | Drain the input as long as the predicate succeeds, running the effects and
691-- discarding the results.
693-- This is also called @skipWhile@ in some parsing libraries.
695-- >>> dropWhile p = Parser.takeWhile p Fold.drain
699{-# INLINE dropWhile #-}
700dropWhile :: Monad m => (a -> Bool) -> Parser m a ()
701dropWhile p = takeWhile p FL.drain
703-------------------------------------------------------------------------------
705-------------------------------------------------------------------------------
707-- XXX We can remove Maybe from esc
708{-# INLINE takeFramedByGeneric #-}
709takeFramedByGeneric :: Monad m =>
715takeFramedByGeneric esc begin end f =
716 D.toParserK $ D.takeFramedByGeneric esc begin end f
718-- | @takeEndBy cond parser@ parses a token that ends by a separator chosen by
719-- the supplied predicate. The separator is also taken with the token.
721-- This can be combined with other parsers to implement other interesting
722-- parsers as follows:
724-- >>> takeEndByLE cond n p = Parser.takeEndBy cond (Parser.fromFold $ Fold.take n p)
725-- >>> takeEndByBetween cond m n p = Parser.takeEndBy cond (Parser.takeBetween m n p)
727-- >>> takeEndBy = Parser.takeEndByEsc (const False)
729-- See also "Streamly.Data.Fold.takeEndBy". Unlike the fold, the collecting
730-- parser in the takeEndBy parser can decide whether to fail or not if the
731-- stream does not end with separator.
735{-# INLINE takeEndBy #-}
736takeEndBy :: Monad m => (a -> Bool) -> Parser m a b -> Parser m a b
737takeEndBy cond = D.toParserK . D.takeEndBy cond . D.fromParserK
738-- takeEndBy = takeEndByEsc (const False)
740-- | Like 'takeEndBy' but the separator is dropped.
742-- See also "Streamly.Data.Fold.takeEndBy_".
746{-# INLINE takeEndBy_ #-}
747takeEndBy_ :: Monad m => (a -> Bool) -> Parser m a b -> Parser m a b
748takeEndBy_ cond = D.toParserK . D.takeEndBy_ cond . D.fromParserK
751 takeFramedByGeneric Nothing Nothing (Just isEnd) (toFold p)
754-- | Take either the separator or the token. Separator is a Left value and
755-- token is Right value.
758{-# INLINE takeEitherSepBy #-}
759takeEitherSepBy :: -- Monad m =>
760 (a -> Bool) -> Fold m (Either a b) c -> Parser m a c
761takeEitherSepBy _cond = undefined -- D.toParserK . D.takeEitherSepBy cond
763-- | Parse a token that starts with an element chosen by the predicate. The
764-- parser fails if the input does not start with the selected element.
766-- * Stops - when the predicate succeeds in non-leading position.
767-- * Fails - when the predicate fails in the leading position.
769-- >>> splitWithPrefix p f = Stream.parseMany (Parser.takeStartBy p f)
773-- >>> p = Parser.takeStartBy (== ',') Fold.toList
774-- >>> leadingComma = Stream.parse p . Stream.fromList
775-- >>> leadingComma "a,b"
776-- *** Exception: ParseError "takeStartBy: missing frame start"
778-- >>> leadingComma ",,"
780-- >>> leadingComma ",a,b"
782-- >>> leadingComma ""
787{-# INLINE takeStartBy #-}
788takeStartBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
789takeStartBy cond = D.toParserK . D.takeStartBy cond
791-- | Like 'takeStartBy' but drops the separator.
793-- >>> takeStartBy_ isBegin = Parser.takeFramedByGeneric Nothing (Just isBegin) Nothing
795{-# INLINE takeStartBy_ #-}
796takeStartBy_ :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
797takeStartBy_ isBegin = takeFramedByGeneric Nothing (Just isBegin) Nothing
799-------------------------------------------------------------------------------
800-- Quoting and Escaping
801-------------------------------------------------------------------------------
803-- | Like 'takeEndBy' but the separator elements can be escaped using an
804-- escape char determined by the first predicate. The escape characters are
808{-# INLINE takeEndByEsc #-}
809takeEndByEsc :: Monad m =>
810 (a -> Bool) -> (a -> Bool) -> Parser m a b -> Parser m a b
811takeEndByEsc isEsc isEnd p =
812 D.toParserK $ D.takeEndByEsc isEsc isEnd (D.fromParserK p)
814-- | @takeFramedByEsc_ isEsc isBegin isEnd fold@ parses a token framed using a
815-- begin and end predicate, and an escape character. The frame begin and end
816-- characters lose their special meaning if preceded by the escape character.
818-- Nested frames are allowed if begin and end markers are different, nested
819-- frames must be balanced unless escaped, nested frame markers are emitted as
824-- >>> p = Parser.takeFramedByEsc_ (== '\\') (== '{') (== '}') Fold.toList
825-- >>> Stream.parse p $ Stream.fromList "{hello}"
827-- >>> Stream.parse p $ Stream.fromList "{hello {world}}"
829-- >>> Stream.parse p $ Stream.fromList "{hello \\{world}"
831-- >>> Stream.parse p $ Stream.fromList "{hello {world}"
832-- *** Exception: ParseError "takeFramedByEsc_: missing frame end"
835{-# INLINE takeFramedByEsc_ #-}
836takeFramedByEsc_ :: Monad m =>
837 (a -> Bool) -> (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
838takeFramedByEsc_ isEsc isBegin isEnd f =
839 D.toParserK $ D.takeFramedByEsc_ isEsc isBegin isEnd f
840-- takeEndByEsc_ isEsc isEnd p =
841-- takeFramedByGeneric (Just isEsc) Nothing (Just isEnd) (toFold p)
843-- | @takeFramedBy_ isBegin isEnd fold@ parses a token framed by a begin and an
846-- >>> takeFramedBy_ = Parser.takeFramedByEsc_ (const False)
848{-# INLINE takeFramedBy_ #-}
849takeFramedBy_ :: Monad m =>
850 (a -> Bool) -> (a -> Bool) -> Fold m a b -> Parser m a b
851takeFramedBy_ isBegin isEnd f = D.toParserK $ D.takeFramedBy_ isBegin isEnd f
852-- takeFramedBy_ isBegin isEnd =
853-- takeFramedByGeneric (Just (const False)) (Just isBegin) (Just isEnd)
855-------------------------------------------------------------------------------
857-------------------------------------------------------------------------------
859-- Note we can also get words using something like:
860-- sepBy FL.toList (takeWhile (not . p) Fold.toList) (dropWhile p)
862-- But that won't be as efficient and ergonomic.
864-- | Like 'splitOn' but strips leading, trailing, and repeated separators.
865-- Therefore, @".a..b."@ having '.' as the separator would be parsed as
866-- @["a","b"]@. In other words, its like parsing words from whitespace
869-- * Stops - when it finds a word separator after a non-word element
872-- >>> wordBy = Parser.wordFramedBy (const False) (const False) (const False)
875-- S.wordsBy pred f = S.parseMany (PR.wordBy pred f)
879wordBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
880wordBy f = D.toParserK . D.wordBy f
882-- | Like 'wordBy' but treats anything inside a pair of quotes as a single
883-- word, the quotes can be escaped by an escape character. Recursive quotes
884-- are possible if quote begin and end characters are different, quotes must be
885-- balanced. Outermost quotes are stripped.
887-- >>> braces = Parser.wordFramedBy (== '\\') (== '{') (== '}') isSpace Fold.toList
888-- >>> Stream.parse braces $ Stream.fromList "{ab} cd"
890-- >>> Stream.parse braces $ Stream.fromList "{ab}{cd}"
892-- >>> Stream.parse braces $ Stream.fromList "a{b} cd"
894-- >>> Stream.parse braces $ Stream.fromList "a{{b}} cd"
897-- >>> quotes = Parser.wordFramedBy (== '\\') (== '"') (== '"') isSpace Fold.toList
898-- >>> Stream.parse quotes $ Stream.fromList "\"a\"\"b\""
902{-# INLINE wordFramedBy #-}
903wordFramedBy :: Monad m =>
904 (a -> Bool) -- ^ Escape
905 -> (a -> Bool) -- ^ left quote
906 -> (a -> Bool) -- ^ right quote
907 -> (a -> Bool) -- ^ word seperator
910wordFramedBy isEsc isBegin isEnd isSpc =
911 D.toParserK . D.wordFramedBy isEsc isBegin isEnd isSpc
913-- | Like 'wordFramedBy' but the closing quote is determined by the opening
914-- quote. The first quote begin starts a quote that is closed by its
915-- corresponding closing quote.
917-- 'wordFramedBy' and 'wordQuotedBy' both allow multiple quote characters based
918-- on the predicates but 'wordQuotedBy' always fixes the quote at the first
919-- occurrence and then it is closed only by the corresponding closing quote.
920-- Therefore, other quoting characters can be embedded inside it as normal
921-- characters. On the other hand, 'wordFramedBy' would close the quote as soon
922-- as it encounters any of the closing quotes.
924-- >>> q = (`elem` ['"', '\''])
925-- >>> p kQ = Parser.wordQuotedBy kQ (== '\\') q q id isSpace Fold.toList
927-- >>> Stream.parse (p False) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
930-- >>> Stream.parse (p True) $ Stream.fromList "a\"b'c\";'d\"e'f ghi"
933{-# INLINE wordQuotedBy #-}
934wordQuotedBy :: (Monad m, Eq a) =>
935 Bool -- ^ keep the quotes in the output
936 -> (a -> Bool) -- ^ Escape
937 -> (a -> Bool) -- ^ left quote
938 -> (a -> Bool) -- ^ right quote
939 -> (a -> a) -- ^ get right quote from left quote
940 -> (a -> Bool) -- ^ word seperator
943wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSpc =
944 D.toParserK . D.wordQuotedBy keepQuotes isEsc isBegin isEnd toRight isSpc
946-- | Given an input stream @[a,b,c,...]@ and a comparison function @cmp@, the
947-- parser assigns the element @a@ to the first group, then if @a \`cmp` b@ is
948-- 'True' @b@ is also assigned to the same group. If @a \`cmp` c@ is 'True'
949-- then @c@ is also assigned to the same group and so on. When the comparison
950-- fails the parser is terminated. Each group is folded using the 'Fold' @f@ and
951-- the result of the fold is the result of the parser.
953-- * Stops - when the comparison fails.
958-- Stream.fold Fold.toList
959-- . Stream.parseMany (Parser.groupBy eq Fold.toList)
963-- >>> runGroupsBy (<) []
966-- >>> runGroupsBy (<) [1]
969-- >>> runGroupsBy (<) [3, 5, 4, 1, 2, 0]
970-- [[3,5,4],[1,2],[0]]
974{-# INLINE groupBy #-}
975groupBy :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
976groupBy eq = D.toParserK . D.groupBy eq
978-- | Unlike 'groupBy' this combinator performs a rolling comparison of two
979-- successive elements in the input stream. Assuming the input stream
980-- is @[a,b,c,...]@ and the comparison function is @cmp@, the parser
981-- first assigns the element @a@ to the first group, then if @a \`cmp` b@ is
982-- 'True' @b@ is also assigned to the same group. If @b \`cmp` c@ is 'True'
983-- then @c@ is also assigned to the same group and so on. When the comparison
984-- fails the parser is terminated. Each group is folded using the 'Fold' @f@ and
985-- the result of the fold is the result of the parser.
987-- * Stops - when the comparison fails.
991-- runGroupsByRolling eq =
992-- Stream.fold Fold.toList
993-- . Stream.parseMany (Parser.groupByRolling eq Fold.toList)
997-- >>> runGroupsByRolling (<) []
1000-- >>> runGroupsByRolling (<) [1]
1003-- >>> runGroupsByRolling (<) [3, 5, 4, 1, 2, 0]
1004-- [[3,5],[4],[1,2],[0]]
1008{-# INLINE groupByRolling #-}
1009groupByRolling :: Monad m => (a -> a -> Bool) -> Fold m a b -> Parser m a b
1010groupByRolling eq = D.toParserK . D.groupByRolling eq
1012-- | Like 'groupByRolling', but if the predicate is 'True' then collects using
1013-- the first fold as long as the predicate holds 'True', if the predicate is
1014-- 'False' collects using the second fold as long as it remains 'False'.
1015-- Returns 'Left' for the first case and 'Right' for the second case.
1017-- For example, if we want to detect sorted sequences in a stream, both
1018-- ascending and descending cases we can use 'groupByRollingEither (<=)
1019-- Fold.toList Fold.toList'.
1022{-# INLINE groupByRollingEither #-}
1023groupByRollingEither :: Monad m =>
1024 (a -> a -> Bool) -> Fold m a b -> Fold m a c -> Parser m a (Either b c)
1025groupByRollingEither eq f1 = D.toParserK . D.groupByRollingEither eq f1
1027-- XXX Use a stream instead of a list so that we can use any container type.
1029-- | Match the given sequence of elements using the given comparison function.
1031-- >>> Stream.parse (Parser.eqBy (==) "string") $ Stream.fromList "string"
1033-- >>> Stream.parse (Parser.eqBy (==) "mismatch") $ Stream.fromList "match"
1034-- *** Exception: ParseError "eqBy: failed, yet to match 7 elements"
1039eqBy :: Monad m => (a -> a -> Bool) -> [a] -> Parser m a ()
1040eqBy cmp = D.toParserK . D.eqBy cmp
1042-- | Match the input sequence with the supplied list and return it if
1047list :: (Monad m, Eq a) => [a] -> Parser m a [a]
1048list xs = eqBy (==) xs $> xs
1050-------------------------------------------------------------------------------
1052-------------------------------------------------------------------------------
1054-- | Sequential parser application. Apply two parsers sequentially to an input
1055-- stream. The input is provided to the first parser, when it is done the
1056-- remaining input is provided to the second parser. If both the parsers
1057-- succeed their outputs are combined using the supplied function. The
1058-- operation fails if any of the parsers fail.
1060-- Note: This is a parsing dual of appending streams using
1061-- 'Streamly.Prelude.serial', it splits the streams using two parsers and zips
1064-- This implementation is strict in the second argument, therefore, the
1065-- following will fail:
1067-- >>> Stream.parse (Parser.serialWith const (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
1068-- *** Exception: Prelude.undefined
1071-- Compare with 'Applicative' instance method '<*>'. This implementation allows
1072-- stream fusion but has quadratic complexity. This can fuse with other
1073-- operations and can be faster than 'Applicative' instance for small number
1074-- (less than 8) of compositions.
1076-- Many combinators can be expressed using @serialWith@ and other parser
1077-- primitives. Some common idioms are described below,
1080-- span :: (a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
1081-- span pred f1 f2 = serialWith (,) ('takeWhile' pred f1) ('fromFold' f2)
1085-- spanBy :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
1086-- spanBy eq f1 f2 = serialWith (,) ('groupBy' eq f1) ('fromFold' f2)
1090-- spanByRolling :: (a -> a -> Bool) -> Fold m a b -> Fold m a b -> Parser m a b
1091-- spanByRolling eq f1 f2 = serialWith (,) ('groupByRolling' eq f1) ('fromFold' f2)
1096{-# INLINE serialWith #-}
1097serialWith :: Monad m
1098 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
1100 D.toParserK $ D.serialWith f (D.fromParserK p1) (D.fromParserK p2)
1102-- | Sequential parser application ignoring the output of the first parser.
1103-- Apply two parsers sequentially to an input stream. The input is provided to
1104-- the first parser, when it is done the remaining input is provided to the
1105-- second parser. The output of the parser is the output of the second parser.
1106-- The operation fails if any of the parsers fail.
1108-- This implementation is strict in the second argument, therefore, the
1109-- following will fail:
1111-- >>> Stream.parse (Parser.split_ (Parser.satisfy (> 0)) undefined) $ Stream.fromList [1]
1112-- *** Exception: Prelude.undefined
1115-- Compare with 'Applicative' instance method '*>'. This implementation allows
1116-- stream fusion but has quadratic complexity. This can fuse with other
1117-- operations, and can be faster than 'Applicative' instance for small
1118-- number (less than 8) of compositions.
1122{-# INLINE split_ #-}
1123split_ :: Monad m => Parser m x a -> Parser m x b -> Parser m x b
1124split_ p1 p2 = D.toParserK $ D.split_ (D.fromParserK p1) (D.fromParserK p2)
1127-- | @teeWith f p1 p2@ distributes its input to both @p1@ and @p2@ until both
1128-- of them succeed or anyone of them fails and combines their output using @f@.
1129-- The parser succeeds if both the parsers succeed.
1133{-# INLINE teeWith #-}
1135 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
1137 D.toParserK $ D.teeWith f (D.fromParserK p1) (D.fromParserK p2)
1139-- | Like 'teeWith' but ends parsing and zips the results, if available,
1140-- whenever the first parser ends.
1144{-# INLINE teeWithFst #-}
1145teeWithFst :: Monad m
1146 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
1148 D.toParserK $ D.teeWithFst f (D.fromParserK p1) (D.fromParserK p2)
1150-- | Like 'teeWith' but ends parsing and zips the results, if available,
1151-- whenever any of the parsers ends or fails.
1155{-# INLINE teeWithMin #-}
1156teeWithMin :: Monad m
1157 => (a -> b -> c) -> Parser m x a -> Parser m x b -> Parser m x c
1159 D.toParserK $ D.teeWithMin f (D.fromParserK p1) (D.fromParserK p2)
1162-- | Sequential alternative. Apply the input to the first parser and return the
1163-- result if the parser succeeds. If the first parser fails then backtrack and
1164-- apply the same input to the second parser and return the result.
1166-- Note: This implementation is not lazy in the second argument. The following
1169-- >>> Stream.parse (Parser.satisfy (> 0) `Parser.alt` undefined) $ Stream.fromList [1..10]
1172-- Compare with 'Alternative' instance method '<|>'. This implementation allows
1173-- stream fusion but has quadratic complexity. This can fuse with other
1174-- operations and can be much faster than 'Alternative' instance for small
1175-- number (less than 8) of alternatives.
1180alt :: Monad m => Parser m x a -> Parser m x a -> Parser m x a
1181alt p1 p2 = D.toParserK $ D.alt (D.fromParserK p1) (D.fromParserK p2)
1184-- | Shortest alternative. Apply both parsers in parallel but choose the result
1185-- from the one which consumed least input i.e. take the shortest succeeding
1190{-# INLINE shortest #-}
1192 => Parser m x a -> Parser m x a -> Parser m x a
1193shortest p1 p2 = D.toParserK $ D.shortest (D.fromParserK p1) (D.fromParserK p2)
1195-- | Longest alternative. Apply both parsers in parallel but choose the result
1196-- from the one which consumed more input i.e. take the longest succeeding
1201{-# INLINE longest #-}
1203 => Parser m x a -> Parser m x a -> Parser m x a
1204longest p1 p2 = D.toParserK $ D.longest (D.fromParserK p1) (D.fromParserK p2)
1207-- | Run a parser without consuming the input.
1211{-# INLINE lookAhead #-}
1212lookAhead :: Monad m => Parser m a b -> Parser m a b
1213lookAhead p = D.toParserK $ D.lookAhead $ D.fromParserK p
1215-- | Takes at-most @n@ input elements.
1217-- * Stops - when the collecting parser stops.
1218-- * Fails - when the collecting parser fails.
1220-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 2 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1223-- >>> Stream.parse (Parser.takeP 4 (Parser.takeEQ 5 Fold.toList)) $ Stream.fromList [1, 2, 3, 4, 5]
1224-- *** Exception: ParseError "takeEQ: Expecting exactly 5 elements, input terminated on 4"
1228takeP :: Monad m => Int -> Parser m a b -> Parser m a b
1229takeP i p = D.toParserK $ D.takeP i $ D.fromParserK p
1231-------------------------------------------------------------------------------
1232-- Sequential Collection
1233-------------------------------------------------------------------------------
1235-- | @concatSequence f p@ collects sequential parses of parsers in a
1236-- serial stream @p@ using the fold @f@. Fails if the input ends or any
1237-- of the parsers fail.
1239-- An even more efficient implementation can use ParserD type Parser in
1244{-# INLINE concatSequence #-}
1247 Fold m b c -> Stream m (Parser m a b) -> Parser m a c
1249 let sp = fmap D.fromParserK $ Stream.toStreamD p
1250 in D.toParserK $ D.sequence f sp
1252-- | Map a 'Parser' returning function on the result of a 'Parser'.
1254-- Compare with 'Monad' instance method '>>='. This implementation allows
1255-- stream fusion but has quadratic complexity. This can fuse with other
1256-- operations and can be much faster than 'Monad' instance for small number
1257-- (less than 8) of compositions.
1261{-# INLINE concatMap #-}
1263 => (b -> Parser m a c) -> Parser m a b -> Parser m a c
1264concatMap f p = D.toParserK $ D.concatMap (D.fromParserK . f) (D.fromParserK p)
1267-------------------------------------------------------------------------------
1268-- Alternative Collection
1269-------------------------------------------------------------------------------
1271-- | @choice parsers@ applies the @parsers@ in order and returns the first
1274-- This is same as 'asum' but more efficient.
1278{-# INLINE choice #-}
1280 (Functor t, Foldable t, Monad m) => t (Parser m a b) -> Parser m a b
1281choice ps = D.toParserK $ D.choice $ D.fromParserK <$> ps
1284-------------------------------------------------------------------------------
1285-- Sequential Repetition
1286-------------------------------------------------------------------------------
1288-- | Like 'many' but uses a 'Parser' instead of a 'Fold' to collect the
1289-- results. Parsing stops or fails if the collecting parser stops or fails.
1294manyP :: -- Monad m =>
1295 Parser m a b -> Parser m b c -> Parser m a c
1296manyP _p _f = undefined -- D.toParserK $ D.manyP (D.fromParserK p) f
1298-- | Collect zero or more parses. Apply the supplied parser repeatedly on the
1299-- input stream and push the parse results to a downstream fold.
1301-- Stops: when the downstream fold stops or the parser fails.
1302-- Fails: never, produces zero or more results.
1304-- >>> many = Parser.countBetween 0 maxBound
1306-- Compare with 'Control.Applicative.many'.
1311many :: Monad m => Parser m a b -> Fold m b c -> Parser m a c
1312many p f = D.toParserK $ D.many (D.fromParserK p) f
1314-- Note: many1 would perhaps be a better name for this and consistent with
1315-- other names like takeWhile1. But we retain the name "some" for
1318-- | Collect one or more parses. Apply the supplied parser repeatedly on the
1319-- input stream and push the parse results to a downstream fold.
1321-- Stops: when the downstream fold stops or the parser fails.
1322-- Fails: if it stops without producing a single result.
1324-- >>> some p f = Parser.manyP p (Parser.takeGE 1 f)
1325-- >>> some = Parser.countBetween 1 maxBound
1327-- Compare with 'Control.Applicative.some'.
1332some :: Monad m => Parser m a b -> Fold m b c -> Parser m a c
1333some p f = D.toParserK $ D.some (D.fromParserK p) f
1334-- some p f = manyP p (takeGE 1 f)
1335-- some = countBetween 1 maxBound
1337-- | @countBetween m n f p@ collects between @m@ and @n@ sequential parses of
1338-- parser @p@ using the fold @f@. Stop after collecting @n@ results. Fails if
1339-- the input ends or the parser fails before @m@ results are collected.
1341-- >>> countBetween m n p f = Parser.manyP p (Parser.takeBetween m n f)
1345{-# INLINE countBetween #-}
1346countBetween :: -- Monad m =>
1347 Int -> Int -> Parser m a b -> Fold m b c -> Parser m a c
1348countBetween _m _n _p = undefined
1349-- countBetween m n p f = manyP p (takeBetween m n f)
1351-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
1352-- the fold @f@. Fails if the input ends or the parser fails before @n@
1353-- results are collected.
1355-- >>> count n = Parser.countBetween n n
1356-- >>> count n p f = Parser.manyP p (Parser.takeEQ n f)
1361count :: -- Monad m =>
1362 Int -> Parser m a b -> Fold m b c -> Parser m a c
1363count n = countBetween n n
1364-- count n p f = manyP p (takeEQ n f)
1366-- | Like 'manyTill' but uses a 'Parser' to collect the results instead of a
1367-- 'Fold'. Parsing stops or fails if the collecting parser stops or fails.
1369-- We can implemnent parsers like the following using 'manyTillP':
1372-- countBetweenTill m n f p = manyTillP (takeBetween m n f) p
1377{-# INLINE manyTillP #-}
1378manyTillP :: -- Monad m =>
1379 Parser m a b -> Parser m a x -> Parser m b c -> Parser m a c
1380manyTillP _p1 _p2 _f = undefined
1381 -- D.toParserK $ D.manyTillP (D.fromParserK p1) (D.fromParserK p2) f
1383-- | @manyTill f test collect@ tries the parser @test@ on the input, if @test@
1384-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
1385-- tried again and so on. The parser stops when @test@ succeeds. The output of
1386-- @test@ is discarded and the output of @collect@ is accumulated by the
1387-- supplied fold. The parser fails if @collect@ fails.
1389-- Stops when the fold @f@ stops.
1393{-# INLINE manyTill #-}
1395 => Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1397 D.toParserK $ D.manyTill f (D.fromParserK p1) (D.fromParserK p2)
1399-- | @manyThen f collect recover@ repeats the parser @collect@ on the input and
1400-- collects the output in the supplied fold. If the the parser @collect@ fails,
1401-- parser @recover@ is run until it stops and then we start repeating the
1402-- parser @collect@ again. The parser fails if the recovery parser fails.
1404-- For example, this can be used to find a key frame in a video stream after an
1409{-# INLINE manyThen #-}
1410manyThen :: -- (Foldable t, Monad m) =>
1411 Parser m a b -> Parser m a x -> Fold m b c -> Parser m a c
1412manyThen _parser _recover _f = undefined
1414-------------------------------------------------------------------------------
1416-------------------------------------------------------------------------------
1418-- To deinterleave we can chain two parsers one behind the other. The input is
1419-- given to the first parser and the input definitively rejected by the first
1420-- parser is given to the second parser.
1422-- We can either have the parsers themselves buffer the input or use the shared
1423-- global buffer to hold it until none of the parsers need it. When the first
1424-- parser returns Skip (i.e. rewind) we let the second parser consume the
1425-- rejected input and when it is done we move the cursor forward to the first
1426-- parser again. This will require a "move forward" command as well.
1428-- To implement grep we can use three parsers, one to find the pattern, one
1429-- to store the context behind the pattern and one to store the context in
1430-- front of the pattern. When a match occurs we need to emit the accumulator of
1431-- all the three parsers. One parser can count the line numbers to provide the
1434-- XXX rename this to intercalate
1435-- | Apply two parsers alternately to an input stream. The input stream is
1436-- considered an interleaving of two patterns. The two parsers represent the
1439-- This undoes a "gintercalate" of two streams.
1443{-# INLINE deintercalate #-}
1444deintercalate :: Monad m =>
1445 Fold m (Either x y) z
1449deintercalate sink contentL contentR =
1452 sink (D.fromParserK contentL) (D.fromParserK contentR)
1454-- | Parse items separated by a separator parsed by the supplied parser. At
1455-- least one item must be present for the parser to succeed.
1457-- Note that this can go in infinite loop if both the parsers fail on some
1458-- input. Detection of that would make the implementation more complex.
1460{-# INLINE sepBy1 #-}
1462 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1463sepBy1 sink p sep = do
1465 f <- fromEffect $ FL.initialize sink
1466 f1 <- fromEffect $ FL.snoc f x
1469-- | Run the content parser first, when it is done, the separator parser is
1470-- run, when it is done content parser is run again and so on. If none of the
1471-- parsers consumes an input then parser returns a failure.
1473-- >>> sepBy sink = Parser.deintercalate (Fold.lefts sink)
1474-- >>> sepBy sink content sep = Parser.sepBy1 sink content sep <|> return mempty
1478 Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
1479sepBy sink content sep =
1480 D.toParserK $ D.sepBy sink (D.fromParserK content) (D.fromParserK sep)
1481-- sepBy sink = deintercalate (FL.lefts sink)
1483-------------------------------------------------------------------------------
1484-- Interleaving a collection of parsers
1485-------------------------------------------------------------------------------
1487-- | Apply a collection of parsers to an input stream in a round robin fashion.
1488-- Each parser is applied until it stops and then we repeat starting with the
1489-- the first parser again.
1493{-# INLINE roundRobin #-}
1494roundRobin :: -- (Foldable t, Monad m) =>
1495 t (Parser m a b) -> Fold m b c -> Parser m a c
1496roundRobin _ps _f = undefined
1498-------------------------------------------------------------------------------
1499-- Repeated Alternatives
1500-------------------------------------------------------------------------------
1502-- | Keep trying a parser up to a maximum of @n@ failures. When the parser
1503-- fails the input consumed till now is dropped and the new instance is tried
1504-- on the fresh input.
1508{-# INLINE retryMaxTotal #-}
1509retryMaxTotal :: -- (Monad m) =>
1510 Int -> Parser m a b -> Fold m b c -> Parser m a c
1511retryMaxTotal _n _p _f = undefined
1513-- | Like 'retryMaxTotal' but aborts after @n@ successive failures.
1517{-# INLINE retryMaxSuccessive #-}
1518retryMaxSuccessive :: -- (Monad m) =>
1519 Int -> Parser m a b -> Fold m b c -> Parser m a c
1520retryMaxSuccessive _n _p _f = undefined
1522-- | Keep trying a parser until it succeeds. When the parser fails the input
1523-- consumed till now is dropped and the new instance is tried on the fresh
1529retry :: -- (Monad m) =>
1530 Parser m a b -> Parser m a b
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list