File: Errors.hs

package info (click to toggle)
ctklight 0.17.11-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 176 kB
  • ctags: 41
  • sloc: haskell: 514; makefile: 26; sh: 12
file content (140 lines) | stat: -rw-r--r-- 4,436 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
--  Compiler Toolkit: basic error management
--
--  Author : Manuel M. T. Chakravarty
--  Created: 20 February 95
--
--  Version $Revision: 1.16 $ from $Date: 1999/09/22 09:36:31 $
--
--  Copyright (c) [1995..1999] Manuel M. T. Chakravarty
--
--  This library is free software; you can redistribute it and/or
--  modify it under the terms of the GNU Library General Public
--  License as published by the Free Software Foundation; either
--  version 2 of the License, or (at your option) any later version.
--
--  This library is distributed in the hope that it will be useful,
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
--  Library General Public License for more details.
--
--- DESCRIPTION ---------------------------------------------------------------
--
--  This modules exports some auxilliary routines for error handling.
--
--- DOCU ----------------------------------------------------------------------
--
--  language: Haskell 98
--
--  *  the single lines of error messages shouldn't be to long as file name 
--     and position are prepended at each line
--
--- TODO ----------------------------------------------------------------------
--
--  * `errorAtPos' should be removed when we can handle multiple error messages
--    in the lexer and parser.
--

module Errors (-- handling of internal error
	       --
	       interr, todo,
	       --
	       -- errors in the compiled program
	       --
	       ErrorLvl(..), Error, makeError, errorLvl, showError, errorAtPos)
where

import Config (assertEnabled)
import Common (Position)
import Utils  (indentMultilineString)


-- internal errors
-- ---------------

-- raise a fatal internal error; message may have multiple lines (EXPORTED)
--
interr     :: String -> a
interr msg  = error ("INTERNAL COMPILER ERROR:\n" 
		     ++ indentMultilineString 2 msg 
		     ++ "\n")

-- raise a error due to a implementation restriction; message may have multiple
-- lines (EXPORTED) 
--
todo     :: String -> a
todo msg  = error ("Feature not yet implemented:\n"
		   ++ indentMultilineString 2 msg 
		   ++ "\n")


-- errors in the compiled program
-- ------------------------------

-- the higher the level of an error, the more critical it is (EXPORTED)
--
data ErrorLvl = WarningErr 		-- does not affect compilation
	      | ErrorErr     		-- cannot generate code
	      | FatalErr     		-- abort immediately
	      deriving (Eq, Ord)

data Error = Error ErrorLvl Position [String]  -- (EXPORTED ABSTRACTLY)

-- note that the equality to on errors takes into account only the error level
-- and position (not the error text)
--
-- note that these comparisions are expensive (the positions contain the file
-- names as strings)
--
instance Eq Error where
  (Error lvl1 pos1 _) == (Error lvl2 pos2 _) = lvl1 == lvl2 && pos1 == pos2
  
instance Ord Error where
  (Error lvl1 pos1 _) <  (Error lvl2 pos2 _) = pos1 < pos2
					       || (pos1 == pos2 && lvl1 < lvl2)
  e1                  <= e2		     = e1 < e2 || e1 == e2
  

-- produce an `Error', given its level, position, and a list of lines of
-- the error message that must not be empty (EXPORTED)
--
makeError :: ErrorLvl -> Position -> [String] -> Error
makeError  = Error

-- inquire the error level (EXPORTED)
--
errorLvl                 :: Error -> ErrorLvl
errorLvl (Error lvl _ _)  = lvl

-- converts an error into a string using a fixed format (EXPORTED)
--
-- * the list of lines of the error message must not be empty
--
-- * the format is
--
--     <fname>:<row>: (column <col>) [<err lvl>] 
--       >>> <line_1>
--       <line_2>
--         ...
--	 <line_n>
--
showError                                      :: Error -> String
showError (Error lvl (fname, row, col) (l:ls))  =
	  let
	    prefix = fname ++ ":" ++ show (row::Int) ++ ": "
	             ++ "(column " 
		     ++ show (col::Int) 
		     ++ ") [" 
		     ++ showErrorLvl lvl
		     ++ "] "
	    showErrorLvl WarningErr = "WARNING"
	    showErrorLvl ErrorErr   = "ERROR"
	    showErrorLvl FatalErr   = "FATAL"
	  in
	    prefix ++ "\n" 
	    ++ "  >>> " ++ l ++ "\n"
	    ++ (indentMultilineString 2 . unlines) ls
showError (Error _  _                  []   )   = interr "Errors: showError:\
					                \ Empty error message!"

errorAtPos         :: Position -> [String] -> a
errorAtPos pos msg  = (error . showError . makeError ErrorErr pos) msg