{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE TupleSections              #-}
{-# LANGUAGE CPP              #-}

{-|

This implements a common DSL for regular expression replacement text. This
is represented with the 'Replace' data type. It also implements the
'IsString' interface, so if 'OverloadedStrings' is on, you can use a raw
string to build the replacement.

-}


module Data.Text.ICU.Replace
    (
    -- * @OverloadedStrings@ Syntax
    -- $doc

    -- * Types
      Replace

    -- * High-level interface
    , replace
    , replace'
    , replaceAll
    , replaceAll'

    -- * Low-level interface
    , rgroup
    , rtext
    , rstring
    , rfn
    , rtfn
    , rbuilder
    ) where


import           Control.Applicative
import           Data.Attoparsec.Text
import           Data.Foldable
#if __GLASGOW_HASKELL__ >= 804
import           Data.Semigroup (Semigroup)
#endif
import           Data.Monoid
import           Data.String
import qualified Data.Text              as T
import           Data.Text.ICU
import qualified Data.Text.ICU          as ICU
import qualified Data.Text.Lazy         as TL
import qualified Data.Text.Lazy.Builder as TB
import           Data.Tuple
import           Prelude                hiding (span)



-- | A 'Replace' instance is a function from a regular expression match to
-- a 'Data.Text.Lazy.Builder.Builder'. This naturally forms a 'Monoid', so
-- they're easy to combine.
--
-- 'Replace' also implements 'IsString', so raw strings can be used to
-- construct them.
newtype Replace = Replace { Replace -> Match -> Builder
unReplace :: Match -> TB.Builder } deriving
                  ( Semigroup Replace
Replace
[Replace] -> Replace
Replace -> Replace -> Replace
forall a.
Semigroup a -> a -> (a -> a -> a) -> ([a] -> a) -> Monoid a
mconcat :: [Replace] -> Replace
$cmconcat :: [Replace] -> Replace
mappend :: Replace -> Replace -> Replace
$cmappend :: Replace -> Replace -> Replace
mempty :: Replace
$cmempty :: Replace
Monoid
#if __GLASGOW_HASKELL__ >= 804
                  , NonEmpty Replace -> Replace
Replace -> Replace -> Replace
forall b. Integral b => b -> Replace -> Replace
forall a.
(a -> a -> a)
-> (NonEmpty a -> a)
-> (forall b. Integral b => b -> a -> a)
-> Semigroup a
stimes :: forall b. Integral b => b -> Replace -> Replace
$cstimes :: forall b. Integral b => b -> Replace -> Replace
sconcat :: NonEmpty Replace -> Replace
$csconcat :: NonEmpty Replace -> Replace
<> :: Replace -> Replace -> Replace
$c<> :: Replace -> Replace -> Replace
Semigroup
#endif
                  )

instance IsString Replace where
    fromString :: String -> Replace
fromString = Text -> Replace
parseReplace forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
T.pack

type MatchState = (Match, Int)

-- | Execute a regular expression on a 'Data.Text.Text' and replace the
-- first match.
replace :: Regex        -- ^ The regular expression to match.
        -> Replace      -- ^ The specification to replace it with.
        -> T.Text       -- ^ The text to operate on.
        -> T.Text       -- ^ The text with the first match replaced.
replace :: Regex -> Replace -> Text -> Text
replace Regex
re Replace
r Text
t = forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
t (Replace -> Match -> Text
replace' Replace
r) forall a b. (a -> b) -> a -> b
$ Regex -> Text -> Maybe Match
ICU.find Regex
re Text
t

-- | Replace one regular expression match with the 'Replace'.
replace' :: Replace     -- ^ The specification to replace it with.
         -> Match       -- ^ The match to replace.
         -> T.Text      -- ^ The text with the match replaced.
replace' :: Replace -> Match -> Text
replace' Replace
r Match
m =
    Last MatchState -> Builder -> Text
finish (forall a. Maybe a -> Last a
Last (forall a. a -> Maybe a
Just (Match
m, Int
0))) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Builder
p forall a. Semigroup a => a -> a -> a
<>) forall a b. (a -> b) -> a -> b
$ Replace -> Match -> Builder
unReplace Replace
r Match
m
    where
        p :: Builder
p = forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap Text -> Builder
TB.fromText forall a b. (a -> b) -> a -> b
$ Int -> Match -> Maybe Text
prefix Int
0 Match
m

-- | Execute a regular expression on a 'Data.Text.Text' and replace all
-- matches.
replaceAll :: Regex     -- ^ The regular expression to match.
           -> Replace   -- ^ The specification to replace it with.
           -> T.Text    -- ^ The text to operate on.
           -> T.Text    -- ^ The text with all matches replaced.
replaceAll :: Regex -> Replace -> Text -> Text
replaceAll Regex
re Replace
r Text
t = case Regex -> Text -> [Match]
ICU.findAll Regex
re Text
t of
                        [] -> Text
t
                        [Match]
ms -> Replace -> [Match] -> Text
replaceAll' Replace
r [Match]
ms

-- | Replace all regular expression matches with the 'Replace'.
replaceAll' :: Replace  -- ^ The specification to replace it with.
            -> [Match]  -- ^ The matches to replace.
            -> T.Text   -- ^ The text with all matches replaced.
replaceAll' :: Replace -> [Match] -> Text
replaceAll' Replace
r =
    forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Last MatchState -> Builder -> Text
finish forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Last MatchState, Builder) -> Match -> (Last MatchState, Builder)
step (forall a. Maybe a -> Last a
Last forall a. Maybe a
Nothing, forall a. Monoid a => a
mempty)
    where
        step :: (Last MatchState, TB.Builder)
             -> Match
             -> (Last MatchState, TB.Builder)
        step :: (Last MatchState, Builder) -> Match -> (Last MatchState, Builder)
