[ruby/prism] Combine expression checks into a single switch

https://github.com/ruby/prism/commit/825d5d7bd4
This commit is contained in:
Kevin Newton 2023-11-22 09:32:45 -05:00 committed by git
parent cdd07781b0
commit 2aefbbaab9

View File

@ -16768,23 +16768,26 @@ parse_expression(pm_parser_t *parser, pm_binding_power_t binding_power, pm_diagn
pm_token_t recovery = parser->previous; pm_token_t recovery = parser->previous;
pm_node_t *node = parse_expression_prefix(parser, binding_power); pm_node_t *node = parse_expression_prefix(parser, binding_power);
switch (PM_NODE_TYPE(node)) {
case PM_MISSING_NODE:
// If we found a syntax error, then the type of node returned by // If we found a syntax error, then the type of node returned by
// parse_expression_prefix is going to be a missing node. In that case we need // parse_expression_prefix is going to be a missing node. In that
// to add the error message to the parser's error list. // case we need to add the error message to the parser's error list.
if (PM_NODE_TYPE_P(node, PM_MISSING_NODE)) {
pm_parser_err(parser, recovery.end, recovery.end, diag_id); pm_parser_err(parser, recovery.end, recovery.end, diag_id);
return node; return node;
} case PM_PRE_EXECUTION_NODE:
case PM_POST_EXECUTION_NODE:
// The statements BEGIN { ... }, END { ... }, alias ..., and undef ... are statement. case PM_ALIAS_GLOBAL_VARIABLE_NODE:
// They cannot follow operators, but they can follow modifiers. case PM_ALIAS_METHOD_NODE:
bool is_statement = case PM_UNDEF_NODE:
PM_NODE_TYPE_P(node, PM_PRE_EXECUTION_NODE) || PM_NODE_TYPE_P(node, PM_POST_EXECUTION_NODE) || // These expressions are statements, and cannot be followed by
PM_NODE_TYPE_P(node, PM_ALIAS_GLOBAL_VARIABLE_NODE) || PM_NODE_TYPE_P(node, PM_ALIAS_METHOD_NODE) || // operators (except modifiers).
PM_NODE_TYPE_P(node, PM_UNDEF_NODE); if (pm_binding_powers[parser->current.type].left > PM_BINDING_POWER_MODIFIER_RESCUE) {
if (is_statement && pm_binding_powers[parser->current.type].left > PM_BINDING_POWER_MODIFIER_RESCUE) {
return node; return node;
} }
default:
break;
}
// Otherwise we'll look and see if the next token can be parsed as an infix // Otherwise we'll look and see if the next token can be parsed as an infix
// operator. If it can, then we'll parse it using parse_expression_infix. // operator. If it can, then we'll parse it using parse_expression_infix.