From f2728c3393d7571f681accd9c887dcd5bad65950 Mon Sep 17 00:00:00 2001 From: yui-knk Date: Thu, 25 Jul 2024 14:57:14 +0900 Subject: [PATCH] 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) ``` --- ast.c | 2 +- compile.c | 6 +++++- node_dump.c | 3 ++- parse.y | 22 +++++++++++----------- rubyparser.h | 1 + 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/ast.c b/ast.c index 54d2ce1e11..338ab00819 100644 --- a/ast.c +++ b/ast.c @@ -447,7 +447,7 @@ node_children(VALUE ast_value, const NODE *node) case NODE_RESCUE: 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); case NODE_RESBODY: - 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); + 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); case NODE_ENSURE: return rb_ary_new_from_node_args(ast_value, 2, RNODE_ENSURE(node)->nd_head, RNODE_ENSURE(node)->nd_ensr); case NODE_AND: diff --git a/compile.c b/compile.c index 3efdc64dd2..f98e3a00ef 100644 --- a/compile.c +++ b/compile.c @@ -8396,7 +8396,11 @@ compile_resbody(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *const node, ADD_LABEL(ret, label_hit); ADD_TRACE(ret, RUBY_EVENT_RESCUE); - if (nd_type(RNODE_RESBODY(resq)->nd_body) == NODE_BEGIN && RNODE_BEGIN(RNODE_RESBODY(resq)->nd_body)->nd_body == NULL) { + if (RNODE_RESBODY(resq)->nd_exc_var) { + CHECK(COMPILE_POPPED(ret, "resbody exc_var", RNODE_RESBODY(resq)->nd_exc_var)); + } + + 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) { // empty body ADD_SYNTHETIC_INSN(ret, nd_line(RNODE_RESBODY(resq)->nd_body), -1, putnil); } diff --git a/node_dump.c b/node_dump.c index 1ca45047ca..45deabb7b9 100644 --- a/node_dump.c +++ b/node_dump.c @@ -397,9 +397,10 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node) case NODE_RESBODY: ANN("rescue clause (cont'd)"); - ANN("format: rescue [nd_args]; [nd_body]; (rescue) [nd_head]"); + ANN("format: rescue [nd_args] (=> [nd_exc_var]); [nd_body]; (rescue) [nd_next]"); ANN("example: begin; foo; rescue; bar; else; baz; end"); F_NODE(nd_args, RNODE_RESBODY, "rescue exceptions"); + F_NODE(nd_exc_var, RNODE_RESBODY, "exception variable"); F_NODE(nd_body, RNODE_RESBODY, "rescue clause"); LAST_NODE; F_NODE(nd_next, RNODE_RESBODY, "next rescue clause"); diff --git a/parse.y b/parse.y index cd76194df8..a68fb63402 100644 --- a/parse.y +++ b/parse.y @@ -1075,7 +1075,7 @@ static rb_node_for_masgn_t *rb_node_for_masgn_new(struct parser_params *p, NODE static rb_node_retry_t *rb_node_retry_new(struct parser_params *p, const YYLTYPE *loc); static rb_node_begin_t *rb_node_begin_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); 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); -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); +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); static rb_node_ensure_t *rb_node_ensure_new(struct parser_params *p, NODE *nd_head, NODE *nd_ensr, const YYLTYPE *loc); static rb_node_and_t *rb_node_and_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc); 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 #define NEW_RETRY(loc) (NODE *)rb_node_retry_new(p,loc) #define NEW_BEGIN(b,loc) (NODE *)rb_node_begin_new(p,b,loc) #define NEW_RESCUE(b,res,e,loc) (NODE *)rb_node_rescue_new(p,b,res,e,loc) -#define NEW_RESBODY(a,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,ex,n,loc) +#define NEW_RESBODY(a,v,ex,n,loc) (NODE *)rb_node_resbody_new(p,a,v,ex,n,loc) #define NEW_ENSURE(b,en,loc) (NODE *)rb_node_ensure_new(p,b,en,loc) #define NEW_AND(f,s,loc) (NODE *)rb_node_and_new(p,f,s,loc) #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, const YYLTYPE *arg_loc, const YYLTYPE *mod_loc, const YYLTYPE *res_loc) { YYLTYPE loc = code_loc_gen(mod_loc, res_loc); - rescue = NEW_RESBODY(0, remove_begin(rescue), 0, &loc); + rescue = NEW_RESBODY(0, 0, remove_begin(rescue), 0, &loc); loc.beg_pos = arg_loc->beg_pos; return NEW_RESCUE(arg, rescue, 0, &loc); } @@ -3204,7 +3204,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem p->ctxt.in_rescue = $3.in_rescue; NODE *resq; YYLTYPE loc = code_loc_gen(&@2, &@4); - resq = NEW_RESBODY(0, remove_begin($4), 0, &loc); + resq = NEW_RESBODY(0, 0, remove_begin($4), 0, &loc); $$ = NEW_RESCUE(remove_begin($1), resq, 0, &@$); /*% ripper: rescue_mod!($:1, $:4) %*/ } @@ -3238,7 +3238,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem { p->ctxt.in_rescue = $3.in_rescue; YYLTYPE loc = code_loc_gen(&@modifier_rescue, &@resbody); - $resbody = NEW_RESBODY(0, remove_begin($resbody), 0, &loc); + $resbody = NEW_RESBODY(0, 0, remove_begin($resbody), 0, &loc); loc.beg_pos = @mrhs_arg.beg_pos; $mrhs_arg = NEW_RESCUE($mrhs_arg, $resbody, 0, &loc); $$ = node_assign(p, (NODE *)$mlhs, $mrhs_arg, $lex_ctxt, &@$); @@ -3343,7 +3343,7 @@ command_rhs : command_call %prec tOP_ASGN p->ctxt.in_rescue = $3.in_rescue; YYLTYPE loc = code_loc_gen(&@2, &@4); value_expr($1); - $$ = NEW_RESCUE($1, NEW_RESBODY(0, remove_begin($4), 0, &loc), 0, &@$); + $$ = NEW_RESCUE($1, NEW_RESBODY(0, 0, remove_begin($4), 0, &loc), 0, &@$); /*% ripper: rescue_mod!($:1, $:4) %*/ } | command_asgn @@ -5960,13 +5960,12 @@ opt_rescue : k_rescue exc_list exc_var then compstmt opt_rescue { - NODE *body = $5; + NODE *err = $3; if ($3) { - NODE *err = NEW_ERRINFO(&@3); + err = NEW_ERRINFO(&@3); err = node_assign(p, $3, err, NO_LEX_CTXT, &@3); - body = block_append(p, err, body); } - $$ = NEW_RESBODY($2, body, $6, &@$); + $$ = NEW_RESBODY($2, $3, $5, $6, &@$); if ($2) { fixpos($$, $2); } @@ -11475,10 +11474,11 @@ rb_node_rescue_new(struct parser_params *p, NODE *nd_head, NODE *nd_resq, NODE * } 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) +rb_node_resbody_new(struct parser_params *p, NODE *nd_args, NODE *nd_exc_var, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc) { rb_node_resbody_t *n = NODE_NEWNODE(NODE_RESBODY, rb_node_resbody_t, loc); n->nd_args = nd_args; + n->nd_exc_var = nd_exc_var; n->nd_body = nd_body; n->nd_next = nd_next; diff --git a/rubyparser.h b/rubyparser.h index b56bb6293e..02c36ea4b7 100644 --- a/rubyparser.h +++ b/rubyparser.h @@ -396,6 +396,7 @@ typedef struct RNode_RESBODY { NODE node; struct RNode *nd_args; + struct RNode *nd_exc_var; struct RNode *nd_body; struct RNode *nd_next; } rb_node_resbody_t;