module Text.Regex.TDFA.String(
Regex
,MatchOffset
,MatchLength
,CompOption
,ExecOption
,compile
,execute
,regexec
) where
import Data.Array((!),elems)
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)
import qualified Text.Regex.TDFA.NewDFA as N(matchAll,matchOnce,matchCount,matchTest)
import Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.TDFA(patternToRegex)
import Text.Regex.TDFA.Wrap(Regex(..),CompOption,ExecOption)
err :: String -> a
err = common_error "Text.Regex.TDFA.String"
unwrap :: Either String v -> v
unwrap x = case x of Left msg -> err ("Text.Regex.TDFA.String died: "++msg)
Right v -> v
compile :: CompOption
-> ExecOption
-> String
-> Either String Regex
compile compOpt execOpt source =
case parseRegex source of
Left msg -> Left ("parseRegex for Text.Regex.TDFA.String failed:"++show msg)
Right pattern -> Right (patternToRegex pattern compOpt execOpt)
instance RegexMaker Regex CompOption ExecOption String where
makeRegexOpts c e source = unwrap (compile c e source)
makeRegexOptsM c e source = either fail return $ compile c e source
execute :: Regex
-> String
-> Either String (Maybe MatchArray)
execute r s = Right (N.matchOnce r s)
regexec :: Regex
-> String
-> Either String (Maybe (String, String, String, [String]))
regexec r s =
case matchOnceText r s of
Nothing -> Right Nothing
Just (pre,mt,post) ->
let main = fst (mt!0)
rest = map fst (tail (elems mt))
in Right (Just (pre,main,post,rest))
instance RegexLike Regex String where
matchOnce = N.matchOnce
matchAll = N.matchAll
matchCount = N.matchCount
matchTest = N.matchTest
instance RegexContext Regex String String where
match = polymatch
matchM = polymatchM