-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.h
278 lines (247 loc) · 6.89 KB
/
error.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* Authored by Charlie Keaney */
/* error.h - Responsible for defining error
codes. */
#ifndef ERROR_H
#define ERROR_H
#include <cstddef>
#include "preprocessing-token.h"
#define ERR_COLOUR_START "\x1B[31m"
#define ERR_COLOUR_FINISHED "\033[0m"
#define WARN_COLOUR_START "\x1B[32m"
#define WARN_COLOUR_FINISHED "\033[0m"
#define MSG_COLOUR_START "\x1B[33m"
#define MSG_COLOUR_FINISHED "\033[0m"
/*****************************************************//**
* Declarations *
/********************************************************/
/* Error messages consist of format : err-solutions */
static
const char* err_messages[] = {
"Could not recognise this token (). Did you mispell it?"
"Could not recognise this identifier ()."
"Did you forget to declare it or mispell it?",
"Expected a semicolon here. Did you forget to put it in?",
};
enum class ErrorCode {
UNDEFINED,
ERR_UNRECOGNISED_IDENTIFIER,
ERR_MISSING_SEMICOLON,
/* Preprocessor Errors */
ERR_PP_INVALID_HEADER,
ERR_PP_INVALID_IDENTIFIER,
ERR_PP_INVALID_PP_NUMBER,
ERR_PP_INVALID_PUNCTUATOR,
ERR_PP_INVALID_CONSTANT,
ERR_PP_INVALID_STRING_LITERAL,
/* Lexer Errors. */
ERR_INVALID_CONSTANT,
ERR_INVALID_IDENTIFIER,
ERR_INVALID_STRING_LITERAL,
ERR_INVALID_PUNCTUATOR,
/* Parser Errors. */
ERR_PARSER_INVALID_TRANSLATION_UNIT,
};
struct Error {
ErrorCode error_code;
const char* error_message;
FileLocationDescriptor fld;
Error* next_error;
};
enum class WarningCode {
UNDEFINED
};
struct Warning {
WarningCode warning_code;
const char* warning_message;
FileLocationDescriptor fld;
Warning* next_warning;
};
enum class MessageCode {
UNDEFINED
};
struct Message {
MessageCode message_code;
const char* message;
FileLocationDescriptor fld;
Message* next_message;
};
Error* construct_error_book(
ErrorCode const& error_code,
const char* const& error_message,
FileLocationDescriptor const& fld);
static inline
const char* construct_invalid_constant_error_message(
const char* const& error_lexeme);
static inline
void display_pointer_to_input_position(
FileLocationDescriptor const& fld,
const char* const& input);
static inline
void print_error_book(
Error* const& error_book,
const char* const& input);
static inline
void print_warning_book(
Warning* const& warning_book,
const char* const& input);
static inline
void print_message_book(
Message* const& message_book,
const char* const& input);
class AlertList {
private:
Error* error;
Warning* warning;
Message* message;
public:
inline void add_error(
Error* const& new_error)
{
if (error == NULL) {
error = new_error;
}
else {
Error* last_error = error;
while (last_error->next_error != NULL) {
last_error = last_error->next_error;
}
last_error->next_error = new_error;
}
}
/* The messages reference the input, so we will need the input to be passed in too.*/
inline void print(const char* input)
{
print_error_book(error, input);
print_warning_book(warning, input);
print_message_book(message, input);
}
};
/*****************************************************//**
* Definitions *
/********************************************************/
Error* construct_error_book(
ErrorCode const& error_code,
const char* const& error_message,
FileLocationDescriptor const& fld)
{
Error* error
= new Error({
error_code,
error_message,
fld,
NULL
});
return error;
}
static inline
const char* construct_invalid_constant_error_message(
const char* const& error_lexeme)
{
return NULL;
}
static inline
void display_pointer_to_input_position(
FileLocationDescriptor const& fld,
const char* const& input)
{
int r = 0;
int c = 0;
const char* input_ptr = input;
/* Move pointer to start of error line. */
while (r < fld.line_number) {
while (*input_ptr++ != '\n') { }
r++;
}
/* Print error line. */
while (*input_ptr != '\n') {
cout << *input_ptr++;
}
cout << *input_ptr++;
cout << ERR_COLOUR_START;
/* Now print space before pointer. */
for (int i = 0;
i < fld.character_number;
i++) {
cout << " ";
}
/* Now print pointer with tail. */
cout << "^";
for (int i = 0;
i < fld.lexeme_length - 1;
i++) {
cout << "~";
}
cout << ERR_COLOUR_FINISHED
<< endl;
}
static inline
void print_error_book(
Error* const& error_book,
const char* const& input)
{
Error* e = error_book;
while (e != NULL) {
/* Print out the message. */
cout << ERR_COLOUR_START
<< "kcc" << ":"
<< "error" << ":"
<< "err" << (int) e->error_code << ":"
<< e->fld.filename << ":"
<< e->fld.line_number << ":"
<< e->fld.character_number << ":"
<< e->error_message
<< ERR_COLOUR_FINISHED
<< endl;
/* Show where the input went wrong. */
display_pointer_to_input_position(e->fld, input);
/* Move onto next error. */
e = e->next_error;
}
}
static inline
void print_warning_book(
Warning* const& warning_book,
const char* const& input)
{
Warning* e = warning_book;
while (e != NULL) {
/* Print out the message. */
cout << WARN_COLOUR_START
<< "kcc" << ":"
<< "error" << ":"
<< "err" << (int) warning_book->warning_code << ":"
<< warning_book->fld.filename << ":"
<< warning_book->fld.line_number << ":"
<< warning_book->fld.character_number << ":"
<< warning_book->warning_message
<< WARN_COLOUR_FINISHED;
/* Show where the input went wrong. */
display_pointer_to_input_position(warning_book->fld, input);
/* Move onto next error. */
e = e->next_warning;
}
}
static inline
void print_message_book(
Message* const& message_book,
const char* const& input)
{
Message* e = message_book;
while (e != NULL) {
/* Print out the message. */
cout << MSG_COLOUR_START
<< "kcc" << ":"
<< "error" << ":"
<< "err" << (int) message_book->message_code << ":"
<< message_book->fld.filename << ":"
<< message_book->fld.line_number << ":"
<< message_book->fld.character_number << ":"
<< message_book->message
<< MSG_COLOUR_FINISHED;
/* Show where the input went wrong. */
display_pointer_to_input_position(message_book->fld, input);
/* Move onto next error. */
e = e->next_message;
}
}
#endif