2015-11-10 06:48:14 -05:00
|
|
|
# frozen_string_literal: true
|
2015-10-05 09:27:48 -04:00
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
require 'optparse'
|
|
|
|
require 'erb'
|
2008-08-09 22:45:18 -04:00
|
|
|
require 'fileutils'
|
2010-03-14 13:01:06 -04:00
|
|
|
require 'pp'
|
|
|
|
|
|
|
|
class Array
|
|
|
|
unless [].respond_to? :product
|
|
|
|
def product(*args)
|
|
|
|
if args.empty?
|
|
|
|
self.map {|e| [e] }
|
|
|
|
else
|
|
|
|
result = []
|
|
|
|
self.each {|e0|
|
|
|
|
result.concat args.first.product(*args[1..-1]).map {|es| [e0, *es] }
|
|
|
|
}
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-18 06:53:38 -04:00
|
|
|
class String
|
|
|
|
unless "".respond_to? :start_with?
|
|
|
|
def start_with?(*prefixes)
|
|
|
|
prefixes.each {|prefix|
|
|
|
|
return true if prefix.length <= self.length && prefix == self[0, prefix.length]
|
|
|
|
}
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-09-01 13:06:07 -04:00
|
|
|
NUM_ELEM_BYTELOOKUP = 2
|
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
C_ESC = {
|
|
|
|
"\\" => "\\\\",
|
|
|
|
'"' => '\"',
|
|
|
|
"\n" => '\n',
|
|
|
|
}
|
|
|
|
|
|
|
|
0x00.upto(0x1f) {|ch| C_ESC[[ch].pack("C")] ||= "\\%03o" % ch }
|
|
|
|
0x7f.upto(0xff) {|ch| C_ESC[[ch].pack("C")] = "\\%03o" % ch }
|
|
|
|
C_ESC_PAT = Regexp.union(*C_ESC.keys)
|
|
|
|
|
|
|
|
def c_esc(str)
|
|
|
|
'"' + str.gsub(C_ESC_PAT) { C_ESC[$&] } + '"'
|
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
HEX2 = /(?:[0-9A-Fa-f]{2})/
|
2010-03-03 10:11:56 -05:00
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
class ArrayCode
|
|
|
|
def initialize(type, name)
|
|
|
|
@type = type
|
|
|
|
@name = name
|
|
|
|
@len = 0;
|
2015-10-05 09:27:48 -04:00
|
|
|
@content = ''.dup
|
2010-03-14 13:01:06 -04:00
|
|
|
end
|
2010-03-08 22:21:07 -05:00
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def length
|
|
|
|
@len
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def insert_at_last(num, str)
|
2018-05-17 20:38:00 -04:00
|
|
|
# newnum = self.length + num
|
2010-03-14 13:01:06 -04:00
|
|
|
@content << str
|
|
|
|
@len += num
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def to_s
|
|
|
|
<<"End"
|
|
|
|
static const #{@type}
|
|
|
|
#{@name}[#{@len}] = {
|
|
|
|
#{@content}};
|
|
|
|
End
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
2010-03-14 13:01:06 -04:00
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
class Action
|
|
|
|
def initialize(value)
|
|
|
|
@value = value
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
2010-03-14 13:01:06 -04:00
|
|
|
attr_reader :value
|
2010-03-14 13:45:21 -04:00
|
|
|
|
|
|
|
def hash
|
|
|
|
@value.hash
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
self.class == other.class &&
|
|
|
|
@value == other.value
|
|
|
|
end
|
|
|
|
alias == eql?
|
2010-03-14 13:01:06 -04:00
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
class Branch
|
|
|
|
def initialize(byte_min, byte_max, child_tree)
|
|
|
|
@byte_min = byte_min
|
|
|
|
@byte_max = byte_max
|
|
|
|
@child_tree = child_tree
|
|
|
|
@hash = byte_min.hash ^ byte_max.hash ^ child_tree.hash
|
|
|
|
end
|
|
|
|
attr_reader :byte_min, :byte_max, :child_tree, :hash
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
self.class == other.class &&
|
|
|
|
@hash == other.hash &&
|
|
|
|
@byte_min == other.byte_min &&
|
|
|
|
@byte_max == other.byte_max &&
|
|
|
|
@child_tree == other.child_tree
|
|
|
|
end
|
|
|
|
alias == eql?
|
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
class ActionMap
|
2010-03-15 18:21:24 -04:00
|
|
|
def self.parse_to_rects(mapping)
|
2010-03-14 13:01:06 -04:00
|
|
|
rects = []
|
|
|
|
n = 0
|
2010-03-15 18:21:24 -04:00
|
|
|
mapping.each {|pat, action|
|
|
|
|
pat = pat.to_s
|
2010-03-14 13:01:06 -04:00
|
|
|
if /\A\s*\(empset\)\s*\z/ =~ pat
|
|
|
|
next
|
|
|
|
elsif /\A\s*\(empstr\)\s*\z/ =~ pat
|
|
|
|
rects << ['', '', action]
|
|
|
|
n += 1
|
|
|
|
elsif /\A\s*(#{HEX2}+)\s*\z/o =~ pat
|
|
|
|
hex = $1.upcase
|
|
|
|
rects << [hex, hex, action]
|
|
|
|
elsif /\A\s*((#{HEX2}|\{#{HEX2}(?:-#{HEX2})?(,#{HEX2}(?:-#{HEX2})?)*\})+(\s+|\z))*\z/o =~ pat
|
|
|
|
pat = pat.upcase
|
|
|
|
pat.scan(/\S+/) {
|
|
|
|
pat1 = $&
|
|
|
|
ranges_list = []
|
|
|
|
pat1.scan(/#{HEX2}|\{([^\}]*)\}/o) {
|
|
|
|
ranges_list << []
|
|
|
|
if !$1
|
|
|
|
ranges_list.last << [$&,$&]
|
2008-08-06 07:47:14 -04:00
|
|
|
else
|
2010-03-14 13:01:06 -04:00
|
|
|
set = {}
|
|
|
|
$1.scan(/(#{HEX2})(?:-(#{HEX2}))?/o) {
|
|
|
|
if !$2
|
|
|
|
c = $1.to_i(16)
|
|
|
|
set[c] = true
|
2008-08-06 07:47:14 -04:00
|
|
|
else
|
2010-03-14 13:01:06 -04:00
|
|
|
b = $1.to_i(16)
|
|
|
|
e = $2.to_i(16)
|
2018-05-17 20:40:06 -04:00
|
|
|
b.upto(e) {|_| set[_] = true }
|
2008-08-06 07:47:14 -04:00
|
|
|
end
|
2010-03-14 13:01:06 -04:00
|
|
|
}
|
|
|
|
i = nil
|
|
|
|
0.upto(256) {|j|
|
|
|
|
if set[j]
|
|
|
|
if !i
|
|
|
|
i = j
|
|
|
|
end
|
|
|
|
if !set[j+1]
|
|
|
|
ranges_list.last << ["%02X" % i, "%02X" % j]
|
|
|
|
i = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
2008-08-06 07:47:14 -04:00
|
|
|
end
|
2010-03-14 13:01:06 -04:00
|
|
|
}
|
|
|
|
first_ranges = ranges_list.shift
|
|
|
|
first_ranges.product(*ranges_list).each {|range_list|
|
|
|
|
min = range_list.map {|x, y| x }.join
|
|
|
|
max = range_list.map {|x, y| y }.join
|
|
|
|
rects << [min, max, action]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
raise ArgumentError, "invalid pattern: #{pat.inspect}"
|
|
|
|
end
|
|
|
|
}
|
|
|
|
rects
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def self.unambiguous_action(actions0)
|
|
|
|
actions = actions0.uniq
|
|
|
|
if actions.length == 1
|
|
|
|
actions[0]
|
2008-08-09 02:02:01 -04:00
|
|
|
else
|
2010-03-20 06:18:55 -04:00
|
|
|
actions.delete(:nomap0)
|
2010-03-14 13:01:06 -04:00
|
|
|
if actions.length == 1
|
|
|
|
actions[0]
|
|
|
|
else
|
|
|
|
raise ArgumentError, "ambiguous actions: #{actions0.inspect}"
|
|
|
|
end
|
2008-08-09 02:02:01 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def self.build_tree(rects)
|
2010-03-19 22:32:40 -04:00
|
|
|
expand(rects) {|prefix, actions|
|
2010-03-14 13:01:06 -04:00
|
|
|
unambiguous_action(actions)
|
|
|
|
}
|
2008-08-09 02:02:01 -04:00
|
|
|
end
|
|
|
|
|
2010-03-15 18:21:24 -04:00
|
|
|
def self.parse(mapping)
|
|
|
|
rects = parse_to_rects(mapping)
|
2010-03-14 13:01:06 -04:00
|
|
|
tree = build_tree(rects)
|
2010-03-15 18:02:42 -04:00
|
|
|
self.new(tree)
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
def self.merge_rects(*rects_list)
|
2010-03-14 13:01:06 -04:00
|
|
|
if rects_list.length < 2
|
|
|
|
raise ArgumentError, "not enough arguments"
|
|
|
|
end
|
|
|
|
|
|
|
|
all_rects = []
|
|
|
|
rects_list.each_with_index {|rects, i|
|
|
|
|
all_rects.concat rects.map {|min, max, action| [min, max, [i, action]] }
|
2010-03-13 12:54:43 -05:00
|
|
|
}
|
|
|
|
|
2010-03-19 22:32:40 -04:00
|
|
|
tree = expand(all_rects) {|prefix, actions|
|
2010-03-14 13:01:06 -04:00
|
|
|
args = Array.new(rects_list.length) { [] }
|
|
|
|
actions.each {|i, action|
|
|
|
|
args[i] << action
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-15 08:25:20 -04:00
|
|
|
yield(prefix, *args)
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-14 13:01:06 -04:00
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
self.new(tree)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.merge(*mappings, &block)
|
|
|
|
merge_rects(*mappings.map {|m| parse_to_rects(m) }, &block)
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-20 06:18:55 -04:00
|
|
|
def self.merge2(map1, map2, &block)
|
|
|
|
rects1 = parse_to_rects(map1)
|
|
|
|
rects2 = parse_to_rects(map2)
|
|
|
|
|
|
|
|
actions = []
|
|
|
|
all_rects = []
|
|
|
|
|
|
|
|
rects1.each {|rect|
|
2018-05-17 20:38:00 -04:00
|
|
|
_, _, action = rect
|
2010-03-20 06:18:55 -04:00
|
|
|
rect[2] = actions.length
|
|
|
|
actions << action
|
|
|
|
all_rects << rect
|
|
|
|
}
|
|
|
|
|
|
|
|
boundary = actions.length
|
|
|
|
|
|
|
|
rects2.each {|rect|
|
2018-05-17 20:38:00 -04:00
|
|
|
_, _, action = rect
|
2010-03-20 06:18:55 -04:00
|
|
|
rect[2] = actions.length
|
|
|
|
actions << action
|
|
|
|
all_rects << rect
|
|
|
|
}
|
|
|
|
|
|
|
|
tree = expand(all_rects) {|prefix, as0|
|
|
|
|
as1 = []
|
|
|
|
as2 = []
|
|
|
|
as0.each {|i|
|
|
|
|
if i < boundary
|
|
|
|
as1 << actions[i]
|
|
|
|
else
|
|
|
|
as2 << actions[i]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
yield(prefix, as1, as2)
|
|
|
|
}
|
|
|
|
|
|
|
|
self.new(tree)
|
|
|
|
end
|
|
|
|
|
2010-03-19 22:32:40 -04:00
|
|
|
def self.expand(rects, &block)
|
2010-03-16 09:52:16 -04:00
|
|
|
#numsing = numreg = 0
|
|
|
|
#rects.each {|min, max, action| if min == max then numsing += 1 else numreg += 1 end }
|
|
|
|
#puts "#{numsing} singleton mappings and #{numreg} region mappings."
|
|
|
|
singleton_rects = []
|
|
|
|
region_rects = []
|
|
|
|
rects.each {|rect|
|
2018-05-17 20:38:00 -04:00
|
|
|
min, max, = rect
|
2010-03-16 09:52:16 -04:00
|
|
|
if min == max
|
|
|
|
singleton_rects << rect
|
|
|
|
else
|
|
|
|
region_rects << rect
|
|
|
|
end
|
|
|
|
}
|
2010-03-18 06:53:38 -04:00
|
|
|
@singleton_rects = singleton_rects.sort_by {|min, max, action| min }
|
|
|
|
@singleton_rects.reverse!
|
2010-03-19 22:32:40 -04:00
|
|
|
ret = expand_rec("", region_rects, &block)
|
2010-03-18 06:53:38 -04:00
|
|
|
@singleton_rects = nil
|
|
|
|
ret
|
2010-03-16 09:52:16 -04:00
|
|
|
end
|
|
|
|
|
2010-03-21 04:15:16 -04:00
|
|
|
TMPHASH = {}
|
2010-03-18 06:53:38 -04:00
|
|
|
def self.expand_rec(prefix, region_rects, &block)
|
2010-03-19 22:32:40 -04:00
|
|
|
return region_rects if region_rects.empty? && !((s_rect = @singleton_rects.last) && s_rect[0].start_with?(prefix))
|
2010-03-18 06:53:38 -04:00
|
|
|
if region_rects.empty? ? s_rect[0].length == prefix.length : region_rects[0][0].empty?
|
2010-03-21 04:15:16 -04:00
|
|
|
h = TMPHASH
|
2010-03-18 06:53:38 -04:00
|
|
|
while (s_rect = @singleton_rects.last) && s_rect[0].start_with?(prefix)
|
2018-05-17 20:38:00 -04:00
|
|
|
min, _, action = @singleton_rects.pop
|
2010-03-18 06:53:38 -04:00
|
|
|
raise ArgumentError, "ambiguous pattern: #{prefix}" if min.length != prefix.length
|
|
|
|
h[action] = true
|
|
|
|
end
|
2018-05-17 20:40:06 -04:00
|
|
|
for min, _, action in region_rects
|
2010-03-15 18:02:42 -04:00
|
|
|
raise ArgumentError, "ambiguous pattern: #{prefix}" if !min.empty?
|
2010-03-16 09:52:16 -04:00
|
|
|
h[action] = true
|
2018-05-17 20:40:06 -04:00
|
|
|
end
|
2010-03-19 22:32:40 -04:00
|
|
|
tree = Action.new(block.call(prefix, h.keys))
|
|
|
|
h.clear
|
2010-03-14 13:01:06 -04:00
|
|
|
else
|
|
|
|
tree = []
|
2010-03-18 06:53:38 -04:00
|
|
|
each_firstbyte_range(prefix, region_rects) {|byte_min, byte_max, r_rects2|
|
2010-03-14 13:01:06 -04:00
|
|
|
if byte_min == byte_max
|
2010-03-16 09:52:16 -04:00
|
|
|
prefix2 = prefix + "%02X" % byte_min
|
2010-03-14 13:01:06 -04:00
|
|
|
else
|
2010-03-16 09:52:16 -04:00
|
|
|
prefix2 = prefix + "{%02X-%02X}" % [byte_min, byte_max]
|
2010-03-14 13:01:06 -04:00
|
|
|
end
|
2010-03-18 06:53:38 -04:00
|
|
|
child_tree = expand_rec(prefix2, r_rects2, &block)
|
2010-03-15 18:02:42 -04:00
|
|
|
tree << Branch.new(byte_min, byte_max, child_tree)
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-14 13:01:06 -04:00
|
|
|
end
|
|
|
|
return tree
|
|
|
|
end
|
|
|
|
|
2010-03-18 06:53:38 -04:00
|
|
|
def self.each_firstbyte_range(prefix, region_rects)
|
2010-03-21 04:15:16 -04:00
|
|
|
index_from = TMPHASH
|
2010-03-16 09:52:16 -04:00
|
|
|
|
|
|
|
region_ary = []
|
|
|
|
region_rects.each {|min, max, action|
|
2010-03-15 18:02:42 -04:00
|
|
|
raise ArgumentError, "ambiguous pattern: #{prefix}" if min.empty?
|
2010-03-14 13:01:06 -04:00
|
|
|
min_firstbyte = min[0,2].to_i(16)
|
|
|
|
min_rest = min[2..-1]
|
|
|
|
max_firstbyte = max[0,2].to_i(16)
|
|
|
|
max_rest = max[2..-1]
|
2010-03-16 09:52:16 -04:00
|
|
|
region_ary << [min_firstbyte, max_firstbyte, [min_rest, max_rest, action]]
|
2010-03-14 13:01:06 -04:00
|
|
|
index_from[min_firstbyte] = true
|
|
|
|
index_from[max_firstbyte+1] = true
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-16 09:52:16 -04:00
|
|
|
|
|
|
|
byte_from = Array.new(index_from.size)
|
2010-03-18 06:53:38 -04:00
|
|
|
bytes = index_from.keys
|
|
|
|
bytes.sort!
|
|
|
|
bytes.reverse!
|
|
|
|
bytes.each_with_index {|byte, i|
|
2010-03-14 13:01:06 -04:00
|
|
|
index_from[byte] = i
|
|
|
|
byte_from[i] = byte
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-16 09:52:16 -04:00
|
|
|
|
2010-03-18 06:53:38 -04:00
|
|
|
region_rects_ary = Array.new(index_from.size) { [] }
|
2010-03-16 09:52:16 -04:00
|
|
|
region_ary.each {|min_firstbyte, max_firstbyte, rest_elt|
|
2010-03-18 06:53:38 -04:00
|
|
|
index_from[min_firstbyte].downto(index_from[max_firstbyte+1]+1) {|i|
|
|
|
|
region_rects_ary[i] << rest_elt
|
2010-03-14 13:01:06 -04:00
|
|
|
}
|
|
|
|
}
|
2010-03-16 09:52:16 -04:00
|
|
|
|
2010-03-19 22:32:40 -04:00
|
|
|
index_from.clear
|
|
|
|
|
|
|
|
r_rects = region_rects_ary.pop
|
|
|
|
region_byte = byte_from.pop
|
|
|
|
prev_r_start = region_byte
|
|
|
|
prev_r_rects = []
|
|
|
|
while r_rects && (s_rect = @singleton_rects.last) && (seq = s_rect[0]).start_with?(prefix)
|
2010-03-18 06:53:38 -04:00
|
|
|
singleton_byte = seq[prefix.length, 2].to_i(16)
|
|
|
|
min_byte = singleton_byte < region_byte ? singleton_byte : region_byte
|
2010-03-19 22:32:40 -04:00
|
|
|
if prev_r_start < min_byte && !prev_r_rects.empty?
|
|
|
|
yield prev_r_start, min_byte-1, prev_r_rects
|
2010-03-16 09:52:16 -04:00
|
|
|
end
|
2010-03-18 06:53:38 -04:00
|
|
|
if region_byte < singleton_byte
|
2010-03-19 22:32:40 -04:00
|
|
|
prev_r_start = region_byte
|
|
|
|
prev_r_rects = r_rects
|
2010-03-18 06:53:38 -04:00
|
|
|
r_rects = region_rects_ary.pop
|
2010-03-19 22:32:40 -04:00
|
|
|
region_byte = byte_from.pop
|
2010-03-18 06:53:38 -04:00
|
|
|
elsif region_byte > singleton_byte
|
2010-03-19 22:32:40 -04:00
|
|
|
yield singleton_byte, singleton_byte, prev_r_rects
|
|
|
|
prev_r_start = singleton_byte+1
|
2010-03-18 06:53:38 -04:00
|
|
|
else # region_byte == singleton_byte
|
2010-03-19 22:32:40 -04:00
|
|
|
prev_r_start = region_byte+1
|
|
|
|
prev_r_rects = r_rects
|
2010-03-18 06:53:38 -04:00
|
|
|
r_rects = region_rects_ary.pop
|
2010-03-19 22:32:40 -04:00
|
|
|
region_byte = byte_from.pop
|
|
|
|
yield singleton_byte, singleton_byte, prev_r_rects
|
2010-03-18 06:53:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-19 22:32:40 -04:00
|
|
|
while r_rects
|
|
|
|
if prev_r_start < region_byte && !prev_r_rects.empty?
|
|
|
|
yield prev_r_start, region_byte-1, prev_r_rects
|
2010-03-18 06:53:38 -04:00
|
|
|
end
|
2010-03-19 22:32:40 -04:00
|
|
|
prev_r_start = region_byte
|
|
|
|
prev_r_rects = r_rects
|
2010-03-18 06:53:38 -04:00
|
|
|
r_rects = region_rects_ary.pop
|
2010-03-19 22:32:40 -04:00
|
|
|
region_byte = byte_from.pop
|
2010-03-18 06:53:38 -04:00
|
|
|
end
|
|
|
|
|
2010-03-19 22:32:40 -04:00
|
|
|
while (s_rect = @singleton_rects.last) && (seq = s_rect[0]).start_with?(prefix)
|
2010-03-18 06:53:38 -04:00
|
|
|
singleton_byte = seq[prefix.length, 2].to_i(16)
|
|
|
|
yield singleton_byte, singleton_byte, []
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
def initialize(tree)
|
2010-03-14 13:01:06 -04:00
|
|
|
@tree = tree
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def inspect
|
2011-05-15 07:55:52 -04:00
|
|
|
"\#<#{self.class}:" +
|
2010-03-14 13:01:06 -04:00
|
|
|
@tree.inspect +
|
2008-08-05 08:32:13 -04:00
|
|
|
">"
|
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def max_input_length_rec(tree)
|
|
|
|
case tree
|
|
|
|
when Action
|
|
|
|
0
|
|
|
|
else
|
2010-03-15 18:02:42 -04:00
|
|
|
tree.map {|branch|
|
|
|
|
max_input_length_rec(branch.child_tree)
|
2010-03-14 13:01:06 -04:00
|
|
|
}.max + 1
|
|
|
|
end
|
2008-08-09 02:02:01 -04:00
|
|
|
end
|
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def max_input_length
|
|
|
|
max_input_length_rec(@tree)
|
2010-03-13 12:54:43 -05:00
|
|
|
end
|
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
def empty_action
|
2010-03-14 13:01:06 -04:00
|
|
|
if @tree.kind_of? Action
|
|
|
|
@tree.value
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
OffsetsMemo = {}
|
|
|
|
InfosMemo = {}
|
|
|
|
|
2008-08-08 11:48:17 -04:00
|
|
|
def format_offsets(min, max, offsets)
|
|
|
|
offsets = offsets[min..max]
|
2008-09-01 09:21:06 -04:00
|
|
|
code = "%d, %d,\n" % [min, max]
|
2008-08-05 08:32:13 -04:00
|
|
|
0.step(offsets.length-1,16) {|i|
|
|
|
|
code << " "
|
|
|
|
code << offsets[i,8].map {|off| "%3d," % off.to_s }.join('')
|
2008-08-08 11:48:17 -04:00
|
|
|
if i+8 < offsets.length
|
|
|
|
code << " "
|
|
|
|
code << offsets[i+8,8].map {|off| "%3d," % off.to_s }.join('')
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
code << "\n"
|
|
|
|
}
|
|
|
|
code
|
|
|
|
end
|
|
|
|
|
2008-09-07 06:17:52 -04:00
|
|
|
UsedName = {}
|
|
|
|
|
2008-09-07 05:30:54 -04:00
|
|
|
StrMemo = {}
|
|
|
|
|
2008-09-07 06:17:52 -04:00
|
|
|
def str_name(bytes)
|
|
|
|
size = @bytes_code.length
|
|
|
|
rawbytes = [bytes].pack("H*")
|
|
|
|
|
|
|
|
n = nil
|
|
|
|
if !n && !(suf = rawbytes.gsub(/[^A-Za-z0-9_]/, '')).empty? && !UsedName[nn = "str1_" + suf] then n = nn end
|
|
|
|
if !n && !UsedName[nn = "str1_" + bytes] then n = nn end
|
|
|
|
n ||= "str1s_#{size}"
|
|
|
|
|
|
|
|
StrMemo[bytes] = n
|
|
|
|
UsedName[n] = true
|
|
|
|
n
|
|
|
|
end
|
|
|
|
|
|
|
|
def gen_str(bytes)
|
|
|
|
if n = StrMemo[bytes]
|
|
|
|
n
|
|
|
|
else
|
|
|
|
len = bytes.length/2
|
|
|
|
size = @bytes_code.length
|
|
|
|
n = str_name(bytes)
|
|
|
|
@bytes_code.insert_at_last(1 + len,
|
|
|
|
"\#define #{n} makeSTR1(#{size})\n" +
|
2008-09-15 11:42:30 -04:00
|
|
|
" makeSTR1LEN(#{len})," + bytes.gsub(/../, ' 0x\&,') + "\n\n")
|
2008-09-07 06:17:52 -04:00
|
|
|
n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
def generate_info(info)
|
|
|
|
case info
|
2009-05-04 20:05:11 -04:00
|
|
|
when :nomap, :nomap0
|
|
|
|
# :nomap0 is low priority. it never collides.
|
2008-08-05 08:32:13 -04:00
|
|
|
"NOMAP"
|
|
|
|
when :undef
|
|
|
|
"UNDEF"
|
|
|
|
when :invalid
|
|
|
|
"INVALID"
|
2008-08-07 10:53:30 -04:00
|
|
|
when :func_ii
|
|
|
|
"FUNii"
|
|
|
|
when :func_si
|
|
|
|
"FUNsi"
|
|
|
|
when :func_io
|
|
|
|
"FUNio"
|
2008-08-05 08:32:13 -04:00
|
|
|
when :func_so
|
|
|
|
"FUNso"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(#{HEX2})\z/o
|
2008-08-05 08:32:13 -04:00
|
|
|
"o1(0x#$1)"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(#{HEX2})(#{HEX2})\z/o
|
2008-08-05 08:32:13 -04:00
|
|
|
"o2(0x#$1,0x#$2)"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(#{HEX2})(#{HEX2})(#{HEX2})\z/o
|
2008-08-05 08:32:13 -04:00
|
|
|
"o3(0x#$1,0x#$2,0x#$3)"
|
2009-12-10 06:59:12 -05:00
|
|
|
when /funsio\((\d+)\)/
|
|
|
|
"funsio(#{$1})"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(#{HEX2})(3[0-9])(#{HEX2})(3[0-9])\z/o
|
2009-01-14 06:12:30 -05:00
|
|
|
"g4(0x#$1,0x#$2,0x#$3,0x#$4)"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(f[0-7])(#{HEX2})(#{HEX2})(#{HEX2})\z/o
|
2008-08-05 08:32:13 -04:00
|
|
|
"o4(0x#$1,0x#$2,0x#$3,0x#$4)"
|
2010-03-03 10:11:56 -05:00
|
|
|
when /\A(#{HEX2}){4,259}\z/o
|
2008-09-07 06:17:52 -04:00
|
|
|
gen_str(info.upcase)
|
2008-09-01 12:22:49 -04:00
|
|
|
when /\A\/\*BYTE_LOOKUP\*\// # pointer to BYTE_LOOKUP structure
|
2008-09-01 14:26:58 -04:00
|
|
|
$'.to_s
|
2008-08-14 07:53:47 -04:00
|
|
|
else
|
|
|
|
raise "unexpected action: #{info.inspect}"
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_infos(infos)
|
|
|
|
infos = infos.map {|info| generate_info(info) }
|
|
|
|
maxlen = infos.map {|info| info.length }.max
|
|
|
|
columns = maxlen <= 16 ? 4 : 2
|
2015-10-05 09:27:48 -04:00
|
|
|
code = "".dup
|
2008-08-05 08:32:13 -04:00
|
|
|
0.step(infos.length-1, columns) {|i|
|
|
|
|
code << " "
|
|
|
|
is = infos[i,columns]
|
|
|
|
is.each {|info|
|
|
|
|
code << sprintf(" %#{maxlen}s,", info)
|
|
|
|
}
|
|
|
|
code << "\n"
|
|
|
|
}
|
|
|
|
code
|
|
|
|
end
|
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
def generate_lookup_node(name, table)
|
|
|
|
bytes_code = @bytes_code
|
|
|
|
words_code = @words_code
|
2008-08-05 08:32:13 -04:00
|
|
|
offsets = []
|
|
|
|
infos = []
|
|
|
|
infomap = {}
|
2008-08-08 11:48:17 -04:00
|
|
|
min = max = nil
|
2008-08-05 08:32:13 -04:00
|
|
|
table.each_with_index {|action, byte|
|
2008-08-06 07:47:14 -04:00
|
|
|
action ||= :invalid
|
2008-08-08 11:48:17 -04:00
|
|
|
if action != :invalid
|
|
|
|
min = byte if !min
|
|
|
|
max = byte
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
unless o = infomap[action]
|
|
|
|
infomap[action] = o = infos.length
|
|
|
|
infos[o] = action
|
|
|
|
end
|
|
|
|
offsets[byte] = o
|
|
|
|
}
|
2010-03-19 22:32:40 -04:00
|
|
|
infomap.clear
|
2008-08-08 11:48:17 -04:00
|
|
|
if !min
|
|
|
|
min = max = 0
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2012-04-06 14:17:01 -04:00
|
|
|
offsets_key = [min, max, offsets[min..max]]
|
|
|
|
if n = OffsetsMemo[offsets_key]
|
|
|
|
offsets_name = n
|
|
|
|
else
|
|
|
|
offsets_name = "#{name}_offsets"
|
|
|
|
OffsetsMemo[offsets_key] = offsets_name
|
|
|
|
size = bytes_code.length
|
|
|
|
bytes_code.insert_at_last(2+max-min+1,
|
|
|
|
"\#define #{offsets_name} #{size}\n" +
|
|
|
|
format_offsets(min,max,offsets) + "\n")
|
|
|
|
end
|
2012-04-06 03:23:40 -04:00
|
|
|
|
2012-04-06 14:17:01 -04:00
|
|
|
if n = InfosMemo[infos]
|
|
|
|
infos_name = n
|
|
|
|
else
|
|
|
|
infos_name = "#{name}_infos"
|
|
|
|
InfosMemo[infos] = infos_name
|
2012-04-06 03:23:40 -04:00
|
|
|
|
|
|
|
size = words_code.length
|
2012-04-06 14:17:01 -04:00
|
|
|
words_code.insert_at_last(infos.length,
|
|
|
|
"\#define #{infos_name} WORDINDEX2INFO(#{size})\n" +
|
|
|
|
format_infos(infos) + "\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
size = words_code.length
|
|
|
|
words_code.insert_at_last(NUM_ELEM_BYTELOOKUP,
|
|
|
|
"\#define #{name} WORDINDEX2INFO(#{size})\n" +
|
|
|
|
<<"End" + "\n")
|
2008-09-01 13:49:13 -04:00
|
|
|
#{offsets_name},
|
2008-09-01 13:06:07 -04:00
|
|
|
#{infos_name},
|
2008-08-05 08:32:13 -04:00
|
|
|
End
|
|
|
|
end
|
|
|
|
|
|
|
|
PreMemo = {}
|
|
|
|
NextName = "a"
|
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
def generate_node(name_hint=nil)
|
|
|
|
if n = PreMemo[@tree]
|
2008-08-05 08:32:13 -04:00
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
2008-08-08 11:48:17 -04:00
|
|
|
table = Array.new(0x100, :invalid)
|
2010-03-15 18:02:42 -04:00
|
|
|
@tree.each {|branch|
|
|
|
|
byte_min, byte_max, child_tree = branch.byte_min, branch.byte_max, branch.child_tree
|
|
|
|
rest = ActionMap.new(child_tree)
|
2008-08-05 08:32:13 -04:00
|
|
|
if a = rest.empty_action
|
2010-03-15 18:02:42 -04:00
|
|
|
table.fill(a, byte_min..byte_max)
|
2008-08-05 08:32:13 -04:00
|
|
|
else
|
|
|
|
name_hint2 = nil
|
2010-03-15 08:59:54 -04:00
|
|
|
if name_hint
|
2010-03-15 15:10:29 -04:00
|
|
|
name_hint2 = "#{name_hint}_#{byte_min == byte_max ? '%02X' % byte_min : '%02Xto%02X' % [byte_min, byte_max]}"
|
2010-03-15 08:59:54 -04:00
|
|
|
end
|
2010-03-15 18:02:42 -04:00
|
|
|
v = "/*BYTE_LOOKUP*/" + rest.gennode(@bytes_code, @words_code, name_hint2)
|
|
|
|
table.fill(v, byte_min..byte_max)
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if !name_hint
|
2010-03-09 01:00:58 -05:00
|
|
|
name_hint = "fun_" + NextName
|
2008-08-05 08:32:13 -04:00
|
|
|
NextName.succ!
|
|
|
|
end
|
|
|
|
|
2010-03-20 06:18:55 -04:00
|
|
|
PreMemo[@tree] = name_hint
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-15 18:02:42 -04:00
|
|
|
generate_lookup_node(name_hint, table)
|
2008-08-05 08:32:13 -04:00
|
|
|
name_hint
|
|
|
|
end
|
2008-08-31 13:35:00 -04:00
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
def gennode(bytes_code, words_code, name_hint=nil)
|
2008-09-07 05:13:16 -04:00
|
|
|
@bytes_code = bytes_code
|
|
|
|
@words_code = words_code
|
2010-03-15 18:02:42 -04:00
|
|
|
name = generate_node(name_hint)
|
2008-09-07 05:13:16 -04:00
|
|
|
@bytes_code = nil
|
|
|
|
@words_code = nil
|
2008-09-01 10:16:30 -04:00
|
|
|
return name
|
2008-08-31 13:35:00 -04:00
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2008-08-31 12:23:04 -04:00
|
|
|
def citrus_mskanji_cstomb(csid, index)
|
|
|
|
case csid
|
|
|
|
when 0
|
|
|
|
index
|
|
|
|
when 1
|
|
|
|
index + 0x80
|
|
|
|
when 2, 3
|
|
|
|
row = index >> 8
|
2008-08-31 13:49:08 -04:00
|
|
|
raise "invalid byte sequence" if row < 0x21
|
2008-08-31 12:23:04 -04:00
|
|
|
if csid == 3
|
|
|
|
if row <= 0x2F
|
|
|
|
offset = (row == 0x22 || row >= 0x26) ? 0xED : 0xF0
|
|
|
|
elsif row >= 0x4D && row <= 0x7E
|
|
|
|
offset = 0xCE
|
|
|
|
else
|
2008-08-31 13:49:08 -04:00
|
|
|
raise "invalid byte sequence"
|
2008-08-31 12:23:04 -04:00
|
|
|
end
|
|
|
|
else
|
2008-08-31 13:49:08 -04:00
|
|
|
raise "invalid byte sequence" if row > 0x97
|
2008-08-31 12:23:04 -04:00
|
|
|
offset = (row < 0x5F) ? 0x81 : 0xC1
|
|
|
|
end
|
|
|
|
col = index & 0xFF
|
2008-08-31 13:49:08 -04:00
|
|
|
raise "invalid byte sequence" if (col < 0x21 || col > 0x7E)
|
2008-08-31 12:23:04 -04:00
|
|
|
|
|
|
|
row -= 0x21
|
|
|
|
col -= 0x21
|
|
|
|
if (row & 1) == 0
|
|
|
|
col += 0x40
|
|
|
|
col += 1 if (col >= 0x7F)
|
|
|
|
else
|
|
|
|
col += 0x9F;
|
|
|
|
end
|
|
|
|
row = row / 2 + offset
|
|
|
|
(row << 8) | col
|
|
|
|
end.to_s(16)
|
|
|
|
end
|
|
|
|
|
|
|
|
def citrus_euc_cstomb(csid, index)
|
|
|
|
case csid
|
|
|
|
when 0x0000
|
|
|
|
index
|
|
|
|
when 0x8080
|
|
|
|
index | 0x8080
|
|
|
|
when 0x0080
|
|
|
|
index | 0x8E80
|
|
|
|
when 0x8000
|
|
|
|
index | 0x8F8080
|
|
|
|
end.to_s(16)
|
|
|
|
end
|
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
def citrus_stateless_iso_cstomb(csid, index)
|
|
|
|
(index | 0x8080 | (csid << 16)).to_s(16)
|
|
|
|
end
|
|
|
|
|
2008-08-31 12:23:04 -04:00
|
|
|
def citrus_cstomb(ces, csid, index)
|
|
|
|
case ces
|
|
|
|
when 'mskanji'
|
|
|
|
citrus_mskanji_cstomb(csid, index)
|
|
|
|
when 'euc'
|
|
|
|
citrus_euc_cstomb(csid, index)
|
2010-03-09 04:15:42 -05:00
|
|
|
when 'stateless_iso'
|
|
|
|
citrus_stateless_iso_cstomb(csid, index)
|
2008-08-31 12:23:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
SUBDIR = %w/APPLE AST BIG5 CNS CP EBCDIC EMOJI GB GEORGIAN ISO646 ISO-8859 JIS KAZAKH KOI KS MISC TCVN/
|
2008-08-31 12:23:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
def citrus_decode_mapsrc(ces, csid, mapsrcs)
|
|
|
|
table = []
|
|
|
|
mapsrcs.split(',').each do |mapsrc|
|
|
|
|
path = [$srcdir]
|
|
|
|
mode = nil
|
2012-04-24 07:14:18 -04:00
|
|
|
if mapsrc.rindex(/UCS(?:@[A-Z]+)?/, 0)
|
2008-08-31 12:23:04 -04:00
|
|
|
mode = :from_ucs
|
2012-04-24 07:14:18 -04:00
|
|
|
from = mapsrc[$&.size+1..-1]
|
2008-09-03 21:43:50 -04:00
|
|
|
path << SUBDIR.find{|x| from.rindex(x, 0) }
|
2008-08-31 12:23:04 -04:00
|
|
|
else
|
|
|
|
mode = :to_ucs
|
2008-09-03 21:43:50 -04:00
|
|
|
path << SUBDIR.find{|x| mapsrc.rindex(x, 0) }
|
2008-08-31 12:23:04 -04:00
|
|
|
end
|
2012-04-24 07:14:18 -04:00
|
|
|
if /\bUCS@(BMP|SMP|SIP|TIP|SSP)\b/ =~ mapsrc
|
|
|
|
plane = {"BMP"=>0, "SMP"=>1, "SIP"=>2, "TIP"=>3, "SSP"=>14}[$1]
|
|
|
|
else
|
|
|
|
plane = 0
|
|
|
|
end
|
|
|
|
plane <<= 16
|
2008-08-31 12:23:04 -04:00
|
|
|
path << mapsrc.gsub(':', '@')
|
|
|
|
path = File.join(*path)
|
|
|
|
path << ".src"
|
|
|
|
path[path.rindex('/')] = '%'
|
2020-12-29 02:42:24 -05:00
|
|
|
STDOUT.puts 'load mapsrc %s' % path if VERBOSE_MODE > 1
|
2016-03-16 10:51:40 -04:00
|
|
|
open(path, 'rb') do |f|
|
2008-08-31 12:23:04 -04:00
|
|
|
f.each_line do |l|
|
2008-09-01 01:54:36 -04:00
|
|
|
break if /^BEGIN_MAP/ =~ l
|
2008-08-31 12:23:04 -04:00
|
|
|
end
|
|
|
|
f.each_line do |l|
|
2008-09-01 01:54:36 -04:00
|
|
|
next if /^\s*(?:#|$)/ =~ l
|
2016-03-16 11:04:11 -04:00
|
|
|
break if /^END_MAP/ =~ l
|
2008-09-01 01:54:36 -04:00
|
|
|
case mode
|
|
|
|
when :from_ucs
|
|
|
|
case l
|
|
|
|
when /0x(\w+)\s*-\s*0x(\w+)\s*=\s*INVALID/
|
|
|
|
# Citrus OOB_MODE
|
|
|
|
when /(0x\w+)\s*=\s*(0x\w+)/
|
2012-04-24 07:14:18 -04:00
|
|
|
table.push << [plane | $1.hex, citrus_cstomb(ces, csid, $2.hex)]
|
2008-09-01 01:54:36 -04:00
|
|
|
else
|
2016-03-16 11:04:11 -04:00
|
|
|
raise "unknown notation '%s'"% l.chomp
|
2008-09-01 01:54:36 -04:00
|
|
|
end
|
|
|
|
when :to_ucs
|
|
|
|
case l
|
|
|
|
when /(0x\w+)\s*=\s*(0x\w+)/
|
2012-04-24 07:14:18 -04:00
|
|
|
table.push << [citrus_cstomb(ces, csid, $1.hex), plane | $2.hex]
|
2008-09-01 01:54:36 -04:00
|
|
|
else
|
2016-03-16 11:04:11 -04:00
|
|
|
raise "unknown notation '%s'"% l.chomp
|
2008-09-01 01:54:36 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-31 12:23:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
|
2010-11-22 04:35:08 -05:00
|
|
|
def import_ucm(path)
|
|
|
|
to_ucs = []
|
|
|
|
from_ucs = []
|
|
|
|
File.foreach(File.join($srcdir, "ucm", path)) do |line|
|
|
|
|
uc, bs, fb = nil
|
2012-02-16 19:53:13 -05:00
|
|
|
if /^<U([0-9a-fA-F]+)>\s*([\+0-9a-fA-Fx\\]+)\s*\|(\d)/ =~ line
|
2010-11-22 04:35:08 -05:00
|
|
|
uc = $1.hex
|
|
|
|
bs = $2.delete('x\\')
|
|
|
|
fb = $3.to_i
|
|
|
|
next if uc < 128 && uc == bs.hex
|
2012-02-16 19:53:13 -05:00
|
|
|
elsif /^([<U0-9a-fA-F>+]+)\s*([\+0-9a-fA-Fx\\]+)\s*\|(\d)/ =~ line
|
|
|
|
uc = $1.scan(/[0-9a-fA-F]+>/).map(&:hex).pack("U*").unpack("H*")[0]
|
2010-11-22 04:35:08 -05:00
|
|
|
bs = $2.delete('x\\')
|
|
|
|
fb = $3.to_i
|
|
|
|
end
|
|
|
|
to_ucs << [bs, uc] if fb == 0 || fb == 3
|
|
|
|
from_ucs << [uc, bs] if fb == 0 || fb == 1
|
|
|
|
end
|
|
|
|
[to_ucs, from_ucs]
|
|
|
|
end
|
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
def encode_utf8(map)
|
|
|
|
r = []
|
|
|
|
map.each {|k, v|
|
|
|
|
# integer means UTF-8 encoded sequence.
|
|
|
|
k = [k].pack("U").unpack("H*")[0].upcase if Integer === k
|
|
|
|
v = [v].pack("U").unpack("H*")[0].upcase if Integer === v
|
|
|
|
r << [k,v]
|
|
|
|
}
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
2010-03-20 23:38:58 -04:00
|
|
|
UnspecifiedValidEncoding = Object.new
|
|
|
|
|
|
|
|
def transcode_compile_tree(name, from, map, valid_encoding)
|
2008-08-05 08:32:13 -04:00
|
|
|
map = encode_utf8(map)
|
|
|
|
h = {}
|
|
|
|
map.each {|k, v|
|
2008-09-01 01:54:36 -04:00
|
|
|
h[k] = v unless h[k] # use first mapping
|
2008-08-05 08:32:13 -04:00
|
|
|
}
|
2010-03-20 23:38:58 -04:00
|
|
|
if valid_encoding.equal? UnspecifiedValidEncoding
|
|
|
|
valid_encoding = ValidEncoding.fetch(from)
|
|
|
|
end
|
2010-03-15 08:25:20 -04:00
|
|
|
if valid_encoding
|
2010-03-20 06:18:55 -04:00
|
|
|
am = ActionMap.merge2(h, {valid_encoding => :undef}) {|prefix, as1, as2|
|
2010-03-15 08:25:20 -04:00
|
|
|
a1 = as1.empty? ? nil : ActionMap.unambiguous_action(as1)
|
|
|
|
a2 = as2.empty? ? nil : ActionMap.unambiguous_action(as2)
|
|
|
|
if !a2
|
|
|
|
raise "invalid mapping: #{prefix}"
|
|
|
|
end
|
2010-03-14 13:01:06 -04:00
|
|
|
a1 || a2
|
|
|
|
}
|
2008-08-06 07:47:14 -04:00
|
|
|
else
|
2010-03-14 13:01:06 -04:00
|
|
|
am = ActionMap.parse(h)
|
2008-08-06 07:47:14 -04:00
|
|
|
end
|
2010-03-19 22:32:40 -04:00
|
|
|
h.clear
|
2008-08-06 07:47:14 -04:00
|
|
|
|
2010-03-14 13:01:06 -04:00
|
|
|
max_input = am.max_input_length
|
|
|
|
defined_name = am.gennode(TRANSCODE_GENERATED_BYTES_CODE, TRANSCODE_GENERATED_WORDS_CODE, name)
|
2008-09-01 10:16:30 -04:00
|
|
|
return defined_name, max_input
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
TRANSCODERS = []
|
2015-10-05 09:27:48 -04:00
|
|
|
TRANSCODE_GENERATED_TRANSCODER_CODE = ''.dup
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-20 23:38:58 -04:00
|
|
|
def transcode_tbl_only(from, to, map, valid_encoding=UnspecifiedValidEncoding)
|
2020-12-29 02:42:24 -05:00
|
|
|
if VERBOSE_MODE > 1
|
2008-09-07 12:11:40 -04:00
|
|
|
if from.empty? || to.empty?
|
2020-12-29 01:57:42 -05:00
|
|
|
STDOUT.puts "converter for #{from.empty? ? to : from}"
|
2008-09-07 12:11:40 -04:00
|
|
|
else
|
2020-12-29 01:57:42 -05:00
|
|
|
STDOUT.puts "converter from #{from} to #{to}"
|
2008-09-07 12:11:40 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
id_from = from.tr('^0-9A-Za-z', '_')
|
|
|
|
id_to = to.tr('^0-9A-Za-z', '_')
|
|
|
|
if from == "UTF-8"
|
|
|
|
tree_name = "to_#{id_to}"
|
|
|
|
elsif to == "UTF-8"
|
|
|
|
tree_name = "from_#{id_from}"
|
|
|
|
else
|
|
|
|
tree_name = "from_#{id_from}_to_#{id_to}"
|
|
|
|
end
|
2010-03-15 08:25:20 -04:00
|
|
|
real_tree_name, max_input = transcode_compile_tree(tree_name, from, map, valid_encoding)
|
2009-01-14 06:12:30 -05:00
|
|
|
return map, tree_name, real_tree_name, max_input
|
|
|
|
end
|
|
|
|
|
2015-12-14 19:14:17 -05:00
|
|
|
#
|
|
|
|
# call-seq:
|
|
|
|
# transcode_tblgen(from_name, to_name, map [, valid_encoding_check [, ascii_compatibility]]) -> ''
|
|
|
|
#
|
|
|
|
# Returns an empty string just in case the result is used somewhere.
|
|
|
|
# Stores the actual product for later output with transcode_generated_code and
|
|
|
|
# transcode_register_code.
|
|
|
|
#
|
|
|
|
# The first argument is a string that will be used for the source (from) encoding.
|
|
|
|
# The second argument is a string that will be used for the target (to) encoding.
|
|
|
|
#
|
|
|
|
# The third argument is the actual data, a map represented as an array of two-element
|
|
|
|
# arrays. Each element of the array stands for one character being converted. The
|
|
|
|
# first element of each subarray is the code of the character in the source encoding,
|
|
|
|
# the second element of each subarray is the code of the character in the target encoding.
|
|
|
|
#
|
|
|
|
# Each code (i.e. byte sequence) is represented as a string of hexadecimal characters
|
|
|
|
# of even length. Codes can also be represented as integers (usually in the form Ox...),
|
|
|
|
# in which case they are interpreted as Unicode codepoints encoded in UTF-8. So as
|
|
|
|
# an example, 0x677E is the same as "E69DBE" (but somewhat easier to produce and check).
|
|
|
|
#
|
|
|
|
# In addition, the following symbols can also be used instead of actual codes in the
|
|
|
|
# second element of a subarray:
|
|
|
|
# :nomap (no mapping, just copy input to output), :nomap0 (same as :nomap, but low priority),
|
|
|
|
# :undef (input code undefined in the destination encoding),
|
|
|
|
# :invalid (input code is an invalid byte sequence in the source encoding),
|
|
|
|
# :func_ii, :func_si, :func_io, :func_so (conversion by function with specific call
|
|
|
|
# convention).
|
2015-12-14 19:16:16 -05:00
|
|
|
#
|
2015-12-14 19:14:17 -05:00
|
|
|
# The forth argument specifies the overall structure of the encoding. For examples,
|
|
|
|
# see ValidEncoding below. This is used to cross-check the data in the third argument
|
|
|
|
# and to automatically add :undef and :invalid mappings where necessary.
|
|
|
|
#
|
|
|
|
# The fifth argument gives the ascii-compatibility of the transcoding. See
|
|
|
|
# rb_transcoder_asciicompat_type_t in transcode_data.h for details. In most
|
|
|
|
# cases, this argument can be left out.
|
|
|
|
#
|
2015-12-14 08:11:31 -05:00
|
|
|
def transcode_tblgen(from, to, map, valid_encoding=UnspecifiedValidEncoding,
|
|
|
|
ascii_compatibility='asciicompat_converter')
|
2010-03-15 08:25:20 -04:00
|
|
|
map, tree_name, real_tree_name, max_input = transcode_tbl_only(from, to, map, valid_encoding)
|
2008-08-05 08:32:13 -04:00
|
|
|
transcoder_name = "rb_#{tree_name}"
|
|
|
|
TRANSCODERS << transcoder_name
|
2008-08-09 02:02:01 -04:00
|
|
|
input_unit_length = UnitLength[from]
|
2008-08-05 08:32:13 -04:00
|
|
|
max_output = map.map {|k,v| String === v ? v.length/2 : 1 }.max
|
|
|
|
transcoder_code = <<"End"
|
|
|
|
static const rb_transcoder
|
|
|
|
#{transcoder_name} = {
|
2008-09-01 12:22:49 -04:00
|
|
|
#{c_esc from}, #{c_esc to}, #{real_tree_name},
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_TABLE_INFO,
|
2008-08-09 02:02:01 -04:00
|
|
|
#{input_unit_length}, /* input_unit_length */
|
|
|
|
#{max_input}, /* max_input */
|
|
|
|
#{max_output}, /* max_output */
|
2015-12-14 08:11:31 -05:00
|
|
|
#{ascii_compatibility}, /* asciicompat_type */
|
2020-02-07 00:14:05 -05:00
|
|
|
0, 0, 0, /* state_size, state_init, state_fini */
|
|
|
|
0, 0, 0, 0,
|
|
|
|
0, 0, 0
|
2008-08-05 08:32:13 -04:00
|
|
|
};
|
|
|
|
End
|
2008-09-01 10:16:30 -04:00
|
|
|
TRANSCODE_GENERATED_TRANSCODER_CODE << transcoder_code
|
2008-08-31 13:35:00 -04:00
|
|
|
''
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2008-08-08 11:48:17 -04:00
|
|
|
def transcode_generate_node(am, name_hint=nil)
|
2020-12-29 02:42:24 -05:00
|
|
|
STDOUT.puts "converter for #{name_hint}" if VERBOSE_MODE > 1
|
2018-05-17 20:38:00 -04:00
|
|
|
am.gennode(TRANSCODE_GENERATED_BYTES_CODE, TRANSCODE_GENERATED_WORDS_CODE, name_hint)
|
2008-08-31 13:35:00 -04:00
|
|
|
''
|
|
|
|
end
|
|
|
|
|
|
|
|
def transcode_generated_code
|
2008-09-03 07:36:04 -04:00
|
|
|
TRANSCODE_GENERATED_BYTES_CODE.to_s +
|
|
|
|
TRANSCODE_GENERATED_WORDS_CODE.to_s +
|
2008-09-03 07:48:17 -04:00
|
|
|
"\#define TRANSCODE_TABLE_INFO " +
|
|
|
|
"#{OUTPUT_PREFIX}byte_array, #{TRANSCODE_GENERATED_BYTES_CODE.length}, " +
|
|
|
|
"#{OUTPUT_PREFIX}word_array, #{TRANSCODE_GENERATED_WORDS_CODE.length}, " +
|
2009-09-22 03:11:45 -04:00
|
|
|
"((int)sizeof(unsigned int))\n" +
|
2008-09-01 14:18:50 -04:00
|
|
|
TRANSCODE_GENERATED_TRANSCODER_CODE
|
2008-08-05 18:47:44 -04:00
|
|
|
end
|
|
|
|
|
2008-08-05 08:32:13 -04:00
|
|
|
def transcode_register_code
|
2015-10-05 09:27:48 -04:00
|
|
|
code = ''.dup
|
2008-08-05 08:32:13 -04:00
|
|
|
TRANSCODERS.each {|transcoder_name|
|
|
|
|
code << " rb_register_transcoder(&#{transcoder_name});\n"
|
|
|
|
}
|
|
|
|
code
|
|
|
|
end
|
|
|
|
|
2008-08-08 11:48:17 -04:00
|
|
|
UnitLength = {
|
|
|
|
'UTF-16BE' => 2,
|
|
|
|
'UTF-16LE' => 2,
|
|
|
|
'UTF-32BE' => 4,
|
|
|
|
'UTF-32LE' => 4,
|
|
|
|
}
|
|
|
|
UnitLength.default = 1
|
|
|
|
|
2008-08-06 07:47:14 -04:00
|
|
|
ValidEncoding = {
|
2013-06-02 10:36:41 -04:00
|
|
|
'1byte' => '{00-ff}',
|
|
|
|
'2byte' => '{00-ff}{00-ff}',
|
|
|
|
'4byte' => '{00-ff}{00-ff}{00-ff}{00-ff}',
|
|
|
|
'US-ASCII' => '{00-7f}',
|
|
|
|
'UTF-8' => '{00-7f}
|
|
|
|
{c2-df}{80-bf}
|
|
|
|
e0{a0-bf}{80-bf}
|
|
|
|
{e1-ec}{80-bf}{80-bf}
|
|
|
|
ed{80-9f}{80-bf}
|
|
|
|
{ee-ef}{80-bf}{80-bf}
|
|
|
|
f0{90-bf}{80-bf}{80-bf}
|
|
|
|
{f1-f3}{80-bf}{80-bf}{80-bf}
|
|
|
|
f4{80-8f}{80-bf}{80-bf}',
|
|
|
|
'UTF-16BE' => '{00-d7,e0-ff}{00-ff}
|
|
|
|
{d8-db}{00-ff}{dc-df}{00-ff}',
|
|
|
|
'UTF-16LE' => '{00-ff}{00-d7,e0-ff}
|
|
|
|
{00-ff}{d8-db}{00-ff}{dc-df}',
|
|
|
|
'UTF-32BE' => '0000{00-d7,e0-ff}{00-ff}
|
|
|
|
00{01-10}{00-ff}{00-ff}',
|
|
|
|
'UTF-32LE' => '{00-ff}{00-d7,e0-ff}0000
|
|
|
|
{00-ff}{00-ff}{01-10}00',
|
|
|
|
'EUC-JP' => '{00-7f}
|
|
|
|
{a1-fe}{a1-fe}
|
|
|
|
8e{a1-fe}
|
|
|
|
8f{a1-fe}{a1-fe}',
|
|
|
|
'CP51932' => '{00-7f}
|
|
|
|
{a1-fe}{a1-fe}
|
|
|
|
8e{a1-fe}',
|
|
|
|
'EUC-JIS-2004' => '{00-7f}
|
|
|
|
{a1-fe}{a1-fe}
|
|
|
|
8e{a1-fe}
|
|
|
|
8f{a1-fe}{a1-fe}',
|
|
|
|
'Shift_JIS' => '{00-7f}
|
|
|
|
{81-9f,e0-fc}{40-7e,80-fc}
|
|
|
|
{a1-df}',
|
|
|
|
'EUC-KR' => '{00-7f}
|
|
|
|
{a1-fe}{a1-fe}',
|
|
|
|
'CP949' => '{00-7f}
|
|
|
|
{81-fe}{41-5a,61-7a,81-fe}',
|
|
|
|
'Big5' => '{00-7f}
|
|
|
|
{81-fe}{40-7e,a1-fe}',
|
|
|
|
'EUC-TW' => '{00-7f}
|
|
|
|
{a1-fe}{a1-fe}
|
|
|
|
8e{a1-b0}{a1-fe}{a1-fe}',
|
|
|
|
'GBK' => '{00-80}
|
|
|
|
{81-fe}{40-7e,80-fe}',
|
|
|
|
'GB18030' => '{00-7f}
|
|
|
|
{81-fe}{40-7e,80-fe}
|
|
|
|
{81-fe}{30-39}{81-fe}{30-39}',
|
2008-08-06 07:47:14 -04:00
|
|
|
}
|
|
|
|
|
2010-03-20 23:38:58 -04:00
|
|
|
def ValidEncoding(enc)
|
|
|
|
ValidEncoding.fetch(enc)
|
|
|
|
end
|
|
|
|
|
2010-03-03 10:11:56 -05:00
|
|
|
def set_valid_byte_pattern(encoding, pattern_or_label)
|
2008-10-18 04:10:57 -04:00
|
|
|
pattern =
|
|
|
|
if ValidEncoding[pattern_or_label]
|
|
|
|
ValidEncoding[pattern_or_label]
|
|
|
|
else
|
|
|
|
pattern_or_label
|
|
|
|
end
|
|
|
|
if ValidEncoding[encoding] and ValidEncoding[encoding]!=pattern
|
|
|
|
raise ArgumentError, "trying to change valid byte pattern for encoding #{encoding} from #{ValidEncoding[encoding]} to #{pattern}"
|
|
|
|
end
|
|
|
|
ValidEncoding[encoding] = pattern
|
|
|
|
end
|
|
|
|
|
|
|
|
# the following may be used in different places, so keep them here for the moment
|
|
|
|
set_valid_byte_pattern 'ASCII-8BIT', '1byte'
|
|
|
|
set_valid_byte_pattern 'Windows-31J', 'Shift_JIS'
|
|
|
|
set_valid_byte_pattern 'eucJP-ms', 'EUC-JP'
|
2008-08-05 08:32:13 -04:00
|
|
|
|
|
|
|
def make_signature(filename, src)
|
|
|
|
"src=#{filename.dump}, len=#{src.length}, checksum=#{src.sum}"
|
|
|
|
end
|
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
if __FILE__ == $0
|
2010-03-12 13:25:13 -05:00
|
|
|
start_time = Time.now
|
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
output_filename = nil
|
2020-12-29 02:42:24 -05:00
|
|
|
verbose_mode = 0
|
2010-03-09 04:15:42 -05:00
|
|
|
force_mode = false
|
|
|
|
|
|
|
|
op = OptionParser.new
|
|
|
|
op.def_option("--help", "show help message") { puts op; exit 0 }
|
2020-12-29 02:42:24 -05:00
|
|
|
op.def_option("--verbose", "verbose mode, twice for more verbose") { verbose_mode += 1 }
|
2010-03-09 04:15:42 -05:00
|
|
|
op.def_option("--force", "force table generation") { force_mode = true }
|
|
|
|
op.def_option("--output=FILE", "specify output file") {|arg| output_filename = arg }
|
|
|
|
op.parse!
|
|
|
|
|
|
|
|
VERBOSE_MODE = verbose_mode
|
|
|
|
|
|
|
|
OUTPUT_FILENAME = output_filename
|
2016-10-28 10:18:50 -04:00
|
|
|
OUTPUT_PREFIX = output_filename ? File.basename(output_filename)[/\A[A-Za-z0-9_]*/] : "".dup
|
2010-03-09 04:15:42 -05:00
|
|
|
OUTPUT_PREFIX.sub!(/\A_+/, '')
|
|
|
|
OUTPUT_PREFIX.sub!(/_*\z/, '_')
|
|
|
|
|
|
|
|
TRANSCODE_GENERATED_BYTES_CODE = ArrayCode.new("unsigned char", "#{OUTPUT_PREFIX}byte_array")
|
|
|
|
TRANSCODE_GENERATED_WORDS_CODE = ArrayCode.new("unsigned int", "#{OUTPUT_PREFIX}word_array")
|
|
|
|
|
|
|
|
arg = ARGV.shift
|
|
|
|
$srcdir = File.dirname(arg)
|
|
|
|
$:.unshift $srcdir unless $:.include? $srcdir
|
|
|
|
src = File.read(arg)
|
|
|
|
src.force_encoding("ascii-8bit") if src.respond_to? :force_encoding
|
|
|
|
this_script = File.read(__FILE__)
|
|
|
|
this_script.force_encoding("ascii-8bit") if this_script.respond_to? :force_encoding
|
|
|
|
|
2015-10-05 09:27:48 -04:00
|
|
|
base_signature = "/* autogenerated. */\n".dup
|
2010-03-09 04:15:42 -05:00
|
|
|
base_signature << "/* #{make_signature(File.basename(__FILE__), this_script)} */\n"
|
|
|
|
base_signature << "/* #{make_signature(File.basename(arg), src)} */\n"
|
|
|
|
|
|
|
|
if !force_mode && output_filename && File.readable?(output_filename)
|
|
|
|
old_signature = File.open(output_filename) {|f| f.gets("").chomp }
|
|
|
|
chk_signature = base_signature.dup
|
|
|
|
old_signature.each_line {|line|
|
|
|
|
if %r{/\* src="([0-9a-z_.-]+)",} =~ line
|
|
|
|
name = $1
|
|
|
|
next if name == File.basename(arg) || name == File.basename(__FILE__)
|
|
|
|
path = File.join($srcdir, name)
|
|
|
|
if File.readable? path
|
|
|
|
chk_signature << "/* #{make_signature(name, File.read(path))} */\n"
|
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
2010-03-09 04:15:42 -05:00
|
|
|
}
|
|
|
|
if old_signature == chk_signature
|
|
|
|
now = Time.now
|
|
|
|
File.utime(now, now, output_filename)
|
2020-12-29 02:42:24 -05:00
|
|
|
STDOUT.puts "already up-to-date: #{output_filename}" if VERBOSE_MODE > 0
|
2010-03-09 04:15:42 -05:00
|
|
|
exit
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-29 02:42:24 -05:00
|
|
|
if VERBOSE_MODE > 0
|
2010-03-09 04:15:42 -05:00
|
|
|
if output_filename
|
2020-12-29 01:57:42 -05:00
|
|
|
STDOUT.puts "generating #{output_filename} ..."
|
2010-03-09 04:15:42 -05:00
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
libs1 = $".dup
|
2018-02-27 06:12:23 -05:00
|
|
|
if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+
|
2018-02-22 08:28:25 -05:00
|
|
|
erb = ERB.new(src, trim_mode: '%')
|
|
|
|
else
|
|
|
|
erb = ERB.new(src, nil, '%')
|
|
|
|
end
|
2010-03-09 04:15:42 -05:00
|
|
|
erb.filename = arg
|
|
|
|
erb_result = erb.result(binding)
|
|
|
|
libs2 = $".dup
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2010-03-09 04:15:42 -05:00
|
|
|
libs = libs2 - libs1
|
2015-10-05 09:27:48 -04:00
|
|
|
lib_sigs = ''.dup
|
2010-03-09 04:15:42 -05:00
|
|
|
libs.each {|lib|
|
|
|
|
lib = File.basename(lib)
|
|
|
|
path = File.join($srcdir, lib)
|
|
|
|
if File.readable? path
|
|
|
|
lib_sigs << "/* #{make_signature(lib, File.read(path))} */\n"
|
|
|
|
end
|
|
|
|
}
|
2008-08-05 08:32:13 -04:00
|
|
|
|
2015-10-05 09:27:48 -04:00
|
|
|
result = ''.dup
|
2010-03-09 04:15:42 -05:00
|
|
|
result << base_signature
|
|
|
|
result << lib_sigs
|
|
|
|
result << "\n"
|
|
|
|
result << erb_result
|
|
|
|
result << "\n"
|
|
|
|
|
|
|
|
if output_filename
|
|
|
|
new_filename = output_filename + ".new"
|
|
|
|
FileUtils.mkdir_p(File.dirname(output_filename))
|
|
|
|
File.open(new_filename, "wb") {|f| f << result }
|
|
|
|
File.rename(new_filename, output_filename)
|
2010-03-12 13:25:13 -05:00
|
|
|
tms = Process.times
|
|
|
|
elapsed = Time.now - start_time
|
2020-12-29 02:42:24 -05:00
|
|
|
STDOUT.puts "done. (#{'%.2f' % tms.utime}user #{'%.2f' % tms.stime}system #{'%.2f' % elapsed}elapsed)" if VERBOSE_MODE > 1
|
2010-03-09 04:15:42 -05:00
|
|
|
else
|
2012-04-06 14:17:01 -04:00
|
|
|
print result
|
2010-03-09 04:15:42 -05:00
|
|
|
end
|
2008-08-05 08:32:13 -04:00
|
|
|
end
|