{- |
This modules provides 'RegexMaker' and 'RegexLike' instances for using
'String' with the TDFA backend.

This exports instances of the high level API and the medium level
API of 'compile','execute', and 'regexec'.
-}
{- By Chris Kuklewicz, 2009. BSD License, see the LICENSE file. -}
module Text.Regex.TDFA.String(
  -- ** Types
  Regex
 ,MatchOffset
 ,MatchLength
 ,CompOption
 ,ExecOption
  -- ** Medium level API functions
 ,compile
 ,execute
 ,regexec
 ) where

import Text.Regex.Base.Impl(polymatch,polymatchM)
import Text.Regex.Base.RegexLike(RegexMaker(..),RegexLike(..),RegexContext(..),MatchOffset,MatchLength,MatchArray)
import Text.Regex.TDFA.Common(common_error,Regex(..),CompOption,ExecOption(captureGroups))
import Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.TDFA(patternToRegex)

import Data.Array.IArray((!),elems,amap)
import Data.Maybe(listToMaybe)
import Text.Regex.TDFA.NewDFA.Engine(execMatch)
import Text.Regex.TDFA.NewDFA.Tester as Tester(matchTest)

err :: String -> a
err :: forall a. String -> a
err = forall a. String -> String -> a
common_error String
"Text.Regex.TDFA.String"

unwrap :: Either String v -> v
unwrap :: forall v. Either String v -> v
unwrap Either String v
x = case Either String v
x of Left String
msg -> forall a. String -> a
err (String
"Text.Regex.TDFA.String died: "forall a. [a] -> [a] -> [a]
++String
msg)
                     Right v
v -> v
v

compile  :: CompOption -- ^ Flags (summed together)
         -> ExecOption -- ^ Flags (summed together)
         -> String     -- ^ The regular expression to compile (ASCII only, no null bytes)
         -> Either String Regex -- ^ Returns: the compiled regular expression
compile :: CompOption -> ExecOption -> String -> Either String Regex
compile CompOption
compOpt ExecOption
execOpt String
source =
  case String -> Either ParseError (Pattern, (GroupIndex, DoPa))
parseRegex String
source of
    Left ParseError
msg -> forall a b. a -> Either a b
Left (String
"parseRegex for Text.Regex.TDFA.String failed:"forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show ParseError
msg)
    Right (Pattern, (GroupIndex, DoPa))
pattern -> forall a b. b -> Either a b
Right ((Pattern, (GroupIndex, DoPa)) -> CompOption -> ExecOption -> Regex
patternToRegex (Pattern, (GroupIndex, DoPa))
pattern CompOption
compOpt ExecOption
execOpt)

instance RegexMaker Regex CompOption ExecOption String where
  makeRegexOpts :: CompOption -> ExecOption -> String -> Regex
makeRegexOpts CompOption
c ExecOption
e String
source = forall v. Either String v -> v
unwrap (CompOption -> ExecOption -> String -> Either String Regex
compile CompOption
c ExecOption
e String
source)
  makeRegexOptsM :: forall (m :: * -> *).
MonadFail m =>
CompOption -> ExecOption -> String -> m Regex
makeRegexOptsM CompOption
c ExecOption
e String
source = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ CompOption -> ExecOption -> String -> Either String Regex
compile CompOption
c ExecOption
e String
source

execute :: Regex      -- ^ Compiled regular expression
        -> String     -- ^ String to match against
        -> Either String (Maybe MatchArray)
execute :: Regex -> String -> Either String (Maybe MatchArray)
execute Regex
r String
s = forall a b. b -> Either a b
Right (forall regex source.
RegexLike regex source =>
regex -> source -> Maybe MatchArray
matchOnce Regex
r String
s)

regexec :: Regex      -- ^ Compiled regular expression
        -> String     -- ^ String to match against
        -> Either String (Maybe (String, String, String, [String]))
regexec :: Regex
-> String
-> Either String (Maybe (String, String, String, [String]))
regexec Regex
r String
s =
  case forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
matchOnceText Regex
r String
s of
    Maybe (String, MatchText String, String)
Nothing -> forall a b. b -> Either a b
Right forall a. Maybe a
Nothing
    Just (String
pre,MatchText String
mt,String
post) ->
      let main :: String
main = forall a b. (a, b) -> a
fst (MatchText String
mtforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!GroupIndex
0)
          rest :: [String]
