[project @ 1997-05-19 00:12:10 by sof]
[ghc-hetmet.git] / ghc / compiler / main / ErrUtils.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1994-1995
3 %
4 \section[ErrsUtils]{Utilities for error reporting}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module ErrUtils (
10         SYN_IE(Error), SYN_IE(Warning), SYN_IE(Message),
11         addErrLoc,
12         addShortErrLocLine, addShortWarnLocLine,
13         dontAddErrLoc,
14         pprBagOfErrors,
15         ghcExit
16     ) where
17
18 IMP_Ubiq(){-uitous-}
19
20 import Bag              --( bagToList )
21 import PprStyle         ( PprStyle(..) )
22 import Pretty
23 import SrcLoc           ( noSrcLoc, SrcLoc{-instance-} )
24 #if __GLASGOW_HASKELL__ >= 202
25 import Outputable
26 #endif
27 \end{code}
28
29 \begin{code}
30 type Error   = PprStyle -> Doc
31 type Warning = PprStyle -> Doc
32 type Message = PprStyle -> Doc
33
34 addErrLoc :: SrcLoc -> String -> Error -> Error
35 addErrLoc locn title rest_of_err_msg sty
36   = hang (hcat [ppr PprForUser locn,
37                 if null title then empty else text (": " ++ title),
38                 char ':'])
39          4 (rest_of_err_msg sty)
40
41 addShortErrLocLine, addShortWarnLocLine :: SrcLoc -> Error -> Error
42
43 addShortErrLocLine locn rest_of_err_msg sty
44   = hang ((<>) (ppr PprForUser locn) (char ':'))
45          4 (rest_of_err_msg sty)
46
47 addShortWarnLocLine locn rest_of_err_msg sty
48   = hang ((<>) (ppr PprForUser locn) (ptext SLIT(":warning:")))
49          4 (rest_of_err_msg sty)
50
51 dontAddErrLoc :: String -> Error -> Error
52 dontAddErrLoc title rest_of_err_msg sty
53   = hang (hcat [text title, char ':'])
54          4 (rest_of_err_msg sty)
55
56 pprBagOfErrors :: PprStyle -> Bag Error -> Doc
57 pprBagOfErrors sty bag_of_errors
58   = let  pretties = map ( \ e -> e sty ) (bagToList bag_of_errors)  in
59     vcat (map (\ p -> ($$) space p) pretties)
60 \end{code}
61
62 \begin{code}
63 ghcExit :: Int -> IO ()
64
65 ghcExit val
66   = if val /= 0
67     then error "Compilation had errors\n"
68     else return ()
69 \end{code}