Skip to content

Commit

Permalink
Merge pull request #93 from iconnect/prepare-0.10.0.2
Browse files Browse the repository at this point in the history
Tweak Templates, fix release scripts add Escape IsRegex methods
  • Loading branch information
cdornan committed Mar 27, 2017
2 parents 2f519c7 + c659c7e commit 8d263b8
Show file tree
Hide file tree
Showing 69 changed files with 1,704 additions and 562 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
.ghci
.hub
.ghci
stack.yaml
/stack.yaml
README.markdown
tmp

/releases/test-regex-examples

# Haskell

Expand Down
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ matrix:
# For the Stack builds we can pass in arbitrary Stack arguments via the ARGS
# variable, such as using --stack-yaml to point to a different file.

# Linux/stack/hackage release

- env: BUILD=release-stack GHCVER=8.0.2 STACK_YAML=stack-8.0.yaml
compiler: ": #stack 8.0.2"
addons: {apt: {packages: [libgmp-dev]}}

# Linux/stack

- env: BUILD=stack GHCVER=7.10.3 STACK_YAML=stack-7.10.yaml
Expand Down Expand Up @@ -84,6 +78,12 @@ matrix:
os: osx


# Linux/stack/hackage release

- env: BUILD=release-stack GHCVER=8.0.2 STACK_YAML=stack-8.0.yaml
compiler: ": #stack 8.0.2"
addons: {apt: {packages: [libgmp-dev]}}

# Nightly builds are allowed to fail

