Skip to content

Commit

Permalink
[ruby/yarp] Allow for block statements after elsif and else
Browse files Browse the repository at this point in the history
  • Loading branch information
jemmaissroff authored and k0kubun committed Jun 22, 2023
1 parent 7fad7d3 commit e8fb842
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 2 deletions.
11 changes: 11 additions & 0 deletions test/yarp/fixtures/if.txt
Expand Up @@ -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
114 changes: 112 additions & 2 deletions 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)(),
Expand Down Expand Up @@ -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)
)]
)
)
6 changes: 6 additions & 0 deletions yarp/yarp.c
Expand Up @@ -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);
Expand All @@ -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.");
Expand Down

0 comments on commit e8fb842

Please sign in to comment.