Skip to content

Commit d60144a

Browse files
committed
Implement COLON3 NODE locations
1 parent c584790 commit d60144a

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

ast.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,11 @@ node_locations(VALUE ast_value, const NODE *node)
817817
location_new(nd_code_loc(node)),
818818
location_new(&RNODE_COLON2(node)->delimiter_loc),
819819
location_new(&RNODE_COLON2(node)->name_loc));
820+
case NODE_COLON3:
821+
return rb_ary_new_from_args(3,
822+
location_new(nd_code_loc(node)),
823+
location_new(&RNODE_COLON3(node)->delimiter_loc),
824+
location_new(&RNODE_COLON3(node)->name_loc));
820825
case NODE_DOT2:
821826
return rb_ary_new_from_args(2,
822827
location_new(nd_code_loc(node)),

node_dump.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,6 +1038,8 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
10381038
ANN("format: ::[nd_mid]");
10391039
ANN("example: ::Object");
10401040
F_ID(nd_mid, RNODE_COLON3, "constant name");
1041+
F_LOC(delimiter_loc, RNODE_COLON3);
1042+
F_LOC(name_loc, RNODE_COLON3);
10411043
return;
10421044

10431045
case NODE_DOT2:

parse.y

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,7 @@ static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpat
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);
11491149
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);
1150-
static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc);
1150+
static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_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);
11531153
static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc);
@@ -1255,7 +1255,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
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)
12571257
#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
1258-
#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc)
1258+
#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_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)
12611261
#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc)
@@ -3073,7 +3073,7 @@ rb_parser_ary_free(rb_parser_t *p, rb_parser_ary_t *ary)
30733073
| tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
30743074
{
30753075
YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
3076-
$$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
3076+
$$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
30773077
/*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
30783078
}
30793079
| backref tOP_ASGN lex_ctxt rhs
@@ -3761,7 +3761,7 @@ mlhs_node : user_or_keyword_variable
37613761
| tCOLON3 tCONSTANT
37623762
{
37633763
/*% ripper: top_const_field!($:2) %*/
3764-
$$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3764+
$$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
37653765
}
37663766
| backref
37673767
{
@@ -3799,7 +3799,7 @@ lhs : user_or_keyword_variable
37993799
| tCOLON3 tCONSTANT
38003800
{
38013801
/*% ripper: top_const_field!($:2) %*/
3802-
$$ = const_decl(p, NEW_COLON3($2, &@$), &@$);
3802+
$$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
38033803
}
38043804
| backref
38053805
{
@@ -3822,7 +3822,7 @@ cname : tIDENTIFIER
38223822

38233823
cpath : tCOLON3 cname
38243824
{
3825-
$$ = NEW_COLON3($2, &@$);
3825+
$$ = NEW_COLON3($2, &@$, &@1, &@2);
38263826
/*% ripper: top_const_ref!($:2) %*/
38273827
}
38283828
| cname
@@ -4390,7 +4390,7 @@ primary : inline_primary
43904390
}
43914391
| tCOLON3 tCONSTANT
43924392
{
4393-
$$ = NEW_COLON3($2, &@$);
4393+
$$ = NEW_COLON3($2, &@$, &@1, &@2);
43944394
/*% ripper: top_const_ref!($:2) %*/
43954395
}
43964396
| tLBRACK aref_args ']'
@@ -5810,7 +5810,7 @@ p_expr_ref : '^' tLPAREN expr_value rparen
58105810

58115811
p_const : tCOLON3 cname
58125812
{
5813-
$$ = NEW_COLON3($2, &@$);
5813+
$$ = NEW_COLON3($2, &@$, &@1, &@2);
58145814
/*% ripper: top_const_ref!($:2) %*/
58155815
}
58165816
| p_const tCOLON2 cname
@@ -11565,10 +11565,12 @@ rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLT
1156511565
}
1156611566

1156711567
static rb_node_colon3_t *
11568-
rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc)
11568+
rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
1156911569
{
1157011570
rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
1157111571
n->nd_mid = nd_mid;
11572+
n->delimiter_loc = *delimiter_loc;
11573+
n->name_loc = *name_loc;
1157211574

1157311575
return n;
1157411576
}

rubyparser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,8 @@ typedef struct RNode_COLON3 {
923923
NODE node;
924924

925925
ID nd_mid;
926+
rb_code_location_t delimiter_loc;
927+
rb_code_location_t name_loc;
926928
} rb_node_colon3_t;
927929

928930
/* NODE_DOT2, NODE_DOT3, NODE_FLIP2, NODE_FLIP3 */

test/ruby/test_ast.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,15 @@ def test_colon2_locations
14061406
assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 4], [1, 1, 1, 3], [1, 3, 1, 4]])
14071407
end
14081408

1409+
def test_colon3_locations
1410+
node = ast_parse("::A")
1411+
assert_locations(node.children[-1].locations, [[1, 0, 1, 3], [1, 0, 1, 2], [1, 2, 1, 3]])
1412+
1413+
node = ast_parse("::A::B")
1414+
assert_locations(node.children[-1].locations, [[1, 0, 1, 6], [1, 3, 1, 5], [1, 5, 1, 6]])
1415+
assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 3], [1, 0, 1, 2], [1, 2, 1, 3]])
1416+
end
1417+
14091418
def test_dot2_locations
14101419
node = ast_parse("1..2")
14111420
assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3]])

0 commit comments

Comments
 (0)