- env: BUILD=stack STACK_YAML=stack-nightly.yaml
Expand All @@ -97,6 +97,7 @@ matrix:
allow_failures:
- env: BUILD=cabal GHCVER=head CABALVER=head HAPPYVER=1.19.5 ALEXVER=3.1.7
- env: BUILD=stack STACK_YAML=stack-nightly.yaml
- env: BUILD=release-stack GHCVER=8.0.2 STACK_YAML=stack-8.0.yaml
- os: osx


Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ two packages:
- [X] 2017-03-13 v0.6.0.1 [Fix .travis.yml release-stack script](https://github.com/iconnect/regex/issues/67)
- [X] 2017-03-15 v0.7.0.0 [Better organization of API](https://github.com/iconnect/regex/milestone/8)
- [X] 2017-03-16 v0.8.0.0 [Tidy up the API](https://github.com/iconnect/regex/milestone/10)
- [X] 2017-03-18 v0.9.0.0 [Finish tidying up the API, Add type-safe replacement templates and exploit TemplateHaskellQuotes](https://github.com/iconnect/regex/milestone/9)
- [X] 2017-03-24 v0.9.0.0 [Finish tidying up the API, Add type-safe replacement templates and exploit TemplateHaskellQuotes](https://github.com/iconnect/regex/milestone/9)
- [X] 2017-03-27 v0.10.0.2 [Tweak Templates, Fix release scripts and update Haddocks commentary](https://github.com/iconnect/regex/milestone/12)
- [ ] 2017-03-31 v1.0.0.0 [First stable release](https://github.com/iconnect/regex/milestone/3)
- [ ] 2017-08-31 v2.0.0.0 [Fast text replacement with benchmarks](https://github.com/iconnect/regex/milestone/4)

Expand Down
19 changes: 0 additions & 19 deletions Text/RE.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ module Text.RE
-- * How to use this library
-- $use

-- ** The Match Operators
-- $operators

-- * Matches
Matches
, matchesSource
Expand Down Expand Up @@ -74,19 +71,3 @@ import Text.RE.Types.Matches
-- * "Text.RE.PCRE.RE"
-- * "Text.RE.PCRE.Sequence"
-- * "Text.RE.PCRE.String"

-- $operators
--
-- The traditional @=~@ and @=~~@ operators are exported by the above
-- API module, but we recommend that you use the two new operators,
-- especially if you are not familiar with the old operators. We have:
--
-- * @txt ?=~ re@ searches for a single match yielding a value of type
-- 'Match' @a@ where @a@ is the type of the text you are searching.
--
-- * @txt *=~ re@ searches for all non-overlapping matches in @txt@,
-- returning a value of type 'Matches' @a@.
--
-- The remainder of this module outlines these @Matches@ and
-- @Match@ result types. Only an outline is given here. For more details
-- see the 'Text.RE.Type.Matches' and 'Text.RE.Type.Match' modules.
84 changes: 84 additions & 0 deletions Text/RE/Internal/SearchReplace.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# OPTIONS_GHC -fno-warn-redundant-constraints #-}
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Text.RE.Internal.SearchReplace
( unsafeCompileSearchReplace_
, compileSearchReplace_
, compileSearchAndReplace_
) where

import qualified Data.HashMap.Strict as HMS
import Prelude.Compat
import Text.RE.Internal.NamedCaptures
import Text.RE.Types.Capture
import Text.RE.Types.CaptureID
import Text.RE.Types.Matches
import Text.RE.Types.Replace
import Text.RE.Types.SearchReplace
import qualified Text.Regex.TDFA as TDFA


-- | warapper on 'compileSearchReplace_' that will generate an error
-- if any compilation errors are found
unsafeCompileSearchReplace_ :: (String->s)
-> (String->Either String re)
-> String
-> SearchReplace re s
unsafeCompileSearchReplace_ pk cf = either err id . compileSearchReplace_ pk cf
where
err msg = error $ "unsafeCompileSearchReplace_: " ++ msg

-- | compile a SearchReplace template generating errors if the RE or
-- the template are not well formed -- all capture references being checked
compileSearchReplace_ :: (Monad m,Functor m)
=> (String->s)
-> (String->Either String re)
-> String
-> m (SearchReplace re s)
compileSearchReplace_ pack compile_re sr_tpl = either fail return $ do
case mainCaptures $ sr_tpl $=~ "///" of
[cap] ->
compileSearchAndReplace_ pack compile_re
(capturePrefix cap) (captureSuffix cap)
_ -> Left $ "bad search-replace template syntax: " ++ sr_tpl

-- | compile 'SearcgReplace' from two strings containing the RE
-- and the replacement template
compileSearchAndReplace_ :: (Monad m,Functor m)
=> (String->s)
-> (String->Either String re)
-> String
-> String
-> m (SearchReplace re s)
compileSearchAndReplace_ pack compile_re re_s tpl = either fail return $ do
re <- compile_re re_s
((n,cnms),_) <- extractNamedCaptures re_s
mapM_ (check n cnms) $ templateCaptures id tpl
return $ SearchReplace re $ pack tpl
where
check :: Int -> CaptureNames -> CaptureID -> Either String ()
check n cnms cid = case cid of
IsCaptureOrdinal co -> check_co n co
IsCaptureName cn -> check_cn cnms cn

check_co n (CaptureOrdinal i) = case i <= n of
True -> return ()
False -> Left $ "capture ordinal out of range: " ++
show i ++ " >= " ++ show n

check_cn cnms cnm = case cnm `HMS.member` cnms of
True -> return ()
False -> Left $ "capture name not defined: " ++
show (getCaptureName cnm)

($=~) :: String -> String -> Matches String
($=~) = (TDFA.=~)
54 changes: 54 additions & 0 deletions Text/RE/Internal/SearchReplace/PCRE.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
#endif

module Text.RE.Internal.SearchReplace.PCRE
( ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_
) where

import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Prelude.Compat
import Text.RE.Internal.SearchReplace.PCREEdPrime
import Text.RE.Types.REOptions


-- | the @[ed| ... /// ... |]@ quasi quoters
ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_ :: QuasiQuoter

ed = ed' cast $ Just minBound
edMS = edMultilineSensitive
edMI = edMultilineInsensitive
edBS = edBlockSensitive
edBI = edBlockInsensitive
edMultilineSensitive = ed' cast $ Just MultilineSensitive
edMultilineInsensitive = ed' cast $ Just MultilineInsensitive
edBlockSensitive = ed' cast $ Just BlockSensitive
edBlockInsensitive = ed' cast $ Just BlockInsensitive
ed_ = ed' cast Nothing

cast :: Q Exp
cast = [|id|]
58 changes: 58 additions & 0 deletions Text/RE/Internal/SearchReplace/PCRE/ByteString.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
#endif

module Text.RE.Internal.SearchReplace.PCRE.ByteString
( ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_
) where

import qualified Data.ByteString.Char8 as B
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Text.RE.PCRE.RE
import Text.RE.Internal.SearchReplace.PCREEdPrime
import Text.RE.SearchReplace
import Text.RE.Types.REOptions


-- | the @[ed| ... /// ... |]@ quasi quoters
ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_ :: QuasiQuoter

ed = ed' sr_cast $ Just minBound
edMS = edMultilineSensitive
edMI = edMultilineInsensitive
edBS = edBlockSensitive
edBI = edBlockInsensitive
edMultilineSensitive = ed' sr_cast $ Just MultilineSensitive
edMultilineInsensitive = ed' sr_cast $ Just MultilineInsensitive
edBlockSensitive = ed' sr_cast $ Just BlockSensitive
edBlockInsensitive = ed' sr_cast $ Just BlockInsensitive
ed_ = ed' fn_cast Nothing

sr_cast :: Q Exp
sr_cast = [|\x -> x :: SearchReplace RE B.ByteString|]

fn_cast :: Q Exp
fn_cast = [|\x -> x :: SimpleREOptions -> SearchReplace RE B.ByteString|]
58 changes: 58 additions & 0 deletions Text/RE/Internal/SearchReplace/PCRE/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 800
{-# LANGUAGE TemplateHaskellQuotes #-}
#else
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
#endif

module Text.RE.Internal.SearchReplace.PCRE.ByteString.Lazy
( ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_
) where

import qualified Data.ByteString.Lazy.Char8 as LBS
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Text.RE.PCRE.RE
import Text.RE.Internal.SearchReplace.PCREEdPrime
import Text.RE.SearchReplace
import Text.RE.Types.REOptions


-- | the @[ed| ... /// ... |]@ quasi quoters
ed
, edMS
, edMI
, edBS
, edBI
, edMultilineSensitive
, edMultilineInsensitive
, edBlockSensitive
, edBlockInsensitive
, ed_ :: QuasiQuoter

ed = ed' sr_cast $ Just minBound
edMS = edMultilineSensitive
edMI = edMultilineInsensitive
edBS = edBlockSensitive
edBI = edBlockInsensitive
edMultilineSensitive = ed' sr_cast $ Just MultilineSensitive
edMultilineInsensitive = ed' sr_cast $ Just MultilineInsensitive
edBlockSensitive = ed' sr_cast $ Just BlockSensitive
edBlockInsensitive = ed' sr_cast $ Just BlockInsensitive
ed_ = ed' fn_cast Nothing

sr_cast :: Q Exp
sr_cast = [|\x -> x :: SearchReplace RE LBS.ByteString|]

fn_cast :: Q Exp
fn_cast = [|\x -> x :: SimpleREOptions -> SearchReplace RE LBS.ByteString|]
Loading

0 comments on commit 8d263b8

Please sign in to comment.