[ruby/prism] Change the location of an implicit begin to method

https://github.com/ruby/prism/commit/d08e140859
This commit is contained in:
Kevin Newton 2024-02-05 15:04:04 -05:00 committed by git
parent c5694c647a
commit c42b1029d9
21 changed files with 47 additions and 62 deletions

View File

@ -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;
}
@ -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);
}
}
@ -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);
@ -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);
@ -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);
@ -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;
@ -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);

View File

@ -63,8 +63,8 @@ module Prism
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

View File

@ -2157,7 +2157,7 @@
│ │ ├── keyword_rest: ∅
│ │ └── block: ∅
│ ├── body:
│ │ @ BeginNode (location: (140,10)-(140,29))
│ │ @ BeginNode (location: (140,0)-(140,29))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:
@ -2220,7 +2220,7 @@
│ ├── keyword_rest: ∅
│ └── block: ∅
├── body:
│ @ BeginNode (location: (142,10)-(142,32))
│ @ BeginNode (location: (142,0)-(142,32))
│ ├── begin_keyword_loc: ∅
│ ├── statements: ∅
│ ├── rescue_clause:

View File

@ -373,7 +373,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (21,0)-(22,3))
│ │ @ BeginNode (location: (20,4)-(22,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:

View File

@ -33,7 +33,7 @@
│ ├── inheritance_operator_loc: ∅
│ ├── superclass: ∅
│ ├── body:
│ │ @ BeginNode (location: (3,9)-(3,20))
│ │ @ BeginNode (location: (3,0)-(3,20))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause: ∅
@ -55,7 +55,7 @@
│ ├── inheritance_operator_loc: ∅
│ ├── superclass: ∅
│ ├── body:
│ │ @ BeginNode (location: (5,9)-(5,34))
│ │ @ BeginNode (location: (5,0)-(5,34))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:
@ -147,7 +147,7 @@
│ │ ├── expression:
│ │ │ @ SelfNode (location: (14,18)-(14,22))
│ │ ├── body:
│ │ │ @ BeginNode (location: (14,24)-(14,35))
│ │ │ @ BeginNode (location: (14,9)-(14,35))
│ │ │ ├── begin_keyword_loc: ∅
│ │ │ ├── statements: ∅
│ │ │ ├── rescue_clause: ∅
@ -179,7 +179,7 @@
│ │ ├── expression:
│ │ │ @ SelfNode (location: (16,18)-(16,22))
│ │ ├── body:
│ │ │ @ BeginNode (location: (16,24)-(16,49))
│ │ │ @ BeginNode (location: (16,9)-(16,49))
│ │ │ ├── begin_keyword_loc: ∅
│ │ │ ├── statements: ∅
│ │ │ ├── rescue_clause:

View File

@ -95,7 +95,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (8,7)-(8,18))
│ │ @ BeginNode (location: (8,0)-(8,18))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause: ∅
@ -643,7 +643,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (77,7)-(77,32))
│ │ @ BeginNode (location: (77,0)-(77,32))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:

View File

@ -84,7 +84,7 @@
│ │ @ ConstantReadNode (location: (8,7)-(8,8))
│ │ └── name: :A
│ ├── body:
│ │ @ BeginNode (location: (9,1)-(9,19))
│ │ @ BeginNode (location: (8,0)-(9,19))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (9,1)-(9,6))

View File

