From ab18b1b4f55d3d06e6e040d755e75b5b447198f3 Mon Sep 17 00:00:00 2001 From: ydah Date: Tue, 3 Sep 2024 16:20:52 +0900 Subject: [PATCH] Implement VALIAS NODE keyword locations --- ast.c | 4 ++++ node_dump.c | 1 + parse.y | 11 ++++++----- rubyparser.h | 1 + test/ruby/test_ast.rb | 8 ++++++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ast.c b/ast.c index d6d3e11d60..12366dd432 100644 --- a/ast.c +++ b/ast.c @@ -789,6 +789,10 @@ node_locations(VALUE ast_value, const NODE *node) location_new(&RNODE_UNLESS(node)->keyword_loc), location_new(&RNODE_UNLESS(node)->then_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_LAST: break; diff --git a/node_dump.c b/node_dump.c index 6ab91bd853..54a4eb0e64 100644 --- a/node_dump.c +++ b/node_dump.c @@ -933,6 +933,7 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node) ANN("example: alias $y $x"); F_ID(nd_alias, RNODE_VALIAS, "new name"); F_ID(nd_orig, RNODE_VALIAS, "old name"); + F_LOC(keyword_loc, RNODE_VALIAS); return; case NODE_UNDEF: diff --git a/parse.y b/parse.y index 1dffac64f4..6ebd60844a 100644 --- a/parse.y +++ b/parse.y @@ -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_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_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_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); @@ -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_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_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_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) @@ -3133,7 +3133,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem } | keyword_alias tGVAR tGVAR { - $$ = NEW_VALIAS($2, $3, &@$); + $$ = NEW_VALIAS($2, $3, &@$, &@1); /*% ripper: var_alias!($:2, $:3) %*/ } | keyword_alias tGVAR tBACK_REF @@ -3141,7 +3141,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem char buf[2]; buf[0] = '$'; 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) %*/ } | 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 * -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); n->nd_alias = nd_alias; n->nd_orig = nd_orig; + n->keyword_loc = *keyword_loc; return n; } diff --git a/rubyparser.h b/rubyparser.h index a690394928..5383c31f03 100644 --- a/rubyparser.h +++ b/rubyparser.h @@ -828,6 +828,7 @@ typedef struct RNode_VALIAS { ID nd_alias; ID nd_orig; + rb_code_location_t keyword_loc; } rb_node_valias_t; typedef struct RNode_UNDEF { diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index cded28cf1f..d1422fc2c0 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -1351,6 +1351,14 @@ dummy assert_locations(node.children[-1].locations, [[1, 0, 1, 14], [1, 0, 1, 5]]) 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 def assert_locations(locations, expected) ary = locations.map {|loc| loc && [loc.first_lineno, loc.first_column, loc.last_lineno, loc.last_column] }