{-# LANGUAGE CPP #-}
module Parsley.Selective (
branch, select,
(>??>), filteredBy, (>?>),
predicate, (<?:>),
conditional, match, (||=),
when, while,
fromMaybeP
) where
import Prelude hiding (pure, (<$>)
#if __GLASGOW_HASKELL__ >= 906
, liftA2
#endif
)
import Data.Function (fix)
import Language.Haskell.TH.Syntax (Lift(..))
import Parsley.Alternative (empty)
import Parsley.Applicative (pure, (<$>), liftA2, unit, constp)
import Parsley.Defunctionalized (Defunc(ID, EQ_H, IF_S, LAM_S, LET_S, APP_H))
import Parsley.Internal (makeQ, Parser)
import Parsley.ParserOps (ParserOps, conditional)
import qualified Parsley.Internal as Internal (branch)
branch :: Parser (Either a b)
-> Parser (a -> c)
-> Parser (b -> c)
-> Parser c
branch :: forall a b c.
Parser (Either a b)
-> Parser (a -> c) -> Parser (b -> c) -> Parser c
branch = forall a b c.
Parser (Either a b)
-> Parser (a -> c) -> Parser (b -> c) -> Parser c
Internal.branch
select :: Parser (Either a b) -> Parser (a -> b) -> Parser b
select :: forall a b. Parser (Either a b) -> Parser (a -> b) -> Parser b
select Parser (Either a b)
p Parser (a -> b)
q = forall a b c.
Parser (Either a b)
-> Parser (a -> c) -> Parser (b -> c) -> Parser c
branch Parser (Either a b)
p Parser (a -> b)
q (forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure forall a1. Defunc (a1 -> a1)
ID)
infixl 4 >??>
(>??>) :: Parser a -> Parser (a -> Bool) -> Parser a
Parser a
px >??> :: forall a. Parser a -> Parser (a -> Bool) -> Parser a
>??> Parser (a -> Bool)
pf = forall a b. Parser (Either a b) -> Parser (a -> b) -> Parser b
select (forall (rep :: Type -> Type) a b c.
ParserOps rep =>
rep (a -> b -> c) -> Parser a -> Parser b -> Parser c
liftA2 forall {a1}. Defunc ((a1 -> Bool) -> a1 -> Either () a1)
g Parser (a -> Bool)
pf Parser a
px) forall a. Parser a
empty
where
g :: Defunc ((a1 -> Bool) -> a1 -> Either () a1)
g =
forall a1 b. (Defunc a1 -> Defunc b) -> Defunc (a1 -> b)
LAM_S forall a b. (a -> b) -> a -> b
$ \Defunc (a1 -> Bool)
f ->
forall a1 b. (Defunc a1 -> Defunc b) -> Defunc (a1 -> b)
LAM_S forall a b. (a -> b) -> a -> b
$ \Defunc a1
x ->
forall a1 a. Defunc a1 -> (Defunc a1 -> Defunc a) -> Defunc a
LET_S Defunc a1
x forall a b. (a -> b) -> a -> b
$ \Defunc a1
x ->
forall a. Defunc Bool -> Defunc a -> Defunc a -> Defunc a
IF_S (forall a1 a. Defunc (a1 -> a) -> Defunc a1 -> Defunc a
APP_H Defunc (a1 -> Bool)
f Defunc a1
x)
(forall a1 a. Defunc (a1 -> a) -> Defunc a1 -> Defunc a
APP_H (forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ forall a b. b -> Either a b
Right [||Right||]) Defunc a1
x)
(forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ (forall a b. a -> Either a b
Left ()) [||Left ()||])
filteredBy :: ParserOps rep => Parser a -> rep (a -> Bool) -> Parser a
filteredBy :: forall (rep :: Type -> Type) a.
ParserOps rep =>
Parser a -> rep (a -> Bool) -> Parser a
filteredBy Parser a
p rep (a -> Bool)
f = Parser a
p forall a. Parser a -> Parser (a -> Bool) -> Parser a
>??> forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure rep (a -> Bool)
f
infixl 4 >?>
(>?>) :: ParserOps rep => Parser a -> rep (a -> Bool) -> Parser a
>?> :: forall (rep :: Type -> Type) a.
ParserOps rep =>
Parser a -> rep (a -> Bool) -> Parser a
(>?>) = forall (rep :: Type -> Type) a.
ParserOps rep =>
Parser a -> rep (a -> Bool) -> Parser a
filteredBy
predicate :: ParserOps rep => rep (a -> Bool) -> Parser a -> Parser b -> Parser b -> Parser b
predicate :: forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> Bool) -> Parser a -> Parser b -> Parser b -> Parser b
predicate rep (a -> Bool)
cond Parser a
p Parser b
t Parser b
e = forall (rep :: Type -> Type) a b.
ParserOps rep =>
[(rep (a -> Bool), Parser b)] -> Parser a -> Parser b -> Parser b
conditional [(rep (a -> Bool)
cond, Parser b
t)] Parser a
p Parser b
e
infixl 4 <?:>
(<?:>) :: Parser Bool -> (Parser a, Parser a) -> Parser a
Parser Bool
cond <?:> :: forall a. Parser Bool -> (Parser a, Parser a) -> Parser a
<?:> (Parser a
p, Parser a
q) = forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> Bool) -> Parser a -> Parser b -> Parser b -> Parser b
predicate forall a1. Defunc (a1 -> a1)
ID Parser Bool
cond Parser a
p Parser a
q
match :: (Eq a, Lift a)
=> [a]
-> Parser a
-> (a -> Parser b)
-> Parser b
-> Parser b
match :: forall a b.
(Eq a, Lift a) =>
[a] -> Parser a -> (a -> Parser b) -> Parser b -> Parser b
match [a]
vs Parser a
p a -> Parser b
f = forall (rep :: Type -> Type) a b.
ParserOps rep =>
[(rep (a -> Bool), Parser b)] -> Parser a -> Parser b -> Parser b
conditional (forall a b. (a -> b) -> [a] -> [b]
map (\a
v -> (forall a1. Eq a1 => Defunc a1 -> Defunc (a1 -> Bool)
EQ_H (forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ a
v [||v||]), a -> Parser b
f a
v)) [a]
vs) Parser a
p
infixl 1 ||=
(||=) :: (Enum a, Bounded a, Eq a, Lift a) => Parser a -> (a -> Parser b) -> Parser b
Parser a
p ||= :: forall a b.
(Enum a, Bounded a, Eq a, Lift a) =>
Parser a -> (a -> Parser b) -> Parser b
||= a -> Parser b
f = forall a b.
(Eq a, Lift a) =>
[a] -> Parser a -> (a -> Parser b) -> Parser b -> Parser b
match [forall a. Bounded a => a
minBound..forall a. Bounded a => a
maxBound] Parser a
p a -> Parser b
f forall a. Parser a
empty
when :: Parser Bool -> Parser () -> Parser ()
when :: Parser Bool -> Parser () -> Parser ()
when Parser Bool
p Parser ()
q = Parser Bool
p forall a. Parser Bool -> (Parser a, Parser a) -> Parser a
<?:> (Parser ()
q, Parser ()
unit)
while :: Parser Bool -> Parser ()
while :: Parser Bool -> Parser ()
while Parser Bool
x = forall a. (a -> a) -> a
fix (Parser Bool -> Parser () -> Parser ()
when Parser Bool
x)
fromMaybeP :: Parser (Maybe a) -> Parser a -> Parser a
fromMaybeP :: forall a. Parser (Maybe a) -> Parser a -> Parser a
fromMaybeP Parser (Maybe a)
pm Parser a
px = forall a b. Parser (Either a b) -> Parser (a -> b) -> Parser b
select (forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ (forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall a b. a -> Either a b
Left ()) forall a b. b -> Either a b
Right) [||maybe (Left ()) Right||] forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser (Maybe a)
pm) (forall a b. Parser a -> Parser (b -> a)
constp Parser a
px)