summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJemma Issroff <jemmaissroff@gmail.com>2023-06-21 12:59:22 -0400
committerTakashi Kokubun <takashikkbn@gmail.com>2023-06-22 09:20:40 -0700
commite8fb84265c897d5e64b1fcb71054ef7eebe13262 (patch)
tree969818ba6caeaa576a1daa03741de3feeea7cdeb
parent7fad7d345acae5a44bc12c1ee98220b83f6f249c (diff)
[ruby/yarp] Allow for block statements after elsif and else
https://github.com/ruby/yarp/commit/4560cab235
-rw-r--r--test/yarp/fixtures/if.txt11
-rw-r--r--test/yarp/snapshots/if.txt114
-rw-r--r--yarp/yarp.c6
3 files changed, 129 insertions, 2 deletions
diff --git a/test/yarp/fixtures/if.txt b/test/yarp/fixtures/if.txt
index 895d668094..c5281a6693 100644
--- a/test/yarp/fixtures/if.txt
+++ b/test/yarp/fixtures/if.txt
@@ -29,3 +29,14 @@ end
if type in 1
elsif type in B
end
+
+if 1
+ lambda do |_|
+ end
+elsif 2
+ lambda do |_|
+ end
+else
+ lambda do |_|
+ end
+end
diff --git a/test/yarp/snapshots/if.txt b/test/yarp/snapshots/if.txt
index f8a697c746..1dc934f73a 100644
--- a/test/yarp/snapshots/if.txt
+++ b/test/yarp/snapshots/if.txt
@@ -1,6 +1,6 @@
-ProgramNode(0...293)(
+ProgramNode(0...382)(
[],
- StatementsNode(0...293)(
+ StatementsNode(0...382)(
[IfNode(0...15)(
(0...2),
TrueNode(3...7)(),
@@ -225,6 +225,116 @@ ProgramNode(0...293)(
nil
),
(290...293)
+ ),
+ IfNode(295...382)(
+ (295...297),
+ IntegerNode(298...299)(),
+ StatementsNode(302...321)(
+ [CallNode(302...321)(
+ nil,
+ nil,
+ (302...308),
+ nil,
+ nil,
+ nil,
+ BlockNode(309...321)(
+ [:_],
+ BlockParametersNode(312...315)(
+ ParametersNode(313...314)(
+ [RequiredParameterNode(313...314)(:_)],
+ [],
+ [],
+ nil,
+ [],
+ nil,
+ nil
+ ),
+ [],
+ (312...313),
+ (314...315)
+ ),
+ nil,
+ (309...311),
+ (318...321)
+ ),
+ 0,
+ "lambda"
+ )]
+ ),
+ IfNode(322...382)(
+ (322...327),
+ IntegerNode(328...329)(),
+ StatementsNode(332...351)(
+ [CallNode(332...351)(
+ nil,
+ nil,
+ (332...338),
+ nil,
+ nil,
+ nil,
+ BlockNode(339...351)(
+ [:_],
+ BlockParametersNode(342...345)(
+ ParametersNode(343...344)(
+ [RequiredParameterNode(343...344)(:_)],
+ [],
+ [],
+ nil,
+ [],
+ nil,
+ nil
+ ),
+ [],
+ (342...343),
+ (344...345)
+ ),
+ nil,
+ (339...341),
+ (348...351)
+ ),
+ 0,
+ "lambda"
+ )]
+ ),
+ ElseNode(352...382)(
+ (352...356),
+ StatementsNode(359...378)(
+ [CallNode(359...378)(
+ nil,
+ nil,
+ (359...365),
+ nil,
+ nil,
+ nil,
+ BlockNode(366...378)(
+ [:_],
+ BlockParametersNode(369...372)(
+ ParametersNode(370...371)(
+ [RequiredParameterNode(370...371)(:_)],
+ [],
+ [],
+ nil,
+ [],
+ nil,
+ nil
+ ),
+ [],
+ (369...370),
+ (371...372)
+ ),
+ nil,
+ (366...368),
+ (375...378)
+ ),
+ 0,
+ "lambda"
+ )]
+ ),
+ (379...382)
+ ),
+ (379...382)
+ ),
+ (379...382)
)]
)
)
diff --git a/yarp/yarp.c b/yarp/yarp.c
index 0475c44836..c4fe886a72 100644
--- a/yarp/yarp.c
+++ b/yarp/yarp.c
@@ -8701,7 +8701,10 @@ parse_conditional(yp_parser_t *parser, yp_context_t context) {
accept_any(parser, 2, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
accept(parser, YP_TOKEN_KEYWORD_THEN);
+ yp_accepts_block_stack_push(parser, true);
yp_statements_node_t *statements = parse_statements(parser, YP_CONTEXT_ELSIF);
+ yp_accepts_block_stack_pop(parser);
+
accept_any(parser, 2, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
yp_node_t *elsif = (yp_node_t *) yp_if_node_create(parser, &elsif_keyword, predicate, statements, NULL, &end_keyword);
@@ -8713,7 +8716,10 @@ parse_conditional(yp_parser_t *parser, yp_context_t context) {
if (match_type_p(parser, YP_TOKEN_KEYWORD_ELSE)) {
parser_lex(parser);
yp_token_t else_keyword = parser->previous;
+
+ yp_accepts_block_stack_push(parser, true);
yp_statements_node_t *else_statements = parse_statements(parser, YP_CONTEXT_ELSE);
+ yp_accepts_block_stack_pop(parser);
accept_any(parser, 2, YP_TOKEN_NEWLINE, YP_TOKEN_SEMICOLON);
expect(parser, YP_TOKEN_KEYWORD_END, "Expected `end` to close `else` clause.");