Reduct  v4.0.5-1-g4851deb
A functional and immutable language.
Loading...
Searching...
No Matches
Error

Detailed Description

Data Structures

struct  reduct_error_frame_t
 Backtrace frame structure. More...
 
struct  reduct_error_t
 Error structure. More...
 

Macros

#define REDUCT_ERROR_MAX_LEN   128
 Maximum length of an error string.
 
#define REDUCT_ERROR_BACKTRACE_MAX   8
 Maximum number of backtrace frames.
 
#define REDUCT_ERROR()   ((reduct_error_t){0})
 Create a Reduct error structure.
 
#define REDUCT_ERROR_GENERIC(_reduct, _error, _item, _type, ...)
 Throw an error using the jump buffer in the error structure.
 
#define REDUCT_ERROR_SUCCESS(_error)   ((_error)->type == REDUCT_ERROR_TYPE_NONE)
 Check if an error structure indicates a successful operation.
 
#define REDUCT_ERROR_CATCH(_error)   (setjmp((_error)->jmp))
 Catch an error using the jump buffer in the error structure.
 
#define REDUCT_ERROR_TRY(_reduct, _error)
 Execute a block of code safely, catching any Reduct errors.
 
#define REDUCT_ERROR_THROW(_reduct, ...)   reduct_error_throw_runtime((_reduct), __VA_ARGS__)
 Throw a runtime error using the jump buffer in the error structure.
 
#define REDUCT_ERROR_RETHROW(_reduct, _error)   REDUCT_ERROR_THROW(_reduct, "%s", (_error)->message);
 Rethrow an existing error.
 
#define REDUCT_ERROR_ASSERT(_reduct, _expr, ...)
 Throw a runtime error if the expression is false.
 
#define REDUCT_ERROR_SYNTAX(_error, _input, _ptr, ...)
 Throw a syntax error using the jump buffer in the error structure.
 
#define REDUCT_ERROR_COMPILE(_compiler, _handle, ...)
 Throw a compile error using the jump buffer in the error structure.
 
#define REDUCT_ERROR_COMPILE_LAST(_compiler, ...)
 Throw a compile error using the jump buffer in the error structure using the last item process by the compiler.
 
#define REDUCT_ERROR_COMPILE_ASSERT(_compiler, _expr, ...)
 Throw a compile error if the expression is false.
 
#define REDUCT_ERROR_INTERNAL(_reduct, ...)   REDUCT_ERROR_GENERIC(_reduct, (_reduct)->error, NULL, INTERNAL, __VA_ARGS__)
 Throw an internal error using the jump buffer in the error structure.
 

Enumerations

enum  reduct_error_type_t {
  REDUCT_ERROR_TYPE_NONE , REDUCT_ERROR_TYPE_SYNTAX , REDUCT_ERROR_TYPE_COMPILE , REDUCT_ERROR_TYPE_RUNTIME ,
  REDUCT_ERROR_TYPE_INTERNAL
}
 Error type enumeration. More...
 

Functions

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_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_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 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 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_API void reduct_error_pop (struct reduct *reduct)
 Pop the current error handler from the stack.
 

Macro Definition Documentation

◆ REDUCT_ERROR_MAX_LEN

#define REDUCT_ERROR_MAX_LEN   128

Maximum length of an error string.

Definition at line 22 of file error.h.

◆ REDUCT_ERROR_BACKTRACE_MAX

#define REDUCT_ERROR_BACKTRACE_MAX   8

Maximum number of backtrace frames.

Definition at line 23 of file error.h.

◆ REDUCT_ERROR

#define REDUCT_ERROR ( )    ((reduct_error_t){0})

Create a Reduct error structure.

Returns
A new Reduct error structure initialized to zero.

Definition at line 75 of file error.h.

◆ REDUCT_ERROR_GENERIC

