Skip to content

Commit f2728c3

Browse files
committed
Change RESBODY Node structure
Extracrt exception variable into `nd_exc_var` field to keep the original grammar structure. For example: ``` begin rescue Error => e1 end ``` Before: ``` @ NODE_RESBODY (id: 8, line: 2, location: (2,0)-(2,18)) +- nd_args: | @ NODE_LIST (id: 2, line: 2, location: (2,7)-(2,12)) | +- as.nd_alen: 1 | +- nd_head: | | @ NODE_CONST (id: 1, line: 2, location: (2,7)-(2,12)) | | +- nd_vid: :Error | +- nd_next: | (null node) +- nd_body: | @ NODE_BLOCK (id: 6, line: 2, location: (2,13)-(2,18)) | +- nd_head (1): | | @ NODE_LASGN (id: 3, line: 2, location: (2,13)-(2,18)) | | +- nd_vid: :e1 | | +- nd_value: | | @ NODE_ERRINFO (id: 5, line: 2, location: (2,13)-(2,18)) | +- nd_head (2): | @ NODE_BEGIN (id: 4, line: 2, location: (2,18)-(2,18)) | +- nd_body: | (null node) +- nd_next: (null node) ``` After: ``` @ NODE_RESBODY (id: 6, line: 2, location: (2,0)-(2,18)) +- nd_args: | @ NODE_LIST (id: 2, line: 2, location: (2,7)-(2,12)) | +- as.nd_alen: 1 | +- nd_head: | | @ NODE_CONST (id: 1, line: 2, location: (2,7)-(2,12)) | | +- nd_vid: :Error | +- nd_next: | (null node) +- nd_exc_var: | @ NODE_LASGN (id: 3, line: 2, location: (2,13)-(2,18)) | +- nd_vid: :e1 | +- nd_value: | @ NODE_ERRINFO (id: 5, line: 2, location: (2,13)-(2,18)) +- nd_body: | @ NODE_BEGIN (id: 4, line: 2, location: (2,18)-(2,18)) | +- nd_body: | (null node) +- nd_next: (null node) ```
1 parent a4563be commit f2728c3

File tree

5 files changed

+20
-14
lines changed

5 files changed

+20
-14
lines changed

ast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ node_children(VALUE ast_value, const NODE *node)
447447
case NODE_RESCUE:
448448
return rb_ary_new_from_node_args(ast_value, 3, RNODE_RESCUE(node)->nd_head, RNODE_RESCUE(node)->nd_resq, RNODE_RESCUE(node)->nd_else);
449449
case NODE_RESBODY:
450-
return rb_ary_new_from_node_args(ast_value, 3, RNODE_RESBODY(node)->nd_args, RNODE_RESBODY(node)->nd_body, RNODE_RESBODY(node)->nd_next);
450+
return rb_ary_new_from_node_args(ast_value, 4, RNODE_RESBODY(node)->nd_args, RNODE_RESBODY(node)->nd_exc_var, RNODE_RESBODY(node)->nd_body, RNODE_RESBODY(node)->nd_next);
451451
case NODE_ENSURE:
452452
return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr);
453453
case NODE_AND:

compile.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8396,7 +8396,11 @@ compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node,
83968396
ADD_LABEL(ret, label_hit);
83978397
ADD_TRACE(ret, RUBY_EVENT_RESCUE);
83988398

8399-
if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL) {
8399+
if (RNODE_RESBODY(resq)->nd_exc_var) {
8400+
CHECK(COMPILE_POPPED(ret, "resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var));
8401+
}
8402+
8403+
if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL && !RNODE_RESBODY(resq)->nd_exc_var) {
84008404
// empty body
84018405
ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil);
84028406
}

node_dump.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,9 +397,10 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
397397

398398
case NODE_RESBODY:
399399
ANN("rescue clause (cont'd)");
400-
ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]");
400+
ANN("format: rescue [nd_args] (=> [nd_exc_var]); [nd_body]; (rescue) [nd_next]");
401401
ANN("example: begin; foo; rescue; bar; else; baz; end");
402402
F_NODE(nd_args, RNODE_RESBODY, "rescue exceptions");
403+
F_NODE(nd_exc_var, RNODE_RESBODY, "exception variable");
403404
F_NODE(nd_body, RNODE_RESBODY, "rescue clause");
404405
LAST_NODE;
405406
F_NODE(nd_next, RNODE_RESBODY, "next rescue clause");

