Debian Codegraph @

ext/ripper/tools/generate.rb

raw
1 # frozen_string_literal: true
2 # $Id$
3
4 require 'optparse'
5
6 def main
7 mode = nil
8 ids1src = nil
9 ids2src = nil
10 output = nil
11
12 parser = @parser = OptionParser.new
13 parser.banner = "Usage: #{File.basename($0)} --mode=MODE [--ids1src=PATH] [--ids2src=PATH] [--output=PATH]"
14 parser.on('--mode=MODE', 'check, eventids1_h, eventids1, or eventids2table.') {|m|
15 mode = m
16 }
17 parser.on('--ids1src=PATH', 'A source file of event-IDs 1 (parse.y).') {|path|
18 ids1src = path
19 }
20 parser.on('--ids2src=PATH', 'A source file of event-IDs 2 (eventids2.c).') {|path|
21 ids2src = path
22 }
23 parser.on('--output=PATH', 'An output file.') {|path|
24 output = path
25 }
26 parser.on('--help', 'Prints this message and quit.') {
27 puts parser.help
28 exit true
29 }
30 begin
31 parser.parse!
32 rescue OptionParser::ParseError => err
33 usage err.message
34 end
35 usage 'no mode given' unless mode
36 case mode
37 when 'check'
38 usage 'no --ids1src' unless ids1src
39 usage 'no --ids2src' unless ids2src
40 h = read_ids1_with_locations(ids1src)
41 check_arity h
42 ids2 = read_ids2(ids2src)
43 common = h.keys & ids2
44 unless common.empty?
45 abort "event crash: #{common.join(' ')}"
46 end
47 exit 0
48 when 'eventids1_h'
49 usage 'no --ids1src' unless ids1src
50 result = generate_eventids1_h(read_ids1(ids1src))
51 when 'eventids1'
52 usage 'no --ids1src' unless ids1src
53 result = generate_eventids1(read_ids1(ids1src))
54 when 'eventids2table'
55 usage 'no --ids2src' unless ids2src
56 result = generate_eventids2_table(read_ids2(ids2src))
57 end
58 if output
59 File.open(output, 'w') {|f|
60 f.write result
61 }
62 else
63 puts result
64 end
65 end
66
67 def usage(msg)
68 $stderr.puts msg
69 $stderr.puts @parser.help
70 exit false
71 end
72
73 def generate_eventids1_h(ids)
74 buf = "".dup
75 buf << %Q[#ifndef RIPPER_EVENTIDS1\n]
76 buf << %Q[#define RIPPER_EVENTIDS1\n]
77 buf << %Q[\n]
78 buf << %Q[void ripper_init_eventids1(void);\n]
79 buf << %Q[void ripper_init_eventids1_table(VALUE self);\n]
80 buf << %Q[\n]
81 buf << %Q[struct ripper_parser_ids {\n]
82 ids.each do |id, arity|
83 buf << %Q[ ID id_#{id};\n]
84 end
85 buf << %Q[};\n]
86 buf << %Q[\n]
87 ids.each do |id, arity|
88 buf << %Q[#define ripper_id_#{id} ripper_parser_ids.id_#{id}\n]
89 end
90 buf << %Q[#endif /* RIPPER_EVENTIDS1 */\n]
91 buf << %Q[\n]
92 end
93
94 def generate_eventids1(ids)
95 buf = "".dup
96 buf << %Q[#include "ruby/ruby.h"\n]
97 buf << %Q[#include "eventids1.h"\n]
98 buf << %Q[\n]
99 buf << %Q[struct ripper_parser_ids ripper_parser_ids;\n]
100 buf << %Q[\n]
101 buf << %Q[void\n]
102 buf << %Q[ripper_init_eventids1(void)\n]
103 buf << %Q[{\n]
104 buf << %Q[#define set_id1(name) ripper_id_##name = rb_intern_const("on_"#name)\n]
105 ids.each do |id, arity|
106 buf << %Q[ set_id1(#{id});\n]
107 end
108 buf << %Q[}\n]
109 buf << %Q[\n]
110 buf << %Q[#define intern_sym(name) ID2SYM(rb_intern_const(name))\n]
111 buf << %Q[\n]
112 buf << %Q[void\n]
113 buf << %Q[ripper_init_eventids1_table(VALUE self)\n]
114 buf << %Q[{\n]
115 buf << %Q[ VALUE h = rb_hash_new();\n]
116 buf << %Q[ rb_define_const(self, "PARSER_EVENT_TABLE", h);\n]
117 ids.each do |id, arity|
118 buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(#{arity}));\n]
119 end
120 buf << %Q[}\n]
121 buf
122 end
123
124 def generate_eventids2_table(ids)
125 buf = "".dup
126 buf << %Q[#include "ruby/ruby.h"\n]
127 buf << %Q[\n]
128 buf << %Q[#define intern_sym(name) ID2SYM(rb_intern_const(name))\n]
129 buf << %Q[\n]
130 buf << %Q[void\n]
131 buf << %Q[ripper_init_eventids2_table(VALUE self)\n]
132 buf << %Q[{\n]
133 buf << %Q[ VALUE h = rb_hash_new();\n]
134 buf << %Q[ rb_define_const(self, "SCANNER_EVENT_TABLE", h);\n]
135 ids.each do |id|
136 buf << %Q[ rb_hash_aset(h, intern_sym("#{id}"), INT2FIX(1));\n]
137 end
138 buf << %Q[}\n]
139 buf
140 end
141
142 def read_ids1(path)
143 strip_locations(read_ids1_with_locations(path))
144 end
145
146 def strip_locations(h)
147 h.map {|event, list| [event, list.first[1]] }\
148 .sort_by {|event, arity| event.to_s }
149 end
150
151 def check_arity(h)
152 invalid = false
153 h.each do |event, list|
154 unless list.map {|line, arity| arity }.uniq.size == 1
155 invalid = true
156 locations = list.map {|line, a| "#{line}:#{a}" }.join(', ')
157 $stderr.puts "arity crash [event=#{event}]: #{locations}"
158 end
159 end
160 abort if invalid
161 end
162
163 require_relative "dsl"
164
165 def read_ids1_with_locations(path)
166 h = {}
167 File.open(path) {|f|
168 f.each do |line|
169 next if /\A\#\s*define\s+dispatch/ =~ line
170 next if /ripper_dispatch/ =~ line
171 line.scan(/\bdispatch(\d)\((\w+)/) do |arity, event|
172 (h[event] ||= []).push [f.lineno, arity.to_i]
173 end
174 if line =~ %r</\*% *ripper(?:\[(.*?)\])?: *(.*?) *%\*/>
175 gen = DSL.new($2, ($1 || "").split(","))
176 gen.generate
177 gen.events.each do |event, arity|
178 (h[event] ||= []).push [f.lineno, arity.to_i]
179 end
180 end
181 end
182 }
183 h
184 end
185
186 def read_ids2(path)
187 src = File.open(path) {|f| f.read}
188 ids2 = src.scan(/ID\s+ripper_id_(\w+)/).flatten.uniq.sort
189 diff = src.scan(/set_id2\((\w+)\);/).flatten - ids2
190 unless diff.empty?
191 abort "missing scanner IDs: #{diff}"
192 end
193 return ids2
194 end
195
196 main