feat(parser): better error messages

main
Jef Roosens 2024-03-25 15:58:54 +01:00
parent 2dfbf8a986
commit 7227e953ae
Signed by: Jef Roosens
GPG Key ID: 02D4C0997E74717B
4 changed files with 62 additions and 11 deletions

View File

@ -37,7 +37,21 @@ int main(int argc, char **argv) {
mrk_parser_open(parser, lexer);
mrk_ast_node *root;
assert(mrk_parser_parse(&root, parser) == mrk_err_ok);
mrk_err res = mrk_parser_parse(&root, parser);
switch (res) {
case mrk_err_ok:
break;
case mrk_err_invalid_md: {
const char *err_msg = mrk_parser_err_msg(parser);
printf("%s\n", err_msg);
return 2;
}
default:
printf("mrk error %i\n", res);
return 3;
}
char *html;
assert(mrk_ast_to_html(&html, buf, root) == mrk_err_ok);

View File

@ -44,6 +44,8 @@ typedef enum mrk_token_type {
mrk_token_type_list_item_unchecked,
} mrk_token_type;
extern const char *mrk_token_type_names[];
typedef struct mrk_token {
mrk_token_type type;
size_t start;

29
src/lexer/consts.c 100644
View File

@ -0,0 +1,29 @@
#include "mrk/lexer.h"
const char *mrk_token_type_names[] = {
"",
"backtick",
"triple backtick",
"underscore",
"double underscore",
"star",
"double star",
"newline",
"blank line",
"line break",
"right angle brackets",
"left bracket",
"right bracket",
"left parenthesis",
"right parenthesis",
"bang",
"backslash",
"text",
"header start",
"horizontal rule",
"indent",
"unordered list item",
"ordered list item",
"checked box",
"unchecked box",
};

View File

@ -15,21 +15,27 @@ const char *mrk_parser_err_msg(mrk_parser *parser) {
parser->error.buf[0] = '\0';
break;
case mrk_parser_err_unexpected_token:
sprintf(parser->error.buf, "%lu:%lu: unexpected token type %i",
parser->error.token.start_line,
parser->error.token.start_line_index, parser->error.token.type);
sprintf(parser->error.buf, "%lu:%lu: unexpected token type '%s' (%i)",
parser->error.token.start_line + 1,
parser->error.token.start_line_index + 1,
mrk_token_type_names[parser->error.token.type],
parser->error.token.type);
break;
case mrk_parser_err_unexpected_eat:
sprintf(parser->error.buf,
"%lu:%lu: unexpected token, expected type %i but got %i",
parser->error.token.start_line,
parser->error.token.start_line_index,
parser->error.expected_token_type, parser->error.token.type);
sprintf(
parser->error.buf,
"%lu:%lu: unexpected token, expected type '%s' (%i) but got '%s' (%i)",
parser->error.token.start_line + 1,
parser->error.token.start_line_index + 1,
mrk_token_type_names[parser->error.expected_token_type],
parser->error.expected_token_type,
mrk_token_type_names[parser->error.token.type],
parser->error.token.type);
break;
case mrk_parser_unclosed_brackets:
sprintf(parser->error.buf, "%lu:%lu: unclosed bracket",
parser->error.token.start_line,
parser->error.token.start_line_index);
parser->error.token.start_line + 1,
parser->error.token.start_line_index + 1);
break;
case mrk_parser_unexpected_path:
return unexpected_path_msg;