| 1 | -- Compiler Toolkit: basic error management |
| 2 | -- |
| 3 | -- Author : Manuel M. T. Chakravarty |
| 4 | -- Created: 20 February 95 |
| 5 | -- |
| 6 | -- Version $Revision: 1.2 $ from $Date: 2004/11/13 17:26:50 $ |
| 7 | -- |
| 8 | -- Copyright (c) [1995..2000] Manuel M. T. Chakravarty |
| 9 | -- |
| 10 | -- This library is free software; you can redistribute it and/or |
| 11 | -- modify it under the terms of the GNU Library General Public |
| 12 | -- License as published by the Free Software Foundation; either |
| 13 | -- version 2 of the License, or (at your option) any later version. |
| 14 | -- |
| 15 | -- This library is distributed in the hope that it will be useful, |
| 16 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | -- Library General Public License for more details. |
| 19 | -- |
| 20 | --- DESCRIPTION --------------------------------------------------------------- |
| 21 | -- |
| 22 | -- This modules exports some auxiliary routines for error handling. |
| 23 | -- |
| 24 | --- DOCU ---------------------------------------------------------------------- |
| 25 | -- |
| 26 | -- language: Haskell 98 |
| 27 | -- |
| 28 | -- * the single lines of error messages shouldn't be to long as file name |
| 29 | -- and position are prepended at each line |
| 30 | -- |
| 31 | --- TODO ---------------------------------------------------------------------- |
| 32 | -- |
| 33 | |
| 34 | module Errors ( |
| 35 | -- handling of internal error |
| 36 | -- |
| 37 | interr, todo, |
| 38 | -- |
| 39 | -- errors in the compiled program |
| 40 | -- |
| 41 | ErrorLvl(..), Error, makeError, errorLvl, showError, errorAtPos |
| 42 | ) where |
| 43 | |
| 44 | import Position (Position(..), isInternalPos) |
| 45 | |
| 46 | |
| 47 | -- internal errors |
| 48 | -- --------------- |
| 49 | |
| 50 | -- raise a fatal internal error; message may have multiple lines (EXPORTED) |
| 51 | -- |
| 52 | interr :: String -> a |
| 53 | interr msg = error ("INTERNAL COMPILER ERROR:\n" |
| 54 | ++ indentMultilineString 2 msg |
| 55 | ++ "\n") |
| 56 | |
| 57 | -- raise a error due to a implementation restriction; message may have multiple |
| 58 | -- lines (EXPORTED) |
| 59 | -- |
| 60 | todo :: String -> a |
| 61 | todo msg = error ("Feature not yet implemented:\n" |
| 62 | ++ indentMultilineString 2 msg |
| 63 | ++ "\n") |
| 64 | |
| 65 | |
| 66 | -- errors in the compiled program |
| 67 | -- ------------------------------ |
| 68 | |
| 69 | -- the higher the level of an error, the more critical it is (EXPORTED) |
| 70 | -- |
| 71 | data ErrorLvl = WarningErr -- does not affect compilation |
| 72 | | ErrorErr -- cannot generate code |
| 73 | | FatalErr -- abort immediately |
| 74 | deriving (Eq, Ord) |
| 75 | |
| 76 | data Error = Error ErrorLvl Position [String] -- (EXPORTED ABSTRACTLY) |
| 77 | |
| 78 | -- note that the equality to on errors takes into account only the error level |
| 79 | -- and position (not the error text) |
| 80 | -- |
| 81 | -- note that these comparisons are expensive (the positions contain the file |
| 82 | -- names as strings) |
| 83 | -- |
| 84 | instance Eq Error where |
| 85 | (Error lvl1 pos1 _) == (Error lvl2 pos2 _) = lvl1 == lvl2 && pos1 == pos2 |
| 86 | |
| 87 | instance Ord Error where |
| 88 | (Error lvl1 pos1 _) < (Error lvl2 pos2 _) = pos1 < pos2 |
| 89 | || (pos1 == pos2 && lvl1 < lvl2) |
| 90 | e1 <= e2 = e1 < e2 || e1 == e2 |
| 91 | |
| 92 | |
| 93 | -- produce an `Error', given its level, position, and a list of lines of |
| 94 | -- the error message that must not be empty (EXPORTED) |
| 95 | -- |
| 96 | makeError :: ErrorLvl -> Position -> [String] -> Error |
| 97 | makeError = Error |
| 98 | |
| 99 | -- inquire the error level (EXPORTED) |
| 100 | -- |
| 101 | errorLvl :: Error -> ErrorLvl |
| 102 | errorLvl (Error lvl _ _) = lvl |
| 103 | |
| 104 | -- converts an error into a string using a fixed format (EXPORTED) |
| 105 | -- |
| 106 | -- * the list of lines of the error message must not be empty |
| 107 | -- |
| 108 | -- * the format is |
| 109 | -- |
| 110 | -- <fname>:<row>: (column <col>) [<err lvl>] |
| 111 | -- >>> <line_1> |
| 112 | -- <line_2> |
| 113 | -- ... |
| 114 | -- <line_n> |
| 115 | -- |
| 116 | -- * internal errors (identified by a special position value) are formatted as |
| 117 | -- |
| 118 | -- INTERNAL ERROR! |
| 119 | -- >>> <line_1> |
| 120 | -- <line_2> |
| 121 | -- ... |
| 122 | -- <line_n> |
| 123 | -- |
| 124 | showError :: Error -> String |
| 125 | showError (Error _ pos (l:ls)) | isInternalPos pos = |
| 126 | "INTERNAL ERROR!\n" |
| 127 | ++ " >>> " ++ l ++ "\n" |
| 128 | ++ (indentMultilineString 2 . unlines) ls |
| 129 | showError (Error lvl (Position fname row col) (l:ls)) = |
| 130 | let |
| 131 | prefix = fname ++ ":" ++ show (row::Int) ++ ": " |
| 132 | ++ "(column " |
| 133 | ++ show (col::Int) |
| 134 | ++ ") [" |
| 135 | ++ showErrorLvl lvl |
| 136 | ++ "] " |
| 137 | showErrorLvl WarningErr = "WARNING" |
| 138 | showErrorLvl ErrorErr = "ERROR" |
| 139 | showErrorLvl FatalErr = "FATAL" |
| 140 | in |
| 141 | prefix ++ "\n" |
| 142 | ++ " >>> " ++ l ++ "\n" |
| 143 | ++ (indentMultilineString 2 . unlines) ls |
| 144 | showError (Error _ _ [] ) = interr "Errors: showError:\ |
| 145 | \ Empty error message!" |
| 146 | |
| 147 | errorAtPos :: Position -> [String] -> a |
| 148 | errorAtPos pos msg = (error . showError . makeError ErrorErr pos) msg |
| 149 | |
| 150 | -- indent the given multiline text by the given number of spaces |
| 151 | -- |
| 152 | indentMultilineString :: Int -> String -> String |
| 153 | indentMultilineString n = unlines . (map (spaces++)) . lines |
| 154 | where |
| 155 | spaces = take n (repeat ' ') |