164 lines
4.3 KiB
C
164 lines
4.3 KiB
C
#include "block.h"
|
|
|
|
#include "ctx.h"
|
|
#include "expr/expr.h"
|
|
#include "iterate.h"
|
|
#include "node.h"
|
|
|
|
#include <blue/object/string.h>
|
|
#include <ivy/lang/lex.h>
|
|
|
|
void block_add_terminator(struct block_parser_state *state, unsigned short tok)
|
|
{
|
|
if (state->s_nr_terminators < BLOCK_TERMINATOR_MAX) {
|
|
state->s_terminators[state->s_nr_terminators++] = tok;
|
|
}
|
|
}
|
|
|
|
void block_copy_terminators(
|
|
const struct block_parser_state *src, struct block_parser_state *dest)
|
|
{
|
|
dest->s_nr_terminators = src->s_nr_terminators;
|
|
|
|
for (unsigned int i = 0; i < src->s_nr_terminators; i++) {
|
|
dest->s_terminators[i] = src->s_terminators[i];
|
|
}
|
|
}
|
|
|
|
bool block_terminates_at_token(struct block_parser_state *state, unsigned short tok)
|
|
{
|
|
for (unsigned int i = 0; i < BLOCK_TERMINATOR_MAX; i++) {
|
|
if (state->s_terminators[i] == tok) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
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);
|
|
|
|
int flags = 0;
|
|
if (block_terminates_at_token(state, IVY_KW_END)) {
|
|
flags = PARSE_REPEAT_TOKEN;
|
|
}
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, flags);
|
|
}
|
|
|
|
static struct token_parse_result parse_else(
|
|
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, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
static struct token_parse_result parse_keyword(
|
|
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 (block_terminates_at_token(state, tok->t_keyword)) {
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_symbol(
|
|
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 (block_terminates_at_token(state, tok->t_symbol)) {
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
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);
|
|
|
|
struct expr_parser_state *expr
|
|
= (struct expr_parser_state *)parser_push_state(
|
|
ctx, IVY_AST_EXPR, 0);
|
|
|
|
memcpy(expr->s_terminators, state->s_terminators,
|
|
sizeof expr->s_terminators);
|
|
expr->s_nr_terminators = state->s_nr_terminators;
|
|
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
}
|
|
|
|
static enum ivy_status add_child(
|
|
struct parser_state *parent, struct ivy_ast_node *child)
|
|
{
|
|
struct ivy_ast_block_node *block
|
|
= (struct ivy_ast_block_node *)parent->s_node;
|
|
|
|
b_queue_push_back(&block->n_expr, &child->n_entry);
|
|
|
|
return IVY_OK;
|
|
}
|
|
|
|
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_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),
|
|
KW_PARSER(ELSE, parse_else),
|
|
KW_PARSER_FALLBACK(parse_keyword),
|
|
},
|
|
.n_symbol_parsers = {
|
|
SYM_PARSER_FALLBACK(parse_symbol),
|
|
},
|
|
.n_expr_parser = {
|
|
.expr_begin = parse_expr_begin,
|
|
},
|
|
};
|