Skip to content

Commit

Permalink
Support for MonadFail since GHC 8.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
yxnan committed Sep 1, 2020
1 parent b3e4ea7 commit be69f64
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
9 changes: 9 additions & 0 deletions Text/JSON5.hs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ instance MonadPlus Result where
_ `mplus` x = x
mzero = Error "Result: MonadPlus.empty"

#if __GLASGOW_HASKELL__ >= 808
instance Monad Result where
return x = Ok x
Ok a >>= f = f a
Expand All @@ -144,6 +145,14 @@ instance Monad Result where
instance MonadFail Result where
fail x = Error x

#else
instance Monad Result where
return x = Ok x
fail x = Error x
Ok a >>= f = f a
Error x >>= _ = Error x
#endif

mkError :: String -> Result a
mkError s = Error s

Expand Down
12 changes: 11 additions & 1 deletion Text/JSON5/String.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE CPP #-}
-- | Basic support for working with JSON5 values.

module Text.JSON5.String
(
-- * Parsing
Expand Down Expand Up @@ -55,6 +55,7 @@ instance A.Applicative GetJSON where
pure = return
(<*>) = ap

#if __GLASGOW_HASKELL__ >= 808
instance Monad GetJSON where
return x = GetJSON (\s -> Right (x,s))
GetJSON m >>= f = GetJSON (\s -> case m s of
Expand All @@ -64,6 +65,15 @@ instance Monad GetJSON where
instance MonadFail GetJSON where
fail x = GetJSON (\_ -> Left x)

#else
instance Monad GetJSON where
return x = GetJSON (\s -> Right (x,s))
fail x = GetJSON (\_ -> Left x)
GetJSON m >>= f = GetJSON (\s -> case m s of
Left err -> Left err
Right (a,s1) -> un (f a) s1)
#endif

-- | Run a JSON5 reader on an input String, returning some Haskell value.
-- All input will be consumed.
runGetJSON :: GetJSON a -> String -> Either String a
Expand Down
9 changes: 5 additions & 4 deletions json5hs.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json5hs
version: 0.1.2.2
version: 0.1.3.1
synopsis: Serialising to and from JSON5
description:
The JSON5 Data Interchange Format (JSON5) is a superset of JSON
Expand All @@ -12,14 +12,14 @@ category: Web, Text, JSON5
license: BSD3
license-file: LICENSE
author: Yang X. Nan
maintainer: [email protected]
maintainer: [email protected]
Copyright: 2019 Yang X. Nan
cabal-version: >= 1.6
cabal-version: >= 1.10
build-type: Simple

source-repository head
type: git
location: https://github.com/sakamitz/json5hs.git
location: https://github.com/typowritter/json5hs.git

flag split-base
default: True
Expand All @@ -40,6 +40,7 @@ library
Text.JSON5.Types,
Text.JSON5.String
ghc-options: -Wall -O2
default-language: Haskell2010

if flag(split-base)
if flag(generic)
Expand Down

0 comments on commit be69f64

Please sign in to comment.