packages feed

regex-applicative 0.1.1 → 0.1.2

raw patch · 5 files changed

+42/−70 lines, 5 filesdep ~basedep ~containers

Dependency ranges changed: base, containers

Files

+ CHANGES.md view
@@ -0,0 +1,15 @@+Changes+=======++0.1.2+-----+* Relax the constraint on the containers version++0.1.1+---+* Fix a bug in 'reFoldl' and 'many'+* "Lazy" infinite regexes are no longer supported++0.1+---+* Initial release
Text/Regex/Applicative/Implementation.hs view
@@ -1,49 +1,8 @@ {-# LANGUAGE GADTs, TupleSections, DeriveFunctor #-} module Text.Regex.Applicative.Implementation where-import Control.Applicative hiding (empty)-import qualified Control.Applicative as Applicative+import Control.Applicative import Data.List-import qualified Data.Sequence as Sequence---- | An applicative functor similar to Maybe, but it's '<|>' method honors--- priority.-data Priority a = Priority { priority :: !PrSeq, pValue :: a } | Fail-    deriving (Functor, Show)-type PrSeq = Sequence.Seq PrNum-type PrNum = Int--instance Applicative Priority where-    pure x = Priority Sequence.empty x-    Priority p1 f <*> Priority p2 x = Priority (p1 Sequence.>< p2) (f x)-    _ <*> _ = Fail--instance Alternative Priority where-    empty = Fail-    p@Priority {} <|> Fail = p-    Fail <|> p@Priority {} = p-    Fail <|> Fail = Fail-    p1@Priority {} <|> p2@Priority {} =-        case compare (priority p1) (priority p2) of-            LT -> p2-            GT -> p1-            EQ -> error $-                "Two priorities are the same! Should not happen.\n" ++ show (priority p1)----- Adds priority to the end-withPriority :: PrNum -> Priority a -> Priority a-withPriority p (Priority ps x) = Priority (ps Sequence.|> p) x-withPriority _ Fail = Fail---- Overwrite the priority---setPriority :: PrSeq -> Priority a -> Priority a---- Discards priority information-priorityToMaybe :: Priority a -> Maybe a-priorityToMaybe p =-    case p of-        Priority { pValue = x } -> Just x-        Fail -> Nothing+import Text.Regex.Applicative.Priorities  data Regexp s r a where     Eps :: Regexp s r a@@ -61,41 +20,35 @@  data RegexpNode s r a = RegexpNode     { active :: !Bool-    , empty  :: !(Priority a)+    , skip   :: !(Priority a)     , final_ :: !(Priority r)     , reg    :: !(Regexp s r a)     } -zero = Fail-isOK p =-    case p of-        Fail -> False-        Priority {} -> True- emptyChoice p1 p2 = withPriority 1 p1 <|> withPriority 0 p2 -final r = if active r then final_ r else zero+final r = if active r then final_ r else empty  epsNode :: RegexpNode s r a epsNode = RegexpNode     { active = False-    , empty  = pure $ error "epsNode"-    , final_ = zero+    , skip   = pure $ error "epsNode"+    , final_ = empty     , reg    = Eps     }  symbolNode :: (s -> Bool) -> RegexpNode s r s symbolNode c = RegexpNode     { active = False-    , empty  = zero-    , final_ = zero+    , skip   = empty+    , final_ = empty     , reg    = Symbol c     }  altNode :: RegexpNode s r a -> RegexpNode s r a -> RegexpNode s r a altNode a1 a2 = RegexpNode     { active = active a1 || active a2-    , empty  = empty a1 `emptyChoice` empty a2+    , skip   = skip a1 `emptyChoice` skip a2     , final_ = final a1 <|> final a2     , reg    = Alt a1 a2     }@@ -103,15 +56,15 @@ appNode :: RegexpNode s (a -> r) (a -> b) -> RegexpNode s r a -> RegexpNode s r b appNode a1 a2 = RegexpNode     { active = active a1 || active a2-    , empty  = empty a1 <*> empty a2-    , final_ = final a1 <*> empty a2 <|> final a2+    , skip   = skip a1 <*> skip a2+    , final_ = final a1 <*> skip a2 <|> final a2     , reg    = App a1 a2     }  fmapNode :: (a -> b) -> RegexpNode s r a -> RegexpNode s r b fmapNode f a = RegexpNode     { active = active a-    , empty = fmap f $ empty a+    , skip = fmap f $ skip a     , final_ = final a     , reg = Fmap f a     }@@ -119,23 +72,23 @@ repNode :: (b -> a -> b) -> b -> RegexpNode s (b, b -> r) a -> RegexpNode s r b repNode f b a = RegexpNode     { active = active a-    , empty = withPriority 0 $ pure b+    , skip = withPriority 0 $ pure b     , final_ = withPriority 0 $ (\(b, f) -> f b) <$> final a     , reg = Rep f b a     }  shift :: Priority (a -> r) -> RegexpNode s r a -> s -> RegexpNode s r a-shift Fail r _ | not $ active r = r+shift k r _ | not (active r) && not (isOK k) = r shift k re s =     case reg re of         Eps -> re         Symbol predicate ->-            let f = k <*> if predicate s then pure s else zero+            let f = k <*> if predicate s then pure s else empty             in re { final_ = f, active = isOK f }         Alt a1 a2 -> altNode (shift (withPriority 1 k) a1 s) (shift (withPriority 0 k) a2 s)         App a1 a2 -> appNode             (shift kc a1 s)-            (shift (kc <*> empty a1 <|> final a1) a2 s)+            (shift (kc <*> skip a1 <|> final a1) a2 s)             where kc = fmap (.) k         Fmap f a -> fmapNode f $ shift (fmap (. f) k) a s         Rep f b a -> repNode f b $ shift k' a s@@ -145,6 +98,6 @@                         ((b,) <$> k <|> final a)  match :: RegexpNode s r r -> [s] -> Priority r-match r [] = empty r+match r [] = skip r match r (s:ss) = final $-    foldl' (\r s -> shift zero r s) (shift (pure id) r s) ss+    foldl' (\r s -> shift empty r s) (shift (pure id) r s) ss
Text/Regex/Applicative/Interface.hs view
@@ -4,6 +4,7 @@ import qualified Control.Applicative import Data.Traversable import Text.Regex.Applicative.Implementation+import Text.Regex.Applicative.Priorities  -- | Type of regular expressions that recognize symbols of type @s@ and -- produce a result of type @a@.
Text/Regex/Applicative/Reference.hs view
@@ -13,7 +13,7 @@ {-# LANGUAGE GADTs #-} module Text.Regex.Applicative.Reference (reference) where import Prelude hiding (getChar)-import Text.Regex.Applicative.Implementation hiding (empty)+import Text.Regex.Applicative.Implementation import Text.Regex.Applicative.Interface import Control.Applicative import Control.Monad
regex-applicative.cabal view
@@ -7,8 +7,10 @@ -- The package version. See the Haskell package versioning policy -- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for -- standards guiding when and how versions should be incremented.-Version:             0.1.1 +-- DO NOT FORGET TO UPDATE THE GIT TAG BELOW!!!+Version:             0.1.2+ -- A short (one-line) description of the package. Synopsis:            Regex-based parsing with applicative interface @@ -42,7 +44,7 @@  -- Extra files to be distributed with the package, such as examples or -- a README.-Extra-source-files:  README.md CREDITS.md test.hs+Extra-source-files:  README.md CREDITS.md test.hs CHANGES.md  -- Constraint on the version of Cabal needed to build this package. Cabal-version:       >=1.6@@ -54,14 +56,15 @@ Source-repository this   type:     git   location: git://github.com/feuerbach/regex-applicative.git-  tag:      v0.1+  tag:      v0.1.2  Library   -- Modules exported by the library.   Exposed-modules:     Text.Regex.Applicative, Text.Regex.Applicative.Reference      -- Packages needed in order to build this package.-  Build-depends:       base == 4.3.*, containers == 0.3.*+  Build-depends:       base == 4.3.*,+                       containers >= 0.3 && < 0.5      -- Modules not exported by this package.   Other-modules:       Text.Regex.Applicative.Interface,