summaryrefslogtreecommitdiff
path: root/ext/ripper/tools/generate.rb
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-23 11:10:59 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-09-23 11:10:59 +0000
commit2d683b3190f60e82ed791381d21c2a3530beb824 (patch)
tree1e3baeaa92f2d06ba1c328b15fc9506e95d63837 /ext/ripper/tools/generate.rb
parent5d71328bca0ebb29859d574d204d74c499030232 (diff)
* 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
Diffstat (limited to 'ext/ripper/tools/generate.rb')
-rwxr-xr-xext/ripper/tools/generate.rb84
1 files changed, 38 insertions, 46 deletions
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 <<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
}