-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.ReadE
-- Copyright   :  Jose Iborra 2008
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Simple parsing with failure

{- Copyright (c) 2007, Jose Iborra
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of Isaac Jones nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

module Distribution.ReadE (
   -- * ReadE
   ReadE(..), succeedReadE, failReadE,
   -- * Projections
   parseReadE, readEOrFail,
   readP_to_E
  ) where

import Distribution.Compat.ReadP
import Data.Char ( isSpace )

-- | Parser with simple error reporting
newtype runReadE :: String -> Either ErrorMsg aReadE a = ReadE {runReadE :: String -> Either ErrorMsg a}
type ErrorMsg   = String

instance D:Functor ::
  (forall a b. (a -> b) -> f a -> f b)
  -> (forall a b. a -> f b -> f a)
  -> T:Functor fFunctor ReadE where
  fmap f (ReadE p) = ReadE :: (String -> Either ErrorMsg a) -> ReadE aReadE ($) :: (a -> b) -> a -> b$ \txt -> case p :: String -> Either ErrorMsg ap txt :: Stringtxt of
                                       Right a  -> Right :: b -> Either a bRight (f :: a -> bf a :: aa)
                                       Left err -> Left :: a -> Either a bLeft err :: ErrorMsgerr

succeedReadE :: (String -> a) -> ReadE a
succeedReadE f = ReadE :: (String -> Either ErrorMsg a) -> ReadE aReadE (Right :: b -> Either a bRight (.) :: (b -> c) -> (a -> b) -> a -> c. f :: a -> bf)

failReadE :: ErrorMsg -> ReadE a
failReadE = ReadE :: (String -> Either ErrorMsg a) -> ReadE aReadE (.) :: (b -> c) -> (a -> b) -> a -> c. const :: a -> b -> aconst Left :: a -> Either a bLeft

parseReadE :: ReadE a -> ReadP r a
parseReadE (ReadE p) = do
  txt <- look :: ReadP r Stringlook
  either :: (a -> c) -> (b -> c) -> Either a b -> ceither fail :: Monad m => forall a. String -> m afail return :: Monad m => forall a. a -> m areturn (p :: String -> Either ErrorMsg ap txt :: Stringtxt)

readEOrFail :: ReadE a -> (String -> a)
readEOrFail r = either :: (a -> c) -> (b -> c) -> Either a b -> ceither error :: [Char] -> aerror id :: a -> aid (.) :: (b -> c) -> (a -> b) -> a -> c. runReadE :: ReadE a -> String -> Either ErrorMsg arunReadE r :: ReadE ar

readP_to_E :: (String -> ErrorMsg) -> ReadP a a -> ReadE a
readP_to_E err r =
    ReadE :: (String -> Either ErrorMsg a) -> ReadE aReadE ($) :: (a -> b) -> a -> b$ \txt -> case [ p :: String -> Either ErrorMsg ap | (p, s) <- readP_to_S :: ReadP a a -> ReadS areadP_to_S r :: ReadE ar txt :: Stringtxt
                         , all :: (a -> Bool) -> [a] -> Boolall isSpace :: Char -> BoolisSpace s :: Strings ]
                    of [] -> Left :: a -> Either a bLeft (err :: ErrorMsgerr txt :: Stringtxt)
                       (p:_) -> Right :: b -> Either a bRight p :: String -> Either ErrorMsg ap