summaryrefslogtreecommitdiff
diff options
authorydah <[email protected]>2024-11-03 00:26:25 +0900
committerYudai Takada <[email protected]>2025-01-04 20:27:40 +0900
commit88da6856a3f5cc6a84a8d1909f56952c605ef0dc (patch)
treee3d4d59629e43776fe28c7ecdf2d64a03f056474
parenta1f010b8e41f5406a728cc8a75816a0a20bf2174 (diff)
Implement DOT2 NODE locations
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/11986
-rw-r--r--ast.c4
-rw-r--r--node_dump.c3
-rw-r--r--parse.y19
-rw-r--r--rubyparser.h1
-rw-r--r--test/ruby/test_ast.rb14
5 files changed, 31 insertions, 10 deletions
diff --git a/ast.c b/ast.c
index 35e8b9d667..22246cb058 100644
--- a/ast.c
+++ b/ast.c
@@ -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)),
diff --git a/node_dump.c b/node_dump.c
index a585ff12fe..a80073f05e 100644
--- a/node_dump.c
+++ b/node_dump.c
@@ -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:
diff --git a/parse.y b/parse.y
index 08047976bc..bd8aad4237 100644
--- a/parse.y
+++ b/parse.y
@@ -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;
}
diff --git a/rubyparser.h b/rubyparser.h
index 2db250c27e..0d3a04c88e 100644
--- a/rubyparser.h
+++ b/rubyparser.h
@@ -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 {
diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb
index 7f4fb36a9f..0e8f422193 100644
--- a/test/ruby/test_ast.rb
+++ b/test/ruby/test_ast.rb
@@ -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]])