{-# LANGUAGE CPP #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Streamly.Internal.Data.Parser
(
Parser (..)
, fromFold
, any
, all
, yield
, yieldM
, die
, dieM
, peek
, eof
, satisfy
, take
, takeEQ
, takeGE
, lookAhead
, takeWhile
, takeWhile1
, sliceSepBy
, sliceSepByMax
, sliceEndWith
, sliceBeginWith
, wordBy
, groupBy
, eqBy
, splitWith
, teeWith
, teeWithFst
, teeWithMin
, deintercalate
, shortest
, longest
, sequence
, count
, countBetween
, many
, some
, manyTill
, choice
)
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
{-# INLINE fromFold #-}
fromFold :: Monad m => Fold m a b -> Parser m a b
fromFold :: Fold m a b -> Parser m a b
fromFold (Fold s -> a -> m s
fstep m s
finitial s -> m b
fextract) = (s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
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)
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 = Int -> s -> Step s b
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Step s b) -> m s -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
{-# INLINABLE any #-}
any :: Monad m => (a -> Bool) -> Parser m a Bool
any :: (a -> Bool) -> Parser m a Bool
any a -> Bool
predicate = (Bool -> a -> m (Step Bool Bool))
-> m Bool -> (Bool -> m Bool) -> Parser m a Bool
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Bool -> a -> m (Step Bool Bool)
forall (m :: * -> *). Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m Bool
initial = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = Step Bool Bool -> m (Step Bool Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Bool Bool -> m (Step Bool Bool))
-> Step Bool Bool -> m (Step Bool Bool)
forall a b. (a -> b) -> a -> b
$
if Bool
s
then Int -> Bool -> Step Bool Bool
forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
else
if a -> Bool
predicate a
a
then Int -> Bool -> Step Bool Bool
forall s b. Int -> b -> Step s b
Stop Int
0 Bool
True
else Int -> Bool -> Step Bool Bool
forall s b. Int -> s -> Step s b
Yield Int
0 Bool
False
{-# INLINABLE all #-}
all :: Monad m => (a -> Bool) -> Parser m a Bool
all :: (a -> Bool) -> Parser m a Bool
all a -> Bool
predicate = (Bool -> a -> m (Step Bool Bool))
-> m Bool -> (Bool -> m Bool) -> Parser m a Bool
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser Bool -> a -> m (Step Bool Bool)
forall (m :: * -> *). Monad m => Bool -> a -> m (Step Bool Bool)
step m Bool
initial Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m Bool
initial = Bool -> m Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
step :: Bool -> a -> m (Step Bool Bool)
step Bool
s a
a = Step Bool Bool -> m (Step Bool Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step Bool Bool -> m (Step Bool Bool))
-> Step Bool Bool -> m (Step Bool Bool)
forall a b. (a -> b) -> a -> b
$
if Bool
s
then
if a -> Bool
predicate a
a
then Int -> Bool -> Step Bool Bool
forall s b. Int -> s -> Step s b
Yield Int
0 Bool
True
else Int -> Bool -> Step Bool Bool
forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False
else Int -> Bool -> Step Bool Bool
forall s b. Int -> b -> Step s b
Stop Int
0 Bool
False
{-# INLINABLE peek #-}
peek :: MonadThrow m => Parser m a a
peek :: Parser m a a
peek = (() -> a -> m (Step () a)) -> m () -> (() -> m a) -> Parser m a a
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser () -> a -> m (Step () a)
forall (m :: * -> *) b s. Monad m => () -> b -> m (Step s b)
step m ()
initial () -> m a
forall (m :: * -> *) a. MonadThrow m => () -> m a
extract
where
initial :: m ()
initial = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> b -> m (Step s b)
step () b
a = Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Step s b
forall s b. Int -> b -> Step s b
Stop Int
1 b
a
extract :: () -> m a
extract () = ParseError -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m a) -> ParseError -> m a
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"peek: end of input"
{-# INLINABLE eof #-}
eof :: Monad m => Parser m a ()
eof :: Parser m a ()
eof = (() -> a -> m (Step () ()))
-> m () -> (() -> m ()) -> Parser m a ()
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser () -> a -> m (Step () ())
forall (m :: * -> *) p s b. Monad m => () -> p -> m (Step s b)
step m ()
initial () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return
where
initial :: m ()
initial = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> p -> m (Step s b)
step () p
_ = Step s b -> m (Step s b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s b -> m (Step s b)) -> Step s b -> m (Step s b)
forall a b. (a -> b) -> a -> b
$ String -> Step s b
forall s b. String -> Step s b
Error String
"eof: not at end of input"
{-# INLINE satisfy #-}
satisfy :: MonadThrow m => (a -> Bool) -> Parser m a a
satisfy :: (a -> Bool) -> Parser m a a
satisfy a -> Bool
predicate = (() -> a -> m (Step () a)) -> m () -> (() -> m a) -> Parser m a a
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser () -> a -> m (Step () a)
forall (m :: * -> *) s. Monad m => () -> a -> m (Step s a)
step m ()
initial () -> m a
forall (m :: * -> *) p a. MonadThrow m => p -> m a
extract
where
initial :: m ()
initial = () -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
step :: () -> a -> m (Step s a)
step () a
a = Step s a -> m (Step s a)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step s a -> m (Step s a)) -> Step s a -> m (Step s a)
forall a b. (a -> b) -> a -> b
$
if a -> Bool
predicate a
a
then Int -> a -> Step s a
forall s b. Int -> b -> Step s b
Stop Int
0 a
a
else String -> Step s a
forall s b. String -> Step s b
Error String
"satisfy: predicate failed"
extract :: p -> m a
extract p
_ = ParseError -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m a) -> ParseError -> m a
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
"satisfy: end of input"
{-# INLINE take #-}
take :: Monad m => Int -> Fold m a b -> Parser m a b
take :: 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) = (Tuple' Int s -> a -> m (Step (Tuple' Int s) b))
-> m (Tuple' Int s) -> (Tuple' Int s -> m b) -> Parser m a 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 Tuple' Int s -> m b
forall a. Tuple' a s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
0 (s -> Tuple' Int s) -> m s -> m (Tuple' Int s)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
if Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b))
-> Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall a b. (a -> b) -> a -> b
$ Int -> Tuple' Int s -> Step (Tuple' Int s) b
forall s b. Int -> s -> Step s b
Yield Int
0 Tuple' Int s
s1
else Int -> b -> Step (Tuple' Int s) b
forall s b. Int -> b -> Step s b
Stop Int
0 (b -> Step (Tuple' Int s) b) -> m b -> m (Step (Tuple' Int s) b)
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
{-# INLINE takeEQ #-}
takeEQ :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeEQ :: 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) = (Tuple' Int s -> a -> m (Step (Tuple' Int s) b))
-> m (Tuple' Int s) -> (Tuple' Int s -> m b) -> Parser m a 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 Tuple' Int s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
0 (s -> Tuple' Int s) -> m s -> m (Tuple' Int s)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
if Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n then Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> Tuple' Int s -> Step (Tuple' Int s) b
forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1) else Int -> b -> Step (Tuple' Int s) b
forall s b. Int -> b -> Step s b
Stop Int
0 (b -> Step (Tuple' Int s) b) -> m b -> m (Step (Tuple' Int s) b)
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 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i
then s -> m b
fextract s
r
else ParseError -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m b) -> ParseError -> m b
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
where
err :: String
err =
String
"takeEQ: Expecting exactly " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" elements, got " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
i
{-# INLINE takeGE #-}
takeGE :: MonadThrow m => Int -> Fold m a b -> Parser m a b
takeGE :: 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) = (Tuple' Int s -> a -> m (Step (Tuple' Int s) b))
-> m (Tuple' Int s) -> (Tuple' Int s -> m b) -> Parser m a 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)
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 = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
0 (s -> Tuple' Int s) -> m s -> m (Tuple' Int s)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
i1 s
res
Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b))
-> Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall a b. (a -> b) -> a -> b
$
if Int
i1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then Int -> Tuple' Int s -> Step (Tuple' Int s) b
forall s b. Int -> s -> Step s b
Skip Int
0 Tuple' Int s
s1
else Int -> Tuple' Int s -> Step (Tuple' Int s) b
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 m b -> (b -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= b -> m b
forall (m :: * -> *) a. MonadThrow m => a -> m a
f
where
err :: String
err =
String
"takeGE: Expecting at least " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" elements, got only " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
i
f :: a -> m a
f a
x =
if Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
n
then a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
else ParseError -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m a) -> ParseError -> m a
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError String
err
{-# INLINE takeWhile #-}
takeWhile :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile :: (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) =
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
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 Int -> s -> Step s b
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Step s b) -> m s -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
else Int -> b -> Step s b
forall s b. Int -> b -> Step s b
Stop Int
1 (b -> Step s b) -> m b -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s
{-# INLINE takeWhile1 #-}
takeWhile1 :: MonadThrow m => (a -> Bool) -> Fold m a b -> Parser m a b
takeWhile1 :: (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) =
(Maybe s -> a -> m (Step (Maybe s) b))
-> m (Maybe s) -> (Maybe s -> m b) -> Parser m a b
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 m (Maybe s)
forall a. m (Maybe a)
initial Maybe s -> m b
extract
where
initial :: m (Maybe a)
initial = Maybe a -> m (Maybe a)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
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
Step (Maybe s) b -> m (Step (Maybe s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe s) b -> m (Step (Maybe s) b))
-> Step (Maybe s) b -> m (Step (Maybe s) b)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe s -> Step (Maybe s) b
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Maybe s
forall a. a -> Maybe a
Just s
r)
else Step (Maybe s) b -> m (Step (Maybe s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe s) b -> m (Step (Maybe s) b))
-> Step (Maybe s) b -> m (Step (Maybe s) b)
forall a b. (a -> b) -> a -> b
$ String -> Step (Maybe s) 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
Step (Maybe s) b -> m (Step (Maybe s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe s) b -> m (Step (Maybe s) b))
-> Step (Maybe s) b -> m (Step (Maybe s) b)
forall a b. (a -> b) -> a -> b
$ Int -> Maybe s -> Step (Maybe s) b
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Maybe s
forall a. a -> Maybe a
Just s
r)
else do
b
b <- s -> m b
fextract s
s
Step (Maybe s) b -> m (Step (Maybe s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Maybe s) b -> m (Step (Maybe s) b))
-> Step (Maybe s) b -> m (Step (Maybe s) b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Step (Maybe s) b
forall s b. Int -> b -> Step s b
Stop Int
1 b
b
extract :: Maybe s -> m b
extract Maybe s
Nothing = ParseError -> m b
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m b) -> ParseError -> m b
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
{-# INLINABLE sliceSepBy #-}
sliceSepBy :: Monad m => (a -> Bool) -> Fold m a b -> Parser m a b
sliceSepBy :: (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) =
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
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 Int -> s -> Step s b
forall s b. Int -> s -> Step s b
Yield Int
0 (s -> Step s b) -> m s -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> a -> m s
fstep s
s a
a
else Int -> b -> Step s b
forall s b. Int -> b -> Step s b
Stop Int
0 (b -> Step s b) -> m b -> m (Step s b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> m b
fextract s
s
{-# INLINABLE sliceEndWith #-}
sliceEndWith ::
(a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith :: (a -> Bool) -> Fold m a b -> Parser m a b
sliceEndWith = (a -> Bool) -> Fold m a b -> Parser m a b
forall a. HasCallStack => a
undefined
{-# INLINABLE sliceBeginWith #-}
sliceBeginWith ::
(a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith :: (a -> Bool) -> Fold m a b -> Parser m a b
sliceBeginWith = (a -> Bool) -> Fold m a b -> Parser m a b
forall a. HasCallStack => a
undefined
{-# INLINABLE sliceSepByMax #-}
sliceSepByMax :: Monad m
=> (a -> Bool) -> Int -> Fold m a b -> Parser m a b
sliceSepByMax :: (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) =
(Tuple' Int s -> a -> m (Step (Tuple' Int s) b))
-> m (Tuple' Int s) -> (Tuple' Int s -> m b) -> Parser m a 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 Tuple' Int s -> m b
forall a. Tuple' a s -> m b
extract
where
initial :: m (Tuple' Int s)
initial = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
0 (s -> Tuple' Int s) -> m s -> m (Tuple' Int s)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
s1 :: Tuple' Int s
s1 = Int -> s -> Tuple' Int s
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 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
cnt
then Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b))
-> Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall a b. (a -> b) -> a -> b
$ Int -> Tuple' Int s -> Step (Tuple' Int s) 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
Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b))
-> Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall a b. (a -> b) -> a -> b
$ Int -> b -> Step (Tuple' Int s) 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
{-# INLINABLE wordBy #-}
wordBy ::
(a -> Bool) -> Fold m a b -> Parser m a b
wordBy :: (a -> Bool) -> Fold m a b -> Parser m a b
wordBy = (a -> Bool) -> Fold m a b -> Parser m a b
forall a. HasCallStack => a
undefined
{-# INLINABLE groupBy #-}
groupBy ::
(a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy :: (a -> a -> Bool) -> Fold m a b -> Parser m a b
groupBy = (a -> a -> Bool) -> Fold m a b -> Parser m a b
forall a. HasCallStack => a
undefined
{-# INLINE eqBy #-}
eqBy :: MonadThrow m => (a -> a -> Bool) -> [a] -> Parser m a ()
eqBy :: (a -> a -> Bool) -> [a] -> Parser m a ()
eqBy a -> a -> Bool
cmp [a]
str = ([a] -> a -> m (Step [a] ()))
-> m [a] -> ([a] -> m ()) -> Parser m a ()
forall (m :: * -> *) a b s.
(s -> a -> m (Step s b)) -> m s -> (s -> m b) -> Parser m a b
Parser [a] -> a -> m (Step [a] ())
forall (m :: * -> *). Monad m => [a] -> a -> m (Step [a] ())
step m [a]
initial [a] -> m ()
forall (m :: * -> *) (t :: * -> *) a a.
(MonadThrow m, Foldable t) =>
t a -> m a
extract
where
initial :: m [a]
initial = [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [a]
str
step :: [a] -> a -> m (Step [a] ())
step [] a
_ = String -> m (Step [a] ())
forall a. HasCallStack => String -> a
error String
"Bug: unreachable"
step [a
x] a
a = Step [a] () -> m (Step [a] ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step [a] () -> m (Step [a] ())) -> Step [a] () -> m (Step [a] ())
forall a b. (a -> b) -> a -> b
$
if a
x a -> a -> Bool
`cmp` a
a
then Int -> () -> Step [a] ()
forall s b. Int -> b -> Step s b
Stop Int
0 ()
else String -> Step [a] ()
forall s b. String -> Step s b
Error (String -> Step [a] ()) -> String -> Step [a] ()
forall a b. (a -> b) -> a -> b
$
String
"eqBy: failed, at the last element"
step (a
x:[a]
xs) a
a = Step [a] () -> m (Step [a] ())
forall (m :: * -> *) a. Monad m => a -> m a
return (Step [a] () -> m (Step [a] ())) -> Step [a] () -> m (Step [a] ())
forall a b. (a -> b) -> a -> b
$
if a
x a -> a -> Bool
`cmp` a
a
then Int -> [a] -> Step [a] ()
forall s b. Int -> s -> Step s b
Skip Int
0 [a]
xs
else String -> Step [a] ()
forall s b. String -> Step s b
Error (String -> Step [a] ()) -> String -> Step [a] ()
forall a b. (a -> b) -> a -> b
$
String
"eqBy: failed, yet to match " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" elements"
extract :: t a -> m a
extract t a
xs = ParseError -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m a) -> ParseError -> m a
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError (String -> ParseError) -> String -> ParseError
forall a b. (a -> b) -> a -> b
$
String
"eqBy: end of input, yet to match " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (t a -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length t a
xs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" elements"
{-# INLINE lookAhead #-}
lookAhead :: MonadThrow m => Parser m a b -> Parser m a b
lookAhead :: Parser m a b -> Parser m a b
lookAhead (Parser s -> a -> m (Step s b)
step1 m s
initial1 s -> m b
_) =
(Tuple' Int s -> a -> m (Step (Tuple' Int s) b))
-> m (Tuple' Int s) -> (Tuple' Int s -> m b) -> Parser m a 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 Tuple' Int s -> m b
forall (m :: * -> *) a b a.
(MonadThrow m, Show a) =>
Tuple' a b -> m a
extract
where
initial :: m (Tuple' Int s)
initial = Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
0 (s -> Tuple' Int s) -> m s -> m (Tuple' Int s)
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 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b))
-> Step (Tuple' Int s) b -> m (Step (Tuple' Int s) b)
forall a b. (a -> b) -> a -> b
$ case Step s b
r of
Yield Int
_ s
s -> Int -> Tuple' Int s -> Step (Tuple' Int s) b
forall s b. Int -> s -> Step s b
Skip Int
0 (Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' Int
cnt1 s
s)
Skip Int
n s
s -> Int -> Tuple' Int s -> Step (Tuple' Int s) b
forall s b. Int -> s -> Step s b
Skip Int
n (Int -> s -> Tuple' Int s
forall a b. a -> b -> Tuple' a b
Tuple' (Int
cnt1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) s
s)
Stop Int
_ b
b -> Int -> b -> Step (Tuple' Int s) b
forall s b. Int -> b -> Step s b
Stop Int
cnt1 b
b
Error String
err -> String -> Step (Tuple' Int s) b
forall s b. String -> Step s b
Error String
err
extract :: Tuple' a b -> m a
extract (Tuple' a
n b
_) = ParseError -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (ParseError -> m a) -> ParseError -> m a
forall a b. (a -> b) -> a -> b
$ String -> ParseError
ParseError (String -> ParseError) -> String -> ParseError
forall a b. (a -> b) -> a -> b
$
String
"lookAhead: end of input after consuming " String -> String -> String
forall a. [a] -> [a] -> [a]
++ a -> String
forall a. Show a => a -> String
show a
n String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" elements"
{-# INLINE deintercalate #-}
deintercalate ::
Fold m a y -> Parser m x a
-> Fold m b z -> Parser m x b
-> Parser m x (y, z)
deintercalate :: Fold m a y
-> Parser m x a -> Fold m b z -> Parser m x b -> Parser m x (y, z)
deintercalate = Fold m a y
-> Parser m x a -> Fold m b z -> Parser m x b -> Parser m x (y, z)
forall a. HasCallStack => a
undefined
{-# INLINE sequence #-}
sequence ::
Fold m b c -> t (Parser m a b) -> Parser m a c
sequence :: 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 = Parser m a c
forall a. HasCallStack => a
undefined
{-# INLINE choice #-}
choice ::
t (Parser m a b) -> Parser m a b
choice :: t (Parser m a b) -> Parser m a b
choice t (Parser m a b)
_ps = Parser m a b
forall a. HasCallStack => a
undefined
{-# INLINE many #-}
many :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
many :: Fold m b c -> Parser m a b -> Parser m a c
many = Fold m b c -> Parser m a b -> Parser m a c
forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitMany
{-# INLINE some #-}
some :: MonadCatch m => Fold m b c -> Parser m a b -> Parser m a c
some :: Fold m b c -> Parser m a b -> Parser m a c
some = Fold m b c -> Parser m a b -> Parser m a c
forall (m :: * -> *) b c a.
MonadCatch m =>
Fold m b c -> Parser m a b -> Parser m a c
splitSome
{-# INLINE countBetween #-}
countBetween ::
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween :: Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
_m Int
_n Fold m b c
_f = Parser m a b -> Parser m a c
forall a. HasCallStack => a
undefined
{-# INLINE count #-}
count ::
Int -> Fold m b c -> Parser m a b -> Parser m a c
count :: Int -> Fold m b c -> Parser m a b -> Parser m a c
count Int
n = Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
forall (m :: * -> *) b c a.
Int -> Int -> Fold m b c -> Parser m a b -> Parser m a c
countBetween Int
n Int
n
data ManyTillState fs sr sl = ManyTillR Int fs sr | ManyTillL fs sl
{-# INLINE manyTill #-}
manyTill :: MonadCatch m
=> Fold m b c -> Parser m a b -> Parser m a x -> Parser m a c
manyTill :: 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
_) =
(ManyTillState s s s -> a -> m (Step (ManyTillState s s s) c))
-> m (ManyTillState s s s)
-> (ManyTillState s s s -> m c)
-> Parser m a c
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 m (ManyTillState s s s)
forall sl. m (ManyTillState s s sl)
initial ManyTillState s s s -> m c
forall sr. ManyTillState s sr s -> m c
extract
where
initial :: m (ManyTillState s s sl)
initial = do
s
fs <- m s
finitial
Int -> s -> s -> ManyTillState s s sl
forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs (s -> ManyTillState s s sl) -> m s -> m (ManyTillState s s sl)
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 -> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Yield Int
n (Int -> s -> s -> ManyTillState s s s
forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs s
s)
Skip Int
n s
s -> do
Bool -> m () -> m ()
forall a. HasCallStack => Bool -> a -> a
assert (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0) (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Skip Int
n (Int -> s -> s -> ManyTillState s s s
forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
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
Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> c -> Step (ManyTillState s s s) c
forall s b. Int -> b -> Step s b
Stop Int
n c
b
Error String
_ -> do
s
rR <- m s
initialL
Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Skip (Int
cnt Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (s -> s -> ManyTillState s s s
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 -> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Yield Int
n (s -> s -> ManyTillState s s s
forall fs sr sl. fs -> sl -> ManyTillState fs sr sl
ManyTillL s
fs s
s)
Skip Int
n s
s -> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Skip Int
n (s -> s -> ManyTillState s s s
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
Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ Int -> ManyTillState s s s -> Step (ManyTillState s s s) c
forall s b. Int -> s -> Step s b
Skip Int
n (Int -> s -> s -> ManyTillState s s s
forall fs sr sl. Int -> fs -> sr -> ManyTillState fs sr sl
ManyTillR Int
0 s
fs1 s
l)
Error String
err -> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall (m :: * -> *) a. Monad m => a -> m a
return (Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c))
-> Step (ManyTillState s s s) c -> m (Step (ManyTillState s s s) c)
forall a b. (a -> b) -> a -> b
$ String -> Step (ManyTillState s s s) c
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 m b -> (b -> m s) -> m s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= s -> b -> m s
fstep s
fs m s -> (s -> m c) -> m c
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