Skip to content

Commit a47e686

Browse files
committed
Implement POSTEXE NODE locations
The following Location information has been added This is the information required for parse.y to be a universal parser: ``` ❯ ruby --parser=prism --dump=parsetree -e "END { }" @ ProgramNode (location: (1,0)-(1,8)) +-- locals: [] +-- statements: @ StatementsNode (location: (1,0)-(1,8)) +-- body: (length: 1) +-- @ PostExecutionNode (location: (1,0)-(1,8)) +-- statements: nil +-- keyword_loc: (1,0)-(1,3) = "END" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-- opening_loc: (1,4)-(1,5) = "{" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-- closing_loc: (1,7)-(1,8) = "}" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ```
1 parent 617e860 commit a47e686

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

ast.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,12 @@ node_locations(VALUE ast_value, const NODE *node)
868868
location_new(&RNODE_OP_ASGN2(node)->call_operator_loc),
869869
location_new(&RNODE_OP_ASGN2(node)->message_loc),
870870
location_new(&RNODE_OP_ASGN2(node)->binary_operator_loc));
871+
case NODE_POSTEXE:
872+
return rb_ary_new_from_args(4,
873+
location_new(nd_code_loc(node)),
874+
location_new(&RNODE_POSTEXE(node)->keyword_loc),
875+
location_new(&RNODE_POSTEXE(node)->opening_loc),
876+
location_new(&RNODE_POSTEXE(node)->closing_loc));
871877
case NODE_REDO:
872878
return rb_ary_new_from_args(2,
873879
location_new(nd_code_loc(node)),

node_dump.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,8 +1106,11 @@ dump_node(VALUE buf, VALUE indent, int comment, const NODE * node)
11061106
ANN("post-execution");
11071107
ANN("format: END { [nd_body] }");
11081108
ANN("example: END { foo }");
1109-
LAST_NODE;
11101109
F_NODE(nd_body, RNODE_POSTEXE, "END clause");
1110+
F_LOC(keyword_loc, RNODE_POSTEXE);
1111+
F_LOC(opening_loc, RNODE_POSTEXE);
1112+
LAST_NODE;
1113+
F_LOC(closing_loc, RNODE_POSTEXE);
11111114
return;
11121115

11131116
case NODE_ATTRASGN:

parse.y

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ static rb_node_true_t *rb_node_true_new(struct parser_params *p, const YYLTYPE *
11561156
static rb_node_false_t *rb_node_false_new(struct parser_params *p, const YYLTYPE *loc);
11571157
static rb_node_errinfo_t *rb_node_errinfo_new(struct parser_params *p, const YYLTYPE *loc);
11581158
static rb_node_defined_t *rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc);
1159-
static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc);
1159+
static rb_node_postexe_t *rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc);
11601160
static rb_node_sym_t *rb_node_sym_new(struct parser_params *p, VALUE str, const YYLTYPE *loc);
11611161
static rb_node_dsym_t *rb_node_dsym_new(struct parser_params *p, rb_parser_string_t *string, long nd_alen, NODE *nd_next, const YYLTYPE *loc);
11621162
static rb_node_attrasgn_t *rb_node_attrasgn_new(struct parser_params *p, NODE *nd_recv, ID nd_mid, NODE *nd_args, const YYLTYPE *loc);
@@ -1264,7 +1264,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
12641264
#define NEW_FALSE(loc) (NODE *)rb_node_false_new(p,loc)
12651265
#define NEW_ERRINFO(loc) (NODE *)rb_node_errinfo_new(p,loc)
12661266
#define NEW_DEFINED(e,loc) (NODE *)rb_node_defined_new(p,e,loc)
1267-
#define NEW_POSTEXE(b,loc) (NODE *)rb_node_postexe_new(p,b,loc)
1267+
#define NEW_POSTEXE(b,loc,k_loc,o_loc,c_loc) (NODE *)rb_node_postexe_new(p,b,loc,k_loc,o_loc,c_loc)
12681268
#define NEW_SYM(str,loc) (NODE *)rb_node_sym_new(p,str,loc)
12691269
#define NEW_DSYM(s,l,n,loc) (NODE *)rb_node_dsym_new(p,s,l,n,loc)
12701270
#define NEW_ATTRASGN(r,m,a,loc) (NODE *)rb_node_attrasgn_new(p,r,m,a,loc)
@@ -3314,7 +3314,7 @@ stmt : keyword_alias fitem {SET_LEX_STATE(EXPR_FNAME|EXPR_FITEM);} fitem
33143314
p->ctxt = $k_END;
33153315
{
33163316
NODE *scope = NEW_SCOPE2(0 /* tbl */, 0 /* args */, $compstmt /* body */, &@$);
3317-
$$ = NEW_POSTEXE(scope, &@$);
3317+
$$ = NEW_POSTEXE(scope, &@$, &@1, &@3, &@5);
33183318
}
33193319
/*% ripper: END!($:compstmt) %*/
33203320
}
@@ -12272,10 +12272,13 @@ rb_node_defined_new(struct parser_params *p, NODE *nd_head, const YYLTYPE *loc)
1227212272
}
1227312273

1227412274
static rb_node_postexe_t *
12275-
rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc)
12275+
rb_node_postexe_new(struct parser_params *p, NODE *nd_body, const YYLTYPE *loc, const YYLTYPE *keyword_loc, const YYLTYPE *opening_loc, const YYLTYPE *closing_loc)
1227612276
{
1227712277
rb_node_postexe_t *n = NODE_NEWNODE(NODE_POSTEXE, rb_node_postexe_t, loc);
1227812278
n->nd_body = nd_body;
12279+
n->keyword_loc = *keyword_loc;
12280+
n->opening_loc = *opening_loc;
12281+
n->closing_loc = *closing_loc;
1227912282

1228012283
return n;
1228112284
}

rubyparser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,9 @@ typedef struct RNode_POSTEXE {
961961
NODE node;
962962

963963
struct RNode *nd_body;
964+
rb_code_location_t keyword_loc;
965+
rb_code_location_t opening_loc;
966+
rb_code_location_t closing_loc;
964967
} rb_node_postexe_t;
965968

966969
typedef struct RNode_SYM {

test/ruby/test_ast.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,14 @@ def test_op_asgn2_locations
14971497
assert_locations(node.children[-1].children[-1].locations, [[1, 4, 1, 15], [1, 8, 1, 9], [1, 9, 1, 10], [1, 11, 1, 13]])
14981498
end
14991499

1500+
def test_postexe_locations
1501+
node = ast_parse("END { }")
1502+
assert_locations(node.children[-1].locations, [[1, 0, 1, 8], [1, 0, 1, 3], [1, 4, 1, 5], [1, 7, 1, 8]])
1503+
1504+
node = ast_parse("END { 1 }")
1505+
assert_locations(node.children[-1].locations, [[1, 0, 1, 9], [1, 0, 1, 3], [1, 4, 1, 5], [1, 8, 1, 9]])
1506+
end
1507+
15001508
def test_redo_locations
15011509
node = ast_parse("loop { redo }")
15021510
assert_locations(node.children[-1].children[-1].children[-1].locations, [[1, 7, 1, 11], [1, 7, 1, 11]])

0 commit comments

Comments
 (0)