2#define REDUCT_ERROR_H 1
23#define REDUCT_ERROR_MAX_LEN 128
24#define REDUCT_ERROR_BACKTRACE_MAX 8
30typedef enum reduct_error_type
45typedef struct reduct_error_frame
55typedef struct reduct_error
76#define REDUCT_ERROR() ((reduct_error_t){0})
123 const char** input,
size_t* inputLength,
size_t* regionLength,
size_t* position);
160#define REDUCT_ERROR_GENERIC(_reduct, _error, _item, _type, ...) \
163 const char* __path; \
164 const char* __input; \
165 size_t __inputLength; \
166 size_t __regionLength; \
168 reduct_error_get_item_params((_reduct), (_item), &__path, &__input, &__inputLength, &__regionLength, \
170 reduct_error_set((_error), __path, __input, __inputLength, __regionLength, __position, \
171 REDUCT_ERROR_TYPE_##_type, __VA_ARGS__); \
172 longjmp((_error)->jmp, true); \
180#define REDUCT_ERROR_SUCCESS(_error) ((_error)->type == REDUCT_ERROR_TYPE_NONE)
187#define REDUCT_ERROR_CATCH(_error) (setjmp((_error)->jmp))
198#define REDUCT_ERROR_TRY(_reduct, _error) \
199 for (int _once = (reduct_error_push((_reduct), (_error)), 0); _once < 1; (reduct_error_pop(_reduct), _once++)) \
200 if (REDUCT_ERROR_CATCH(_error) == 0)
208#define REDUCT_ERROR_THROW(_reduct, ...) reduct_error_throw_runtime((_reduct), __VA_ARGS__)
216#define REDUCT_ERROR_RETHROW(_reduct, _error) REDUCT_ERROR_THROW(_reduct, "%s", (_error)->message)
225#define REDUCT_ERROR_ASSERT(_reduct, _expr, ...) \
228 if (REDUCT_UNLIKELY(!(_expr))) \
230 REDUCT_ERROR_THROW(_reduct, __VA_ARGS__); \
242#define REDUCT_ERROR_SYNTAX(_error, _input, _ptr, ...) \
245 reduct_error_set((_error), (_input)->path, (_input)->buffer, (_input)->end - (_input)->buffer, 1, \
246 (size_t)((_ptr) - (_input)->buffer), REDUCT_ERROR_TYPE_SYNTAX, __VA_ARGS__); \
247 longjmp((_error)->jmp, true); \
257#define REDUCT_ERROR_COMPILE(_compiler, _handle, ...) \
260 struct reduct_item* __item = REDUCT_HANDLE_TO_ITEM(_handle); \
261 REDUCT_ERROR_GENERIC((_compiler)->reduct, (_compiler)->reduct->error, \
262 (((__item) != NULL && (__item)->moduleId != REDUCT_MODULE_ID_NONE) \
264 : ((_compiler)->lastItem != NULL ? (_compiler)->lastItem : (__item))), \
265 COMPILE, __VA_ARGS__); \
275#define REDUCT_ERROR_COMPILE_LAST(_compiler, ...) \
278 reduct_handle_t __handle = REDUCT_HANDLE_FROM_ITEM((_compiler)->lastItem); \
279 REDUCT_ERROR_COMPILE((_compiler), __handle, __VA_ARGS__); \
290#define REDUCT_ERROR_COMPILE_ASSERT(_compiler, _expr, ...) \
293 if (REDUCT_UNLIKELY(!(_expr))) \
295 reduct_handle_t __handle = REDUCT_HANDLE_FROM_ITEM((_compiler)->lastItem); \
296 REDUCT_ERROR_COMPILE((_compiler), __handle, __VA_ARGS__); \
306#define REDUCT_ERROR_INTERNAL(_reduct, ...) REDUCT_ERROR_GENERIC(_reduct, (_reduct)->error, NULL, INTERNAL, __VA_ARGS__)
REDUCT_API void reduct_error_set(reduct_error_t *error, const char *path, const char *input, size_t inputLength, size_t regionLength, size_t position, reduct_error_type_t type, const char *message,...)
Set the error information in the error structure.
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.
REDUCT_API void reduct_error_push(struct reduct *reduct, reduct_error_t *error)
Push a new error handler onto the stack.
reduct_error_type_t
Error type enumeration.
REDUCT_API void reduct_error_get_item_params(struct reduct *reduct, struct reduct_item *item, const char **path, const char **input, size_t *inputLength, size_t *regionLength, size_t *position)
Get the error parameters from a Reduct item.
REDUCT_API void reduct_error_get_row_column(reduct_error_t *error, size_t *row, size_t *column)
Get the row and column by traversing the input buffer.
REDUCT_API void reduct_error_print(reduct_error_t *error, FILE *file)
Format and print the error to a file.
REDUCT_API void reduct_error_pop(struct reduct *reduct)
Pop the current error handler from the stack.
#define REDUCT_ERROR_BACKTRACE_MAX
Maximum number of backtrace frames.
#define REDUCT_ERROR_MAX_LEN
Maximum length of an error string.
@ REDUCT_ERROR_TYPE_SYNTAX
@ REDUCT_ERROR_TYPE_RUNTIME
@ REDUCT_ERROR_TYPE_INTERNAL
@ REDUCT_ERROR_TYPE_COMPILE
uint16_t reduct_module_id_t
Identifies a reduct_module_t within a Reduct structure.
Backtrace frame structure.
reduct_module_id_t moduleId
The ID of the module that caused the error.
uint32_t modulePos
The position in the input buffer.
reduct_error_type_t type
The type of the error.
size_t index
The index of the region in the input buffer that caused the error.
size_t regionLength
The length of the region that caused the error.
const char * input
The input buffer.
uint8_t frameCount
The number of backtrace frames.
struct reduct_error * prev
Previous error handler in the stack.
struct reduct * reduct
The owning Reduct structure.
const char * path
The path to the file that caused the error.
size_t inputLength
The total length of the input buffer.