{-# LANGUAGE PatternSynonyms #-}
module Parsley.Alternative (
(<|>), empty,
(<+>), option, optionally, optional, choice, maybeP, manyTill
) where
import Prelude hiding (pure, (<$>))
import Parsley.Applicative (pure, (<$>), ($>), (<:>))
import Parsley.Internal (makeQ, Parser, Defunc(EMPTY), pattern UNIT, ParserOps, (<|>), empty)
infixl 3 <+>
(<+>) :: Parser a -> Parser b -> Parser (Either a b)
Parser a
p <+> :: Parser a -> Parser b -> Parser (Either a b)
<+> Parser b
q = (a -> Either a b)
-> Code (a -> Either a b) -> Defunc (a -> Either a b)
forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ a -> Either a b
forall a b. a -> Either a b
Left [||Left||] Defunc (a -> Either a b) -> Parser a -> Parser (Either a b)
forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser a
p Parser (Either a b) -> Parser (Either a b) -> Parser (Either a b)
forall a. Parser a -> Parser a -> Parser a
<|> (b -> Either a b)
-> Code (b -> Either a b) -> Defunc (b -> Either a b)
forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ b -> Either a b
forall a b. b -> Either a b
Right [||Right||] Defunc (b -> Either a b) -> Parser b -> Parser (Either a b)
forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser b
q
option :: ParserOps rep => rep a -> Parser a -> Parser a
option :: rep a -> Parser a -> Parser a
option rep a
x Parser a
p = Parser a
p Parser a -> Parser a -> Parser a
forall a. Parser a -> Parser a -> Parser a
<|> rep a -> Parser a
forall (rep :: Type -> Type) a. ParserOps rep => rep a -> Parser a
pure rep a
x
optionally :: ParserOps rep => Parser a -> rep b -> Parser b
optionally :: Parser a -> rep b -> Parser b
optionally Parser a
p rep b
x = rep b -> Parser b -> Parser b
forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option rep b
x (Parser a
p Parser a -> rep b -> Parser b
forall (rep :: Type -> Type) a b.
ParserOps rep =>
Parser a -> rep b -> Parser b
$> rep b
x)
optional :: Parser a -> Parser ()
optional :: Parser a -> Parser ()
optional = (Parser a -> Defunc () -> Parser ())
-> Defunc () -> Parser a -> Parser ()
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser a -> Defunc () -> Parser ()
forall (rep :: Type -> Type) a b.
ParserOps rep =>
Parser a -> rep b -> Parser b
optionally Defunc ()
UNIT
choice :: [Parser a] -> Parser a
choice :: [Parser a] -> Parser a
choice = (Parser a -> Parser a -> Parser a)
-> Parser a -> [Parser a] -> Parser a
forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr Parser a -> Parser a -> Parser a
forall a. Parser a -> Parser a -> Parser a
(<|>) Parser a
forall a. Parser a
empty
maybeP :: Parser a -> Parser (Maybe a)
maybeP :: Parser a -> Parser (Maybe a)
maybeP Parser a
p = Defunc (Maybe a) -> Parser (Maybe a) -> Parser (Maybe a)
forall (rep :: Type -> Type) a.
ParserOps rep =>
rep a -> Parser a -> Parser a
option (Maybe a -> Code (Maybe a) -> Defunc (Maybe a)
forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ Maybe a
forall a. Maybe a
Nothing [||Nothing||]) ((a -> Maybe a) -> Code (a -> Maybe a) -> Defunc (a -> Maybe a)
forall (q :: Type -> Type) a. Quapplicative q => a -> Code a -> q a
makeQ a -> Maybe a
forall a. a -> Maybe a
Just [||Just||] Defunc (a -> Maybe a) -> Parser a -> Parser (Maybe a)
forall (rep :: Type -> Type) a b.
ParserOps rep =>
rep (a -> b) -> Parser a -> Parser b
<$> Parser a
p)
manyTill :: Parser a -> Parser b -> Parser [a]
manyTill :: Parser a -> Parser b -> Parser [a]
manyTill Parser a
p Parser b
end = let go :: Parser [a]
go = Parser b
end Parser b -> Defunc [a] -> Parser [a]
forall (rep :: Type -> Type) a b.
ParserOps rep =>
Parser a -> rep b -> Parser b
$> Defunc [a]
forall a. Defunc [a]
EMPTY Parser [a] -> Parser [a] -> Parser [a]
forall a. Parser a -> Parser a -> Parser a
<|> Parser a
p Parser a -> Parser [a] -> Parser [a]
forall a. Parser a -> Parser [a] -> Parser [a]
<:> Parser [a]
go in Parser [a]
go