{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
module Replace.Megaparsec.Internal.ByteString
(
sepCapByteString
, anyTillByteString
)
where
import Control.Monad
import qualified Data.ByteString as B
import Text.Megaparsec
{-# INLINE [1] sepCapByteString #-}
sepCapByteString
:: forall e s m a. (MonadParsec e s m, s ~ B.ByteString)
=> m a
-> m [Either (Tokens s) a]
sepCapByteString :: m a -> m [Either (Tokens s) a]
sepCapByteString m a
sep = m ByteString
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput m ByteString
-> (ByteString -> m [Either ByteString a])
-> m [Either ByteString a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> m [Either ByteString a]
go
where
go :: ByteString -> m [Either ByteString a]
go ByteString
restBegin = do
m [Either ByteString a]
-> m [Either ByteString a] -> m [Either ByteString a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
ByteString
restThis <- m ByteString
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
Maybe (a, ByteString)
thisiter <- m (Maybe (a, ByteString))
-> m (Maybe (a, ByteString)) -> m (Maybe (a, ByteString))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
a
x <- m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m a
sep
ByteString
restAfter <- m ByteString
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
B.length ByteString
restAfter Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
B.length ByteString
restThis) m ()
forall (f :: * -> *) a. Alternative f => f a
empty
Maybe (a, ByteString) -> m (Maybe (a, ByteString))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (a, ByteString) -> m (Maybe (a, ByteString)))
-> Maybe (a, ByteString) -> m (Maybe (a, ByteString))
forall a b. (a -> b) -> a -> b
$ (a, ByteString) -> Maybe (a, ByteString)
forall a. a -> Maybe a
Just (a
x, ByteString
restAfter)
)
(m Word8
forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle m Word8 -> m (Maybe (a, ByteString)) -> m (Maybe (a, ByteString))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (a, ByteString) -> m (Maybe (a, ByteString))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (a, ByteString)
forall a. Maybe a
Nothing)
case Maybe (a, ByteString)
thisiter of
(Just (a
x, ByteString
restAfter)) | ByteString -> Int
B.length ByteString
restThis Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ByteString -> Int
B.length ByteString
restBegin -> do
let unmatched :: ByteString
unmatched = Int -> ByteString -> ByteString
B.take (ByteString -> Int
B.length ByteString
restBegin Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
restThis) ByteString
restBegin
(ByteString -> Either ByteString a
forall a b. a -> Either a b
Left ByteString
unmatchedEither ByteString a
-> [Either ByteString a] -> [Either ByteString a]
forall a. a -> [a] -> [a]
:) ([Either ByteString a] -> [Either ByteString a])
-> ([Either ByteString a] -> [Either ByteString a])
-> [Either ByteString a]
-> [Either ByteString a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Either ByteString a
forall a b. b -> Either a b
Right a
xEither ByteString a
-> [Either ByteString a] -> [Either ByteString a]
forall a. a -> [a] -> [a]
:) ([Either ByteString a] -> [Either ByteString a])
-> m [Either ByteString a] -> m [Either ByteString a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> m [Either ByteString a]
go ByteString
restAfter
(Just (a
x, ByteString
restAfter)) -> do
(a -> Either ByteString a
forall a b. b -> Either a b
Right a
xEither ByteString a
-> [Either ByteString a] -> [Either ByteString a]
forall a. a -> [a] -> [a]
:) ([Either ByteString a] -> [Either ByteString a])
-> m [Either ByteString a] -> m [Either ByteString a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> m [Either ByteString a]
go ByteString
restAfter
Maybe (a, ByteString)
Nothing -> ByteString -> m [Either ByteString a]
go ByteString
restBegin
)
( do
if ByteString -> Int
B.length ByteString
restBegin Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then
[Either ByteString a] -> m [Either ByteString a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [ByteString -> Either ByteString a
forall a b. a -> Either a b
Left ByteString
restBegin]
else [Either ByteString a] -> m [Either ByteString a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
)
{-# INLINE [1] anyTillByteString #-}
anyTillByteString
:: forall e s m a. (MonadParsec e s m, s ~ B.ByteString)
=> m a
-> m (Tokens s, a)
anyTillByteString :: m a -> m (Tokens s, a)
anyTillByteString m a
sep = do
ByteString
begin <- m ByteString
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
(ByteString
end, a
x) <- m (ByteString, a)
go
(ByteString, a) -> m (ByteString, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ByteString -> ByteString
B.take (ByteString -> Int
B.length ByteString
begin Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
end) ByteString
begin, a
x)
where
go :: m (ByteString, a)
go = do
ByteString
end <- m ByteString
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
Maybe a
r <- m a -> m (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (m a -> m (Maybe a)) -> m a -> m (Maybe a)
forall a b. (a -> b) -> a -> b
$ m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m a
sep
case Maybe a
r of
Maybe a
Nothing -> m Word8
forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle m Word8 -> m (ByteString, a) -> m (ByteString, a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m (ByteString, a)
go
Just a
x -> (ByteString, a) -> m (ByteString, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString
end, a
x)