-----------------------------------------------------------------------------
-- A Parser monad with access to the 'DynFlags'.
--
-- The 'P' monad only has access to the subset of 'DynFlags'
-- required for parsing Haskell.

-- The parser for C-- requires access to a lot more of the 'DynFlags',
-- so 'PD' provides access to 'DynFlags' via a 'HasDynFlags' instance.
-----------------------------------------------------------------------------
module GHC.Cmm.Monad (
    PD(..)
  , liftP
  , failMsgPD
  ) where

import GHC.Prelude

import Control.Monad

import GHC.Driver.Session
import GHC.Parser.Lexer

newtype PD a = PD { forall a. PD a -> DynFlags -> PState -> ParseResult a
unPD :: DynFlags -> PState -> ParseResult a }

instance Functor PD where
  fmap :: forall a b. (a -> b) -> PD a -> PD b
fmap = (a -> b) -> PD a -> PD b
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM

instance Applicative PD where
  pure :: forall a. a -> PD a
pure = a -> PD a
forall a. a -> PD a
returnPD
  <*> :: forall a b. PD (a -> b) -> PD a -> PD b
(<*>) = PD (a -> b) -> PD a -> PD b
forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap

instance Monad PD where
  >>= :: forall a b. PD a -> (a -> PD b) -> PD b
(>>=) = PD a -> (a -> PD b) -> PD b
forall a b. PD a -> (a -> PD b) -> PD b
thenPD

liftP :: P a -> PD a
liftP :: forall a. P a -> PD a
liftP (P PState -> ParseResult a
f) = (DynFlags -> PState -> ParseResult a) -> PD a
forall a. (DynFlags -> PState -> ParseResult a) -> PD a
PD ((DynFlags -> PState -> ParseResult a) -> PD a)
-> (DynFlags -> PState -> ParseResult a) -> PD a
forall a b. (a -> b) -> a -> b
$ \DynFlags
_ PState
s -> PState -> ParseResult a
f PState
s

failMsgPD :: String -> PD a
failMsgPD :: forall a. String -> PD a
failMsgPD = P a -> PD a
forall a. P a -> PD a
liftP (P a -> PD a) -> (String -> P a) -> String -> PD a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> P a
forall a. String -> P a
failMsgP

returnPD :: a -> PD a
returnPD :: forall a. a -> PD a
returnPD = P a -> PD a
forall a. P a -> PD a
liftP (P a -> PD a) -> (a -> P a) -> a -> PD a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> P a
forall (m :: * -> *) a. Monad m => a -> m a
return

thenPD :: PD a -> (a -> PD b) -> PD b
(PD DynFlags -> PState -> ParseResult a
m) thenPD :: forall a b. PD a -> (a -> PD b) -> PD b
`thenPD` a -> PD b
k = (DynFlags -> PState -> ParseResult b) -> PD b
forall a. (DynFlags -> PState -> ParseResult a) -> PD a
PD ((DynFlags -> PState -> ParseResult b) -> PD b)
-> (DynFlags -> PState -> ParseResult b) -> PD b
forall a b. (a -> b) -> a -> b
$ \DynFlags
d PState
s ->
        case DynFlags -> PState -> ParseResult a
m DynFlags
d PState
s of
                POk PState
s1 a
a         -> PD b -> DynFlags -> PState -> ParseResult b
forall a. PD a -> DynFlags -> PState -> ParseResult a
unPD (a -> PD b
k a
a) DynFlags
d PState
s1
                PFailed PState
s1 -> PState -> ParseResult b
forall a. PState -> ParseResult a
PFailed PState
s1

instance HasDynFlags PD where
   getDynFlags :: PD DynFlags
getDynFlags = (DynFlags -> PState -> ParseResult DynFlags) -> PD DynFlags
forall a. (DynFlags -> PState -> ParseResult a) -> PD a
PD ((DynFlags -> PState -> ParseResult DynFlags) -> PD DynFlags)
-> (DynFlags -> PState -> ParseResult DynFlags) -> PD DynFlags
forall a b. (a -> b) -> a -> b
$ \DynFlags
d PState
s -> PState -> DynFlags -> ParseResult DynFlags
forall a. PState -> a -> ParseResult a
POk PState
s DynFlags
d