ruby-changes:36074
From: nobu <ko1@a...>
Date: Mon, 27 Oct 2014 15:38:23 +0900 (JST)
Subject: [ruby-changes:36074] nobu:r48155 (trunk): ripper/tools/generate.rb: shorten generated source
nobu 2014-10-27 15:38:09 +0900 (Mon, 27 Oct 2014) New Revision: 48155 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48155 Log: 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. Modified files: trunk/ext/ripper/tools/generate.rb trunk/parse.y Index: parse.y =================================================================== --- parse.y (revision 48154) +++ parse.y (revision 48155) @@ -540,6 +540,8 @@ static int lvar_defined_gen(struct parse https://github.com/ruby/ruby/blob/trunk/parse.y#L540 #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) https://github.com/ruby/ruby/blob/trunk/parse.y#L5133 #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) { Index: ext/ripper/tools/generate.rb =================================================================== --- ext/ripper/tools/generate.rb (revision 48154) +++ ext/ripper/tools/generate.rb (revision 48155) @@ -68,15 +68,22 @@ end https://github.com/ruby/ruby/blob/trunk/ext/ripper/tools/generate.rb#L68 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) https://github.com/ruby/ruby/blob/trunk/ext/ripper/tools/generate.rb#L91 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) https://github.com/ruby/ruby/blob/trunk/ext/ripper/tools/generate.rb#L105 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) https://github.com/ruby/ruby/blob/trunk/ext/ripper/tools/generate.rb#L138 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 https://github.com/ruby/ruby/blob/trunk/ext/ripper/tools/generate.rb#L150 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/