| 1 |
{-# LANGUAGE NoImplicitPrelude #-} |
| 2 |
{-# LANGUAGE FlexibleContexts #-} |
| 3 |
{-# LANGUAGE CPP #-} |
| 4 |
#if __GLASGOW_HASKELL__ >= 800 |
| 5 |
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-} |
| 6 |
{-# LANGUAGE TemplateHaskellQuotes #-} |
| 7 |
#else |
| 8 |
{-# LANGUAGE QuasiQuotes #-} |
| 9 |
{-# LANGUAGE TemplateHaskell #-} |
| 10 |
#endif |
| 11 |
|
| 12 |
module Text.RE.ZeInternals.SearchReplace.TDFAEdPrime |
| 13 |
( ed' |
| 14 |
) where |
| 15 |
|
| 16 |
import Language.Haskell.TH |
| 17 |
import Language.Haskell.TH.Quote |
| 18 |
import Prelude.Compat |
| 19 |
import Text.RE.REOptions |
| 20 |
import Text.RE.Replace |
| 21 |
import Text.RE.Tools.IsRegex |
| 22 |
import Text.RE.ZeInternals |
| 23 |
import Text.RE.ZeInternals.TDFA |
| 24 |
import Text.RE.ZeInternals.Types.Poss |
| 25 |
|
| 26 |
|
| 27 |
-- | construct a quasi quoter from a casting function and @Just sro@ |
| 28 |
-- if the options are known, otherwise a function take takes the |
| 29 |
-- 'SimpleREOptions' and constructs the 'SearchReplace' template |
| 30 |
ed' :: Q Exp -> Maybe SimpleREOptions -> QuasiQuoter |
| 31 |
ed' qe mb = case mb of |
| 32 |
Nothing -> |
| 33 |
(qq0 "ed'") |
| 34 |
{ quoteExp = parse minBound $ \rs -> AppE <$> qe <*> [|flip unsafe_compile_sr rs|] |
| 35 |
} |
| 36 |
Just sro -> |
| 37 |
(qq0 "ed'") |
| 38 |
{ quoteExp = parse sro $ \rs -> AppE <$> qe <*> [|unsafe_compile_sr_simple sro rs|] |
| 39 |
} |
| 40 |
where |
| 41 |
parse :: SimpleREOptions -> (String->Q Exp) -> String -> Q Exp |
| 42 |
parse sro mk ts = either error (\_->mk ts) ei |
| 43 |
where |
| 44 |
ei :: Either String (SearchReplace RE String) |
| 45 |
ei = poss2either $ compileSearchReplace_ id (poss2either . compileRegexWith sro) ts |
| 46 |
|
| 47 |
unsafe_compile_sr_simple :: IsRegex RE s |
| 48 |
=> SimpleREOptions |
| 49 |
-> String |
| 50 |
-> SearchReplace RE s |
| 51 |
unsafe_compile_sr_simple sro = |
| 52 |
unsafe_compile_sr $ unpackSimpleREOptions sro |
| 53 |
|
| 54 |
unsafe_compile_sr :: (IsOption o, IsRegex RE s) |
| 55 |
=> o |
| 56 |
-> String |
| 57 |
-> SearchReplace RE s |
| 58 |
unsafe_compile_sr os = |
| 59 |
unsafeCompileSearchReplace_ packR $ poss2either . compileRegexWithOptionsForQQ os |