1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
nobu 2005-09-23 11:10:59 +00:00
parent 5d71328bca
commit 2d683b3190
5 changed files with 69 additions and 72 deletions

View file

@ -1,3 +1,8 @@
Fri Sep 23 20:10:35 2005 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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 <masaki.suketa@nifty.ne.jp> Fri Sep 23 17:36:48 2005 Masaki Suketa <masaki.suketa@nifty.ne.jp>
* ext/win32ole/win32ole.c: avoid core dump with WIN32OLE_EVENT. * ext/win32ole/win32ole.c: avoid core dump with WIN32OLE_EVENT.

View file

@ -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 ripper.o: ripper.c eventids1.c $(srcdir)/eventids2.c $(hdrdir)/lex.c
.y.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 ripper.y: $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y
$(RUBY) $(srcdir)/tools/preproc.rb $(hdrdir)/parse.y --output=$@ $(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 $(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 eventids1.c: $(srcdir)/tools/generate.rb $(hdrdir)/parse.y

View file

@ -6,8 +6,8 @@ require 'rbconfig'
def main def main
unless find_executable('bison') unless find_executable('bison')
unless File.exist?('ripper.c') or File.exist?("#{$srcdir}/ripper.c") unless File.exist?('ripper.c') or File.exist?("#{$srcdir}/ripper.c")
$stderr.puts 'missing bison; abort' Logging.message 'missing bison; abort'
exit 1 return
end end
end end
$objs = %w(ripper.o) $objs = %w(ripper.o)

View file

@ -1,6 +1,5 @@
# $Id$ # $Id$
require 'stringio'
require 'optparse' require 'optparse'
def main def main
@ -12,7 +11,7 @@ def main
parser = @parser = OptionParser.new parser = @parser = OptionParser.new
parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--template=PATH] [--output=PATH]" 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 mode = m
} }
parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path| 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.') { parser.on('--help', 'Prints this message and quit.') {
puts parser.help puts parser.help
exit 0 exit true
} }
begin begin
parser.parse! parser.parse!
@ -38,18 +37,18 @@ def main
end end
usage 'no mode given' unless mode usage 'no mode given' unless mode
case mode case mode
when 'ripper/core', 'ripper/core.rb' when 'ripper/core'
usage 'no --ids1src' unless ids1src usage 'no --ids1src' unless ids1src
usage 'no --ids2src' unless ids2src usage 'no --ids2src' unless ids2src
usage 'no --template' unless template usage 'no --template' unless template
ids1 = read_ids1(ids1src) ids1 = read_ids1(ids1src)
ids2 = read_ids2(ids2src) ids2 = read_ids2(ids2src)
unless (ids1.keys & ids2).empty? diff = ids1.map {|id, *| id} & ids2
$stderr.puts "event crash: #{(ids1.keys & ids2).join(' ')}" unless diff.empty?
exit 1 abort "event crash: #{diff.join(' ')}"
end end
result = generate_ripper_core(template, ids1, ids2) result = generate_ripper_core(template, ids1, ids2)
when 'eventids1', 'eventids1.c' when 'eventids1'
usage 'no --ids1src' unless ids1src usage 'no --ids1src' unless ids1src
result = generate_eventids1(read_ids1(ids1src)) result = generate_eventids1(read_ids1(ids1src))
end end
@ -65,12 +64,11 @@ end
def usage(msg) def usage(msg)
$stderr.puts msg $stderr.puts msg
$stderr.puts @parser.help $stderr.puts @parser.help
exit 1 exit false
end end
def generate_ripper_core(template, ids1, ids2) def generate_ripper_core(template, ids1, ids2)
f = StringIO.new str = <<header
f.print <<header
# This file is automatically generated from #{File.basename(template)} and parse.y. # This file is automatically generated from #{File.basename(template)} and parse.y.
# DO NOT MODIFY!!!!!! # DO NOT MODIFY!!!!!!
@ -78,40 +76,34 @@ header
File.foreach(template) do |line| File.foreach(template) do |line|
case line case line
when /\A\#include ids1/ when /\A\#include ids1/
comma = '' str << ids1.map {|id, arity|
ids1.each do |id, arity| " #{id.intern.inspect} => #{arity}"
f.print comma; comma = ",\n" }.join(",\n") << "\n"
f.print " #{id.intern.inspect} => #{arity}"
end
f.puts
when /\A\#include ids2/ when /\A\#include ids2/
comma = '' str << ids2.map {|id|
ids2.each do |id| " #{id.intern.inspect} => 1"
f.print comma; comma = ",\n" }.join(",\n") << "\n"
f.print " #{id.intern.inspect} => 1"
end
f.puts
when /\A\#include handlers1/ when /\A\#include handlers1/
ids1.each do |id, arity| ids1.each do |id, arity|
f.puts str << $/
f.puts " def on_#{id}#{paramdecl(arity)}" str << " def on_#{id}#{paramdecl(arity)}" << $/
f.puts " #{arity == 0 ? 'nil' : 'a'}" str << " #{arity == 0 ? 'nil' : 'a'}" << $/
f.puts " end" str << " end" << $/
end end
when /\A\#include handlers2/ when /\A\#include handlers2/
ids2.each do |id| ids2.each do |id|
f.puts str << $/
f.puts " def on_#{id}(token)" str << " def on_#{id}(token)" << $/
f.puts " token" str << " token" << $/
f.puts " end" str << " end" << $/
end end
when /\A\#include (.*)/ when /\A\#include (.*)/
raise "unknown operation: #include #{$1}" raise "unknown operation: #include #{$1}"
else else
f.print line str << line
end end
end end
f.string str
end end
def paramdecl(n) def paramdecl(n)
@ -120,19 +112,19 @@ def paramdecl(n)
end end
def generate_eventids1(ids) def generate_eventids1(ids)
f = StringIO.new str = ""
ids.each do |id, arity| ids.each do |id, arity|
f.puts "static ID ripper_id_#{id};" str << "static ID ripper_id_#{id};" << $/
end end
f.puts str << $/
f.puts 'static void' str << 'static void' << $/
f.puts 'ripper_init_eventids1()' str << 'ripper_init_eventids1()' << $/
f.puts '{' str << '{' << $/
ids.each do |id, arity| 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 end
f.puts '}' str << '}' << $/
f.string str
end end
def read_ids1(path) def read_ids1(path)
@ -147,12 +139,12 @@ def check_arity(h)
h.each do |event, list| h.each do |event, list|
unless list.map {|line, arity| arity }.uniq.size == 1 unless list.map {|line, arity| arity }.uniq.size == 1
invalid = true invalid = true
$stderr.puts "arity crash [event=#{event}]: #{ $stderr.puts "arity crash [event=#{event}]: #{ # "
list.map {|line,a| "#{line}:#{a}" }.join(', ') list.map {|line,a| "#{line}:#{a}" }.join(', ')
}" }" # "
end end
end end
exit 1 if invalid abort if invalid
end end
def read_ids1_with_locations(path) 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 /\A\#\s*define\s+s?dispatch/ =~ line
next if /ripper_dispatch/ =~ line next if /ripper_dispatch/ =~ line
line.scan(/dispatch(\d)\((\w+)/) do |arity, event| line.scan(/dispatch(\d)\((\w+)/) do |arity, event|
(h[event] ||= []).push [f.lineno, arity] (h[event] ||= []).push [f.lineno, arity.to_i]
end end
end end
} }

