#include "ctx.h" #include "expr/expr.h" #include "iterate.h" #include "node.h" #include static void to_string(struct ivy_ast_node *node, b_string *str) { struct ivy_ast_string_node *s = (struct ivy_ast_string_node *)node; b_string_append_cstrf( str, "%s (\"%s\")", ivy_ast_node_type_to_string(node->n_type), s->n_value->t_str); } struct token_parse_result parse_string(struct ivy_parser *ctx, struct ivy_token *tok) { struct parser_state *state = parser_get_state_generic(ctx); struct ivy_ast_fstring_node *parent = (struct ivy_ast_fstring_node *)state->s_node; struct ivy_ast_string_node *child = (struct ivy_ast_string_node *)ast_node_create(IVY_AST_STRING); if (!child) { return PARSE_RESULT(IVY_ERR_NO_MEMORY, 0); } child->n_value = tok; b_queue_push_back(&parent->n_value, &child->n_base.n_entry); return PARSE_RESULT(IVY_OK, 0); } struct token_parse_result parse_left_brace( struct ivy_parser *ctx, struct ivy_token *tok) { struct expr_parser_state *expr = (struct expr_parser_state *)parser_push_state( ctx, IVY_AST_EXPR, 0); expr_add_terminator(expr, IVY_SYM_RIGHT_BRACE); return PARSE_RESULT(IVY_OK, 0); } struct token_parse_result parse_right_brace( struct ivy_parser *ctx, struct ivy_token *tok) { return PARSE_RESULT(IVY_OK, 0); } struct token_parse_result parse_str_end( struct ivy_parser *ctx, struct ivy_token *tok) { parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT); return PARSE_RESULT(IVY_OK, 0); } static enum ivy_status add_child( struct parser_state *parent, struct ivy_ast_node *child) { struct ivy_ast_fstring_node *str = (struct ivy_ast_fstring_node *)parent->s_node; b_queue_push_back(&str->n_value, &child->n_entry); return IVY_OK; } static void collect_children( struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator) { struct ivy_ast_fstring_node *str = (struct ivy_ast_fstring_node *)node; b_queue_entry *entry = b_queue_first(&str->n_value); while (entry) { struct ivy_ast_node *child = b_unbox(struct ivy_ast_node, entry, n_entry); ast_node_iterator_enqueue_node(iterator, node, child); entry = b_queue_next(entry); } } struct ast_node_type string_node_ops = { .n_to_string = to_string, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_string_node), }; struct ast_node_type fstring_node_ops = { .n_add_child = add_child, .n_collect_children = collect_children, .n_state_size = sizeof(struct parser_state), .n_node_size = sizeof(struct ivy_ast_fstring_node), .n_token_parsers = { TOK_PARSER(STRING, parse_string), TOK_PARSER(STR_END, parse_str_end), }, .n_symbol_parsers = { SYM_PARSER(LEFT_BRACE, parse_left_brace), SYM_PARSER(RIGHT_BRACE, parse_right_brace), }, };