lang: ast: move block parser state to separate header file
this allows other parsers to specify what kind of token should end the block.
This commit is contained in:
@@ -1,21 +1,17 @@
|
||||
#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>
|
||||
|
||||
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,
|
||||
},
|
||||
|
||||
12
lang/ast/block.h
Normal file
12
lang/ast/block.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user