Implement DOT2 NODE locations

This commit is contained in:
ydah 2024-11-03 00:26:25 +09:00 committed by Yudai Takada
parent a1f010b8e4
commit 88da6856a3
Notes: git 2025-01-04 11:27:59 +00:00
5 changed files with 31 additions and 10 deletions

4
ast.c
View File

@ -807,6 +807,10 @@ node_locations(VALUE ast_value, const NODE *node)
location_new(nd_code_loc(node)),
location_new(&RNODE_CASE3(node)->case_keyword_loc),
location_new(&RNODE_CASE3(node)->end_keyword_loc));
case NODE_DOT2:
return rb_ary_new_from_args(2,
location_new(nd_code_loc(node)),
location_new(&RNODE_DOT2(node)->operator_loc));
case NODE_EVSTR:
return rb_ary_new_from_args(3,
location_new(nd_code_loc(node)),

View File

@ -1049,8 +1049,9 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("example: if (x==1)...(x==5); foo; end");
dot:
F_NODE(nd_beg, RNODE_DOT2, "begin");
LAST_NODE;
F_NODE(nd_end, RNODE_DOT2, "end");
LAST_NODE;
F_LOC(operator_loc, RNODE_DOT2);
return;
case NODE_SELF:

19
parse.y
View File

@ -1150,7 +1150,7 @@ static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cp
static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
static rb_node_dot2_t *rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc);
static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
static rb_node_nil_t *rb_node_nil_new(struct parser_params *p, const YYLTYPE *loc);
@ -1258,7 +1258,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
#define NEW_DOT2(b,e,loc) (NODE *)rb_node_dot2_new(p,b,e,loc)
#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
#define NEW_DOT3(b,e,loc) (NODE *)rb_node_dot3_new(p,b,e,loc)
#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
#define NEW_NIL(loc) (NODE *)rb_node_nil_new(p,loc)
@ -3867,7 +3867,7 @@ arg : asgn(lhs, arg_rhs)
{
value_expr($1);
value_expr($3);
$$ = NEW_DOT2($1, $3, &@$);
$$ = NEW_DOT2($1, $3, &@$, &@2);
/*% ripper: dot2!($:1, $:3) %*/
}
| arg tDOT3 arg
@ -3880,7 +3880,7 @@ arg : asgn(lhs, arg_rhs)
| arg tDOT2
{
value_expr($1);
$$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
$$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
/*% ripper: dot2!($:1, Qnil) %*/
}
| arg tDOT3
@ -3892,7 +3892,7 @@ arg : asgn(lhs, arg_rhs)
| tBDOT2 arg
{
value_expr($2);
$$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
$$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
/*% ripper: dot2!(Qnil, $:2) %*/
}
| tBDOT3 arg
@ -5786,7 +5786,7 @@ p_any_kwrest : p_kwrest
p_value : p_primitive
| p_primitive_value tDOT2 p_primitive_value
{
$$ = NEW_DOT2($1, $3, &@$);
$$ = NEW_DOT2($1, $3, &@$, &@2);
/*% ripper: dot2!($:1, $:3) %*/
}
| p_primitive_value tDOT3 p_primitive_value
@ -5796,7 +5796,7 @@ p_value : p_primitive
}
| p_primitive_value tDOT2
{
$$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$);
$$ = NEW_DOT2($1, new_nil_at(p, &@2.end_pos), &@$, &@2);
/*% ripper: dot2!($:1, Qnil) %*/
}
| p_primitive_value tDOT3
@ -5809,7 +5809,7 @@ p_value : p_primitive
| p_const
| tBDOT2 p_primitive_value
{
$$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$);
$$ = NEW_DOT2(new_nil_at(p, &@1.beg_pos), $2, &@$, &@1);
/*% ripper: dot2!(Qnil, $:2) %*/
}
| tBDOT3 p_primitive_value
@ -11639,11 +11639,12 @@ rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
}
static rb_node_dot2_t *
rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc)
rb_node_dot2_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc)
{
rb_node_dot2_t *n = NODE_NEWNODE(NODE_DOT2, rb_node_dot2_t, loc);
n->nd_beg = nd_beg;
n->nd_end = nd_end;
n->operator_loc = *operator_loc;
return n;
}

View File

@ -915,6 +915,7 @@ typedef struct RNode_DOTS {
struct RNode *nd_beg;
struct RNode *nd_end;
rb_code_location_t operator_loc;
} rb_node_dot2_t, rb_node_dot3_t, rb_node_flip2_t, rb_node_flip3_t;
typedef struct RNode_SELF {

View File

@ -1376,6 +1376,20 @@ dummy
assert_locations(node.children[-1].locations, [[1, 0, 1, 17], [1, 0, 1, 4], [1, 14, 1, 17]])
end
def test_dot2_locations
node = ast_parse("1..2")
assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3]])
node = ast_parse("foo(1..2)")
assert_locations(node.children[-1].children[-1].children[0].locations, [[1, 4, 1, 8], [1, 5, 1, 7]])
node = ast_parse("foo(1..2, 3)")
assert_locations(node.children[-1].children[-1].children[0].locations, [[1, 4, 1, 8], [1, 5, 1, 7]])
node = ast_parse("foo(..2)")
assert_locations(node.children[-1].children[-1].children[0].locations, [[1, 4, 1, 7], [1, 4, 1, 6]])
end
def test_evstr_locations
node = ast_parse('"#{foo}"')
assert_locations(node.children[-1].children[1].locations, [[1, 0, 1, 8], [1, 1, 1, 3], [1, 6, 1, 7]])