Clean up error definitions, location and expose them in the headers

- Exposes all errors in the header file so any user of the api can test
   for the specific error conditions
 - Mark all static error pointers as const
 - Move generic errors into error.h
 - Name all errors err_modulename_* for errors that belong to a specific
   module and err_* for generic errors.
This commit is contained in:
omicron 2025-04-04 12:23:35 +02:00
parent cb8768b1d0
commit af66790cff
8 changed files with 24 additions and 17 deletions

View File

@ -3,7 +3,7 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
error_t *err_node_children_cap = &(error_t){ error_t *const err_ast_children_cap = &(error_t){
.message = "Failed to increase ast node children, max capacity reached"}; .message = "Failed to increase ast node children, max capacity reached"};
error_t *ast_node_alloc(ast_node_t **output) { error_t *ast_node_alloc(ast_node_t **output) {
@ -50,7 +50,7 @@ error_t *ast_node_alloc_children(ast_node_t *node) {
error_t *ast_node_grow_cap(ast_node_t *node) { error_t *ast_node_grow_cap(ast_node_t *node) {
if (node->cap >= node_max_children_cap) { if (node->cap >= node_max_children_cap) {
return err_node_children_cap; return err_ast_children_cap;
} }
size_t new_cap = node->cap * 2; size_t new_cap = node->cap * 2;

View File

@ -7,6 +7,8 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
extern error_t *const err_ast_children_cap;
typedef enum node_id { typedef enum node_id {
NODE_INVALID, NODE_INVALID,

View File

@ -9,8 +9,13 @@ error_t *const err_errorf_alloc = &(error_t){
error_t *const err_errorf_length = &(error_t){ error_t *const err_errorf_length = &(error_t){
.message = .message =
"Formatting of another error failed to determine the error length"}; "Formatting of another error failed to determine the error length"};
error_t *const err_eof =
&(error_t){.message = "Read failed because EOF is reached"};
error_t *err_allocation_failed = error_t *const err_unknown_read_failure =
&(error_t){.message = "Unknown read error"};
error_t *const err_allocation_failed =
&(error_t){.message = "Memory allocation failed"}; &(error_t){.message = "Memory allocation failed"};
error_t *errorf(const char *fmt, ...) { error_t *errorf(const char *fmt, ...) {

View File

@ -19,6 +19,8 @@ static inline void error_free(error_t *err) {
} }
/* Some global errors */ /* Some global errors */
extern error_t *err_allocation_failed; extern error_t *const err_allocation_failed;
extern error_t *const err_eof;
extern error_t *const err_unknown_read_failure;
#endif // INCLUDE_SRC_ERROR_H_ #endif // INCLUDE_SRC_ERROR_H_

View File

@ -5,21 +5,16 @@
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
error_t *err_lexer_already_open = &(error_t){ error_t *const err_lexer_already_open = &(error_t){
.message = .message =
"Can't open on a lexer object that is already opened. Close it first."}; "Can't open on a lexer object that is already opened. Close it first."};
error_t *err_prefix_too_large = error_t *const err_prefix_too_large =
&(error_t){.message = "Prefix too large for internal lexer buffer"}; &(error_t){.message = "Prefix too large for internal lexer buffer"};
error_t *err_buffer_underrun = &(error_t){ error_t *const err_buffer_underrun = &(error_t){
.message = "Buffer does not contain enough characters for lexer_consume_n"}; .message = "Buffer does not contain enough characters for lexer_consume_n"};
error_t *err_consume_excessive_length = error_t *const err_consume_excessive_length =
&(error_t){.message = "Too many valid characters to consume"}; &(error_t){.message = "Too many valid characters to consume"};
error_t *err_eof =
&(error_t){.message = "Can't read from file because EOF is reached"};
error_t *err_unknown_read = &(error_t){.message = "Unknown read error"};
typedef bool (*char_predicate_t)(char); typedef bool (*char_predicate_t)(char);
const char *lexer_token_id_to_cstr(lexer_token_id_t id) { const char *lexer_token_id_to_cstr(lexer_token_id_t id) {
@ -112,7 +107,7 @@ error_t *lexer_fill_buffer(lexer_t *lex) {
if (n == 0 && ferror(lex->fp)) if (n == 0 && ferror(lex->fp))
return errorf("Read error: %s", strerror(errno)); return errorf("Read error: %s", strerror(errno));
if (n == 0) if (n == 0)
return err_unknown_read; return err_unknown_read_failure;
remaining -= n; remaining -= n;
lex->buffer_count += n; lex->buffer_count += n;
} }

View File

@ -5,7 +5,10 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
extern error_t *err_eof; extern error_t *const err_lexer_already_open;
extern error_t *const err_prefix_too_large;
extern error_t *const err_buffer_underrun;
extern error_t *const err_consume_excessive_length;
typedef enum { typedef enum {
TOKEN_ERROR, TOKEN_ERROR,

View File

@ -1,7 +1,7 @@
#include "util.h" #include "util.h"
#include "../tokenlist.h" #include "../tokenlist.h"
error_t *err_parse_no_match = error_t *const err_parse_no_match =
&(error_t){.message = "parsing failed to find the correct token sequence"}; &(error_t){.message = "parsing failed to find the correct token sequence"};
parse_result_t parse_error(error_t *err) { parse_result_t parse_error(error_t *err) {

View File

@ -21,6 +21,6 @@ parse_result_t parse_token(tokenlist_entry_t *current,
token_validator_t is_valid); token_validator_t is_valid);
parse_result_t parse_result_wrap(node_id_t id, parse_result_t result); parse_result_t parse_result_wrap(node_id_t id, parse_result_t result);
extern error_t *err_parse_no_match; extern error_t *const err_parse_no_match;
#endif // INCLUDE_PARSER_UTIL_H_ #endif // INCLUDE_PARSER_UTIL_H_