module Parse
( parseCabalFile
, readCabalFile
, displayError
, printWarnings
, Result(..)
, result
) where
import Control.DeepSeq
import Data.Data
import Data.Maybe
import Distribution.PackageDescription.Parse
import Distribution.ParseUtils
import Distribution.Simple.Utils
import Distribution.Verbosity
import GHC.Generics
import System.Environment
import System.Exit
import System.IO
data Result a
= Error (Maybe LineNo)
String
| Warn [PWarning]
| Success a
deriving (Show, Eq, Functor, Generic, Typeable, Data)
result :: (Maybe LineNo -> String -> b) -> ([PWarning] -> b) -> (a -> b) -> Result a -> b
result e w s p =
case p of
Error l m -> e l m
Warn ws -> w ws
Success r -> s r
instance NFData a => NFData (Result a)
deriving instance Generic PWarning
deriving instance Data PWarning
#if !MIN_VERSION_base(4,8,0)
deriving instance Typeable PWarning
#endif
instance NFData PWarning
parseCabalFile input =
case parseGenericPackageDescription input of
ParseFailed e -> uncurry Error $ locatedErrorMsg e
ParseOk warnings x
| null warnings -> Success x
| otherwise -> Warn $ reverse warnings
readCabalFile fpath = result (displayError fpath) printWarnings return . parseCabalFile
printWarnings ps = mapM_ (warn normal . showPWarning "<input>") ps >> exitFailure
displayError :: Maybe FilePath -> Maybe LineNo -> String -> IO a
displayError fpath line' message = do
prog <- getProgName
hPutStrLn stderr $
prog ++
": " ++
fromMaybe "<input>" fpath ++
(case line' of
Just lineno -> ":" ++ show lineno
Nothing -> "") ++
": " ++ message
exitFailure