Messages.hs 1.74 KB
Newer Older
Björn Peemöller 's avatar
Björn Peemöller committed
1
module Base.Messages
2
  ( -- * Output of user information
3
    info, status, putErrLn, putErrsLn
4
    -- * program abortion
Björn Peemöller 's avatar
Björn Peemöller committed
5
6
  , abortWith, abortWithMessage, abortWithMessages
  , internalError, errorMessage, errorMessages
7
    -- * creating messages
Björn Peemöller 's avatar
Björn Peemöller committed
8
  , Message, message, posMessage
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
9
10
11
  ) where

import Control.Monad (unless)
12
13
import System.IO     (hPutStrLn, stderr)
import System.Exit   (exitFailure)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
14

Björn Peemöller 's avatar
Björn Peemöller committed
15
16
import Curry.Base.Message
  (Message, message, posMessage, ppMessage, ppMessages)
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
17

18
import CompilerOpts (Options (optVerbosity), Verbosity (..))
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
19
20

info :: Options -> String -> IO ()
21
22
info opts msg = unless (optVerbosity opts < VerbInfo)
                       (putStrLn $ msg ++ " ...")
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
23
24

status :: Options -> String -> IO ()
25
26
status opts msg = unless (optVerbosity opts < VerbStatus)
                         (putStrLn $ msg ++ " ...")
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
27
28
29
30
31
32
33
34
35

-- |Print an error message on 'stderr'
putErrLn :: String -> IO ()
putErrLn = hPutStrLn stderr

-- |Print a list of error messages on 'stderr'
putErrsLn :: [String] -> IO ()
putErrsLn = mapM_ putErrLn

Björn Peemöller 's avatar
Björn Peemöller committed
36
37
-- |Print a list of 'String's as error messages on 'stderr'
-- and abort the program
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
38
abortWith :: [String] -> IO a
39
40
abortWith errs = putErrsLn errs >> exitFailure

Björn Peemöller 's avatar
Björn Peemöller committed
41
42
43
44
-- |Print a single error message on 'stderr' and abort the program
abortWithMessage :: Message -> IO a
abortWithMessage msg = abortWithMessages [msg]

45
46
47
-- |Print a list of error messages on 'stderr' and abort the program
abortWithMessages :: [Message] -> IO a
abortWithMessages msgs = putErrLn (show $ ppMessages msgs) >> exitFailure
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
48
49
50

-- |Raise an internal error
internalError :: String -> a
Björn Peemöller 's avatar
Björn Peemöller committed
51
internalError msg = error $ "Internal error: " ++ msg
Bjoern Peemoeller's avatar
Bjoern Peemoeller committed
52

53
errorMessage :: Message -> a
54
errorMessage = error . show . ppMessage
55

Björn Peemöller 's avatar
Björn Peemöller committed
56
errorMessages :: [Message] -> a
57
errorMessages = error . show . ppMessages