From 92c61ca747e319d76884960643ef098734040fd3 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 27 Oct 2014 06:38:09 +0000 Subject: [PATCH] ripper/tools/generate.rb: shorten generated source * ext/ripper/tools/generate.rb (generate_eventids1): shorten generated source. * ext/ripper/tools/generate.rb (generate_eventids2): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/ripper/tools/generate.rb | 25 ++++++++++++++----------- parse.y | 9 +++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/ext/ripper/tools/generate.rb b/ext/ripper/tools/generate.rb index 48ad9e1d25..fc30e775bb 100755 --- a/ext/ripper/tools/generate.rb +++ b/ext/ripper/tools/generate.rb @@ -68,15 +68,22 @@ end def generate_eventids1(ids) buf = "" + buf << %Q[static struct {\n] ids.each do |id, arity| - buf << %Q[static ID ripper_id_#{id};\n] + buf << %Q[ ID id_#{id};\n] + end + buf << %Q[} ripper_parser_ids;\n] + buf << %Q[\n] + ids.each do |id, arity| + buf << %Q[#define ripper_id_#{id} ripper_parser_ids.id_#{id}\n] end buf << %Q[\n] buf << %Q[static void\n] buf << %Q[ripper_init_eventids1(void)\n] buf << %Q[{\n] + buf << %Q[#define set_id1(name) ripper_id_##name = rb_intern_const("on_"#name)\n] ids.each do |id, arity| - buf << %Q[ ripper_id_#{id} = rb_intern_const("on_#{id}");\n] + buf << %Q[ set_id1(#{id});\n] end buf << %Q[}\n] buf << %Q[\n] @@ -84,11 +91,9 @@ def generate_eventids1(ids) buf << %Q[ripper_init_eventids1_table(VALUE self)\n] buf << %Q[{\n] buf << %Q[ VALUE h = rb_hash_new();\n] - buf << %Q[ ID id;\n] buf << %Q[ rb_define_const(self, "PARSER_EVENT_TABLE", h);\n] ids.each do |id, arity| - buf << %Q[ id = rb_intern_const("#{id}");\n] - buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(#{arity}));\n] + buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(#{arity}));\n] end buf << %Q[}\n] buf @@ -100,11 +105,9 @@ def generate_eventids2_table(ids) buf << %Q[ripper_init_eventids2_table(VALUE self)\n] buf << %Q[{\n] buf << %Q[ VALUE h = rb_hash_new();\n] - buf << %Q[ ID id;\n] buf << %Q[ rb_define_const(self, "SCANNER_EVENT_TABLE", h);\n] ids.each do |id| - buf << %Q[ id = rb_intern_const("#{id}");\n] - buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(1));\n] + buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(1));\n] end buf << %Q[}\n] buf @@ -135,9 +138,9 @@ def read_ids1_with_locations(path) h = {} File.open(path) {|f| f.each do |line| - next if /\A\#\s*define\s+s?dispatch/ =~ line + next if /\A\#\s*define\s+dispatch/ =~ line next if /ripper_dispatch/ =~ line - line.scan(/dispatch(\d)\((\w+)/) do |arity, event| + line.scan(/\bdispatch(\d)\((\w+)/) do |arity, event| (h[event] ||= []).push [f.lineno, arity.to_i] end end @@ -147,7 +150,7 @@ end def read_ids2(path) File.open(path) {|f| - return f.read.scan(/ripper_id_(\w+)/).flatten.uniq.sort + return f.read.scan(/ID\s+ripper_id_(\w+)/).flatten.uniq.sort } end diff --git a/parse.y b/parse.y index 87866c029f..e4f0d2fe1b 100644 --- a/parse.y +++ b/parse.y @@ -540,6 +540,8 @@ static int lvar_defined_gen(struct parser_params*, ID); #ifdef RIPPER #define RIPPER_VERSION "0.1.0" +static inline VALUE intern_sym(const char *name); + #include "eventids1.c" #include "eventids2.c" @@ -5131,6 +5133,13 @@ ripper_yylval_id(ID x) #define yylval_rval (*(RB_TYPE_P(yylval.val, T_NODE) ? &yylval.node->nd_rval : &yylval.val)) +static inline VALUE +intern_sym(const char *name) +{ + ID id = rb_intern_const(name); + return ID2SYM(id); +} + static int ripper_has_scan_event(struct parser_params *parser) {