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 Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.MutRun(findMatch,findMatchAll,countMatchAll)
import Text.Regex.TDFA.TDFA(patternToDFA)
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 ->
let (dfa,i,tags,groups) = patternToDFA compOpt pattern
in Right (Regex dfa i tags groups 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 (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 = findMatch
matchAll = findMatchAll
matchCount = countMatchAll
instance RegexContext Regex String String where
match = polymatch
matchM = polymatchM