{-|
Module      :  Text.Regex.TDFA.Text.Lazy
Copyright   :  Chris Kuklewicz 2007-2009, shelarcy 2012
License     :  BSD-style (see the file LICENSE)

This modules provides 'RegexMaker' and 'RegexLike' instances for using
'Text' with the TDFA backend ("Text.Regex.TDFA.NewDFA.Engine" and
"Text.Regex.TDFA.NewDFA.Tester").

This exports instances of the high level API and the medium level
API of 'compile','execute', and 'regexec'.

@since 1.3.1
-}
module Text.Regex.TDFA.Text.Lazy(
  Regex
 ,CompOption
 ,ExecOption
 ,compile
 ,execute
 ,regexec
 ) where

import Data.Array.IArray(Array,(!),elems)
import qualified Data.Text.Lazy as L(Text,unpack)

import Text.Regex.Base(MatchArray,RegexContext(..),Extract(..),RegexMaker(..),RegexLike(..))
import Text.Regex.Base.Impl(polymatch,polymatchM)
import Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.String() -- piggyback on RegexMaker for String
import Text.Regex.TDFA.TDFA(patternToRegex)
import Text.Regex.TDFA.Common(Regex(..),CompOption,ExecOption(captureGroups),Position)

import Data.Maybe(listToMaybe)
import Text.Regex.TDFA.NewDFA.Uncons(Uncons)
import qualified Text.Regex.TDFA.NewDFA.Engine as Engine(execMatch)
import qualified Text.Regex.TDFA.NewDFA.Tester as Tester(matchTest)

-- | @since 1.3.1
instance RegexContext Regex L.Text L.Text where
  match :: Regex -> Text -> Text
match = forall a b. RegexLike a b => a -> b -> b
polymatch
  matchM :: forall (m :: * -> *). MonadFail m => Regex -> Text -> m Text
matchM = forall a b (m :: * -> *).
(RegexLike a b, MonadFail m) =>
a -> b -> m b
polymatchM

-- | @since 1.3.1
instance RegexMaker Regex CompOption ExecOption L.Text where
  makeRegexOptsM :: forall (m :: * -> *).
MonadFail m =>
CompOption -> ExecOption -> Text -> m Regex
makeRegexOptsM CompOption
c ExecOption
e Text
source = forall regex compOpt execOpt source (m :: * -> *).
(RegexMaker regex compOpt execOpt source, MonadFail m) =>
compOpt -> execOpt -> source -> m regex
makeRegexOptsM CompOption
c ExecOption
e (Text -> String
L.unpack Text
source)

-- | @since 1.3.1
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> L.Text -> [MatchArray] #-}
execMatch :: Uncons text => Regex -> Position -> Char -> text -> [MatchArray]
execMatch :: forall text.
Uncons text =>
Regex -> MatchOffset -> Char -> text -> [MatchArray]
execMatch = forall text.
Uncons text =>
Regex -> MatchOffset -> Char -> text -> [MatchArray]
Engine.execMatch

-- | @since 1.3.1
{-# SPECIALIZE myMatchTest :: Regex -> L.Text -> Bool #-}
myMatchTest :: Uncons text => Regex -> text -> Bool
myMatchTest :: forall text. Uncons text => Regex -> text -> Bool
myMatchTest = forall text. Uncons text => Regex -> text -> Bool
Tester.matchTest

-- | @since 1.3.1
instance RegexLike Regex L.Text where
  matchOnce :: Regex -> Text -> Maybe MatchArray
matchOnce Regex
r Text
s = forall a. [a] -> Maybe a
listToMaybe (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r Text
s)
  matchAll :: Regex -> Text -> [MatchArray]
matchAll Regex
r Text
s = forall text.
Uncons text =>
Regex -> MatchOffset -> Char -> text -> [MatchArray]
execMatch Regex
r MatchOffset
0 Char
'\n' Text
s
  matchCount :: Regex -> Text -> MatchOffset
matchCount Regex
r Text
s = forall (t :: * -> *) a. Foldable t => t a -> MatchOffset
length (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r' Text
s)
    where r' :: Regex
r' = Regex
r { regex_execOptions :: ExecOption
regex_execOptions = (Regex -> ExecOption
regex_execOptions Regex
r) {captureGroups :: Bool
captureGroups = Bool
False} }
  matchTest :: Regex -> Text -> Bool
matchTest = forall text. Uncons text => Regex -> text -> Bool
myMatchTest
  matchOnceText :: Regex -> Text -> Maybe (Text, MatchText Text, Text)
matchOnceText Regex
regex Text
source =
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ MatchArray
ma ->
            let (MatchOffset
o,MatchOffset
l) = MatchArray
maforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!MatchOffset
0
            in (forall source. Extract source => MatchOffset -> source -> source
before MatchOffset
o Text
source
               ,forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ (MatchOffset, MatchOffset)
ol -> (forall source.
Extract source =>
(MatchOffset, MatchOffset) -> source -> source
extract (MatchOffset, MatchOffset)
ol Text
source,(MatchOffset, MatchOffset)
ol)) MatchArray
ma
               ,forall source. Extract source => MatchOffset -> source -> source
after (MatchOffset
oforall a. Num a => a -> a -> a
+MatchOffset
l) Text
source))
         (forall regex source.
RegexLike regex source =>
regex -> source -> Maybe MatchArray
matchOnce Regex
regex Text
source)
  matchAllText :: Regex -> Text -> [MatchText Text]
