summaryrefslogtreecommitdiff
path: root/ext/ripper/tools
diff options
context:
space:
mode:
Diffstat (limited to 'ext/ripper/tools')
-rwxr-xr-xext/ripper/tools/generate.rb122
1 files changed, 52 insertions, 70 deletions
diff --git a/ext/ripper/tools/generate.rb b/ext/ripper/tools/generate.rb
index 545bc81af6..0efb997604 100755
--- a/ext/ripper/tools/generate.rb
+++ b/ext/ripper/tools/generate.rb
@@ -10,8 +10,8 @@ def main
output = nil
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 eventids1") {|m|
+ parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--output=PATH]"
+ parser.on('--mode=MODE', 'check, eventids1, or eventids2table.') {|m|
mode = m
}
parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path|
@@ -20,9 +20,6 @@ def main
parser.on('--ids2src=PATH', 'A source file of event-IDs 2 (eventids2.c).') {|path|
ids2src = path
}
- parser.on('--template=PATH', 'A template file of ripper/core.rb.') {|path|
- template = path
- }
parser.on('--output=PATH', 'An output file.') {|path|
output = path
}
@@ -37,20 +34,23 @@ def main
end
usage 'no mode given' unless mode
case mode
- when 'ripper/core'
+ when 'check'
usage 'no --ids1src' unless ids1src
usage 'no --ids2src' unless ids2src
- usage 'no --template' unless template
- ids1 = read_ids1(ids1src)
+ h = read_ids1_with_locations(ids1src)
+ check_arity h
ids2 = read_ids2(ids2src)
- diff = ids1.map {|id, *| id} & ids2
- unless diff.empty?
- abort "event crash: #{diff.join(' ')}"
+ common = h.keys & ids2
+ unless common.empty?
+ abort "event crash: #{common.join(' ')}"
end
- result = generate_ripper_core(template, ids1, ids2)
+ exit 0
when 'eventids1'
usage 'no --ids1src' unless ids1src
result = generate_eventids1(read_ids1(ids1src))
+ when 'eventids2table'
+ usage 'no --ids2src' unless ids2src
+ result = generate_eventids2_table(read_ids2(ids2src))
end
if output
File.open(output, 'w') {|f|
@@ -67,69 +67,52 @@ def usage(msg)
exit false
end
-def generate_ripper_core(template, ids1, ids2)
- str = <<header
-# This file is automatically generated from #{File.basename(template)} and parse.y.
-# DO NOT MODIFY!!!!!!
-
-header
- File.foreach(template) do |line|
- case line
- when /\A\#include ids1/
- str << ids1.map {|id, arity|
- " #{id.intern.inspect} => #{arity}"
- }.join(",\n") << "\n"
- when /\A\#include ids2/
- str << ids2.map {|id|
- " #{id.intern.inspect} => 1"
- }.join(",\n") << "\n"
- when /\A\#include handlers1/
- ids1.each do |id, arity|
- str << $/
- str << " def on_#{id}#{paramdecl(arity)}" << $/
- str << " #{arity == 0 ? 'nil' : 'a'}" << $/
- str << " end" << $/
- end
- when /\A\#include handlers2/
- ids2.each do |id|
- str << $/
- str << " def on_#{id}(token)" << $/
- str << " token" << $/
- str << " end" << $/
- end
- when /\A\#include (.*)/
- raise "unknown operation: #include #{$1}"
- else
- str << line
- end
- end
- str
-end
-
-def paramdecl(n)
- return '' if n == 0
- '(' + ('a'..'z').to_a[0, n].join(', ') + ')'
-end
-
def generate_eventids1(ids)
- str = ""
+ buf = ""
ids.each do |id, arity|
- str << "static ID ripper_id_#{id};" << $/
+ buf << %Q[static ID ripper_id_#{id};\n]
end
- str << $/
- str << 'static void' << $/
- str << 'ripper_init_eventids1()' << $/
- str << '{' << $/
+ buf << %Q[\n]
+ buf << %Q[static void\n]
+ buf << %Q[ripper_init_eventids1(VALUE self)\n]
+ buf << %Q[{\n]
+ buf << %Q[ VALUE h;\n]
+ buf << %Q[ ID id;\n]
ids.each do |id, arity|
- str << %Q[ ripper_id_#{id} = rb_intern("on_#{id}");] << $/
+ buf << %Q[ ripper_id_#{id} = rb_intern("on_#{id}");\n]
end
- str << '}' << $/
- str
+ buf << %Q[\n]
+ buf << %Q[ h = rb_hash_new();\n]
+ buf << %Q[ rb_define_const(self, "PARSER_EVENT_TABLE", h);\n]
+ ids.each do |id, arity|
+ buf << %Q[ id = rb_intern("#{id}");\n]
+ buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(#{arity}));\n]
+ end
+ buf << %Q[}\n]
+ buf
+end
+
+def generate_eventids2_table(ids)
+ buf = ""
+ buf << %Q[static void\n]
+ buf << %Q[ripper_init_eventids2_table(VALUE self)\n]
+ buf << %Q[{\n]
+ buf << %Q[ VALUE h = rb_hash_new();\n]
+ buf << %Q[ ID id;\n]
+ buf << %Q[ rb_define_const(self, "SCANNER_EVENT_TABLE", h);\n]
+ ids.each do |id|
+ buf << %Q[ id = rb_intern("#{id}");\n]
+ buf << %Q[ rb_hash_aset(h, ID2SYM(id), INT2NUM(1));\n]
+ end
+ buf << %Q[}\n]
+ buf
end
def read_ids1(path)
- h = read_ids1_with_locations(path)
- check_arity h
+ strip_locations(read_ids1_with_locations(path))
+end
+
+def strip_locations(h)
h.map {|event, list| [event, list.first[1]] }\
.sort_by {|event, arity| event.to_s }
end
@@ -139,9 +122,8 @@ 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}]: #{ # "
- list.map {|line,a| "#{line}:#{a}" }.join(', ')
- }" # "
+ locations = list.map {|line, a| "#{line}:#{a}" }.join(', ')
+ $stderr.puts "arity crash [event=#{event}]: #{locations}"
end
end
abort if invalid