parse.y

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE
10751075
static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc);
10761076
static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
10771077
static rb_node_rescue_t *rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *nd_else, const YYLTYPE *loc);
1078-
static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1078+
static rb_node_resbody_t *rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
10791079
static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc);
10801080
static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
10811081
static rb_node_or_t *rb_node_or_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
@@ -1183,7 +1183,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
11831183
#define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc)
11841184
#define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc)
11851185
#define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc)
1186-
#define NEW_RESBODY(a,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,ex,n,loc)
1186+
#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc)
11871187
#define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc)
11881188
#define NEW_AND(f,s,loc) (NODE *)rb_node_and_new(p,f,s,loc)
11891189
#define NEW_OR(f,s,loc) (NODE *)rb_node_or_new(p,f,s,loc)
@@ -1645,7 +1645,7 @@ rescued_expr(struct parser_params *p, NODE *arg, NODE *rescue,
16451645
const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc)
16461646
{
16471647
YYLTYPE loc = code_loc_gen(mod_loc, res_loc);
1648-
rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc);
1648+
rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc);
16491649
loc.beg_pos = arg_loc->beg_pos;
16501650
return NEW_RESCUE(arg, rescue, 0, &loc);
16511651
}
@@ -3204,7 +3204,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
32043204
p->ctxt.in_rescue = $3.in_rescue;
32053205
NODE *resq;
32063206
YYLTYPE loc = code_loc_gen(&@2, &@4);
3207-
resq = NEW_RESBODY(0, remove_begin($4), 0, &loc);
3207+
resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc);
32083208
$$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$);
32093209
/*% ripper: rescue_mod!($:1, $:4) %*/
32103210
}
@@ -3238,7 +3238,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
32383238
{
32393239
p->ctxt.in_rescue = $3.in_rescue;
32403240
YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody);
3241-
$resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc);
3241+
$resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc);
32423242
loc.beg_pos = @mrhs_arg.beg_pos;
32433243
$mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc);
32443244
$$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$);
@@ -3343,7 +3343,7 @@ command_rhs : command_call %prec tOP_ASGN
33433343
p->ctxt.in_rescue = $3.in_rescue;
33443344
YYLTYPE loc = code_loc_gen(&@2, &@4);
33453345
value_expr($1);
3346-
$$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$);
3346+
$$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$);
33473347
/*% ripper: rescue_mod!($:1, $:4) %*/
33483348
}
33493349
| command_asgn
@@ -5960,13 +5960,12 @@ opt_rescue : k_rescue exc_list exc_var then
59605960
compstmt
59615961
opt_rescue
59625962
{
5963-
NODE *body = $5;
5963+
NODE *err = $3;
59645964
if ($3) {
5965-
NODE *err = NEW_ERRINFO(&@3);
5965+
err = NEW_ERRINFO(&@3);
59665966
err = node_assign(p, $3, err, NO_LEX_CTXT, &@3);
5967-
body = block_append(p, err, body);
59685967
}
5969-
$$ = NEW_RESBODY($2, body, $6, &@$);
5968+
$$ = NEW_RESBODY($2, $3, $5, $6, &@$);
59705969
if ($2) {
59715970
fixpos($$, $2);
59725971
}
@@ -11475,10 +11474,11 @@ rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE *
1147511474
}
1147611475

1147711476
static rb_node_resbody_t *
11478-
rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11477+
rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
1147911478
{
1148011479
rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc);
1148111480
n->nd_args = nd_args;
11481+
n->nd_exc_var = nd_exc_var;
1148211482
n->nd_body = nd_body;
1148311483
n->nd_next = nd_next;
1148411484

rubyparser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ typedef struct RNode_RESBODY {
396396
NODE node;
397397

398398
struct RNode *nd_args;
399+
struct RNode *nd_exc_var;
399400
struct RNode *nd_body;
400401
struct RNode *nd_next;
401402
} rb_node_resbody_t;

0 commit comments

Comments
 (0)