Implement COLON3 NODE locations

This commit is contained in:
ydah 2024-11-03 01:43:21 +09:00 committed by Yudai Takada
parent c584790bde
commit d60144a490
Notes: git 2025-06-15 13:37:33 +00:00
5 changed files with 29 additions and 9 deletions

5
ast.c
View file

@ -817,6 +817,11 @@ node_locations(VALUE ast_value, const NODE *node)
location_new(nd_code_loc(node)), location_new(nd_code_loc(node)),
location_new(&RNODE_COLON2(node)->delimiter_loc), location_new(&RNODE_COLON2(node)->delimiter_loc),
location_new(&RNODE_COLON2(node)->name_loc)); location_new(&RNODE_COLON2(node)->name_loc));
case NODE_COLON3:
return rb_ary_new_from_args(3,
location_new(nd_code_loc(node)),
location_new(&RNODE_COLON3(node)->delimiter_loc),
location_new(&RNODE_COLON3(node)->name_loc));
case NODE_DOT2: case NODE_DOT2:
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)),

View file

@ -1038,6 +1038,8 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
ANN("format: ::[nd_mid]"); ANN("format: ::[nd_mid]");
ANN("example: ::Object"); ANN("example: ::Object");
F_ID(nd_mid, RNODE_COLON3, "constant name"); F_ID(nd_mid, RNODE_COLON3, "constant name");
F_LOC(delimiter_loc, RNODE_COLON3);
F_LOC(name_loc, RNODE_COLON3);
return; return;
case NODE_DOT2: case NODE_DOT2:

20
parse.y
View file

@ -1147,7 +1147,7 @@ static rb_node_class_t *rb_node_class_new(struct parser_params *p, NODE *nd_cpat
static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc); static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
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_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, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc); 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);
static rb_node_colon3_t *rb_node_colon3_new(struct parser_params *p, 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, const YYLTYPE *delimiter_loc, const YYLTYPE *name_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_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, 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, const YYLTYPE *operator_loc);
static rb_node_self_t *rb_node_self_new(struct parser_params *p, const YYLTYPE *loc); 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
#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc) #define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc) #define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc) #define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
#define NEW_COLON3(i,loc) (NODE *)rb_node_colon3_new(p,i,loc) #define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
#define NEW_DOT2(b,e,loc,op_loc) (NODE *)rb_node_dot2_new(p,b,e,loc,op_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,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc) #define NEW_DOT3(b,e,loc,op_loc) (NODE *)rb_node_dot3_new(p,b,e,loc,op_loc)
#define NEW_SELF(loc) (NODE *)rb_node_self_new(p,loc) #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)
| tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs | tCOLON3 tCONSTANT tOP_ASGN lex_ctxt rhs
{ {
YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT); YYLTYPE loc = code_loc_gen(&@tCOLON3, &@tCONSTANT);
$$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc), $tOP_ASGN, $rhs, $lex_ctxt, &@$); $$ = new_const_op_assign(p, NEW_COLON3($tCONSTANT, &loc, &@tCOLON3, &@tCONSTANT), $tOP_ASGN, $rhs, $lex_ctxt, &@$);
/*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/ /*% ripper: opassign!(top_const_field!($:2), $:3, $:5) %*/
} }
| backref tOP_ASGN lex_ctxt rhs | backref tOP_ASGN lex_ctxt rhs
@ -3761,7 +3761,7 @@ mlhs_node : user_or_keyword_variable
| tCOLON3 tCONSTANT | tCOLON3 tCONSTANT
{ {
/*% ripper: top_const_field!($:2) %*/ /*% ripper: top_const_field!($:2) %*/
$$ = const_decl(p, NEW_COLON3($2, &@$), &@$); $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
} }
| backref | backref
{ {
@ -3799,7 +3799,7 @@ lhs : user_or_keyword_variable
| tCOLON3 tCONSTANT | tCOLON3 tCONSTANT
{ {
/*% ripper: top_const_field!($:2) %*/ /*% ripper: top_const_field!($:2) %*/
$$ = const_decl(p, NEW_COLON3($2, &@$), &@$); $$ = const_decl(p, NEW_COLON3($2, &@$, &@1, &@2), &@$);
} }
| backref | backref
{ {
@ -3822,7 +3822,7 @@ cname : tIDENTIFIER
cpath : tCOLON3 cname cpath : tCOLON3 cname
{ {
$$ = NEW_COLON3($2, &@$); $$ = NEW_COLON3($2, &@$, &@1, &@2);
/*% ripper: top_const_ref!($:2) %*/ /*% ripper: top_const_ref!($:2) %*/
} }
| cname | cname
@ -4390,7 +4390,7 @@ primary : inline_primary
} }
| tCOLON3 tCONSTANT | tCOLON3 tCONSTANT
{ {
$$ = NEW_COLON3($2, &@$); $$ = NEW_COLON3($2, &@$, &@1, &@2);
/*% ripper: top_const_ref!($:2) %*/ /*% ripper: top_const_ref!($:2) %*/
} }
| tLBRACK aref_args ']' | tLBRACK aref_args ']'
@ -5810,7 +5810,7 @@ p_expr_ref : '^' tLPAREN expr_value rparen
p_const : tCOLON3 cname p_const : tCOLON3 cname
{ {
$$ = NEW_COLON3($2, &@$); $$ = NEW_COLON3($2, &@$, &@1, &@2);
/*% ripper: top_const_ref!($:2) %*/ /*% ripper: top_const_ref!($:2) %*/
} }
| p_const tCOLON2 cname | p_const tCOLON2 cname
@ -11565,10 +11565,12 @@ rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLT
} }
static rb_node_colon3_t * static rb_node_colon3_t *
rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc) rb_node_colon3_new(struct parser_params *p, ID nd_mid, const YYLTYPE *loc, const YYLTYPE *delimiter_loc, const YYLTYPE *name_loc)
{ {
rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc); rb_node_colon3_t *n = NODE_NEWNODE(NODE_COLON3, rb_node_colon3_t, loc);
n->nd_mid = nd_mid; n->nd_mid = nd_mid;
n->delimiter_loc = *delimiter_loc;
n->name_loc = *name_loc;
return n; return n;
} }

View file

@ -923,6 +923,8 @@ typedef struct RNode_COLON3 {
NODE node; NODE node;
ID nd_mid; ID nd_mid;
rb_code_location_t delimiter_loc;
rb_code_location_t name_loc;
} rb_node_colon3_t; } rb_node_colon3_t;
/* NODE_DOT2, NODE_DOT3, NODE_FLIP2, NODE_FLIP3 */ /* NODE_DOT2, NODE_DOT3, NODE_FLIP2, NODE_FLIP3 */

View file

@ -1406,6 +1406,15 @@ dummy
assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 4], [1, 1, 1, 3], [1, 3, 1, 4]]) assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 4], [1, 1, 1, 3], [1, 3, 1, 4]])
end end
def test_colon3_locations
node = ast_parse("::A")
assert_locations(node.children[-1].locations, [[1, 0, 1, 3], [1, 0, 1, 2], [1, 2, 1, 3]])
node = ast_parse("::A::B")
assert_locations(node.children[-1].locations, [[1, 0, 1, 6], [1, 3, 1, 5], [1, 5, 1, 6]])
assert_locations(node.children[-1].children[0].locations, [[1, 0, 1, 3], [1, 0, 1, 2], [1, 2, 1, 3]])
end
def test_dot2_locations def test_dot2_locations
node = ast_parse("1..2") node = ast_parse("1..2")
assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3]]) assert_locations(node.children[-1].locations, [[1, 0, 1, 4], [1, 1, 1, 3]])