{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}
module Text.ParserCombinators.Poly.StateLazy
(
Parser(P)
, Result(..)
, runParser
, next
, eof
, satisfy
, onFail
, manyFinally
, stUpdate
, stQuery
, stGet
, reparse
, module Text.ParserCombinators.Poly.Base
, module Control.Applicative
) where
import Text.ParserCombinators.Poly.Base hiding (manyFinally)
import Text.ParserCombinators.Poly.Result
import qualified Text.ParserCombinators.Poly.StateParser as P
import Control.Applicative
#if __GLASGOW_HASKELL__
import Control.Exception hiding (bracket)
throwE :: String -> a
throwE msg = throw (ErrorCall msg)
#else
throwE :: String -> a
throwE msg = error msg
#endif
newtype Parser s t a = P (P.Parser s t a)
#ifdef __GLASGOW_HASKELL__
deriving (Functor,Monad,Commitment)
#else
instance Functor (Parser s t) where
fmap f (P p) = P (fmap f p)
instance Monad (Parser s t) where
return x = P (return x)
fail e = P (fail e)
(P f) >>= g = P (f >>= (\(P g')->g') . g)
instance Commitment (Parser s t) where
commit (P p) = P (commit p)
(P p) `adjustErr` f = P (p `adjustErr` f)
#endif
runParser :: Parser s t a -> s -> [t] -> (a, s, [t])
runParser (P (P.P p)) = \s -> fromResult . p s
where
fromResult :: Result (z,s) a -> (a, s, z)
fromResult (Success (z,s) a) = (a, s, z)
fromResult (Failure _ e) = throwE e
fromResult (Committed r) = fromResult r
instance Applicative (Parser s t) where
pure f = return f
(P (P.P pf)) <*> px = P (P.P (\s-> continue . pf s))
where
continue (Success (z,s) f) = let (x,s',z') = runParser px s z
in Success (z',s') (f x)
continue (Failure zs e) = Failure zs e
continue (Committed r) = Committed (continue r)
#if defined(GLASGOW_HASKELL) && GLASGOW_HASKELL > 610
p <* q = p `discard` q
#endif
instance Alternative (Parser s t) where
empty = fail "no parse"
p <|> q = p `onFail` q
instance PolyParse (Parser s t)
next :: Parser s t t
next = P P.next
eof :: Parser s t ()
eof = P P.eof
satisfy :: (t->Bool) -> Parser s t t
satisfy = P . P.satisfy
onFail :: Parser s t a -> Parser s t a -> Parser s t a
onFail (P a) (P b) = P (a `P.onFail` b)
reparse :: [t] -> Parser s t ()
reparse = P . P.reparse
stUpdate :: (s->s) -> Parser s t ()
stUpdate f = P (P.stUpdate f)
stQuery :: (s->a) -> Parser s t a
stQuery f = P (P.stQuery f)
stGet :: Parser s t s
stGet = P (P.stGet)
manyFinally :: Parser s t a -> Parser s t z -> Parser s t [a]
manyFinally p z =
(do x <- p; return (x:) `apply` manyFinally p z)
`onFail`
(do z; return [])
`onFail`
oneOf' [ ("item in sequence", (do p; return []))
, ("sequence terminator", (do z; return [])) ]