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>
* 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
.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

View file

@ -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)

View file

@ -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 <<header
str = <<header
# This file is automatically generated from #{File.basename(template)} and parse.y.
# DO NOT MODIFY!!!!!!
@ -78,40 +76,34 @@ header
File.foreach(template) do |line|
case line
when /\A\#include ids1/
comma = ''
ids1.each do |id, arity|
f.print comma; comma = ",\n"
f.print " #{id.intern.inspect} => #{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
}

View file

@ -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+>/, '<val>')
out << line.sub(/<\w+>/, '<val>') << $/
when /\A%type/
out.puts line.sub(/<\w+>/, '<val>')
out << line.sub(/<\w+>/, '<val>') << $/
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</\*%c%\*/>
out.puts '/*'
out << '/*' << $/
when %r</\*%c>
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