1{-| 2This modules provides 'RegexMaker' and 'RegexLike' instances for using 3@ByteString@ with the DFA backend ("Text.Regex.Lib.WrapDFAEngine" and 4"Text.Regex.Lazy.DFAEngineFPS"). This module is usually used via 5import "Text.Regex.TDFA". 6 7This exports instances of the high level API and the medium level 8API of 'compile','execute', and 'regexec'. 9-} 10module Text.Regex.TDFA.Sequence( 11 Regex 12 ,CompOption 13 ,ExecOption 14 ,compile 15 ,execute 16 ,regexec 17 ) where 18 19import Data.Sequence(Seq) 20import Data.Foldable as F(toList) 21 22import Text.Regex.Base(MatchArray,RegexContext(..),RegexMaker(..),RegexLike(..),Extract(..)) 23import Text.Regex.Base.Impl(polymatch,polymatchM) 24import Text.Regex.TDFA.Common(Regex(..),CompOption,ExecOption(captureGroups)) 25import Text.Regex.TDFA.String() -- piggyback on RegexMaker for String 26import Text.Regex.TDFA.TDFA(patternToRegex) 27import Text.Regex.TDFA.ReadRegex(parseRegex) 28 29import Data.Array.IArray((!),elems) 30import Data.Maybe(listToMaybe) 31import Text.Regex.TDFA.NewDFA.Engine(execMatch) 32import Text.Regex.TDFA.NewDFA.Tester as Tester(matchTest) 33 34{- By Chris Kuklewicz, 2007. BSD License, see the LICENSE file. -} 35 36instance RegexContext Regex (Seq Char) (Seq Char) where 37 match = polymatch 38 matchM = polymatchM 39 40instance RegexMaker Regex CompOption ExecOption (Seq Char) where 41 makeRegexOptsM c e source = 42 case parseRegex (F.toList source) of 43 Left err -> fail $ "parseRegex for Text.Regex.TDFA.Sequence failed:"++show err 44 Right pattern -> return $ patternToRegex pattern c e 45 46instance RegexLike Regex (Seq Char) where 47 matchOnce r s = listToMaybe (matchAll r s) 48 matchAll r s = execMatch r 0 '\n' s 49 matchCount r s = length (matchAll r' s) 50 where r' = r { regex_execOptions = (regex_execOptions r) {captureGroups = False} } 51 matchTest = Tester.matchTest 52 matchOnceText regex source = 53 fmap (\ma -> let (o,l) = ma!0 54 in (before o source 55 ,fmap (\ol -> (extract ol source,ol)) ma 56 ,after (o+l) source)) 57 (matchOnce regex source) 58 matchAllText regex source = 59 map (fmap (\ol -> (extract ol source,ol))) 60 (matchAll regex source) 61 62compile :: CompOption -- ^ Flags (summed together) 63 -> ExecOption -- ^ Flags (summed together) 64 -> (Seq Char) -- ^ The regular expression to compile 65 -> Either String Regex -- ^ Returns: the compiled regular expression 66compile compOpt execOpt bs = 67 case parseRegex (F.toList bs) of 68 Left err -> Left ("parseRegex for Text.Regex.TDFA.Sequence failed:"++show err) 69 Right pattern -> Right (patternToRegex pattern compOpt execOpt) 70 71execute :: Regex -- ^ Compiled regular expression 72 -> (Seq Char) -- ^ ByteString to match against 73 -> Either String (Maybe MatchArray) 74execute r bs = Right (matchOnce r bs) 75 76regexec :: Regex -- ^ Compiled regular expression 77 -> (Seq Char) -- ^ ByteString to match against 78 -> Either String (Maybe ((Seq Char), (Seq Char), (Seq Char), [(Seq Char)])) 79regexec r bs = 80 case matchOnceText r bs of 81 Nothing -> Right (Nothing) 82 Just (pre,mt,post) -> 83 let main = fst (mt!0) 84 rest = map fst (tail (elems mt)) -- will be [] 85 in Right (Just (pre,main,post,rest)) 86