Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject invalid splat as last statement of parentheses #3175

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -18255,21 +18255,37 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
}
}

context_pop(parser);
pm_accepts_block_stack_pop(parser);
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN);

// When we're parsing multi targets, we allow them to be followed by
// a right parenthesis if they are at the statement level. This is
// only possible if they are the final statement in a parentheses.
// We need to explicitly reject that here.
{
const pm_node_t *statement = statements->body.nodes[statements->body.size - 1];
pm_node_t *statement = statements->body.nodes[statements->body.size - 1];

if (PM_NODE_TYPE_P(statement, PM_SPLAT_NODE)) {
pm_multi_target_node_t *multi_target = pm_multi_target_node_create(parser);
pm_multi_target_node_targets_append(parser, multi_target, statement);

statement = (pm_node_t *) multi_target;
statements->body.nodes[statements->body.size - 1] = statement;
}

if (PM_NODE_TYPE_P(statement, PM_MULTI_TARGET_NODE)) {
const uint8_t *offset = statement->location.end;
pm_token_t operator = { .type = PM_TOKEN_EQUAL, .start = offset, .end = offset };
pm_node_t *value = (pm_node_t *) pm_missing_node_create(parser, offset, offset);

statement = (pm_node_t *) pm_multi_write_node_create(parser, (pm_multi_target_node_t *) statement, &operator, value);
statements->body.nodes[statements->body.size - 1] = statement;

pm_parser_err_node(parser, statement, PM_ERR_WRITE_TARGET_UNEXPECTED);
}
}

context_pop(parser);
pm_accepts_block_stack_pop(parser);
expect1(parser, PM_TOKEN_PARENTHESIS_RIGHT, PM_ERR_EXPECT_RPAREN);

pop_block_exits(parser, previous_block_exits);
pm_node_list_free(&current_block_exits);

Expand Down
4 changes: 4 additions & 0 deletions test/prism/errors/invalid_splat.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(1
*a)
^~ unexpected write target

Loading