{-# OPTIONS_GHC -fno-warn-orphans #-}
{-|
This exports instances of the high level API and the medium level
API of 'compile','execute', and 'regexec'.
-}
{- Copyright   :  (c) Chris Kuklewicz 2007 -}
module Text.Regex.PCRE.String(
  -- ** Types
  Regex,
  MatchOffset,
  MatchLength,
  CompOption(CompOption),
  ExecOption(ExecOption),
  ReturnCode,
  WrapError,
  -- ** Miscellaneous
  unusedOffset,
  getVersion,
  -- ** Medium level API functions
  compile,
  execute,
  regexec,
  -- ** Constants for CompOption
  compBlank,
  compAnchored,
  compAutoCallout,
  compCaseless,
  compDollarEndOnly,
  compDotAll,
  compExtended,
  compExtra,
  compFirstLine,
  compMultiline,
  compNoAutoCapture,
  compUngreedy,
  compUTF8,
  compNoUTF8Check,
  -- ** Constants for ExecOption
  execBlank,
  execAnchored,
  execNotBOL,
  execNotEOL,
  execNotEmpty,
  execNoUTF8Check,
  execPartial
  ) where

import Prelude hiding (fail)
import Control.Monad.Fail (MonadFail(fail))

import Text.Regex.PCRE.Wrap -- all
import Foreign.C.String(withCStringLen,withCString)
import Data.Array(Array,listArray)
import System.IO.Unsafe(unsafePerformIO)
import Text.Regex.Base.RegexLike(RegexMaker(..),RegexLike(..),RegexContext(..),MatchLength,MatchOffset)
import Text.Regex.Base.Impl(polymatch,polymatchM)

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

unwrap :: (Show e) => Either e v -> IO v
unwrap :: forall e v. Show e => Either e v -> IO v
unwrap Either e v
x = case Either e v
x of Left e
err -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String
"Text.Regex.PCRE.String died: "forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show e
err)
                     Right v
v -> forall (m :: * -> *) a. Monad m => a -> m a
return v
v

instance RegexMaker Regex CompOption ExecOption String where
  makeRegexOpts :: CompOption -> ExecOption -> String -> Regex
makeRegexOpts CompOption
c ExecOption
e String
pattern = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    CompOption
-> ExecOption -> String -> IO (Either (Int, String) Regex)
compile CompOption
c ExecOption
e String
pattern forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e v. Show e => Either e v -> IO v
unwrap
  makeRegexOptsM :: forall (m :: * -> *).
MonadFail m =>
CompOption -> ExecOption -> String -> m Regex
makeRegexOptsM CompOption
c ExecOption
e String
pattern = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall (m :: * -> *) a. MonadFail m => String -> m a
failforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall a. Show a => a -> String
show) forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    CompOption
-> ExecOption -> String -> IO (Either (Int, String) Regex)
compile CompOption
c ExecOption
e String
pattern

instance RegexLike Regex String where
  matchTest :: Regex -> String -> Bool
matchTest Regex
regex String
str = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str (Int -> Regex -> CStringLen -> IO (Either WrapError Bool)
wrapTest Int
0 Regex
regex) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e v. Show e => Either e v -> IO v
unwrap
  matchOnce :: Regex -> String -> Maybe MatchArray
matchOnce Regex
regex String
str = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
    Regex -> String -> IO (Either WrapError (Maybe MatchArray))
execute Regex
regex String
str forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e v. Show e => Either e v -> IO v
unwrap
  matchAll :: Regex -> String -> [MatchArray]
matchAll Regex
regex String
str = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ 
    forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str (Regex -> CStringLen -> IO (Either WrapError [MatchArray])
wrapMatchAll Regex
regex) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e v. Show e => Either e v -> IO v
unwrap
  matchCount :: Regex -> String -> Int
matchCount Regex
regex String
str = forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$ 
    forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str (Regex -> CStringLen -> IO (Either WrapError Int)
wrapCount Regex
regex) forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall e v. Show e => Either e v -> IO v
unwrap

-- | Compiles a regular expression
compile :: CompOption -- ^ Flags (summed together)
        -> ExecOption -- ^ Flags (summed together)
        -> String     -- ^ The regular expression to compile
        -> IO (Either (MatchOffset,String) Regex) -- ^ Returns: an error string and offset or the compiled regular expression
