packages feed

regex-tdfa-quasiquoter (empty) → 0.1.0.0

raw patch · 4 files changed

+228/−0 lines, 4 filesdep +basedep +regex-tdfadep +template-haskellsetup-changed

Dependencies added: base, regex-tdfa, template-haskell

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Eric Brisco
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Eric Brisco nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple
+main = defaultMain
+ Text/Regex/TDFA/QuasiQuoter.hs view
@@ -0,0 +1,163 @@+{-|
+See the documentation of 're'.
+-}
+{-# LANGUAGE StandaloneDeriving #-}
+{-# LANGUAGE DeriveDataTypeable #-}
+module Text.Regex.TDFA.QuasiQuoter
+( re
+, unescape
+) where
+
+import Prelude
+  ( Maybe(..)
+  , Either(..)
+  , String
+  , (++)
+  , all
+  , otherwise
+  , error
+  , const
+  , show
+  )
+--
+
+import Numeric
+  ( readHex
+  )
+--
+
+import Data.Char
+  ( isHexDigit
+  , chr
+  )
+--
+
+import Language.Haskell.TH.Quote
+  ( QuasiQuoter(..)
+  , dataToExpQ
+  )
+--
+
+import Text.Regex.TDFA.ReadRegex
+  ( parseRegex
+  )
+--
+
+import Text.Regex.TDFA.Pattern
+  ( Pattern(..)
+  , PatternSet(..)
+  , PatternSetCharacterClass(..)
+  , PatternSetCollatingElement(..)
+  , PatternSetEquivalenceClass(..)
+  )
+--
+
+import Text.Regex.TDFA.Common
+  ( DoPa(..)
+  )
+--
+
+import Data.Typeable
+  ( Typeable
+  )
+--
+
+import Data.Data
+  ( Data
+  )
+--
+
+deriving instance Typeable PatternSetEquivalenceClass
+deriving instance Data PatternSetEquivalenceClass
+
+deriving instance Typeable PatternSetCollatingElement
+deriving instance Data PatternSetCollatingElement
+
+deriving instance Typeable PatternSetCharacterClass
+deriving instance Data PatternSetCharacterClass
+
+deriving instance Typeable DoPa
+deriving instance Data DoPa
+
+deriving instance Typeable PatternSet
+deriving instance Data PatternSet
+
+deriving instance Typeable Pattern
+deriving instance Data Pattern
+
+-- | Quasi-quoter for "Text.Regex.TDFA" (extended POSIX) regular
+--   expressions. Refer to that module's documentation for more
+--   information on supported features.
+--
+--   See the documentation of 'unescape' for escape sequences this
+--   quasi-quoter supports.
+--
+--   @[re|regexp]@ is the parsed 'Text.Regex.TDFA.Pattern.Pattern' (AST)
+--   of @regexp@. A parse failure is a compile-time error.
+--
+re :: QuasiQuoter
+re = QuasiQuoter { quoteExp  = quoter
+                 , quotePat  = error "no quotePat"
+                 , quoteType = error "no quoteType"
+                 , quoteDec  = error "no quoteDec"
+                 }
+  where
+  quoter txt = dataToExpQ (const Nothing) pat
+    where
+    pat = case parseRegex (unescape txt) of
+            Right pat -> pat
+            Left  err -> error (show err)
+--
+
+-- | Replaces escape sequences with their respective characters. Any
+--   sequence not listed will be left as-is.
+--
+-- @
+-- Sequence  | Character
+-- ----------+--------------------
+-- \\\\      | \\
+-- \\n       | Newline
+-- \\r       | Carriage return
+-- \\t       | Horizontal tab
+-- \\f       | Form feed
+-- \\v       | Vertical tab
+-- \\xFFFF   | Code point (in hex)
+-- |~]       | |]
+-- \\|~]     | |~]
+-- @
+--
+-- Note that if you are reading the source file and not the generated
+-- Haddock documentation that the backslashes have been doubled up.
+--
+unescape :: String -> String
+unescape = unescaped
+  where
+  
+  delim ('|':'~':']':xs) = Just ("|]", xs)
+  delim _                = Nothing
+  
+  control xxs@(d1:d2:d3:d4:xs)
+    | all isHexDigit ds = Just ([chr v], xs)
+    | otherwise         = Nothing
+    where ds = [d1,d2,d3,d4]
+          (v,_):_ = readHex ds
+  control _ = Nothing
+  
+  escaped ('\\':xs) = Just ("\\", xs)
+  escaped ('n' :xs) = Just ("\n", xs)
+  escaped ('r' :xs) = Just ("\r", xs)
+  escaped ('t' :xs) = Just ("\t", xs)
+  escaped ('f' :xs) = Just ("\f", xs)
+  escaped ('v' :xs) = Just ("\v", xs)
+  escaped ('x' :xs) = control xs
+  escaped ('|':'~':']':xs) = Just ("|~]", xs)
+  escaped _         = Nothing
+  
+  unescaped ('\\':xs)  = case escaped xs of
+                           Just (cs, xs') -> cs ++ unescaped xs'
+                           Nothing        -> '\\' : unescaped xs
+  unescaped xxs@(x:xs) = case delim xxs of
+                           Just (cs, xs') -> cs ++ unescaped xs'
+                           Nothing        -> x : unescaped xs
+  unescaped []         = []
+--
+ regex-tdfa-quasiquoter.cabal view
@@ -0,0 +1,33 @@+name:                regex-tdfa-quasiquoter
+version:             0.1.0.0
+synopsis:            Quasi-quoter for TDFA (extended POSIX) regular
+                     expressions.
+description:         Quasi-quoter for TDFA (extended POSIX) regular
+                     expressions.
+homepage:            http://github.com/erisco/regex-tdfa-quasiquoter
+license:             BSD3
+license-file:        LICENSE
+author:              Eric Brisco
+maintainer:          eric.brisco@gmail.com
+copyright:           Copyright (c) 2015, Eric Brisco
+category:            Text
+build-type:          Simple
+cabal-version:       >=1.10
+
+source-repository this
+  type:         git
+  location:     http://github.com/erisco/regex-tdfa-quasiquoter.git
+  tag:          0.1.0.0
+
+library
+  
+  exposed-modules:     Text.Regex.TDFA.QuasiQuoter
+  
+  other-extensions:      StandaloneDeriving
+                       , DeriveDataTypeable
+                       
+  build-depends:         base >=4.7 && <4.8
+                       , template-haskell >=2.9 && <2.10
+                       , regex-tdfa >=1.2 && <1.3
+  
+  default-language:    Haskell2010