rest = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall a. [a] -> [a]
tail (forall (a :: * -> * -> *) e i. (IArray a e, Ix i) => a i e -> [e]
elems MatchText String
mt)) -- will be []
      in forall a b. b -> Either a b
Right (forall a. a -> Maybe a
Just (String
pre,String
main,String
post,[String]
rest))

-- Minimal definition for now
instance RegexLike Regex String where
  matchOnce :: Regex -> String -> Maybe MatchArray
matchOnce Regex
r String
s = forall a. [a] -> Maybe a
listToMaybe (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r String
s)
  matchAll :: Regex -> String -> [MatchArray]
matchAll Regex
r String
s = forall text.
Uncons text =>
Regex -> GroupIndex -> Char -> text -> [MatchArray]
execMatch Regex
r GroupIndex
0 Char
'\n' String
s
  matchCount :: Regex -> String -> GroupIndex
matchCount Regex
r String
s = forall (t :: * -> *) a. Foldable t => t a -> GroupIndex
length (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r' String
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 -> String -> Bool
matchTest = forall text. Uncons text => Regex -> text -> Bool
Tester.matchTest
  -- matchOnceText
  matchAllText :: Regex -> String -> [MatchText String]
matchAllText Regex
r String
s =
    let go :: GroupIndex
-> [a]
-> [a i (GroupIndex, GroupIndex)]
-> [a i ([a], (GroupIndex, GroupIndex))]
go GroupIndex
i [a]
_ [a i (GroupIndex, GroupIndex)]
_ | GroupIndex
i seq :: forall a b. a -> b -> b
`seq` Bool
False = forall a. HasCallStack => a
undefined
        go GroupIndex
_i [a]
_t [] = []
        go GroupIndex
i [a]
t (a i (GroupIndex, GroupIndex)
x:[a i (GroupIndex, GroupIndex)]
xs) = let (GroupIndex
off0,GroupIndex
len0) = a i (GroupIndex, GroupIndex)
xforall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> e
!i
0
                            trans :: (GroupIndex, GroupIndex) -> ([a], (GroupIndex, GroupIndex))
trans pair :: (GroupIndex, GroupIndex)
pair@(GroupIndex
off,GroupIndex
len) = (forall a. GroupIndex -> [a] -> [a]
take GroupIndex
len (forall a. GroupIndex -> [a] -> [a]
drop (GroupIndex
offforall a. Num a => a -> a -> a
-GroupIndex
i) [a]
t),(GroupIndex, GroupIndex)
pair)
                            t' :: [a]
t' = forall a. GroupIndex -> [a] -> [a]
drop (GroupIndex
off0forall a. Num a => a -> a -> a
+GroupIndex
len0forall a. Num a => a -> a -> a
-GroupIndex
i) [a]
t
                        in forall (a :: * -> * -> *) e' e i.
(IArray a e', IArray a e, Ix i) =>
(e' -> e) -> a i e' -> a i e
amap (GroupIndex, GroupIndex) -> ([a], (GroupIndex, GroupIndex))
trans a i (GroupIndex, GroupIndex)
x forall a. a -> [a] -> [a]
: seq :: forall a b. a -> b -> b
seq [a]
t' (GroupIndex
-> [a]
-> [a i (GroupIndex, GroupIndex)]
-> [a i ([a], (GroupIndex, GroupIndex))]
go (GroupIndex
off0forall a. Num a => a -> a -> a
+GroupIndex
len0) [a]
t' [a i (GroupIndex, GroupIndex)]
xs)
    in forall {a :: * -> * -> *} {a} {i}.
(IArray a ([a], (GroupIndex, GroupIndex)),
 IArray a (GroupIndex, GroupIndex), Ix i, Num i) =>
GroupIndex
-> [a]
-> [a i (GroupIndex, GroupIndex)]
-> [a i ([a], (GroupIndex, GroupIndex))]
go GroupIndex
0 String
s (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r String
s)

instance RegexContext Regex String String where
  match :: Regex -> String -> String
match = forall a b. RegexLike a b => a -> b -> b
polymatch
  matchM :: forall (m :: * -> *). MonadFail m => Regex -> String -> m String
matchM = forall a b (m :: * -> *).
(RegexLike a b, MonadFail m) =>
a -> b -> m b
polymatchM