From 7530dd36a2801dedb44dba07938435fc41f6de56 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Thu, 28 Nov 2024 16:56:25 +0000 Subject: [PATCH] lang: ast: move block parser state to separate header file this allows other parsers to specify what kind of token should end the block. --- lang/ast/block.c | 45 +++++++++++++++++++++++++++++++++++++-------- lang/ast/block.h | 12 ++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 lang/ast/block.h diff --git a/lang/ast/block.c b/lang/ast/block.c index 08575fa..a2704eb 100644 --- a/lang/ast/block.c +++ b/lang/ast/block.c @@ -1,21 +1,17 @@ #include "ctx.h" #include "node.h" +#include "block.h" +#include "iterate.h" #include #include #include -struct block_parser_state { - struct parser_state s_base; - bool s_single_expr; - unsigned int s_terminator; -}; - 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 class_parser_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); @@ -24,11 +20,28 @@ static struct token_parse_result parse_end( 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 class_parser_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); @@ -52,14 +65,30 @@ 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, }, diff --git a/lang/ast/block.h b/lang/ast/block.h new file mode 100644 index 0000000..5952e25 --- /dev/null +++ b/lang/ast/block.h @@ -0,0 +1,12 @@ +#ifndef _AST_BLOCK_H_ +#define _AST_BLOCK_H_ + +#include "ctx.h" + +struct block_parser_state { + struct parser_state s_base; + bool s_single_expr; + unsigned int s_terminator; +}; + +#endif \ No newline at end of file