step (Last Maybe MatchState
prev, Builder
buffer) Match
m =
            let s :: Text
s      = Match -> Text
span Match
m
                g :: Text
g      = forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall a b. (a -> b) -> a -> b
$ Int -> Match -> Maybe Text
group Int
0 Match
m
                offset :: Int
offset = (forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
s) forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Num a => a -> a -> a
+ Text -> Int
T.length Text
g) forall a b. (a -> b) -> a -> b
$ forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 forall a b. (a, b) -> b
snd Maybe MatchState
prev
            in  ( forall a. Maybe a -> Last a
Last forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (Match
m, Int
offset)
                , Builder
buffer forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TB.fromText Text
s forall a. Semigroup a => a -> a -> a
<> Replace -> Match -> Builder
unReplace Replace
r Match
m
                )

-- | This handles the last match by including not only the match's prefix
-- and the replacement text, but also the suffix trailing the match.
finish :: Last MatchState       -- ^ The state of the match to get the prefix
                                -- and suffix from.
       -> TB.Builder            -- ^ The current replacement's output.
       -> T.Text
finish :: Last MatchState -> Builder -> Text
finish Last MatchState
m Builder
b =   Text -> Text
TL.toStrict
           forall b c a. (b -> c) -> (a -> b) -> a -> c
.   Builder -> Text
TB.toLazyText
           forall b c a. (b -> c) -> (a -> b) -> a -> c
.   forall a. Monoid a => a -> a -> a
mappend Builder
b
           forall b c a. (b -> c) -> (a -> b) -> a -> c
.   Text -> Builder
TB.fromText
           forall b c a. (b -> c) -> (a -> b) -> a -> c
.   forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
           forall a b. (a -> b) -> a -> b
$   Int -> Match -> Maybe Text
suffix Int
0
           forall b c a. (b -> c) -> (a -> b) -> a -> c
.   forall a b. (a, b) -> a
fst
           forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall a. Last a -> Maybe a
getLast Last MatchState
m

-- | Create a 'Replace' that inserts a regular expression group.
rgroup :: Int       -- ^ The number of the group in a regular expression.
       -> Replace   -- ^ The 'Replace' that inserts a group's match.
rgroup :: Int -> Replace
rgroup Int
g = (Match -> Builder) -> Replace
Replace forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Builder
TB.fromText forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Match -> Maybe Text
group Int
g

-- | Create a 'Replace' that inserts static 'Data.Text.Text'.
rtext :: T.Text     -- ^ The static 'Data.Text.Text' to insert.
      -> Replace    -- ^ The 'Replace' that inserts the static 'Data.Text.Text'.
rtext :: Text -> Replace
rtext = Builder -> Replace
rbuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Builder
TB.fromText

-- | Create a 'Replace' that inserts a static 'String'.
rstring :: String   -- ^ The static 'String' to insert.
        -> Replace  -- ^ The 'Replace' that inserts the static 'String'.
