Skip to content

Commit

Permalink
Fix up void value expression checking for rescue
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Sep 25, 2024
1 parent 7753853 commit 509ff88
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
47 changes: 33 additions & 14 deletions src/prism.c
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ pm_parser_optional_constant_id_token(pm_parser_t *parser, const pm_token_t *toke
*/
static pm_node_t *
pm_check_value_expression(pm_parser_t *parser, pm_node_t *node) {
pm_node_t* void_node = NULL;
pm_node_t *void_node = NULL;

while (node != NULL) {
switch (PM_NODE_TYPE(node)) {
Expand All @@ -1050,24 +1050,43 @@ pm_check_value_expression(pm_parser_t *parser, pm_node_t *node) {
case PM_BEGIN_NODE: {
pm_begin_node_t *cast = (pm_begin_node_t *) node;

if (cast->statements == NULL && cast->ensure_clause != NULL) {
node = (pm_node_t *) cast->ensure_clause;
}
else {
if (cast->ensure_clause != NULL) {
if (cast->rescue_clause != NULL) {
if (cast->rescue_clause->statements == NULL) {
return NULL;
}
else if (cast->else_clause != NULL) {
node = (pm_node_t *) cast->else_clause;
pm_node_t *vn = pm_check_value_expression(parser, (pm_node_t *) cast->rescue_clause);
if (vn != NULL) return vn;
}

if (cast->statements != NULL) {
pm_node_t *vn = pm_check_value_expression(parser, (pm_node_t *) cast->statements);
if (vn != NULL) return vn;
}

node = (pm_node_t *) cast->ensure_clause;
} else if (cast->rescue_clause != NULL) {
if (cast->statements == NULL) return NULL;

pm_node_t *vn = pm_check_value_expression(parser, (pm_node_t *) cast->statements);
if (vn == NULL) return NULL;
if (void_node == NULL) void_node = vn;

for (pm_rescue_node_t *rescue_clause = cast->rescue_clause; rescue_clause != NULL; rescue_clause = rescue_clause->subsequent) {
pm_node_t *vn = pm_check_value_expression(parser, (pm_node_t *) rescue_clause->statements);
if (vn == NULL) {
void_node = NULL;
break;
}
else {
node = (pm_node_t *) cast->statements;
if (void_node == NULL) {
void_node = vn;
}
}
else {
node = (pm_node_t *) cast->statements;

if (cast->else_clause != NULL) {
node = (pm_node_t *) cast->else_clause;
} else {
return void_node;
}
} else {
node = (pm_node_t *) cast->statements;
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ x = begin return ensure return end
x = begin return; rescue; return end
^~~~~~ unexpected void value expression
x = begin return; rescue; return; else return end
^~~~~~ unexpected void value expression
^~~~~~ unexpected void value expression
x = begin; return; rescue; retry; end
^~~~~~ unexpected void value expression

0 comments on commit 509ff88

Please sign in to comment.