Skip to content

Commit

Permalink
Forward parameters into arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Nov 1, 2023
1 parent fcc9298 commit 2a11bfe
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 23 deletions.
20 changes: 15 additions & 5 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -12443,7 +12443,8 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
bool parsed_bare_hash = false;

while (!match2(parser, PM_TOKEN_BRACKET_RIGHT, PM_TOKEN_EOF)) {
// Handle the case where we don't have a comma and we have a newline followed by a right bracket.
// Handle the case where we don't have a comma and we have a
// newline followed by a right bracket.
if (accept1(parser, PM_TOKEN_NEWLINE) && match1(parser, PM_TOKEN_BRACKET_RIGHT)) {
break;
}
Expand All @@ -12452,16 +12453,25 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
expect1(parser, PM_TOKEN_COMMA, PM_ERR_ARRAY_SEPARATOR);
}

// If we have a right bracket immediately following a comma, this is
// allowed since it's a trailing comma. In this case we can break out of
// the loop.
// If we have a right bracket immediately following a comma,
// this is allowed since it's a trailing comma. In this case we
// can break out of the loop.
if (match1(parser, PM_TOKEN_BRACKET_RIGHT)) break;

pm_node_t *element;

if (accept1(parser, PM_TOKEN_USTAR)) {
pm_token_t operator = parser->previous;
pm_node_t *expression = parse_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION_AFTER_STAR);
pm_node_t *expression = NULL;

if (match3(parser, PM_TOKEN_BRACKET_RIGHT, PM_TOKEN_COMMA, PM_TOKEN_EOF)) {
if (pm_parser_local_depth(parser, &parser->previous) == -1) {
pm_parser_err_token(parser, &operator, PM_ERR_ARGUMENT_NO_FORWARDING_STAR);
}
} else {
expression = parse_expression(parser, PM_BINDING_POWER_DEFINED, PM_ERR_ARRAY_EXPRESSION_AFTER_STAR);
}

element = (pm_node_t *) pm_splat_node_create(parser, &operator, expression);
} else if (match2(parser, PM_TOKEN_LABEL, PM_TOKEN_USTAR_STAR)) {
if (parsed_bare_hash) {
Expand Down
2 changes: 2 additions & 0 deletions test/prism/fixtures/methods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,5 @@ end

foo = 1
def foo.bar; end

def f(*); [*]; end
70 changes: 52 additions & 18 deletions test/prism/snapshots/methods.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2a11bfe

Please sign in to comment.