Skip to content

Commit

Permalink
Merge pull request #3445 from ruby/rescue-modifier
Browse files Browse the repository at this point in the history
Fix rescue modifier precedence
  • Loading branch information
kddnewton authored Jan 22, 2025
2 parents 0d0f26c + 0720200 commit 2e9bf5e
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -12975,7 +12975,7 @@ typedef struct {

pm_binding_powers_t pm_binding_powers[PM_TOKEN_MAXIMUM] = {
// rescue
[PM_TOKEN_KEYWORD_RESCUE_MODIFIER] = LEFT_ASSOCIATIVE(PM_BINDING_POWER_MODIFIER_RESCUE),
[PM_TOKEN_KEYWORD_RESCUE_MODIFIER] = { PM_BINDING_POWER_MODIFIER_RESCUE, PM_BINDING_POWER_COMPOSITION, true, false },

// if unless until while
[PM_TOKEN_KEYWORD_IF_MODIFIER] = LEFT_ASSOCIATIVE(PM_BINDING_POWER_MODIFIER),
Expand Down Expand Up @@ -19480,7 +19480,7 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
context_push(parser, PM_CONTEXT_RESCUE_MODIFIER);

pm_token_t rescue_keyword = parser->previous;
pm_node_t *value = parse_expression(parser, binding_power, false, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
pm_node_t *value = parse_expression(parser, pm_binding_powers[PM_TOKEN_KEYWORD_RESCUE_MODIFIER].right, false, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
context_pop(parser);

statement = (pm_node_t *) pm_rescue_modifier_node_create(parser, statement, &rescue_keyword, value);
Expand Down Expand Up @@ -20701,7 +20701,7 @@ parse_assignment_value(pm_parser_t *parser, pm_binding_power_t previous_binding_
pm_token_t rescue = parser->current;
parser_lex(parser);

pm_node_t *right = parse_expression(parser, binding_power, false, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
pm_node_t *right = parse_expression(parser, pm_binding_powers[PM_TOKEN_KEYWORD_RESCUE_MODIFIER].right, false, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
context_pop(parser);

return (pm_node_t *) pm_rescue_modifier_node_create(parser, value, &rescue, right);
Expand Down Expand Up @@ -20807,7 +20807,7 @@ parse_assignment_values(pm_parser_t *parser, pm_binding_power_t previous_binding
}
}

pm_node_t *right = parse_expression(parser, binding_power, accepts_command_call_inner, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
pm_node_t *right = parse_expression(parser, pm_binding_powers[PM_TOKEN_KEYWORD_RESCUE_MODIFIER].right, accepts_command_call_inner, false, PM_ERR_RESCUE_MODIFIER_VALUE, (uint16_t) (depth + 1));
context_pop(parser);

return (pm_node_t *) pm_rescue_modifier_node_create(parser, value, &rescue, right);
Expand Down
7 changes: 7 additions & 0 deletions test/prism/fixtures/rescue_modifier.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a rescue b if c

a = b rescue c if d

a, = b rescue c if d

def a = b rescue c if d
230 changes: 230 additions & 0 deletions test/prism/snapshots/rescue_modifier.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2e9bf5e

Please sign in to comment.