rex 0.3 → 0.3.1
raw patch · 6 files changed
+117/−21 lines, 6 files
Files
- Bench.hs +14/−5
- README +69/−6
- Test.hs +22/−0
- Text/Regex/PCRE/Precompile.hs +2/−0
- Text/Regex/PCRE/Rex.hs +9/−9
- rex.cabal +1/−1
Bench.hs view
@@ -14,8 +14,17 @@ (concat $ repeat ["+", "/", "-", "*"])) (map show [100..]) -main = defaultMain [- bgroup "math" [ bench "10" $ nf addEm 10- , bench "1000" $ nf addEm 1000- ]- ]+peanoize n = map (\i -> peano . (++"Z") . concat $ replicate i "S ") [0..n]++testPairs n = [parsePair $ "<" ++ replicate i ' ' ++ "a, pair>" | i <- [0..n]]++testDate n = [parseDate $ show i ++ ".10.20" | i <- [1900 .. 1900 + n]]++--NOTE: benchmark time includes test generation++main = defaultMain + [ bgroup "math" [ bench "10" $ nf addEm 10 ]+ , bgroup "peano" [ bench "100" $ nf peanoize 100 ]+ , bgroup "parsePair" [ bench "10" $ nf testPairs 10 ]+ , bgroup "parseDate" [ bench "100" $ nf testDate 100 ]+ ]
README view
@@ -1,11 +1,9 @@ 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.+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: @@ -14,3 +12,68 @@ ./Setup.hs install See the haddock or Text/Regex/PCRE/QQT.hs for documentation.++Some examples (verbatim from Test.hs):++ 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+++ 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++ 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")+++-- 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)++ 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'
Test.hs view
@@ -7,6 +7,28 @@ import qualified Data.ByteString.Char8 as B import Data.Maybe (catMaybes, isJust) +demo =+ do demonstrate "math" math "1 + 3"+ demonstrate "math" math "3 * 2 + 100"+ demonstrate "math" math "20 / 3 + 100 * 2"+ demonstrate "peano" peano "S Z"+ demonstrate "peano" peano "S S S S Z"+ demonstrate "peano" peano "S S Z"+ demonstrate "parsePair" parsePair "<-1, 3>"+ demonstrate "parsePair" parsePair "<-4,3b0>"+ demonstrate "parsePair" parsePair "< a, -30 >"+ demonstrate "parsePair" parsePair "< a, other>"+ demonstrate "parseDate" parseDate "1993.8.10"+ demonstrate "parseDate" parseDate "1993.08.10"+ demonstrate "parseDate" parseDate "2003.02.28"+ demonstrate "parseDate" parseDate "2003.02.27"+ demonstrate "disjunct" disjunct "a"+ demonstrate "disjunct" disjunct "ab"+ demonstrate "disjunct" disjunct "abc"+ print $ "btest: " ++ show btest++demonstrate n f input = putStrLn $ n ++ " \"" ++ input ++ "\" == " ++ show (f input)+ math x = mathl x 0 mathl [] x = x
Text/Regex/PCRE/Precompile.hs view
@@ -32,6 +32,8 @@ import Text.Regex.PCRE.Light import Text.Regex.PCRE.Light.Base +import Debug.Trace+ -- | A type synonym indicating which ByteStrings represent PCRE-format compiled -- data. type CompiledBytes = B.ByteString
Text/Regex/PCRE/Rex.hs view
@@ -22,6 +22,9 @@ -- 4) Precompiles the regular expression at compile time, by calling into the -- PCRE library and storing a 'B.ByteString' literal representation of its state. --+-- NOTE: for some unknown reason this feature is currently broken, and so off by+-- default.+-- -- 5) Compile-time configurable to use different PCRE options, turn off -- precompilation, use 'B.ByteString's, or set a default mapping expression. --@@ -100,15 +103,12 @@ -- general logic for this is a bit complicated, and postponed for a later -- release. -- --- 3) While this error is believed to no longer exist, the following error+-- 3) The following error currently sometimes happens when using precompiled+-- regular expressions. This 'feature' is now off by default until this is+-- fixed. -- -- > <interactive>: out of memory (requested 17584491593728 bytes) ----- Used to occur when evaluating in GHCi, due to a bug in the way precompilation--- worked. If this happens, please report it, and as a temporary work around,--- make your own quasiquoter using \"'rexConf' _ False _ _ _\" to disable--- pre-compilation.--- -- Since pcre-light is a wrapper over a C API, the most efficient interface is -- ByteStrings, as it does not natively speak Haskell lists. The [rex| ... ] -- quasiquoter implicitely packs the input into a bystestring, and unpacks the@@ -173,8 +173,8 @@ -- | Default regular expression quasiquoter for 'String's and 'B.ByteString's, -- respectively. rex, brex :: QuasiQuoter-rex = rexConf False True "id" rexPCREOptions []-brex = rexConf True True "id" rexPCREOptions []+rex = rexConf False False "id" rexPCREOptions []+brex = rexConf True False "id" rexPCREOptions [] -- | This is a 'QuasiQuoter' transformer, which allows for a whitespace-sensitive -- quasi-quoter to be broken over multiple lines. The default 'rex' and@@ -365,7 +365,7 @@ -- | 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 xs = xs+padRight _ 0 _ = [] padRight v i [] = replicate i v padRight v i (x:xs) = x : padRight v (i-1) xs
rex.cabal view
@@ -1,5 +1,5 @@ Name: rex-Version: 0.3+Version: 0.3.1 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,