diff --git a/include/mrk/parser.h b/include/mrk/parser.h index 7674145..5f21910 100644 --- a/include/mrk/parser.h +++ b/include/mrk/parser.h @@ -11,8 +11,10 @@ typedef struct mrk_parser mrk_parser; typedef enum mrk_parser_err { mrk_parser_err_ok = 0, + mrk_parser_err_unexpected_eat, mrk_parser_err_unexpected_token, mrk_parser_unclosed_brackets, + mrk_parser_unexpected_path, } mrk_parser_err; /** diff --git a/src/_include/mrk/parser_internal.h b/src/_include/mrk/parser_internal.h index 9a3c6f1..247e73e 100644 --- a/src/_include/mrk/parser_internal.h +++ b/src/_include/mrk/parser_internal.h @@ -6,11 +6,6 @@ #define MRK_PARSER_ERRMSG_BUF 256 -#define MRK_PARSE_ERR(p, t, m) \ - p->error.token = t; \ - p->error.msg = m; \ - return mrk_err_invalid - struct mrk_parser { mrk_lexer *lexer; mrk_token next_token; diff --git a/src/parser/err.c b/src/parser/err.c index 77aaeca..3a3b0e8 100644 --- a/src/parser/err.c +++ b/src/parser/err.c @@ -2,6 +2,9 @@ #include "mrk/parser_internal.h" +const char *unexpected_path_msg = + "You should never see this error; please report it."; + mrk_parser_err mrk_parser_err_code(mrk_parser *parser) { return parser->error.code; } @@ -12,6 +15,11 @@ 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); + 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, @@ -23,6 +31,8 @@ const char *mrk_parser_err_msg(mrk_parser *parser) { parser->error.token.start_line, parser->error.token.start_line_index); break; + case mrk_parser_unexpected_path: + return unexpected_path_msg; } return parser->error.buf; diff --git a/src/parser/parser.c b/src/parser/parser.c index 0ddfda0..45d0599 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -50,6 +50,10 @@ mrk_err mrk_parser_parse_indent_block(mrk_parser *parser, case mrk_token_type_list_item_ordered: MRK_RES(mrk_parser_parse_list(child, parser)); break; + default: + parser->error.code = mrk_parser_err_unexpected_token; + parser->error.token = mrk_parser_peek(parser); + return mrk_err_invalid_md; } } @@ -180,7 +184,9 @@ mrk_err mrk_parser_parse_list(mrk_ast_node *out, mrk_parser *parser) { case mrk_token_type_list_item_ordered: out->d.list.ordered = true; break; - // TODO error on default + default: + parser->error.code = mrk_parser_unexpected_path; + return mrk_err_invalid_md; } mrk_ast_node *child; @@ -238,7 +244,9 @@ mrk_err mrk_parser_parse_list_item(mrk_ast_node *out, mrk_parser *parser) { out->d.list_item.checked = false; break; // This path should never be taken - default:; + default: + parser->error.code = mrk_parser_unexpected_path; + return mrk_err_invalid_md; } mrk_parser_advance(parser); diff --git a/src/parser/util.c b/src/parser/util.c index a2df712..5fa55ef 100644 --- a/src/parser/util.c +++ b/src/parser/util.c @@ -22,7 +22,7 @@ void mrk_parser_advance(mrk_parser *parser) { mrk_err mrk_parser_eat(mrk_token *out, mrk_parser *parser, mrk_token_type type) { if (mrk_parser_done(parser) || mrk_parser_peek(parser).type != type) { - parser->error.code = mrk_parser_err_unexpected_token; + parser->error.code = mrk_parser_err_unexpected_eat; parser->error.token = mrk_parser_peek(parser); parser->error.expected_token_type = type;