{-# LANGUAGE DeriveFunctor #-}
module Test.Hspec.Core.Formatters.Pretty.Parser.Parser where

import           Prelude ()
import           Test.Hspec.Core.Compat

newtype Parser token a = Parser { forall token a. Parser token a -> [token] -> Maybe (a, [token])
runParser :: [token] -> Maybe (a, [token]) }
  deriving (forall a b. (a -> b) -> Parser token a -> Parser token b)
-> (forall a b. a -> Parser token b -> Parser token a)
-> Functor (Parser token)
forall a b. a -> Parser token b -> Parser token a
forall a b. (a -> b) -> Parser token a -> Parser token b
forall token a b. a -> Parser token b -> Parser token a
forall token a b. (a -> b) -> Parser token a -> Parser token b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall token a b. (a -> b) -> Parser token a -> Parser token b
fmap :: forall a b. (a -> b) -> Parser token a -> Parser token b
$c<$ :: forall token a b. a -> Parser token b -> Parser token a
<$ :: forall a b. a -> Parser token b -> Parser token a
Functor

instance Applicative (Parser token) where
  pure :: forall a. a -> Parser token a
pure a
a = ([token] -> Maybe (a, [token])) -> Parser token a
forall token a. ([token] -> Maybe (a, [token])) -> Parser token a
Parser (([token] -> Maybe (a, [token])) -> Parser token a)
-> ([token] -> Maybe (a, [token])) -> Parser token a
forall a b. (a -> b) -> a -> b
$ \ [token]
input -> (a, [token]) -> Maybe (a, [token])
forall a. a -> Maybe a
Just (a
a, [token]
input)
  <*> :: forall a b.
Parser token (a -> b) -> Parser token a -> Parser token b
(<*>) = Parser token (a -> b) -> Parser token a -> Parser token b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad (Parser token) where
  return :: forall a. a -> Parser token a
return = a -> Parser token a
forall a. a -> Parser token a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
  Parser token a
p1 >>= :: forall a b.
Parser token a -> (a -> Parser token b) -> Parser token b
>>= a -> Parser token b
p2 = ([token] -> Maybe (b, [token])) -> Parser token b
forall token a. ([token] -> Maybe (a, [token])) -> Parser token a
Parser (([token] -> Maybe (b, [token])) -> Parser token b)
-> ([token] -> Maybe (b, [token])) -> Parser token b
forall a b. (a -> b) -> a -> b
$ Parser token a -> [token] -> Maybe (a, [token])
forall token a. Parser token a -> [token] -> Maybe (a, [token])
runParser Parser token a
p1 ([token] -> Maybe (a, [token]))
-> ((a, [token]) -> Maybe (b, [token]))
-> [token]
-> Maybe (b, [token])
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (a -> [token] -> Maybe (b, [token]))
-> (a, [token]) -> Maybe (b, [token])
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry (Parser token b -> [token] -> Maybe (b, [token])
forall token a. Parser token a -> [token] -> Maybe (a, [token])
runParser (Parser token b -> [token] -> Maybe (b, [token]))
-> (a -> Parser token b) -> a -> [token] -> Maybe (b, [token])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Parser token b
p2)

instance Alternative (Parser token) where
  empty :: forall a. Parser token a
empty = ([token] -> Maybe (a, [token])) -> Parser token a
forall token a. ([token] -> Maybe (a, [token])) -> Parser token a
Parser (([token] -> Maybe (a, [token])) -> Parser token a)
-> ([token] -> Maybe (a, [token])) -> Parser token a
forall a b. (a -> b) -> a -> b
$ Maybe (a, [token]) -> [token] -> Maybe (a, [token])
forall a b. a -> b -> a
const Maybe (a, [token])
forall a. Maybe a
Nothing
  Parser token a
p1 <|> :: forall a. Parser token a -> Parser token a -> Parser token a
<|> Parser token a
p2 = ([token] -> Maybe (a, [token])) -> Parser token a
forall token a. ([token] -> Maybe (a, [token])) -> Parser token a
Parser (([token] -> Maybe (a, [token])) -> Parser token a)
-> ([token] -> Maybe (a, [token])) -> Parser token a
forall a b. (a -> b) -> a -> b
$ \ [token]
input -> Parser token a -> [token] -> Maybe (a, [token])
forall token a. Parser token a -> [token] -> Maybe (a, [token])
runParser Parser token a
p1 [token]
input Maybe (a, [token]) -> Maybe (a, [token]) -> Maybe (a, [token])
forall a. Maybe a -> Maybe a -> Maybe a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser token a -> [token] -> Maybe (a, [token])
forall token a. Parser token a -> [token] -> Maybe (a, [token])
runParser Parser token a
p2 [token]
input

satisfy :: (token -> Bool) -> Parser token token
satisfy :: forall token. (token -> Bool) -> Parser token token
satisfy token -> Bool
p = ([token] -> Maybe (token, [token])) -> Parser token token
forall token a. ([token] -> Maybe (a, [token])) -> Parser token a
Parser (([token] -> Maybe (token, [token])) -> Parser token token)
-> ([token] -> Maybe (token, [token])) -> Parser token token
forall a b. (a -> b) -> a -> b
$ \ [token]
input -> case [token]
input of
  token
t : [token]
ts | token -> Bool
p token
t -> (token, [token]) -> Maybe (token, [token])
forall a. a -> Maybe a
Just (token
t, [token]
ts)
  [token]
_ -> Maybe (token, [token])
forall a. Maybe a
Nothing

sepBy :: Alternative m => m a -> m sep -> m [a]
sepBy :: forall (m :: * -> *) a sep. Alternative m => m a -> m sep -> m [a]
sepBy m a
p m sep
sep = m a -> m sep -> m [a]
forall (m :: * -> *) a sep. Alternative m => m a -> m sep -> m [a]
sepBy1 m a
p m sep
sep m [a] -> m [a] -> m [a]
forall a. m a -> m a -> m a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [a] -> m [a]
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

sepBy1 :: Alternative m => m a -> m sep -> m [a]
sepBy1 :: forall (m :: * -> *) a sep. Alternative m => m a -> m sep -> m [a]
sepBy1 m a
p m sep
sep = (:) (a -> [a] -> [a]) -> m a -> m ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m a
p m ([a] -> [a]) -> m [a] -> m [a]
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> m a -> m [a]
forall a. m a -> m [a]
forall (f :: * -> *) a. Alternative f => f a -> f [a]
many (m sep
sep m sep -> m a -> m a
forall a b. m a -> m b -> m b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> m a
p)

readA :: (Alternative m, Read a) => String -> m a
readA :: forall (m :: * -> *) a. (Alternative m, Read a) => String -> m a
readA = m a -> (a -> m a) -> Maybe a -> m a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe m a
forall a. m a
forall (f :: * -> *) a. Alternative f => f a
empty a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> m a) -> (String -> Maybe a) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe a
forall a. Read a => String -> Maybe a
readMaybe