{-# LANGUAGE BangPatterns #-}
module Data.Binary.Parser
(
Parser
, parseOnly
, parseLazy
, parseDetail
, parseDetailLazy
, parse
, maybeDecoder
, eitherDecoder
, (<?>)
, endOfInput
, option
, eitherP
, match
, many'
, some'
, sepBy
, sepBy'
, sepBy1
, sepBy1'
, manyTill
, manyTill'
, skipMany
, skipMany1
, module Data.Binary.Get
, module Data.Binary.Parser.Word8
, module Data.Binary.Parser.Numeric
) where
import Control.Applicative
import Control.Monad
import Data.Binary.Get
import qualified Data.Binary.Get.Internal as I
import Data.Binary.Parser.Numeric
import Data.Binary.Parser.Word8
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as L (ByteString(..))
type Parser a = Get a
parseOnly :: Get a -> B.ByteString -> Either String a
parseOnly :: Get a -> ByteString -> Either String a
parseOnly g :: Get a
g bs :: ByteString
bs =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
bs) of
Fail _ _ err :: String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err
Done _ _ a :: a
a -> a -> Either String a
forall a b. b -> Either a b
Right a
a
_ -> String -> Either String a
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
{-# INLINE parseOnly #-}
parseLazy :: Get a -> L.ByteString -> Either String a
parseLazy :: Get a -> ByteString -> Either String a
parseLazy g :: Get a
g (L.Chunk bs :: ByteString
bs lbs :: ByteString
lbs) =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Decoder a -> ByteString -> Decoder a
forall a. Decoder a -> ByteString -> Decoder a
pushChunks (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
bs) ByteString
lbs) of
Fail _ _ err :: String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err
Done _ _ a :: a
a -> a -> Either String a
forall a b. b -> Either a b
Right a
a
_ -> String -> Either String a
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
parseLazy g :: Get a
g L.Empty =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
B.empty) of
Fail _ _ err :: String
err -> String -> Either String a
forall a b. a -> Either a b
Left String
err
Done _ _ a :: a
a -> a -> Either String a
forall a b. b -> Either a b
Right a
a
_ -> String -> Either String a
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
{-# INLINE parseLazy #-}
parseDetail :: Get a
-> B.ByteString
-> Either (B.ByteString, ByteOffset, String) (B.ByteString, ByteOffset, a)
parseDetail :: Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
parseDetail g :: Get a
g bs :: ByteString
bs =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
bs) of
Fail rest :: ByteString
rest offset :: ByteOffset
offset err :: String
err -> (ByteString, ByteOffset, String)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. a -> Either a b
Left (ByteString
rest, ByteOffset
offset, String
err)
Done rest :: ByteString
rest offset :: ByteOffset
offset a :: a
a -> (ByteString, ByteOffset, a)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. b -> Either a b
Right (ByteString
rest, ByteOffset
offset, a
a)
_ -> String
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
{-# INLINE parseDetail #-}
parseDetailLazy :: Get a
-> L.ByteString
-> Either (B.ByteString, ByteOffset, String) (B.ByteString, ByteOffset, a)
parseDetailLazy :: Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
parseDetailLazy g :: Get a
g (L.Chunk bs :: ByteString
bs lbs :: ByteString
lbs) =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Decoder a -> ByteString -> Decoder a
forall a. Decoder a -> ByteString -> Decoder a
pushChunks (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
bs) ByteString
lbs) of
Fail rest :: ByteString
rest offset :: ByteOffset
offset err :: String
err -> (ByteString, ByteOffset, String)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. a -> Either a b
Left (ByteString
rest, ByteOffset
offset, String
err)
Done rest :: ByteString
rest offset :: ByteOffset
offset a :: a
a -> (ByteString, ByteOffset, a)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. b -> Either a b
Right (ByteString
rest, ByteOffset
offset, a
a)
_ -> String
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
parseDetailLazy g :: Get a
g L.Empty =
case Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
pushEndOfInput (Get a -> ByteString -> Decoder a
forall a. Get a -> ByteString -> Decoder a
parse Get a
g ByteString
B.empty) of
Fail rest :: ByteString
rest offset :: ByteOffset
offset err :: String
err -> (ByteString, ByteOffset, String)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. a -> Either a b
Left (ByteString
rest, ByteOffset
offset, String
err)
Done rest :: ByteString
rest offset :: ByteOffset
offset a :: a
a -> (ByteString, ByteOffset, a)
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a b. b -> Either a b
Right (ByteString
rest, ByteOffset
offset, a
a)
_ -> String
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
forall a. HasCallStack => String -> a
error "parseOnly: impossible error!"
{-# INLINE parseDetailLazy #-}
parse :: Get a -> B.ByteString -> Decoder a
parse :: Get a -> ByteString -> Decoder a
parse g :: Get a
g bs :: ByteString
bs = Decoder a -> ByteOffset -> Decoder a
forall a. Decoder a -> ByteOffset -> Decoder a
calculateOffset (Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
loop (Get a -> ByteString -> Success a a -> Decoder a
forall a. Get a -> forall r. ByteString -> Success a r -> Decoder r
I.runCont Get a
g ByteString
bs Success a a
forall a. ByteString -> a -> Decoder a
I.Done)) (Int -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> ByteOffset) -> Int -> ByteOffset
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
B.length ByteString
bs)
where
calculateOffset :: Decoder a -> ByteOffset -> Decoder a
calculateOffset r :: Decoder a
r !ByteOffset
acc = case Decoder a
r of
I.Done inp :: ByteString
inp a :: a
a -> ByteString -> ByteOffset -> a -> Decoder a
forall a. ByteString -> ByteOffset -> a -> Decoder a
Done ByteString
inp (ByteOffset
acc ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- Int -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
inp)) a
a
I.Fail inp :: ByteString
inp s :: String
s -> ByteString -> ByteOffset -> String -> Decoder a
forall a. ByteString -> ByteOffset -> String -> Decoder a
Fail ByteString
inp (ByteOffset
acc ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- Int -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
inp)) String
s
I.Partial k :: Maybe ByteString -> Decoder a
k -> (Maybe ByteString -> Decoder a) -> Decoder a
forall a. (Maybe ByteString -> Decoder a) -> Decoder a
Partial ((Maybe ByteString -> Decoder a) -> Decoder a)
-> (Maybe ByteString -> Decoder a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \ms :: Maybe ByteString
ms -> case Maybe ByteString
ms of
Nothing -> Decoder a -> ByteOffset -> Decoder a
calculateOffset (Maybe ByteString -> Decoder a
k Maybe ByteString
forall a. Maybe a
Nothing) ByteOffset
acc
Just i :: ByteString
i -> Decoder a -> ByteOffset -> Decoder a
calculateOffset (Maybe ByteString -> Decoder a
k Maybe ByteString
ms) (ByteOffset
acc ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
+ Int -> ByteOffset
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
B.length ByteString
i))
I.BytesRead unused :: ByteOffset
unused k :: ByteOffset -> Decoder a
k -> Decoder a -> ByteOffset -> Decoder a
calculateOffset (ByteOffset -> Decoder a
k (ByteOffset -> Decoder a) -> ByteOffset -> Decoder a
forall a b. (a -> b) -> a -> b
$! (ByteOffset
acc ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- ByteOffset
unused)) ByteOffset
acc
loop :: Decoder a -> Decoder a
loop r :: Decoder a
r = case Decoder a
r of
I.Partial k :: Maybe ByteString -> Decoder a
k -> (Maybe ByteString -> Decoder a) -> Decoder a
forall a. (Maybe ByteString -> Decoder a) -> Decoder a
I.Partial ((Maybe ByteString -> Decoder a) -> Decoder a)
-> (Maybe ByteString -> Decoder a) -> Decoder a
forall a b. (a -> b) -> a -> b
$ \ms :: Maybe ByteString
ms -> case Maybe ByteString
ms of Just _ -> Decoder a -> Decoder a
loop (Maybe ByteString -> Decoder a
k Maybe ByteString
ms)
Nothing -> Decoder a -> Decoder a
forall a. Decoder a -> Decoder a
completeLoop (Maybe ByteString -> Decoder a
k Maybe ByteString
ms)
I.BytesRead n :: ByteOffset
n k :: ByteOffset -> Decoder a
k -> ByteOffset -> (ByteOffset -> Decoder a) -> Decoder a
forall a. ByteOffset -> (ByteOffset -> Decoder a) -> Decoder a
I.BytesRead ByteOffset
n (Decoder a -> Decoder a
loop (Decoder a -> Decoder a)
-> (ByteOffset -> Decoder a) -> ByteOffset -> Decoder a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Decoder a
k)
I.Done _ _ -> Decoder a
r
I.Fail _ _ -> Decoder a
r
completeLoop :: Decoder a -> Decoder a
completeLoop r :: Decoder a
r = case Decoder a
r of
I.Partial k :: Maybe ByteString -> Decoder a
k -> Decoder a -> Decoder a
completeLoop (Maybe ByteString -> Decoder a
k Maybe ByteString
forall a. Maybe a
Nothing)
I.BytesRead n :: ByteOffset
n k :: ByteOffset -> Decoder a
k -> ByteOffset -> (ByteOffset -> Decoder a) -> Decoder a
forall a. ByteOffset -> (ByteOffset -> Decoder a) -> Decoder a
I.BytesRead ByteOffset
n (Decoder a -> Decoder a
completeLoop (Decoder a -> Decoder a)
-> (ByteOffset -> Decoder a) -> ByteOffset -> Decoder a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Decoder a
k)
I.Fail _ _ -> Decoder a
r
I.Done _ _ -> Decoder a
r
maybeDecoder :: Decoder r -> Maybe r
maybeDecoder :: Decoder r -> Maybe r
maybeDecoder (Done _ _ r :: r
r) = r -> Maybe r
forall a. a -> Maybe a
Just r
r
maybeDecoder _ = Maybe r
forall a. Maybe a
Nothing
{-# INLINE maybeDecoder #-}
eitherDecoder :: Decoder r -> Either String r
eitherDecoder :: Decoder r -> Either String r
eitherDecoder (Done _ _ r :: r
r) = r -> Either String r
forall a b. b -> Either a b
Right r
r
eitherDecoder (Fail _ _ msg :: String
msg) = String -> Either String r
forall a b. a -> Either a b
Left String
msg
eitherDecoder _ = String -> Either String r
forall a b. a -> Either a b
Left "Decoder: incomplete input"
{-# INLINE eitherDecoder #-}
(<?>) :: Get a -> String -> Get a
<?> :: Get a -> String -> Get a
(<?>) = (String -> Get a -> Get a) -> Get a -> String -> Get a
forall a b c. (a -> b -> c) -> b -> a -> c
flip String -> Get a -> Get a
forall a. String -> Get a -> Get a
label
infix 0 <?>
{-# INLINE (<?>) #-}
endOfInput :: Get ()
endOfInput :: Get ()
endOfInput = do
Bool
e <- Get Bool
isEmpty
Bool -> Get () -> Get ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
e (String -> Get ()
forall (m :: * -> *) a. MonadFail m => String -> m a
fail "endOfInput")
{-# INLINE endOfInput #-}
option :: Alternative f => a -> f a -> f a
option :: a -> f a -> f a
option x :: a
x p :: f a
p = f a
p f a -> f a -> f a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# SPECIALIZE option :: a -> Get a -> Get a #-}
eitherP :: (Alternative f) => f a -> f b -> f (Either a b)
eitherP :: f a -> f b -> f (Either a b)
eitherP a :: f a
a b :: f b
b = (a -> Either a b
forall a b. a -> Either a b
Left (a -> Either a b) -> f a -> f (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
a) f (Either a b) -> f (Either a b) -> f (Either a b)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (b -> Either a b
forall a b. b -> Either a b
Right (b -> Either a b) -> f b -> f (Either a b)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f b
b)
{-# INLINE eitherP #-}
match :: Get a -> Get (B.ByteString, a)
match :: Get a -> Get (ByteString, a)
match p :: Get a
p = do
ByteOffset
pos1 <- Get ByteOffset
bytesRead
(x :: a
x, pos2 :: ByteOffset
pos2) <- Get (a, ByteOffset) -> Get (a, ByteOffset)
forall a. Get a -> Get a
lookAhead (Get (a, ByteOffset) -> Get (a, ByteOffset))
-> Get (a, ByteOffset) -> Get (a, ByteOffset)
forall a b. (a -> b) -> a -> b
$ (,) (a -> ByteOffset -> (a, ByteOffset))
-> Get a -> Get (ByteOffset -> (a, ByteOffset))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get a
p Get (ByteOffset -> (a, ByteOffset))
-> Get ByteOffset -> Get (a, ByteOffset)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get ByteOffset
bytesRead
(,) (ByteString -> a -> (ByteString, a))
-> Get ByteString -> Get (a -> (ByteString, a))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Get ByteString
getByteString (Int -> Get ByteString)
-> (ByteOffset -> Int) -> ByteOffset -> Get ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral) (ByteOffset
pos2 ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- ByteOffset
pos1) Get (a -> (ByteString, a)) -> Get a -> Get (ByteString, a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> a -> Get a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
x
{-# INLINE match #-}
liftM2' :: (Monad m) => (a -> b -> c) -> m a -> m b -> m c
liftM2' :: (a -> b -> c) -> m a -> m b -> m c
liftM2' f :: a -> b -> c
f a :: m a
a b :: m b
b = do
!a
x <- m a
a
b
y <- m b
b
c -> m c
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b -> c
f a
x b
y)
{-# INLINE liftM2' #-}
many' :: (MonadPlus m) => m a -> m [a]
many' :: m a -> m [a]
many' p :: m a
p = m [a]
many_p
where many_p :: m [a]
many_p = m [a]
some_p m [a] -> m [a] -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
some_p :: m [a]
some_p = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> m a -> m b -> m c
liftM2' (:) m a
p m [a]
many_p
{-# INLINE many' #-}
some' :: (MonadPlus m) => m a -> m [a]
some' :: m a -> m [a]
some' p :: m a
p = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> m a -> m b -> m c
liftM2' (:) m a
p (m a -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many' m a
p)
{-# INLINE some' #-}
sepBy :: Alternative f => f a -> f s -> f [a]
sepBy :: f a -> f s -> f [a]
sepBy p :: f a
p s :: f s
s = (a -> [a] -> [a]) -> f a -> f [a] -> f [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) f a
p ((f s
s f s -> f [a] -> f [a]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f a -> f s -> f [a]
forall (f :: * -> *) a s. Alternative f => f a -> f s -> f [a]
sepBy1 f a
p f s
s) f [a] -> f [a] -> f [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> f [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) f [a] -> f [a] -> f [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> f [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
{-# SPECIALIZE sepBy :: Get a -> Get s -> Get [a] #-}
sepBy' :: (MonadPlus m) => m a -> m s -> m [a]
sepBy' :: m a -> m s -> m [a]
sepBy' p :: m a
p s :: m s
s = m [a]
go m [a] -> m [a] -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []
where go :: m [a]
go = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> m a -> m b -> m c
liftM2' (:) m a
p ((m s
s m s -> m [a] -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m a -> m s -> m [a]
forall (m :: * -> *) a s. MonadPlus m => m a -> m s -> m [a]
sepBy1' m a
p m s
s) m [a] -> m [a] -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [])
{-# SPECIALIZE sepBy' :: Get a -> Get s -> Get [a] #-}
sepBy1 :: Alternative f => f a -> f s -> f [a]
sepBy1 :: f a -> f s -> f [a]
sepBy1 p :: f a
p s :: f s
s = f [a]
go
where go :: f [a]
go = (a -> [a] -> [a]) -> f a -> f [a] -> f [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) f a
p ((f s
s f s -> f [a] -> f [a]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f [a]
go) f [a] -> f [a] -> f [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> f [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
{-# SPECIALIZE sepBy1 :: Get a -> Get s -> Get [a] #-}
sepBy1' :: (MonadPlus m) => m a -> m s -> m [a]
sepBy1' :: m a -> m s -> m [a]
sepBy1' p :: m a
p s :: m s
s = m [a]
go
where go :: m [a]
go = (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> m a -> m b -> m c
liftM2' (:) m a
p ((m s
s m s -> m [a] -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m [a]
go) m [a] -> m [a] -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return [])
{-# SPECIALIZE sepBy1' :: Get a -> Get s -> Get [a] #-}
manyTill :: Alternative f => f a -> f b -> f [a]
manyTill :: f a -> f b -> f [a]
manyTill p :: f a
p end :: f b
end = f [a]
go
where go :: f [a]
go = (f b
end f b -> f [a] -> f [a]
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> [a] -> f [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []) f [a] -> f [a] -> f [a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (a -> [a] -> [a]) -> f a -> f [a] -> f [a]
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:) f a
p f [a]
go
{-# SPECIALIZE manyTill :: Get a -> Get b -> Get [a] #-}
manyTill' :: (MonadPlus m) => m a -> m b -> m [a]
manyTill' :: m a -> m b -> m [a]
manyTill' p :: m a
p end :: m b
end = m [a]
go
where go :: m [a]
go = (m b
end m b -> m [a] -> m [a]
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> [a] -> m [a]
forall (m :: * -> *) a. Monad m => a -> m a
return []) m [a] -> m [a] -> m [a]
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` (a -> [a] -> [a]) -> m a -> m [a] -> m [a]
forall (m :: * -> *) a b c.
Monad m =>
(a -> b -> c) -> m a -> m b -> m c
liftM2' (:) m a
p m [a]
go
{-# SPECIALIZE manyTill' :: Get a -> Get b -> Get [a] #-}
skipMany :: Alternative f => f a -> f ()
skipMany :: f a -> f ()
skipMany p :: f a
p = f ()
go
where go :: f ()
go = (f a
p f a -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f ()
go) f () -> f () -> f ()
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> () -> f ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
{-# SPECIALIZE skipMany :: Get a -> Get () #-}
skipMany1 :: Alternative f => f a -> f ()
skipMany1 :: f a -> f ()
skipMany1 p :: f a
p = f a
p f a -> f () -> f ()
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> f a -> f ()
forall (f :: * -> *) a. Alternative f => f a -> f ()
skipMany f a
p
{-# SPECIALIZE skipMany1 :: Get a -> Get () #-}