Skip to content

Commit c584790

Browse files
committed
Implement COLON2 NODE locations
1 parent 2d96400 commit c584790

File tree

5 files changed

+31
-11
lines changed

5 files changed

+31
-11
lines changed

ast.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,11 @@ node_locations(VALUE ast_value, const NODE *node)
812812
location_new(&RNODE_CLASS(node)->class_keyword_loc),
813813
location_new(&RNODE_CLASS(node)->inheritance_operator_loc),
814814
location_new(&RNODE_CLASS(node)->end_keyword_loc));
815+
case NODE_COLON2:
816+
return rb_ary_new_from_args(3,
817+
location_new(nd_code_loc(node)),
818+
location_new(&RNODE_COLON2(node)->delimiter_loc),
819+
location_new(&RNODE_COLON2(node)->name_loc));
815820
case NODE_DOT2:
816821
return rb_ary_new_from_args(2,
817822
location_new(nd_code_loc(node)),

node_dump.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,8 +1027,10 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
10271027
ANN("format: [nd_head]::[nd_mid]");
10281028
ANN("example: M::C");
10291029
F_ID(nd_mid, RNODE_COLON2, "constant name");
1030-
LAST_NODE;
10311030
F_NODE(nd_head, RNODE_COLON2, "receiver");
1031+
F_LOC(delimiter_loc, RNODE_COLON2);
1032+
LAST_NODE;
1033+
F_LOC(name_loc, RNODE_COLON2);
10321034
return;
10331035

10341036
case NODE_COLON3:

parse.y

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_unde
11461146
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, const YYLTYPE *class_keyword_loc, const YYLTYPE *inheritance_operator_loc, const YYLTYPE *end_keyword_loc);
11471147
static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
11481148
static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
1149-
static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
1149+
static rb_node_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc);
11501150
static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
11511151
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);
11521152
static rb_node_dot3_t *rb_node_dot3_new(struct parser_params *p, NODE *nd_beg, NODE *nd_end, const YYLTYPE *loc, const YYLTYPE *operator_loc);
@@ -1254,7 +1254,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
12541254
#define NEW_CLASS(n,b,s,loc,ck_loc,io_loc,ek_loc) (NODE *)rb_node_class_new(p,n,b,s,loc,ck_loc,io_loc,ek_loc)
12551255
#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
12561256
#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
1257-
#define NEW_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
1257+
#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
12581258
#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
12591259
#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_loc)
12601260
#define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
@@ -3067,7 +3067,7 @@ rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
30673067
| primary_value tCOLON2 tCONSTANT tOP_ASGN lex_ctxt rhs
30683068
{
30693069
YYLTYPE loc = code_loc_gen(&@primary_value, &@tCONSTANT);
3070-
$$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3070+
$$ = new_const_op_assign(p, NEW_COLON2($primary_value, $tCONSTANT, &loc, &@tCOLON2, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
30713071
/*% ripper: opassign!(const_path_field!($:1, $:3), $:4, $:6) %*/
30723072
}
30733073
| tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
@@ -3756,7 +3756,7 @@ mlhs_node : user_or_keyword_variable
37563756
| primary_value tCOLON2 tCONSTANT
37573757
{
37583758
/*% ripper: const_path_field!($:1, $:3) %*/
3759-
$$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3759+
$$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
37603760
}
37613761
| tCOLON3 tCONSTANT
37623762
{
@@ -3794,7 +3794,7 @@ lhs : user_or_keyword_variable
37943794
| primary_value tCOLON2 tCONSTANT
37953795
{
37963796
/*% ripper: const_path_field!($:1, $:3) %*/
3797-
$$ = const_decl(p, NEW_COLON2($1, $3, &@$), &@$);
3797+
$$ = const_decl(p, NEW_COLON2($1, $3, &@$, &@2, &@3), &@$);
37983798
}
37993799
| tCOLON3 tCONSTANT
38003800
{
@@ -3827,12 +3827,12 @@ cpath : tCOLON3 cname
38273827
}
38283828
| cname
38293829
{
3830-
$$ = NEW_COLON2(0, $1, &@$);
3830+
$$ = NEW_COLON2(0, $1, &@$, &NULL_LOC, &@1);
38313831
/*% ripper: const_ref!($:1) %*/
38323832
}
38333833
| primary_value tCOLON2 cname
38343834
{
3835-
$$ = NEW_COLON2($1, $3, &@$);
3835+
$$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
38363836
/*% ripper: const_path_ref!($:1, $:3) %*/
38373837
}
38383838
;
@@ -4385,7 +4385,7 @@ primary : inline_primary
43854385
}
43864386
| primary_value tCOLON2 tCONSTANT
43874387
{
4388-
$$ = NEW_COLON2($1, $3, &@$);
4388+
$$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
43894389
/*% ripper: const_path_ref!($:1, $:3) %*/
43904390
}
43914391
| tCOLON3 tCONSTANT
@@ -5815,7 +5815,7 @@ p_const : tCOLON3 cname
58155815
}
58165816
| p_const tCOLON2 cname
58175817
{
5818-
$$ = NEW_COLON2($1, $3, &@$);
5818+
$$ = NEW_COLON2($1, $3, &@$, &@2, &@3);
58195819
/*% ripper: const_path_ref!($:1, $:3) %*/
58205820
}
58215821
| tCONSTANT
@@ -11553,11 +11553,13 @@ rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd
1155311553
}
1155411554

1155511555
static rb_node_colon2_t *
11556-
rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc)
11556+
rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
1155711557
{
1155811558
rb_node_colon2_t *n = NODE_NEWNODE(NODE_COLON2, rb_node_colon2_t, loc);
1155911559
n->nd_head = nd_head;
1156011560
n->nd_mid = nd_mid;
11561+
n->delimiter_loc = *delimiter_loc;
11562+
n->name_loc = *name_loc;
1156111563

1156211564
return n;
1156311565
}

rubyparser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ typedef struct RNode_COLON2 {
915915

916916
struct RNode *nd_head;
917917
ID nd_mid;
918+
rb_code_location_t delimiter_loc;
919+
rb_code_location_t name_loc;
918920
} rb_node_colon2_t;
919921

920922
typedef struct RNode_COLON3 {

test/ruby/test_ast.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,15 @@ def test_class_locations
13971397
assert_locations(node.children[-1].locations, [[1, 0, 1, 16], [1, 0, 1, 5], [1, 8, 1, 9], [1, 13, 1, 16]])
13981398
end
13991399

1400+
def test_colon2_locations
1401+
node = ast_parse("A::B")
1402+
assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3], [1, 3, 1, 4]])
1403+
1404+
node = ast_parse("A::B::C")
1405+
assert_locations(node.children[-1].locations, [[1, 0, 1, 7], [1, 4, 1, 6], [1, 6, 1, 7]])
1406+
assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 4], [1, 1, 1, 3], [1, 3, 1, 4]])
1407+
end
1408+
14001409
def test_dot2_locations
14011410
node = ast_parse("1..2")
14021411
assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3]])

0 commit comments

Comments
 (0)