rex 0.5.2 → 0.6
raw patch · 6 files changed
+184/−153 lines, 6 files
Files
- CHANGES.md +0/−41
- README.md +0/−90
- Text/Regex/PCRE/Rex.hs +34/−19
- changelog.md +57/−0
- readme.md +90/−0
- rex.cabal +3/−3
− CHANGES.md
@@ -1,41 +0,0 @@-0.1: Jul 25 2011------------------ * initial release--0.2: Sep 24 2011------------------ * Added custom configuration of PCRE options.- * Added non-precompiling quasiquoters.- * Fixed a bug where patterns with no captures would fail.- * Decided to remove the defaulting to 'read' - too much magic.--0.3: Sep 25 2011- * Fixed a capture indexing bug, where capture fields which aren't bound would- cause subsequent bound captures to be incorrect.- * Above bug fix actually neatened up code.- * Added configuration of default mapping pattern.--0.4: Oct 11 2012------------------ * Made configuration into a datatype.--0.4.2: Feb 4 2013------------------- * Precompilation bugs fixed by [takano-akio](https://github.com/takano-akio)!--0.4.3: Dec 21 2013-------------------- * Patch from [aavogt](https://github.com/aavogt)! to use haskell-src-exts to- parse view patterns.- * Miscellaneous code cleanups.--0.5: Feb 20 2014------------------ * Changed the configuration datatype to allow custom preprocessing of expr /- pat antiquotes. The default match processing is now "rexView", an- identifier that can be shadowed locally or hidden on import.- * Removed 'maybeRead'.--0.5.1: Feb 20 2014-------------------- * Made the type of the default 'rexView' fully polymorphic.
− README.md
@@ -1,90 +0,0 @@-http://hackage.haskell.org/package/rex--Provides a quasi-quoter for regular expressions which yields a tuple, of -appropriate arity and types, representing the results of the captures. Allows -the user to specify parsers for captures as inline Haskell. Can also be used to-provide typeful pattern matching in function definitions and case patterns.--To build / install:--```-./Setup.hs configure --user-./Setup.hs build-./Setup.hs install-```--See the haddock or `Text/Regex/PCRE/Rex.hs` for documentation.--Some examples (verbatim from Test.hs):--```haskell-math x = mathl x 0--mathl [] x = x-mathl [rex|^ \s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s y-mathl [rex|^\+\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x + y-mathl [rex|^ -\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x - y-mathl [rex|^\*\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x * y-mathl [rex|^ /\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x / y-mathl str x = error str---- math "1 + 3" == 4.0--- math "3 * 2 + 100" == 106.0--- math "20 / 3 + 100 * 2" == 213.33333333333334-```---```haskell-peano :: String -> Maybe Int-peano = [rex|^(?{ length . filter (=='S') } \s* (?:S\s+)*Z)\s*$|]---- peano "S Z" == Just 1--- peano "S S S S Z" == Just 4--- peano "S S Z" == Just 2-```--```haskell-parsePair :: String -> Maybe (String, String)-parsePair = [rex|^<\s* (?{ }[^\s,>]+) \s*,\s* (?{ }[^\s,>]+) \s*>$|]---- parsePair "<-1, 3>" == Just ("-1","3")--- parsePair "<-4,3b0>" == Just ("-4","3b0")--- parsePair "< a, -30 >" == Just ("a","-30")--- parsePair "< a, other>" == Just ("a","other")-```---```haskell--- From http://www.regular-expressions.info/dates.html-parseDate :: String -> Maybe (Int, Int, Int)-parseDate [rex|^(?{ read -> y }(?:19|20)\d\d)[- /.]- (?{ read -> m }0[1-9]|1[012])[- /.]- (?{ read -> d }0[1-9]|[12][0-9]|3[01])$|]- | (d > 30 && (m `elem` [4, 6, 9, 11]))- || (m == 2 &&- (d ==29 && not (mod y 4 == 0 && (mod y 100 /= 0 || mod y 400 == 0)))- || (d > 29)) = Nothing- | otherwise = Just (y, m, d)-parseDate _ = Nothing---- parseDate "1993.8.10" == Nothing--- parseDate "1993.08.10" == Just (1993,8,10)--- parseDate "2003.02.28" == Just (2003,2,28)--- parseDate "2003.02.27" == Just (2003,2,27)-```--```haskell-onNull a f [] = a-onNull _ f xs = f xs--nonNull = onNull Nothing--disjunct [rex| ^(?:(?{nonNull $ Just . head -> a} .)- | (?{nonNull $ Just . head -> b} ..)- | (?{nonNull $ Just . last -> c} ...))$|] =- head $ catMaybes [a, b, c]---- disjunct "a" == 'a'--- disjunct "ab" == 'a'--- disjunct "abc" == 'c'-```
Text/Regex/PCRE/Rex.hs view
@@ -157,7 +157,7 @@ , parsePat , rexParseMode -- * Used by the generated code- , padRight, rexView+ , rexView ) where import Text.Regex.PCRE.Precompile@@ -166,14 +166,14 @@ import Control.Applicative ( (<$>) ) import Control.Arrow ( first )-import Data.ByteString.Char8 ( pack, unpack, empty )+import Data.ByteString.Char8 ( ByteString, pack, unpack, empty ) import Data.Either ( partitionEithers )-import Data.Maybe ( catMaybes, fromJust, isJust )+import Data.Maybe ( catMaybes ) import Data.Char ( isSpace ) import System.IO.Unsafe ( unsafePerformIO ) -import Language.Haskell.TH (Exp(..), ExpQ, Pat(..), PatQ, Lit(..),- mkName, runIO)+import Language.Haskell.TH (Body(..), Dec(..), Exp(..), ExpQ, Pat(..), PatQ, Lit(..),+ mkName, newName, runIO) import Language.Haskell.TH.Quote import Language.Haskell.Meta (toExp,toPat) import Language.Haskell.Exts.Extension (Extension(..), KnownExtension(..))@@ -248,7 +248,9 @@ makeQuasiMultiline (QuasiQuoter a b c d) = QuasiQuoter (a . pre) (b . pre) (c . pre) (d . pre) where- pre = concat . (\(x:xs) -> x : map (dropWhile isSpace) xs) . lines+ pre = removeLineSpaces . lines+ removeLineSpaces [] = []+ removeLineSpaces (x:xs) = x ++ concatMap (dropWhile isSpace) xs -- | A configureable regular-expression QuasiQuoter. Takes the options to pass -- to the PCRE engine, along with 'Bool's to flag 'ByteString' usage and@@ -320,15 +322,27 @@ process = case (null vs, rexByteString) of (True, _) -> [| fmap ( const () ) |]- (_, False) -> [| fmap ($(return maps) . padRight "" pad . map unpack) |]- (_, True) -> [| fmap ($(return maps) . padRight empty pad) |]- pad = cnt + 2- maps = LamE [ListP . (WildP:) $ map VarP vs]- . TupE . map (uncurry AppE)+ (_, False) -> [| fmap ($(maps 'unconsStr)) |]+ (_, True) -> [| fmap ($(maps 'unconsByte)) |]+ maps def = do+ vsName <- newName "vs"+ lets <- makeLets vsName . (WildP:) $ map VarP vs+ return $ LamE [VarP vsName] . LetE lets+ . TupE -- filter out all "Nothing" exprs- . map (first fromJust) . filter (isJust . fst) -- [(Expr, Variable applied to)]- . zip xs $ map VarE vs+ $ [AppE x (VarE v) | (Just x, v) <- zip xs vs]+ where+ makeLets _ [] = return []+ makeLets vsName (y:ys)+ | null ys = return [makeLet WildP] -- special case so we don't create a variable we don't use+ | otherwise = do+ innerVsName <- newName "vs"+ let yLet = makeLet (VarP innerVsName)+ yLets <- makeLets innerVsName ys+ return $ yLet:yLets+ where+ makeLet innerVs = ValD (TupP [y,innerVs]) (NormalB (AppE (VarE def) (VarE vsName))) [] vs = [mkName $ "v" ++ show i | i <- [0..cnt]] -- | Converts @Left@ to @'ParseFailed' 'noLoc'@, and a @Right@ to @'ParseOk'@.@@ -419,12 +433,13 @@ -- Utils -------------------------------------------------------------------------------- --- | Given a desired list-length, if the passed list is too short, it is padded--- with the given element. Otherwise, it trims.-padRight :: a -> Int -> [a] -> [a]-padRight _ 0 _ = []-padRight v i [] = replicate i v-padRight v i (x:xs) = x : padRight v (i-1) xs+unconsStr :: [ByteString] -> (String,[ByteString])+unconsStr [] = ("",[])+unconsStr (x:xs) = (unpack x,xs)++unconsByte :: [ByteString] -> (ByteString,[ByteString])+unconsByte [] = (empty,[])+unconsByte (x:xs) = (x,xs) -- | A default view function used when expression antiquotes are empty, or when -- pattern antiquotes omit a view pattern. See the documentation for
+ changelog.md view
@@ -0,0 +1,57 @@+# Changelog++## 0.6++ * Made the generated code total. See [#10][]++[#10]: https://github.com/mgsloan/rex/issues/10++## 0.5.1 (2014-02-20)++ * Made the type of the default 'rexView' fully polymorphic.++## 0.5: (2014-02-20)++ * Changed the configuration datatype to allow custom preprocessing of expr /+ pat antiquotes. The default match processing is now "rexView", an+ identifier that can be shadowed locally or hidden on import.++ * Removed 'maybeRead'.++## 0.4.3: (2013-12-21)++ * Patch from [aavogt](https://github.com/aavogt)! to use haskell-src-exts to+ parse view patterns.++ * Miscellaneous code cleanups.++## 0.4.2: (2013-02-04)++ * Precompilation bugs fixed by [takano-akio](https://github.com/takano-akio)!++## 0.4: (2012-10-11)++ * Made configuration into a datatype.++## 0.3: (2011-09-25)++ * Fixed a capture indexing bug, where capture fields which aren't bound would+ cause subsequent bound captures to be incorrect.++ * Above bug fix actually neatened up code.++ * Added configuration of default mapping pattern.++## 0.2: (2011-09-24)++ * Added custom configuration of PCRE options.++ * Added non-precompiling quasiquoters.++ * Fixed a bug where patterns with no captures would fail.++ * Decided to remove the defaulting to 'read' - too much magic.++## 0.1: (2011-07-25)++ * initial release
+ readme.md view
@@ -0,0 +1,90 @@+http://hackage.haskell.org/package/rex++Provides a quasi-quoter for regular expressions which yields a tuple, of +appropriate arity and types, representing the results of the captures. Allows +the user to specify parsers for captures as inline Haskell. Can also be used to+provide typeful pattern matching in function definitions and case patterns.++To build / install:++```+./Setup.hs configure --user+./Setup.hs build+./Setup.hs install+```++See the haddock or `Text/Regex/PCRE/Rex.hs` for documentation.++Some examples (verbatim from Test.hs):++```haskell+math x = mathl x 0++mathl [] x = x+mathl [rex|^ \s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s y+mathl [rex|^\+\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x + y+mathl [rex|^ -\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x - y+mathl [rex|^\*\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x * y+mathl [rex|^ /\s*(?{ read -> y }\d+)\s*(?{ s }.*)$|] x = mathl s $ x / y+mathl str x = error str++-- math "1 + 3" == 4.0+-- math "3 * 2 + 100" == 106.0+-- math "20 / 3 + 100 * 2" == 213.33333333333334+```+++```haskell+peano :: String -> Maybe Int+peano = [rex|^(?{ length . filter (=='S') } \s* (?:S\s+)*Z)\s*$|]++-- peano "S Z" == Just 1+-- peano "S S S S Z" == Just 4+-- peano "S S Z" == Just 2+```++```haskell+parsePair :: String -> Maybe (String, String)+parsePair = [rex|^<\s* (?{ }[^\s,>]+) \s*,\s* (?{ }[^\s,>]+) \s*>$|]++-- parsePair "<-1, 3>" == Just ("-1","3")+-- parsePair "<-4,3b0>" == Just ("-4","3b0")+-- parsePair "< a, -30 >" == Just ("a","-30")+-- parsePair "< a, other>" == Just ("a","other")+```+++```haskell+-- From http://www.regular-expressions.info/dates.html+parseDate :: String -> Maybe (Int, Int, Int)+parseDate [rex|^(?{ read -> y }(?:19|20)\d\d)[- /.]+ (?{ read -> m }0[1-9]|1[012])[- /.]+ (?{ read -> d }0[1-9]|[12][0-9]|3[01])$|]+ | (d > 30 && (m `elem` [4, 6, 9, 11]))+ || (m == 2 &&+ (d ==29 && not (mod y 4 == 0 && (mod y 100 /= 0 || mod y 400 == 0)))+ || (d > 29)) = Nothing+ | otherwise = Just (y, m, d)+parseDate _ = Nothing++-- parseDate "1993.8.10" == Nothing+-- parseDate "1993.08.10" == Just (1993,8,10)+-- parseDate "2003.02.28" == Just (2003,2,28)+-- parseDate "2003.02.27" == Just (2003,2,27)+```++```haskell+onNull a f [] = a+onNull _ f xs = f xs++nonNull = onNull Nothing++disjunct [rex| ^(?:(?{nonNull $ Just . head -> a} .)+ | (?{nonNull $ Just . head -> b} ..)+ | (?{nonNull $ Just . last -> c} ...))$|] =+ head $ catMaybes [a, b, c]++-- disjunct "a" == 'a'+-- disjunct "ab" == 'a'+-- disjunct "abc" == 'c'+```
rex.cabal view
@@ -1,5 +1,5 @@ Name: rex-Version: 0.5.2+Version: 0.6 Synopsis: A quasi-quoter for typeful results of regex captures. Description: Provides a quasi-quoter for regular expressions which yields a tuple, of appropriate arity and types,@@ -19,8 +19,8 @@ Build-type: Simple Stability: experimental Extra-source-files: Demo.hs, Bench.hs-Data-files: README.md- CHANGES.md+Data-files: readme.md+ changelog.md Cabal-version: >=1.6 Bug-Reports: http://github.com/mgsloan/rex/issues Source-Repository head