88 lines
2.3 KiB
C
88 lines
2.3 KiB
C
#include "ctx.h"
|
|
#include "expr/expr.h"
|
|
#include "iterate.h"
|
|
#include "node.h"
|
|
|
|
#include <ivy/lang/ast.h>
|
|
|
|
static struct token_parse_result parse_package_keyword(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
parser_push_state(ctx, IVY_AST_UNIT_PACKAGE, 0);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_use_keyword(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
parser_push_state(ctx, IVY_AST_UNIT_IMPORT, 0);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_global_keyword(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
parser_push_state(ctx, IVY_AST_GLOBAL, 0);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_class_keyword(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
parser_push_state(ctx, IVY_AST_CLASS, 0);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_expr_begin(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
parser_push_state(ctx, IVY_AST_EXPR, 0);
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
static struct token_parse_result parse_dot(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static enum ivy_status add_child(
|
|
struct parser_state *parent, struct ivy_ast_node *child)
|
|
{
|
|
struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)parent->s_node;
|
|
b_queue_push_back(&unit->n_children, &child->n_entry);
|
|
return IVY_OK;
|
|
}
|
|
|
|
static void collect_children(
|
|
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
|
{
|
|
struct ivy_ast_unit_node *unit = (struct ivy_ast_unit_node *)node;
|
|
b_queue_entry *entry = b_queue_first(&unit->n_children);
|
|
while (entry) {
|
|
struct ivy_ast_node *child
|
|
= b_unbox(struct ivy_ast_node, entry, n_entry);
|
|
ast_node_iterator_enqueue_node(iterator, node, child);
|
|
entry = b_queue_next(entry);
|
|
}
|
|
}
|
|
|
|
struct ast_node_type unit_node_ops = {
|
|
.n_add_child = add_child,
|
|
.n_collect_children = collect_children,
|
|
.n_state_size = sizeof(struct parser_state),
|
|
.n_node_size = sizeof(struct ivy_ast_unit_node),
|
|
.n_keyword_parsers = {
|
|
KW_PARSER(PACKAGE, parse_package_keyword),
|
|
KW_PARSER(CLASS, parse_class_keyword),
|
|
KW_PARSER(USE, parse_use_keyword),
|
|
KW_PARSER(GLOBAL, parse_global_keyword),
|
|
},
|
|
.n_symbol_parsers = {
|
|
SYM_PARSER(DOT, parse_dot),
|
|
},
|
|
.n_expr_parser = {
|
|
.expr_begin = parse_expr_begin,
|
|
},
|
|
};
|