Skip to content

Commit 4775d1f

Browse files
S-H-GAMELINKSnobu
authored andcommitted
Add NODE IN locations
Add locations to struct `RNode_IN`. memo: ```bash > ruby -e 'case 1; in 2 then 3; end' --parser=prism --dump=parsetree @ ProgramNode (location: (1,0)-(1,24)) +-- locals: [] +-- statements: @ StatementsNode (location: (1,0)-(1,24)) +-- body: (length: 1) +-- @ CaseMatchNode (location: (1,0)-(1,24)) +-- predicate: | @ IntegerNode (location: (1,5)-(1,6)) | +-- IntegerBaseFlags: decimal | +-- value: 1 +-- conditions: (length: 1) | +-- @ InNode (location: (1,8)-(1,19)) | +-- pattern: | | @ IntegerNode (location: (1,11)-(1,12)) | | +-- IntegerBaseFlags: decimal | | +-- value: 2 | +-- statements: | | @ StatementsNode (location: (1,18)-(1,19)) | | +-- body: (length: 1) | | +-- @ IntegerNode (location: (1,18)-(1,19)) | | +-- IntegerBaseFlags: decimal | | +-- value: 3 | +-- in_loc: (1,8)-(1,10) = "in" | +-- then_loc: (1,13)-(1,17) = "then" +-- else_clause: nil +-- case_keyword_loc: (1,0)-(1,4) = "case" +-- end_keyword_loc: (1,21)-(1,24) = "end" ```
1 parent e0b72ad commit 4775d1f

File tree

5 files changed

+36
-7
lines changed

5 files changed

+36
-7
lines changed

ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,12 @@ node_locations(VALUE ast_value, const NODE *node)
866866
location_new(&RNODE_IF(node)->if_keyword_loc),
867867
location_new(&RNODE_IF(node)->then_keyword_loc),
868868
location_new(&RNODE_IF(node)->end_keyword_loc));
869+
case NODE_IN:
870+
return rb_ary_new_from_args(4,
871+
location_new(nd_code_loc(node)),
872+
location_new(&RNODE_IN(node)->in_keyword_loc),
873+
location_new(&RNODE_IN(node)->then_keyword_loc),
874+
location_new(&RNODE_IN(node)->operator_loc));
869875
case NODE_MODULE:
870876
return rb_ary_new_from_args(3,
871877
location_new(nd_code_loc(node)),

node_dump.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,11 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
309309
ANN("example: case x; in 1; foo; in 2; bar; else baz; end");
310310
F_NODE(nd_head, RNODE_IN, "in pattern");
311311
F_NODE(nd_body, RNODE_IN, "in body");
312-
LAST_NODE;
313312
F_NODE(nd_next, RNODE_IN, "next in clause");
313+
F_LOC(in_keyword_loc, RNODE_IN);
314+
F_LOC(then_keyword_loc, RNODE_IN);
315+
LAST_NODE;
316+
F_LOC(operator_loc, RNODE_IN);
314317
return;
315318

316319
case NODE_WHILE:

parse.y

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ static rb_node_case_t *rb_node_case_new(struct parser_params *p, NODE *nd_head,
10701070
static rb_node_case2_t *rb_node_case2_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
10711071
static rb_node_case3_t *rb_node_case3_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *case_keyword_loc, const YYLTYPE *end_keyword_loc);
10721072
static rb_node_when_t *rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *then_keyword_loc);
1073-
static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc);
1073+
static rb_node_in_t *rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc);
10741074
static rb_node_while_t *rb_node_while_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
10751075
static rb_node_until_t *rb_node_until_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, long nd_state, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *closing_loc);
10761076
static rb_node_iter_t *rb_node_iter_new(struct parser_params *p, rb_node_args_t *nd_args, NODE *nd_body, const YYLTYPE *loc);
@@ -1178,7 +1178,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
11781178
#define NEW_CASE2(b,loc,ck_loc,ek_loc) (NODE *)rb_node_case2_new(p,b,loc,ck_loc,ek_loc)
11791179
#define NEW_CASE3(h,b,loc,ck_loc,ek_loc) (NODE *)rb_node_case3_new(p,h,b,loc,ck_loc,ek_loc)
11801180
#define NEW_WHEN(c,t,e,loc,k_loc,t_loc) (NODE *)rb_node_when_new(p,c,t,e,loc,k_loc,t_loc)
1181-
#define NEW_IN(c,t,e,loc) (NODE *)rb_node_in_new(p,c,t,e,loc)
1181+
#define NEW_IN(c,t,e,loc,ik_loc,tk_loc,o_loc) (NODE *)rb_node_in_new(p,c,t,e,loc,ik_loc,tk_loc,o_loc)
11821182
#define NEW_WHILE(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_while_new(p,c,b,n,loc,k_loc,c_loc)
11831183
#define NEW_UNTIL(c,b,n,loc,k_loc,c_loc) (NODE *)rb_node_until_new(p,c,b,n,loc,k_loc,c_loc)
11841184
#define NEW_ITER(a,b,loc) (NODE *)rb_node_iter_new(p,a,b,loc)
@@ -3472,7 +3472,7 @@ expr : command_call
34723472
pop_pktbl(p, $p_pktbl);
34733473
pop_pvtbl(p, $p_pvtbl);
34743474
p->ctxt.in_kwarg = $ctxt.in_kwarg;
3475-
$$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body), &@$, &NULL_LOC, &NULL_LOC);
3475+
$$ = NEW_CASE3($arg, NEW_IN($body, 0, 0, &@body, &NULL_LOC, &NULL_LOC, &@2), &@$, &NULL_LOC, &NULL_LOC);
34763476
/*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
34773477
}
34783478
| arg keyword_in
@@ -3485,7 +3485,7 @@ expr : command_call
34853485
pop_pktbl(p, $p_pktbl);
34863486
pop_pvtbl(p, $p_pvtbl);
34873487
p->ctxt.in_kwarg = $ctxt.in_kwarg;
3488-
$$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body), &@$, &NULL_LOC, &NULL_LOC);
3488+
$$ = NEW_CASE3($arg, NEW_IN($body, NEW_TRUE(&@body), NEW_FALSE(&@body), &@body, &@keyword_in, &NULL_LOC, &NULL_LOC), &@$, &NULL_LOC, &NULL_LOC);
34893489
/*% ripper: case!($:arg, in!($:body, Qnil, Qnil)) %*/
34903490
}
34913491
| arg %prec tLBRACE_ARG
@@ -5399,7 +5399,7 @@ p_case_body : keyword_in
53995399
compstmt(stmts)
54005400
p_cases[cases]
54015401
{
5402-
$$ = NEW_IN($expr, $compstmt, $cases, &@$);
5402+
$$ = NEW_IN($expr, $compstmt, $cases, &@$, &@keyword_in, &@then, &NULL_LOC);
54035403
/*% ripper: in!($:expr, $:compstmt, $:cases) %*/
54045404
}
54055405
;
@@ -11528,12 +11528,15 @@ rb_node_when_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd
1152811528
}
1152911529

