Skip to content

Commit bf43006

Browse files
committed
Check whether the predicate is closed for conditionals
1 parent 62469a5 commit bf43006

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/prism.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9835,12 +9835,10 @@ parse_arguments_list(pm_parser_t *parser, pm_arguments_t *arguments, bool accept
98359835
}
98369836

98379837
static inline pm_node_t *
9838-
parse_conditional(pm_parser_t *parser, pm_context_t context) {
9839-
pm_token_t keyword = parser->previous;
9840-
9838+
parse_predicate(pm_parser_t *parser, pm_binding_power_t binding_power, pm_context_t context) {
98419839
context_push(parser, PM_CONTEXT_PREDICATE);
98429840
pm_diagnostic_id_t error_id = context == PM_CONTEXT_IF ? PM_ERR_CONDITIONAL_IF_PREDICATE : PM_ERR_CONDITIONAL_UNLESS_PREDICATE;
9843-
pm_node_t *predicate = parse_expression(parser, PM_BINDING_POWER_MODIFIER, error_id);
9841+
pm_node_t *predicate = parse_expression(parser, binding_power, error_id);
98449842

98459843
// Predicates are closed by a term, a "then", or a term and then a "then".
98469844
bool predicate_closed = accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON);
@@ -9850,6 +9848,13 @@ parse_conditional(pm_parser_t *parser, pm_context_t context) {
98509848
}
98519849

98529850
context_pop(parser);
9851+
return predicate;
9852+
}
9853+
9854+
static inline pm_node_t *
9855+
parse_conditional(pm_parser_t *parser, pm_context_t context) {
9856+
pm_token_t keyword = parser->previous;
9857+
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER, context);
98539858
pm_statements_node_t *statements = NULL;
98549859

98559860
if (!match3(parser, PM_TOKEN_KEYWORD_ELSIF, PM_TOKEN_KEYWORD_ELSE, PM_TOKEN_KEYWORD_END)) {
@@ -9881,12 +9886,7 @@ parse_conditional(pm_parser_t *parser, pm_context_t context) {
98819886
if (context == PM_CONTEXT_IF) {
98829887
while (accept1(parser, PM_TOKEN_KEYWORD_ELSIF)) {
98839888
pm_token_t elsif_keyword = parser->previous;
9884-
pm_node_t *predicate = parse_expression(parser, PM_BINDING_POWER_MODIFIER, PM_ERR_CONDITIONAL_ELSIF_PREDICATE);
9885-
9886-
// Predicates are closed by a term, a "then", or a term and then a "then".
9887-
accept2(parser, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON);
9888-
accept1(parser, PM_TOKEN_KEYWORD_THEN);
9889-
9889+
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER, PM_CONTEXT_ELSIF);
98909890
pm_accepts_block_stack_push(parser, true);
98919891
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_ELSIF);
98929892
pm_accepts_block_stack_pop(parser);

test/prism/errors_test.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,10 +1317,11 @@ def test_numbered_parameters_in_block_arguments
13171317
end
13181318

13191319
def test_conditional_predicate_closed
1320-
source = "if 0 0; end\nunless 0 0; end"
1320+
source = "if 0 0; elsif 0 0; end\nunless 0 0; end"
13211321
assert_errors expression(source), source, [
13221322
["Expected `then` or `;` or '\n" + "'", 5..6],
1323-
["Expected `then` or `;` or '\n" + "'", 21..22],
1323+
["Expected `then` or `;` or '\n" + "'", 16..17],
1324+
["Expected `then` or `;` or '\n" + "'", 32..33],
13241325
]
13251326
end
13261327

0 commit comments

Comments
 (0)