lang: replace stack-based parser with a recursive parser
This commit is contained in:
+3
-138
@@ -12,9 +12,9 @@ extern struct ast_node_type unit_import_node_ops;
|
||||
extern struct ast_node_type class_node_ops;
|
||||
extern struct ast_node_type msgh_node_ops;
|
||||
extern struct ast_node_type selector_node_ops;
|
||||
extern struct ast_node_type expr_node_ops;
|
||||
extern struct ast_node_type block_node_ops;
|
||||
extern struct ast_node_type msg_node_ops;
|
||||
extern struct ast_node_type do_node_ops;
|
||||
extern struct ast_node_type op_node_ops;
|
||||
extern struct ast_node_type global_node_ops;
|
||||
extern struct ast_node_type ident_node_ops;
|
||||
@@ -34,8 +34,6 @@ extern struct ast_node_type loop_break_node_ops;
|
||||
extern struct ast_node_type loop_repeat_node_ops;
|
||||
extern struct ast_node_type return_node_ops;
|
||||
extern struct ast_node_type property_node_ops;
|
||||
extern struct ast_node_type lambda_node_ops;
|
||||
extern struct ast_node_type pkg_node_ops;
|
||||
extern struct ast_node_type pkg_static_node_ops;
|
||||
extern struct ast_node_type pkg_static_item_node_ops;
|
||||
extern struct ast_node_type pkg_dynamic_node_ops;
|
||||
@@ -53,9 +51,9 @@ static const struct ast_node_type *node_ops[] = {
|
||||
[IVY_AST_CLASS] = &class_node_ops,
|
||||
[IVY_AST_MSGH] = &msgh_node_ops,
|
||||
[IVY_AST_SELECTOR] = &selector_node_ops,
|
||||
[IVY_AST_EXPR] = &expr_node_ops,
|
||||
[IVY_AST_BLOCK] = &block_node_ops,
|
||||
[IVY_AST_MSG] = &msg_node_ops,
|
||||
[IVY_AST_DO] = &do_node_ops,
|
||||
[IVY_AST_OP] = &op_node_ops,
|
||||
[IVY_AST_GLOBAL] = &global_node_ops,
|
||||
[IVY_AST_IDENT] = &ident_node_ops,
|
||||
@@ -75,8 +73,6 @@ static const struct ast_node_type *node_ops[] = {
|
||||
[IVY_AST_LOOP_REPEAT] = &loop_repeat_node_ops,
|
||||
[IVY_AST_RETURN] = &return_node_ops,
|
||||
[IVY_AST_PROPERTY] = &property_node_ops,
|
||||
[IVY_AST_LAMBDA] = &lambda_node_ops,
|
||||
[IVY_AST_PKG] = &pkg_node_ops,
|
||||
[IVY_AST_PKG_STATIC] = &pkg_static_node_ops,
|
||||
[IVY_AST_PKG_ITEM] = &pkg_static_item_node_ops,
|
||||
[IVY_AST_PKG_DYNAMIC] = &pkg_dynamic_node_ops,
|
||||
@@ -98,135 +94,6 @@ const struct ast_node_type *get_ast_node_type(enum ivy_ast_node_type type)
|
||||
return node_ops[type];
|
||||
}
|
||||
|
||||
enum token_expr_type get_token_expr_type(struct ivy_token *tok)
|
||||
{
|
||||
const struct ivy_operator *op = NULL;
|
||||
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_IDENT:
|
||||
case IVY_TOK_INT:
|
||||
case IVY_TOK_DOUBLE:
|
||||
case IVY_TOK_STRING:
|
||||
case IVY_TOK_STR_START:
|
||||
case IVY_TOK_ATOM:
|
||||
return TOK_EXPR_BEGIN;
|
||||
case IVY_TOK_SYMBOL:
|
||||
switch (tok->t_symbol) {
|
||||
case IVY_SYM_LEFT_PAREN:
|
||||
case IVY_SYM_LEFT_BRACKET:
|
||||
case IVY_SYM_LEFT_BRACE:
|
||||
case IVY_SYM_CARET:
|
||||
case IVY_SYM_UNDERSCORE:
|
||||
return TOK_EXPR_BEGIN;
|
||||
case IVY_SYM_COMMA:
|
||||
return TOK_EXPR_ANY;
|
||||
default:
|
||||
op = ivy_operator_get_by_token(tok->t_symbol);
|
||||
return op ? TOK_EXPR_ANY : TOK_EXPR_NONE;
|
||||
}
|
||||
case IVY_TOK_KEYWORD:
|
||||
switch (tok->t_keyword) {
|
||||
case IVY_KW_IF:
|
||||
case IVY_KW_ELSE:
|
||||
case IVY_KW_MATCH:
|
||||
case IVY_KW_FOR:
|
||||
case IVY_KW_WHILE:
|
||||
case IVY_KW_BREAK:
|
||||
case IVY_KW_CONTINUE:
|
||||
case IVY_KW_TRY:
|
||||
case IVY_KW_THROW:
|
||||
case IVY_KW_TRUE:
|
||||
case IVY_KW_FALSE:
|
||||
case IVY_KW_NULL:
|
||||
return TOK_EXPR_BEGIN;
|
||||
default:
|
||||
op = ivy_operator_get_by_token(tok->t_keyword);
|
||||
return op ? TOK_EXPR_ANY : TOK_EXPR_NONE;
|
||||
return TOK_EXPR_NONE;
|
||||
}
|
||||
default:
|
||||
return TOK_EXPR_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
token_parse_function get_token_parser(
|
||||
struct ivy_ast_node *context, struct ivy_token *tok)
|
||||
{
|
||||
token_parse_function type_parser = NULL;
|
||||
token_parse_function type_fallback_parser = NULL;
|
||||
token_parse_function token_parser = NULL;
|
||||
token_parse_function expr_begin_parser = NULL;
|
||||
token_parse_function expr_other_parser = NULL;
|
||||
token_parse_function expr_parser = NULL;
|
||||
token_parse_function token_fallback_parser = NULL;
|
||||
|
||||
const struct ast_node_type *type = get_ast_node_type(context->n_type);
|
||||
if (!type) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
token_parser = type->n_token_parsers[__TOK_PARSER_INDEX(tok->t_type)];
|
||||
token_fallback_parser = type->n_token_parsers[__TOK_PARSER_FALLBACK_INDEX];
|
||||
|
||||
token_parse_function better_parser = NULL;
|
||||
|
||||
switch (tok->t_type) {
|
||||
case IVY_TOK_KEYWORD:
|
||||
type_parser
|
||||
= type->n_keyword_parsers[__KW_PARSER_INDEX(tok->t_keyword)];
|
||||
if (type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX]) {
|
||||
type_fallback_parser
|
||||
= type->n_keyword_parsers[__KW_PARSER_FALLBACK_INDEX];
|
||||
}
|
||||
break;
|
||||
case IVY_TOK_SYMBOL:
|
||||
type_parser
|
||||
= type->n_symbol_parsers[__SYM_PARSER_INDEX(tok->t_symbol)];
|
||||
if (type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX]) {
|
||||
type_fallback_parser
|
||||
= type->n_symbol_parsers[__SYM_PARSER_FALLBACK_INDEX];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
enum token_expr_type expr_type = get_token_expr_type(tok);
|
||||
switch (expr_type) {
|
||||
case TOK_EXPR_BEGIN:
|
||||
expr_begin_parser = type->n_expr_parser.expr_begin;
|
||||
expr_parser = type->n_expr_parser.expr_all;
|
||||
break;
|
||||
case TOK_EXPR_ANY:
|
||||
expr_other_parser = type->n_expr_parser.expr_other;
|
||||
expr_parser = type->n_expr_parser.expr_all;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
bool token_has_sub_id
|
||||
= (tok->t_type == IVY_TOK_KEYWORD || tok->t_type == IVY_TOK_SYMBOL);
|
||||
|
||||
if (type_parser)
|
||||
return type_parser;
|
||||
if (token_parser && !token_has_sub_id)
|
||||
return token_parser;
|
||||
if (expr_begin_parser)
|
||||
return expr_begin_parser;
|
||||
if (expr_other_parser)
|
||||
return expr_other_parser;
|
||||
if (expr_parser)
|
||||
return expr_parser;
|
||||
if (type_fallback_parser)
|
||||
return type_fallback_parser;
|
||||
if (token_parser && token_has_sub_id)
|
||||
return token_parser;
|
||||
if (token_fallback_parser)
|
||||
return token_fallback_parser;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct ivy_ast_node *ivy_ast_node_create(enum ivy_ast_node_type type)
|
||||
{
|
||||
return ast_node_create(type);
|
||||
@@ -352,13 +219,12 @@ const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
|
||||
ENUM_STR(IVY_AST_NONE);
|
||||
ENUM_STR(IVY_AST_UNIT);
|
||||
ENUM_STR(IVY_AST_OP);
|
||||
ENUM_STR(IVY_AST_DO);
|
||||
ENUM_STR(IVY_AST_MSG);
|
||||
ENUM_STR(IVY_AST_CLASS);
|
||||
ENUM_STR(IVY_AST_MSGH);
|
||||
ENUM_STR(IVY_AST_PROPERTY);
|
||||
ENUM_STR(IVY_AST_SELECTOR);
|
||||
ENUM_STR(IVY_AST_EXPR);
|
||||
ENUM_STR(IVY_AST_LAMBDA);
|
||||
ENUM_STR(IVY_AST_UNIT_PACKAGE);
|
||||
ENUM_STR(IVY_AST_UNIT_IMPORT);
|
||||
ENUM_STR(IVY_AST_DISCARD);
|
||||
@@ -379,7 +245,6 @@ const char *ivy_ast_node_type_to_string(enum ivy_ast_node_type v)
|
||||
ENUM_STR(IVY_AST_COND);
|
||||
ENUM_STR(IVY_AST_TUPLE);
|
||||
ENUM_STR(IVY_AST_BLOCK);
|
||||
ENUM_STR(IVY_AST_PKG);
|
||||
ENUM_STR(IVY_AST_PKG_STATIC);
|
||||
ENUM_STR(IVY_AST_PKG_ITEM);
|
||||
ENUM_STR(IVY_AST_PKG_DYNAMIC);
|
||||
|
||||
Reference in New Issue
Block a user