1153011530
static rb_node_in_t *
11531-
rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc)
11531+
rb_node_in_new(struct parser_params *p, NODE *nd_head, NODE *nd_body, NODE *nd_next, const YYLTYPE *loc, const YYLTYPE *in_keyword_loc, const YYLTYPE *then_keyword_loc, const YYLTYPE *operator_loc)
1153211532
{
1153311533
rb_node_in_t *n = NODE_NEWNODE(NODE_IN, rb_node_in_t, loc);
1153411534
n->nd_head = nd_head;
1153511535
n->nd_body = nd_body;
1153611536
n->nd_next = nd_next;
11537+
n->in_keyword_loc = *in_keyword_loc;
11538+
n->then_keyword_loc = *then_keyword_loc;
11539+
n->operator_loc = *operator_loc;
1153711540

1153811541
return n;
1153911542
}

rubyparser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ typedef struct RNode_IN {
324324
struct RNode *nd_head;
325325
struct RNode *nd_body;
326326
struct RNode *nd_next;
327+
rb_code_location_t in_keyword_loc;
328+
rb_code_location_t then_keyword_loc;
329+
rb_code_location_t operator_loc;
327330
} rb_node_in_t;
328331

329332
typedef struct RNode_LOOP {

test/ruby/test_ast.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1514,6 +1514,20 @@ def test_if_locations
15141514
assert_locations(node.children[-1].children[1].children[0].locations, [[1, 11, 1, 17], [1, 13, 1, 15], nil, nil])
15151515
end
15161516

1517+
def test_in_locations
1518+
node = ast_parse("case 1; in 2 then 3; end")
1519+
assert_locations(node.children[-1].children[1].locations, [[1, 8, 1, 20], [1, 8, 1, 10], [1, 13, 1, 17], nil])
1520+
1521+
node = ast_parse("1 => a")
1522+
assert_locations(node.children[-1].children[1].locations, [[1, 5, 1, 6], nil, nil, [1, 2, 1, 4]])
1523+
1524+
node = ast_parse("1 in a")
1525+
assert_locations(node.children[-1].children[1].locations, [[1, 5, 1, 6], [1, 2, 1, 4], nil, nil])
1526+
1527+
node = ast_parse("case 1; in 2; 3; end")
1528+
assert_locations(node.children[-1].children[1].locations, [[1, 8, 1, 16], [1, 8, 1, 10], [1, 12, 1, 13], nil])
1529+
end
1530+
15171531
def test_next_locations
15181532
node = ast_parse("loop { next 1 }")
15191533
assert_locations(node.children[-1].children[-1].children[-1].locations, [[1, 7, 1, 13], [1, 7, 1, 11]])

0 commit comments

Comments
 (0)