diff --git a/lang/ast/block.c b/lang/ast/block.c index 3865445..08575fa 100644 --- a/lang/ast/block.c +++ b/lang/ast/block.c @@ -58,7 +58,7 @@ struct ast_node_type block_node_ops = { .n_state_size = sizeof(struct block_parser_state), .n_node_size = sizeof(struct ivy_ast_block_node), .n_keyword_parsers = { - [IVY_KW_END] = parse_end, + KW_PARSER(END, parse_end), }, .n_expr_parser = { .expr_begin = parse_expr_begin, diff --git a/lang/ast/class.c b/lang/ast/class.c index 254839b..f50507b 100644 --- a/lang/ast/class.c +++ b/lang/ast/class.c @@ -164,15 +164,15 @@ struct ast_node_type class_node_ops = { .n_state_size = sizeof(struct class_parser_state), .n_node_size = sizeof(struct ivy_ast_class_node), .n_symbol_parsers = { - [IVY_SYM_DOLLAR] = parse_dollar, - [IVY_SYM_HYPHEN] = parse_hyphen, - [IVY_SYM_PLUS] = parse_plus, + SYM_PARSER(DOLLAR, parse_dollar), + SYM_PARSER(HYPHEN, parse_hyphen), + SYM_PARSER(PLUS, parse_plus), }, .n_token_parsers = { - [IVY_TOK_IDENT] = parse_ident, - [IVY_TOK_LINEFEED] = parse_linefeed, + TOK_PARSER(IDENT, parse_ident), + TOK_PARSER(LINEFEED, parse_linefeed), }, .n_keyword_parsers = { - [IVY_KW_END] = parse_end, + KW_PARSER(END, parse_end), }, }; diff --git a/lang/ast/expr.c b/lang/ast/expr.c index 10b5623..3029046 100644 --- a/lang/ast/expr.c +++ b/lang/ast/expr.c @@ -441,38 +441,38 @@ struct ast_node_type expr_node_ops = { .n_state_size = sizeof(struct expr_parser_state), .n_node_size = sizeof(struct ivy_ast_expr_node), .n_token_parsers = { - [IVY_TOK_IDENT] = parse_ident, - [IVY_TOK_ATOM] = parse_atom, - [IVY_TOK_STRING] = parse_string, - [IVY_TOK_STR_START] = parse_str_start, - [IVY_TOK_STR_END] = parse_str_end, - [IVY_TOK_LABEL] = parse_label, - [IVY_TOK_INT] = parse_int, - [IVY_TOK_DOUBLE] = parse_double, - [IVY_TOK_SYMBOL] = parse_symbol, + TOK_PARSER(IDENT, parse_ident), + TOK_PARSER(ATOM, parse_atom), + TOK_PARSER(STRING, parse_string), + TOK_PARSER(STR_START, parse_str_start), + TOK_PARSER(STR_END, parse_str_end), + TOK_PARSER(LABEL, parse_label), + TOK_PARSER(INT, parse_int), + TOK_PARSER(DOUBLE, parse_double), + TOK_PARSER(SYMBOL, parse_symbol), }, .n_symbol_parsers = { - [IVY_SYM_BANG] = parse_bang, - [IVY_SYM_DOT] = parse_dot, - [IVY_SYM_LEFT_PAREN] = parse_left_paren, - [IVY_SYM_RIGHT_PAREN] = parse_right_paren, + SYM_PARSER(BANG, parse_bang), + SYM_PARSER(DOT, parse_dot), + SYM_PARSER(LEFT_PAREN, parse_left_paren), + SYM_PARSER(RIGHT_PAREN, parse_right_paren), }, .n_keyword_parsers = { - [IVY_KW_IF] = parse_if, - [IVY_KW_ELSE] = parse_else, - [IVY_KW_END] = parse_end, - [IVY_KW_WHILE] = parse_while, - [IVY_KW_FOR] = parse_for, - [IVY_KW_MATCH] = parse_match, - [IVY_KW_TRY] = parse_try, - [IVY_KW_CATCH] = parse_catch, - [IVY_KW_THROW] = parse_throw, - [IVY_KW_UNDERSTANDS] = parse_understands, - [IVY_KW_IN] = parse_in, - [IVY_KW_DO] = parse_do, - [IVY_KW_IS] = parse_is, - [IVY_KW_AND] = parse_and, - [IVY_KW_OR] = parse_or, - [IVY_KW_NOT] = parse_not, + KW_PARSER(IF, parse_if), + KW_PARSER(ELSE, parse_else), + KW_PARSER(END, parse_end), + KW_PARSER(WHILE, parse_while), + KW_PARSER(FOR, parse_for), + KW_PARSER(MATCH, parse_match), + KW_PARSER(TRY, parse_try), + KW_PARSER(CATCH, parse_catch), + KW_PARSER(THROW, parse_throw), + KW_PARSER(UNDERSTANDS, parse_understands), + KW_PARSER(IN, parse_in), + KW_PARSER(DO, parse_do), + KW_PARSER(IS, parse_is), + KW_PARSER(AND, parse_and), + KW_PARSER(OR, parse_or), + KW_PARSER(NOT, parse_not), }, }; diff --git a/lang/ast/msgh.c b/lang/ast/msgh.c index e05f840..fa4e38f 100644 --- a/lang/ast/msgh.c +++ b/lang/ast/msgh.c @@ -85,6 +85,6 @@ struct ast_node_type msgh_node_ops = { .expr_begin = parse_expr, }, .n_symbol_parsers = { - [IVY_SYM_BANG] = parse_bang, + SYM_PARSER(BANG, parse_bang), }, }; diff --git a/lang/ast/node.c b/lang/ast/node.c index 8411ff1..9220ef5 100644 --- a/lang/ast/node.c +++ b/lang/ast/node.c @@ -107,25 +107,25 @@ token_parse_function get_token_parser( if (!type) { return NULL; } - token_parse_function generic_parser = type->n_token_parsers[tok->t_type]; + token_parse_function generic_parser = type->n_token_parsers[__TOK_PARSER_INDEX(tok->t_type)]; if (!generic_parser) { - generic_parser = type->n_token_parsers[IVY_TOK_NONE]; + generic_parser = type->n_token_parsers[__TOK_PARSER_FALLBACK_INDEX]; } token_parse_function better_parser = NULL; switch (tok->t_type) { case IVY_TOK_KEYWORD: - better_parser = type->n_keyword_parsers[tok->t_keyword]; - if (type->n_keyword_parsers[IVY_KW_NONE]) { - generic_parser = type->n_keyword_parsers[IVY_KW_NONE]; + better_parser = type->n_keyword_parsers[__KW_PARSER_INDEX(tok->t_keyword)]; + if (type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX]) { + generic_parser = type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX]; } break; case IVY_TOK_SYMBOL: - better_parser = type->n_symbol_parsers[tok->t_symbol]; - if (type->n_symbol_parsers[IVY_SYM_NONE]) { - generic_parser = type->n_symbol_parsers[IVY_SYM_NONE]; + better_parser = type->n_symbol_parsers[__SYM_PARSER_INDEX(tok->t_symbol)]; + if (type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX]) { + generic_parser = type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX]; } break; default: diff --git a/lang/ast/node.h b/lang/ast/node.h index 649d997..565b6f6 100644 --- a/lang/ast/node.h +++ b/lang/ast/node.h @@ -12,6 +12,21 @@ struct parser_state; #define PARSE_RESULT(status, flags) \ ((struct token_parse_result) {.r_status = (status), .r_flags = (flags)}) +#define __TOK_PARSER_INDEX(x) ((x) - __IVY_TOK_INDEX_BASE) +#define __SYM_PARSER_INDEX(x) ((x) - __IVY_SYM_INDEX_BASE) +#define __KW_PARSER_INDEX(x) ((x) - __IVY_KW_INDEX_BASE) + +#define __TOK_PARSER_FALLBACK_INDEX IVY_TOK_NONE +#define __SYM_PARSER_FALLBACK_INDEX IVY_SYM_NONE +#define __KW_PARSER_FALLBACK_INDEX IVY_KW_NONE + +#define TOK_PARSER(id, func) [__TOK_PARSER_INDEX(IVY_TOK_##id)] = func +#define SYM_PARSER(id, func) [__SYM_PARSER_INDEX(IVY_SYM_##id)] = func +#define KW_PARSER(id, func) [__KW_PARSER_INDEX(IVY_KW_##id)] = func + +#define TOK_PARSER_FALLBACK(func) [__TOK_PARSER_FALLBACK_INDEX] = func +#define SYM_PARSER_FALLBACK(func) [__SYM_PARSER_FALLBACK_INDEX] = func +#define KW_PARSER_FALLBACK(func) [__KW_PARSER_FALLBACK_INDEX] = func enum token_parse_flags { PARSE_REPEAT_TOKEN = 0x01u, @@ -42,9 +57,10 @@ struct ast_node_type { size_t n_state_size; size_t n_node_size; - token_parse_function n_token_parsers[IVY_TOK_TYPE_COUNT]; - token_parse_function n_keyword_parsers[IVY_KW_TYPE_COUNT]; - token_parse_function n_symbol_parsers[IVY_SYM_TYPE_COUNT]; + token_parse_function n_token_parsers[__TOK_PARSER_INDEX(__IVY_TOK_INDEX_LIMIT)]; + token_parse_function n_keyword_parsers[__KW_PARSER_INDEX(__IVY_KW_INDEX_LIMIT)]; + token_parse_function n_symbol_parsers[__SYM_PARSER_INDEX(__IVY_SYM_INDEX_LIMIT)]; + struct { token_parse_function expr_begin; token_parse_function expr_other; diff --git a/lang/ast/selector.c b/lang/ast/selector.c index d18604c..94393a3 100644 --- a/lang/ast/selector.c +++ b/lang/ast/selector.c @@ -178,13 +178,13 @@ struct ast_node_type selector_node_ops = { .n_state_size = sizeof(struct selector_parser_state), .n_node_size = sizeof(struct ivy_ast_selector_node), .n_token_parsers = { - [IVY_TOK_IDENT] = parse_ident, - [IVY_TOK_LABEL] = parse_label, - [IVY_TOK_NONE] = parse_other, + TOK_PARSER(IDENT, parse_ident), + TOK_PARSER(LABEL, parse_label), + TOK_PARSER_FALLBACK(parse_other), }, .n_symbol_parsers = { - [IVY_SYM_LEFT_PAREN] = parse_left_paren, - [IVY_SYM_RIGHT_PAREN] = parse_right_paren, - [IVY_SYM_PIPE] = parse_pipe, + SYM_PARSER(LEFT_PAREN, parse_left_paren), + SYM_PARSER(RIGHT_PAREN, parse_right_paren), + SYM_PARSER(PIPE, parse_pipe), }, }; diff --git a/lang/ast/unit-import.c b/lang/ast/unit-import.c index 4778768..58fec01 100644 --- a/lang/ast/unit-import.c +++ b/lang/ast/unit-import.c @@ -99,11 +99,11 @@ struct ast_node_type unit_import_node_ops = { .n_state_size = sizeof(struct unit_import_parser_state), .n_node_size = sizeof(struct ivy_ast_unit_import_node), .n_symbol_parsers = { - [IVY_SYM_DOT] = parse_dot, + SYM_PARSER(DOT, parse_dot), }, .n_token_parsers = { - [IVY_TOK_IDENT] = parse_ident, - [IVY_TOK_LINEFEED] = parse_linefeed, + TOK_PARSER(IDENT, parse_ident), + TOK_PARSER(LINEFEED, parse_linefeed), } }; diff --git a/lang/ast/unit-package.c b/lang/ast/unit-package.c index 54b58d6..a3bb9b3 100644 --- a/lang/ast/unit-package.c +++ b/lang/ast/unit-package.c @@ -100,11 +100,11 @@ struct ast_node_type unit_package_node_ops = { .n_state_size = sizeof(struct unit_package_parser_state), .n_node_size = sizeof(struct ivy_ast_unit_package_node), .n_symbol_parsers = { - [IVY_SYM_DOT] = parse_dot, + SYM_PARSER(DOT, parse_dot), }, .n_token_parsers = { - [IVY_TOK_IDENT] = parse_ident, - [IVY_TOK_LINEFEED] = parse_linefeed, + TOK_PARSER(IDENT, parse_ident), + TOK_PARSER(LINEFEED, parse_linefeed), } -}; +}; \ No newline at end of file diff --git a/lang/ast/unit.c b/lang/ast/unit.c index b5b93f3..b51a93f 100644 --- a/lang/ast/unit.c +++ b/lang/ast/unit.c @@ -51,8 +51,8 @@ struct ast_node_type unit_node_ops = { .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_unit_node), .n_keyword_parsers = { - [IVY_KW_PACKAGE] = parse_package_keyword, - [IVY_KW_CLASS] = parse_class_keyword, - [IVY_KW_USE] = parse_use_keyword, + KW_PARSER(PACKAGE, parse_package_keyword), + KW_PARSER(CLASS, parse_class_keyword), + KW_PARSER(USE, parse_use_keyword), }, };