Be more selective in which conditionals we invert
[ghc.git] / compiler / cmm / CmmMonad.hs
1 -----------------------------------------------------------------------------
2 -- A Parser monad with access to the 'DynFlags'.
3 --
4 -- The 'P' monad only has access to the subset of of 'DynFlags'
5 -- required for parsing Haskell.
6
7 -- The parser for C-- requires access to a lot more of the 'DynFlags',
8 -- so 'PD' provides access to 'DynFlags' via a 'HasDynFlags' instance.
9 -----------------------------------------------------------------------------
10 module CmmMonad (
11 PD(..)
12 , liftP
13 ) where
14
15 import GhcPrelude
16
17 import Control.Monad
18 import qualified Control.Monad.Fail as MonadFail
19
20 import DynFlags
21 import Lexer
22
23 newtype PD a = PD { unPD :: DynFlags -> PState -> ParseResult a }
24
25 instance Functor PD where
26 fmap = liftM
27
28 instance Applicative PD where
29 pure = returnPD
30 (<*>) = ap
31
32 instance Monad PD where
33 (>>=) = thenPD
34 fail = MonadFail.fail
35
36 instance MonadFail.MonadFail PD where
37 fail = failPD
38
39 liftP :: P a -> PD a
40 liftP (P f) = PD $ \_ s -> f s
41
42 returnPD :: a -> PD a
43 returnPD = liftP . return
44
45 thenPD :: PD a -> (a -> PD b) -> PD b
46 (PD m) `thenPD` k = PD $ \d s ->
47 case m d s of
48 POk s1 a -> unPD (k a) d s1
49 PFailed warnFn span err -> PFailed warnFn span err
50
51 failPD :: String -> PD a
52 failPD = liftP . fail
53
54 instance HasDynFlags PD where
55 getDynFlags = PD $ \d s -> POk s d