lang: ast: move recipient tracking from msg handler node to selector node
This commit is contained in:
@@ -41,9 +41,8 @@ static struct token_parse_result parse_plus(
|
||||
struct parser_state *msgh_state = parser_get_state_generic(ctx);
|
||||
struct ivy_ast_msgh_node *msgh
|
||||
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
|
||||
msgh->n_recipient = IVY_AST_MSGH_CLASS;
|
||||
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_hyphen(
|
||||
@@ -60,9 +59,8 @@ static struct token_parse_result parse_hyphen(
|
||||
struct parser_state *msgh_state = parser_get_state_generic(ctx);
|
||||
struct ivy_ast_msgh_node *msgh
|
||||
= (struct ivy_ast_msgh_node *)msgh_state->s_node;
|
||||
msgh->n_recipient = IVY_AST_MSGH_OBJECT;
|
||||
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_ident(
|
||||
@@ -157,10 +155,29 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
||||
state->s_current_area = AREA_IDENT;
|
||||
}
|
||||
|
||||
static void collect_children(
|
||||
struct ivy_ast_node *node, struct ivy_ast_node_iterator *iterator)
|
||||
{
|
||||
struct ivy_ast_class_node *c = (struct ivy_ast_class_node *)node;
|
||||
b_queue_iterator it = {0};
|
||||
b_queue_foreach (&it, &c->n_properties) {
|
||||
struct ivy_ast_node *child
|
||||
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
||||
ast_node_iterator_enqueue_node(iterator, node, child);
|
||||
}
|
||||
|
||||
b_queue_foreach (&it, &c->n_msg_handlers) {
|
||||
struct ivy_ast_node *child
|
||||
= b_unbox(struct ivy_ast_node, it.entry, n_entry);
|
||||
ast_node_iterator_enqueue_node(iterator, node, child);
|
||||
}
|
||||
}
|
||||
|
||||
struct ast_node_type class_node_ops = {
|
||||
.n_add_child = add_child,
|
||||
.n_print = print,
|
||||
.n_init_state = init_state,
|
||||
.n_collect_children = collect_children,
|
||||
.n_state_size = sizeof(struct class_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_class_node),
|
||||
.n_symbol_parsers = {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <blue/object/string.h>
|
||||
#include <ivy/lang/lex.h>
|
||||
#include <stdio.h>
|
||||
|
||||
struct selector_parser_state {
|
||||
struct parser_state s_base;
|
||||
@@ -19,6 +20,40 @@ struct selector_parser_state {
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
static struct token_parse_result parse_plus(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state
|
||||
= parser_get_state(ctx, struct selector_parser_state);
|
||||
|
||||
struct ivy_ast_selector_node *sel
|
||||
= (struct ivy_ast_selector_node *)state->s_base.s_node;
|
||||
|
||||
if (sel->n_recipient != IVY_SELECTOR_RECIPIENT_NONE) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
sel->n_recipient = IVY_SELECTOR_RECIPIENT_CLASS;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_hyphen(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
struct selector_parser_state *state
|
||||
= parser_get_state(ctx, struct selector_parser_state);
|
||||
|
||||
struct ivy_ast_selector_node *sel
|
||||
= (struct ivy_ast_selector_node *)state->s_base.s_node;
|
||||
|
||||
if (sel->n_recipient != IVY_SELECTOR_RECIPIENT_NONE) {
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
sel->n_recipient = IVY_SELECTOR_RECIPIENT_OBJECT;
|
||||
return PARSE_RESULT(IVY_OK, 0);
|
||||
}
|
||||
|
||||
static struct token_parse_result parse_ident(
|
||||
struct ivy_parser *ctx, struct ivy_token *tok)
|
||||
{
|
||||
@@ -166,6 +201,66 @@ static struct token_parse_result parse_other(
|
||||
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
||||
}
|
||||
|
||||
static void print(struct ivy_ast_node *node)
|
||||
{
|
||||
struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)node;
|
||||
|
||||
printf("%s [", ivy_ast_node_type_to_string(node->n_type));
|
||||
|
||||
switch (sel->n_recipient) {
|
||||
case IVY_SELECTOR_RECIPIENT_CLASS:
|
||||
fputc('+', stdout);
|
||||
break;
|
||||
case IVY_SELECTOR_RECIPIENT_OBJECT:
|
||||
fputc('-', stdout);
|
||||
break;
|
||||
default:
|
||||
fputc('?', stdout);
|
||||
break;
|
||||
}
|
||||
|
||||
if (sel->n_msg_name) {
|
||||
printf("%s", sel->n_msg_name->t_str);
|
||||
}
|
||||
|
||||
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
|
||||
fputc('(', stdout);
|
||||
}
|
||||
|
||||
b_queue_iterator label_it = {0};
|
||||
b_queue_iterator name_it = {0};
|
||||
|
||||
b_queue_iterator_begin(&sel->n_arg_labels, &label_it);
|
||||
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)) {
|
||||
if (i > 0) {
|
||||
fputc(' ', stdout);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (label && name) {
|
||||
printf("%s:%s", label->t_str, name->t_str);
|
||||
}
|
||||
|
||||
i++;
|
||||
b_queue_iterator_next(&label_it);
|
||||
b_queue_iterator_next(&name_it);
|
||||
}
|
||||
|
||||
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
|
||||
fputc(')', stdout);
|
||||
}
|
||||
|
||||
fputs("]\n", stdout);
|
||||
}
|
||||
|
||||
static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
||||
{
|
||||
struct selector_parser_state *state = (struct selector_parser_state *)sp;
|
||||
@@ -175,6 +270,7 @@ static void init_state(struct ivy_parser *ctx, struct parser_state *sp)
|
||||
|
||||
struct ast_node_type selector_node_ops = {
|
||||
.n_init_state = init_state,
|
||||
.n_print = print,
|
||||
.n_state_size = sizeof(struct selector_parser_state),
|
||||
.n_node_size = sizeof(struct ivy_ast_selector_node),
|
||||
.n_token_parsers = {
|
||||
@@ -183,6 +279,8 @@ struct ast_node_type selector_node_ops = {
|
||||
TOK_PARSER_FALLBACK(parse_other),
|
||||
},
|
||||
.n_symbol_parsers = {
|
||||
SYM_PARSER(PLUS, parse_plus),
|
||||
SYM_PARSER(HYPHEN, parse_hyphen),
|
||||
SYM_PARSER(LEFT_PAREN, parse_left_paren),
|
||||
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
|
||||
SYM_PARSER(PIPE, parse_pipe),
|
||||
|
||||
Reference in New Issue
Block a user