Skip to content

Commit 0fa8d8f

Browse files
kai-matsudatekddnewton
authored andcommitted
Reject modifier conditionals in if/unless predicates
Prism accepted a modifier conditional as the predicate of an `if`/`unless` (and `elsif`), while parse.y rejects it: if a if b then end # parse.y: SyntaxError, prism: accepted unless a unless b then end The `if`/`unless`/`elsif` predicate was parsed with a floor of `PM_BINDING_POWER_MODIFIER`, which is inclusive of the modifier conditionals (`if`/`unless`/`while`/`until`, left binding power `PM_BINDING_POWER_MODIFIER`), so they were absorbed into the predicate. `while`/`until` predicates already use `PM_BINDING_POWER_COMPOSITION` and reject these correctly. Raise the floor to `PM_BINDING_POWER_MODIFIER + 1` so the predicate still absorbs `and`/`or` (and tighter operators) but excludes the modifier conditionals, matching parse.y and aligning `if`/`unless` with `while`/`until`.
1 parent d290a34 commit 0fa8d8f

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/prism.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15486,7 +15486,7 @@ parse_conditional(pm_parser_t *parser, pm_context_t context, size_t opening_newl
1548615486
pm_token_t keyword = parser->previous;
1548715487
pm_token_t then_keyword = { 0 };
1548815488

15489-
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER, context, &then_keyword, (uint16_t) (depth + 1));
15489+
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER + 1, context, &then_keyword, (uint16_t) (depth + 1));
1549015490
pm_statements_node_t *statements = NULL;
1549115491

1549215492
if (!match3(parser, PM_TOKEN_KEYWORD_ELSIF, PM_TOKEN_KEYWORD_ELSE, PM_TOKEN_KEYWORD_END)) {
@@ -15524,7 +15524,7 @@ parse_conditional(pm_parser_t *parser, pm_context_t context, size_t opening_newl
1552415524
pm_token_t elsif_keyword = parser->current;
1552515525
parser_lex(parser);
1552615526

15527-
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER, PM_CONTEXT_ELSIF, &then_keyword, (uint16_t) (depth + 1));
15527+
pm_node_t *predicate = parse_predicate(parser, PM_BINDING_POWER_MODIFIER + 1, PM_CONTEXT_ELSIF, &then_keyword, (uint16_t) (depth + 1));
1552815528
pm_accepts_block_stack_push(parser, true);
1552915529

1553015530
pm_statements_node_t *statements = parse_statements(parser, PM_CONTEXT_ELSIF, (uint16_t) (depth + 1));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
if a if b then end
2+
^~ expected `then` or `;` or '\n'
3+
^~ unexpected 'if', ignoring it
4+
^~~~ unexpected 'then', expecting end-of-input
5+
^~~~ unexpected 'then', ignoring it
6+
7+
unless a unless b then end
8+
^~~~~~ expected `then` or `;` or '\n'
9+
^~~~~~ unexpected 'unless', ignoring it
10+
^~~~ unexpected 'then', expecting end-of-input
11+
^~~~ unexpected 'then', ignoring it
12+

0 commit comments

Comments
 (0)