From 20f1d2b5ac2c7acb439a647d56b549b3463d257a Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 28 Nov 2023 21:25:53 +0100 Subject: [PATCH] Implemented a basic logger --- code/inc/logger.h | 105 ++++++++++++++++++++++++++++++++++++++++++++++ code/src/logger.c | 45 ++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 code/inc/logger.h create mode 100644 code/src/logger.c diff --git a/code/inc/logger.h b/code/inc/logger.h new file mode 100644 index 0000000..51ce7e0 --- /dev/null +++ b/code/inc/logger.h @@ -0,0 +1,105 @@ + +#ifndef PARCEL_LOGGER_H +#define PARCEL_LOGGER_H + +#include + +typedef enum +{ + PAC_SYNTAX_ERROR, + PAC_NAMING_ERROR, + PAC_INTERNAL_ERROR + +} pac_error_e; + +typedef enum +{ + PAC_SYNTAX_ERROR_UNSPECIFIED, + PAC_SYNTAX_ERROR_STRAY, + PAC_SYNTAX_ERROR_MISSING_TOKEN, + PAC_SYNTAX_ERROR_ODD_TOKEN + +} pac_syntax_error_e; + +typedef enum +{ + PAC_RESTRICTED_ERROR_INVALID_RULE_NAME + +} pac_naming_error_e; + +typedef struct pac_syntax_error +{ + usz_t line; + usz_t column; + + pac_syntax_error_e type; + union pac_syntax_error_specifics + { + struct pac_syntax_error_stray + { + rune_t sign; + } stray; + + struct pac_syntax_error_missing_token + { + char *wanted_token; + char *hint; + } missing_token; + + struct pac_syntax_error_odd_token + { + usz_t num_valid_options; + char *valid_options[16]; + char *found_token; + char *hint; + } odd_token; + + } specifics; + +} pac_syntax_error_s; + +typedef struct pac_naming_error +{ + usz_t line; + usz_t column; + + pac_naming_error_e type; + union pac_naming_error_specifics + { + struct pac_naming_error_invalid_rule_name + { + char *given_rule_name; + } invalid_rule_name; + } specifics; + +} pac_naming_error_s; + + +typedef struct pac_error +{ + pac_error_e type; + union pac_error_data + { + pac_syntax_error_s syntax_error; + pac_naming_error_s naming_error; + } specifics; + +} pac_error_s; + +typedef struct pac_logger +{ + usz_t allocated_errors; + usz_t num_errors; + pac_error_s *errors; + + pac_arena_s string_arena; + +} pac_logger_s; + +pac_logger_s pac_create_logger (); +void pac_log_syntax_error (pac_logger_s *logger, pac_syntax_error_s error); +void pac_log_naming_error (pac_logger_s *logger, pac_naming_error_s error); + +void * pac_log_alloc (pac_logger_s *logger, usz_t num_bytes); + +#endif // PARCEL_LOGGER_H diff --git a/code/src/logger.c b/code/src/logger.c new file mode 100644 index 0000000..c7f8ada --- /dev/null +++ b/code/src/logger.c @@ -0,0 +1,45 @@ +#include + +pac_logger_s pac_create_logger() +{ + pac_logger_s logger; + logger.allocated_errors = 1024; + logger.num_errors = 0; + logger.errors = calloc(sizeof(pac_error_s), logger.allocated_errors); + logger.string_arena = pac_create_arena(32768); + + return logger; +} + +void pac_resize_log_if_needed(pac_logger_s *logger) +{ + if(logger->num_errors >= logger->allocated_errors) + { + logger->allocated_errors *= 2; + logger->errors = + realloc(logger->errors, sizeof(pac_error_s) * logger->allocated_errors); + } +} + +void pac_log_syntax_error(pac_logger_s *logger, pac_syntax_error_s error) +{ + pac_resize_log_if_needed(logger); + logger->errors[logger->num_errors].type = PAC_SYNTAX_ERROR; + logger->errors[logger->num_errors].specifics.syntax_error = error; + ++logger->num_errors; +} + +void pac_log_naming_error(pac_logger_s *logger, pac_naming_error_s error) +{ + pac_resize_log_if_needed(logger); + logger->errors[logger->num_errors].type = PAC_NAMING_ERROR; + logger->errors[logger->num_errors].specifics.naming_error = error; + ++logger->num_errors; +} + + + +void * pac_log_alloc(pac_logger_s *logger, usz_t num_bytes) +{ + return ac_arena_alloc(logger->string_arena, num_bytes); +}