module Text.Regex.TDFA.Text(
Regex
,CompOption
,ExecOption
,compile
,execute
,regexec
) where
import Data.Array((!),elems)
import qualified Data.Text as T(Text,unpack)
import Text.Regex.Base(RegexLike(..),RegexMaker(..),Extract(..),MatchArray,RegexContext(..))
import Text.Regex.Base.Impl(polymatch,polymatchM)
import Text.Regex.TDFA.ReadRegex(parseRegex)
import Text.Regex.TDFA.String()
import Text.Regex.TDFA.TDFA(patternToRegex)
import Text.Regex.TDFA.Common(Regex(..),CompOption,ExecOption(captureGroups),Position)
import Data.Maybe(listToMaybe)
import Text.Regex.TDFA.NewDFA.Uncons(Uncons)
import qualified Text.Regex.TDFA.NewDFA.Engine as Engine(execMatch)
import qualified Text.Regex.TDFA.NewDFA.Tester as Tester(matchTest)
instance RegexContext Regex T.Text T.Text where
match :: Regex -> Text -> Text
match = forall a b. RegexLike a b => a -> b -> b
polymatch
matchM :: forall (m :: * -> *). MonadFail m => Regex -> Text -> m Text
matchM = forall a b (m :: * -> *).
(RegexLike a b, MonadFail m) =>
a -> b -> m b
polymatchM
instance RegexMaker Regex CompOption ExecOption T.Text where
makeRegexOptsM :: forall (m :: * -> *).
MonadFail m =>
CompOption -> ExecOption -> Text -> m Regex
makeRegexOptsM CompOption
c ExecOption
e Text
source = forall regex compOpt execOpt source (m :: * -> *).
(RegexMaker regex compOpt execOpt source, MonadFail m) =>
compOpt -> execOpt -> source -> m regex
makeRegexOptsM CompOption
c ExecOption
e (Text -> String
T.unpack Text
source)
{-# SPECIALIZE execMatch :: Regex -> Position -> Char -> T.Text -> [MatchArray] #-}
execMatch :: Uncons text => Regex -> Position -> Char -> text -> [MatchArray]
execMatch :: forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
execMatch = forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
Engine.execMatch
{-# SPECIALIZE myMatchTest :: Regex -> T.Text -> Bool #-}
myMatchTest :: Uncons text => Regex -> text -> Bool
myMatchTest :: forall text. Uncons text => Regex -> text -> Bool
myMatchTest = forall text. Uncons text => Regex -> text -> Bool
Tester.matchTest
instance RegexLike Regex T.Text where
matchOnce :: Regex -> Text -> Maybe MatchArray
matchOnce Regex
r Text
s = forall a. [a] -> Maybe a
listToMaybe (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r Text
s)
matchAll :: Regex -> Text -> [MatchArray]
matchAll Regex
r Text
s = forall text.
Uncons text =>
Regex -> Position -> Char -> text -> [MatchArray]
execMatch Regex
r Position
0 Char
'\n' Text
s
matchCount :: Regex -> Text -> Position
matchCount Regex
r Text
s = forall (t :: * -> *) a. Foldable t => t a -> Position
length (forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
r' Text
s)
where r' :: Regex
r' = Regex
r { regex_execOptions :: ExecOption
regex_execOptions = (Regex -> ExecOption
regex_execOptions Regex
r) {captureGroups :: Bool
captureGroups = Bool
False} }
matchTest :: Regex -> Text -> Bool
matchTest = forall text. Uncons text => Regex -> text -> Bool
myMatchTest
matchOnceText :: Regex -> Text -> Maybe (Text, MatchText Text, Text)
matchOnceText Regex
regex Text
source =
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\MatchArray
ma -> let (Position
o,Position
l) = MatchArray
maforall i e. Ix i => Array i e -> i -> e
!Position
0
in (forall source. Extract source => Position -> source -> source
before Position
o Text
source
,forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Position, Position)
ol -> (forall source.
Extract source =>
(Position, Position) -> source -> source
extract (Position, Position)
ol Text
source,(Position, Position)
ol)) MatchArray
ma
,forall source. Extract source => Position -> source -> source
after (Position
oforall a. Num a => a -> a -> a
+Position
l) Text
source))
(forall regex source.
RegexLike regex source =>
regex -> source -> Maybe MatchArray
matchOnce Regex
regex Text
source)
matchAllText :: Regex -> Text -> [MatchText Text]
matchAllText Regex
regex Text
source =
forall a b. (a -> b) -> [a] -> [b]
map (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(Position, Position)
ol -> (forall source.
Extract source =>
(Position, Position) -> source -> source
extract (Position, Position)
ol Text
source,(Position, Position)
ol)))
(forall regex source.
RegexLike regex source =>
regex -> source -> [MatchArray]
matchAll Regex
regex Text
source)
compile :: CompOption
-> ExecOption
-> T.Text
-> Either String Regex
compile :: CompOption -> ExecOption -> Text -> Either String Regex
compile CompOption
compOpt ExecOption
execOpt Text
txt =
case String -> Either ParseError (Pattern, (Position, DoPa))
parseRegex (Text -> String
T.unpack Text
txt) of
Left ParseError
err -> forall a b. a -> Either a b
Left (String
"parseRegex for Text.Regex.TDFA.Text failed:"forall a. [a] -> [a] -> [a]
++forall a. Show a => a -> String
show ParseError
err)
Right (Pattern, (Position, DoPa))
pattern -> forall a b. b -> Either a b
Right ((Pattern, (Position, DoPa)) -> CompOption -> ExecOption -> Regex
patternToRegex (Pattern, (Position, DoPa))
pattern CompOption
compOpt ExecOption
execOpt)
execute :: Regex
-> T.Text
-> Either String (Maybe MatchArray)
execute :: Regex -> Text -> Either String (Maybe MatchArray)
execute Regex
r Text
txt = forall a b. b -> Either a b
Right (forall regex source.
RegexLike regex source =>
regex -> source -> Maybe MatchArray
matchOnce Regex
r Text
txt)
regexec :: Regex
-> T.Text
-> Either String (Maybe (T.Text, T.Text, T.Text, [T.Text]))
regexec :: Regex -> Text -> Either String (Maybe (Text, Text, Text, [Text]))
regexec Regex
r Text
txt = forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$
case forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
matchOnceText Regex
r Text
txt of
Just (Text
pre, MatchText Text
mt, Text
post) | Text
main:[Text]
rest <- forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst (forall i e. Array i e -> [e]
elems MatchText Text
mt)
-> forall a. a -> Maybe a
Just (Text
pre, Text
main, Text
post, [Text]
rest)
Maybe (Text, MatchText Text, Text)
_ -> forall a. Maybe a
Nothing