simplified error output

This commit is contained in:
hugogogo
2026-05-06 15:58:31 +02:00
parent 7f06d505d1
commit 8a479c3c3b
6 changed files with 27 additions and 61 deletions

View File

@@ -133,7 +133,7 @@ static void launch_argv(char *input)
ret = lexerize(input, tokens);
if (ret == 0)
{
stop_errors(ERROR_TOKEN_COUNT, "lexer returned 0 token");
stop_errors("lexer returned 0 token");
}
// parse
@@ -145,7 +145,7 @@ static void launch_argv(char *input)
ret = parse(tokens, terms, terms_count_prediction);
if (ret == 0)
{
stop_errors(ERROR_TERM_COUNT, "parser returned 0 term");
stop_errors("parser returned 0 term");
}
// reduce
@@ -240,7 +240,7 @@ int main(int ac, char **av)
program_mode = MODE_ARGV;
if (ac > 3)
{
stop_errors(ARGUMENTS_ERROR, "too many arguments");
stop_errors("too many arguments");
}
else if (ac == 3)
{
@@ -256,7 +256,7 @@ int main(int ac, char **av)
}
else
{
stop_errors(ARGUMENTS_ERROR, "3rd argument is not a valid flag: '%s'", av[2]);
stop_errors("3rd argument is not a valid flag: '%s'", av[2]);
}
}
else if (ac == 2)

View File

@@ -47,7 +47,7 @@ static bool token_is_number_int(const char *input, int input_pos, int *token_siz
}
if (number_size > max_number_size)
{
stop_errors(ERROR_NUMBER_TOO_BIG, &input[input_pos]);
stop_errors(&input[input_pos]);
}
*token_size = number_size;
return true;
@@ -94,7 +94,7 @@ static bool token_is_number_double(const char *input, int input_pos, int *token_
}
if (number_size > max_number_size)
{
stop_errors(ERROR_NUMBER_TOO_BIG, &input[input_pos]);
stop_errors(&input[input_pos]);
}
*token_size = number_size;
return true;
@@ -252,13 +252,13 @@ int lexerize(const char *input, s_token *tokens)
}
else
{
stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
stop_errors(&input[input_pos]);
}
tokens_count++;
if (token_size == 0)
{
stop_errors(ERROR_UNKNOWN_TOKEN, &input[input_pos]);
stop_errors(&input[input_pos]);
}
input_pos += token_size;
}

View File

@@ -34,7 +34,7 @@ static e_term_sign get_sign(s_token *tokens, int i, int *token_count)
}
else
{
stop_errors(ERROR_TOKEN_POSITION, "at begining of term, we should have a token 'sign', not '%s' (token[%i])", token_type_to_str(tokens[i].type), i);
stop_errors("at begining of term, we should have a token 'sign', not '%s' (token[%i])", token_type_to_str(tokens[i].type), i);
}
return tokens[i].type == TOKEN_SIGN_PLUS ? TERM_PLUS : TERM_MINUS;
@@ -44,7 +44,7 @@ static double get_double_value(s_token token)
{
if (token.tag != TOKEN_NUMBER)
{
stop_errors(ERROR_UNKNOWN_TOKEN, "this was suppose to be a number, instead got a %s", token_type_to_str(token.type));
stop_errors("this was suppose to be a number, instead got a %s", token_type_to_str(token.type));
}
if (token.type == TOKEN_NUMBER_DOUBLE)
{
@@ -131,7 +131,7 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
}
else
{
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, after a '*' we should have a 'var', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
stop_errors("at exponent place, after a '*' we should have a 'var', but instead got : '%c' (token number %i)", tokens[i].value_char, i);
}
}
else
@@ -160,7 +160,7 @@ static int get_exponent(s_token *tokens, int i, int *token_count)
}
else
{
stop_errors(ERROR_TOKEN_POSITION, "at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
stop_errors("at exponent place, we should have an int, but instead got : '%c' (token number %i)", tokens[i].value_char, i);
}
return tokens[i].value_int;
@@ -184,7 +184,7 @@ static void check_variables(s_token *tokens)
}
else if (var != tokens[i].value_char)
{
stop_errors(ERROR_VAR_DIFF, "old var : '%c' - new var : '%c' (token number %i)", var, tokens[i].value_char, i);
stop_errors("old var : '%c' - new var : '%c' (token number %i)", var, tokens[i].value_char, i);
}
}
i++;
@@ -256,7 +256,7 @@ int parse(s_token *tokens, s_term *terms, int terms_count_max)
}
else
{
stop_errors(ERROR_PARSING, "terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type));
stop_errors("terms_count: %i, terms_count_max: %i, tokens[%i].type: %s", terms_count, terms_count_max, i, token_type_to_str(tokens[i].type));
}
return terms_count;

View File

@@ -121,43 +121,21 @@ void print_state()
print_context_polynom();
}
int stop_errors(e_program_error err, const char *details, ...)
int stop_errors(const char *description, ...)
{
// the base error message
const char *msg = "error: error type is out of range";
// map error codes to messages
const char *error_messages[ERROR_SENTINEL] = {
[ERROR_BASE] = "undefined error",
[ARGUMENTS_ERROR] = "arguments error",
[ERROR_UNKNOWN_TOKEN] = "LEXER - unknown token",
[ERROR_NUMBER_TOO_BIG] = "LEXER - number is too big",
[ERROR_PARSING] = "PARSER - too much terms to parse",
[ERROR_TOKEN_POSITION] = "PARSER - token position is not good in grammar",
[ERROR_VAR_DIFF] = "PARSER - expression must only contain one variable",
[ERROR_TOKEN_COUNT] = "LEXER - token count error",
[ERROR_TERM_COUNT] = "PARSER - term count error",
};
// override msg if err is in the error_messages array
if (err >= ERROR_BASE && err < ERROR_SENTINEL)
{
msg = error_messages[err];
}
// print context
// print context (if debug mode)
print_state();
// print the base message
ft_dprintf(STDERR_FILENO, "error: (%i) %s - details: ", err, msg);
ft_putstr_fd("\e[1;31mERROR:\e[0m ", STDERR_FILENO);
// print the formatted details
// print the formatted description
va_list args;
va_start(args, details);
ft_vdprintf(STDERR_FILENO, details, args);
va_start(args, description);
ft_vdprintf(STDERR_FILENO, description, args);
va_end(args);
ft_putchar_fd('\n', STDERR_FILENO);
// print the base message
ft_dprintf(STDERR_FILENO, " (errno[%d] : %s)\n", errno, strerror(errno));
exit(err);
exit(EXIT_FAILURE);
}