lang: ast: add support for multiple block termination tokens
This commit is contained in:
@@ -8,6 +8,34 @@
|
||||
#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)
|
||||
{
|
||||
@@ -18,7 +46,7 @@ static struct token_parse_result parse_end(
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
int flags = 0;
|
||||
if (state->s_terminator == IVY_KW_END) {
|
||||
if (block_terminates_at_token(state, IVY_KW_END)) {
|
||||
flags = PARSE_REPEAT_TOKEN;
|
||||
}
|
||||
|
||||
@@ -39,6 +67,23 @@ static struct token_parse_result parse_else(
|
||||
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)
|
||||
{
|
||||
@@ -48,7 +93,7 @@ static struct token_parse_result parse_symbol(
|
||||
struct ivy_ast_block_node *node
|
||||
= (struct ivy_ast_block_node *)(state->s_base.s_node);
|
||||
|
||||
if (state->s_terminator == tok->t_symbol) {
|
||||
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);
|
||||
}
|
||||
@@ -69,6 +114,10 @@ static struct token_parse_result parse_expr_begin(
|
||||
= (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);
|
||||
}
|
||||
|
||||
@@ -103,6 +152,7 @@ struct ast_node_type block_node_ops = {
|
||||
.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),
|
||||
|
||||
Reference in New Issue
Block a user