#define REDUCT_ERROR_GENERIC (   _reduct,
  _error,
  _item,
  _type,
  ... 
)
Value:
do \
{ \
const char* __path; \
const char* __input; \
size_t __inputLength; \
size_t __regionLength; \
size_t __position; \
reduct_error_get_item_params((_reduct), (_item), &__path, &__input, &__inputLength, &__regionLength, \
&__position); \
reduct_error_set((_error), __path, __input, __inputLength, __regionLength, __position, \
REDUCT_ERROR_TYPE_##_type, __VA_ARGS__); \
longjmp((_error)->jmp, true); \
} while (0)

Throw an error using the jump buffer in the error structure.

Parameters
_reductPointer to the Reduct structure.
_errorPointer to the error structure.
_itemPointer to the item that caused the error.
_typeThe suffix of the error type (e.g., INTERNAL, RUNTIME, etc.).
...The error message format string and any optional arguments.

Definition at line 159 of file error.h.

◆ REDUCT_ERROR_SUCCESS

#define REDUCT_ERROR_SUCCESS (   _error)    ((_error)->type == REDUCT_ERROR_TYPE_NONE)

Check if an error structure indicates a successful operation.

Parameters
_errorPointer to the error structure.

Definition at line 179 of file error.h.

◆ REDUCT_ERROR_CATCH

#define REDUCT_ERROR_CATCH (   _error)    (setjmp((_error)->jmp))

Catch an error using the jump buffer in the error structure.

Parameters
_errorPointer to the error structure.

Definition at line 186 of file error.h.

◆ REDUCT_ERROR_TRY

#define REDUCT_ERROR_TRY (   _reduct,
  _error 
)
Value:
for (int _once = (reduct_error_push((_reduct), (_error)), 0); _once < 1; (reduct_error_pop(_reduct), _once++)) \
if (REDUCT_ERROR_CATCH(_error) == 0)
REDUCT_API void reduct_error_push(struct reduct *reduct, reduct_error_t *error)
Push a new error handler onto the stack.
#define REDUCT_ERROR_CATCH(_error)
Catch an error using the jump buffer in the error structure.
Definition error.h:186
REDUCT_API void reduct_error_pop(struct reduct *reduct)
Pop the current error handler from the stack.

Execute a block of code safely, catching any Reduct errors.

Warning
Do not use return, break, continue, or goto to exit the block, as it will skip the necessary error stack cleanup. Use a status variable instead.
Parameters
_reductPointer to the Reduct structure.
_errorPointer to the error structure.

Definition at line 197 of file error.h.

◆ REDUCT_ERROR_THROW

#define REDUCT_ERROR_THROW (   _reduct,
  ... 
)    reduct_error_throw_runtime((_reduct), __VA_ARGS__)

Throw a runtime error using the jump buffer in the error structure.

Parameters
_reductPointer to the Reduct structure.
...The error message format string and any optional arguments.

Definition at line 207 of file error.h.

◆ REDUCT_ERROR_RETHROW

#define REDUCT_ERROR_RETHROW (   _reduct,
  _error 
)    REDUCT_ERROR_THROW(_reduct, "%s", (_error)->message);

Rethrow an existing error.

Parameters
_reductPointer to the Reduct structure.
_errorPointer to the error structure.

Definition at line 215 of file error.h.

◆ REDUCT_ERROR_ASSERT

#define REDUCT_ERROR_ASSERT (   _reduct,
  _expr,
  ... 
)
Value:
do \
{ \
if (REDUCT_UNLIKELY(!(_expr))) \
{ \
REDUCT_ERROR_THROW(_reduct, __VA_ARGS__); \
} \
} while (0)
#define REDUCT_UNLIKELY(_x)
Definition defs.h:59

Throw a runtime error if the expression is false.

Parameters
_reductPointer to the Reduct structure.
_exprThe expression to check.
...The error message format string and any optional arguments.

Definition at line 224 of file error.h.

◆ REDUCT_ERROR_SYNTAX

#define REDUCT_ERROR_SYNTAX (   _error,
  _input,
  _ptr,
  ... 
)
Value:
do \
{ \
reduct_error_set((_error), (_input)->path, (_input)->buffer, (_input)->end - (_input)->buffer, 1, \
(size_t)((_ptr) - (_input)->buffer), REDUCT_ERROR_TYPE_SYNTAX, __VA_ARGS__); \
longjmp((_error)->jmp, true); \
} while (0)
@ REDUCT_ERROR_TYPE_SYNTAX
Definition error.h:32

Throw a syntax error using the jump buffer in the error structure.

Parameters
_errorPointer to the error structure.
_inputPointer to the input structure being parsed.
_ptrPointer to the current position in the input buffer.
...The error message format string and any optional arguments.

Definition at line 241 of file error.h.

◆ REDUCT_ERROR_COMPILE

#define REDUCT_ERROR_COMPILE (   _compiler,
  _handle,
  ... 
)
Value:
do \
{ \
struct reduct_item* __item = REDUCT_HANDLE_TO_ITEM(_handle); \
REDUCT_ERROR_GENERIC((_compiler)->reduct, (_compiler)->reduct->error, \
(((__item) != NULL && (__item)->inputId != REDUCT_INPUT_ID_NONE) \
? (__item) \
: ((_compiler)->lastItem != NULL ? (_compiler)->lastItem : (__item))), \
COMPILE, __VA_ARGS__); \
} while (0)
#define REDUCT_INPUT_ID_NONE
Invalid handle value.
Definition defs.h:152
#define REDUCT_HANDLE_TO_ITEM(_handle)
Get the item pointer of a handle.
Definition handle.h:347

