{-# LANGUAGE CPP                       #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module      : Streamly.Internal.Data.Parser
-- Copyright   : (c) 2020 Composewell Technologies
-- License     : BSD3
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC
--
-- Fast streaming parsers.
--
-- 'Applicative' and 'Alternative' type class based combinators from the
-- <http://hackage.haskell.org/package/parser-combinators parser-combinators>
-- package can also be used with the 'Parser' type. However, there are two
-- important differences between @parser-combinators@ and the equivalent ones
-- provided in this module in terms of performance:
--
-- 1) @parser-combinators@ use plain Haskell lists to collect the results, in a
-- strict Monad like IO, the results are necessarily buffered before they can
-- be consumed.  This may not perform optimally in streaming applications
-- processing large amounts of data.  Equivalent combinators in this module can
-- consume the results of parsing using a 'Fold', thus providing a scalability
-- and a generic consumer.
--
-- 2) Several combinators in this module can be many times faster because of
-- stream fusion. For example, 'Streamly.Internal.Data.Parser.many' combinator
-- in this module is much faster than the 'Control.Applicative.many' combinator
-- of 'Alternative' type class.
--
-- Failing parsers in this module throw the 'ParseError' exception.

-- XXX As far as possible, try that the combinators in this module and in
-- "Text.ParserCombinators.ReadP/parser-combinators/parsec/megaparsec/attoparsec"
-- have consistent names. takeP/takeWhileP/munch?

module Streamly.Internal.Data.Parser
    (
      Parser (..)

    -- First order parsers
    -- * Accumulators
    , fromFold
    , any
    , all
    , yield
    , yieldM
    , die
    , dieM

    -- * Element parsers
    , peek
    , eof
    , satisfy

    -- * Sequence parsers
    --
    -- Parsers chained in series, if one parser terminates the composition
    -- terminates. Currently we are using folds to collect the output of the
    -- parsers but we can use Parsers instead of folds to make the composition
    -- more powerful. For example, we can do:
    --
    -- sliceSepByMax cond n p = sliceBy cond (take n p)
    -- sliceSepByBetween cond m n p = sliceBy cond (takeBetween m n p)
    -- takeWhileBetween cond m n p = takeWhile cond (takeBetween m n p)
    --
    -- Grab a sequence of input elements without inspecting them
    , take
    -- , takeBetween
    -- , takeLE -- take   -- takeBetween 0 n
    -- , takeLE1 -- take1 -- takeBetween 1 n
    , takeEQ -- takeBetween n n
    , takeGE -- takeBetween n maxBound

    -- Grab a sequence of input elements by inspecting them
    , lookAhead
    , takeWhile
    , takeWhile1
    , sliceSepBy
    , sliceSepByMax
    -- , sliceSepByBetween
    , sliceEndWith
    , sliceBeginWith
    -- , sliceSepWith
    --
    -- , frameSepBy -- parse frames escaped by an escape char/sequence
    -- , frameEndWith
    --
    , wordBy
    , groupBy
    , eqBy
    -- , prefixOf -- match any prefix of a given string
    -- , suffixOf -- match any suffix of a given string
    -- , infixOf -- match any substring of a given string

    -- Second order parsers (parsers using parsers)
    -- * Binary Combinators

    -- ** Sequential Applicative
    , splitWith

    -- ** Parallel Applicatives
    , teeWith
    , teeWithFst
    , teeWithMin
    -- , teeTill -- like manyTill but parallel

    -- ** Sequential Interleaving
    -- Use two folds, run a primary parser, its rejected values go to the
    -- secondary parser.
    , deintercalate

    -- ** Parallel Alternatives
    , shortest
    , longest
    -- , fastest

    -- * N-ary Combinators
    -- ** Sequential Collection
    , sequence

    -- ** Sequential Repetition
    , count
    , countBetween
    -- , countBetweenTill

    , many
    , some
    , manyTill

    -- -- ** Special cases
    -- XXX traditional implmentations of these may be of limited use. For
    -- example, consider parsing lines separated by "\r\n". The main parser
    -- will have to detect and exclude the sequence "\r\n" anyway so that we
    -- can apply the "sep" parser.
    --
    -- We can instead implement these as special cases of deintercalate.
    --
    -- , endBy
    -- , sepBy
    -- , sepEndBy
    -- , beginBy
    -- , sepBeginBy
    -- , sepAroundBy

    -- -- * Distribution
    --
    -- A simple and stupid impl would be to just convert the stream to an array
    -- and give the array reference to all consumers. The array can be grown on
    -- demand by any consumer and truncated when nonbody needs it.
    --
    -- -- ** Distribute to collection
    -- -- ** Distribute to repetition

    -- -- ** Interleaved collection
    -- Round robin
    -- Priority based
    -- -- ** Interleaved repetition
    -- repeat one parser and when it fails run an error recovery parser
    -- e.g. to find a key frame in the stream after an error

    -- ** Collection of Alternatives
    -- , shortestN
    -- , longestN
    -- , fastestN -- first N successful in time
    -- , choiceN  -- first N successful in position
    , choice   -- first successful in position

    -- -- ** Repeated Alternatives
    -- , retryMax    -- try N times
    -- , retryUntil  -- try until successful
    -- , retryUntilN -- try until successful n times
    )