@ -47,7 +47,7 @@
│ ├── closing_loc: (5,0)-(5,3) = "end"
│ ├── parameters: ∅
│ └── body:
│ @ BeginNode (location: (4,0)-(5,3))
│ @ BeginNode (location: (3,3)-(5,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements: ∅
│ ├── rescue_clause: ∅
@ -65,7 +65,7 @@
│ ├── closing_loc: (11,0)-(11,3) = "end"
│ ├── parameters: ∅
│ └── body:
│ @ BeginNode (location: (8,0)-(11,3))
│ @ BeginNode (location: (7,3)-(11,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements: ∅
│ ├── rescue_clause:

View File

@ -325,7 +325,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (29,2)-(31,3))
│ │ @ BeginNode (location: (28,0)-(31,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (29,2)-(29,6))

View File

@ -20,7 +20,7 @@
│ │ ├── keyword_rest: ∅
│ │ └── block: ∅
│ ├── body:
│ │ @ BeginNode (location: (2,2)-(5,3))
│ │ @ BeginNode (location: (1,0)-(5,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (2,2)-(2,13))

View File

@ -21,7 +21,7 @@
│ │ ├── keyword_rest: ∅
│ │ └── block: ∅
│ ├── body:
│ │ @ BeginNode (location: (2,2)-(5,3))
│ │ @ BeginNode (location: (1,0)-(5,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (2,2)-(2,13))

View File

@ -20,7 +20,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (2,2)-(5,3))
│ │ @ BeginNode (location: (1,5)-(5,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (2,2)-(2,8))

View File

@ -17,7 +17,7 @@
├── locals: []
├── parameters: ∅
├── body:
│ @ BeginNode (location: (2,2)-(9,3))
│ @ BeginNode (location: (1,4)-(9,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements:
│ │ @ StatementsNode (location: (2,2)-(2,8))

View File

@ -17,7 +17,7 @@
├── locals: []
├── parameters: ∅
├── body:
│ @ BeginNode (location: (2,2)-(5,3))
│ @ BeginNode (location: (1,4)-(5,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements:
│ │ @ StatementsNode (location: (2,2)-(2,7))

View File

@ -17,7 +17,7 @@
├── locals: []
├── parameters: ∅
├── body:
│ @ BeginNode (location: (2,2)-(9,3))
│ @ BeginNode (location: (1,4)-(9,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements:
│ │ @ StatementsNode (location: (2,2)-(2,7))

View File

@ -17,7 +17,7 @@
├── locals: []
├── parameters: ∅
├── body:
│ @ BeginNode (location: (2,0)-(4,3))
│ @ BeginNode (location: (1,5)-(4,3))
│ ├── begin_keyword_loc: ∅
│ ├── statements: ∅
│ ├── rescue_clause:

View File

@ -846,7 +846,7 @@
│ ├── locals: [:e]
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (50,0)-(51,3))
│ │ @ BeginNode (location: (49,2)-(51,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:
@ -881,7 +881,7 @@
│ ├── locals: [:bar]
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (53,2)-(56,3))
│ │ @ BeginNode (location: (52,2)-(56,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (53,2)-(53,5))
@ -933,7 +933,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (58,2)-(61,3))
│ │ @ BeginNode (location: (57,2)-(61,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (58,2)-(58,5))
@ -1002,7 +1002,7 @@
│ ├── locals: [:exception]
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (63,2)-(66,3))
│ │ @ BeginNode (location: (62,2)-(66,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (63,2)-(63,5))
@ -1074,7 +1074,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (68,2)-(71,3))
│ │ @ BeginNode (location: (67,2)-(71,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (68,2)-(68,5))
@ -1141,7 +1141,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (73,2)-(75,3))
│ │ @ BeginNode (location: (72,2)-(75,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (73,2)-(73,5))
@ -1185,7 +1185,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (77,2)-(81,3))
│ │ @ BeginNode (location: (76,2)-(81,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (77,2)-(77,5))
@ -1243,7 +1243,7 @@
│ ├── locals: [:exception]
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (83,2)-(86,3))
│ │ @ BeginNode (location: (82,2)-(86,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (83,2)-(83,5))
@ -1313,7 +1313,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (88,0)-(89,3))
│ │ @ BeginNode (location: (87,2)-(89,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause: ∅
@ -1340,7 +1340,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (91,0)-(93,3))
│ │ @ BeginNode (location: (90,2)-(93,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:

View File

@ -9,7 +9,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (2,2)-(9,3))
│ │ @ BeginNode (location: (1,0)-(9,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (2,2)-(2,3))
@ -92,7 +92,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (12,2)-(19,3))
│ │ @ BeginNode (location: (11,0)-(19,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (12,2)-(12,12))
@ -256,7 +256,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (32,2)-(37,3))
│ │ @ BeginNode (location: (31,0)-(37,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (32,2)-(32,5))
@ -323,7 +323,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (40,2)-(43,3))
│ │ @ BeginNode (location: (39,0)-(43,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (40,2)-(40,5))
@ -371,7 +371,7 @@
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (46,2)-(49,3))
│ │ @ BeginNode (location: (45,0)-(49,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements:
│ │ │ @ StatementsNode (location: (46,2)-(46,5))

View File

@ -33,7 +33,7 @@
│ ├── locals: []
│ ├── parameters: ∅
│ ├── body:
│ │ @ BeginNode (location: (5,0)-(6,3))
│ │ @ BeginNode (location: (4,4)-(6,3))
│ │ ├── begin_keyword_loc: ∅
│ │ ├── statements: ∅
│ │ ├── rescue_clause:

View File

@ -10,7 +10,7 @@
├── closing_loc: (1,14)-(1,17) = "end"
├── parameters: ∅
└── body:
@ BeginNode (location: (1,6)-(1,17))
@ BeginNode (location: (1,3)-(1,17))
├── begin_keyword_loc: ∅
├── statements: ∅
├── rescue_clause:

View File

@ -17,7 +17,7 @@
├── locals: []
├── parameters: ∅
├── body:
│ @ BeginNode (location: (1,9)-(1,30))
│ @ BeginNode (location: (1,5)-(1,30))
│ ├── begin_keyword_loc: ∅
│ ├── statements:
│ │ @ StatementsNode (location: (1,9)-(1,12))