{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE BangPatterns #-}
module Replace.Attoparsec.Text
(
breakCap
, splitCap
, streamEdit
, streamEditT
, anyTill
, sepCap
, findAll
, findAllCap
)
where
import Data.Functor.Identity
import Data.Bifunctor
import Control.Applicative
import Control.Monad
import Data.Attoparsec.Text as A
import qualified Data.Text as T
import qualified Data.Text.Internal as TI
import qualified Data.Attoparsec.Internal.Types as AT
breakCap
:: Parser a
-> T.Text
-> Maybe (T.Text, a, T.Text)
breakCap :: Parser a -> Text -> Maybe (Text, a, Text)
breakCap Parser a
sep Text
input =
case Parser (Text, a, Text) -> Text -> Either String (Text, a, Text)
forall a. Parser a -> Text -> Either String a
parseOnly Parser (Text, a, Text)
pser Text
input of
(Left String
_) -> Maybe (Text, a, Text)
forall a. Maybe a
Nothing
(Right (Text, a, Text)
x) -> (Text, a, Text) -> Maybe (Text, a, Text)
forall a. a -> Maybe a
Just (Text, a, Text)
x
where
pser :: Parser (Text, a, Text)
pser = do
(Text
prefix, a
cap) <- Parser a -> Parser (Text, a)
forall a. Parser a -> Parser (Text, a)
anyTill Parser a
sep
Text
suffix <- Parser Text
A.takeText
(Text, a, Text) -> Parser (Text, a, Text)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
prefix, a
cap, Text
suffix)
{-# INLINABLE breakCap #-}
splitCap
:: Parser a
-> T.Text
-> [Either T.Text a]
splitCap :: Parser a -> Text -> [Either Text a]
splitCap Parser a
sep Text
input = do
case Parser [Either Text a] -> Text -> Either String [Either Text a]
forall a. Parser a -> Text -> Either String a
parseOnly (Parser a -> Parser [Either Text a]
forall a. Parser a -> Parser [Either Text a]
sepCap Parser a
sep) Text
input of
(Left String
_) -> [Either Text a]
forall a. HasCallStack => a
undefined
(Right [Either Text a]
r) -> [Either Text a]
r
{-# INLINABLE splitCap #-}
streamEdit
:: Parser a
-> (a -> T.Text)
-> T.Text
-> T.Text
streamEdit :: Parser a -> (a -> Text) -> Text -> Text
streamEdit Parser a
sep a -> Text
editor = Identity Text -> Text
forall a. Identity a -> a
runIdentity (Identity Text -> Text) -> (Text -> Identity Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser a -> (a -> Identity Text) -> Text -> Identity Text
forall (m :: * -> *) a.
Monad m =>
Parser a -> (a -> m Text) -> Text -> m Text
streamEditT Parser a
sep (Text -> Identity Text
forall a. a -> Identity a
Identity (Text -> Identity Text) -> (a -> Text) -> a -> Identity Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Text
editor)
{-# INLINABLE streamEdit #-}
streamEditT
:: (Monad m)
=> Parser a
-> (a -> m T.Text)
-> T.Text
-> m T.Text
streamEditT :: Parser a -> (a -> m Text) -> Text -> m Text
streamEditT Parser a
sep a -> m Text
editor Text
input = do
case Parser [Either Text a] -> Text -> Either String [Either Text a]
forall a. Parser a -> Text -> Either String a
parseOnly (Parser a -> Parser [Either Text a]
forall a. Parser a -> Parser [Either Text a]
sepCap Parser a
sep) Text
input of
(Left String
err) -> String -> m Text
forall a. HasCallStack => String -> a
error String
err
(Right [Either Text a]
r) -> ([Text] -> Text) -> m [Text] -> m Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat (m [Text] -> m Text) -> m [Text] -> m Text
forall a b. (a -> b) -> a -> b
$ (Either Text a -> m Text) -> [Either Text a] -> m [Text]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
traverse ((Text -> m Text) -> (a -> m Text) -> Either Text a -> m Text
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Text -> m Text
forall (m :: * -> *) a. Monad m => a -> m a
return a -> m Text
editor) [Either Text a]
r
{-# INLINABLE streamEditT #-}
anyTill
:: Parser a
-> Parser (T.Text, a)
anyTill :: Parser a -> Parser (Text, a)
anyTill Parser a
sep = do
Int
begin <- Parser Int
getOffset
(Int
end, a
x) <- Parser Text (Int, a)
go
Text
prefix <- Int -> Int -> Parser Text
substring Int
begin Int
end
(Text, a) -> Parser (Text, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text
prefix, a
x)
where
go :: Parser Text (Int, a)
go = do
Int
end <- Parser Int
getOffset
Maybe a
r <- Parser a -> Parser Text (Maybe a)
forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional (Parser a -> Parser Text (Maybe a))
-> Parser a -> Parser Text (Maybe a)
forall a b. (a -> b) -> a -> b
$ Parser a -> Parser a
forall i a. Parser i a -> Parser i a
try Parser a
sep
case Maybe a
r of
Maybe a
Nothing -> Parser Text Bool
forall t. Chunk t => Parser t Bool
atEnd Parser Text Bool
-> (Bool -> Parser Text (Int, a)) -> Parser Text (Int, a)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Bool
True -> Parser Text (Int, a)
forall (f :: * -> *) a. Alternative f => f a
empty
Bool
False -> Parser Char
anyChar Parser Char -> Parser Text (Int, a) -> Parser Text (Int, a)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Parser Text (Int, a)
go
Just a
x -> (Int, a) -> Parser Text (Int, a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int
end, a
x)
sepCap
:: Parser a
-> Parser [Either T.Text a]
sepCap :: Parser a -> Parser [Either Text a]
sepCap Parser a
sep = Parser Int
getOffset Parser Int
-> (Int -> Parser [Either Text a]) -> Parser [Either Text a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Parser [Either Text a]
go
where
go :: Int -> Parser [Either Text a]
go !Int
offsetBegin = do
!Int
offsetThis <- Parser Int
getOffset
Parser [Either Text a]
-> Parser [Either Text a] -> Parser [Either Text a]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
()
_ <- Parser Text ()
forall t. Chunk t => Parser t ()
endOfInput
if Int
offsetThis Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
offsetBegin
then
Int -> Int -> Parser Text
substring Int
offsetBegin Int
offsetThis Parser Text
-> (Text -> Parser [Either Text a]) -> Parser [Either Text a]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \Text
s -> [Either Text a] -> Parser [Either Text a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Text -> Either Text a
forall a b. a -> Either a b
Left Text
s]
else [Either Text a] -> Parser [Either Text a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []
)
( do
Maybe (a, Int)
thisiter <- Parser Text (Maybe (a, Int))
-> Parser Text (Maybe (a, Int)) -> Parser Text (Maybe (a, Int))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
( do
a
x <- Parser a -> Parser a
forall i a. Parser i a -> Parser i a
try Parser a
sep
!Int
offsetAfter <- Parser Int
getOffset
Bool -> Parser Text () -> Parser Text ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
offsetAfter Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
offsetThis) Parser Text ()
forall (f :: * -> *) a. Alternative f => f a
empty
Maybe (a, Int) -> Parser Text (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe (a, Int) -> Parser Text (Maybe (a, Int)))
-> Maybe (a, Int) -> Parser Text (Maybe (a, Int))
forall a b. (a -> b) -> a -> b
$ (a, Int) -> Maybe (a, Int)
forall a. a -> Maybe a
Just (a
x, Int
offsetAfter)
)
(Parser Char
anyChar Parser Char
-> Parser Text (Maybe (a, Int)) -> Parser Text (Maybe (a, Int))
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Maybe (a, Int) -> Parser Text (Maybe (a, Int))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (a, Int)
forall a. Maybe a
Nothing)
case Maybe (a, Int)
thisiter of
(Just (a
x, !Int
offsetAfter)) | Int
offsetThis Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
offsetBegin -> do
Text
unmatched <- Int -> Int -> Parser Text
substring Int
offsetBegin Int
offsetThis
(Text -> Either Text a
forall a b. a -> Either a b
Left Text
unmatchedEither Text a -> [Either Text a] -> [Either Text a]
forall a. a -> [a] -> [a]
:) ([Either Text a] -> [Either Text a])
-> ([Either Text a] -> [Either Text a])
-> [Either Text a]
-> [Either Text a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (a -> Either Text a
forall a b. b -> Either a b
Right a
xEither Text a -> [Either Text a] -> [Either Text a]
forall a. a -> [a] -> [a]
:) ([Either Text a] -> [Either Text a])
-> Parser [Either Text a] -> Parser [Either Text a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Parser [Either Text a]
go Int
offsetAfter
(Just (a
x, !Int
offsetAfter)) -> do
(a -> Either Text a
forall a b. b -> Either a b
Right a
xEither Text a -> [Either Text a] -> [Either Text a]
forall a. a -> [a] -> [a]
:) ([Either Text a] -> [Either Text a])
-> Parser [Either Text a] -> Parser [Either Text a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Parser [Either Text a]
go Int
offsetAfter
Maybe (a, Int)
Nothing -> Int -> Parser [Either Text a]
go Int
offsetBegin
)
{-# INLINABLE sepCap #-}
findAllCap
:: Parser a
-> Parser [Either T.Text (T.Text, a)]
findAllCap :: Parser a -> Parser [Either Text (Text, a)]
findAllCap Parser a
sep = Parser (Text, a) -> Parser [Either Text (Text, a)]
forall a. Parser a -> Parser [Either Text a]
sepCap (Parser a -> Parser (Text, a)
forall a. Parser a -> Parser (Text, a)
match Parser a
sep)
{-# INLINABLE findAllCap #-}
{-# DEPRECATED findAllCap "replace with `findAllCap sep = sepCap (match sep)`" #-}
findAll
:: Parser a
-> Parser [Either T.Text T.Text]
findAll :: Parser a -> Parser [Either Text Text]
findAll Parser a
sep = (([Either Text (Text, a)] -> [Either Text Text])
-> Parser Text [Either Text (Text, a)] -> Parser [Either Text Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap(([Either Text (Text, a)] -> [Either Text Text])
-> Parser Text [Either Text (Text, a)]
-> Parser [Either Text Text])
-> ((Either Text (Text, a) -> Either Text Text)
-> [Either Text (Text, a)] -> [Either Text Text])
-> (Either Text (Text, a) -> Either Text Text)
-> Parser Text [Either Text (Text, a)]
-> Parser [Either Text Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(Either Text (Text, a) -> Either Text Text)
-> [Either Text (Text, a)] -> [Either Text Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap) (((Text, a) -> Text) -> Either Text (Text, a) -> Either Text Text
forall (p :: * -> * -> *) b c a.
Bifunctor p =>
(b -> c) -> p a b -> p a c
second (Text, a) -> Text
forall a b. (a, b) -> a
fst) (Parser Text [Either Text (Text, a)] -> Parser [Either Text Text])
-> Parser Text [Either Text (Text, a)] -> Parser [Either Text Text]
forall a b. (a -> b) -> a -> b
$ Parser (Text, a) -> Parser Text [Either Text (Text, a)]
forall a. Parser a -> Parser [Either Text a]
sepCap (Parser a -> Parser (Text, a)
forall a. Parser a -> Parser (Text, a)
match Parser a
sep)
{-# INLINABLE findAll #-}
{-# DEPRECATED findAll "replace with `findAll sep = (fmap.fmap) (second fst) $ sepCap (match sep)`" #-}
getOffset :: Parser Int
getOffset :: Parser Int
getOffset = (forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Int r
-> IResult Text r)
-> Parser Int
forall i a.
(forall r.
State i
-> Pos
-> More
-> Failure i (State i) r
-> Success i (State i) a r
-> IResult i r)
-> Parser i a
AT.Parser ((forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Int r
-> IResult Text r)
-> Parser Int)
-> (forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Int r
-> IResult Text r)
-> Parser Int
forall a b. (a -> b) -> a -> b
$ \State Text
t Pos
pos More
more Failure Text (State Text) r
_ Success Text (State Text) Int r
succ' -> Success Text (State Text) Int r
succ' State Text
t Pos
pos More
more (Pos -> Int
AT.fromPos Pos
pos)
{-# INLINABLE getOffset #-}
substring :: Int -> Int -> Parser T.Text
substring :: Int -> Int -> Parser Text
substring !Int
bgn !Int
end = (forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Text r
-> IResult Text r)
-> Parser Text
forall i a.
(forall r.
State i
-> Pos
-> More
-> Failure i (State i) r
-> Success i (State i) a r
-> IResult i r)
-> Parser i a
AT.Parser ((forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Text r
-> IResult Text r)
-> Parser Text)
-> (forall r.
State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Text r
-> IResult Text r)
-> Parser Text
forall a b. (a -> b) -> a -> b
$ \State Text
t Pos
pos More
more Failure Text (State Text) r
lose Success Text (State Text) Text r
succes ->
let succes' :: p -> p -> p -> Text -> IResult Text r
succes' p
_t p
_pos p
_more Text
a = Success Text (State Text) Text r
succes State Text
t Pos
pos More
more Text
a
in
Parser Text
-> State Text
-> Pos
-> More
-> Failure Text (State Text) r
-> Success Text (State Text) Text r
-> IResult Text r
forall i a.
Parser i a
-> forall r.
State i
-> Pos
-> More
-> Failure i (State i) r
-> Success i (State i) a r
-> IResult i r
AT.runParser (Int -> Parser Text
takeCheat (Int
end Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
bgn)) State Text
t (Int -> Pos
AT.Pos Int
bgn) More
more Failure Text (State Text) r
lose Success Text (State Text) Text r
forall p p p. p -> p -> p -> Text -> IResult Text r
succes'
where
takeCheat :: Int -> Parser Text
takeCheat Int
len = do
(TI.Text Array
arr Int
off Int
_len) <- Int -> Parser Text
A.take Int
0
Text -> Parser Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Array -> Int -> Int -> Text
TI.Text Array
arr Int
off Int
len)