summaryrefslogtreecommitdiff
path: root/ext/ripper/tools
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ripper/tools')
-rwxr-xr-xext/ripper/tools/generate.rb84
-rwxr-xr-xext/ripper/tools/preproc.rb44
2 files changed, 59 insertions, 69 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
}
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+>/, '<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