Skip to content

Commit

Permalink
Merge pull request #2363 from ruby/begin-location
Browse files Browse the repository at this point in the history
Change the location of an implicit begin
  • Loading branch information
kddnewton authored Feb 5, 2024
2 parents ed337ad + d08e140 commit 837af48
Show file tree
Hide file tree
Showing 21 changed files with 47 additions and 62 deletions.
29 changes: 7 additions & 22 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -12377,25 +12377,10 @@ parse_rescues(pm_parser_t *parser, pm_begin_node_t *parent_node, bool def_p) {
}

static inline pm_begin_node_t *
parse_rescues_as_begin(pm_parser_t *parser, pm_statements_node_t *statements, bool def_p) {
parse_rescues_as_begin(pm_parser_t *parser, const uint8_t *start, pm_statements_node_t *statements, bool def_p) {
pm_token_t no_begin_token = not_provided(parser);
pm_begin_node_t *begin_node = pm_begin_node_create(parser, &no_begin_token, statements);
parse_rescues(parser, begin_node, def_p);

// All nodes within a begin node are optional, so we look
// for the earliest possible node that we can use to set
// the BeginNode's start location
const uint8_t *start = begin_node->base.location.start;
if (begin_node->statements) {
start = begin_node->statements->base.location.start;
} else if (begin_node->rescue_clause) {
start = begin_node->rescue_clause->base.location.start;
} else if (begin_node->else_clause) {
start = begin_node->else_clause->base.location.start;
} else if (begin_node->ensure_clause) {
start = begin_node->ensure_clause->base.location.start;
}

begin_node->base.location.start = start;
return begin_node;
}
Expand Down Expand Up @@ -12490,7 +12475,7 @@ parse_block(pm_parser_t *parser) {

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
statements = (pm_node_t *) parse_rescues_as_begin(parser, opening.start, (pm_statements_node_t *) statements, false);
}
}

Expand Down Expand Up @@ -15290,7 +15275,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
statements = (pm_node_t *) parse_rescues_as_begin(parser, class_keyword.start, (pm_statements_node_t *) statements, false);
}

expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_CLASS_TERM);
Expand Down Expand Up @@ -15343,7 +15328,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
statements = (pm_node_t *) parse_rescues_as_begin(parser, class_keyword.start, (pm_statements_node_t *) statements, false);
}

expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_CLASS_TERM);
Expand Down Expand Up @@ -15612,7 +15597,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, true);
statements = (pm_node_t *) parse_rescues_as_begin(parser, def_keyword.start, (pm_statements_node_t *) statements, true);
}

pm_accepts_block_stack_pop(parser);
Expand Down Expand Up @@ -15872,7 +15857,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(statements == NULL || PM_NODE_TYPE_P(statements, PM_STATEMENTS_NODE));
statements = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) statements, false);
statements = (pm_node_t *) parse_rescues_as_begin(parser, module_keyword.start, (pm_statements_node_t *) statements, false);
}

pm_constant_id_list_t locals = parser->current_scope->locals;
Expand Down Expand Up @@ -16605,7 +16590,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b

if (match2(parser, PM_TOKEN_KEYWORD_RESCUE, PM_TOKEN_KEYWORD_ENSURE)) {
assert(body == NULL || PM_NODE_TYPE_P(body, PM_STATEMENTS_NODE));
body = (pm_node_t *) parse_rescues_as_begin(parser, (pm_statements_node_t *) body, false);
body = (pm_node_t *) parse_rescues_as_begin(parser, opening.start, (pm_statements_node_t *) body, false);
}

expect1(parser, PM_TOKEN_KEYWORD_END, PM_ERR_LAMBDA_TERM_END);
Expand Down
4 changes: 2 additions & 2 deletions test/prism/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def test_BeginNode
assert_location(BeginNode, "begin foo; rescue bar\nelse baz end")
assert_location(BeginNode, "begin foo; rescue bar\nelse baz\nensure qux end")

assert_location(BeginNode, "class Foo\nrescue then end", 10..25, &:body)
assert_location(BeginNode, "module Foo\nrescue then end", 11..26, &:body)
assert_location(BeginNode, "class Foo\nrescue then end", 0..25, &:body)
assert_location(BeginNode, "module Foo\nrescue then end", 0..26, &:body)
end

def test_BlockArgumentNode
Expand Down
4 changes: 2 additions & 2 deletions test/prism/snapshots/arrays.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/blocks.txt

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

8 changes: 4 additions & 4 deletions test/prism/snapshots/classes.txt

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

4 changes: 2 additions & 2 deletions test/prism/snapshots/methods.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/modules.txt

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

4 changes: 2 additions & 2 deletions test/prism/snapshots/procs.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/rescue.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/defn_oneliner_rescue.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/defs_oneliner_rescue.txt

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

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/rescue_do_end_no_raise.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/rescue_do_end_raised.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/rescue_do_end_rescued.txt

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

2 changes: 1 addition & 1 deletion test/prism/snapshots/seattlerb/rescue_in_block.txt

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

20 changes: 10 additions & 10 deletions test/prism/snapshots/unparser/corpus/literal/block.txt

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

Loading

0 comments on commit 837af48

Please sign in to comment.