Skip to content

Commit 57038f1

Browse files
committed
Reject assigning command call without parentheses
Fixes: #3106
1 parent 3167699 commit 57038f1

9 files changed

+57
-0
lines changed

src/prism.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20895,6 +20895,23 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *
2089520895
}
2089620896
}
2089720897

20898+
static void
20899+
validate_local_variable_assignment_with_call(pm_parser_t *parser, pm_node_t *node) {
20900+
if (PM_NODE_TYPE_P(node, PM_LOCAL_VARIABLE_WRITE_NODE)) {
20901+
pm_local_variable_write_node_t* cast = (pm_local_variable_write_node_t *)node;
20902+
if (PM_NODE_TYPE_P(cast->value, PM_CALL_NODE)) {
20903+
pm_call_node_t *call_node = (pm_call_node_t *)cast->value;
20904+
if (call_node->arguments != NULL) {
20905+
if (call_node->opening_loc.start == NULL) {
20906+
pm_node_t *arguments = (pm_node_t *)call_node->arguments;
20907+
PM_PARSER_ERR_NODE_FORMAT_CONTENT(parser, arguments, PM_ERR_LAMBDA_OPEN);
20908+
PM_PARSER_ERR_NODE_FORMAT_CONTENT(parser, arguments, PM_ERR_EXPECT_LPAREN_REQ_PARAMETER);
20909+
}
20910+
}
20911+
}
20912+
}
20913+
}
20914+
2089820915
static inline pm_node_t *
2089920916
parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t previous_binding_power, pm_binding_power_t binding_power, bool accepts_command_call, uint16_t depth) {
2090020917
pm_token_t token = parser->current;
@@ -21317,13 +21334,17 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2131721334
parser_lex(parser);
2131821335

2131921336
pm_node_t *right = parse_expression(parser, binding_power, parser->previous.type == PM_TOKEN_KEYWORD_AND, false, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR, (uint16_t) (depth + 1));
21337+
validate_local_variable_assignment_with_call(parser, node);
21338+
validate_local_variable_assignment_with_call(parser, right);
2132021339
return (pm_node_t *) pm_and_node_create(parser, node, &token, right);
2132121340
}
2132221341
case PM_TOKEN_KEYWORD_OR:
2132321342
case PM_TOKEN_PIPE_PIPE: {
2132421343
parser_lex(parser);
2132521344

2132621345
pm_node_t *right = parse_expression(parser, binding_power, parser->previous.type == PM_TOKEN_KEYWORD_OR, false, PM_ERR_EXPECT_EXPRESSION_AFTER_OPERATOR, (uint16_t) (depth + 1));
21346+
validate_local_variable_assignment_with_call(parser, node);
21347+
validate_local_variable_assignment_with_call(parser, right);
2132721348
return (pm_node_t *) pm_or_node_create(parser, node, &token, right);
2132821349
}
2132921350
case PM_TOKEN_EQUAL_TILDE: {
@@ -21542,13 +21563,17 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
2154221563
parser_lex(parser);
2154321564

2154421565
pm_node_t *predicate = parse_value_expression(parser, binding_power, true, false, PM_ERR_CONDITIONAL_IF_PREDICATE, (uint16_t) (depth + 1));
21566+
validate_local_variable_assignment_with_call(parser, node);
21567+
validate_local_variable_assignment_with_call(parser, predicate);
2154521568
return (pm_node_t *) pm_if_node_modifier_create(parser, node, &keyword, predicate);
2154621569
}
2154721570
case PM_TOKEN_KEYWORD_UNLESS_MODIFIER: {
2154821571
pm_token_t keyword = parser->current;
2154921572
parser_lex(parser);
2155021573

2155121574
pm_node_t *predicate = parse_value_expression(parser, binding_power, true, false, PM_ERR_CONDITIONAL_UNLESS_PREDICATE, (uint16_t) (depth + 1));
21575+
validate_local_variable_assignment_with_call(parser, node);
21576+
validate_local_variable_assignment_with_call(parser, predicate);
2155221577
return (pm_node_t *) pm_unless_node_modifier_create(parser, node, &keyword, predicate);
2155321578
}
2155421579
case PM_TOKEN_KEYWORD_UNTIL_MODIFIER: {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 and a = p 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 if a = p 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 or a = p 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1 unless a = p 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = p 1 and 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = p 1 if true
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = p 1 unless true
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
a = p 1 or 2
2+
^ expected a `do` keyword or a `{` to open the lambda block
3+
^ expected a `(` to start a required parameter
4+

0 commit comments

Comments
 (0)