{-# LANGUAGE LambdaCase #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.ReadE
-- Copyright   :  Jose Iborra 2008
-- License     :  BSD3
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Simple parsing with failure

module Distribution.ReadE (
   -- * ReadE
   ReadE(..), succeedReadE, failReadE,
   -- * Projections
   parsecToReadE, parsecToReadEErr,
   -- * Parse Errors
   unexpectMsgString,
  ) where

import Distribution.Compat.Prelude
import Prelude ()
import qualified Data.Bifunctor as Bi (first)

import Distribution.Parsec
import qualified Text.Parsec.Error as Parsec
import Distribution.Parsec.FieldLineStream

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

instance Functor ReadE where
  fmap :: forall a b. (a -> b) -> ReadE a -> ReadE b
fmap a -> b
f (ReadE String -> Either String a
p) = forall a. (String -> Either String a) -> ReadE a
ReadE forall a b. (a -> b) -> a -> b
$ \String
txt -> case String -> Either String a
p String
txt of
                                       Right a
a  -> forall a b. b -> Either a b
Right (a -> b
f a
a)
                                       Left String
err -> forall a b. a -> Either a b
Left String
err

succeedReadE :: (String -> a) -> ReadE a
succeedReadE :: forall a. (String -> a) -> ReadE a
succeedReadE String -> a
f = forall a. (String -> Either String a) -> ReadE a
ReadE (forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> a
f)

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

runParsecFromString :: ParsecParser a -> String -> Either Parsec.ParseError a
runParsecFromString :: forall a. ParsecParser a -> String -> Either ParseError a
runParsecFromString ParsecParser a
p String
txt =
    forall a.
ParsecParser a -> String -> FieldLineStream -> Either ParseError a
runParsecParser ParsecParser a
p String
"<parsecToReadE>" (String -> FieldLineStream
fieldLineStreamFromString String
txt)

parsecToReadE :: (String -> ErrorMsg) -> ParsecParser a -> ReadE a
parsecToReadE :: forall a. (String -> String) -> ParsecParser a -> ReadE a
parsecToReadE String -> String
err ParsecParser a
p = forall a. (String -> Either String a) -> ReadE a
ReadE forall a b. (a -> b) -> a -> b
$ \String
txt ->
    (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ String -> String
err String
txt) forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
`Bi.first` forall a. ParsecParser a -> String -> Either ParseError a
runParsecFromString ParsecParser a
p String
txt

parsecToReadEErr :: (Parsec.ParseError -> ErrorMsg) -> ParsecParser a -> ReadE a
parsecToReadEErr :: forall a. (ParseError -> String) -> ParsecParser a -> ReadE a
parsecToReadEErr ParseError -> String
err ParsecParser a
p = forall a. (String -> Either String a) -> ReadE a
ReadE forall a b. (a -> b) -> a -> b
$
    forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
Bi.first ParseError -> String
err forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. ParsecParser a -> String -> Either ParseError a
runParsecFromString ParsecParser a
p

-- Show only unexpected error messages
unexpectMsgString :: Parsec.ParseError -> String
unexpectMsgString :: ParseError -> String
unexpectMsgString = [String] -> String
unlines
   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map Message -> String
Parsec.messageString
   forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> [a] -> [a]
filter (\case { Parsec.UnExpect String
_ -> Bool
True; Message
_ -> Bool
False })
   forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError -> [Message]
Parsec.errorMessages