compile :: CompOption
-> ExecOption -> String -> IO (Either (Int, String) Regex)
compile CompOption
c ExecOption
e String
pattern = forall a. String -> (CString -> IO a) -> IO a
withCString String
pattern (CompOption
-> ExecOption -> CString -> IO (Either (Int, String) Regex)
wrapCompile CompOption
c ExecOption
e)

-- | Matches a regular expression against a string
execute :: Regex      -- ^ Compiled regular expression
        -> String     -- ^ String to match against
        -> IO (Either WrapError (Maybe (Array Int (MatchOffset,MatchLength))))
                -- ^ Returns: 'Nothing' if the regex did not match the
                -- string, or:
                --   'Just' an array of (offset,length) pairs where index 0 is whole match, and the rest are the captured subexpressions.
execute :: Regex -> String -> IO (Either WrapError (Maybe MatchArray))
execute Regex
regex String
str = do
  Either WrapError (Maybe [(Int, Int)])
maybeStartEnd <- forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str (Int
-> Regex
-> CStringLen
-> IO (Either WrapError (Maybe [(Int, Int)]))
wrapMatch Int
0 Regex
regex)
  case Either WrapError (Maybe [(Int, Int)])
maybeStartEnd of
    Right Maybe [(Int, Int)]
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right forall a. Maybe a
Nothing)
--  Right (Just []) -> fail "got [] back!" -- should never happen
    Right (Just [(Int, Int)]
parts) -> 
      forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Int
0,forall a. Enum a => a -> a
pred (forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Int, Int)]
parts))
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (\(Int
s,Int
e)->(forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
s, forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
eforall a. Num a => a -> a -> a
-Int
s))) forall a b. (a -> b) -> a -> b
$ [(Int, Int)]
parts
    Left WrapError
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left WrapError
err)

-- | execute match and extract substrings rather than just offsets
regexec  :: Regex      -- ^ compiled regular expression
         -> String     -- ^ string to match
         -> IO (Either WrapError (Maybe (String, String,String, [String])))
                      -- ^ Returns: Nothing if no match, else
                      --   (text before match, text after match, array of matches with 0 being the whole match)
regexec :: Regex
-> String
-> IO (Either WrapError (Maybe (String, String, String, [String])))
regexec Regex
regex String
str = do
  let getSub :: (Int, Int) -> String
getSub (Int
start,Int
stop) | Int
start forall a. Eq a => a -> a -> Bool
== Int
unusedOffset = String
""
                          | Bool
otherwise = forall a. Int -> [a] -> [a]
take (Int
stopforall a. Num a => a -> a -> a
-Int
start) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Int -> [a] -> [a]
drop Int
start forall a b. (a -> b) -> a -> b
$ String
str
      matchedParts :: [(Int, Int)] -> (String, String, String, [String])
matchedParts [] = (String
"",String
"",String
str,[]) -- no information
      matchedParts (matchedStartStop :: (Int, Int)
matchedStartStop@(Int
start,Int
stop):[(Int, Int)]
subStartStop) = 
        (forall a. Int -> [a] -> [a]
take Int
start String
str
        ,(Int, Int) -> String
getSub (Int, Int)
matchedStartStop
        ,forall a. Int -> [a] -> [a]
drop Int
stop String
str
        ,forall a b. (a -> b) -> [a] -> [b]
map (Int, Int) -> String
getSub [(Int, Int)]
subStartStop)
  Either WrapError (Maybe [(Int, Int)])
maybeStartEnd <- forall a. String -> (CStringLen -> IO a) -> IO a
withCStringLen String
str (Int
-> Regex
-> CStringLen
-> IO (Either WrapError (Maybe [(Int, Int)]))
wrapMatch Int
0 Regex
regex)
  case Either WrapError (Maybe [(Int, Int)])
maybeStartEnd of
    Right Maybe [(Int, Int)]
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right forall a. Maybe a
Nothing)
--  Right (Just []) -> fail "got [] back!" -- should never happen
    Right (Just [(Int, Int)]
parts) -> forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Int, Int)] -> (String, String, String, [String])
matchedParts forall a b. (a -> b) -> a -> b
$ [(Int, Int)]
parts
    Left WrapError
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left WrapError
err)