Implement VALIAS NODE keyword locations

This commit is contained in:
ydah 2024-09-03 16:20:52 +09:00 committed by Yuichiro Kaneko
parent 37d7ae06af
commit ab18b1b4f5
Notes: git 2024-09-04 05:36:53 +00:00
5 changed files with 20 additions and 5 deletions

4
ast.c
View File

@ -789,6 +789,10 @@ node_locations(VALUE ast_value, const NODE *node)
location_new(&RNODE_UNLESS(node)->keyword_loc), location_new(&RNODE_UNLESS(node)->keyword_loc),
location_new(&RNODE_UNLESS(node)->then_keyword_loc), location_new(&RNODE_UNLESS(node)->then_keyword_loc),
location_new(&RNODE_UNLESS(node)->end_keyword_loc)); location_new(&RNODE_UNLESS(node)->end_keyword_loc));
case NODE_VALIAS:
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),
location_new(&RNODE_VALIAS(node)->keyword_loc));
case NODE_ARGS_AUX: case NODE_ARGS_AUX:
case NODE_LAST: case NODE_LAST:
break; break;

View File

@ -933,6 +933,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("example: alias $y $x"); ANN("example: alias $y $x");
F_ID(nd_alias, RNODE_VALIAS, "new name"); F_ID(nd_alias, RNODE_VALIAS, "new name");
F_ID(nd_orig, RNODE_VALIAS, "old name"); F_ID(nd_orig, RNODE_VALIAS, "old name");
F_LOC(keyword_loc, RNODE_VALIAS);
return; return;
case NODE_UNDEF: case NODE_UNDEF:

11
parse.y
View File

@ -1139,7 +1139,7 @@ static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NOD
static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); static rb_node_defn_t *rb_node_defn_new(struct parser_params *p, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc); static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_defn, const YYLTYPE *loc);
static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc); static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc); static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc); static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc); static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *nd_super, const YYLTYPE *loc);
static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc); static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
@ -1247,7 +1247,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
#define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc) #define NEW_DEFN(i,s,loc) (NODE *)rb_node_defn_new(p,i,s,loc)
#define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc) #define NEW_DEFS(r,i,s,loc) (NODE *)rb_node_defs_new(p,r,i,s,loc)
#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc) #define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
#define NEW_VALIAS(n,o,loc) (NODE *)rb_node_valias_new(p,n,o,loc) #define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc) #define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc) #define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc) #define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
@ -3133,7 +3133,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
} }
| keyword_alias tGVAR tGVAR | keyword_alias tGVAR tGVAR
{ {
$$ = NEW_VALIAS($2, $3, &@$); $$ = NEW_VALIAS($2, $3, &@$, &@1);
/*% ripper: var_alias!($:2, $:3) %*/ /*% ripper: var_alias!($:2, $:3) %*/
} }
| keyword_alias tGVAR tBACK_REF | keyword_alias tGVAR tBACK_REF
@ -3141,7 +3141,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
char buf[2]; char buf[2];
buf[0] = '$'; buf[0] = '$';
buf[1] = (char)RNODE_BACK_REF($3)->nd_nth; buf[1] = (char)RNODE_BACK_REF($3)->nd_nth;
$$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$); $$ = NEW_VALIAS($2, rb_intern2(buf, 2), &@$, &@1);
/*% ripper: var_alias!($:2, $:3) %*/ /*% ripper: var_alias!($:2, $:3) %*/
} }
| keyword_alias tGVAR tNTH_REF | keyword_alias tGVAR tNTH_REF
@ -12308,11 +12308,12 @@ rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYL
} }
static rb_node_valias_t * static rb_node_valias_t *
rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc) rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
{ {
rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc); rb_node_valias_t *n = NODE_NEWNODE(NODE_VALIAS, rb_node_valias_t, loc);
n->nd_alias = nd_alias; n->nd_alias = nd_alias;
n->nd_orig = nd_orig; n->nd_orig = nd_orig;
n->keyword_loc = *keyword_loc;
return n; return n;
} }

View File

@ -828,6 +828,7 @@ typedef struct RNode_VALIAS {
ID nd_alias; ID nd_alias;
ID nd_orig; ID nd_orig;
rb_code_location_t keyword_loc;
} rb_node_valias_t; } rb_node_valias_t;
typedef struct RNode_UNDEF { typedef struct RNode_UNDEF {

View File

@ -1351,6 +1351,14 @@ dummy
assert_locations(node.children[-1].locations, [[1, 0, 1, 14], [1, 0, 1, 5]]) assert_locations(node.children[-1].locations, [[1, 0, 1, 14], [1, 0, 1, 5]])
end end
def test_valias_locations
node = RubyVM::AbstractSyntaxTree.parse("alias $foo $bar")
assert_locations(node.children[-1].locations, [[1, 0, 1, 15], [1, 0, 1, 5]])
node = RubyVM::AbstractSyntaxTree.parse("alias $foo $&")
assert_locations(node.children[-1].locations, [[1, 0, 1, 13], [1, 0, 1, 5]])
end
private private
def assert_locations(locations, expected) def assert_locations(locations, expected)
ary = locations.map {|loc| loc && [loc.first_lineno, loc.first_column, loc.last_lineno, loc.last_column] } ary = locations.map {|loc| loc && [loc.first_lineno, loc.first_column, loc.last_lineno, loc.last_column] }