module Text.Regex.TDFA.Sequence(
Regex
,CompOption
,ExecOption
,compile
,execute
,regexec
) where
import Data.Array((!),elems)
import Data.Sequence as S
import Text.Regex.Base(MatchArray,RegexContext(..),RegexMaker(..),RegexLike(..))
import Text.Regex.Base.Impl(polymatch,polymatchM)
import Text.Regex.TDFA.String()
import Text.Regex.TDFA.TDFA(patternToRegex)
import Text.Regex.TDFA.Wrap(Regex(..),CompOption,ExecOption)
import Text.Regex.TDFA.ReadRegex(parseRegex)
import qualified Data.Foldable as F(toList)
instance RegexContext Regex (S.Seq Char) (S.Seq Char) where
match = polymatch
matchM = polymatchM
instance RegexMaker Regex CompOption ExecOption (S.Seq Char) where
makeRegexOptsM c e source = either fail return $ compile c e source
instance RegexLike Regex (S.Seq Char) where
matchOnce r = matchOnce r . F.toList
matchAll r = matchAll r . F.toList
matchCount r = matchCount r . F.toList
matchTest r = matchTest r . F.toList
matchOnceText regex source =
fmap (\ma -> let (o,l) = ma!0
in (S.take o source
,fmap (\ol@(off,len) -> (S.take len (S.drop off source),ol)) ma
,S.drop (o+l) source))
(matchOnce regex source)
matchAllText regex source =
map (fmap (\ol@(off,len) -> (S.take len (S.drop off source),ol)))
(matchAll regex source)
toList :: S.Seq Char -> [Char]
toList s = expand (S.viewl s) where
expand EmptyL = []
expand (c :< cs) = c : expand (S.viewl cs)
compile :: CompOption
-> ExecOption
-> (S.Seq Char)
-> Either String Regex
compile compOpt execOpt bs =
case parseRegex (toList bs) of
Left err -> Left ("parseRegex for Text.Regex.TDFA.ByteString failed:"++show err)
Right pattern -> Right (patternToRegex pattern compOpt execOpt)
execute :: Regex
-> (S.Seq Char)
-> Either String (Maybe MatchArray)
execute r bs = Right (matchOnce r bs)
regexec :: Regex
-> (S.Seq Char)
-> Either String (Maybe ((S.Seq Char), (S.Seq Char), (S.Seq Char), [(S.Seq Char)]))
regexec r bs =
case matchOnceText r bs 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))