2024-11-26 13:08:39 +00:00
|
|
|
#include "ctx.h"
|
|
|
|
|
#include "ivy/status.h"
|
|
|
|
|
#include "node.h"
|
|
|
|
|
|
|
|
|
|
#include <blue/object/string.h>
|
|
|
|
|
#include <ivy/lang/lex.h>
|
2024-11-28 16:58:01 +00:00
|
|
|
#include <stdio.h>
|
2024-11-26 13:08:39 +00:00
|
|
|
|
|
|
|
|
struct selector_parser_state {
|
|
|
|
|
struct parser_state s_base;
|
|
|
|
|
unsigned int s_prev;
|
2024-11-26 21:30:40 +00:00
|
|
|
bool s_complete;
|
2024-11-26 13:08:39 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-06 22:25:33 +00:00
|
|
|
#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); \
|
|
|
|
|
} \
|
2024-11-26 21:30:40 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
2024-11-28 16:58:01 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_ident(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
CHECK_SELECTOR_COMPLETE();
|
|
|
|
|
|
2024-11-26 13:08:39 +00:00
|
|
|
if (state->s_prev == 0) {
|
|
|
|
|
/* message name */
|
|
|
|
|
sel->n_msg_name = tok;
|
|
|
|
|
state->s_prev = IVY_TOK_IDENT;
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_complete = true;
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_prev == IVY_TOK_LABEL) {
|
|
|
|
|
/* internal parameter name */
|
|
|
|
|
b_queue_push_back(&sel->n_arg_names, &tok->t_entry);
|
|
|
|
|
state->s_prev = IVY_TOK_IDENT;
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_complete = true;
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_label(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
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;
|
2024-12-06 22:25:33 +00:00
|
|
|
|
|
|
|
|
if (sel->n_recipient == IVY_SELECTOR_RECIPIENT_NONE
|
|
|
|
|
&& state->s_prev != IVY_TOK_IDENT
|
|
|
|
|
&& state->s_prev != IVY_SYM_LEFT_PAREN) {
|
2024-12-06 13:23:02 +00:00
|
|
|
/* 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. */
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_queue_push_back(&sel->n_arg_labels, &tok->t_entry);
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_prev = IVY_TOK_LABEL;
|
|
|
|
|
state->s_complete = false;
|
|
|
|
|
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_linefeed(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
CHECK_SELECTOR_COMPLETE();
|
|
|
|
|
|
2024-11-26 13:08:39 +00:00
|
|
|
if (!b_queue_empty(&sel->n_arg_labels)
|
|
|
|
|
&& state->s_prev != IVY_SYM_RIGHT_PAREN) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state->s_prev != IVY_TOK_IDENT) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_prev = IVY_TOK_LINEFEED;
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_left_paren(
|
2024-11-26 13:08:39 +00:00
|
|
|
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 (state->s_prev != IVY_TOK_IDENT || !b_queue_empty(&sel->n_arg_labels)) {
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_prev = IVY_SYM_LEFT_PAREN;
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_right_paren(
|
2024-11-26 13:08:39 +00:00
|
|
|
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;
|
2024-12-06 22:25:33 +00:00
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
if (state->s_prev != IVY_TOK_IDENT || b_queue_empty(&sel->n_arg_labels)) {
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_prev = IVY_SYM_RIGHT_PAREN;
|
|
|
|
|
state->s_complete = true;
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
|
|
|
|
return PARSE_RESULT(IVY_OK, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
static struct token_parse_result parse_pipe(
|
|
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
2024-12-06 13:23:02 +00:00
|
|
|
switch (state->s_prev) {
|
|
|
|
|
case IVY_SYM_RIGHT_PAREN:
|
|
|
|
|
/* this looks like a complex message selector */
|
2024-12-06 22:25:33 +00:00
|
|
|
if (b_queue_empty(&sel->n_arg_labels)
|
|
|
|
|
|| b_queue_empty(&sel->n_arg_names)) {
|
2024-12-06 13:23:02 +00:00
|
|
|
/* no message args */
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!sel->n_msg_name) {
|
|
|
|
|
/* no message name */
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case IVY_TOK_IDENT:
|
|
|
|
|
/* this looks like a unary or keyword message. */
|
2024-12-06 22:25:33 +00:00
|
|
|
if (!b_queue_empty(&sel->n_arg_labels)
|
|
|
|
|
&& b_queue_empty(&sel->n_arg_names)) {
|
2024-12-06 13:23:02 +00:00
|
|
|
/* keyword message with no arg names. */
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
/* not sure what we're parsing. unknown token at end of selector. */
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parser_pop_state(ctx, STATE_ADD_NODE_TO_PARENT);
|
2024-11-26 21:30:40 +00:00
|
|
|
return PARSE_RESULT(IVY_OK, PARSE_REPEAT_TOKEN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct token_parse_result parse_other(
|
2024-12-06 22:25:33 +00:00
|
|
|
struct ivy_parser *ctx, struct ivy_token *tok)
|
2024-11-26 21:30:40 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
CHECK_SELECTOR_COMPLETE();
|
|
|
|
|
|
|
|
|
|
return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0);
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 20:24:08 +00:00
|
|
|
static void to_string(struct ivy_ast_node *node, b_string *str)
|
2024-11-28 16:58:01 +00:00
|
|
|
{
|
|
|
|
|
struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)node;
|
|
|
|
|
|
2024-12-06 22:25:33 +00:00
|
|
|
b_string_append_cstrf(
|
|
|
|
|
str, "%s [", ivy_ast_node_type_to_string(node->n_type));
|
2024-11-28 16:58:01 +00:00
|
|
|
|
|
|
|
|
switch (sel->n_recipient) {
|
|
|
|
|
case IVY_SELECTOR_RECIPIENT_CLASS:
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, "+");
|
2024-11-28 16:58:01 +00:00
|
|
|
break;
|
|
|
|
|
case IVY_SELECTOR_RECIPIENT_OBJECT:
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, "-");
|
2024-11-28 16:58:01 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
2024-11-28 22:06:25 +00:00
|
|
|
/* this will occur if the selector is being used to send a
|
|
|
|
|
message at runtime, rather than as part of a message
|
|
|
|
|
handler definition. */
|
2024-11-28 16:58:01 +00:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sel->n_msg_name) {
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, sel->n_msg_name->t_str);
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, "(");
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2024-12-06 22:25:33 +00:00
|
|
|
while (b_queue_iterator_is_valid(&label_it)) {
|
2024-11-28 16:58:01 +00:00
|
|
|
if (i > 0) {
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, " ");
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 22:25:33 +00:00
|
|
|
struct ivy_token *label, *name;
|
|
|
|
|
|
|
|
|
|
label = b_unbox(struct ivy_token, label_it.entry, t_entry);
|
|
|
|
|
name = b_unbox(struct ivy_token, name_it.entry, t_entry);
|
|
|
|
|
|
2025-04-23 10:58:22 +01:00
|
|
|
if (label && label->t_type == IVY_TOK_LABEL && label->t_str) {
|
2024-12-06 22:25:33 +00:00
|
|
|
b_string_append_cstrf(str, "%s:", label->t_str);
|
2024-12-07 10:07:20 +00:00
|
|
|
} else {
|
|
|
|
|
b_string_append_cstrf(str, "_:");
|
2024-12-06 22:25:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
|
b_string_append_cstrf(str, "%s", name->t_str);
|
|
|
|
|
}
|
2024-11-28 16:58:01 +00:00
|
|
|
|
2024-12-06 22:25:33 +00:00
|
|
|
if (name) {
|
|
|
|
|
i++;
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b_queue_iterator_next(&label_it);
|
|
|
|
|
b_queue_iterator_next(&name_it);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sel->n_msg_name && !b_queue_empty(&sel->n_arg_labels)) {
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, ")");
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-06 20:24:08 +00:00
|
|
|
b_string_append_cstr(str, "]");
|
2024-11-28 16:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
2024-12-04 16:35:19 +00:00
|
|
|
static void init_state(struct ivy_parser *ctx, struct parser_state *sp, uintptr_t arg)
|
2024-11-26 13:08:39 +00:00
|
|
|
{
|
|
|
|
|
struct selector_parser_state *state = (struct selector_parser_state *)sp;
|
|
|
|
|
state->s_prev = 0;
|
2024-11-26 21:30:40 +00:00
|
|
|
state->s_complete = false;
|
2024-11-26 13:08:39 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:30:40 +00:00
|
|
|
struct ast_node_type selector_node_ops = {
|
2024-11-26 13:08:39 +00:00
|
|
|
.n_init_state = init_state,
|
2024-12-06 20:24:08 +00:00
|
|
|
.n_to_string = to_string,
|
2024-11-26 13:08:39 +00:00
|
|
|
.n_state_size = sizeof(struct selector_parser_state),
|
|
|
|
|
.n_node_size = sizeof(struct ivy_ast_selector_node),
|
|
|
|
|
.n_token_parsers = {
|
2024-11-28 10:56:43 +00:00
|
|
|
TOK_PARSER(IDENT, parse_ident),
|
|
|
|
|
TOK_PARSER(LABEL, parse_label),
|
|
|
|
|
TOK_PARSER_FALLBACK(parse_other),
|
2024-11-26 13:08:39 +00:00
|
|
|
},
|
|
|
|
|
.n_symbol_parsers = {
|
2024-11-28 16:58:01 +00:00
|
|
|
SYM_PARSER(PLUS, parse_plus),
|
|
|
|
|
SYM_PARSER(HYPHEN, parse_hyphen),
|
2024-11-28 10:56:43 +00:00
|
|
|
SYM_PARSER(LEFT_PAREN, parse_left_paren),
|
|
|
|
|
SYM_PARSER(RIGHT_PAREN, parse_right_paren),
|
|
|
|
|
SYM_PARSER(PIPE, parse_pipe),
|
2024-11-26 13:08:39 +00:00
|
|
|
},
|
|
|
|
|
};
|