Skip to content

Commit 27bddf1

Browse files
vaibhavsagarmpickering
authored andcommitted
Add MonadFail instances to ParseMonad.hs
1 parent 18370d1 commit 27bddf1

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

haskell-src-exts.cabal

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ Library
4949
base >= 4.5 && < 5,
5050
-- this is needed to access GHC.Generics on GHC 7.4
5151
ghc-prim
52-
-- this is needed to access Data.Semigroup on GHCs before 8.0
52+
-- this is needed to access Data.Semigroup and Control.Monad.Fail on GHCs
53+
-- before 8.0
5354
if !impl(ghc >= 8.0)
5455
Build-Depends:
55-
semigroups >= 0.18.3
56+
semigroups >= 0.18.3,
57+
fail == 4.9.*
5658

5759
Exposed-modules: Language.Haskell.Exts,
5860
Language.Haskell.Exts.Lexer,

src/Language/Haskell/Exts/ParseMonad.hs

+10-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import Language.Haskell.Exts.Extension -- (Extension, impliesExts, haskell2010)
4545
import Data.List (intercalate)
4646
import Control.Applicative
4747
import Control.Monad (when, liftM, ap)
48+
import qualified Control.Monad.Fail as Fail
4849
import Data.Monoid hiding ((<>))
4950
import Data.Semigroup (Semigroup(..))
5051
-- To avoid import warnings for Control.Applicative, Data.Monoid, and Data.Semigroup
@@ -95,9 +96,11 @@ instance Applicative ParseResult where
9596

9697
instance Monad ParseResult where
9798
return = ParseOk
98-
fail = ParseFailed noLoc
99+
fail = Fail.fail
99100
ParseOk x >>= f = f x
100101
ParseFailed loc msg >>= _ = ParseFailed loc msg
102+
instance Fail.MonadFail ParseResult where
103+
fail = ParseFailed noLoc
101104

102105
instance Semigroup m => Semigroup (ParseResult m) where
103106
ParseOk x <> ParseOk y = ParseOk $ x <> y
@@ -243,6 +246,9 @@ instance Monad P where
243246
case m i x y l ch s mode of
244247
Failed loc msg -> Failed loc msg
245248
Ok s' a -> runP (k a) i x y l ch s' mode
249+
fail = Fail.fail
250+
251+
instance Fail.MonadFail P where
246252
fail s = P $ \_r _col _line loc _ _stk _m -> Failed loc s
247253

248254
atSrcLoc :: P a -> SrcLoc -> P a
@@ -348,6 +354,9 @@ instance Monad (Lex r) where
348354
return a = Lex $ \k -> k a
349355
Lex v >>= f = Lex $ \k -> v (\a -> runL (f a) k)
350356
Lex v >> Lex w = Lex $ \k -> v (\_ -> w k)
357+
fail = Fail.fail
358+
359+
instance Fail.MonadFail (Lex r) where
351360
fail s = Lex $ \_ -> fail s
352361

353362
-- Operations on this monad

0 commit comments

Comments
 (0)