matchAllText Regex
regex Text
source =
    let go :: Int -> L.Text -> [Array Int (Int, Int)] -> [Array Int (L.Text, (Int, Int))]
        go :: MatchOffset -> Text -> [MatchArray] -> [MatchText Text]
go MatchOffset
i Text
_ [MatchArray]
_ | MatchOffset
i seq :: forall a b. a -> b -> b
`seq` Bool
False = forall a. HasCallStack => a
undefined
        go MatchOffset
_i Text
_t [] = []
        go MatchOffset
i Text
t (MatchArray
x:[MatchArray]
xs) =
          let (MatchOffset
off0,MatchOffset
len0) = MatchArray
xforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!MatchOffset
0
              trans :: (MatchOffset, MatchOffset) -> (Text, (MatchOffset, MatchOffset))
trans pair :: (MatchOffset, MatchOffset)
pair@(MatchOffset
off,MatchOffset
len) = (forall source.
Extract source =>
(MatchOffset, MatchOffset) -> source -> source
extract (MatchOffset
offforall a. Num a => a -> a -> a
-MatchOffset
i,MatchOffset
len) Text
t,(MatchOffset, MatchOffset)
pair)
              t' :: Text
t' = forall source. Extract source => MatchOffset -> source -> source
after (MatchOffset
off0forall a. Num a => a -> a -> a
+(MatchOffset
len0forall a. Num a => a -> a -> a
-MatchOffset
i)) Text
t
          in forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (MatchOffset, MatchOffset) -> (Text, (MatchOffset, MatchOffset))
trans MatchArray
x forall a. a -> [a] -> [a]
: seq :: forall a b. a -> b -> b
seq Text
t' (MatchOffset -> Text -> [MatchArray] -> [MatchText Text]
go (MatchOffset
off0forall a. Num a => a -> a -> a
+MatchOffset
len0) Text
t' [MatchArray]
xs)
    in MatchOffset -> Text -> [MatchArray] -> [MatchText Text]
go MatchOffset
0 Text
source (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
regex Text
source)

-- | @since 1.3.1
compile :: CompOption -- ^ Flags (summed together)
        -> ExecOption -- ^ Flags (summed together)
        -> L.Text -- ^ The regular expression to compile
        -> Either String Regex -- ^ Returns: the compiled regular expression
compile :: CompOption -> ExecOption -> Text -> Either String Regex
compile CompOption
compOpt ExecOption
execOpt Text
txt =
  case String -> Either ParseError (Pattern, (MatchOffset, DoPa))
parseRegex (Text -> String
L.unpack Text
txt) of
    Left ParseError
err -> forall a b. a -> Either a b
Left (String
"parseRegex for Text.Regex.TDFA.Text.Lazy failed:"forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show ParseError
err)
    Right (Pattern, (MatchOffset, DoPa))
pattern -> forall a b. b -> Either a b
Right ((Pattern, (MatchOffset, DoPa)) -> CompOption -> ExecOption -> Regex
patternToRegex (Pattern, (MatchOffset, DoPa))
pattern CompOption
compOpt ExecOption
execOpt)

-- | @since 1.3.1
execute :: Regex  -- ^ Compiled regular expression
        -> L.Text -- ^ Text to match against
        -> Either String (Maybe MatchArray)
execute :: Regex -> Text -> Either String (Maybe MatchArray)
execute Regex
r Text
txt = forall a b. b -> Either a b
Right (forall regex source.
RegexLike regex source =>
regex -> source -> Maybe MatchArray
matchOnce Regex
r Text
txt)

-- | @since 1.3.1
regexec :: Regex  -- ^ Compiled regular expression
        -> L.Text -- ^ Text to match against
        -> Either String (Maybe (L.Text, L.Text, L.Text, [L.Text]))
regexec :: Regex -> Text -> Either String (Maybe (Text, Text, Text, [Text]))
regexec Regex
r Text
txt = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$
  case forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
matchOnceText Regex
r Text
txt of
    Just (Text
pre, MatchText Text
mt, Text
post) | Text
main:[Text]
rest <- forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> [e]
elems MatchText Text
mt)
      -> forall a. a -> Maybe a
Just (Text
pre, Text
main, Text
post, [Text]
rest)
    Maybe (Text, MatchText Text, Text)
_ -> forall a. Maybe a
Nothing