Skip to content

Commit 7f71527

Browse files
committed
Fix multi target parentheses locations
1 parent d813c30 commit 7f71527

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

src/yarp.c

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11193,28 +11193,27 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1119311193
parser_lex(parser);
1119411194
yp_accepts_block_stack_pop(parser);
1119511195

11196-
// If we have a single statement and are ending on a right parenthesis,
11197-
// then we need to check if this is possibly a multiple target node.
11196+
// If we have a single statement and are ending on a right
11197+
// parenthesis, then we need to check if this is possibly a
11198+
// multiple target node.
1119811199
if (binding_power == YP_BINDING_POWER_STATEMENT && YP_NODE_TYPE_P(statement, YP_MULTI_TARGET_NODE)) {
11199-
yp_node_t *target;
11200-
yp_multi_target_node_t *multi_target = (yp_multi_target_node_t *) statement;
11200+
yp_multi_target_node_t *multi_target;
11201+
if (((yp_multi_target_node_t *) statement)->lparen_loc.start == NULL) {
11202+
multi_target = (yp_multi_target_node_t *) statement;
11203+
} else {
11204+
multi_target = yp_multi_target_node_create(parser);
11205+
yp_multi_target_node_targets_append(multi_target, statement);
11206+
}
1120111207

1120211208
yp_location_t lparen_loc = YP_LOCATION_TOKEN_VALUE(&opening);
1120311209
yp_location_t rparen_loc = YP_LOCATION_TOKEN_VALUE(&parser->previous);
1120411210

11205-
if (multi_target->lparen_loc.start == NULL) {
11206-
multi_target->base.location.start = lparen_loc.start;
11207-
multi_target->base.location.end = rparen_loc.end;
11208-
multi_target->lparen_loc = lparen_loc;
11209-
multi_target->rparen_loc = rparen_loc;
11210-
target = (yp_node_t *) multi_target;
11211-
} else {
11212-
yp_multi_target_node_t *parent_target = yp_multi_target_node_create(parser);
11213-
yp_multi_target_node_targets_append(parent_target, (yp_node_t *) multi_target);
11214-
target = (yp_node_t *) parent_target;
11215-
}
11211+
multi_target->lparen_loc = lparen_loc;
11212+
multi_target->rparen_loc = rparen_loc;
11213+
multi_target->base.location.start = lparen_loc.start;
11214+
multi_target->base.location.end = rparen_loc.end;
1121611215

11217-
return parse_targets(parser, target, YP_BINDING_POWER_INDEX);
11216+
return parse_targets(parser, (yp_node_t *) multi_target, YP_BINDING_POWER_INDEX);
1121811217
}
1121911218

1122011219
// If we have a single statement and are ending on a right parenthesis
@@ -11226,9 +11225,9 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1122611225
return (yp_node_t *) yp_parentheses_node_create(parser, &opening, (yp_node_t *) statements, &parser->previous);
1122711226
}
1122811227

11229-
// If we have more than one statement in the set of parentheses, then we
11230-
// are going to parse all of them as a list of statements. We'll do that
11231-
// here.
11228+
// If we have more than one statement in the set of parentheses,
11229+
// then we are going to parse all of them as a list of statements.
11230+
// We'll do that here.
1123211231
context_push(parser, YP_CONTEXT_PARENS);
1123311232
yp_statements_node_t *statements = yp_statements_node_create(parser);
1123411233
yp_statements_node_body_append(statements, statement);
@@ -11240,11 +11239,11 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) {
1124011239
yp_node_t *node = parse_expression(parser, YP_BINDING_POWER_STATEMENT, YP_ERR_CANNOT_PARSE_EXPRESSION);
1124111240
yp_statements_node_body_append(statements, node);
1124211241

11243-
// If we're recovering from a syntax error, then we need to stop parsing the
11244-
// statements now.
11242+
// If we're recovering from a syntax error, then we need to stop
11243+
// parsing the statements now.
1124511244
if (parser->recovering) {
11246-
// If this is the level of context where the recovery has happened, then
11247-
// we can mark the parser as done recovering.
11245+
// If this is the level of context where the recovery has
11246+
// happened, then we can mark the parser as done recovering.
1124811247
if (match1(parser, YP_TOKEN_PARENTHESIS_RIGHT)) parser->recovering = false;
1124911248
break;
1125011249
}

test/yarp/location_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ def test_MultiTargetNode
556556

557557
def test_MultiWriteNode
558558
assert_location(MultiWriteNode, "foo, bar = baz")
559+
assert_location(MultiWriteNode, "(foo, bar) = baz")
560+
assert_location(MultiWriteNode, "((foo, bar)) = baz")
559561
end
560562

561563
def test_NextNode

test/yarp/snapshots/seattlerb/masgn_double_paren.txt

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/yarp/snapshots/whitequark/masgn_nested.txt

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)