From b112e89bb1de4019595647e62405e6b88902383e Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Mon, 21 Aug 2023 18:10:47 -0400 Subject: [PATCH] [ruby/yarp] Add closing_loc to WhileNode https://github.com/ruby/yarp/commit/b4132b876d --- test/yarp/snapshots/method_calls.txt | 1 + .../seattlerb/parse_while_not_canonical.txt | 1 + .../parse_while_not_noncanonical.txt | 1 + .../unparser/corpus/literal/send.txt | 1 + .../unparser/corpus/literal/while.txt | 19 ++++++- .../unparser/corpus/semantic/while.txt | 3 ++ test/yarp/snapshots/while.txt | 10 ++++ .../whitequark/bug_while_not_parens_do.txt | 1 + .../class_definition_in_while_cond.txt | 4 ++ .../if_while_after_class__since_32.txt | 2 + .../method_definition_in_while_cond.txt | 4 ++ test/yarp/snapshots/whitequark/while.txt | 2 + test/yarp/snapshots/whitequark/while_mod.txt | 1 + test/yarp/snapshots/whitequark/while_post.txt | 1 + yarp/config.yml | 2 + yarp/yarp.c | 53 ++++++++++--------- 16 files changed, 81 insertions(+), 25 deletions(-) diff --git a/test/yarp/snapshots/method_calls.txt b/test/yarp/snapshots/method_calls.txt index a6e6709045..cfbb078471 100644 --- a/test/yarp/snapshots/method_calls.txt +++ b/test/yarp/snapshots/method_calls.txt @@ -1369,6 +1369,7 @@ ProgramNode(0...1237)( [SymbolNode(1067...1069)((1067...1068), (1068...1069), nil, "a"), WhileNode(1073...1117)( (1073...1078), + (1114...1117), CallNode(1079...1080)( nil, nil, diff --git a/test/yarp/snapshots/seattlerb/parse_while_not_canonical.txt b/test/yarp/snapshots/seattlerb/parse_while_not_canonical.txt index b113e0ad53..8d152ba76e 100644 --- a/test/yarp/snapshots/seattlerb/parse_while_not_canonical.txt +++ b/test/yarp/snapshots/seattlerb/parse_while_not_canonical.txt @@ -3,6 +3,7 @@ ProgramNode(0...30)( StatementsNode(0...30)( [WhileNode(0...30)( (0...5), + (27...30), CallNode(6...18)( CallNode(10...18)( CallNode(10...13)( diff --git a/test/yarp/snapshots/seattlerb/parse_while_not_noncanonical.txt b/test/yarp/snapshots/seattlerb/parse_while_not_noncanonical.txt index b113e0ad53..8d152ba76e 100644 --- a/test/yarp/snapshots/seattlerb/parse_while_not_noncanonical.txt +++ b/test/yarp/snapshots/seattlerb/parse_while_not_noncanonical.txt @@ -3,6 +3,7 @@ ProgramNode(0...30)( StatementsNode(0...30)( [WhileNode(0...30)( (0...5), + (27...30), CallNode(6...18)( CallNode(10...18)( CallNode(10...13)( diff --git a/test/yarp/snapshots/unparser/corpus/literal/send.txt b/test/yarp/snapshots/unparser/corpus/literal/send.txt index 54262865df..ca65b7d58d 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/send.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/send.txt @@ -308,6 +308,7 @@ ProgramNode(0...999)( CallNode(273...290)( WhileNode(273...286)( (273...278), + (283...286), CallNode(279...282)( nil, nil, diff --git a/test/yarp/snapshots/unparser/corpus/literal/while.txt b/test/yarp/snapshots/unparser/corpus/literal/while.txt index 6807dec9e4..003c03d614 100644 --- a/test/yarp/snapshots/unparser/corpus/literal/while.txt +++ b/test/yarp/snapshots/unparser/corpus/literal/while.txt @@ -32,6 +32,7 @@ ProgramNode(0...620)( StatementsNode(27...60)( [WhileNode(27...60)( (27...32), + (57...60), CallNode(33...36)( nil, nil, @@ -72,6 +73,7 @@ ProgramNode(0...620)( StatementsNode(80...106)( [WhileNode(80...106)( (90...95), + nil, CallNode(96...106)( LocalVariableReadNode(96...99)(:foo, 0), nil, @@ -132,6 +134,7 @@ ProgramNode(0...620)( StatementsNode(123...142)( [WhileNode(123...142)( (133...138), + nil, LocalVariableReadNode(139...142)(:foo, 0), StatementsNode(123...132)( [LocalVariableWriteNode(123...132)( @@ -199,6 +202,7 @@ ProgramNode(0...620)( StatementsNode(195...224)( [WhileNode(195...224)( (195...200), + (221...224), CallNode(201...204)( nil, nil, @@ -266,6 +270,7 @@ ProgramNode(0...620)( StatementsNode(258...291)( [WhileNode(258...291)( (258...263), + (288...291), CallNode(264...267)( nil, nil, @@ -340,6 +345,7 @@ ProgramNode(0...620)( StatementsNode(329...362)( [WhileNode(329...362)( (329...334), + (359...362), LocalVariableReadNode(335...338)(:foo, 0), StatementsNode(345...354)( [LocalVariableWriteNode(345...354)( @@ -380,6 +386,7 @@ ProgramNode(0...620)( StatementsNode(376...401)( [WhileNode(376...401)( (392...397), + nil, CallNode(398...401)( nil, nil, @@ -424,6 +431,7 @@ ProgramNode(0...620)( ), WhileNode(403...428)( (419...424), + nil, CallNode(425...428)( nil, nil, @@ -510,6 +518,7 @@ ProgramNode(0...620)( ), WhileNode(461...492)( (483...488), + nil, CallNode(489...492)( nil, nil, @@ -556,15 +565,23 @@ ProgramNode(0...620)( ), 1 ), - WhileNode(493...508)((493...498), FalseNode(499...504)(), nil, 0), + WhileNode(493...508)( + (493...498), + (505...508), + FalseNode(499...504)(), + nil, + 0 + ), WhileNode(509...528)( (509...514), + (525...528), FalseNode(515...520)(), StatementsNode(523...524)([IntegerNode(523...524)()]), 0 ), WhileNode(529...556)( (529...534), + (553...556), ParenthesesNode(535...544)( StatementsNode(536...543)( [CallNode(536...543)( diff --git a/test/yarp/snapshots/unparser/corpus/semantic/while.txt b/test/yarp/snapshots/unparser/corpus/semantic/while.txt index 171447dc67..c381369c41 100644 --- a/test/yarp/snapshots/unparser/corpus/semantic/while.txt +++ b/test/yarp/snapshots/unparser/corpus/semantic/while.txt @@ -41,6 +41,7 @@ ProgramNode(0...188)( ), WhileNode(36...55)( (46...51), + nil, LocalVariableReadNode(52...55)(:foo, 0), StatementsNode(36...45)( [LocalVariableWriteNode(36...45)( @@ -88,6 +89,7 @@ ProgramNode(0...188)( ), WhileNode(77...96)( (77...82), + (93...96), LocalVariableWriteNode(83...88)( :a, 0, @@ -159,6 +161,7 @@ ProgramNode(0...188)( ), WhileNode(155...184)( (155...160), + (181...184), LocalVariableReadNode(161...164)(:foo, 0), StatementsNode(169...178)( [LocalVariableWriteNode(169...178)( diff --git a/test/yarp/snapshots/while.txt b/test/yarp/snapshots/while.txt index 484eb68b2c..667dd30359 100644 --- a/test/yarp/snapshots/while.txt +++ b/test/yarp/snapshots/while.txt @@ -3,36 +3,42 @@ ProgramNode(0...314)( StatementsNode(0...314)( [WhileNode(0...18)( (0...5), + (15...18), TrueNode(6...10)(), StatementsNode(12...13)([IntegerNode(12...13)()]), 0 ), WhileNode(20...32)( (22...27), + nil, TrueNode(28...32)(), StatementsNode(20...21)([IntegerNode(20...21)()]), 0 ), WhileNode(34...50)( (40...45), + nil, TrueNode(46...50)(), StatementsNode(34...39)([BreakNode(34...39)(nil, (34...39))]), 0 ), WhileNode(52...67)( (57...62), + nil, TrueNode(63...67)(), StatementsNode(52...56)([NextNode(52...56)(nil, (52...56))]), 0 ), WhileNode(69...86)( (76...81), + nil, TrueNode(82...86)(), StatementsNode(69...75)([ReturnNode(69...75)((69...75), nil)]), 0 ), WhileNode(88...109)( (99...104), + nil, CallNode(105...109)( nil, nil, @@ -64,6 +70,7 @@ ProgramNode(0...314)( ), WhileNode(111...161)( (111...116), + (158...161), DefNode(117...149)( (126...129), SelfNode(121...125)(), @@ -105,6 +112,7 @@ ProgramNode(0...314)( ), WhileNode(163...210)( (163...168), + (207...210), ClassNode(169...198)( [:a], (169...174), @@ -138,6 +146,7 @@ ProgramNode(0...314)( ), WhileNode(212...260)( (212...217), + (257...260), SingletonClassNode(218...248)( [], (218...223), @@ -163,6 +172,7 @@ ProgramNode(0...314)( ), WhileNode(262...314)( (262...267), + (311...314), SingletonClassNode(268...302)( [:a], (268...273), diff --git a/test/yarp/snapshots/whitequark/bug_while_not_parens_do.txt b/test/yarp/snapshots/whitequark/bug_while_not_parens_do.txt index 0f517f935c..f5e086059a 100644 --- a/test/yarp/snapshots/whitequark/bug_while_not_parens_do.txt +++ b/test/yarp/snapshots/whitequark/bug_while_not_parens_do.txt @@ -3,6 +3,7 @@ ProgramNode(0...23)( StatementsNode(0...23)( [WhileNode(0...23)( (0...5), + (20...23), CallNode(6...16)( ParenthesesNode(10...16)( StatementsNode(11...15)([TrueNode(11...15)()]), diff --git a/test/yarp/snapshots/whitequark/class_definition_in_while_cond.txt b/test/yarp/snapshots/whitequark/class_definition_in_while_cond.txt index 0af9f40e8a..a3108d7060 100644 --- a/test/yarp/snapshots/whitequark/class_definition_in_while_cond.txt +++ b/test/yarp/snapshots/whitequark/class_definition_in_while_cond.txt @@ -3,6 +3,7 @@ ProgramNode(0...197)( StatementsNode(0...197)( [WhileNode(0...52)( (0...5), + (49...52), SingletonClassNode(6...40)( [:a], (6...11), @@ -34,6 +35,7 @@ ProgramNode(0...197)( ), WhileNode(54...102)( (54...59), + (99...102), SingletonClassNode(60...90)( [], (60...65), @@ -59,6 +61,7 @@ ProgramNode(0...197)( ), WhileNode(104...151)( (104...109), + (148...151), ClassNode(110...139)( [:a], (110...115), @@ -92,6 +95,7 @@ ProgramNode(0...197)( ), WhileNode(153...197)( (153...158), + (194...197), ClassNode(159...185)( [], (159...164), diff --git a/test/yarp/snapshots/whitequark/if_while_after_class__since_32.txt b/test/yarp/snapshots/whitequark/if_while_after_class__since_32.txt index 2096097f5c..1ebb67a457 100644 --- a/test/yarp/snapshots/whitequark/if_while_after_class__since_32.txt +++ b/test/yarp/snapshots/whitequark/if_while_after_class__since_32.txt @@ -27,6 +27,7 @@ ProgramNode(0...178)( ConstantPathNode(46...82)( WhileNode(46...74)( (46...51), + (71...74), TrueNode(52...56)(), StatementsNode(58...70)( [BreakNode(58...70)( @@ -69,6 +70,7 @@ ProgramNode(0...178)( ConstantPathNode(137...173)( WhileNode(137...165)( (137...142), + (162...165), TrueNode(143...147)(), StatementsNode(149...161)( [BreakNode(149...161)( diff --git a/test/yarp/snapshots/whitequark/method_definition_in_while_cond.txt b/test/yarp/snapshots/whitequark/method_definition_in_while_cond.txt index 3b67815c7b..3179d39b95 100644 --- a/test/yarp/snapshots/whitequark/method_definition_in_while_cond.txt +++ b/test/yarp/snapshots/whitequark/method_definition_in_while_cond.txt @@ -3,6 +3,7 @@ ProgramNode(0...190)( StatementsNode(0...190)( [WhileNode(0...45)( (0...5), + (42...45), DefNode(6...33)( (10...13), nil, @@ -44,6 +45,7 @@ ProgramNode(0...190)( ), WhileNode(47...89)( (47...52), + (86...89), DefNode(53...77)( (57...60), nil, @@ -74,6 +76,7 @@ ProgramNode(0...190)( ), WhileNode(91...141)( (91...96), + (138...141), DefNode(97...129)( (106...109), SelfNode(101...105)(), @@ -115,6 +118,7 @@ ProgramNode(0...190)( ), WhileNode(143...190)( (143...148), + (187...190), DefNode(149...178)( (158...161), SelfNode(153...157)(), diff --git a/test/yarp/snapshots/whitequark/while.txt b/test/yarp/snapshots/whitequark/while.txt index 7d95f466c8..7529b6e1bc 100644 --- a/test/yarp/snapshots/whitequark/while.txt +++ b/test/yarp/snapshots/whitequark/while.txt @@ -3,6 +3,7 @@ ProgramNode(0...42)( StatementsNode(0...42)( [WhileNode(0...21)( (0...5), + (18...21), CallNode(6...9)(nil, nil, (6...9), nil, nil, nil, nil, 2, "foo"), StatementsNode(13...17)( [CallNode(13...17)( @@ -21,6 +22,7 @@ ProgramNode(0...42)( ), WhileNode(23...42)( (23...28), + (39...42), CallNode(29...32)(nil, nil, (29...32), nil, nil, nil, nil, 2, "foo"), StatementsNode(34...38)( [CallNode(34...38)( diff --git a/test/yarp/snapshots/whitequark/while_mod.txt b/test/yarp/snapshots/whitequark/while_mod.txt index b1a0203da3..2f0ade7006 100644 --- a/test/yarp/snapshots/whitequark/while_mod.txt +++ b/test/yarp/snapshots/whitequark/while_mod.txt @@ -3,6 +3,7 @@ ProgramNode(0...14)( StatementsNode(0...14)( [WhileNode(0...14)( (5...10), + nil, CallNode(11...14)(nil, nil, (11...14), nil, nil, nil, nil, 2, "foo"), StatementsNode(0...4)( [CallNode(0...4)(nil, nil, (0...4), nil, nil, nil, nil, 2, "meth")] diff --git a/test/yarp/snapshots/whitequark/while_post.txt b/test/yarp/snapshots/whitequark/while_post.txt index 28a423e156..1b2169107a 100644 --- a/test/yarp/snapshots/whitequark/while_post.txt +++ b/test/yarp/snapshots/whitequark/while_post.txt @@ -3,6 +3,7 @@ ProgramNode(0...24)( StatementsNode(0...24)( [WhileNode(0...24)( (15...20), + nil, CallNode(21...24)(nil, nil, (21...24), nil, nil, nil, nil, 2, "foo"), StatementsNode(0...14)( [BeginNode(0...14)( diff --git a/yarp/config.yml b/yarp/config.yml index d9c0891c7a..4cb060d80e 100644 --- a/yarp/config.yml +++ b/yarp/config.yml @@ -2180,6 +2180,8 @@ nodes: child_nodes: - name: keyword_loc type: location + - name: closing_loc + type: location? - name: predicate type: node - name: statements diff --git a/yarp/yarp.c b/yarp/yarp.c index a5981c3d6e..5299ae9401 100644 --- a/yarp/yarp.c +++ b/yarp/yarp.c @@ -4293,34 +4293,43 @@ yp_when_node_statements_set(yp_when_node_t *node, yp_statements_node_t *statemen // Allocate a new WhileNode node. static yp_while_node_t * -yp_while_node_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *predicate, yp_statements_node_t *statements, yp_node_flags_t flags) { +yp_while_node_create(yp_parser_t *parser, const yp_token_t *keyword, const yp_token_t *closing, yp_node_t *predicate, yp_statements_node_t *statements, yp_node_flags_t flags) { yp_while_node_t *node = YP_ALLOC_NODE(parser, yp_while_node_t); - const char *start = NULL; - bool has_statements = (statements != NULL) && (statements->body.size != 0); - if (has_statements && (keyword->start > statements->base.location.start)) { - start = statements->base.location.start; - } else { - start = keyword->start; - } - - const char *end = NULL; - if (has_statements && (predicate->location.end < statements->base.location.end)) { - end = statements->base.location.end; - } else { - end = predicate->location.end; - } - *node = (yp_while_node_t) { { .type = YP_NODE_WHILE_NODE, .flags = flags, .location = { - .start = start, - .end = end, + .start = keyword->start, + .end = closing->end }, }, .keyword_loc = YP_LOCATION_TOKEN_VALUE(keyword), + .closing_loc = YP_OPTIONAL_LOCATION_TOKEN_VALUE(closing), + .predicate = predicate, + .statements = statements + }; + + return node; +} + +// Allocate a new WhileNode node. +static yp_while_node_t * +yp_while_node_modifier_create(yp_parser_t *parser, const yp_token_t *keyword, yp_node_t *predicate, yp_statements_node_t *statements, yp_node_flags_t flags) { + yp_while_node_t *node = YP_ALLOC_NODE(parser, yp_while_node_t); + + *node = (yp_while_node_t) { + { + .type = YP_NODE_WHILE_NODE, + .flags = flags, + .location = { + .start = statements->base.location.start, + .end = predicate->location.end + }, + }, + .keyword_loc = YP_LOCATION_TOKEN_VALUE(keyword), + .closing_loc = YP_OPTIONAL_LOCATION_NOT_PROVIDED_VALUE, .predicate = predicate, .statements = statements }; @@ -11912,11 +11921,7 @@ parse_expression_prefix(yp_parser_t *parser, yp_binding_power_t binding_power) { expect(parser, YP_TOKEN_KEYWORD_END, "Expected `end` to close `while` statement."); } - yp_while_node_t *while_node = yp_while_node_create(parser, &keyword, predicate, statements, 0); - if (parser->previous.type == YP_TOKEN_KEYWORD_END) { - while_node->base.location.end = parser->previous.end; - } - return (yp_node_t *) while_node; + return (yp_node_t *) yp_while_node_create(parser, &keyword, &parser->previous, predicate, statements, 0); } case YP_TOKEN_PERCENT_LOWER_I: { parser_lex(parser); @@ -13197,7 +13202,7 @@ parse_expression_infix(yp_parser_t *parser, yp_node_t *node, yp_binding_power_t yp_statements_node_body_append(statements, node); yp_node_t *predicate = parse_expression(parser, binding_power, "Expected a predicate after 'while'"); - return (yp_node_t *) yp_while_node_create(parser, &token, predicate, statements, YP_NODE_TYPE_P(node, YP_NODE_BEGIN_NODE) ? YP_LOOP_FLAGS_BEGIN_MODIFIER : 0); + return (yp_node_t *) yp_while_node_modifier_create(parser, &token, predicate, statements, YP_NODE_TYPE_P(node, YP_NODE_BEGIN_NODE) ? YP_LOOP_FLAGS_BEGIN_MODIFIER : 0); } case YP_TOKEN_QUESTION_MARK: { parser_lex(parser);