Debian Codegraph @

Text/RE/ZeInternals/Types/IsRegex.lhs

raw
1 \begin{code}
2 {-# LANGUAGE RecordWildCards #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE AllowAmbiguousTypes #-}
5 {-# LANGUAGE CPP #-}
6 #if __GLASGOW_HASKELL__ >= 800
7 {-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
8 {-# OPTIONS_GHC -fno-warn-unused-imports #-}
9 #endif
10
11 module Text.RE.ZeInternals.Types.IsRegex
12 ( IsRegex(..)
13 , SearchReplace(..)
14 , searchReplaceAll
15 , searchReplaceFirst
16 ) where
17
18 import Control.Monad.Fail
19 import Text.RE.REOptions
20 import Text.RE.Replace
21 import Text.RE.ZeInternals.EscapeREString
22 import Text.RE.ZeInternals.Types.SearchReplace
23 \end{code}
24
25 \begin{code}
26 -- | the 'IsRegex' class allows polymorhic tools to be written that
27 -- will work with a variety of regex back ends and text types
28 class Replace s => IsRegex re s where
29 -- | finding the first match
30 matchOnce :: re -> s -> Match s
31 -- | finding all matches
32 matchMany :: re -> s -> Matches s
33 -- | compiling an RE, failing if the RE is not well formed
34 makeRegex :: (Functor m,Monad m, MonadFail m) => s -> m re
35 -- | comiling an RE, specifying the 'SimpleREOptions'
36 makeRegexWith :: (Functor m,Monad m, MonadFail m) => SimpleREOptions -> s -> m re
37 -- | compiling a 'SearchReplace' template from the RE text and the template Text, failing if they are not well formed
38 makeSearchReplace :: (Functor m,Monad m, MonadFail m,IsRegex re s) => s -> s -> m (SearchReplace re s)
39 -- | compiling a 'SearchReplace' template specifying the 'SimpleREOptions' for the RE
40 makeSearchReplaceWith :: (Functor m,Monad m, MonadFail m,IsRegex re s) => SimpleREOptions -> s -> s -> m (SearchReplace re s)
41 -- | incorporate an escaped string into a compiled RE with the default options
42 makeEscaped :: (Functor m,Monad m, MonadFail m) => (s->s) -> s -> m re
43 -- | incorporate an escaped string into a compiled RE with the specified 'SimpleREOptions'
44 makeEscapedWith :: (Functor m,Monad m, MonadFail m) => SimpleREOptions -> (s->s) -> s -> m re
45 -- | extract the text of the RE from the RE
46 regexSource :: re -> s
47
48 makeRegex = makeRegexWith minBound
49 makeSearchReplace = makeSearchReplaceWith minBound
50 makeEscaped = makeEscapedWith minBound
51 makeEscapedWith o f = makeRegexWith o . f . packR . escapeREString . unpackR
52 \end{code}
53
54 \begin{code}
55 -- | search and replace all matches in the argument text; e.g., this function
56 -- will convert every YYYY-MM-DD format date in its argument text into a
57 -- DD\/MM\/YYYY date:
58 --
59 -- @searchReplaceAll [ed|${y}([0-9]{4})-0*${m}([0-9]{2})-0*${d}([0-9]{2})\/\/\/${d}\/${m}\/${y}|]@
60 --
61 searchReplaceAll :: IsRegex re s => SearchReplace re s -> s -> s
62 searchReplaceAll SearchReplace{..} = replaceAll getTemplate . matchMany getSearch
63
64 -- | search and replace the first occurrence only (if any) in the input text
65 -- e.g., to prefix the first string of four hex digits in the imput text,
66 -- if any, with @0x@:
67 --
68 -- @searchReplaceFirst [ed|[0-9A-Fa-f]{4}\/\/\/0x$0|]@
69 --
70 searchReplaceFirst :: IsRegex re s => SearchReplace re s -> s -> s
71 searchReplaceFirst SearchReplace{..} = replace getTemplate . matchOnce getSearch
72 \end{code}