1 -----------------------------------------------------------------------------
3 -- Module : Distribution.ReadE
4 -- Copyright : Jose Iborra 2008
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
10 -- Simple parsing with failure
12 module Distribution
.ReadE
(
14 ReadE
(..), succeedReadE
, failReadE
,
16 parseReadE
, readEOrFail
,
21 import Distribution
.Compat
.Prelude
23 import Distribution
.Compat
.ReadP
25 -- | Parser with simple error reporting
26 newtype ReadE a
= ReadE
{runReadE
:: String -> Either ErrorMsg a
}
27 type ErrorMsg
= String
29 instance Functor ReadE
where
30 fmap f
(ReadE p
) = ReadE
$ \txt
-> case p txt
of
31 Right a
-> Right
(f a
)
34 succeedReadE
:: (String -> a
) -> ReadE a
35 succeedReadE f
= ReadE
(Right
. f
)
37 failReadE
:: ErrorMsg
-> ReadE a
38 failReadE
= ReadE
. const . Left
40 parseReadE
:: ReadE a
-> ReadP r a
41 parseReadE
(ReadE p
) = do
43 either fail return (p txt
)
45 readEOrFail
:: ReadE a
-> String -> a
46 readEOrFail r
= either error id . runReadE r
48 readP_to_E
:: (String -> ErrorMsg
) -> ReadP a a
-> ReadE a
50 ReadE
$ \txt
-> case [ p |
(p
, s
) <- readP_to_S r txt
52 of [] -> Left
(err txt
)