Implement ALIAS NODE keyword locations

This commit is contained in:
ydah 2024-09-03 16:12:30 +09:00 коммит произвёл Yuichiro Kaneko
Родитель af143d8a74
Коммит a2243ee48b
5 изменённых файлов: 17 добавлений и 5 удалений

4
ast.c
Просмотреть файл

@ -775,6 +775,10 @@ node_locations(VALUE ast_value, const NODE *node)
{ {
enum node_type type = nd_type(node); enum node_type type = nd_type(node);
switch (type) { switch (type) {
case NODE_ALIAS:
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),
location_new(&RNODE_ALIAS(node)->keyword_loc));
case NODE_UNDEF: case NODE_UNDEF:
return rb_ary_new_from_args(2, return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)), location_new(nd_code_loc(node)),

Просмотреть файл

@ -922,8 +922,9 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("format: alias [nd_1st] [nd_2nd]"); ANN("format: alias [nd_1st] [nd_2nd]");
ANN("example: alias bar foo"); ANN("example: alias bar foo");
F_NODE(nd_1st, RNODE_ALIAS, "new name"); F_NODE(nd_1st, RNODE_ALIAS, "new name");
LAST_NODE;
F_NODE(nd_2nd, RNODE_ALIAS, "old name"); F_NODE(nd_2nd, RNODE_ALIAS, "old name");
LAST_NODE;
F_LOC(keyword_loc, RNODE_ALIAS);
return; return;
case NODE_VALIAS: case NODE_VALIAS:

Просмотреть файл

@ -1138,7 +1138,7 @@ static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head
static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc); static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, 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_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); 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);
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);
@ -1246,7 +1246,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
#define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc) #define NEW_BLOCK_PASS(b,loc) rb_node_block_pass_new(p,b,loc)
#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) (NODE *)rb_node_alias_new(p,n,o,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) (NODE *)rb_node_valias_new(p,n,o,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)
@ -3128,7 +3128,7 @@ k_END : keyword_END lex_ctxt
stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
{ {
$$ = NEW_ALIAS($2, $4, &@$); $$ = NEW_ALIAS($2, $4, &@$, &@1);
/*% ripper: alias!($:2, $:4) %*/ /*% ripper: alias!($:2, $:4) %*/
} }
| keyword_alias tGVAR tGVAR | keyword_alias tGVAR tGVAR
@ -12297,11 +12297,12 @@ rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *lo
} }
static rb_node_alias_t * static rb_node_alias_t *
rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc) rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc)
{ {
rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc); rb_node_alias_t *n = NODE_NEWNODE(NODE_ALIAS, rb_node_alias_t, loc);
n->nd_1st = nd_1st; n->nd_1st = nd_1st;
n->nd_2nd = nd_2nd; n->nd_2nd = nd_2nd;
n->keyword_loc = *keyword_loc;
return n; return n;
} }

Просмотреть файл

@ -820,6 +820,7 @@ typedef struct RNode_ALIAS {
struct RNode *nd_1st; struct RNode *nd_1st;
struct RNode *nd_2nd; struct RNode *nd_2nd;
rb_code_location_t keyword_loc;
} rb_node_alias_t; } rb_node_alias_t;
typedef struct RNode_VALIAS { typedef struct RNode_VALIAS {

Просмотреть файл

@ -1330,6 +1330,11 @@ dummy
assert_locations(node.locations, [[1, 0, 1, 5]]) assert_locations(node.locations, [[1, 0, 1, 5]])
end end
def test_alias_locations
node = RubyVM::AbstractSyntaxTree.parse("alias foo bar")
assert_locations(node.children[-1].locations, [[1, 0, 1, 13], [1, 0, 1, 5]])
end
def test_unless_locations def test_unless_locations
node = RubyVM::AbstractSyntaxTree.parse("unless cond then 1 else 2 end") node = RubyVM::AbstractSyntaxTree.parse("unless cond then 1 else 2 end")
assert_locations(node.children[-1].locations, [[1, 0, 1, 29], [1, 0, 1, 6], [1, 12, 1, 16], [1, 26, 1, 29]]) assert_locations(node.children[-1].locations, [[1, 0, 1, 29], [1, 0, 1, 6], [1, 12, 1, 16], [1, 26, 1, 29]])