-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.h
188 lines (164 loc) · 4.37 KB
/
errors.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
#ifndef WARP_LANG_ERRORS_H
#define WARP_LANG_ERRORS_H
#include <stdint.h>
#include <wchar.h>
#include <stdio.h>
typedef enum ErrorType {
E_MEMORY,
E_IO,
E_INTERNAL,
E_LEXER,
E_SYNTAX,
E_RUNTIME,
E_NONE,
} ErrorType;
typedef struct LexerError {
/*
* The lexer's position in the stream at the time the error occurred.
* This is only non-zero if #type == #E_LEXER.
*/
unsigned long position;
} LexerError;
#define ERROR_MSG_LENGTH 1023
typedef struct Error {
ErrorType type;
union {
LexerError lexer;
};
wchar_t message[ERROR_MSG_LENGTH + 1];
const char *fileName;
uint64_t lineNumber;
const char *functionName;
} Error;
void errorInitContents(Error *error);
/*
* These are the core return codes:
* - #R_SUCCESS means whatever was attempted succeeded.
* - #R_ERROR means that the error struct has been populated
* with more information.
*
* The remainder of the codes in here are situation-specific. They are
* represented here so I can keep track of them.
*
* Yes, this leaks implementation details of the application here. I'm open to
* suggestions.
*/
typedef enum RetVal {
R_SUCCESS,
R_ERROR,
/*
* This code is returned by the lexer to indicate that no next token is
* available to be read. It isn't an error unless you can't continue
* without another token.
*/
R_EOF
} RetVal;
/*
* Error factories
*/
RetVal memoryError(Error *error, char *desc);
RetVal ioError(Error *error, char *desc);
RetVal internalError(Error *error, char *desc);
RetVal tokenizationError(Error *error, unsigned long position, char *desc);
RetVal syntaxError(Error *error, unsigned long position, char *desc);
RetVal runtimeError(Error *error, char *desc);
RetVal compilerError(Error *error, char *desc);
#define throwMemoryError(error, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = memoryError(error, msg); \
goto failure; \
}
#define throwIOError(error, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = ioError(error, msg); \
goto failure; \
}
#define throwInternalError(error, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = internalError(error, msg); \
goto failure; \
}
#define throwTokenizationError(error, pos, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = tokenizationError(error, pos, msg); \
goto failure; \
}
#define throwSyntaxError(error, pos, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = syntaxError(error, pos, msg); \
goto failure; \
}
#define throwCompilerError(error, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = compilerError(error, msg); \
goto failure; \
}
#define throwRuntimeError(error, str, ...) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
ret = runtimeError(error, msg); \
goto failure; \
}
#define throws(f) {\
ret = f;\
if (ret != R_SUCCESS) {\
goto failure;\
}\
}
#define tryMalloc(var, size, desc) {\
var = malloc(size);\
if (var == NULL) {\
error->fileName = __FILE__; \
error->lineNumber = __LINE__; \
error->functionName = __func__; \
ret = memoryError(error, desc);\
goto failure; \
}\
}
#define explode(str, ...) {\
Error error; \
errorInitContents(&error); \
error.fileName = __FILE__; \
error.lineNumber = __LINE__; \
error.functionName = __func__; \
int len = 64; \
char msg[len]; \
snprintf(msg, len, str, ##__VA_ARGS__); \
internalError(&error, msg); \
exit(-1); \
}
#endif //WARP_LANG_ERRORS_H