Debian Codegraph @

Text/RE/ZeInternals/Types/Capture.lhs

raw
1 \begin{code}
2 {-# LANGUAGE RecordWildCards #-}
3 {-# LANGUAGE FlexibleInstances #-}
4 {-# LANGUAGE UndecidableInstances #-}
5 {-# LANGUAGE MultiParamTypeClasses #-}
6 {-# LANGUAGE DeriveDataTypeable #-}
7 \end{code}
8
9 \begin{code}
10 module Text.RE.ZeInternals.Types.Capture
11 ( Capture(..)
12 , hasCaptured
13 , capturePrefix
14 , captureSuffix
15 ) where
16 \end{code}
17
18 \begin{code}
19 import Text.Regex.Base
20 \end{code}
21
22
23
24 \begin{code}
25 -- | the matching of a single sub-expression against part of the source
26 -- text
27 data Capture a =
28 Capture
29 { captureSource :: !a -- ^ the whole text that was searched
30 , capturedText :: !a -- ^ the text that was matched
31 , captureOffset :: !Int -- ^ the number of characters preceding the
32 -- match with -1 used if no text was captured
33 -- by the RE (not even the empty string)
34 , captureLength :: !Int -- ^ the number of chacter in the captured
35 -- sub-string
36 }
37 deriving (Show,Eq)
38 \end{code}
39
40 \begin{code}
41 instance Functor Capture where
42 fmap f c@Capture{..} =
43 c
44 { captureSource = f captureSource
45 , capturedText = f capturedText
46 }
47 \end{code}
48
49 \begin{code}
50 -- | test if the capture has matched any text
51 hasCaptured :: Capture a -> Bool
52 hasCaptured = (>=0) . captureOffset
53
54 -- | returns the text preceding the match
55 capturePrefix :: Extract a => Capture a -> a
56 capturePrefix Capture{..} = before captureOffset captureSource
57
58 -- | returns the text after the match
59 captureSuffix :: Extract a => Capture a -> a
60 captureSuffix Capture{..} = after (captureOffset+captureLength) captureSource
61 \end{code}