lang: ast: fix selector to_string not including keyword message labels

This commit is contained in:
2024-12-06 22:25:33 +00:00
parent dc55cfa1f8
commit 68ad0655aa

View File

@@ -12,12 +12,12 @@ struct selector_parser_state {
bool s_complete;
};
#define CHECK_SELECTOR_COMPLETE() \
do { \
if (state->s_complete) { \
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT); \
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); \
} \
#define CHECK_SELECTOR_COMPLETE() \
do { \
if (state->s_complete) { \
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT); \
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN); \
} \
} while (0)
static struct token_parse_result parse_plus(
@@ -92,9 +92,10 @@ static struct token_parse_result parse_label(
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (sel->n_recipient == IVY_SELECTOR_RECIPIENT_NONE && state->s_prev != IVY_TOK_IDENT
&& state->s_prev != IVY_SYM_LEFT_PAREN) {
if (sel->n_recipient == IVY_SELECTOR_RECIPIENT_NONE
&& state->s_prev != IVY_TOK_IDENT
&& state->s_prev != IVY_SYM_LEFT_PAREN) {
/* if recipient is not NONE, this selector appears at the beginning of a message
* handler. only then is a label without a preceding identifier allowed. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
@@ -157,7 +158,7 @@ static struct token_parse_result parse_right_paren(
struct ivy_ast_selector_node *sel
= (struct ivy_ast_selector_node *)state->s_base.s_node;
if (state->s_prev != IVY_TOK_IDENT || b_queue_empty(&sel->n_arg_labels)) {
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
@@ -180,7 +181,8 @@ static struct token_parse_result parse_pipe(
switch (state->s_prev) {
case IVY_SYM_RIGHT_PAREN:
/* this looks like a complex message selector */
if (b_queue_empty(&sel->n_arg_labels) || b_queue_empty(&sel->n_arg_names)) {
if (b_queue_empty(&sel->n_arg_labels)
|| b_queue_empty(&sel->n_arg_names)) {
/* no message args */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
@@ -192,7 +194,8 @@ static struct token_parse_result parse_pipe(
break;
case IVY_TOK_IDENT:
/* this looks like a unary or keyword message. */
if (!b_queue_empty(&sel->n_arg_labels) && b_queue_empty(&sel->n_arg_names)) {
if (!b_queue_empty(&sel->n_arg_labels)
&& b_queue_empty(&sel->n_arg_names)) {
/* keyword message with no arg names. */
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
}
@@ -207,7 +210,7 @@ static struct token_parse_result parse_pipe(
}
static struct token_parse_result parse_other(
struct ivy_parser* ctx, struct ivy_token* tok)
struct ivy_parser *ctx, struct ivy_token *tok)
{
struct selector_parser_state *state
= parser_get_state(ctx, struct selector_parser_state);
@@ -224,7 +227,8 @@ static void to_string(struct ivy_ast_node *node, b_string *str)
{
struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)node;
b_string_append_cstrf(str, "%s [", ivy_ast_node_type_to_string(node->n_type));
b_string_append_cstrf(
str, "%s [", ivy_ast_node_type_to_string(node->n_type));
switch (sel->n_recipient) {
case IVY_SELECTOR_RECIPIENT_CLASS:
@@ -255,22 +259,28 @@ static void to_string(struct ivy_ast_node *node, b_string *str)
b_queue_iterator_begin(&sel->n_arg_names, &name_it);
int i = 0;
while (b_queue_iterator_is_valid(&label_it)
&& b_queue_iterator_is_valid(&name_it)) {
while (b_queue_iterator_is_valid(&label_it)) {
if (i > 0) {
b_string_append_cstr(str, " ");
}
struct ivy_token *label
= b_unbox(struct ivy_token, label_it.entry, t_entry);
struct ivy_token *name
= b_unbox(struct ivy_token, name_it.entry, t_entry);
struct ivy_token *label, *name;
if (label && name) {
b_string_append_cstrf(str, "%s:%s", label->t_str, name->t_str);
label = b_unbox(struct ivy_token, label_it.entry, t_entry);
name = b_unbox(struct ivy_token, name_it.entry, t_entry);
if (label) {
b_string_append_cstrf(str, "%s:", label->t_str);
}
if (name) {
b_string_append_cstrf(str, "%s", name->t_str);
}
if (name) {
i++;
}
i++;
b_queue_iterator_next(&label_it);
b_queue_iterator_next(&name_it);
}