Skip to content

Commit f3df218

Browse files
Introduced rb_node_const_decl_val function
Introduce `rb_node_const_decl_val` function to allow `rb_ary_join` and `rb_ary_reverse` functions to be removed from Universal Parser.
1 parent 8041b7d commit f3df218

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

common.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16081,6 +16081,7 @@ ruby_parser.$(OBJEXT): {$(VPATH)}internal/variable.h
1608116081
ruby_parser.$(OBJEXT): {$(VPATH)}internal/warning_push.h
1608216082
ruby_parser.$(OBJEXT): {$(VPATH)}internal/xmalloc.h
1608316083
ruby_parser.$(OBJEXT): {$(VPATH)}missing.h
16084+
ruby_parser.$(OBJEXT): {$(VPATH)}node.h
1608416085
ruby_parser.$(OBJEXT): {$(VPATH)}onigmo.h
1608516086
ruby_parser.$(OBJEXT): {$(VPATH)}oniguruma.h
1608616087
ruby_parser.$(OBJEXT): {$(VPATH)}ruby_assert.h

internal/ruby_parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ VALUE rb_node_sym_string_val(const NODE *);
8080
VALUE rb_node_line_lineno_val(const NODE *);
8181
VALUE rb_node_file_path_val(const NODE *);
8282
VALUE rb_node_encoding_val(const NODE *);
83+
VALUE rb_node_const_decl_val(const NODE *node);
8384

8485
VALUE rb_node_integer_literal_val(const NODE *);
8586
VALUE rb_node_float_literal_val(const NODE *);

parse.y

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13991,31 +13991,7 @@ const_decl_path(struct parser_params *p, NODE **dest)
1399113991
NODE *n = *dest;
1399213992
if (!nd_type_p(n, NODE_CALL)) {
1399313993
const YYLTYPE *loc = &n->nd_loc;
13994-
VALUE path;
13995-
if (RNODE_CDECL(n)->nd_vid) {
13996-
path = rb_id2str(RNODE_CDECL(n)->nd_vid);
13997-
}
13998-
else {
13999-
n = RNODE_CDECL(n)->nd_else;
14000-
path = rb_ary_new();
14001-
for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
14002-
rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
14003-
}
14004-
if (n && nd_type_p(n, NODE_CONST)) {
14005-
// Const::Name
14006-
rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
14007-
}
14008-
else if (n && nd_type_p(n, NODE_COLON3)) {
14009-
// ::Const::Name
14010-
rb_ary_push(path, rb_str_new(0, 0));
14011-
}
14012-
else {
14013-
// expression::Name
14014-
rb_ary_push(path, rb_str_new_cstr("..."));
14015-
}
14016-
path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
14017-
path = rb_fstring(path);
14018-
}
13994+
VALUE path = rb_node_const_decl_val(n);
1401913995
*dest = n = NEW_LIT(path, loc);
1402013996
RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
1402113997
}

ruby_parser.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "internal/ruby_parser.h"
44

5+
#include "node.h"
56
#include "rubyparser.h"
67
#include "internal/error.h"
78

@@ -26,7 +27,6 @@
2627
#include "ruby/ractor.h"
2728
#include "ruby/ruby.h"
2829
#include "ruby/util.h"
29-
#include "node.h"
3030
#include "internal.h"
3131
#include "vm_core.h"
3232
#include "symbol.h"
@@ -522,8 +522,6 @@ static const rb_parser_config_t rb_global_parser_config = {
522522
.ary_unshift = rb_ary_unshift,
523523
.ary_new2 = rb_ary_new2,
524524
.ary_entry = rb_ary_entry,
525-
.ary_join = rb_ary_join,
526-
.ary_reverse = rb_ary_reverse,
527525
.ary_clear = rb_ary_clear,
528526
.ary_modify = rb_ary_modify,
529527
.array_len = rb_array_len,
@@ -1014,3 +1012,34 @@ rb_node_encoding_val(const NODE *node)
10141012
{
10151013
return rb_enc_from_encoding(RNODE_ENCODING(node)->enc);
10161014
}
1015+
1016+
VALUE
1017+
rb_node_const_decl_val(const NODE *node)
1018+
{
1019+
VALUE path;
1020+
if (RNODE_CDECL(node)->nd_vid) {
1021+
path = rb_id2str(RNODE_CDECL(node)->nd_vid);
1022+
}
1023+
else {
1024+
NODE *n = RNODE_CDECL(node)->nd_else;
1025+
path = rb_ary_new();
1026+
for (; n && nd_type_p(n, NODE_COLON2); n = RNODE_COLON2(n)->nd_head) {
1027+
rb_ary_push(path, rb_id2str(RNODE_COLON2(n)->nd_mid));
1028+
}
1029+
if (n && nd_type_p(n, NODE_CONST)) {
1030+
// Const::Name
1031+
rb_ary_push(path, rb_id2str(RNODE_CONST(n)->nd_vid));
1032+
}
1033+
else if (n && nd_type_p(n, NODE_COLON3)) {
1034+
// ::Const::Name
1035+
rb_ary_push(path, rb_str_new(0, 0));
1036+
}
1037+
else {
1038+
// expression::Name
1039+
rb_ary_push(path, rb_str_new_cstr("..."));
1040+
}
1041+
path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
1042+
path = rb_fstring(path);
1043+
}
1044+
return path;
1045+
}

rubyparser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,6 @@ typedef struct rb_parser_config_struct {
12431243
VALUE (*ary_unshift)(VALUE ary, VALUE item);
12441244
VALUE (*ary_new2)(long capa); // ary_new_capa
12451245
VALUE (*ary_entry)(VALUE ary, long offset);
1246-
VALUE (*ary_join)(VALUE ary, VALUE sep);
1247-
VALUE (*ary_reverse)(VALUE ary);
12481246
VALUE (*ary_clear)(VALUE ary);
12491247
void (*ary_modify)(VALUE ary);
12501248
long (*array_len)(VALUE a);

universal_parser.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,6 @@ struct rb_imemo_tmpbuf_struct {
144144
#undef rb_ary_new2
145145
#define rb_ary_new2 p->config->ary_new2
146146
#define rb_ary_entry p->config->ary_entry
147-
#define rb_ary_join p->config->ary_join
148-
#define rb_ary_reverse p->config->ary_reverse
149147
#define rb_ary_clear p->config->ary_clear
150148
#define rb_ary_modify p->config->ary_modify
151149
#undef RARRAY_LEN

0 commit comments

Comments
 (0)