where

import Control.Exception (assert)
import Control.Monad.Catch (MonadCatch, MonadThrow(..))
import Prelude
       hiding (any, all, take, takeWhile, sequence)

import Streamly.Internal.Data.Fold.Types (Fold(..))

import Streamly.Internal.Data.Parser.Tee
import Streamly.Internal.Data.Parser.Types
import Streamly.Internal.Data.Strict

-------------------------------------------------------------------------------
-- Upgrade folds to parses
-------------------------------------------------------------------------------
--
-- | The resulting parse never terminates and never errors out.
--
{-# INLINE fromFold #-}
fromFold :: Monad m => Fold m a b -> Parser m a b
fromFold :: forall (m :: * -> *) a b. Monad m => Fold m a b -> Parser m a b
fromFold (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {b}. s -> a -> m (Step s b)
step m s
finitial s -> m b
fextract

    where

    step :: s -> a -> m (Step s b)
step s
s a
a = forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a

-------------------------------------------------------------------------------
-- Terminating but not failing folds
-------------------------------------------------------------------------------
--
-- |
-- >>> S.parse (PR.any (== 0)) $ S.fromList [1,0,1]
-- > Right True
--
{-# INLINABLE any #-}
any :: Monad m => (a -> Bool) -> Parser m a Bool
any :: forall (m :: * -> *) a. Monad m => (a -> Bool) -> Parser m a Bool
any a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial forall (m :: * -> *) a. Monad m => a -> m a
return

    where

    initial :: m Bool
initial = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False

    step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if Bool
s
        then forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
        else
            if a -> Bool
predicate a
a
            then forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
            else forall s b. Int -> s -> Step s b
Yield Int
0 Bool
False

-- |
-- >>> S.parse (PR.all (== 0)) $ S.fromList [1,0,1]
-- > Right False
--
{-# INLINABLE all #-}
all :: Monad m => (a -> Bool) -> Parser m a Bool
all :: forall (m :: * -> *) a. Monad m => (a -> Bool) -> Parser m a Bool
all a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial forall (m :: * -> *) a. Monad m => a -> m a
return

    where

    initial :: m Bool
initial = forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True

    step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if Bool
s
        then
            if a -> Bool
predicate a
a
            then forall s b. Int -> s -> Step s b
Yield Int
0 Bool
True
            else forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False
        else forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False

-------------------------------------------------------------------------------
-- Failing Parsers
-------------------------------------------------------------------------------

-- | Peek the head element of a stream, without consuming it. Fails if it
-- encounters end of input.
--
-- >>> S.parse ((,) <$> PR.peek <*> PR.satisfy (> 0)) $ S.fromList [1]
-- (1,1)
--
-- @
-- peek = lookAhead (satisfy True)
-- @
--
-- /Internal/
--
{-# INLINABLE peek #-}
peek :: MonadThrow m => Parser m a a
peek :: forall (m :: * -> *) a. MonadThrow m => Parser m a a
peek = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {b} {s}. Monad m => () -> b -> m (Step s b)
step m ()
initial forall {m :: * -> *} {a}. MonadThrow m => () -> m a
extract

    where

    initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()

    step :: () -> b -> m (Step s b)
step () b
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
1 b
a

    extract :: () -> m a
extract () = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"peek: end of input"

-- | Succeeds if we are at the end of input, fails otherwise.
--
-- >>> S.parse ((,) <$> PR.satisfy (> 0) <*> PR.eof) $ S.fromList [1]
-- > (1,())
--
-- /Internal/
--
{-# INLINABLE eof #-}
eof :: Monad m => Parser m a ()
eof :: forall (m :: * -> *) a. Monad m => Parser m a ()
eof = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {p} {s} {b}.
Monad m =>
() -> p -> m (Step s b)
step m ()
initial forall (m :: * -> *) a. Monad m => a -> m a
return

    where

    initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()

    step :: () -> p -> m (Step s b)
step () p
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
"eof: not at end of input"

-- | Returns the next element if it passes the predicate, fails otherwise.
--
-- >>> S.parse (PR.satisfy (== 1)) $ S.fromList [1,0,1]
-- > 1
--
-- /Internal/
--
{-# INLINE satisfy #-}
satisfy :: MonadThrow m => (a -> Bool) -> Parser m a a
satisfy :: forall (m :: * -> *) a. MonadThrow m => (a -> Bool) -> Parser m a a
satisfy a -> Bool
predicate = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *} {s}. Monad m => () -> a -> m (Step s a)
step m ()
initial forall {m :: * -> *} {p} {a}. MonadThrow m => p -> m a
extract

    where

    initial :: m ()
initial = forall (m :: * -> *) a. Monad m => a -> m a
return ()

    step :: () -> a -> m (Step s a)
step () a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if a -> Bool
predicate a
a
        then forall s b. Int -> b -> Step s b
Stop Int
0 a
a
        else forall s b. String -> Step s b
Error String
"satisfy: predicate failed"

    extract :: p -> m a
extract p
_ = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"satisfy: end of input"

-------------------------------------------------------------------------------
-- Taking elements
-------------------------------------------------------------------------------
--
-- XXX Once we have terminating folds, this Parse should get replaced by Fold.
-- Alternatively, we can name it "chunkOf" and the corresponding time domain
-- combinator as "intervalOf" or even "chunk" and "interval".
--
-- | Take at most @n@ input elements and fold them using the supplied fold.
--
-- Stops after @n@ elements.
-- Never fails.
--
-- >>> S.parse (PR.take 1 FL.toList) $ S.fromList [1]
-- [1]
--
-- @
-- S.chunksOf n f = S.splitParse (FL.take n f)
-- @
--
-- /Internal/
--
{-# INLINE take #-}
take :: Monad m => Int -> Fold m a b -> Parser m a b
take :: forall (m :: * -> *) a b.
Monad m =>
Int -> Fold m a b -> Parser m a b
take Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {a}. Tuple' a s -> m b
extract

    where

    initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial

    step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
        s
res <- s -> a -> m s
fstep s
r a
a
        let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
            s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
        if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n
        then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
        else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
res

    extract :: Tuple' a s -> m b
extract (Tuple' a
_ s
r) = s -> m b
fextract s
r

--
-- XXX can we use a "cmp" operation in a common implementation?
--
-- | Stops after taking exactly @n@ input elements.
--
-- * Stops - after @n@ elements.
-- * Fails - if the stream ends before it can collect @n@ elements.
--
-- >>> S.parse (PR.takeEQ 4 FL.toList) $ S.fromList [1,0,1]
-- > "takeEQ: Expecting exactly 4 elements, got 3"
--
-- /Internal/
--
{-# INLINE takeEQ #-}
takeEQ :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeEQ :: forall (m :: * -> *) a b.
MonadThrow m =>
Int -> Fold m a b -> Parser m a b
takeEQ Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial Tuple' Int s -> m b
extract

    where

    initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial

    step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
        s
res <- s -> a -> m s
fstep s
r a
a
        let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
            s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
        if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n then forall (m :: * -> *) a. Monad m => a -> m a
return (forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1) else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
res

    extract :: Tuple' Int s -> m b
extract (Tuple' Int
i s
r) =
        if Int
n forall a. Eq a => a -> a -> Bool
== Int
i
        then s -> m b
fextract s
r
        else forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

        where

        err :: String
err =
               String
"takeEQ: Expecting exactly " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n
            forall a. [a] -> [a] -> [a]
++ String
" elements, got " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i

-- | Take at least @n@ input elements, but can collect more.
--
-- * Stops - never.
-- * Fails - if the stream ends before producing @n@ elements.
--
-- >>> S.parse (PR.takeGE 4 FL.toList) $ S.fromList [1,0,1]
-- > "takeGE: Expecting at least 4 elements, got only 3"
--
-- >>> S.parse (PR.takeGE 4 FL.toList) $ S.fromList [1,0,1,0,1]
-- > [1,0,1,0,1]
--
-- /Internal/
--
{-# INLINE takeGE #-}
takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeGE :: forall (m :: * -> *) a b.
MonadThrow m =>
Int -> Fold m a b -> Parser m a b
takeGE Int
n (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {b}. Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial Tuple' Int s -> m b
extract

    where

    initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial

    step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
        s
res <- s -> a -> m s
fstep s
r a
a
        let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
            s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
            if Int
i1 forall a. Ord a => a -> a -> Bool
< Int
n
            then forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1
            else forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1

    extract :: Tuple' Int s -> m b
extract (Tuple' Int
i s
r) = s -> m b
fextract s
r forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall {m :: * -> *} {a}. MonadThrow m => a -> m a
f

        where

        err :: String
err =
              String
"takeGE: Expecting at least " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
n
           forall a. [a] -> [a] -> [a]
++ String
" elements, got only " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Int
i

        f :: a -> m a
f a
x =
            if Int
i forall a. Ord a => a -> a -> Bool
>= Int
n
            then forall (m :: * -> *) a. Monad m => a -> m a
return a
x
            else forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err

-- | Collect stream elements until an element fails the predicate. The element
-- on which the predicate fails is returned back to the input stream.
--
-- * Stops - when the predicate fails.
-- * Fails - never.
--
-- >>> S.parse (PR.takeWhile (== 0) FL.toList) $ S.fromList [0,0,1,0,1]
-- > [0,0]
--
-- We can implement a @breakOn@ using 'takeWhile':
--
-- @
-- breakOn p = takeWhile (not p)
-- @
--
-- /Internal/
--
{-# INLINE takeWhile #-}
takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Parser m a b
takeWhile a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser s -> a -> m (Step s b)
step m s
initial s -> m b
fextract

    where

    initial :: m s
initial = m s
finitial

    step :: s -> a -> m (Step s b)
step s
s a
a =
        if a -> Bool
predicate a
a
        then forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
        else forall s b. Int -> b -> Step s b
Stop Int
1 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s

-- | Like 'takeWhile' but takes at least one element otherwise fails.
--
-- /Internal/
--
{-# INLINE takeWhile1 #-}
takeWhile1 :: MonadThrow m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 :: forall (m :: * -> *) a b.
MonadThrow m =>
(a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Maybe s -> a -> m (Step (Maybe s) b)
step forall {a}. m (Maybe a)
initial Maybe s -> m b
extract

    where

    initial :: m (Maybe a)
initial = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing

    step :: Maybe s -> a -> m (Step (Maybe s) b)
step Maybe s
Nothing a
a =
        if a -> Bool
predicate a
a
        then do
            s
s <- m s
finitial
            s
r <- s -> a -> m s
fstep s
s a
a
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 (forall a. a -> Maybe a
Just s
r)
        else forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
"takeWhile1: empty"
    step (Just s
s) a
a =
        if a -> Bool
predicate a
a
        then do
            s
r <- s -> a -> m s
fstep s
s a
a
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 (forall a. a -> Maybe a
Just s
r)
        else do
            b
b <- s -> m b
fextract s
s
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
1 b
b

    extract :: Maybe s -> m b
extract Maybe s
Nothing = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"takeWhile1: end of input"
    extract (Just s
s) = s -> m b
fextract s
s

-- | Collect stream elements until an element succeeds the predicate. Drop the
-- element on which the predicate succeeded. The succeeding element is treated
-- as an infix separator which is dropped from the output.
--
-- * Stops - when the predicate succeeds.
-- * Fails - never.
--
-- >>> S.parse (PR.sliceSepBy (== 1) FL.toList) $ S.fromList [0,0,1,0,1]
-- > [0,0]
--
-- S.splitOn pred f = S.splitParse (PR.sliceSepBy pred f)
--
-- >>> S.toList $ S.splitParse (PR.sliceSepBy (== 1) FL.toList) $ S.fromList [0,0,1,0,1]
-- > [[0,0],[0],[]]
--
-- /Internal/
--
{-# INLINABLE sliceSepBy #-}
sliceSepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
sliceSepBy :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Fold m a b -> Parser m a b
sliceSepBy a -> Bool
predicate (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser s -> a -> m (Step s b)
step m s
initial s -> m b
fextract

    where

    initial :: m s
initial = m s
finitial
    step :: s -> a -> m (Step s b)
step s
s a
a =
        if Bool -> Bool
not (a -> Bool
predicate a
a)
        then forall s b. Int -> s -> Step s b
Yield Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
        else forall s b. Int -> b -> Step s b
Stop Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s

-- | Collect stream elements until an element succeeds the predicate. Also take
-- the element on which the predicate succeeded. The succeeding element is
-- treated as a suffix separator which is kept in the output segement.
--
-- * Stops - when the predicate succeeds.
-- * Fails - never.
--
-- S.splitWithSuffix pred f = S.splitParse (PR.sliceEndWith pred f)
--
-- /Unimplemented/
--
{-# INLINABLE sliceEndWith #-}
sliceEndWith ::
    -- Monad m =>
    (a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith = forall a. HasCallStack => a
undefined

-- | Collect stream elements until an elements passes the predicate, return the
-- last element on which the predicate succeeded back to the input stream.  If
-- the predicate succeeds on the first element itself then it is kept in the
-- stream and we continue collecting. The succeeding element is treated as a
-- prefix separator which is kept in the output segement.
--
-- * Stops - when the predicate succeeds in non-leading position.
-- * Fails - never.
--
-- S.splitWithPrefix pred f = S.splitParse (PR.sliceBeginWith pred f)
--
-- /Unimplemented/
--
{-# INLINABLE sliceBeginWith #-}
sliceBeginWith ::
    -- Monad m =>
    (a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith = forall a. HasCallStack => a
undefined

-- | Split using a condition or a count whichever occurs first. This is a
-- hybrid of 'splitOn' and 'take'. The element on which the condition succeeds
-- is dropped.
--
-- /Internal/
--
{-# INLINABLE sliceSepByMax #-}
sliceSepByMax :: Monad m
    => (a -> Bool) -> Int -> Fold m a b -> Parser m a b
sliceSepByMax :: forall (m :: * -> *) a b.
Monad m =>
(a -> Bool) -> Int -> Fold m a b -> Parser m a b
sliceSepByMax a -> Bool
predicate Int
cnt (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {a}. Tuple' a s -> m b
extract

    where

    initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
finitial
    step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
i s
r) a
a = do
        s
res <- s -> a -> m s
fstep s
r a
a
        let i1 :: Int
i1 = Int
i forall a. Num a => a -> a -> a
+ Int
1
            s1 :: Tuple' Int s
s1 = forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
        if Bool -> Bool
not (a -> Bool
predicate a
a) Bool -> Bool -> Bool
&& Int
i1 forall a. Ord a => a -> a -> Bool
< Int
cnt
        then forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
        else do
            b
b <- s -> m b
fextract s
res
            forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
0 b
b
    extract :: Tuple' a s -> m b
extract (Tuple' a
_ s
r) = s -> m b
fextract s
r

-- | Like 'splitOn' but strips leading, trailing, and repeated separators.
-- Therefore, @".a..b."@ having '.' as the separator would be parsed as
-- @["a","b"]@.  In other words, its like parsing words from whitespace
-- separated text.
--
-- * Stops - when it finds a word separator after a non-word element
-- * Fails - never.
--
-- @
-- S.wordsBy pred f = S.splitParse (PR.wordBy pred f)
-- @
--
-- /Unimplemented/
--
{-# INLINABLE wordBy #-}
wordBy ::
    -- Monad m =>
    (a -> Bool) -> Fold m a b -> Parser m a b
wordBy :: forall a (m :: * -> *) b. (a -> Bool) -> Fold m a b -> Parser m a b
wordBy = forall a. HasCallStack => a
undefined

-- | @groupBy cmp f $ S.fromList [a,b,c,...]@ assigns the element @a@ to the
-- first group, then if @a \`cmp` b@ is 'True' @b@ is also assigned to the same
-- group.  If @a \`cmp` c@ is 'True' then @c@ is also assigned to the same
-- group and so on. When the comparison fails a new group is started. Each
-- group is folded using the 'Fold' @f@ and the result of the fold is emitted
-- in the output stream.
--
-- * Stops - when the comparison fails.
-- * Fails - never.
--
-- @
-- S.groupsBy cmp f = S.splitParse (PR.groupBy cmp f)
-- @
--
-- /Unimplemented/
--
{-# INLINABLE groupBy #-}
groupBy ::
    -- Monad m =>
    (a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy :: forall a (m :: * -> *) b.
(a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy = forall a. HasCallStack => a
undefined

-- XXX use an Unfold instead of a list?
-- XXX custom combinators for matching list, array and stream?
--
-- | Match the given sequence of elements using the given comparison function.
--
-- /Internal/
--
{-# INLINE eqBy #-}
eqBy :: MonadThrow m => (a -> a -> Bool) -> [a] -> Parser m a ()
eqBy :: forall (m :: * -> *) a.
MonadThrow m =>
(a -> a -> Bool) -> [a] -> Parser m a ()
eqBy a -> a -> Bool
cmp [a]
str = forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser forall {m :: * -> *}. Monad m => [a] -> a -> m (Step [a] ())
step m [a]
initial forall {m :: * -> *} {t :: * -> *} {a} {a}.
(MonadThrow m, Foldable t) =>
t a -> m a
extract

    where

    initial :: m [a]
initial = forall (m :: * -> *) a. Monad m => a -> m a
return [a]
str

    step :: [a] -> a -> m (Step [a] ())
step [] a
_ = forall a. HasCallStack => String -> a
error String
"Bug: unreachable"
    step [a
x] a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if a
x a -> a -> Bool
`cmp` a
a
        then forall s b. Int -> b -> Step s b
Stop Int
0 ()
        else forall s b. String -> Step s b
Error forall a b. (a -> b) -> a -> b
$
            String
"eqBy: failed, at the last element"
    step (a
x:[a]
xs) a
a = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
        if a
x a -> a -> Bool
`cmp` a
a
        then forall s b. Int -> s -> Step s b
Skip Int
0 [a]
xs
        else forall s b. String -> Step s b
Error forall a b. (a -> b) -> a -> b
$
            String
"eqBy: failed, yet to match " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) forall a. [a] -> [a] -> [a]
++ String
" elements"

    extract :: t a -> m a
extract t a
xs = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError forall a b. (a -> b) -> a -> b
$
        String
"eqBy: end of input, yet to match " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
xs) forall a. [a] -> [a] -> [a]
++ String
" elements"

-------------------------------------------------------------------------------
-- nested parsers
-------------------------------------------------------------------------------

{-# INLINE lookAhead #-}
lookAhead :: MonadThrow m => Parser m a b -> Parser m a b
lookAhead :: forall (m :: * -> *) a b.
MonadThrow m =>
Parser m a b -> Parser m a b
lookAhead (Parser s -> a -> m (Step s b)
step1 m s
initial1 s -> m b
_) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step m (Tuple' Int s)
initial forall {m :: * -> *} {a} {b} {a}.
(MonadThrow m, Show a) =>
Tuple' a b -> m a
extract

    where

    initial :: m (Tuple' Int s)
initial = forall a b. a -> b -> Tuple' a b
Tuple' Int
0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initial1

    step :: Tuple' Int s -> a -> m (Step (Tuple' Int s) b)
step (Tuple' Int
cnt s
st) a
a = do
        Step s b
r <- s -> a -> m (Step s b)
step1 s
st a
a
        let cnt1 :: Int
cnt1 = Int
cnt forall a. Num a => a -> a -> a
+ Int
1
        forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ case Step s b
r of
            Yield Int
_ s
s -> forall s b. Int -> s -> Step s b
Skip Int
0 (forall a b. a -> b -> Tuple' a b
Tuple' Int
cnt1 s
s)
            Skip Int
n s
s -> forall s b. Int -> s -> Step s b
Skip Int
n (forall a b. a -> b -> Tuple' a b
Tuple' (Int
cnt1 forall a. Num a => a -> a -> a
- Int
n) s
s)
            Stop Int
_ b
b -> forall s b. Int -> b -> Step s b
Stop Int
cnt1 b
b
            Error String
err -> forall s b. String -> Step s b
Error String
err

    -- XXX returning an error let's us backtrack.  To implement it in a way so
    -- that it terminates on eof without an error then we need a way to
    -- backtrack on eof, that will require extract to return 'Step' type.
    extract :: Tuple' a b -> m a
extract (Tuple' a
n b
_) = forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError forall a b. (a -> b) -> a -> b
$
        String
"lookAhead: end of input after consuming " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show a
n forall a. [a] -> [a] -> [a]
++ String
" elements"

-------------------------------------------------------------------------------
-- Interleaving
-------------------------------------------------------------------------------
--
-- To deinterleave we can chain two parsers one behind the other. The input is
-- given to the first parser and the input definitively rejected by the first
-- parser is given to the second parser.
--
-- We can either have the parsers themselves buffer the input or use the shared
-- global buffer to hold it until none of the parsers need it. When the first
-- parser returns Skip (i.e. rewind) we let the second parser consume the
-- rejected input and when it is done we move the cursor forward to the first
-- parser again. This will require a "move forward" command as well.
--
-- To implement grep we can use three parsers, one to find the pattern, one
-- to store the context behind the pattern and one to store the context in
-- front of the pattern. When a match occurs we need to emit the accumulator of
-- all the three parsers. One parser can count the line numbers to provide the
-- line number info.
--
-- | Apply two parsers alternately to an input stream. The input stream is
-- considered an interleaving of two patterns. The two parsers represent the
-- two patterns.
--
-- This undoes a "gintercalate" of two streams.
--
-- /Unimplemented/
--
{-# INLINE deintercalate #-}
deintercalate ::
    -- Monad m =>
       Fold m a y -> Parser m x a
    -> Fold m b z -> Parser m x b
    -> Parser m x (y, z)
deintercalate :: forall (m :: * -> *) a y x b z.
Fold m a y
-> Parser m x a -> Fold m b z -> Parser m x b -> Parser m x (y, z)
deintercalate = forall a. HasCallStack => a
undefined

-------------------------------------------------------------------------------
-- Sequential Collection
-------------------------------------------------------------------------------
--
-- | @sequence f t@ collects sequential parses of parsers in the container @t@
-- using the fold @f@. Fails if the input ends or any of the parsers fail.
--
-- /Unimplemented/
--
{-# INLINE sequence #-}
sequence ::
    -- Foldable t =>
    Fold m b c -> t (Parser m a b) -> Parser m a c
sequence :: forall (m :: * -> *) b c (t :: * -> *) a.
Fold m b c -> t (Parser m a b) -> Parser m a c
sequence Fold m b c
_f t (Parser m a b)
_p = forall a. HasCallStack => a
undefined

-------------------------------------------------------------------------------
-- Alternative Collection
-------------------------------------------------------------------------------
--
-- | @choice parsers@ applies the @parsers@ in order and returns the first
-- successful parse.
--
{-# INLINE choice #-}
choice ::
    -- Foldable t =>
    t (Parser m a b) -> Parser m a b
choice :: forall (t :: * -> *) (m :: * -> *) a b.
t (Parser m a b) -> Parser m a b
choice t (Parser m a b)
_ps = forall a. HasCallStack => a
undefined

-------------------------------------------------------------------------------
-- Sequential Repetition
-------------------------------------------------------------------------------
--
-- XXX "many" is essentially a Fold because it cannot fail. So it can be
-- downgraded to a Fold. Or we can make the return type a Fold instead and
-- upgrade that to a parser when needed.
--
-- | Collect zero or more parses. Apply the parser repeatedly on the input
-- stream, stop when the parser fails, accumulate zero or more parse results
-- using the supplied 'Fold'. This parser never fails, in case the first
-- application of parser fails it returns an empty result.
--
-- Compare with 'Control.Applicative.many'.
--
-- /Internal/
--
{-# INLINE many #-}
many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
many :: forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
many = forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitMany
-- many = countBetween 0 maxBound

-- | Collect one or more parses. Apply the supplied parser repeatedly on the
-- input stream and accumulate the parse results as long as the parser
-- succeeds, stop when it fails.  This parser fails if not even one result is
-- collected.
--
-- Compare with 'Control.Applicative.some'.
--
-- /Internal/
--
{-# INLINE some #-}
some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
some :: forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
some = forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitSome
-- some f p = many (takeGE 1 f) p
-- many = countBetween 1 maxBound

-- | @countBetween m n f p@ collects between @m@ and @n@ sequential parses of
-- parser @p@ using the fold @f@. Stop after collecting @n@ results. Fails if
-- the input ends or the parser fails before @m@ results are collected.
--
-- /Unimplemented/
--
{-# INLINE countBetween #-}
countBetween ::
    -- MonadCatch m =>
    Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween :: forall (m :: * -> *) b c a.
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
_m Int
_n Fold m b c
_f = forall a. HasCallStack => a
undefined
-- countBetween m n f p = many (takeBetween m n f) p

-- | @count n f p@ collects exactly @n@ sequential parses of parser @p@ using
-- the fold @f@.  Fails if the input ends or the parser fails before @n@
-- results are collected.
--
-- /Unimplemented/
--
{-# INLINE count #-}
count ::
    -- MonadCatch m =>
    Int -> Fold m b c -> Parser m a b -> Parser m a c
count :: forall (m :: * -> *) b c a.
Int -> Fold m b c -> Parser m a b -> Parser m a c
count Int
n = forall (m :: * -> *) b c a.
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
n Int
n
-- count n f p = many (takeEQ n f) p

data ManyTillState fs sr sl = ManyTillR Int fs sr | ManyTillL fs sl

-- | @manyTill f collect test@ tries the parser @test@ on the input, if @test@
-- fails it backtracks and tries @collect@, after @collect@ succeeds @test@ is
-- tried again and so on. The parser stops when @test@ succeeds.  The output of
-- @test@ is discarded and the output of @collect@ is accumulated by the
-- supplied fold. The parser fails if @collect@ fails.
--
-- /Internal/
--
{-# INLINE manyTill #-}
manyTill :: MonadCatch m
    => Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill :: forall (m :: * -> *) b c a x.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill (Fold s -> b -> m s
fstep m s
finitial s -> m c
fextract)
         (Parser s -> a -> m (Step s b)
stepL m s
initialL s -> m b
extractL)
         (Parser s -> a -> m (Step s x)
stepR m s
initialR s -> m x
_) =
    forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser ManyTillState s s s -> a -> m (Step (ManyTillState s s s) c)
step forall {sl}. m (ManyTillState s s sl)
initial forall {sr}. ManyTillState s sr s -> m c
extract

    where

    initial :: m (ManyTillState s s sl)
initial = do
        s
fs <- m s
finitial
        forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m s
initialR

    step :: ManyTillState s s s -> a -> m (Step (ManyTillState s s s) c)
step (ManyTillR Int
cnt s
fs s
st) a
a = do
        Step s x
r <- s -> a -> m (Step s x)
stepR s
st a
a
        case Step s x
r of
            Yield Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs s
s)
            Skip Int
n s
s -> do
                forall a. HasCallStack => Bool -> a -> a
assert (Int
cnt forall a. Num a => a -> a -> a
+ Int
1 forall a. Num a => a -> a -> a
- Int
n forall a. Ord a => a -> a -> Bool
>= Int
0) (forall (m :: * -> *) a. Monad m => a -> m a
return ())
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR (Int
cnt forall a. Num a => a -> a -> a
+ Int
1 forall a. Num a => a -> a -> a
- Int
n) s
fs s
s)
            Stop Int
n x
_ -> do
                c
b <- s -> m c
fextract s
fs
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> b -> Step s b
Stop Int
n c
b
            Error String
_ -> do
                s
rR <- m s
initialL
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip (Int
cnt forall a. Num a => a -> a -> a
+ Int
1) (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
rR)

    step (ManyTillL s
fs s
st) a
a = do
        Step s b
r <- s -> a -> m (Step s b)
stepL s
st a
a
        case Step s b
r of
            Yield Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Yield Int
n (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
s)
            Skip Int
n s
s -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
s)
            Stop Int
n b
b -> do
                s
fs1 <- s -> b -> m s
fstep s
fs b
b
                s
l <- m s
initialR
                -- XXX we need a yield with backtrack here
                -- return $ Yield n (ManyTillR 0 fs1 l)
                forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. Int -> s -> Step s b
Skip Int
n (forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs1 s
l)
            Error String
err -> forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s b. String -> Step s b
Error String
err

    extract :: ManyTillState s sr s -> m c
extract (ManyTillL s
fs s
sR) = s -> m b
extractL s
sR forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m s
fstep s
fs forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> m c
fextract
    extract (ManyTillR Int
_ s
fs sr
_) = s -> m c
fextract s
fs