\begin{code}
\end{code}
\begin{code}
module Text.RE.Capture
( Matches(..)
, Match(..)
, Capture(..)
, noMatch
, emptyMatchArray
, anyMatches
, countMatches
, matches
, mainCaptures
, matched
, matchedText
, matchCapture
, matchCaptures
, (!$$)
, captureText
, (!$$?)
, captureTextMaybe
, (!$)
, capture
, (!$?)
, captureMaybe
, hasCaptured
, capturePrefix
, captureSuffix
) where
\end{code}
\begin{code}
import Data.Array
import Data.Maybe
import Data.Typeable
import Text.Regex.Base
import Text.RE.CaptureID
infixl 9 !$, !$$
\end{code}
\begin{code}
data Matches a =
Matches
{ matchesSource :: !a
, allMatches :: ![Match a]
}
deriving (Show,Eq,Typeable)
\end{code}
\begin{code}
data Match a =
Match
{ matchSource :: !a
, captureNames :: !CaptureNames
, matchArray :: !(Array CaptureOrdinal (Capture a))
}
deriving (Show,Eq,Typeable)
\end{code}
\begin{code}
data Capture a =
Capture
{ captureSource :: !a
, capturedText :: !a
, captureOffset :: !Int
, captureLength :: !Int
}
deriving (Show,Eq)
\end{code}
\begin{code}
noMatch :: a -> Match a
noMatch t = Match t noCaptureNames emptyMatchArray
emptyMatchArray :: Array CaptureOrdinal (Capture a)
emptyMatchArray = listArray (CaptureOrdinal 0,CaptureOrdinal $ 1) []
\end{code}
\begin{code}
instance Functor Matches where
fmap f Matches{..} =
Matches
{ matchesSource = f matchesSource
, allMatches = map (fmap f) allMatches
}
instance Functor Match where
fmap f Match{..} =
Match
{ matchSource = f matchSource
, captureNames = captureNames
, matchArray = fmap (fmap f) matchArray
}
instance Functor Capture where
fmap f c@Capture{..} =
c
{ captureSource = f captureSource
, capturedText = f capturedText
}
\end{code}
\begin{code}
anyMatches :: Matches a -> Bool
anyMatches = not . null . allMatches
countMatches :: Matches a -> Int
countMatches = length . allMatches
matches :: Matches a -> [a]
matches = map capturedText . mainCaptures
mainCaptures :: Matches a -> [Capture a]
mainCaptures ac = [ capture c0 cs | cs<-allMatches ac ]
where
c0 = IsCaptureOrdinal $ CaptureOrdinal 0
\end{code}
\begin{code}
matched :: Match a -> Bool
matched = isJust . matchCapture
matchedText :: Match a -> Maybe a
matchedText = fmap capturedText . matchCapture
matchCapture :: Match a -> Maybe (Capture a)
matchCapture = fmap fst . matchCaptures
matchCaptures :: Match a -> Maybe (Capture a,[Capture a])
matchCaptures Match{..} = case rangeSize (bounds matchArray) == 0 of
True -> Nothing
False -> Just (matchArray!0,drop 1 $ elems matchArray)
(!$$) :: Match a -> CaptureID -> a
(!$$) = flip captureText
captureText :: CaptureID -> Match a -> a
captureText cid mtch = capturedText $ capture cid mtch
(!$$?) :: Match a -> CaptureID -> Maybe a
(!$$?) = flip captureTextMaybe
captureTextMaybe :: CaptureID -> Match a -> Maybe a
captureTextMaybe cid mtch = do
cap <- mtch !$? cid
case hasCaptured cap of
True -> Just $ capturedText cap
False -> Nothing
(!$) :: Match a -> CaptureID -> Capture a
(!$) = flip capture
capture :: CaptureID -> Match a -> Capture a
capture cid mtch = fromMaybe oops $ mtch !$? cid
where
oops = error $ "capture: out of bounds (" ++ show cid ++ ")"
(!$?) :: Match a -> CaptureID -> Maybe (Capture a)
(!$?) = flip captureMaybe
captureMaybe :: CaptureID -> Match a -> Maybe (Capture a)
captureMaybe cid mtch@Match{..} = do
cap <- case bounds matchArray `inRange` CaptureOrdinal i of
True -> Just $ matchArray ! CaptureOrdinal i
False -> Nothing
case hasCaptured cap of
True -> Just cap
False -> Nothing
where
i = lookupCaptureID cid mtch
lookupCaptureID :: CaptureID -> Match a -> Int
lookupCaptureID cid Match{..} = findCaptureID cid captureNames
\end{code}
\begin{code}
hasCaptured :: Capture a -> Bool
hasCaptured = (>=0) . captureOffset
capturePrefix :: Extract a => Capture a -> a
capturePrefix Capture{..} = before captureOffset captureSource
captureSuffix :: Extract a => Capture a -> a
captureSuffix Capture{..} = after (captureOffset+captureLength) captureSource
\end{code}
\begin{code}
instance
( RegexContext regex source (AllTextSubmatches (Array Int) (source,(Int,Int)))
, RegexLike regex source
) =>
RegexContext regex source (Match source) where
match r s = cvt s $ getAllTextSubmatches $ match r s
matchM r s = do
y <- matchM r s
return $ cvt s $ getAllTextSubmatches y
instance
( RegexContext regex source [MatchText source]
, RegexLike regex source
) =>
RegexContext regex source (Matches source) where
match r s = Matches s $ map (cvt s) $ match r s
matchM r s = do
y <- matchM r s
return $ Matches s $ map (cvt s) y
\end{code}
\begin{code}
cvt :: source -> MatchText source -> Match source
cvt hay arr =
Match
{ matchSource = hay
, captureNames = noCaptureNames
, matchArray =
ixmap (CaptureOrdinal lo,CaptureOrdinal hi) getCaptureOrdinal $
fmap f arr
}
where
(lo,hi) = bounds arr
f (ndl,(off,len)) =
Capture
{ captureSource = hay
, capturedText = ndl
, captureOffset = off
, captureLength = len
}
\end{code}