chore(parser): stop the switch statement warnings

main
Jef Roosens 2024-03-19 14:54:58 +01:00
parent d235bd0000
commit e0852de230
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
5 changed files with 23 additions and 8 deletions

View File

@ -11,8 +11,10 @@ typedef struct mrk_parser mrk_parser;
typedef enum mrk_parser_err { typedef enum mrk_parser_err {
mrk_parser_err_ok = 0, mrk_parser_err_ok = 0,
mrk_parser_err_unexpected_eat,
mrk_parser_err_unexpected_token, mrk_parser_err_unexpected_token,
mrk_parser_unclosed_brackets, mrk_parser_unclosed_brackets,
mrk_parser_unexpected_path,
} mrk_parser_err; } mrk_parser_err;
/** /**

View File

@ -6,11 +6,6 @@
#define MRK_PARSER_ERRMSG_BUF 256 #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 { struct mrk_parser {
mrk_lexer *lexer; mrk_lexer *lexer;
mrk_token next_token; mrk_token next_token;

View File

@ -2,6 +2,9 @@
#include "mrk/parser_internal.h" #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) { mrk_parser_err mrk_parser_err_code(mrk_parser *parser) {
return parser->error.code; return parser->error.code;
} }
@ -12,6 +15,11 @@ const char *mrk_parser_err_msg(mrk_parser *parser) {
parser->error.buf[0] = '\0'; parser->error.buf[0] = '\0';
break; break;
case mrk_parser_err_unexpected_token: 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, sprintf(parser->error.buf,
"%lu:%lu: unexpected token, expected type %i but got %i", "%lu:%lu: unexpected token, expected type %i but got %i",
parser->error.token.start_line, 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,
parser->error.token.start_line_index); parser->error.token.start_line_index);
break; break;
case mrk_parser_unexpected_path:
return unexpected_path_msg;
} }
return parser->error.buf; return parser->error.buf;

View File

@ -50,6 +50,10 @@ mrk_err mrk_parser_parse_indent_block(mrk_parser *parser,
case mrk_token_type_list_item_ordered: case mrk_token_type_list_item_ordered:
MRK_RES(mrk_parser_parse_list(child, parser)); MRK_RES(mrk_parser_parse_list(child, parser));
break; 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: case mrk_token_type_list_item_ordered:
out->d.list.ordered = true; out->d.list.ordered = true;
break; break;
// TODO error on default default:
parser->error.code = mrk_parser_unexpected_path;
return mrk_err_invalid_md;
} }
mrk_ast_node *child; 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; out->d.list_item.checked = false;
break; break;
// This path should never be taken // This path should never be taken
default:; default:
parser->error.code = mrk_parser_unexpected_path;
return mrk_err_invalid_md;
} }
mrk_parser_advance(parser); mrk_parser_advance(parser);

View File

@ -22,7 +22,7 @@ void mrk_parser_advance(mrk_parser *parser) {
mrk_err mrk_parser_eat(mrk_token *out, mrk_parser *parser, mrk_err mrk_parser_eat(mrk_token *out, mrk_parser *parser,
mrk_token_type type) { mrk_token_type type) {
if (mrk_parser_done(parser) || mrk_parser_peek(parser).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.token = mrk_parser_peek(parser);
parser->error.expected_token_type = type; parser->error.expected_token_type = type;