Skip to content

Commit fd217d4

Browse files
ydahmatzbot
authored andcommitted
[ruby/prism] Reject invalid operator after match predicate or after match required
Partially fixes: ruby#3171 ruby/prism@d0d9699c27
1 parent 2ba5987 commit fd217d4

7 files changed

+45
-0
lines changed

prism/prism.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21416,6 +21416,33 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2141621416
case PM_TOKEN_STAR:
2141721417
case PM_TOKEN_STAR_STAR: {
2141821418
parser_lex(parser);
21419+
pm_token_t operator = parser->previous;
21420+
switch (PM_NODE_TYPE(node)) {
21421+
case PM_RESCUE_MODIFIER_NODE: {
21422+
pm_rescue_modifier_node_t *cast = (pm_rescue_modifier_node_t *) node;
21423+
if (PM_NODE_TYPE_P(cast->rescue_expression, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->rescue_expression, PM_MATCH_REQUIRED_NODE)) {
21424+
PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type));
21425+
}
21426+
break;
21427+
}
21428+
case PM_AND_NODE: {
21429+
pm_and_node_t *cast = (pm_and_node_t *) node;
21430+
if (PM_NODE_TYPE_P(cast->right, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->right, PM_MATCH_REQUIRED_NODE)) {
21431+
PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type));
21432+
}
21433+
break;
21434+
}
21435+
case PM_OR_NODE: {
21436+
pm_or_node_t *cast = (pm_or_node_t *) node;
21437+
if (PM_NODE_TYPE_P(cast->right, PM_MATCH_PREDICATE_NODE) || PM_NODE_TYPE_P(cast->right, PM_MATCH_REQUIRED_NODE)) {
21438+
PM_PARSER_ERR_TOKEN_FORMAT(parser, operator, PM_ERR_EXPECT_EOL_AFTER_STATEMENT, pm_token_type_human(operator.type));
21439+
}
21440+
break;
21441+
}
21442+
default:
21443+
break;
21444+
}
21445+
2141921446
pm_node_t *argument = parse_expression(parser, binding_power, false, false, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR, (uint16_t) (depth + 1));
2142021447
return (pm_node_t *) pm_call_node_binary_create(parser, node, &token, argument, 0);
2142121448
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 and 2 in 3 % 4
2+
^ unexpected '%', expecting end-of-input
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 or 2 in 3 + 4
2+
^ unexpected '+', expecting end-of-input
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 rescue 2 in 3 << 4
2+
^~ unexpected <<, expecting end-of-input
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 and 2 => 3 - 4
2+
^ unexpected '-', expecting end-of-input
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 or 2 => 3 ^ 4
2+
^ unexpected '^', expecting end-of-input
3+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1 rescue 2 => 3 ** 4
2+
^~ unexpected '**', expecting end-of-input
3+

0 commit comments

Comments
 (0)