2005-09-22 16:58:31 -04:00
|
|
|
# $Id$
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
|
|
|
|
def main
|
|
|
|
mode = nil
|
|
|
|
ids1src = nil
|
|
|
|
ids2src = nil
|
|
|
|
output = nil
|
|
|
|
|
|
|
|
parser = @parser = OptionParser.new
|
2005-09-23 17:37:38 -04:00
|
|
|
parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--output=PATH]"
|
|
|
|
parser.on('--mode=MODE', 'check, eventids1, or eventids2table.') {|m|
|
2005-09-22 16:58:31 -04:00
|
|
|
mode = m
|
|
|
|
}
|
|
|
|
parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path|
|
|
|
|
ids1src = path
|
|
|
|
}
|
|
|
|
parser.on('--ids2src=PATH', 'A source file of event-IDs 2 (eventids2.c).') {|path|
|
|
|
|
ids2src = path
|
|
|
|
}
|
|
|
|
parser.on('--output=PATH', 'An output file.') {|path|
|
|
|
|
output = path
|
|
|
|
}
|
|
|
|
parser.on('--help', 'Prints this message and quit.') {
|
|
|
|
puts parser.help
|
2005-09-23 07:10:59 -04:00
|
|
|
exit true
|
2005-09-22 16:58:31 -04:00
|
|
|
}
|
|
|
|
begin
|
|
|
|
parser.parse!
|
|
|
|
rescue OptionParser::ParseError => err
|
|
|
|
usage err.message
|
|
|
|
end
|
|
|
|
usage 'no mode given' unless mode
|
|
|
|
case mode
|
2005-09-23 17:37:38 -04:00
|
|
|
when 'check'
|
2005-09-22 16:58:31 -04:00
|
|
|
usage 'no --ids1src' unless ids1src
|
|
|
|
usage 'no --ids2src' unless ids2src
|
2005-09-23 17:37:38 -04:00
|
|
|
h = read_ids1_with_locations(ids1src)
|
|
|
|
check_arity h
|
2005-09-22 18:01:32 -04:00
|
|
|
ids2 = read_ids2(ids2src)
|
2005-09-23 17:37:38 -04:00
|
|
|
common = h.keys & ids2
|
|
|
|
unless common.empty?
|
|
|
|
abort "event crash: #{common.join(' ')}"
|
2005-09-22 18:01:32 -04:00
|
|
|
end
|
2005-09-23 17:37:38 -04:00
|
|
|
exit 0
|
2005-09-23 07:10:59 -04:00
|
|
|
when 'eventids1'
|
2005-09-22 16:58:31 -04:00
|
|
|
usage 'no --ids1src' unless ids1src
|
|
|
|
result = generate_eventids1(read_ids1(ids1src))
|
2005-09-23 17:37:38 -04:00
|
|
|
when 'eventids2table'
|
|
|
|
usage 'no --ids2src' unless ids2src
|
|
|
|
result = generate_eventids2_table(read_ids2(ids2src))
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
if output
|
|
|
|
File.open(output, 'w') {|f|
|
|
|
|
f.write result
|
|
|
|
}
|
|
|
|
else
|
|
|
|
puts result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage(msg)
|
|
|
|
$stderr.puts msg
|
|
|
|
$stderr.puts @parser.help
|
2005-09-23 07:10:59 -04:00
|
|
|
exit false
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_eventids1(ids)
|
2005-09-23 17:37:38 -04:00
|
|
|
buf = ""
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[static struct {\n]
|
2005-09-22 16:58:31 -04:00
|
|
|
ids.each do |id, arity|
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[ ID id_#{id};\n]
|
|
|
|
end
|
|
|
|
buf << %Q[} ripper_parser_ids;\n]
|
|
|
|
buf << %Q[\n]
|
|
|
|
ids.each do |id, arity|
|
|
|
|
buf << %Q[#define ripper_id_#{id} ripper_parser_ids.id_#{id}\n]
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
2005-09-23 17:37:38 -04:00
|
|
|
buf << %Q[\n]
|
|
|
|
buf << %Q[static void\n]
|
2011-10-18 10:05:51 -04:00
|
|
|
buf << %Q[ripper_init_eventids1(void)\n]
|
2005-09-23 17:37:38 -04:00
|
|
|
buf << %Q[{\n]
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[#define set_id1(name) ripper_id_##name = rb_intern_const("on_"#name)\n]
|
2005-09-22 16:58:31 -04:00
|
|
|
ids.each do |id, arity|
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[ set_id1(#{id});\n]
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
2011-10-18 10:05:51 -04:00
|
|
|
buf << %Q[}\n]
|
2005-09-23 17:37:38 -04:00
|
|
|
buf << %Q[\n]
|
2011-10-18 10:05:51 -04:00
|
|
|
buf << %Q[static void\n]
|
|
|
|
buf << %Q[ripper_init_eventids1_table(VALUE self)\n]
|
|
|
|
buf << %Q[{\n]
|
|
|
|
buf << %Q[ VALUE h = rb_hash_new();\n]
|
2005-09-23 17:37:38 -04:00
|
|
|
buf << %Q[ rb_define_const(self, "PARSER_EVENT_TABLE", h);\n]
|
|
|
|
ids.each do |id, arity|
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(#{arity}));\n]
|
2005-09-23 17:37:38 -04:00
|
|
|
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[ rb_define_const(self, "SCANNER_EVENT_TABLE", h);\n]
|
|
|
|
ids.each do |id|
|
2014-10-27 02:38:09 -04:00
|
|
|
buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(1));\n]
|
2005-09-23 17:37:38 -04:00
|
|
|
end
|
|
|
|
buf << %Q[}\n]
|
|
|
|
buf
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_ids1(path)
|
2005-09-23 17:37:38 -04:00
|
|
|
strip_locations(read_ids1_with_locations(path))
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip_locations(h)
|
2005-09-22 18:01:32 -04:00
|
|
|
h.map {|event, list| [event, list.first[1]] }\
|
|
|
|
.sort_by {|event, arity| event.to_s }
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
|
2005-09-22 18:01:32 -04:00
|
|
|
def check_arity(h)
|
2005-09-22 16:58:31 -04:00
|
|
|
invalid = false
|
2005-09-22 18:01:32 -04:00
|
|
|
h.each do |event, list|
|
|
|
|
unless list.map {|line, arity| arity }.uniq.size == 1
|
|
|
|
invalid = true
|
2005-09-23 17:37:38 -04:00
|
|
|
locations = list.map {|line, a| "#{line}:#{a}" }.join(', ')
|
|
|
|
$stderr.puts "arity crash [event=#{event}]: #{locations}"
|
2005-09-22 18:01:32 -04:00
|
|
|
end
|
|
|
|
end
|
2005-09-23 07:10:59 -04:00
|
|
|
abort if invalid
|
2005-09-22 18:01:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_ids1_with_locations(path)
|
|
|
|
h = {}
|
2005-09-22 16:58:31 -04:00
|
|
|
File.open(path) {|f|
|
2005-09-22 18:01:32 -04:00
|
|
|
f.each do |line|
|
2014-10-27 02:38:09 -04:00
|
|
|
next if /\A\#\s*define\s+dispatch/ =~ line
|
2005-09-22 18:01:32 -04:00
|
|
|
next if /ripper_dispatch/ =~ line
|
2014-10-27 02:38:09 -04:00
|
|
|
line.scan(/\bdispatch(\d)\((\w+)/) do |arity, event|
|
2005-09-23 07:10:59 -04:00
|
|
|
(h[event] ||= []).push [f.lineno, arity.to_i]
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2005-09-22 18:01:32 -04:00
|
|
|
h
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def read_ids2(path)
|
2014-10-27 22:53:24 -04:00
|
|
|
src = File.open(path) {|f| f.read}
|
|
|
|
ids2 = src.scan(/ID\s+ripper_id_(\w+)/).flatten.uniq.sort
|
|
|
|
diff = src.scan(/set_id2\((\w+)\);/).flatten - ids2
|
|
|
|
unless diff.empty?
|
|
|
|
abort "missing scanner IDs: #{diff}"
|
|
|
|
end
|
|
|
|
return ids2
|
2005-09-22 16:58:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
main
|