Reduct  v1.0.4-3-gdaf0d70
A functional and immutable language.
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1#ifndef REDUCT_ERROR_H
2#define REDUCT_ERROR_H 1
3
4#include "defs.h"
5
6struct reduct;
7struct reduct_item;
8
9/**
10 * @file error.h
11 * @brief Error handling and reporting.
12 * @defgroup error Error
13 *
14 * @{
15 */
16
17#define REDUCT_ERROR_MAX_LEN 512 ///< Maximum length of an error string.
18
19/**
20 * @brief Error type enumeration.
21 * @enum reduct_error_type_t
22 */
31
32/**
33 * @brief Error structure.
34 * @struct reduct_error_t
35 */
36typedef struct
37{
38 const char* input; ///< The input buffer.
39 reduct_size_t inputLength; ///< The total length of the input buffer.
40 const char* path; ///< THe path to the file that caused the error.
41 reduct_size_t regionLength; ///< The length of the region that caused the error.
42 reduct_size_t index; ///< The index of the region in the input buffer that caused the error.
44 reduct_error_type_t type; ///< The type of the error.
45 char message[REDUCT_ERROR_MAX_LEN];
47
48/**
49 * @brief Create a Reduct error structure.
50 *
51 * @return A new Reduct error structure initialized to zero.
52 */
53#define REDUCT_ERROR() ((reduct_error_t){0})
54
55/**
56 * @brief Format and print the error to a file.
57 *
58 * @param error Pointer to the error structure.
59 * @param file The file to print to.
60 */
62
63/**
64 * @brief Get the row and column by traversing the input buffer.
65 *
66 * @param error Pointer to the error structure.
67 * @param row Pointer to the row variable.
68 * @param column Pointer to the column variable.
69 */
71
72/**
73 * @brief Set the error information in the error structure.
74 *
75 * @param error Pointer to the error structure.
76 * @param path The path to the file where the error occurred.
77 * @param input The input buffer where the error occurred.
78 * @param inputLength The total length of the input buffer.
79 * @param regionLength The length of the token/region that caused the error.
80 * @param position The position in the input buffer where the error occurred.
81 * @param type The type of the error.
82 * @param message The error message format string.
83 * @param ... The arguments for the format string.
84 */
85REDUCT_API void reduct_error_set(reduct_error_t* error, const char* path, const char* input, reduct_size_t inputLength,
86 reduct_size_t regionLength, reduct_size_t position, reduct_error_type_t type, const char* message, ...);
87
88/**
89 * @brief Get the error parameters from a Reduct item.
90 *
91 * @param item Pointer to the item.
92 * @param path Pointer to the path variable.
93 * @param input Pointer to the input variable.
94 * @param inputLength Pointer to the input length variable.
95 * @param regionLength Pointer to the region length variable.
96 * @param position Pointer to the position variable.
97 */
98REDUCT_API void reduct_error_get_item_params(struct reduct_item* item, const char** path, const char** input,
99 reduct_size_t* inputLength, reduct_size_t* regionLength, reduct_size_t* position);
100
101/**
102 * @brief Throw a runtime error utilizing the evaluation state to determine the context.
103 *
104 * @param reduct Pointer to the Reduct instance.
105 * @param message The error message format string.
106 * @param ... Additional arguments.
107 */
108REDUCT_API REDUCT_NORETURN void reduct_error_throw_runtime(struct reduct* reduct, const char* message, ...);
109
110/**
111 * @brief Catch an error using the jump buffer in the error structure.
112 *
113 * @param _error Pointer to the error structure.
114 */
115#define REDUCT_ERROR_CATCH(_error) (REDUCT_SETJMP((_error)->jmp))
116
117/**
118 * @brief Throw an error using the jump buffer in the error structure.
119 *
120 * @param _error Pointer to the error structure.
121 * @param _item Pointer to the item that caused the error.
122 * @param _type The suffix of the error type (e.g., INTERNAL, RUNTIME, etc.).
123 * @param ... The error message format string and any optional arguments.
124 */
125#define REDUCT_ERROR_THROW(_error, _item, _type, ...) \
126 do \
127 { \
128 const char* __path; \
129 const char* __input; \
130 reduct_size_t __input_length; \
131 reduct_size_t __region_length; \
132 reduct_size_t __position; \
133 reduct_error_get_item_params((_item), &__path, &__input, &__input_length, &__region_length, &__position); \
134 reduct_error_set((_error), __path, __input, __input_length, __region_length, __position, \
135 REDUCT_ERROR_TYPE_##_type, __VA_ARGS__); \
136 REDUCT_LONGJMP((_error)->jmp, REDUCT_TRUE); \
137 } while (0)
138
139/**
140 * @brief Throw a syntax error using the jump buffer in the error structure.
141 *
142 * @param _error Pointer to the error structure.
143 * @param _input Pointer to the input structure being parsed.
144 * @param _ptr Pointer to the current position in the input buffer.
145 * @param ... The error message format string and any optional arguments.
146 */
147#define REDUCT_ERROR_SYNTAX(_error, _input, _ptr, ...) \
148 do \
149 { \
150 reduct_error_set((_error), (_input)->path, (_input)->buffer, (_input)->end - (_input)->buffer, 1, \
151 (reduct_size_t)((_ptr) - (_input)->buffer), REDUCT_ERROR_TYPE_SYNTAX, __VA_ARGS__); \
152 REDUCT_LONGJMP((_error)->jmp, REDUCT_TRUE); \
153 } while (0)
154
155/**
156 * @brief Throw a compile error using the jump buffer in the error structure.
157 *
158 * @param _compiler The compiler instance.
159 * @param _item Pointer to the item that caused the error.
160 * @param ... The error message format string and any optional arguments.
161 */
162#define REDUCT_ERROR_COMPILE(_compiler, _item, ...) \
163 REDUCT_ERROR_THROW((_compiler)->reduct->error, \
164 (((_item) != REDUCT_NULL && (_item)->input != REDUCT_NULL) \
165 ? (_item) \
166 : ((_compiler)->lastItem != REDUCT_NULL ? (_compiler)->lastItem : (_item))), \
167 COMPILE, __VA_ARGS__)
168
169/**
170 * @brief Throw a runtime error using the jump buffer in the error structure.
171 *
172 * @param _reduct Pointer to the reduct instance.
173 * @param ... The error message format string and any optional arguments.
174 */
175#define REDUCT_ERROR_RUNTIME(_reduct, ...) reduct_error_throw_runtime((_reduct), __VA_ARGS__)
176
177/**
178 * @brief Throw an internal error using the jump buffer in the error structure.
179 *
180 * @param _reduct Pointer to the reduct instance.
181 * @param ... The error message format string and any optional arguments.
182 */
183#define REDUCT_ERROR_INTERNAL(_reduct, ...) REDUCT_ERROR_THROW((_reduct)->error, REDUCT_NULL, INTERNAL, __VA_ARGS__)
184
185/**
186 * @brief Report a type error for a handle.
187 */
188#define REDUCT_ERROR_CHECK_TYPE(_reduct, _name, _handle, _expected) \
189 do \
190 { \
191 REDUCT_ERROR_RUNTIME(_reduct, "%s expects %s, got %s", _name, _expected, \
192 reduct_item_type_str(REDUCT_HANDLE_GET_TYPE(_handle))); \
193 } while (0)
194
195/**
196 * @brief Assert that a handle is a list.
197 */
198#define REDUCT_ERROR_CHECK_LIST(_reduct, _handle, _name) \
199 do \
200 { \
201 if (!REDUCT_HANDLE_IS_LIST(_handle)) \
202 { \
203 REDUCT_ERROR_CHECK_TYPE(_reduct, _name, _handle, "a list"); \
204 } \
205 } while (0)
206
207/**
208 * @brief Assert that a handle is a callable.
209 */
210#define REDUCT_ERROR_CHECK_CALLABLE(_reduct, _handle, _name) \
211 do \
212 { \
213 if (!REDUCT_HANDLE_IS_CALLABLE(_handle)) \
214 { \
215 REDUCT_ERROR_CHECK_TYPE(_reduct, _name, _handle, "a callable"); \
216 } \
217 } while (0)
218
219/**
220 * @brief Assert that a handle is a sequence (list or atom).
221 */
222#define REDUCT_ERROR_CHECK_SEQUENCE(_reduct, _handle, _name) \
223 do \
224 { \
225 if (!REDUCT_HANDLE_IS_LIST(_handle) && !REDUCT_HANDLE_IS_ATOM(_handle)) \
226 { \
227 REDUCT_ERROR_CHECK_TYPE(_reduct, _name, _handle, "a list or atom"); \
228 } \
229 } while (0)
230
231/**
232 * @brief Check the arity of a native function call.
233 */
234REDUCT_API void reduct_error_check_arity(struct reduct* reduct, reduct_size_t argc, reduct_size_t expected,
235 const char* name);
237 const char* name);
239 reduct_size_t max, const char* name);
240
241/** @} */
242
243#endif
#define REDUCT_NORETURN
Definition defs.h:119
size_t reduct_size_t
Definition defs.h:100
FILE * reduct_file_t
Definition defs.h:44
jmp_buf reduct_jmp_buf_t
Definition defs.h:40
#define REDUCT_API
Definition defs.h:7
REDUCT_API void reduct_error_print(reduct_error_t *error, reduct_file_t file)
Format and print the error to a file.
Definition error_impl.h:99
REDUCT_API void reduct_error_check_arity(struct reduct *reduct, reduct_size_t argc, reduct_size_t expected, const char *name)
Check the arity of a native function call.
REDUCT_API REDUCT_NORETURN void reduct_error_throw_runtime(struct reduct *reduct, const char *message,...)
Throw a runtime error utilizing the evaluation state to determine the context.
Definition error_impl.h:226
REDUCT_API void reduct_error_get_item_params(struct reduct_item *item, const char **path, const char **input, reduct_size_t *inputLength, reduct_size_t *regionLength, reduct_size_t *position)
Get the error parameters from a Reduct item.
Definition error_impl.h:200
REDUCT_API void reduct_error_get_row_column(reduct_error_t *error, reduct_size_t *row, reduct_size_t *column)
Get the row and column by traversing the input buffer.
Definition error_impl.h:153
reduct_error_type_t
Error type enumeration.
Definition error.h:24
REDUCT_API void reduct_error_check_arity_range(struct reduct *reduct, reduct_size_t argc, reduct_size_t min, reduct_size_t max, const char *name)
REDUCT_API void reduct_error_set(reduct_error_t *error, const char *path, const char *input, reduct_size_t inputLength, reduct_size_t regionLength, reduct_size_t position, reduct_error_type_t type, const char *message,...)
Set the error information in the error structure.
Definition error_impl.h:181
REDUCT_API void reduct_error_check_min_arity(struct reduct *reduct, reduct_size_t argc, reduct_size_t min, const char *name)
#define REDUCT_ERROR_MAX_LEN
Maximum length of an error string.
Definition error.h:17
@ REDUCT_ERROR_TYPE_NONE
Definition error.h:25
@ REDUCT_ERROR_TYPE_SYNTAX
Definition error.h:26
@ REDUCT_ERROR_TYPE_RUNTIME
Definition error.h:28
@ REDUCT_ERROR_TYPE_INTERNAL
Definition error.h:29
@ REDUCT_ERROR_TYPE_COMPILE
Definition error.h:27
Error structure.
Definition error.h:37
reduct_error_type_t type
The type of the error.
Definition error.h:44
reduct_size_t index
The index of the region in the input buffer that caused the error.
Definition error.h:42
const char * input
The input buffer.
Definition error.h:38
const char * path
THe path to the file that caused the error.
Definition error.h:40
reduct_size_t inputLength
The total length of the input buffer.
Definition error.h:39
reduct_size_t regionLength
The length of the region that caused the error.
Definition error.h:41
reduct_jmp_buf_t jmp
Definition error.h:43