{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
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 :: forall e s (m :: * -> *) a.
(MonadParsec e s m, s ~ ByteString) =>
m a -> m [Either (Tokens s) a]
sepCapByteString m a
sep = forall e s (m :: * -> *). MonadParsec e s m => m s
getInput 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
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
ByteString
restThis <- forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
Maybe (a, ByteString)
thisiter <- forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
a
x <- forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m a
sep
ByteString
restAfter <- forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ByteString -> Int
B.length ByteString
restAfter forall a. Ord a => a -> a -> Bool
>= ByteString -> Int
B.length ByteString
restThis) forall (f :: * -> *) a. Alternative f => f a
empty
forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (a
x, ByteString
restAfter)
)
(forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Maybe a
Nothing)
case Maybe (a, ByteString)
thisiter of
(Just (a
x, ByteString
restAfter)) | ByteString -> Int
B.length ByteString
restThis 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 forall a. Num a => a -> a -> a
- ByteString -> Int
B.length ByteString
restThis) ByteString
restBegin
(forall a b. a -> Either a b
Left ByteString
unmatchedforall a. a -> [a] -> [a]
:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a b. b -> Either a b
Right a
xforall a. a -> [a] -> [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
(forall a b. b -> Either a b
Right a
xforall a. a -> [a] -> [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 forall a. Ord a => a -> a -> Bool
> Int
0 then
forall (f :: * -> *) a. Applicative f => a -> f a
pure [forall a b. a -> Either a b
Left ByteString
restBegin]
else 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 :: forall e s (m :: * -> *) a.
(MonadParsec e s m, s ~ ByteString) =>
m a -> m (Tokens s, a)
anyTillByteString m a
sep = do
ByteString
begin <- forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
(ByteString
end, a
x) <- m (ByteString, a)
go
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ByteString -> ByteString
B.take (ByteString -> Int
B.length ByteString
begin 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 <- forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
Maybe a
r <- forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional forall a b. (a -> b) -> a -> b
$ 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 -> forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> m (ByteString, a)
go
Just a
x -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString
end, a
x)