[ruby/prism] Reset do_loop_stack around a body of a endless method definition

Fix https://github.com/ruby/prism/pull/1772

https://github.com/ruby/prism/commit/cdf58e845e
This commit is contained in:
TSUYUSATO Kitsune 2023-11-09 16:01:37 +09:00 committed by git
parent b73e087dbc
commit fae44d6524
3 changed files with 83 additions and 42 deletions

View File

@ -14326,6 +14326,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
equal = parser->previous;
context_push(parser, PM_CONTEXT_DEF);
pm_do_loop_stack_push(parser, false);
statements = (pm_node_t *) pm_statements_node_create(parser);
pm_node_t *statement = parse_expression(parser, PM_BINDING_POWER_DEFINED + 1, PM_ERR_DEF_ENDLESS);
@ -14338,6 +14339,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power) {
}
pm_statements_node_body_append((pm_statements_node_t *) statements, statement);
pm_do_loop_stack_pop(parser);
context_pop(parser);
end_keyword = not_provided(parser);
} else {

View File

@ -17,3 +17,5 @@ while class Foo a = tap do end; end; break; end
while class << self; tap do end; end; break; end
while class << self; a = tap do end; end; break; end
while def foo = bar do end; end

View File

@ -1,8 +1,8 @@
@ ProgramNode (location: (1,0)-(19,52))
@ ProgramNode (location: (1,0)-(21,31))
├── locals: []
└── statements:
@ StatementsNode (location: (1,0)-(19,52))
└── body: (length: 10)
@ StatementsNode (location: (1,0)-(21,31))
└── body: (length: 11)
├── @ WhileNode (location: (1,0)-(1,18))
│ ├── keyword_loc: (1,0)-(1,5) = "while"
│ ├── closing_loc: (1,15)-(1,18) = "end"
@ -239,46 +239,83 @@
│ │ ├── arguments: ∅
│ │ └── keyword_loc: (17,38)-(17,43) = "break"
│ └── flags: ∅
└── @ WhileNode (location: (19,0)-(19,52))
├── keyword_loc: (19,0)-(19,5) = "while"
├── closing_loc: (19,49)-(19,52) = "end"
├── @ WhileNode (location: (19,0)-(19,52))
│ ├── keyword_loc: (19,0)-(19,5) = "while"
│ ├── closing_loc: (19,49)-(19,52) = "end"
│ ├── predicate:
│ │ @ SingletonClassNode (location: (19,6)-(19,40))
│ │ ├── locals: [:a]
│ │ ├── class_keyword_loc: (19,6)-(19,11) = "class"
│ │ ├── operator_loc: (19,12)-(19,14) = "<<"
│ │ ├── expression:
│ │ │ @ SelfNode (location: (19,15)-(19,19))
│ │ ├── body:
│ │ │ @ StatementsNode (location: (19,21)-(19,35))
│ │ │ └── body: (length: 1)
│ │ │ └── @ LocalVariableWriteNode (location: (19,21)-(19,35))
│ │ │ ├── name: :a
│ │ │ ├── depth: 0
│ │ │ ├── name_loc: (19,21)-(19,22) = "a"
│ │ │ ├── value:
│ │ │ │ @ CallNode (location: (19,25)-(19,35))
│ │ │ │ ├── receiver: ∅
│ │ │ │ ├── call_operator_loc: ∅
│ │ │ │ ├── message_loc: (19,25)-(19,28) = "tap"
│ │ │ │ ├── opening_loc: ∅
│ │ │ │ ├── arguments: ∅
│ │ │ │ ├── closing_loc: ∅
│ │ │ │ ├── block:
│ │ │ │ │ @ BlockNode (location: (19,29)-(19,35))
│ │ │ │ │ ├── locals: []
│ │ │ │ │ ├── parameters: ∅
│ │ │ │ │ ├── body: ∅
│ │ │ │ │ ├── opening_loc: (19,29)-(19,31) = "do"
│ │ │ │ │ └── closing_loc: (19,32)-(19,35) = "end"
│ │ │ │ ├── flags: ∅
│ │ │ │ └── name: :tap
│ │ │ └── operator_loc: (19,23)-(19,24) = "="
│ │ └── end_keyword_loc: (19,37)-(19,40) = "end"
│ ├── statements:
│ │ @ StatementsNode (location: (19,42)-(19,47))
│ │ └── body: (length: 1)
│ │ └── @ BreakNode (location: (19,42)-(19,47))
│ │ ├── arguments: ∅
│ │ └── keyword_loc: (19,42)-(19,47) = "break"
│ └── flags: ∅
└── @ WhileNode (location: (21,0)-(21,31))
├── keyword_loc: (21,0)-(21,5) = "while"
├── closing_loc: (21,28)-(21,31) = "end"
├── predicate:
│ @ SingletonClassNode (location: (19,6)-(19,40))
│ ├── locals: [:a]
│ ├── class_keyword_loc: (19,6)-(19,11) = "class"
│ ├── operator_loc: (19,12)-(19,14) = "<<"
│ ├── expression:
│ │ @ SelfNode (location: (19,15)-(19,19))
│ @ DefNode (location: (21,6)-(21,26))
│ ├── name: :foo
│ ├── name_loc: (21,10)-(21,13) = "foo"
│ ├── receiver: ∅
│ ├── parameters: ∅
│ ├── body:
│ │ @ StatementsNode (location: (19,21)-(19,35))
│ │ @ StatementsNode (location: (21,16)-(21,26))
│ │ └── body: (length: 1)
│ │ └── @ LocalVariableWriteNode (location: (19,21)-(19,35))
│ │ ├── name: :a
│ │ ├── depth: 0
│ │ ├── name_loc: (19,21)-(19,22) = "a"
│ │ ├── value:
│ │ │ @ CallNode (location: (19,25)-(19,35))
│ │ │ ├── receiver: ∅
│ │ │ ├── call_operator_loc: ∅
│ │ │ ├── message_loc: (19,25)-(19,28) = "tap"
│ │ │ ├── opening_loc: ∅
│ │ │ ├── arguments: ∅
│ │ │ ├── closing_loc: ∅
│ │ │ ├── block:
│ │ │ │ @ BlockNode (location: (19,29)-(19,35))
│ │ │ │ ├── locals: []
│ │ │ │ ├── parameters: ∅
│ │ │ │ ├── body: ∅
│ │ │ │ ├── opening_loc: (19,29)-(19,31) = "do"
│ │ │ │ └── closing_loc: (19,32)-(19,35) = "end"
│ │ │ ├── flags: ∅
│ │ │ └── name: :tap
│ │ └── operator_loc: (19,23)-(19,24) = "="
│ └── end_keyword_loc: (19,37)-(19,40) = "end"
├── statements:
│ @ StatementsNode (location: (19,42)-(19,47))
│ └── body: (length: 1)
│ └── @ BreakNode (location: (19,42)-(19,47))
│ ├── arguments: ∅
│ └── keyword_loc: (19,42)-(19,47) = "break"
│ │ └── @ CallNode (location: (21,16)-(21,26))
│ │ ├── receiver: ∅
│ │ ├── call_operator_loc: ∅
│ │ ├── message_loc: (21,16)-(21,19) = "bar"
│ │ ├── opening_loc: ∅
│ │ ├── arguments: ∅
│ │ ├── closing_loc: ∅
│ │ ├── block:
│ │ │ @ BlockNode (location: (21,20)-(21,26))
│ │ │ ├── locals: []
│ │ │ ├── parameters: ∅
│ │ │ ├── body: ∅
│ │ │ ├── opening_loc: (21,20)-(21,22) = "do"
│ │ │ └── closing_loc: (21,23)-(21,26) = "end"
│ │ ├── flags: ∅
│ │ └── name: :bar
│ ├── locals: []
│ ├── def_keyword_loc: (21,6)-(21,9) = "def"
│ ├── operator_loc: ∅
│ ├── lparen_loc: ∅
│ ├── rparen_loc: ∅
│ ├── equal_loc: (21,14)-(21,15) = "="
│ └── end_keyword_loc: ∅
├── statements: ∅
└── flags: ∅