Fix header level for foreign-libraries section
[cabal.git] / Cabal / Distribution / ReadE.hs
blobde857a9633c8954586f3fc3f4f1be794ef13dc84
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module : Distribution.ReadE
4 -- Copyright : Jose Iborra 2008
5 -- License : BSD3
6 --
7 -- Maintainer : cabal-devel@haskell.org
8 -- Portability : portable
9 --
10 -- Simple parsing with failure
12 module Distribution.ReadE (
13 -- * ReadE
14 ReadE(..), succeedReadE, failReadE,
15 -- * Projections
16 parseReadE, readEOrFail,
17 readP_to_E
18 ) where
20 import Prelude ()
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)
32 Left err -> Left err
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
42 txt <- look
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
49 readP_to_E err r =
50 ReadE $ \txt -> case [ p | (p, s) <- readP_to_S r txt
51 , all isSpace s ]
52 of [] -> Left (err txt)
53 (p:_) -> Right p