{-# 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 :: forall a. String -> a
throwE String
msg = 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 (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
<$ :: forall a b. a -> Parser t b -> Parser t a
$c<$ :: forall t a b. a -> Parser t b -> Parser t a
fmap :: forall a b. (a -> b) -> Parser t a -> Parser t b
$cfmap :: forall t a b. (a -> b) -> Parser t a -> Parser t b
Functor,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 :: forall a. a -> Parser t a
$creturn :: forall t a. a -> Parser t a
>> :: forall a b. Parser t a -> Parser t b -> Parser t b
$c>> :: forall t a b. Parser t a -> Parser t b -> Parser t b
>>= :: forall a 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
Monad,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 :: forall a. String -> Parser t a
$cfail :: forall t a. String -> Parser t a
Fail.MonadFail,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' :: forall a. [(String, Parser t a)] -> Parser t a
$coneOf' :: forall t a. [(String, Parser t a)] -> Parser t a
adjustErr :: forall a. Parser t a -> (String -> String) -> Parser t a
$cadjustErr :: forall t a. Parser t a -> (String -> String) -> Parser t a
commit :: forall a. 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 :: forall t a. Parser t a -> [t] -> (a, [t])
runParser (P (P.P [t] -> Result [t] a
p)) = forall z a. Result z a -> (a, z)
fromResult forall b c a. (b -> c) -> (a -> b) -> a -> c
. [t] -> Result [t] a
p
where
fromResult :: Result z a -> (a, z)
fromResult :: forall z a. Result z a -> (a, z)
fromResult (Success z
z a
a) = (a
a, z
z)
fromResult (Failure z
z String
e) = forall a. String -> a
throwE String
e
fromResult (Committed Result z a
r) = forall z a. Result z a -> (a, z)
fromResult Result z a
r
instance Applicative (Parser t) where
pure :: forall a. a -> Parser t a
pure a
f = forall (m :: * -> *) a. Monad m => a -> m a
return a
f
(P (P.P [t] -> Result [t] (a -> b)
pf)) <*> :: forall a b. Parser t (a -> b) -> Parser t a -> Parser t b
<*> Parser t a
px = forall t a. Parser t a -> Parser t a
P (forall t a. ([t] -> Result [t] a) -> Parser t a
P.P (forall {a}. Result [t] (a -> a) -> Result [t] a
continue 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') = forall t a. Parser t a -> [t] -> (a, [t])
runParser Parser t a
px [t]
z
in forall z a. z -> a -> Result z a
Success [t]
z' (a -> a
f a
x)
continue (Committed Result [t] (a -> a)
r) = 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) = 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 :: forall a. Parser t a
empty = forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"no parse"
(P Parser t a
p) <|> :: forall a. Parser t a -> Parser t a -> Parser t a
<|> (P Parser t a
q) = forall t a. Parser t a -> Parser t a
P (Parser t a
p 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 :: forall t. Parser t t
next = forall t a. Parser t a -> Parser t a
P forall t. Parser t t
P.next
eof :: Parser t ()
eof :: forall t. Parser t ()
eof = forall t a. Parser t a -> Parser t a
P forall t. Parser t ()
P.eof
satisfy :: (t->Bool) -> Parser t t
satisfy :: forall t. (t -> Bool) -> Parser t t
satisfy = forall t a. Parser t a -> Parser t a
P forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. (t -> Bool) -> Parser t t
P.satisfy
satisfyMsg :: Show t => (t->Bool) -> String -> Parser t t
satisfyMsg :: forall t. Show t => (t -> Bool) -> String -> Parser t t
satisfyMsg t -> Bool
p String
s = forall t a. Parser t a -> Parser t a
P (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 :: forall t a. Parser t a -> Parser t a -> Parser t a
onFail (P Parser t a
a) (P Parser t a
b) = forall t a. Parser t a -> Parser t a
P (Parser t a
a forall t a. Parser t a -> Parser t a -> Parser t a
`P.onFail` Parser t a
b)
reparse :: [t] -> Parser t ()
reparse :: forall t. [t] -> Parser t ()
reparse = forall t a. Parser t a -> Parser t a
P forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. [t] -> Parser t ()
P.reparse