From 2d683b3190f60e82ed791381d21c2a3530beb824 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 23 Sep 2005 11:10:59 +0000 Subject: [PATCH] * ext/ripper/tools/generate.rb, ext/ripper/tools/preproc.rb: StringIO is not available for miniruby. fixed: [ruby-dev:27307] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9283 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++ ext/ripper/depend | 4 +- ext/ripper/extconf.rb | 4 +- ext/ripper/tools/generate.rb | 84 ++++++++++++++++-------------------- ext/ripper/tools/preproc.rb | 44 +++++++++---------- 5 files changed, 69 insertions(+), 72 deletions(-) diff --git a/ChangeLog b/ChangeLog index d7030bb1a0..05c9b70b5d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Fri Sep 23 20:10:35 2005 Nobuyoshi Nakada + + * ext/ripper/tools/generate.rb, ext/ripper/tools/preproc.rb: StringIO + is not available for miniruby. fixed: [ruby-dev:27307] + Fri Sep 23 17:36:48 2005 Masaki Suketa * ext/win32ole/win32ole.c: avoid core dump with WIN32OLE_EVENT. diff --git a/ext/ripper/depend b/ext/ripper/depend index b18df1efd7..44b09f1e80 100644 --- a/ext/ripper/depend +++ b/ext/ripper/depend @@ -1,3 +1,5 @@ +src: ripper.c eventids1.c $(srcdir)/lib/ripper/core.rb + ripper.o: ripper.c eventids1.c $(srcdir)/eventids2.c $(hdrdir)/lex.c .y.c: @@ -6,7 +8,7 @@ ripper.o: ripper.c eventids1.c $(srcdir)/eventids2.c $(hdrdir)/lex.c ripper.y: $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y $(RUBY) $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y --output=$@ -$(srcdir)/lib/ripper/core.rb: $(srcdir)/tools/generate.rb $(srcdir)/lib/ripper/core.rb.in $(hdrdir)/parse.y $(hdrdir)/eventids2.c +$(srcdir)/lib/ripper/core.rb: $(srcdir)/tools/generate.rb $(srcdir)/lib/ripper/core.rb.in $(hdrdir)/parse.y $(srcdir)/eventids2.c $(RUBY) $(srcdir)/tools/generate.rb --mode=ripper/core --template=$@.in --output=$@ --ids1src=$(hdrdir)/parse.y --ids2src=$(srcdir)/eventids2.c eventids1.c: $(srcdir)/tools/generate.rb $(hdrdir)/parse.y diff --git a/ext/ripper/extconf.rb b/ext/ripper/extconf.rb index adf02cb3e4..069ed7dcc4 100644 --- a/ext/ripper/extconf.rb +++ b/ext/ripper/extconf.rb @@ -6,8 +6,8 @@ require 'rbconfig' def main unless find_executable('bison') unless File.exist?('ripper.c') or File.exist?("#{$srcdir}/ripper.c") - $stderr.puts 'missing bison; abort' - exit 1 + Logging.message 'missing bison; abort' + return end end $objs = %w(ripper.o) diff --git a/ext/ripper/tools/generate.rb b/ext/ripper/tools/generate.rb index e2282c2aec..545bc81af6 100755 --- a/ext/ripper/tools/generate.rb +++ b/ext/ripper/tools/generate.rb @@ -1,6 +1,5 @@ # $Id$ -require 'stringio' require 'optparse' def main @@ -12,7 +11,7 @@ def main parser = @parser = OptionParser.new parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--template=PATH] [--output=PATH]" - parser.on('--mode=MODE', '"ripper/core" or "eventids1".') {|m| + parser.on('--mode=MODE', %"ripper/core eventids1") {|m| mode = m } parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path| @@ -29,7 +28,7 @@ def main } parser.on('--help', 'Prints this message and quit.') { puts parser.help - exit 0 + exit true } begin parser.parse! @@ -38,18 +37,18 @@ def main end usage 'no mode given' unless mode case mode - when 'ripper/core', 'ripper/core.rb' + when 'ripper/core' usage 'no --ids1src' unless ids1src usage 'no --ids2src' unless ids2src usage 'no --template' unless template ids1 = read_ids1(ids1src) ids2 = read_ids2(ids2src) - unless (ids1.keys & ids2).empty? - $stderr.puts "event crash: #{(ids1.keys & ids2).join(' ')}" - exit 1 + diff = ids1.map {|id, *| id} & ids2 + unless diff.empty? + abort "event crash: #{diff.join(' ')}" end result = generate_ripper_core(template, ids1, ids2) - when 'eventids1', 'eventids1.c' + when 'eventids1' usage 'no --ids1src' unless ids1src result = generate_eventids1(read_ids1(ids1src)) end @@ -65,12 +64,11 @@ end def usage(msg) $stderr.puts msg $stderr.puts @parser.help - exit 1 + exit false end def generate_ripper_core(template, ids1, ids2) - f = StringIO.new - f.print <
#{arity}" - end - f.puts + str << ids1.map {|id, arity| + " #{id.intern.inspect} => #{arity}" + }.join(",\n") << "\n" when /\A\#include ids2/ - comma = '' - ids2.each do |id| - f.print comma; comma = ",\n" - f.print " #{id.intern.inspect} => 1" - end - f.puts + str << ids2.map {|id| + " #{id.intern.inspect} => 1" + }.join(",\n") << "\n" when /\A\#include handlers1/ ids1.each do |id, arity| - f.puts - f.puts " def on_#{id}#{paramdecl(arity)}" - f.puts " #{arity == 0 ? 'nil' : 'a'}" - f.puts " end" + str << $/ + str << " def on_#{id}#{paramdecl(arity)}" << $/ + str << " #{arity == 0 ? 'nil' : 'a'}" << $/ + str << " end" << $/ end when /\A\#include handlers2/ ids2.each do |id| - f.puts - f.puts " def on_#{id}(token)" - f.puts " token" - f.puts " end" + str << $/ + str << " def on_#{id}(token)" << $/ + str << " token" << $/ + str << " end" << $/ end when /\A\#include (.*)/ raise "unknown operation: #include #{$1}" else - f.print line + str << line end end - f.string + str end def paramdecl(n) @@ -120,19 +112,19 @@ def paramdecl(n) end def generate_eventids1(ids) - f = StringIO.new + str = "" ids.each do |id, arity| - f.puts "static ID ripper_id_#{id};" + str << "static ID ripper_id_#{id};" << $/ end - f.puts - f.puts 'static void' - f.puts 'ripper_init_eventids1()' - f.puts '{' + str << $/ + str << 'static void' << $/ + str << 'ripper_init_eventids1()' << $/ + str << '{' << $/ ids.each do |id, arity| - f.puts %Q[ ripper_id_#{id} = rb_intern("on_#{id}");] + str << %Q[ ripper_id_#{id} = rb_intern("on_#{id}");] << $/ end - f.puts '}' - f.string + str << '}' << $/ + str end def read_ids1(path) @@ -147,12 +139,12 @@ def check_arity(h) h.each do |event, list| unless list.map {|line, arity| arity }.uniq.size == 1 invalid = true - $stderr.puts "arity crash [event=#{event}]: #{ + $stderr.puts "arity crash [event=#{event}]: #{ # " list.map {|line,a| "#{line}:#{a}" }.join(', ') - }" + }" # " end end - exit 1 if invalid + abort if invalid end def read_ids1_with_locations(path) @@ -162,7 +154,7 @@ def read_ids1_with_locations(path) next if /\A\#\s*define\s+s?dispatch/ =~ line next if /ripper_dispatch/ =~ line line.scan(/dispatch(\d)\((\w+)/) do |arity, event| - (h[event] ||= []).push [f.lineno, arity] + (h[event] ||= []).push [f.lineno, arity.to_i] end end } diff --git a/ext/ripper/tools/preproc.rb b/ext/ripper/tools/preproc.rb index 4b26c5bcb0..bd3f33772c 100755 --- a/ext/ripper/tools/preproc.rb +++ b/ext/ripper/tools/preproc.rb @@ -1,6 +1,5 @@ # $Id$ -require 'stringio' require 'optparse' def main @@ -12,20 +11,19 @@ def main } parser.on('--help', 'Prints this message and quit.') { puts parser.help - exit 0 + exit true } begin parser.parse! rescue OptionParser::ParseError => err $stderr.puts err.message $stderr.puts parser.help - exit 1 + exit false end unless ARGV.size == 1 - $stderr.puts "wrong number of arguments (#{ARGV.size} for 1)" - exit 1 + abort "wrong number of arguments (#{ARGV.size} for 1)" end - out = StringIO.new + out = "" File.open(ARGV[0]) {|f| prelude f, out grammar f, out @@ -33,10 +31,10 @@ def main } if output File.open(output, 'w') {|f| - f.write out.string + f.write out } else - print out.string + print out end end @@ -44,20 +42,20 @@ def prelude(f, out) while line = f.gets case line when %r - out.puts '/*' + out << '/*' << $/ when %r - out.puts '*/' + out << '*/' << $/ when %r<%\*/> - out.puts + out << $/ when /\A%%/ - out.puts '%%' + out << '%%' << $/ return when /\A%token/ - out.puts line.sub(/<\w+>/, '') + out << line.sub(/<\w+>/, '') << $/ when /\A%type/ - out.puts line.sub(/<\w+>/, '') + out << line.sub(/<\w+>/, '') << $/ else - out.print line + out << line end end end @@ -66,27 +64,27 @@ def grammar(f, out) while line = f.gets case line when %r - out.puts '#if 0' + out << '#if 0' << $/ when %r - out.puts '/*' + out << '/*' << $/ when %r - out.puts '*/' + out << '*/' << $/ when %r - out.puts '#endif' + out << '#endif' << $/ when %r<%\*/> - out.puts + out << $/ when /\A%%/ - out.puts '%%' + out << '%%' << $/ return else - out.print line + out << line end end end def usercode(f, out) while line = f.gets - out.print line + out << line end end