Debian Codegraph @

vendor/mruby-source/lib/mruby/presym.rb

raw
1 module MRuby
2 class Presym
3 OPERATORS = {
4 "!" => "not",
5 "%" => "mod",
6 "&" => "and",
7 "*" => "mul",
8 "+" => "add",
9 "-" => "sub",
10 "/" => "div",
11 "<" => "lt",
12 ">" => "gt",
13 "^" => "xor",
14 "`" => "tick",
15 "|" => "or",
16 "~" => "neg",
17 "!=" => "neq",
18 "!~" => "nmatch",
19 "&&" => "andand",
20 "**" => "pow",
21 "+@" => "plus",
22 "-@" => "minus",
23 "<<" => "lshift",
24 "<=" => "le",
25 "==" => "eq",
26 "=~" => "match",
27 ">=" => "ge",
28 ">>" => "rshift",
29 "[]" => "aref",
30 "||" => "oror",
31 "<=>" => "cmp",
32 "===" => "eqq",
33 "[]=" => "aset",
34 }.freeze
35
36 SYMBOL_TO_MACRO = {
37 # Symbol => Macro
38 # [prefix, suffix] => [prefix, suffix]
39 ["@@" , "" ] => ["CV" , "" ],
40 ["@" , "" ] => ["IV" , "" ],
41 ["" , "!" ] => ["" , "_B" ],
42 ["" , "?" ] => ["" , "_Q" ],
43 ["" , "=" ] => ["" , "_E" ],
44 ["" , "" ] => ["" , "" ],
45 }.freeze
46
47 C_STR_LITERAL_RE = /"(?:[^\\\"]|\\.)*"/
48
49 ESCAPE_SEQUENCE_MAP = {
50 "a" => "\a",
51 "b" => "\b",
52 "e" => "\e",
53 "f" => "\f",
54 "n" => "\n",
55 "r" => "\r",
56 "t" => "\t",
57 "v" => "\v",
58 }
59 ESCAPE_SEQUENCE_MAP.keys.each { |k| ESCAPE_SEQUENCE_MAP[ESCAPE_SEQUENCE_MAP[k]] = k }
60
61 def initialize(build)
62 @build = build
63 end
64
65 def scan(paths)
66 presym_hash = {}
67 paths.each {|path| read_preprocessed(presym_hash, path)}
68 presym_hash.keys.sort_by!{|sym| [c_literal_size(sym), sym]}
69 end
70
71 def read_list
72 File.readlines(list_path, mode: "r:binary").each(&:chomp!)
73 end
74
75 def write_list(presyms)
76 _pp "GEN", list_path.relative_path
77 File.binwrite(list_path, presyms.join("\n") << "\n")
78 end
79
80 def write_id_header(presyms)
81 prefix_re = Regexp.union(*SYMBOL_TO_MACRO.keys.map(&:first).uniq)
82 suffix_re = Regexp.union(*SYMBOL_TO_MACRO.keys.map(&:last).uniq)
83 sym_re = /\A(#{prefix_re})?([\w&&\D]\w*)(#{suffix_re})?\z/o
84 _pp "GEN", id_header_path.relative_path
85 File.open(id_header_path, "w:binary") do |f|
86 f.puts "enum mruby_presym {"
87 presyms.each.with_index(1) do |sym, num|
88 if sym_re =~ sym && (affixes = SYMBOL_TO_MACRO[[$1, $3]])
89 f.puts " MRB_#{affixes * 'SYM'}__#{$2} = #{num},"
90 elsif name = OPERATORS[sym]
91 f.puts " MRB_OPSYM__#{name} = #{num},"
92 end
93 end
94 f.puts "};"
95 f.puts
96 f.puts "#define MRB_PRESYM_MAX #{presyms.size}"
97 end
98 end
99
100 def write_table_header(presyms)
101 _pp "GEN", table_header_path.relative_path
102 File.open(table_header_path, "w:binary") do |f|
103 f.puts "static const uint16_t presym_length_table[] = {"
104 presyms.each{|sym| f.puts " #{sym.bytesize},\t/* #{sym} */"}
105 f.puts "};"
106 f.puts
107 f.puts "static const char * const presym_name_table[] = {"
108 presyms.each do |sym|
109 sym = sym.gsub(/([\x01-\x1f\x7f-\xff])|("|\\)/n) {
110 case
111 when $1
112 e = ESCAPE_SEQUENCE_MAP[$1]
113 e ? "\\#{e}" : '\\x%02x""' % $1.ord
114 when $2
115 "\\#$2"
116 end
117 }
118 f.puts %| "#{sym}",|
119 end
120 f.puts "};"
121 end
122 end
123
124 def list_path
125 @list_path ||= "#{@build.build_dir}/presym".freeze
126 end
127
128 def header_dir
129 @header_dir ||= "#{@build.build_dir}/include/mruby/presym".freeze
130 end
131
132 def id_header_path
133 @id_header_path ||= "#{header_dir}/id.h".freeze
134 end
135
136 def table_header_path
137 @table_header_path ||= "#{header_dir}/table.h".freeze
138 end
139
140 private
141
142 def read_preprocessed(presym_hash, path)
143 File.binread(path).scan(/<@! (.*?) !@>/) do |part,|
144 literals = part.scan(C_STR_LITERAL_RE)
145 unless literals.empty?
146 literals = literals.map{|l| l[1..-2]}
147 literals.each do |e|
148 e.gsub!(/\\x([0-9A-Fa-f]{1,2})|\\(0[0-7]{,3})|\\([abefnrtv])|\\(.)/) do
149 case
150 when $1; $1.hex.chr(Encoding::BINARY)
151 when $2; $2.oct.chr(Encoding::BINARY)
152 when $3; ESCAPE_SEQUENCE_MAP[$3]
153 when $4; $4
154 end
155 end
156 end
157 presym_hash[literals.join] = true
158 end
159 end
160 end
161
162 def c_literal_size(literal_without_quote)
163 literal_without_quote.size # TODO: consider escape sequence
164 end
165 end
166 end