View file

@ -1,6 +1,5 @@
# $Id$ # $Id$
require 'stringio'
require 'optparse' require 'optparse'
def main def main
@ -12,20 +11,19 @@ def main
} }
parser.on('--help', 'Prints this message and quit.') { parser.on('--help', 'Prints this message and quit.') {
puts parser.help puts parser.help
exit 0 exit true
} }
begin begin
parser.parse! parser.parse!
rescue OptionParser::ParseError => err rescue OptionParser::ParseError => err
$stderr.puts err.message $stderr.puts err.message
$stderr.puts parser.help $stderr.puts parser.help
exit 1 exit false
end end
unless ARGV.size == 1 unless ARGV.size == 1
$stderr.puts "wrong number of arguments (#{ARGV.size} for 1)" abort "wrong number of arguments (#{ARGV.size} for 1)"
exit 1
end end
out = StringIO.new out = ""
File.open(ARGV[0]) {|f| File.open(ARGV[0]) {|f|
prelude f, out prelude f, out
grammar f, out grammar f, out
@ -33,10 +31,10 @@ def main
} }
if output if output
File.open(output, 'w') {|f| File.open(output, 'w') {|f|
f.write out.string f.write out
} }
else else
print out.string print out
end end
end end
@ -44,20 +42,20 @@ def prelude(f, out)
while line = f.gets while line = f.gets
case line case line
when %r</\*%%%\*/> when %r</\*%%%\*/>
out.puts '/*' out << '/*' << $/
when %r</\*%> when %r</\*%>
out.puts '*/' out << '*/' << $/
when %r<%\*/> when %r<%\*/>
out.puts out << $/
when /\A%%/ when /\A%%/
out.puts '%%' out << '%%' << $/
return return
when /\A%token/ when /\A%token/
out.puts line.sub(/<\w+>/, '<val>') out << line.sub(/<\w+>/, '<val>') << $/
when /\A%type/ when /\A%type/
out.puts line.sub(/<\w+>/, '<val>') out << line.sub(/<\w+>/, '<val>') << $/
else else
out.print line out << line
end end
end end
end end
@ -66,27 +64,27 @@ def grammar(f, out)
while line = f.gets while line = f.gets
case line case line
when %r</\*%%%\*/> when %r</\*%%%\*/>
out.puts '#if 0' out << '#if 0' << $/
when %r</\*%c%\*/> when %r</\*%c%\*/>
out.puts '/*' out << '/*' << $/
when %r</\*%c> when %r</\*%c>
out.puts '*/' out << '*/' << $/
when %r</\*%> when %r</\*%>
out.puts '#endif' out << '#endif' << $/
when %r<%\*/> when %r<%\*/>
out.puts out << $/
when /\A%%/ when /\A%%/
out.puts '%%' out << '%%' << $/
return return
else else
out.print line out << line
end end
end end
end end
def usercode(f, out) def usercode(f, out)
while line = f.gets while line = f.gets
out.print line out << line
end end
end end