rstring :: String -> Replace
rstring = Builder -> Replace
rbuilder forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Builder
TB.fromString

-- | Create a 'Replace' from a function that transforms a 'Match' into
-- a 'Data.Text.Lazy.Builder.Builder'.
rfn :: (Match -> TB.Builder)    -- ^ The function that creates the replacement text.
    -> Replace                  -- ^ The 'Replace' based off that function.
rfn :: (Match -> Builder) -> Replace
rfn = (Match -> Builder) -> Replace
Replace

-- | Create a 'Replace' From a function that transforms a 'Match' into
-- a 'Data.Text.Text'.
rtfn :: (Match -> T.Text)       -- ^ The function that creates the replacement text.
     -> Replace                 -- ^ The 'Replace' based off that function.
rtfn :: (Match -> Text) -> Replace
rtfn = (Match -> Builder) -> Replace
Replace forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text -> Builder
TB.fromText forall b c a. (b -> c) -> (a -> b) -> a -> c
.)

-- | Create a 'Replace' that inserts a static 'Data.Text.Lazy.Builder.Builder'.
rbuilder :: TB.Builder  -- ^ The 'Data.Text.Lazy.Builder.Builder' to insert.
         -> Replace     -- ^ The 'Replace' that inserts the static
                        -- 'Data.Text.Lazy.Builder.Builder'.
rbuilder :: Builder -> Replace
rbuilder = (Match -> Builder) -> Replace
Replace forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. a -> b -> a
const

-- | This parses a 'Data.Text.Text' into a 'Replace' structure.
--
-- Generally, input text is considered to be static.
--
-- However, groups from the regular expression's matches can be insert
-- using @$1@ (to insert the first group) or @${7}@ (to insert the seventh
-- group).
--
-- Dollar signs can be included in the output by doubling them (@$$@).
parseReplace :: T.Text -> Replace
parseReplace :: Text -> Replace
parseReplace Text
t = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ Text -> Replace
rtext Text
t) forall a. a -> a
id
               forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> Text -> Either String a
parseOnly (Parser Text Replace
replacement forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* forall t. Chunk t => Parser t ()
endOfInput) Text
t

-- A replacement.
replacement :: Parser Replace
replacement :: Parser Text Replace
replacement = forall a. Monoid a => [a] -> a
mconcat forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (Parser Text Replace
dollarGroup forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text Replace
raw)

-- A @\$\d+@ or @\$\{\d+\}@ group. This could also be an escaped dollar
-- sign (@$$@).
dollarGroup :: Parser Replace
dollarGroup :: Parser Text Replace
dollarGroup = Char -> Parser Char
char Char
'$' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> (Parser Text Replace
grp forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text Replace
escaped)
    where curly :: Parser Text Int
curly   = Char -> Parser Char
char Char
'{' forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall a. Integral a => Parser a
decimal forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Char -> Parser Char
char Char
'}'
          grp :: Parser Text Replace
grp     = Int -> Replace
rgroup forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall a. Integral a => Parser a
decimal forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser Text Int
curly)
          escaped :: Parser Text Replace
escaped = Text -> Replace
rtext forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Text
T.singleton forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Char -> Parser Char
char Char
'$'

-- A raw input string. It must contain no dollar signs (@$@).
raw :: Parser Replace
raw :: Parser Text Replace
raw = Text -> Replace
rtext forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Char -> Bool) -> Parser Text
takeWhile1 (forall a. Eq a => a -> a -> Bool
/= Char
'$')


-- $doc
--
-- The syntax used with 'OverloadedStrings' is meant to be similar to that
-- used in other regular expression libraries in other programming
-- languages.
--
-- Generally, input text is considered to be static.
--
-- >>> replaceAll "a" "b" "aaa"
-- "bbb"
-- >>> replaceAll "ab" "ba" "cdababcd"
-- "cdbabacd"
--
-- However, groups from the regular expression's matches can be insert
-- using @$1@ (to insert the first group) or @${7}@ (to insert the seventh
-- group).
--
-- >>> replaceAll "(.*), (.*)" "$2 $1" "Beeblebrox, Zaphod"
-- "Zaphod Beeblebrox"
-- >>> replaceAll "4(\\d)" "${1}4" "7458"
-- "7548"
--
-- Dollar signs can be included in the output by doubling them (@$$@).
--
-- >>> replaceAll "(\\d+\\.\\d+)" "$$$1" "9.99"
-- "$9.99"