Skip to content

Commit bcd2105

Browse files
S-H-GAMELINKSydah
authored andcommitted
Add MODULE NODE locations
Add `keyword_module` amd `keyword_end` locations to struct `RNode_MODULE`. memo: ``` >ruby --dump=parsetree -e 'module A end' @ ProgramNode (location: (1,0)-(1,12)) +-- locals: [] +-- statements: @ StatementsNode (location: (1,0)-(1,12)) +-- body: (length: 1) +-- @ ModuleNode (location: (1,0)-(1,12)) +-- locals: [] +-- module_keyword_loc: (1,0)-(1,6) = "module" +-- constant_path: | @ ConstantReadNode (location: (1,7)-(1,8)) | +-- name: :A +-- body: nil +-- end_keyword_loc: (1,9)-(1,12) = "end" +-- name: :A ```
1 parent 7aa0e09 commit bcd2105

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

ast.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,11 @@ 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_MODULE:
870+
return rb_ary_new_from_args(3,
871+
location_new(nd_code_loc(node)),
872+
location_new(&RNODE_MODULE(node)->module_keyword_loc),
873+
location_new(&RNODE_MODULE(node)->end_keyword_loc));
869874
case NODE_NEXT:
870875
return rb_ary_new_from_args(2,
871876
location_new(nd_code_loc(node)),

node_dump.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,10 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
10091009
ANN("format: module [nd_cpath]; [nd_body]; end");
10101010
ANN("example: module M; ..; end");
10111011
F_NODE(nd_cpath, RNODE_MODULE, "module path");
1012-
LAST_NODE;
10131012
F_NODE(nd_body, RNODE_MODULE, "module definition");
1013+
F_LOC(module_keyword_loc, RNODE_MODULE);
1014+
LAST_NODE;
1015+
F_LOC(end_keyword_loc, RNODE_MODULE);
10141016
return;
10151017

10161018
case NODE_SCLASS:

parse.y

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,7 +1145,7 @@ static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st,
11451145
static rb_node_valias_t *rb_node_valias_new(struct parser_params *p, ID nd_alias, ID nd_orig, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
11461146
static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
11471147
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);
1148-
static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc);
1148+
static rb_node_module_t *rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc);
11491149
static rb_node_sclass_t *rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const YYLTYPE *loc);
11501150
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);
11511151
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);
@@ -1253,7 +1253,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
12531253
#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
12541254
#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
12551255
#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)
1256-
#define NEW_MODULE(n,b,loc) (NODE *)rb_node_module_new(p,n,b,loc)
1256+
#define NEW_MODULE(n,b,loc,mk_loc,ek_loc) (NODE *)rb_node_module_new(p,n,b,loc,mk_loc,ek_loc)
12571257
#define NEW_SCLASS(r,b,loc) (NODE *)rb_node_sclass_new(p,r,b,loc)
12581258
#define NEW_COLON2(c,i,loc,d_loc,n_loc) (NODE *)rb_node_colon2_new(p,c,i,loc,d_loc,n_loc)
12591259
#define NEW_COLON3(i,loc,d_loc,n_loc) (NODE *)rb_node_colon3_new(p,i,loc,d_loc,n_loc)
@@ -4621,7 +4621,7 @@ primary : inline_primary
46214621
bodystmt
46224622
k_end
46234623
{
4624-
$$ = NEW_MODULE($cpath, $bodystmt, &@$);
4624+
$$ = NEW_MODULE($cpath, $bodystmt, &@$, &@k_module, &@k_end);
46254625
nd_set_line(RNODE_MODULE($$)->nd_body, @k_end.end_pos.lineno);
46264626
set_line_body($bodystmt, @cpath.end_pos.lineno);
46274627
nd_set_line($$, @cpath.end_pos.lineno);
@@ -11438,13 +11438,15 @@ rb_node_sclass_new(struct parser_params *p, NODE *nd_recv, NODE *nd_body, const
1143811438
}
1143911439

1144011440
static rb_node_module_t *
11441-
rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc)
11441+
rb_node_module_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *module_keyword_loc, const YYLTYPE *end_keyword_loc)
1144211442
{
1144311443
/* Keep the order of node creation */
1144411444
NODE *scope = NEW_SCOPE(0, nd_body, loc);
1144511445
rb_node_module_t *n = NODE_NEWNODE(NODE_MODULE, rb_node_module_t, loc);
1144611446
n->nd_cpath = nd_cpath;
1144711447
n->nd_body = scope;
11448+
n->module_keyword_loc = *module_keyword_loc;
11449+
n->end_keyword_loc = *end_keyword_loc;
1144811450

1144911451
return n;
1145011452
}

rubyparser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,8 @@ typedef struct RNode_MODULE {
901901

902902
struct RNode *nd_cpath;
903903
struct RNode *nd_body;
904+
rb_code_location_t module_keyword_loc;
905+
rb_code_location_t end_keyword_loc;
904906
} rb_node_module_t;
905907

906908
typedef struct RNode_SCLASS {

test/ruby/test_ast.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,11 @@ def test_lambda_locations
14911491
assert_locations(node.children[-1].locations, [[1, 0, 1, 20], [1, 0, 1, 2], [1, 10, 1, 12], [1, 17, 1, 20]])
14921492
end
14931493

1494+
def test_module_locations
1495+
node = ast_parse('module A end')
1496+
assert_locations(node.children[-1].locations, [[1, 0, 1, 12], [1, 0, 1, 6], [1, 9, 1, 12]])
1497+
end
1498+
14941499
def test_if_locations
14951500
node = ast_parse("if cond then 1 else 2 end")
14961501
assert_locations(node.children[-1].locations, [[1, 0, 1, 25], [1, 0, 1, 2], [1, 8, 1, 12], [1, 22, 1, 25]])

0 commit comments

Comments
 (0)