stringifying selectors with unnamed args no longer causes a segfault, and unit-package and unit-import node strings are now formatted correctly.
108 lines
2.6 KiB
C
108 lines
2.6 KiB
C
#include "ctx.h"
|
|
#include "node.h"
|
|
|
|
#include <blue/object/string.h>
|
|
#include <ivy/lang/lex.h>
|
|
#include <stdio.h>
|
|
|
|
struct unit_import_parser_state {
|
|
struct parser_state s_base;
|
|
int s_prev_token;
|
|
};
|
|
|
|
static struct token_parse_result parse_dot(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
struct unit_import_parser_state *state
|
|
= parser_get_state(ctx, struct unit_import_parser_state);
|
|
|
|
if (state->s_prev_token != IVY_TOK_IDENT) {
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
state->s_prev_token = IVY_SYM_DOT;
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_ident(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
struct unit_import_parser_state *state
|
|
= parser_get_state(ctx, struct unit_import_parser_state);
|
|
|
|
struct ivy_ast_unit_import_node *node
|
|
= (struct ivy_ast_unit_import_node *)(state->s_base.s_node);
|
|
|
|
if (state->s_prev_token == IVY_TOK_IDENT) {
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
b_queue_push_back(&node->n_ident, &tok->t_entry);
|
|
|
|
state->s_prev_token = IVY_TOK_IDENT;
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static struct token_parse_result parse_linefeed(
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
|
{
|
|
struct unit_import_parser_state *state
|
|
= parser_get_state(ctx, struct unit_import_parser_state);
|
|
|
|
if (state->s_prev_token != IVY_TOK_IDENT) {
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
}
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
return PARSE_RESULT(IVY_OK, 0);
|
|
}
|
|
|
|
static void to_string(struct ivy_ast_node *node, b_string *str)
|
|
{
|
|
struct ivy_ast_unit_import_node *unit_import
|
|
= (struct ivy_ast_unit_import_node *)node;
|
|
|
|
b_string_append_cstr(str, ivy_ast_node_type_to_string(node->n_type));
|
|
|
|
b_string_append_cstr(str, " (");
|
|
|
|
int i = 0;
|
|
|
|
b_queue_iterator it = {0};
|
|
b_queue_foreach (&it, &unit_import->n_ident) {
|
|
struct ivy_token *tok
|
|
= b_unbox(struct ivy_token, it.entry, t_entry);
|
|
|
|
if (i > 0) {
|
|
b_string_append_cstr(str, ".");
|
|
}
|
|
|
|
b_string_append_cstr(str, tok->t_str);
|
|
i++;
|
|
}
|
|
|
|
b_string_append_cstr(str, ")");
|
|
}
|
|
|
|
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
|
|
{
|
|
struct unit_import_parser_state *state
|
|
= (struct unit_import_parser_state *)sp;
|
|
state->s_prev_token = IVY_KW_PACKAGE;
|
|
}
|
|
|
|
struct ast_node_type unit_import_node_ops = {
|
|
.n_to_string = to_string,
|
|
.n_init_state = init_state,
|
|
.n_state_size = sizeof(struct unit_import_parser_state),
|
|
.n_node_size = sizeof(struct ivy_ast_unit_import_node),
|
|
.n_symbol_parsers = {
|
|
SYM_PARSER(DOT, parse_dot),
|
|
},
|
|
.n_token_parsers = {
|
|
TOK_PARSER(IDENT, parse_ident),
|
|
TOK_PARSER(LINEFEED, parse_linefeed),
|
|
}
|
|
|
|
};
|