96 lines
2.5 KiB
C
96 lines
2.5 KiB
C
#include "ctx.h"
|
|
#include "node.h"
|
|
#include "block.h"
|
|
#include "iterate.h"
|
|
|
|
#include <blue/object/string.h>
|
|
#include <ivy/lang/lex.h>
|
|
#include <stdio.h>
|
|
|
|
static struct token_parse_result parse_end(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
struct block_parser_state *state
|
|
= parser_get_state(ctx, struct block_parser_state);
|
|
|
|
struct ivy_ast_block_node *node
|
|
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_bang(
|
|
struct ivy_parser* ctx, struct ivy_token* tok)
|
|
{
|
|
struct block_parser_state *state
|
|
= parser_get_state(ctx, struct block_parser_state);
|
|
|
|
struct ivy_ast_block_node *node
|
|
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
|
|
|
if (state->s_terminator != IVY_SYM_BANG) {
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
static struct token_parse_result parse_expr_begin(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
struct block_parser_state *state
|
|
= parser_get_state(ctx, struct block_parser_state);
|
|
|
|
struct ivy_ast_block_node *node
|
|
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
|
|
|
parser_push_state(ctx, IVY_AST_EXPR);
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
static enum ivy_status add_child(
|
|
struct ivy_ast_node *parent, struct ivy_ast_node *child)
|
|
{
|
|
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)parent;
|
|
|
|
b_queue_push_back(&block->n_expr, &child->n_entry);
|
|
|
|
return IVY_OK;
|
|
}
|
|
|
|
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
|
{
|
|
struct class_parser_state *state = (struct class_parser_state *)sp;
|
|
}
|
|
|
|
static void collect_children(
|
|
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
|
{
|
|
struct ivy_ast_block_node *block = (struct ivy_ast_block_node *)node;
|
|
b_queue_iterator it = {0};
|
|
b_queue_foreach(&it, &block->n_expr) {
|
|
struct ivy_ast_node *expr
|
|
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
|
ast_node_iterator_enqueue_node(iterator, node, expr);
|
|
}
|
|
}
|
|
|
|
struct ast_node_type block_node_ops = {
|
|
.n_add_child = add_child,
|
|
.n_init_state = init_state,
|
|
.n_collect_children = collect_children,
|
|
.n_state_size = sizeof(struct block_parser_state),
|
|
.n_node_size = sizeof(struct ivy_ast_block_node),
|
|
.n_keyword_parsers = {
|
|
KW_PARSER(END, parse_end),
|
|
},
|
|
.n_symbol_parsers = {
|
|
SYM_PARSER(BANG, parse_bang),
|
|
},
|
|
.n_expr_parser = {
|
|
.expr_begin = parse_expr_begin,
|
|
},
|
|
};
|