summaryrefslogtreecommitdiff
path: root/parse.y
diff options
context:
space:
mode:
authorydah <t.yudai92@gmail.com>2024-11-03 23:52:50 +0900
committerYudai Takada <t.yudai92@gmail.com>2025-03-08 18:26:40 +0900
commiteae0fe37c08b568c0a7cbf904caba4faca517746 (patch)
tree166c20e6046a3be3f5428eda36b848ea0fae0a98 /parse.y
parent98790faae3cbbe67a5335df30f6e9000f3a83ad9 (diff)
Implement CLASS 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 "class A < B; end" @ ProgramNode (location: (1,0)-(1,16)) +-- locals: [] +-- statements: @ StatementsNode (location: (1,0)-(1,16)) +-- body: (length: 1) +-- @ ClassNode (location: (1,0)-(1,16)) +-- locals: [] +-- class_keyword_loc: (1,0)-(1,5) = "class" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-- constant_path: | @ ConstantReadNode (location: (1,6)-(1,7)) | +-- name: :A +-- inheritance_operator_loc: (1,8)-(1,9) = "<" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-- superclass: | @ ConstantReadNode (location: (1,10)-(1,11)) | +-- name: :B +-- body: nil +-- end_keyword_loc: (1,13)-(1,16) = "end" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-- name: :A ```
Diffstat (limited to 'parse.y')
-rw-r--r--parse.y16
1 files changed, 12 insertions, 4 deletions
diff --git a/parse.y b/parse.y
index a22ebbbb99..4bca901fc3 100644
--- a/parse.y
+++ b/parse.y
@@ -1143,7 +1143,7 @@ static rb_node_defs_t *rb_node_defs_new(struct parser_params *p, NODE *nd_recv,
static rb_node_alias_t *rb_node_alias_new(struct parser_params *p, NODE *nd_1st, NODE *nd_2nd, const YYLTYPE *loc, const YYLTYPE *keyword_loc);
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);
static rb_node_undef_t *rb_node_undef_new(struct parser_params *p, NODE *nd_undef, const YYLTYPE *loc);
-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);
+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);
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_colon2_t *rb_node_colon2_new(struct parser_params *p, NODE *nd_head, ID nd_mid, const YYLTYPE *loc);
@@ -1251,7 +1251,7 @@ static rb_node_error_t *rb_node_error_new(struct parser_params *p, const YYLTYPE
#define NEW_ALIAS(n,o,loc,k_loc) (NODE *)rb_node_alias_new(p,n,o,loc,k_loc)
#define NEW_VALIAS(n,o,loc,k_loc) (NODE *)rb_node_valias_new(p,n,o,loc,k_loc)
#define NEW_UNDEF(i,loc) (NODE *)rb_node_undef_new(p,i,loc)
-#define NEW_CLASS(n,b,s,loc) (NODE *)rb_node_class_new(p,n,b,s,loc)
+#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)
#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_COLON2(c,i,loc) (NODE *)rb_node_colon2_new(p,c,i,loc)
@@ -4555,7 +4555,12 @@ primary : inline_primary
bodystmt
k_end
{
- $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$);
+ YYLTYPE inheritance_operator_loc = NULL_LOC;
+ if ($superclass) {
+ inheritance_operator_loc = @superclass;
+ inheritance_operator_loc.end_pos.column = inheritance_operator_loc.beg_pos.column + 1;
+ }
+ $$ = NEW_CLASS($cpath, $bodystmt, $superclass, &@$, &@k_class, &inheritance_operator_loc, &@k_end);
nd_set_line(RNODE_CLASS($$)->nd_body, @k_end.end_pos.lineno);
set_line_body($bodystmt, @superclass.end_pos.lineno);
nd_set_line($$, @superclass.end_pos.lineno);
@@ -11440,7 +11445,7 @@ rb_node_unless_new(struct parser_params *p, NODE *nd_cond, NODE *nd_body, NODE *
}
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)
+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)
{
/* Keep the order of node creation */
NODE *scope = NEW_SCOPE(0, nd_body, loc);
@@ -11448,6 +11453,9 @@ rb_node_class_new(struct parser_params *p, NODE *nd_cpath, NODE *nd_body, NODE *
n->nd_cpath = nd_cpath;
n->nd_body = scope;
n->nd_super = nd_super;
+ n->class_keyword_loc = *class_keyword_loc;
+ n->inheritance_operator_loc = *inheritance_operator_loc;
+ n->end_keyword_loc = *end_keyword_loc;
return n;
}