[ruby/prism] Prevent optional block parameter from accepting certain prefixed values
This commit prevents the following unary operators from being accepted as the value prefix of a block's optional parameter: - `+` - `-` - `~` - `!` For example, `f { |a = +b| }` will now raise a syntax error. https://github.com/ruby/prism/commit/3024bee60c Co-authored-by: Kevin Newton <kddnewton@gmail.com>
This commit is contained in:
parent
aa473489a2
commit
be4589df80
@ -263,6 +263,7 @@ errors:
|
||||
- TERNARY_COLON
|
||||
- TERNARY_EXPRESSION_FALSE
|
||||
- TERNARY_EXPRESSION_TRUE
|
||||
- UNARY_DISALLOWED
|
||||
- UNARY_RECEIVER
|
||||
- UNDEF_ARGUMENT
|
||||
- UNEXPECTED_BLOCK_ARGUMENT
|
||||
|
@ -17355,6 +17355,10 @@ pm_parser_err_prefix(pm_parser_t *parser, pm_diagnostic_id_t diag_id) {
|
||||
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->previous, diag_id, human, parser->previous.start[0]);
|
||||
break;
|
||||
}
|
||||
case PM_ERR_UNARY_DISALLOWED: {
|
||||
PM_PARSER_ERR_TOKEN_FORMAT(parser, parser->current, diag_id, pm_token_type_human(parser->current.type));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
pm_parser_err_previous(parser, diag_id);
|
||||
break;
|
||||
@ -19904,6 +19908,10 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
|
||||
}
|
||||
}
|
||||
case PM_TOKEN_BANG: {
|
||||
if (binding_power > PM_BINDING_POWER_UNARY) {
|
||||
pm_parser_err_prefix(parser, PM_ERR_UNARY_DISALLOWED);
|
||||
}
|
||||
|
||||
parser_lex(parser);
|
||||
|
||||
pm_token_t operator = parser->previous;
|
||||
@ -19914,6 +19922,9 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
|
||||
return (pm_node_t *) node;
|
||||
}
|
||||
case PM_TOKEN_TILDE: {
|
||||
if (binding_power > PM_BINDING_POWER_UNARY) {
|
||||
pm_parser_err_prefix(parser, PM_ERR_UNARY_DISALLOWED);
|
||||
}
|
||||
parser_lex(parser);
|
||||
|
||||
pm_token_t operator = parser->previous;
|
||||
@ -19923,6 +19934,9 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
|
||||
return (pm_node_t *) node;
|
||||
}
|
||||
case PM_TOKEN_UMINUS: {
|
||||
if (binding_power > PM_BINDING_POWER_UNARY) {
|
||||
pm_parser_err_prefix(parser, PM_ERR_UNARY_DISALLOWED);
|
||||
}
|
||||
parser_lex(parser);
|
||||
|
||||
pm_token_t operator = parser->previous;
|
||||
@ -20039,6 +20053,9 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
|
||||
return (pm_node_t *) pm_lambda_node_create(parser, &locals, &operator, &opening, &parser->previous, parameters, body);
|
||||
}
|
||||
case PM_TOKEN_UPLUS: {
|
||||
if (binding_power > PM_BINDING_POWER_UNARY) {
|
||||
pm_parser_err_prefix(parser, PM_ERR_UNARY_DISALLOWED);
|
||||
}
|
||||
parser_lex(parser);
|
||||
|
||||
pm_token_t operator = parser->previous;
|
||||
|
@ -346,6 +346,7 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
|
||||
[PM_ERR_TERNARY_EXPRESSION_TRUE] = { "expected an expression after `?` in the ternary operator", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNDEF_ARGUMENT] = { "invalid argument being passed to `undef`; expected a bare word, constant, or symbol argument", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNARY_RECEIVER] = { "unexpected %s, expected a receiver for unary `%c`", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNARY_DISALLOWED] = { "unexpected %s; unary calls are not allowed in this context", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNEXPECTED_BLOCK_ARGUMENT] = { "block argument should not be given", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNEXPECTED_INDEX_BLOCK] = { "unexpected block arg given in index assignment; blocks are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
|
||||
[PM_ERR_UNEXPECTED_INDEX_KEYWORDS] = { "unexpected keyword arg given in index assignment; keywords are not allowed in index assignment expressions", PM_ERROR_LEVEL_SYNTAX },
|
||||
|
Loading…
x
Reference in New Issue
Block a user