Allow formatting in hbu_pipe_error
This commit is contained in:
parent
b382fb9bd0
commit
48820d8377
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include "hbchar.h"
|
||||
#include "mem.c"
|
||||
#include "../error/error.c"
|
||||
|
@ -11,6 +12,7 @@
|
|||
#include "fstreamout.c"
|
||||
|
||||
#define MAX_POS_MSG_LEN 1024
|
||||
#define MAX_ERR_MSG_LEN 1024
|
||||
|
||||
typedef int (*hbu_pipe_predicate_t)(hb_char_t);
|
||||
|
||||
|
@ -37,11 +39,55 @@ typedef struct hbu_pipe_s {
|
|||
*/
|
||||
|
||||
char *hbu_pipe_generate_pos_msg(hbu_pipe_t pipe) {
|
||||
char *msg = hbu_mem_malloc(SIZEOF_CHAR * (MAX_POS_MSG_LEN + 1));
|
||||
snprintf(msg, MAX_POS_MSG_LEN + 1, "%s [line %d, column %d]", pipe->input->name, pipe->line, pipe->column);
|
||||
char *msg = hbu_mem_calloc(MAX_POS_MSG_LEN + 1, SIZEOF_CHAR);
|
||||
snprintf(msg, MAX_POS_MSG_LEN, "%s [line %d, column %d]", pipe->input->name, pipe->line, pipe->column);
|
||||
return msg;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* MESSAGING
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Generates a warning message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param msg message
|
||||
*/
|
||||
void hbu_pipe_warn(hbu_pipe_t pipe, const char *msg) {
|
||||
hbe_warn("%s at %s", msg, hbu_pipe_generate_pos_msg(pipe));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a debug message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param msg message
|
||||
*/
|
||||
void hbu_pipe_debug(hbu_pipe_t pipe, const char *msg) {
|
||||
hbe_debug("%s at %s", msg, hbu_pipe_generate_pos_msg(pipe));
|
||||
}
|
||||
|
||||
/**
|
||||
* Exits with an error using a message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param errcode error code
|
||||
* @param reason message
|
||||
*/
|
||||
void hbu_pipe_error(hbu_pipe_t pipe, hbe_errcode_t errcode, const char *reason, ...) {
|
||||
va_list args;
|
||||
va_start(args, reason);
|
||||
|
||||
char *msg = hbu_mem_calloc(MAX_ERR_MSG_LEN + 1, SIZEOF_CHAR);
|
||||
vsnprintf(msg, MAX_ERR_MSG_LEN, reason, args);
|
||||
hbe_fatal(errcode, "%s at %s", msg, hbu_pipe_generate_pos_msg(pipe));
|
||||
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* INTERNAL FUNCTIONS
|
||||
|
@ -124,7 +170,7 @@ static void _hbu_pipe_update_pos(hbu_pipe_t pipe, hb_char_t c) {
|
|||
|
||||
static void _hbu_pipe_assert_not_eoi(hbu_pipe_t pipe, hb_eod_char_t c) {
|
||||
if (c == HB_EOD) {
|
||||
hbe_fatal(HBE_PARSE_UNEXPECTED_END, "Unexpected end of input at %s", hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_UNEXPECTED_END, "Unexpected end of input");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -516,7 +562,7 @@ void hbu_pipe_require(hbu_pipe_t pipe, hb_char_t c) {
|
|||
hb_char_t n = hbu_pipe_accept(pipe);
|
||||
|
||||
if (c != n) {
|
||||
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%c` (0x%x), got `%c` (0x%x) at %s", c, c, n, n, hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%c` (0x%x), got `%c` (0x%x)", c, c, n, n);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -532,7 +578,7 @@ hb_char_t hbu_pipe_require_skip(hbu_pipe_t pipe, hb_char_t c) {
|
|||
hb_char_t n = hbu_pipe_skip(pipe);
|
||||
|
||||
if (c != n) {
|
||||
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%c` (0x%x), got `%c` (0x%x) at %s", c, c, n, n, hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%c` (0x%x), got `%c` (0x%x) at %s", c, c, n, n);
|
||||
}
|
||||
|
||||
return n;
|
||||
|
@ -552,7 +598,7 @@ hb_char_t hbu_pipe_require_predicate(hbu_pipe_t pipe, hbu_pipe_predicate_t pred,
|
|||
hb_char_t n = hbu_pipe_accept(pipe);
|
||||
|
||||
if (!(*pred)(n)) {
|
||||
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected %s, got `%c` (0x%x) at %s", name, n, n, hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_EXPECTED_NOT_FOUND, "Expected %s, got `%c` (0x%x)", name, n, n);
|
||||
}
|
||||
|
||||
return n;
|
||||
|
@ -567,7 +613,7 @@ hb_char_t hbu_pipe_require_predicate(hbu_pipe_t pipe, hbu_pipe_predicate_t pred,
|
|||
*/
|
||||
void hbu_pipe_require_match(hbu_pipe_t pipe, const char *match) {
|
||||
if (!hbu_pipe_accept_if_matches(pipe, match)) {
|
||||
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected %s at %s", match, hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%s`", match);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -580,42 +626,10 @@ void hbu_pipe_require_match(hbu_pipe_t pipe, const char *match) {
|
|||
*/
|
||||
void hbu_pipe_require_skip_match(hbu_pipe_t pipe, const char *match) {
|
||||
if (!hbu_pipe_skip_if_matches(pipe, match)) {
|
||||
hbe_fatal(HBE_PARSE_EXPECTED_NOT_FOUND, "Expected %s at %s", match, hbu_pipe_generate_pos_msg(pipe));
|
||||
hbu_pipe_error(pipe, HBE_PARSE_EXPECTED_NOT_FOUND, "Expected `%s`", match);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a warning message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param msg message
|
||||
*/
|
||||
void hbu_pipe_warn(hbu_pipe_t pipe, const char *msg) {
|
||||
hbe_warn("%s at %s", msg, hbu_pipe_generate_pos_msg(pipe));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a debug message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param msg message
|
||||
*/
|
||||
void hbu_pipe_debug(hbu_pipe_t pipe, const char *msg) {
|
||||
hbe_debug("%s at %s", msg, hbu_pipe_generate_pos_msg(pipe));
|
||||
}
|
||||
|
||||
/**
|
||||
* Exits with an error using a message with the current position appended.
|
||||
*
|
||||
* @param pipe pipe
|
||||
* @param errcode error code
|
||||
* @param reason message
|
||||
*/
|
||||
// Don't use this function for error calls in this file, as this function doesn't support varargs
|
||||
void hbu_pipe_error(hbu_pipe_t pipe, hbe_errcode_t errcode, const char *reason) {
|
||||
hbe_fatal(errcode, "%s at %s", reason, hbu_pipe_generate_pos_msg(pipe));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a character.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue