1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* ext/ripper: no longer generates .rb files.

* parse.y (Init_ripper): ripper_init_eventids*() takes 1 argument, self (class Ripper).
* ext/ripper/depend: target removed: `lib/ripper/core.rb'.
* ext/ripper/depend: new target `eventids2table.c'.
* ext/ripper/depend: new target `check'.
* ext/ripper/eventids2.c: include eventids2table.c.
* ext/ripper/eventids2.c: initialize SCANNER_EVENT_TABLE.
* ext/ripper/extconf.rb: update $cleanfiles list.
* ext/ripper/tools/generate.rb: no longer generate ripper/core.rb.
* ext/ripper/tools/generate.rb: new mode `check'.
* ext/ripper/tools/generate.rb: new mode `eventids2table'.
* ext/ripper/lib/ripper/core.rb.in: removed.
* ext/ripper/lib/ripper/core.rb: added.
* ext/ripper/lib/ripper/filter.rb: update copyright year.
* ext/ripper/lib/ripper/lexer.rb: ditto.
* ext/ripper/lib/ripper/sexp.rb: ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9284 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
aamine 2005-09-23 21:37:38 +00:00
parent 2d683b3190
commit 1d62cc0ecc
10 changed files with 141 additions and 112 deletions

View file

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