From 7ba0a05332410286f79c2c55598ab198e12f94f6 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 6 Dec 2024 13:23:02 +0000 Subject: [PATCH] lang: ast: fix error when parsing message handler keyword-message selector --- lang/ast/selector.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lang/ast/selector.c b/lang/ast/selector.c index f80a59e..596d56d 100644 --- a/lang/ast/selector.c +++ b/lang/ast/selector.c @@ -93,7 +93,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 (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); } @@ -174,12 +177,28 @@ static struct token_parse_result parse_pipe( struct ivy_ast_selector_node *sel = (struct ivy_ast_selector_node *)state->s_base.s_node; - if (!b_queue_empty(&sel->n_arg_labels) - && state->s_prev != IVY_SYM_RIGHT_PAREN) { - return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); - } + 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)) { + /* no message args */ + return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); + } - if (state->s_prev != IVY_TOK_IDENT) { + 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. */ + 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); + } + break; + default: + /* not sure what we're parsing. unknown token at end of selector. */ return PARSE_RESULT(IVY_ERR_BAD_SYNTAX, 0); }