Implement SPLAT NODE keyword locations

This commit is contained in:
ydah 2024-09-24 20:05:12 +09:00 коммит произвёл Yuichiro Kaneko
Родитель 239c1c621e
Коммит 044e57ed7c
5 изменённых файлов: 32 добавлений и 11 удалений

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

@ -836,6 +836,10 @@ node_locations(VALUE ast_value, const NODE *node)
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),
location_new(&RNODE_RETURN(node)->keyword_loc));
case NODE_SPLAT:
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),
location_new(&RNODE_SPLAT(node)->operator_loc));
case NODE_UNDEF:
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),

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

@ -902,8 +902,9 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("splat argument");
ANN("format: *[nd_head]");
ANN("example: foo(*ary)");
LAST_NODE;
F_NODE(nd_head, RNODE_SPLAT, "splat'ed array");
LAST_NODE;
F_LOC(operator_loc, RNODE_SPLAT);
return;
case NODE_BLOCK_PASS:

21
parse.y
Просмотреть файл

@ -1133,7 +1133,7 @@ static rb_node_kw_arg_t *rb_node_kw_arg_new(struct parser_params *p, NODE *nd_bo
static rb_node_postarg_t *rb_node_postarg_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc);
static rb_node_argscat_t *rb_node_argscat_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
static rb_node_argspush_t *rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc);
static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
static rb_node_splat_t *rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc);
static rb_node_block_pass_t *rb_node_block_pass_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *operator_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);
@ -1241,7 +1241,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
#define NEW_POSTARG(i,v,loc) (NODE *)rb_node_postarg_new(p,i,v,loc)
#define NEW_ARGSCAT(a,b,loc) (NODE *)rb_node_argscat_new(p,a,b,loc)
#define NEW_ARGSPUSH(a,b,loc) (NODE *)rb_node_argspush_new(p,a,b,loc)
#define NEW_SPLAT(a,loc) (NODE *)rb_node_splat_new(p,a,loc)
#define NEW_SPLAT(a,loc,op_loc) (NODE *)rb_node_splat_new(p,a,loc,op_loc)
#define NEW_BLOCK_PASS(b,loc,o_loc) rb_node_block_pass_new(p,b,loc,o_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)
@ -4349,7 +4349,7 @@ args : arg_value
}
| arg_splat
{
$$ = NEW_SPLAT($arg_splat, &@$);
$$ = $arg_splat;
/*% ripper: args_add_star!(args_new!, $:arg_splat) %*/
}
| args ',' arg_value
@ -4359,7 +4359,7 @@ args : arg_value
}
| args ',' arg_splat
{
$$ = rest_arg_append(p, $1, $3, &@$);
$$ = rest_arg_append(p, $1, RNODE_SPLAT($arg_splat)->nd_head, &@$);
/*% ripper: args_add_star!($:1, $:3) %*/
}
;
@ -4367,13 +4367,13 @@ args : arg_value
/* value */
arg_splat : tSTAR arg_value
{
$$ = $2;
$$ = NEW_SPLAT($2, &@$, &@1);
/*% ripper: $:2 %*/
}
| tSTAR /* none */
{
forwarding_arg_check(p, idFWD_REST, idFWD_ALL, "rest");
$$ = NEW_LVAR(idFWD_REST, &@1);
$$ = NEW_SPLAT(NEW_LVAR(idFWD_REST, &@1), &@$, &@1);
/*% ripper: Qnil %*/
}
;
@ -4396,7 +4396,7 @@ mrhs : args ',' arg_value
}
| tSTAR arg_value
{
$$ = NEW_SPLAT($2, &@$);
$$ = NEW_SPLAT($2, &@$, &@1);
/*% ripper: mrhs_add_star!(mrhs_new!, $:2) %*/
}
;
@ -5430,7 +5430,7 @@ case_args : arg_value
}
| tSTAR arg_value
{
$$ = NEW_SPLAT($2, &@$);
$$ = NEW_SPLAT($2, &@$, &@1);
/*% ripper: args_add_star!(args_new!, $:2) %*/
}
| case_args ',' arg_value
@ -12316,10 +12316,11 @@ rb_node_argspush_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, cons
}
static rb_node_splat_t *
rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
rb_node_splat_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc, const YYLTYPE *operator_loc)
{
rb_node_splat_t *n = NODE_NEWNODE(NODE_SPLAT, rb_node_splat_t, loc);
n->nd_head = nd_head;
n->operator_loc = *operator_loc;
return n;
}
@ -15167,7 +15168,7 @@ new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc
NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc));
#endif
rb_node_block_pass_t *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), argsloc, &NULL_LOC);
NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc);
NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc, &NULL_LOC);
block->forwarding = TRUE;
#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS
args = arg_append(p, args, new_hash(p, kwrest, loc), argsloc);

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

@ -810,6 +810,7 @@ typedef struct RNode_SPLAT {
NODE node;
struct RNode *nd_head;
rb_code_location_t operator_loc;
} rb_node_splat_t;
typedef struct RNode_BLOCK_PASS {

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

@ -1424,6 +1424,20 @@ dummy
assert_locations(node.children[-1].locations, [[1, 0, 1, 6], [1, 0, 1, 6]])
end
def test_splat_locations
node = ast_parse("a = *1")
assert_locations(node.children[-1].children[1].locations, [[1, 4, 1, 6], [1, 4, 1, 5]])
node = ast_parse("a = *1, 2")
assert_locations(node.children[-1].children[1].children[0].locations, [[1, 4, 1, 6], [1, 4, 1, 5]])
node = ast_parse("case a; when *1; end")
assert_locations(node.children[-1].children[1].children[0].locations, [[1, 13, 1, 15], [1, 13, 1, 14]])
node = ast_parse("case a; when *1, 2; end")
assert_locations(node.children[-1].children[1].children[0].children[0].locations, [[1, 13, 1, 15], [1, 13, 1, 14]])
end
def test_unless_locations
node = ast_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]])