{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}
module Text.ParserCombinators.Poly.Lazy
(
Parser(P)
, Result(..)
, runParser
, next
, eof
, satisfy
, satisfyMsg
, onFail
, reparse
, module Text.ParserCombinators.Poly.Base
, module Control.Applicative
) where
import Text.ParserCombinators.Poly.Base
import Text.ParserCombinators.Poly.Result
import qualified Text.ParserCombinators.Poly.Parser as P
import Control.Applicative
import qualified Control.Monad.Fail as Fail
#if __GLASGOW_HASKELL__
import Control.Exception hiding (bracket)
throwE :: String -> a
throwE :: String -> a
throwE String
msg = ErrorCall -> a
forall a e. Exception e => e -> a
throw (String -> ErrorCall
ErrorCall String
msg)
#else
throwE :: String -> a
throwE msg = error msg
#endif
newtype Parser t a = P (P.Parser t a)
#ifdef __GLASGOW_HASKELL__
deriving (a -> Parser t b -> Parser t a
(a -> b) -> Parser t a -> Parser t b
(forall a b. (a -> b) -> Parser t a -> Parser t b)
-> (forall a b. a -> Parser t b -> Parser t a)
-> Functor (Parser t)
forall a b. a -> Parser t b -> Parser t a
forall a b. (a -> b) -> Parser t a -> Parser t b
forall t a b. a -> Parser t b -> Parser t a
forall t a b. (a -> b) -> Parser t a -> Parser t b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> Parser t b -> Parser t a
$c<$ :: forall t a b. a -> Parser t b -> Parser t a
fmap :: (a -> b) -> Parser t a -> Parser t b
$cfmap :: forall t a b. (a -> b) -> Parser t a -> Parser t b
Functor,Applicative (Parser t)
a -> Parser t a
Applicative (Parser t)
-> (forall a b. Parser t a -> (a -> Parser t b) -> Parser t b)
-> (forall a b. Parser t a -> Parser t b -> Parser t b)
-> (forall a. a -> Parser t a)
-> Monad (Parser t)
Parser t a -> (a -> Parser t b) -> Parser t b
Parser t a -> Parser t b -> Parser t b
forall t. Applicative (Parser t)
forall a. a -> Parser t a
forall t a. a -> Parser t a
forall a b. Parser t a -> Parser t b -> Parser t b
forall a b. Parser t a -> (a -> Parser t b) -> Parser t b
forall t a b. Parser t a -> Parser t b -> Parser t b
forall t a b. Parser t a -> (a -> Parser t b) -> Parser t b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: a -> Parser t a
$creturn :: forall t a. a -> Parser t a
>> :: Parser t a -> Parser t b -> Parser t b
$c>> :: forall t a b. Parser t a -> Parser t b -> Parser t b
>>= :: Parser t a -> (a -> Parser t b) -> Parser t b
$c>>= :: forall t a b. Parser t a -> (a -> Parser t b) -> Parser t b
$cp1Monad :: forall t. Applicative (Parser t)
Monad,Monad (Parser t)
Monad (Parser t)
-> (forall a. String -> Parser t a) -> MonadFail (Parser t)
String -> Parser t a
forall t. Monad (Parser t)
forall a. String -> Parser t a
forall t a. String -> Parser t a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: String -> Parser t a
$cfail :: forall t a. String -> Parser t a
$cp1MonadFail :: forall t. Monad (Parser t)
Fail.MonadFail,[(String, Parser t a)] -> Parser t a
Parser t a -> Parser t a
Parser t a -> (String -> String) -> Parser t a
(forall a. Parser t a -> Parser t a)
-> (forall a. Parser t a -> (String -> String) -> Parser t a)
-> (forall a. [(String, Parser t a)] -> Parser t a)
-> Commitment (Parser t)
forall a. [(String, Parser t a)] -> Parser t a
forall a. Parser t a -> Parser t a
forall a. Parser t a -> (String -> String) -> Parser t a
forall t a. [(String, Parser t a)] -> Parser t a
forall t a. Parser t a -> Parser t a
forall t a. Parser t a -> (String -> String) -> Parser t a
forall (p :: * -> *).
(forall a. p a -> p a)
-> (forall a. p a -> (String -> String) -> p a)
-> (forall a. [(String, p a)] -> p a)
-> Commitment p
oneOf' :: [(String, Parser t a)] -> Parser t a
$coneOf' :: forall t a. [(String, Parser t a)] -> Parser t a
adjustErr :: Parser t a -> (String -> String) -> Parser t a
$cadjustErr :: forall t a. Parser t a -> (String -> String) -> Parser t a
commit :: Parser t a -> Parser t a
$ccommit :: forall t a. Parser t a -> Parser t a
Commitment)
#else
instance Functor (Parser t) where
fmap f (P p) = P (fmap f p)
instance Monad (Parser t) where
return x = P (return x)
fail = Fail.fail
(P f) >>= g = P (f >>= (\(P g')->g') . g)
instance Fail.MonadFail (Parser t) where
fail e = P (fail e)
instance Commitment (Parser t) where
commit (P p) = P (commit p)
(P p) `adjustErr` f = P (p `adjustErr` f)
#endif
runParser :: Parser t a -> [t] -> (a, [t])
runParser :: Parser t a -> [t] -> (a, [t])
runParser (P (P.P [t] -> Result [t] a
p)) = Result [t] a -> (a, [t])
forall z a. Result z a -> (a, z)
fromResult (Result [t] a -> (a, [t]))
-> ([t] -> Result [t] a) -> [t] -> (a, [t])
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t] -> Result [t] a
p
where
fromResult :: Result z a -> (a, z)
fromResult :: Result z a -> (a, z)
fromResult (Success z
z a
a) = (a
a, z
z)
fromResult (Failure z
z String
e) = String -> (a, z)
forall a. String -> a
throwE String
e
fromResult (Committed Result z a
r) = Result z a -> (a, z)
forall z a. Result z a -> (a, z)
fromResult Result z a
r
instance Applicative (Parser t) where
pure :: a -> Parser t a
pure a
f = a -> Parser t a
forall (m :: * -> *) a. Monad m => a -> m a
return a
f
(P (P.P [t] -> Result [t] (a -> b)
pf)) <*> :: Parser t (a -> b) -> Parser t a -> Parser t b
<*> Parser t a
px = Parser t b -> Parser t b
forall t a. Parser t a -> Parser t a
P (([t] -> Result [t] b) -> Parser t b
forall t a. ([t] -> Result [t] a) -> Parser t a
P.P (Result [t] (a -> b) -> Result [t] b
forall a. Result [t] (a -> a) -> Result [t] a
continue (Result [t] (a -> b) -> Result [t] b)
-> ([t] -> Result [t] (a -> b)) -> [t] -> Result [t] b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t] -> Result [t] (a -> b)
pf))
where
continue :: Result [t] (a -> a) -> Result [t] a
continue (Success [t]
z a -> a
f) = let (a
x,[t]
z') = Parser t a -> [t] -> (a, [t])
forall t a. Parser t a -> [t] -> (a, [t])
runParser Parser t a
px [t]
z
in [t] -> a -> Result [t] a
forall z a. z -> a -> Result z a
Success [t]
z' (a -> a
f a
x)
continue (Committed Result [t] (a -> a)
r) = Result [t] a -> Result [t] a
forall z a. Result z a -> Result z a
Committed (Result [t] (a -> a) -> Result [t] a
continue Result [t] (a -> a)
r)
continue (Failure [t]
z String
e) = [t] -> String -> Result [t] a
forall z a. z -> String -> Result z a
Failure [t]
z String
e
#if defined(GLASGOW_HASKELL) && GLASGOW_HASKELL > 610
p <* q = p `discard` q
#endif
instance Alternative (Parser t) where
empty :: Parser t a
empty = String -> Parser t a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no parse"
(P Parser t a
p) <|> :: Parser t a -> Parser t a -> Parser t a
<|> (P Parser t a
q) = Parser t a -> Parser t a
forall t a. Parser t a -> Parser t a
P (Parser t a
p Parser t a -> Parser t a -> Parser t a
forall t a. Parser t a -> Parser t a -> Parser t a
`P.onFail` Parser t a
q)
instance PolyParse (Parser t)
next :: Parser t t
next :: Parser t t
next = Parser t t -> Parser t t
forall t a. Parser t a -> Parser t a
P Parser t t
forall t. Parser t t
P.next
eof :: Parser t ()
eof :: Parser t ()
eof = Parser t () -> Parser t ()
forall t a. Parser t a -> Parser t a
P Parser t ()
forall t. Parser t ()
P.eof
satisfy :: (t->Bool) -> Parser t t
satisfy :: (t -> Bool) -> Parser t t
satisfy = Parser t t -> Parser t t
forall t a. Parser t a -> Parser t a
P (Parser t t -> Parser t t)
-> ((t -> Bool) -> Parser t t) -> (t -> Bool) -> Parser t t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (t -> Bool) -> Parser t t
forall t. (t -> Bool) -> Parser t t
P.satisfy
satisfyMsg :: Show t => (t->Bool) -> String -> Parser t t
satisfyMsg :: (t -> Bool) -> String -> Parser t t
satisfyMsg t -> Bool
p String
s = Parser t t -> Parser t t
forall t a. Parser t a -> Parser t a
P ((t -> Bool) -> String -> Parser t t
forall t. Show t => (t -> Bool) -> String -> Parser t t
P.satisfyMsg t -> Bool
p String
s)
onFail :: Parser t a -> Parser t a -> Parser t a
onFail :: Parser t a -> Parser t a -> Parser t a
onFail (P Parser t a
a) (P Parser t a
b) = Parser t a -> Parser t a
forall t a. Parser t a -> Parser t a
P (Parser t a
a Parser t a -> Parser t a -> Parser t a
forall t a. Parser t a -> Parser t a -> Parser t a
`P.onFail` Parser t a
b)
reparse :: [t] -> Parser t ()
reparse :: [t] -> Parser t ()
reparse = Parser t () -> Parser t ()
forall t a. Parser t a -> Parser t a
P (Parser t () -> Parser t ())
-> ([t] -> Parser t ()) -> [t] -> Parser t ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t] -> Parser t ()
forall t. [t] -> Parser t ()
P.reparse