lang: ast: add support for multiple block termination tokens
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "block.h"
|
||||
#include "iterate.h"
|
||||
#include "expr/expr.h"
|
||||
#include "iterate.h"
|
||||
|
||||
struct cond_group_parser_state {
|
||||
struct parser_state s_base;
|
||||
@@ -20,9 +20,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_
|
||||
state->s_prev_node = (struct ivy_ast_node *)arg;
|
||||
}
|
||||
|
||||
struct token_parse_result parse_if(
|
||||
struct ivy_parser* ctx,
|
||||
struct ivy_token* tok)
|
||||
struct token_parse_result parse_if(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct cond_group_parser_state *state
|
||||
= parser_get_state(ctx, struct cond_group_parser_state);
|
||||
@@ -31,7 +29,8 @@ struct token_parse_result parse_if(
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
state->s_cur_branch = (struct ivy_ast_cond_node *)ast_node_create(IVY_AST_COND);
|
||||
state->s_cur_branch
|
||||
= (struct ivy_ast_cond_node *)ast_node_create(IVY_AST_COND);
|
||||
if (!state->s_cur_branch) {
|
||||
return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0);
|
||||
}
|
||||
@@ -60,12 +59,12 @@ static enum ivy_status flush_current_branch(struct cond_group_parser_state *stat
|
||||
if (!state->s_cur_branch) {
|
||||
return IVY_ERR_INTERNAL_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
b_queue_push_back(&state->s_branches, &state->s_cur_branch->n_base.n_entry);
|
||||
|
||||
|
||||
state->s_cur_branch
|
||||
= (struct ivy_ast_cond_node *)ast_node_create(IVY_AST_COND);
|
||||
|
||||
|
||||
if (!state->s_cur_branch) {
|
||||
return IVY_ERR_NO_MEMORY;
|
||||
}
|
||||
@@ -73,7 +72,8 @@ static enum ivy_status flush_current_branch(struct cond_group_parser_state *stat
|
||||
return IVY_OK;
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_then(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
static struct token_parse_result parse_then(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct cond_group_parser_state *state
|
||||
= parser_get_state(ctx, struct cond_group_parser_state);
|
||||
@@ -103,13 +103,14 @@ static struct token_parse_result parse_then(struct ivy_parser *ctx, struct ivy_t
|
||||
= (struct block_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_BLOCK, 0);
|
||||
/* set the sub-expression depth to be non-zero so the expression parser doesn't consume the expression separator. */
|
||||
block->s_terminator = IVY_KW_END;
|
||||
block_add_terminator(block, IVY_KW_END);
|
||||
|
||||
state->s_prev_token = IVY_KW_THEN;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_else(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
static struct token_parse_result parse_else(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
enum ivy_status status;
|
||||
struct cond_group_parser_state *state
|
||||
@@ -155,7 +156,7 @@ static struct token_parse_result parse_else(struct ivy_parser *ctx, struct ivy_t
|
||||
if (status != IVY_OK) {
|
||||
return PARSE_RESULT(status, 0);
|
||||
}
|
||||
|
||||
|
||||
/* next component will be an expression. */
|
||||
struct expr_parser_state *expr
|
||||
= (struct expr_parser_state *)parser_push_state(
|
||||
@@ -168,22 +169,26 @@ static struct token_parse_result parse_else(struct ivy_parser *ctx, struct ivy_t
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_expr_begin(struct ivy_parser* ctx, struct ivy_token* tok)
|
||||
static struct token_parse_result parse_expr_begin(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct cond_group_parser_state *state
|
||||
= parser_get_state(ctx, struct cond_group_parser_state);
|
||||
|
||||
if (state->s_prev_token != IVY_KW_ELSE && state->s_prev_token != IVY_KW_THEN) {
|
||||
if (state->s_prev_token != IVY_KW_ELSE
|
||||
&& state->s_prev_token != IVY_KW_THEN) {
|
||||
/* expression can only follow else and then keywords. */
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
struct block_parser_state *block = (struct block_parser_state *)parser_push_state(ctx, IVY_AST_BLOCK, 0);
|
||||
block->s_terminator = IVY_KW_END;
|
||||
struct block_parser_state *block
|
||||
= (struct block_parser_state *)parser_push_state(
|
||||
ctx, IVY_AST_BLOCK, 0);
|
||||
block_add_terminator(block, IVY_KW_END);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static enum ivy_status finalise_cond_group(struct cond_group_parser_state* state)
|
||||
static enum ivy_status finalise_cond_group(struct cond_group_parser_state *state)
|
||||
{
|
||||
struct ivy_ast_cond_group_node *group
|
||||
= (struct ivy_ast_cond_group_node *)state->s_base.s_node;
|
||||
@@ -205,7 +210,7 @@ static enum ivy_status finalise_cond_group(struct cond_group_parser_state* state
|
||||
state->s_cur_branch->n_body = state->s_prev_node;
|
||||
} else if (state->s_prev_token == IVY_KW_IF) {
|
||||
/* this is the if expression, s_prev_node is the if condition. */
|
||||
state->s_cur_branch->n_cond = state->s_prev_node;
|
||||
state->s_cur_branch->n_cond = state->s_prev_node;
|
||||
}
|
||||
|
||||
state->s_prev_node = NULL;
|
||||
@@ -256,7 +261,8 @@ static struct token_parse_result parse_punct_terminator(
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_end(struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
static struct token_parse_result parse_end(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct cond_group_parser_state *state
|
||||
= parser_get_state(ctx, struct cond_group_parser_state);
|
||||
@@ -284,7 +290,7 @@ static enum ivy_status add_child(
|
||||
if (state->s_prev_node) {
|
||||
return IVY_ERR_BAD_SYNTAX;
|
||||
}
|
||||
|
||||
|
||||
state->s_prev_node = child;
|
||||
return IVY_OK;
|
||||
}
|
||||
@@ -297,7 +303,8 @@ static void cond_group_collect_children(
|
||||
|
||||
b_queue_iterator it = {0};
|
||||
b_queue_foreach (&it, &group->n_branches) {
|
||||
struct ivy_ast_node *branch = b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
||||
struct ivy_ast_node *branch
|
||||
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
||||
ast_node_iterator_enqueue_node(iterator, node, branch);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user