Throw a compile error using the jump buffer in the error structure.

Parameters
_compilerThe compiler instance.
_handlePointer to the handle that caused the error.
...The error message format string and any optional arguments.

Definition at line 256 of file error.h.

◆ REDUCT_ERROR_COMPILE_LAST

#define REDUCT_ERROR_COMPILE_LAST (   _compiler,
  ... 
)
Value:
do \
{ \
reduct_handle_t __handle = REDUCT_HANDLE_FROM_ITEM((_compiler)->lastItem); \
REDUCT_ERROR_COMPILE((_compiler), __handle, __VA_ARGS__); \
} while (0)
#define REDUCT_HANDLE_FROM_ITEM(_ptr)
Create a handle from an item pointer.
Definition handle.h:86
Handle type.
Definition defs.h:119

Throw a compile error using the jump buffer in the error structure using the last item process by the compiler.

Parameters
_compilerThe compiler instance.
...The error message format string and any optional arguments.

Definition at line 274 of file error.h.

◆ REDUCT_ERROR_COMPILE_ASSERT

#define REDUCT_ERROR_COMPILE_ASSERT (   _compiler,
  _expr,
  ... 
)
Value:
do \
{ \
if (REDUCT_UNLIKELY(!(_expr))) \
{ \
reduct_handle_t __handle = REDUCT_HANDLE_FROM_ITEM((_compiler)->lastItem); \
REDUCT_ERROR_COMPILE((_compiler), __handle, __VA_ARGS__); \
} \
} while (0)

Throw a compile error if the expression is false.

Parameters
_compilerThe compiler instance.
_handlePointer to the handle that caused the error.
_exprThe expression to check.
...The error message format string and any optional arguments.

Definition at line 289 of file error.h.

◆ REDUCT_ERROR_INTERNAL

#define REDUCT_ERROR_INTERNAL (   _reduct,
  ... 
)    REDUCT_ERROR_GENERIC(_reduct, (_reduct)->error, NULL, INTERNAL, __VA_ARGS__)

Throw an internal error using the jump buffer in the error structure.

Parameters
_reductPointer to the Reduct structure.
...The error message format string and any optional arguments.

Definition at line 305 of file error.h.

Enumeration Type Documentation

◆ reduct_error_type_t

Error type enumeration.

Enumerator
REDUCT_ERROR_TYPE_NONE 
REDUCT_ERROR_TYPE_SYNTAX 
REDUCT_ERROR_TYPE_COMPILE 
REDUCT_ERROR_TYPE_RUNTIME 
REDUCT_ERROR_TYPE_INTERNAL 

Definition at line 29 of file error.h.

Function Documentation

◆ reduct_error_print()

REDUCT_API void reduct_error_print ( reduct_error_t error,
FILE *  file 
)

Format and print the error to a file.

Parameters
errorPointer to the error structure.
fileThe file to print to.

◆ reduct_error_get_row_column()

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.

Parameters
errorPointer to the error structure.
rowPointer to the row variable.
columnPointer to the column variable.

◆ reduct_error_set()

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.

Parameters
errorPointer to the error structure.
pathThe path to the file where the error occurred.
inputThe input buffer where the error occurred.
inputLengthThe total length of the input buffer.
regionLengthThe length of the token/region that caused the error.
positionThe position in the input buffer where the error occurred.
typeThe type of the error.
messageThe error message format string.
...The arguments for the format string.

◆ reduct_error_get_item_params()

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.

Parameters
reductPointer to the Reduct structure.
itemPointer to the item.
pathPointer to the path variable.
inputPointer to the input variable.
inputLengthPointer to the input length variable.
regionLengthPointer to the region length variable.
positionPointer to the position variable.

◆ reduct_error_throw_runtime()

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.

Parameters
reductPointer to the Reduct structure.
messageThe error message format string.
...Additional arguments.

◆ reduct_error_push()

REDUCT_API void reduct_error_push ( struct reduct *  reduct,
reduct_error_t error 
)

Push a new error handler onto the stack.

This will cause the provided error handler to be called when an error occurs instead of the previous one.

Parameters
reductPointer to the Reduct structure.
errorPointer to the error structure to push.

◆ reduct_error_pop()

REDUCT_API void reduct_error_pop ( struct reduct *  reduct)

Pop the current error handler from the stack.

Parameters
reductPointer to the Reduct structure.