mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
30 lines
696 B
Ruby
30 lines
696 B
Ruby
|
#! /usr/bin/ruby -p
|
||
|
BEGIN {
|
||
|
require 'optparse'
|
||
|
vpath = ["."]
|
||
|
header = nil
|
||
|
|
||
|
opt = OptionParser.new do |o|
|
||
|
o.on('-v', '--vpath=DIR') {|dirs| vpath.concat dirs.split(File::PATH_SEPARATOR)}
|
||
|
header = o.order!(ARGV).shift
|
||
|
end or abort opt.opt_s
|
||
|
|
||
|
TOKENS = {}
|
||
|
vpath.find do |dir|
|
||
|
begin
|
||
|
h = File.read(File.join(dir, header))
|
||
|
rescue Errno::ENOENT
|
||
|
nil
|
||
|
else
|
||
|
h.scan(/^#define\s+RUBY_TOKEN_(\w+)\s+(\d+)/) do |token, id|
|
||
|
TOKENS[token] = id
|
||
|
end
|
||
|
true
|
||
|
end
|
||
|
end or abort "#{header} not found in #{vpath.inspect}"
|
||
|
|
||
|
TOKENS_RE = /\bRUBY_TOKEN\((#{TOKENS.keys.join('|')})\)\s*(?=\s)/
|
||
|
}
|
||
|
|
||
|
$_.gsub!(TOKENS_RE) {TOKENS[$1]} if /^%token/ =~ $_
|