{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE TupleSections #-}
module Replace.Megaparsec
(
breakCap
, breakCapT
, splitCap
, splitCapT
, streamEdit
, streamEditT
, anyTill
, sepCap
, findAll
, findAllCap
)
where
import Data.Bifunctor
import Data.Functor.Identity
import Data.Proxy
import Control.Monad
import qualified Data.ByteString as B
import qualified Data.Text as T
import Text.Megaparsec
import Replace.Megaparsec.Internal.ByteString
import Replace.Megaparsec.Internal.Text
breakCap
:: forall e s a. (Ord e, Stream s, Tokens s ~ s)
=> Parsec e s a
-> s
-> Maybe (s, a, s)
breakCap :: Parsec e s a -> s -> Maybe (s, a, s)
breakCap Parsec e s a
sep s
input = Identity (Maybe (s, a, s)) -> Maybe (s, a, s)
forall a. Identity a -> a
runIdentity (Identity (Maybe (s, a, s)) -> Maybe (s, a, s))
-> Identity (Maybe (s, a, s)) -> Maybe (s, a, s)
forall a b. (a -> b) -> a -> b
$ Parsec e s a -> s -> Identity (Maybe (s, a, s))
forall (m :: * -> *) e s a.
(Ord e, Stream s, Tokens s ~ s, Monad m) =>
ParsecT e s m a -> s -> m (Maybe (s, a, s))
breakCapT Parsec e s a
sep s
input
{-# INLINABLE breakCap #-}
breakCapT
:: forall m e s a. (Ord e, Stream s, Tokens s ~ s, Monad m)
=> ParsecT e s m a
-> s
-> m (Maybe (s, a, s))
breakCapT :: ParsecT e s m a -> s -> m (Maybe (s, a, s))
breakCapT ParsecT e s m a
sep s
input =
ParsecT e s m (s, a, s)
-> String -> s -> m (Either (ParseErrorBundle s e) (s, a, s))
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> String -> s -> m (Either (ParseErrorBundle s e) a)
runParserT ParsecT e s m (s, a, s)
pser String
"" s
input m (Either (ParseErrorBundle s e) (s, a, s))
-> (Either (ParseErrorBundle s e) (s, a, s) -> m (Maybe (s, a, s)))
-> m (Maybe (s, a, s))
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
(Left ParseErrorBundle s e
_) -> Maybe (s, a, s) -> m (Maybe (s, a, s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (s, a, s)
forall a. Maybe a
Nothing
(Right (s, a, s)
x) -> Maybe (s, a, s) -> m (Maybe (s, a, s))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (s, a, s) -> m (Maybe (s, a, s)))
-> Maybe (s, a, s) -> m (Maybe (s, a, s))
forall a b. (a -> b) -> a -> b
$ (s, a, s) -> Maybe (s, a, s)
forall a. a -> Maybe a
Just (s, a, s)
x
where
pser :: ParsecT e s m (s, a, s)
pser = do
(s
prefix, a
cap) <- ParsecT e s m a -> ParsecT e s m (Tokens s, a)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m (Tokens s, a)
anyTill ParsecT e s m a
sep
s
suffix <- ParsecT e s m s
forall e s (m :: * -> *). MonadParsec e s m => m s
getInput
(s, a, s) -> ParsecT e s m (s, a, s)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (s
prefix, a
cap, s
suffix)
{-# INLINABLE breakCapT #-}
splitCap
:: forall e s a. (Ord e, Stream s, Tokens s ~ s)
=> Parsec e s a
-> s
-> [Either s a]
splitCap :: Parsec e s a -> s -> [Either s a]
splitCap Parsec e s a
sep s
input = Identity [Either s a] -> [Either s a]
forall a. Identity a -> a
runIdentity (Identity [Either s a] -> [Either s a])
-> Identity [Either s a] -> [Either s a]
forall a b. (a -> b) -> a -> b
$ Parsec e s a -> s -> Identity [Either s a]
forall e s (m :: * -> *) a.
(Ord e, Stream s, Tokens s ~ s, Monad m) =>
ParsecT e s m a -> s -> m [Either s a]
splitCapT Parsec e s a
sep s
input
{-# INLINABLE splitCap #-}
splitCapT
:: forall e s m a. (Ord e, Stream s, Tokens s ~ s, Monad m)
=> ParsecT e s m a
-> s
-> m [Either s a]
splitCapT :: ParsecT e s m a -> s -> m [Either s a]
splitCapT ParsecT e s m a
sep s
input =
ParsecT e s m [Either s a]
-> String -> s -> m (Either (ParseErrorBundle s e) [Either s a])
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> String -> s -> m (Either (ParseErrorBundle s e) a)
runParserT (ParsecT e s m a -> ParsecT e s m [Either (Tokens s) a]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m [Either (Tokens s) a]
sepCap ParsecT e s m a
sep) String
"" s
input m (Either (ParseErrorBundle s e) [Either s a])
-> (Either (ParseErrorBundle s e) [Either s a] -> m [Either s a])
-> m [Either s a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
(Left ParseErrorBundle s e
_) -> m [Either s a]
forall a. HasCallStack => a
undefined
(Right [Either s a]
r) -> [Either s a] -> m [Either s a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Either s a]
r
{-# INLINABLE splitCapT #-}
streamEdit
:: forall e s a. (Ord e, Stream s, Monoid s, Tokens s ~ s)
=> Parsec e s a
-> (a -> s)
-> s
-> s
streamEdit :: Parsec e s a -> (a -> s) -> s -> s
streamEdit Parsec e s a
sep a -> s
editor = Identity s -> s
forall a. Identity a -> a
runIdentity (Identity s -> s) -> (s -> Identity s) -> s -> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parsec e s a -> (a -> Identity s) -> s -> Identity s
forall e s (m :: * -> *) a.
(Ord e, Stream s, Monad m, Monoid s, Tokens s ~ s) =>
ParsecT e s m a -> (a -> m s) -> s -> m s
streamEditT Parsec e s a
sep (s -> Identity s
forall a. a -> Identity a
Identity (s -> Identity s) -> (a -> s) -> a -> Identity s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> s
editor)
{-# INLINABLE streamEdit #-}
streamEditT
:: forall e s m a. (Ord e, Stream s, Monad m, Monoid s, Tokens s ~ s)
=> ParsecT e s m a
-> (a -> m s)
-> s
-> m s
streamEditT :: ParsecT e s m a -> (a -> m s) -> s -> m s
streamEditT ParsecT e s m a
sep a -> m s
editor s
input = do
ParsecT e s m [Either s a]
-> String -> s -> m (Either (ParseErrorBundle s e) [Either s a])
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> String -> s -> m (Either (ParseErrorBundle s e) a)
runParserT (ParsecT e s m a -> ParsecT e s m [Either (Tokens s) a]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m [Either (Tokens s) a]
sepCap ParsecT e s m a
sep) String
"" s
input m (Either (ParseErrorBundle s e) [Either s a])
-> (Either (ParseErrorBundle s e) [Either s a] -> m s) -> m s
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
(Left ParseErrorBundle s e
_) -> m s
forall a. HasCallStack => a
undefined
(Right [Either s a]
r) -> [s] -> s
forall a. Monoid a => [a] -> a
mconcat ([s] -> s) -> m [s] -> m s
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Either s a -> m s) -> [Either s a] -> m [s]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((s -> m s) -> (a -> m s) -> Either s a -> m s
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either s -> m s
forall (m :: * -> *) a. Monad m => a -> m a
return a -> m s
editor) [Either s a]
r
{-# INLINABLE streamEditT #-}
anyTill
:: forall e s m a. (MonadParsec e s m)
=> m a
-> m (Tokens s, a)
anyTill :: m a -> m (Tokens s, a)
anyTill m a
sep = do
([Token s]
as, a
end) <- m (Token s) -> m a -> m ([Token s], a)
forall (m :: * -> *) a end.
MonadPlus m =>
m a -> m end -> m ([a], end)
manyTill_ m (Token s)
forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle m a
sep
(Tokens s, a) -> m (Tokens s, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Proxy s -> [Token s] -> Tokens s
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy s
forall k (t :: k). Proxy t
Proxy::Proxy s) [Token s]
as, a
end)
{-# INLINE [1] anyTill #-}
#if MIN_VERSION_GLASGOW_HASKELL(8,8,1,0)
{-# RULES "anyTill/ByteString" [2]
forall e. forall.
anyTill @e @B.ByteString =
anyTillByteString @e @B.ByteString #-}
{-# RULES "anyTill/Text" [2]
forall e. forall.
anyTill @e @T.Text =
anyTillText @e @T.Text #-}
#elif MIN_VERSION_GLASGOW_HASKELL(8,0,2,0)
{-# RULES "anyTill/ByteString" [2]
forall (pa :: ParsecT e B.ByteString m a).
anyTill @e @B.ByteString @(ParsecT e B.ByteString m) @a pa =
anyTillByteString @e @B.ByteString @(ParsecT e B.ByteString m) @a pa #-}
{-# RULES "anyTill/Text" [2]
forall (pa :: ParsecT e T.Text m a).
anyTill @e @T.Text @(ParsecT e T.Text m) @a pa =
anyTillText @e @T.Text @(ParsecT e T.Text m) @a pa #-}
#endif
sepCap
:: forall e s m a. (MonadParsec e s m)
=> m a
-> m [Either (Tokens s) a]
sepCap :: m a -> m [Either (Tokens s) a]
sepCap m a
sep = (([Either [Token s] a] -> [Either (Tokens s) a])
-> m [Either [Token s] a] -> m [Either (Tokens s) a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(([Either [Token s] a] -> [Either (Tokens s) a])
-> m [Either [Token s] a] -> m [Either (Tokens s) a])
-> ((Either [Token s] a -> Either (Tokens s) a)
-> [Either [Token s] a] -> [Either (Tokens s) a])
-> (Either [Token s] a -> Either (Tokens s) a)
-> m [Either [Token s] a]
-> m [Either (Tokens s) a]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Either [Token s] a -> Either (Tokens s) a)
-> [Either [Token s] a] -> [Either (Tokens s) a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (([Token s] -> Tokens s)
-> Either [Token s] a -> Either (Tokens s) a
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (([Token s] -> Tokens s)
-> Either [Token s] a -> Either (Tokens s) a)
-> ([Token s] -> Tokens s)
-> Either [Token s] a
-> Either (Tokens s) a
forall a b. (a -> b) -> a -> b
$ Proxy s -> [Token s] -> Tokens s
forall s. Stream s => Proxy s -> [Token s] -> Tokens s
tokensToChunk (Proxy s
forall k (t :: k). Proxy t
Proxy::Proxy s))
(m [Either [Token s] a] -> m [Either (Tokens s) a])
-> m [Either [Token s] a] -> m [Either (Tokens s) a]
forall a b. (a -> b) -> a -> b
$ ([Either (Token s) a] -> [Either [Token s] a])
-> m [Either (Token s) a] -> m [Either [Token s] a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Either (Token s) a] -> [Either [Token s] a]
forall l r. [Either l r] -> [Either [l] r]
sequenceLeft
(m [Either (Token s) a] -> m [Either [Token s] a])
-> m [Either (Token s) a] -> m [Either [Token s] a]
forall a b. (a -> b) -> a -> b
$ m (Either (Token s) a) -> m [Either (Token s) a]
forall (m :: * -> *) a. MonadPlus m => m a -> m [a]
many (m (Either (Token s) a) -> m [Either (Token s) a])
-> m (Either (Token s) a) -> m [Either (Token s) a]
forall a b. (a -> b) -> a -> b
$ (a -> Either (Token s) a) -> m a -> m (Either (Token s) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Either (Token s) a
forall a b. b -> Either a b
Right (m a -> m a
forall e s (m :: * -> *) a. MonadParsec e s m => m a -> m a
try m a
nonZeroSep) m (Either (Token s) a)
-> m (Either (Token s) a) -> m (Either (Token s) a)
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Token s -> Either (Token s) a)
-> m (Token s) -> m (Either (Token s) a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Token s -> Either (Token s) a
forall a b. a -> Either a b
Left m (Token s)
forall e s (m :: * -> *). MonadParsec e s m => m (Token s)
anySingle
where
sequenceLeft :: [Either l r] -> [Either [l] r]
sequenceLeft :: [Either l r] -> [Either [l] r]
sequenceLeft = {-# SCC sequenceLeft #-} (Either l r -> [Either [l] r] -> [Either [l] r])
-> [Either [l] r] -> [Either l r] -> [Either [l] r]
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Either l r -> [Either [l] r] -> [Either [l] r]
forall l r. Either l r -> [Either [l] r] -> [Either [l] r]
consLeft []
where
consLeft :: Either l r -> [Either [l] r] -> [Either [l] r]
consLeft :: Either l r -> [Either [l] r] -> [Either [l] r]
consLeft (Left l
l) ((Left [l]
ls):[Either [l] r]
xs) = {-# SCC consLeft #-} ([l] -> Either [l] r
forall a b. a -> Either a b
Left (l
ll -> [l] -> [l]
forall a. a -> [a] -> [a]
:[l]
ls))Either [l] r -> [Either [l] r] -> [Either [l] r]
forall a. a -> [a] -> [a]
:[Either [l] r]
xs
consLeft (Left l
l) [Either [l] r]
xs = {-# SCC consLeft #-} ([l] -> Either [l] r
forall a b. a -> Either a b
Left [l
l])Either [l] r -> [Either [l] r] -> [Either [l] r]
forall a. a -> [a] -> [a]
:[Either [l] r]
xs
consLeft (Right r
r) [Either [l] r]
xs = {-# SCC consLeft #-} (r -> Either [l] r
forall a b. b -> Either a b
Right r
r)Either [l] r -> [Either [l] r] -> [Either [l] r]
forall a. a -> [a] -> [a]
:[Either [l] r]
xs
nonZeroSep :: m a
nonZeroSep = {-# SCC nonZeroSep #-} do
Int
offset1 <- m Int
forall e s (m :: * -> *). MonadParsec e s m => m Int
getOffset
a
x <- {-# SCC sep #-} m a
sep
Int
offset2 <- m Int
forall e s (m :: * -> *). MonadParsec e s m => m Int
getOffset
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
offset1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
offset2) m ()
forall (f :: * -> *) a. Alternative f => f a
empty
a -> m a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
{-# INLINE [1] sepCap #-}
#if MIN_VERSION_GLASGOW_HASKELL(8,8,1,0)
{-# RULES "sepCap/ByteString" [2]
forall e. forall.
sepCap @e @B.ByteString =
sepCapByteString @e @B.ByteString #-}
{-# RULES "sepCap/Text" [2]
forall e. forall.
sepCap @e @T.Text =
sepCapText @e @T.Text #-}
#elif MIN_VERSION_GLASGOW_HASKELL(8,0,2,0)
{-# RULES "sepCap/ByteString" [2]
forall (pa :: ParsecT e B.ByteString m a).
sepCap @e @B.ByteString @(ParsecT e B.ByteString m) @a pa =
sepCapByteString @e @B.ByteString @(ParsecT e B.ByteString m) @a pa #-}
{-# RULES "sepCap/Text" [2]
forall (pa :: ParsecT e T.Text m a).
sepCap @e @T.Text @(ParsecT e T.Text m) @a pa =
sepCapText @e @T.Text @(ParsecT e T.Text m) @a pa #-}
#endif
findAllCap
:: MonadParsec e s m
=> m a
-> m [Either (Tokens s) (Tokens s, a)]
findAllCap :: m a -> m [Either (Tokens s) (Tokens s, a)]
findAllCap m a
sep = m (Tokens s, a) -> m [Either (Tokens s) (Tokens s, a)]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m [Either (Tokens s) a]
sepCap (m a -> m (Tokens s, a)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m (Tokens s, a)
match m a
sep)
{-# INLINABLE findAllCap #-}
{-# DEPRECATED findAllCap "replace with `findAllCap sep = sepCap (match sep)`" #-}
findAll
:: MonadParsec e s m
=> m a
-> m [Either (Tokens s) (Tokens s)]
findAll :: m a -> m [Either (Tokens s) (Tokens s)]
findAll m a
sep = (([Either (Tokens s) (Tokens s, a)]
-> [Either (Tokens s) (Tokens s)])
-> m [Either (Tokens s) (Tokens s, a)]
-> m [Either (Tokens s) (Tokens s)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(([Either (Tokens s) (Tokens s, a)]
-> [Either (Tokens s) (Tokens s)])
-> m [Either (Tokens s) (Tokens s, a)]
-> m [Either (Tokens s) (Tokens s)])
-> ((Either (Tokens s) (Tokens s, a)
-> Either (Tokens s) (Tokens s))
-> [Either (Tokens s) (Tokens s, a)]
-> [Either (Tokens s) (Tokens s)])
-> (Either (Tokens s) (Tokens s, a)
-> Either (Tokens s) (Tokens s))
-> m [Either (Tokens s) (Tokens s, a)]
-> m [Either (Tokens s) (Tokens s)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Either (Tokens s) (Tokens s, a) -> Either (Tokens s) (Tokens s))
-> [Either (Tokens s) (Tokens s, a)]
-> [Either (Tokens s) (Tokens s)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (((Tokens s, a) -> Tokens s)
-> Either (Tokens s) (Tokens s, a) -> Either (Tokens s) (Tokens s)
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (Tokens s, a) -> Tokens s
forall a b. (a, b) -> a
fst) (m [Either (Tokens s) (Tokens s, a)]
-> m [Either (Tokens s) (Tokens s)])
-> m [Either (Tokens s) (Tokens s, a)]
-> m [Either (Tokens s) (Tokens s)]
forall a b. (a -> b) -> a -> b
$ m (Tokens s, a) -> m [Either (Tokens s) (Tokens s, a)]
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m [Either (Tokens s) a]
sepCap (m a -> m (Tokens s, a)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m (Tokens s, a)
match m a
sep)
{-# INLINABLE findAll #-}
{-# DEPRECATED findAll "replace with `findAll sep = (fmap.fmap) (second fst) $ sepCap (match sep)`" #-}