{-# LANGUAGE GADTs #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE CPP #-}
module Text.Regex.Applicative.Types where
import Control.Applicative
import Control.Monad ((<=<))
import Data.Filtrable (Filtrable (..))
import Data.Functor.Identity (Identity (..))
import Data.String
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup
#endif
newtype ThreadId = ThreadId Int
data Thread s r
= Thread
{ forall s r. Thread s r -> ThreadId
threadId_ :: ThreadId
, forall s r. Thread s r -> s -> [Thread s r]
_threadCont :: s -> [Thread s r]
}
| Accept r
threadId :: Thread s r -> Maybe ThreadId
threadId :: forall s r. Thread s r -> Maybe ThreadId
threadId Thread { threadId_ :: forall s r. Thread s r -> ThreadId
threadId_ = ThreadId
i } = ThreadId -> Maybe ThreadId
forall a. a -> Maybe a
Just ThreadId
i
threadId Thread s r
_ = Maybe ThreadId
forall a. Maybe a
Nothing
data Greediness = Greedy | NonGreedy
deriving (Int -> Greediness -> ShowS
[Greediness] -> ShowS
Greediness -> String
(Int -> Greediness -> ShowS)
-> (Greediness -> String)
-> ([Greediness] -> ShowS)
-> Show Greediness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Greediness -> ShowS
showsPrec :: Int -> Greediness -> ShowS
$cshow :: Greediness -> String
show :: Greediness -> String
$cshowList :: [Greediness] -> ShowS
showList :: [Greediness] -> ShowS
Show, ReadPrec [Greediness]
ReadPrec Greediness
Int -> ReadS Greediness
ReadS [Greediness]
(Int -> ReadS Greediness)
-> ReadS [Greediness]
-> ReadPrec Greediness
-> ReadPrec [Greediness]
-> Read Greediness
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
$creadsPrec :: Int -> ReadS Greediness
readsPrec :: Int -> ReadS Greediness
$creadList :: ReadS [Greediness]
readList :: ReadS [Greediness]
$creadPrec :: ReadPrec Greediness
readPrec :: ReadPrec Greediness
$creadListPrec :: ReadPrec [Greediness]
readListPrec :: ReadPrec [Greediness]
Read, Greediness -> Greediness -> Bool
(Greediness -> Greediness -> Bool)
-> (Greediness -> Greediness -> Bool) -> Eq Greediness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Greediness -> Greediness -> Bool
== :: Greediness -> Greediness -> Bool
$c/= :: Greediness -> Greediness -> Bool
/= :: Greediness -> Greediness -> Bool
Eq, Eq Greediness
Eq Greediness =>
(Greediness -> Greediness -> Ordering)
-> (Greediness -> Greediness -> Bool)
-> (Greediness -> Greediness -> Bool)
-> (Greediness -> Greediness -> Bool)
-> (Greediness -> Greediness -> Bool)
-> (Greediness -> Greediness -> Greediness)
-> (Greediness -> Greediness -> Greediness)
-> Ord Greediness
Greediness -> Greediness -> Bool
Greediness -> Greediness -> Ordering
Greediness -> Greediness -> Greediness
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Greediness -> Greediness -> Ordering
compare :: Greediness -> Greediness -> Ordering
$c< :: Greediness -> Greediness -> Bool
< :: Greediness -> Greediness -> Bool
$c<= :: Greediness -> Greediness -> Bool
<= :: Greediness -> Greediness -> Bool
$c> :: Greediness -> Greediness -> Bool
> :: Greediness -> Greediness -> Bool
$c>= :: Greediness -> Greediness -> Bool
>= :: Greediness -> Greediness -> Bool
$cmax :: Greediness -> Greediness -> Greediness
max :: Greediness -> Greediness -> Greediness
$cmin :: Greediness -> Greediness -> Greediness
min :: Greediness -> Greediness -> Greediness
Ord, Int -> Greediness
Greediness -> Int
Greediness -> [Greediness]
Greediness -> Greediness
Greediness -> Greediness -> [Greediness]
Greediness -> Greediness -> Greediness -> [Greediness]
(Greediness -> Greediness)
-> (Greediness -> Greediness)
-> (Int -> Greediness)
-> (Greediness -> Int)
-> (Greediness -> [Greediness])
-> (Greediness -> Greediness -> [Greediness])
-> (Greediness -> Greediness -> [Greediness])
-> (Greediness -> Greediness -> Greediness -> [Greediness])
-> Enum Greediness
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Greediness -> Greediness
succ :: Greediness -> Greediness
$cpred :: Greediness -> Greediness
pred :: Greediness -> Greediness
$ctoEnum :: Int -> Greediness
toEnum :: Int -> Greediness
$cfromEnum :: Greediness -> Int
fromEnum :: Greediness -> Int
$cenumFrom :: Greediness -> [Greediness]
enumFrom :: Greediness -> [Greediness]
$cenumFromThen :: Greediness -> Greediness -> [Greediness]
enumFromThen :: Greediness -> Greediness -> [Greediness]
$cenumFromTo :: Greediness -> Greediness -> [Greediness]
enumFromTo :: Greediness -> Greediness -> [Greediness]
$cenumFromThenTo :: Greediness -> Greediness -> Greediness -> [Greediness]
enumFromThenTo :: Greediness -> Greediness -> Greediness -> [Greediness]
Enum)
data RE s a where
Eps :: RE s ()
Symbol :: ThreadId -> (s -> Maybe a) -> RE s a
Alt :: RE s a -> RE s a -> RE s a
App :: RE s (a -> b) -> RE s a -> RE s b
Fmap :: (a -> b) -> RE s a -> RE s b
CatMaybes :: RE s (Maybe a) -> RE s a
Fail :: RE s a
Rep :: Greediness
-> (b -> a -> b)
-> b
-> RE s a
-> RE s b
Void :: RE s a -> RE s ()
traversePostorder :: forall s a m . Monad m => (forall a . RE s a -> m (RE s a)) -> RE s a -> m (RE s a)
traversePostorder :: forall s a (m :: * -> *).
Monad m =>
(forall a. RE s a -> m (RE s a)) -> RE s a -> m (RE s a)
traversePostorder forall a. RE s a -> m (RE s a)
f = RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go
where
go :: forall a . RE s a -> m (RE s a)
go :: forall a. RE s a -> m (RE s a)
go = RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
f (RE s a -> m (RE s a))
-> (RE s a -> m (RE s a)) -> RE s a -> m (RE s a)
forall (m :: * -> *) b c a.
Monad m =>
(b -> m c) -> (a -> m b) -> a -> m c
<=< \ case
RE s a
Eps -> RE s a -> m (RE s a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RE s a
RE s ()
forall s. RE s ()
Eps
Symbol ThreadId
i s -> Maybe a
p -> RE s a -> m (RE s a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ThreadId -> (s -> Maybe a) -> RE s a
forall s a. ThreadId -> (s -> Maybe a) -> RE s a
Symbol ThreadId
i s -> Maybe a
p)
Alt RE s a
a RE s a
b -> RE s a -> RE s a -> RE s a
forall s a. RE s a -> RE s a -> RE s a
Alt (RE s a -> RE s a -> RE s a) -> m (RE s a) -> m (RE s a -> RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
a m (RE s a -> RE s a) -> m (RE s a) -> m (RE s a)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
b
App RE s (a -> a)
a RE s a
b -> RE s (a -> a) -> RE s a -> RE s a
forall s a b. RE s (a -> b) -> RE s a -> RE s b
App (RE s (a -> a) -> RE s a -> RE s a)
-> m (RE s (a -> a)) -> m (RE s a -> RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s (a -> a) -> m (RE s (a -> a))
forall a. RE s a -> m (RE s a)
go RE s (a -> a)
a m (RE s a -> RE s a) -> m (RE s a) -> m (RE s a)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
b
Fmap a -> a
g RE s a
a -> (a -> a) -> RE s a -> RE s a
forall a b s. (a -> b) -> RE s a -> RE s b
Fmap a -> a
g (RE s a -> RE s a) -> m (RE s a) -> m (RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
a
CatMaybes RE s (Maybe a)
a -> RE s (Maybe a) -> RE s a
forall s a. RE s (Maybe a) -> RE s a
CatMaybes (RE s (Maybe a) -> RE s a) -> m (RE s (Maybe a)) -> m (RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s (Maybe a) -> m (RE s (Maybe a))
forall a. RE s a -> m (RE s a)
go RE s (Maybe a)
a
RE s a
Fail -> RE s a -> m (RE s a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure RE s a
forall s a. RE s a
Fail
Rep Greediness
greed a -> a -> a
g a
b RE s a
a -> Greediness -> (a -> a -> a) -> a -> RE s a -> RE s a
forall b a s. Greediness -> (b -> a -> b) -> b -> RE s a -> RE s b
Rep Greediness
greed a -> a -> a
g a
b (RE s a -> RE s a) -> m (RE s a) -> m (RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
a
Void RE s a
a -> RE s a -> RE s a
RE s a -> RE s ()
forall s a. RE s a -> RE s ()
Void (RE s a -> RE s a) -> m (RE s a) -> m (RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a -> m (RE s a)
forall a. RE s a -> m (RE s a)
go RE s a
a
foldMapPostorder :: Monoid b => (forall a . RE s a -> b) -> RE s a -> b
foldMapPostorder :: forall b s a. Monoid b => (forall a. RE s a -> b) -> RE s a -> b
foldMapPostorder forall a. RE s a -> b
f = (b, RE s a) -> b
forall a b. (a, b) -> a
fst ((b, RE s a) -> b) -> (RE s a -> (b, RE s a)) -> RE s a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. RE s a -> (b, RE s a)) -> RE s a -> (b, RE s a)
forall s a (m :: * -> *).
Monad m =>
(forall a. RE s a -> m (RE s a)) -> RE s a -> m (RE s a)
traversePostorder ((,) (b -> RE s a -> (b, RE s a))
-> (RE s a -> b) -> RE s a -> RE s a -> (b, RE s a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a -> b
forall a. RE s a -> b
f (RE s a -> RE s a -> (b, RE s a))
-> (RE s a -> RE s a) -> RE s a -> (b, RE s a)
forall a b. (RE s a -> a -> b) -> (RE s a -> a) -> RE s a -> b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a -> RE s a
forall a. a -> a
id)
mapRE :: (forall a . RE s a -> RE s a) -> RE s a -> RE s a
mapRE :: forall s a. (forall a. RE s a -> RE s a) -> RE s a -> RE s a
mapRE forall a. RE s a -> RE s a
f = Identity (RE s a) -> RE s a
forall a. Identity a -> a
runIdentity (Identity (RE s a) -> RE s a)
-> (RE s a -> Identity (RE s a)) -> RE s a -> RE s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. RE s a -> Identity (RE s a))
-> RE s a -> Identity (RE s a)
forall s a (m :: * -> *).
Monad m =>
(forall a. RE s a -> m (RE s a)) -> RE s a -> m (RE s a)
traversePostorder (RE s a -> Identity (RE s a)
forall a. a -> Identity a
Identity (RE s a -> Identity (RE s a))
-> (RE s a -> RE s a) -> RE s a -> Identity (RE s a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RE s a -> RE s a
forall a. RE s a -> RE s a
f)
instance Functor (RE s) where
fmap :: forall a b. (a -> b) -> RE s a -> RE s b
fmap a -> b
f RE s a
x = (a -> b) -> RE s a -> RE s b
forall a b s. (a -> b) -> RE s a -> RE s b
Fmap a -> b
f RE s a
x
a
f <$ :: forall a b. a -> RE s b -> RE s a
<$ RE s b
x = a -> RE s a
forall a. a -> RE s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
f RE s a -> RE s b -> RE s a
forall a b. RE s a -> RE s b -> RE s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* RE s b
x
instance Applicative (RE s) where
pure :: forall a. a -> RE s a
pure a
x = a -> () -> a
forall a b. a -> b -> a
const a
x (() -> a) -> RE s () -> RE s a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s ()
forall s. RE s ()
Eps
RE s (a -> b)
a1 <*> :: forall a b. RE s (a -> b) -> RE s a -> RE s b
<*> RE s a
a2 = RE s (a -> b) -> RE s a -> RE s b
forall s a b. RE s (a -> b) -> RE s a -> RE s b
App RE s (a -> b)
a1 RE s a
a2
RE s a
a *> :: forall a b. RE s a -> RE s b -> RE s b
*> RE s b
b = (() -> b -> b) -> RE s (() -> b -> b)
forall a. a -> RE s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((b -> b) -> () -> b -> b
forall a b. a -> b -> a
const b -> b
forall a. a -> a
id) RE s (() -> b -> b) -> RE s () -> RE s (b -> b)
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a -> RE s ()
forall s a. RE s a -> RE s ()
Void RE s a
a RE s (b -> b) -> RE s b -> RE s b
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s b
b
RE s a
a <* :: forall a b. RE s a -> RE s b -> RE s a
<* RE s b
b = (a -> () -> a) -> RE s (a -> () -> a)
forall a. a -> RE s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a -> () -> a
forall a b. a -> b -> a
const RE s (a -> () -> a) -> RE s a -> RE s (() -> a)
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a
a RE s (() -> a) -> RE s () -> RE s a
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s b -> RE s ()
forall s a. RE s a -> RE s ()
Void RE s b
b
instance Alternative (RE s) where
RE s a
a1 <|> :: forall a. RE s a -> RE s a -> RE s a
<|> RE s a
a2 = RE s a -> RE s a -> RE s a
forall s a. RE s a -> RE s a -> RE s a
Alt RE s a
a1 RE s a
a2
empty :: forall a. RE s a
empty = RE s a
forall s a. RE s a
Fail
many :: forall a. RE s a -> RE s [a]
many RE s a
a = [a] -> [a]
forall a. [a] -> [a]
reverse ([a] -> [a]) -> RE s [a] -> RE s [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Greediness -> ([a] -> a -> [a]) -> [a] -> RE s a -> RE s [a]
forall b a s. Greediness -> (b -> a -> b) -> b -> RE s a -> RE s b
Rep Greediness
Greedy ((a -> [a] -> [a]) -> [a] -> a -> [a]
forall a b c. (a -> b -> c) -> b -> a -> c
flip (:)) [] RE s a
a
some :: forall a. RE s a -> RE s [a]
some RE s a
a = (:) (a -> [a] -> [a]) -> RE s a -> RE s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a
a RE s ([a] -> [a]) -> RE s [a] -> RE s [a]
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a -> RE s [a]
forall a. RE s a -> RE s [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many RE s a
a
instance Filtrable (RE s) where
catMaybes :: forall a. RE s (Maybe a) -> RE s a
catMaybes = RE s (Maybe a) -> RE s a
forall s a. RE s (Maybe a) -> RE s a
CatMaybes
instance (char ~ Char, string ~ String) => IsString (RE char string) where
fromString :: String -> RE char string
fromString = String -> RE char string
String -> RE Char String
forall a. Eq a => [a] -> RE a [a]
string
instance Semigroup a => Semigroup (RE s a) where
RE s a
x <> :: RE s a -> RE s a -> RE s a
<> RE s a
y = a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) (a -> a -> a) -> RE s a -> RE s (a -> a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> RE s a
x RE s (a -> a) -> RE s a -> RE s a
forall a b. RE s (a -> b) -> RE s a -> RE s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> RE s a
y
instance Monoid a => Monoid (RE s a) where
mempty :: RE s a
mempty = a -> RE s a
forall a. a -> RE s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
string :: Eq a => [a] -> RE a [a]
string :: forall a. Eq a => [a] -> RE a [a]
string = (a -> RE a a) -> [a] -> RE a [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse a -> RE a a
forall s. Eq s => s -> RE s s
sym
psym :: (s -> Bool) -> RE s s
psym :: forall s. (s -> Bool) -> RE s s
psym s -> Bool
p = (s -> Maybe s) -> RE s s
forall s a. (s -> Maybe a) -> RE s a
msym (\s
s -> if s -> Bool
p s
s then s -> Maybe s
forall a. a -> Maybe a
Just s
s else Maybe s
forall a. Maybe a
Nothing)
msym :: (s -> Maybe a) -> RE s a
msym :: forall s a. (s -> Maybe a) -> RE s a
msym s -> Maybe a
p = ThreadId -> (s -> Maybe a) -> RE s a
forall s a. ThreadId -> (s -> Maybe a) -> RE s a
Symbol (String -> ThreadId
forall a. HasCallStack => String -> a
error String
"Not numbered symbol") s -> Maybe a
p
sym :: Eq s => s -> RE s s
sym :: forall s. Eq s => s -> RE s s
sym s
s = (s -> Bool) -> RE s s
forall s. (s -> Bool) -> RE s s
psym (s
s s -> s -> Bool
forall a. Eq a => a -> a -> Bool
==)