eturem 0.3.2 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/eturem.rb +12 -10
- data/lib/eturem/base.rb +69 -104
- data/lib/eturem/en.rb +2 -2
- data/lib/eturem/ja.rb +58 -42
- data/lib/eturem/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64d7d985440e56ceded0d1713d1bb8e3a7f42285
|
4
|
+
data.tar.gz: f8a30a97dba553b7e7c2f0de962b2551e6ff383a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc2a5be8cd2f5471f15f0a635a6dac10e3e28eccaf169f792d64c34cd854c7b9798b2f96049ee47ffca20daf07e7885cc9677549608b161aed2478201634b051
|
7
|
+
data.tar.gz: d62c5c694dbf69cee649e34dbf1cb36c2223c0cc78bf375d8d07c41d03a381fce26a3167378f42235af36f1d53f33ce0bdb9ec60b9987a2292efe7d57e6a94f9
|
data/lib/eturem.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
enable = true
|
2
|
+
debug = false
|
2
3
|
lang = "en"
|
3
4
|
output_backtrace = true
|
4
5
|
output_original = true
|
@@ -12,6 +13,7 @@ config_file = File.join(Dir.home, ".eturem")
|
|
12
13
|
if File.exist?(config_file)
|
13
14
|
config = File.read(config_file)
|
14
15
|
enable = false if config.match(/^enable\s*\:\s*(?:false|off|0)/i)
|
16
|
+
debug = true if config.match(/^debug\s*\:\s*(?:true|on|1)/i)
|
15
17
|
lang = Regexp.last_match(:lang) if config.match(/^lang\s*\:\s*(?<lang>\S+)/i)
|
16
18
|
output_backtrace = false if config.match(/^output_backtrace\s*\:\s*(?:false|off|0)/i)
|
17
19
|
output_original = false if config.match( /^output_original\s*\:\s*(?:false|off|0)/i)
|
@@ -24,16 +26,16 @@ end
|
|
24
26
|
require "eturem/#{lang}" if enable && !defined?(Eturem)
|
25
27
|
|
26
28
|
if defined? Eturem
|
27
|
-
Eturem.
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
29
|
+
Eturem.set_config({
|
30
|
+
output_backtrace: output_backtrace,
|
31
|
+
output_original: output_original,
|
32
|
+
output_script: output_script,
|
33
|
+
use_coderay: use_coderay,
|
34
|
+
max_backtrace: max_backtrace,
|
35
|
+
before_line_num: before_line_num,
|
36
|
+
after_line_num: after_line_num
|
37
|
+
})
|
34
38
|
|
35
|
-
|
36
|
-
exception = Eturem.load($0)
|
37
|
-
exception.output if exception
|
39
|
+
exception = Eturem.load_and_output($0, debug)
|
38
40
|
exit
|
39
41
|
end
|
data/lib/eturem/base.rb
CHANGED
@@ -7,28 +7,36 @@ module Eturem
|
|
7
7
|
# @return [nil] if exception did not raise
|
8
8
|
def self.load(file)
|
9
9
|
begin
|
10
|
-
Kernel.load(File.
|
10
|
+
Kernel.load(File.absolute_path(file))
|
11
11
|
rescue Exception => exception
|
12
|
-
return @eturem_class.new(exception) unless exception.is_a? SystemExit
|
12
|
+
return @eturem_class.new(exception, file) unless exception.is_a? SystemExit
|
13
13
|
end
|
14
14
|
return nil
|
15
15
|
end
|
16
16
|
|
17
|
+
def self.load_and_output(file, debug = false)
|
18
|
+
begin
|
19
|
+
Kernel.load(File.absolute_path(file))
|
20
|
+
rescue Exception => exception
|
21
|
+
begin
|
22
|
+
puts @eturem_class.new(exception, file).inspect unless exception.is_a? SystemExit
|
23
|
+
rescue Exception => e
|
24
|
+
raise debug ? e : exception
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
17
29
|
def self.eval(expr, bind = nil, fname = "(eval)", lineno = 1)
|
18
30
|
begin
|
19
31
|
bind ? Kernel.eval(expr, bind, fname, lineno) : Kernel.eval(expr)
|
20
32
|
rescue Exception => exception
|
21
|
-
return @eturem_class.new(exception) unless exception.is_a? SystemExit
|
33
|
+
return @eturem_class.new(exception, fname) unless exception.is_a? SystemExit
|
22
34
|
end
|
23
35
|
return nil
|
24
36
|
end
|
25
37
|
|
26
|
-
def self.
|
27
|
-
@eturem_class
|
28
|
-
end
|
29
|
-
|
30
|
-
def self.eturem_class=(klass)
|
31
|
-
@eturem_class = klass
|
38
|
+
def self.set_config(config)
|
39
|
+
@eturem_class.set_config(config)
|
32
40
|
end
|
33
41
|
|
34
42
|
class Base
|
@@ -48,35 +56,17 @@ module Eturem
|
|
48
56
|
return @inspect_methods
|
49
57
|
end
|
50
58
|
|
51
|
-
def self.
|
52
|
-
@@output_backtrace =
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
@@
|
57
|
-
|
58
|
-
|
59
|
-
def self.output_script=(value)
|
60
|
-
@@output_script = value
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.use_coderay=(value)
|
64
|
-
@@use_coderay = value
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.max_backtrace=(value)
|
68
|
-
@@max_backtrace = value
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.before_line_num=(value)
|
72
|
-
@@before_line_num = value
|
59
|
+
def self.set_config(config)
|
60
|
+
@@output_backtrace = config[:output_backtrace] if config.has_key?(:output_backtrace)
|
61
|
+
@@output_original = config[:output_original] if config.has_key?(:output_original)
|
62
|
+
@@output_script = config[:output_script] if config.has_key?(:output_script)
|
63
|
+
@@use_coderay = config[:use_coderay] if config.has_key?(:use_coderay)
|
64
|
+
@@max_backtrace = config[:max_backtrace] if config.has_key?(:max_backtrace)
|
65
|
+
@@before_line_num = config[:before_line_num] if config.has_key?(:before_line_num)
|
66
|
+
@@after_line_num = config[:after_line_num] if config.has_key?(:after_line_num)
|
73
67
|
end
|
74
68
|
|
75
|
-
def
|
76
|
-
@@after_line_num = value
|
77
|
-
end
|
78
|
-
|
79
|
-
def initialize(exception)
|
69
|
+
def initialize(exception, load_file)
|
80
70
|
@exception = exception
|
81
71
|
@exception_s = exception.to_s
|
82
72
|
|
@@ -86,7 +76,12 @@ module Eturem
|
|
86
76
|
path.start_with?(eturem_path) || path.end_with?("/rubygems/core_ext/kernel_require.rb")
|
87
77
|
end
|
88
78
|
@backtrace_locations.each do |location|
|
89
|
-
if
|
79
|
+
if File.absolute_path(load_file) == location.path
|
80
|
+
if load_file == $0
|
81
|
+
def location.path
|
82
|
+
$0
|
83
|
+
end
|
84
|
+
end
|
90
85
|
def location.label
|
91
86
|
super.sub("<top (required)>", "<main>")
|
92
87
|
end
|
@@ -104,26 +99,28 @@ module Eturem
|
|
104
99
|
prepare
|
105
100
|
end
|
106
101
|
|
107
|
-
|
108
|
-
|
109
|
-
|
102
|
+
def inspect
|
103
|
+
str = ""
|
104
|
+
str = backtrace_inspect if @@output_backtrace
|
110
105
|
error_message = exception_inspect
|
111
|
-
if error_message.
|
112
|
-
|
106
|
+
if error_message.empty?
|
107
|
+
str += original_exception_inspect
|
113
108
|
else
|
114
|
-
|
115
|
-
|
109
|
+
str += original_exception_inspect + "\n" if @@output_original
|
110
|
+
str += error_message + "\n"
|
116
111
|
end
|
117
|
-
|
112
|
+
str += script_inspect if @@output_script
|
113
|
+
return str
|
118
114
|
end
|
119
115
|
|
120
|
-
def
|
121
|
-
return if @backtrace_locations.empty?
|
116
|
+
def backtrace_inspect
|
117
|
+
return "" if @backtrace_locations.empty?
|
122
118
|
|
123
|
-
|
119
|
+
str = traceback_most_recent_call_last + "\n"
|
124
120
|
@backtrace_locations[0, @@max_backtrace].reverse.each_with_index do |location, i|
|
125
|
-
|
121
|
+
str += sprintf("%9d: %s\n", @backtrace_locations.size - i, location_inspect(location))
|
126
122
|
end
|
123
|
+
return str
|
127
124
|
end
|
128
125
|
|
129
126
|
def exception_inspect
|
@@ -132,10 +129,10 @@ module Eturem
|
|
132
129
|
if (key.is_a?(Class) && @exception.is_a?(key)) ||
|
133
130
|
(key.is_a?(String) && @exception.class.to_s == key)
|
134
131
|
method = inspect_methods[key]
|
135
|
-
return method ? public_send(method) :
|
132
|
+
return method ? public_send(method) : ""
|
136
133
|
end
|
137
134
|
end
|
138
|
-
return
|
135
|
+
return ""
|
139
136
|
end
|
140
137
|
|
141
138
|
def original_exception_inspect
|
@@ -143,48 +140,33 @@ module Eturem
|
|
143
140
|
return @exception_s
|
144
141
|
else
|
145
142
|
location_str = "#{@path}:#{@lineno}:in `#{@label}'"
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
else
|
150
|
-
return "#{location_str}: #{@exception_s} (#{@exception.class})"
|
151
|
-
end
|
143
|
+
@exception_s.match(/\A(?<first_line>.*)/)
|
144
|
+
return "#{location_str}: #{Regexp.last_match(:first_line)} (\e[4m#{@exception.class}\e[0m)" +
|
145
|
+
"#{Regexp.last_match.post_match.chomp}\n"
|
152
146
|
end
|
153
147
|
end
|
154
148
|
|
155
|
-
def
|
156
|
-
return if @script.empty?
|
149
|
+
def script_inspect
|
150
|
+
return "" if @script.empty?
|
157
151
|
|
158
|
-
|
159
|
-
@output_lines.
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
152
|
+
str = ""
|
153
|
+
max_lineno_length = @output_lines.max.to_s.length
|
154
|
+
last_i = @output_lines.min - 1
|
155
|
+
@output_lines.sort.each do |i|
|
156
|
+
str += "\e[0m #{' ' * max_lineno_length} :\n" if last_i + 1 != i
|
157
|
+
str += @lineno == i ? "\e[0m => \e[1;34m" : "\e[0m \e[1;34m"
|
158
|
+
str += sprintf("%#{max_lineno_length}d\e[0m: %s", i, @script_lines[i])
|
159
|
+
last_i = i
|
167
160
|
end
|
161
|
+
return str
|
168
162
|
end
|
169
163
|
|
170
164
|
private
|
171
165
|
|
172
166
|
def prepare
|
173
167
|
case @exception
|
174
|
-
when SyntaxError then prepare_syntax_error
|
175
168
|
when NameError then prepare_name_error
|
176
169
|
when ArgumentError then prepare_argument_error
|
177
|
-
when TypeError then prepare_type_error
|
178
|
-
end
|
179
|
-
end
|
180
|
-
|
181
|
-
def prepare_syntax_error
|
182
|
-
@unexpected = @exception_s.match(/unexpected (?<unexpected>(?:','|[^,])+)/) ?
|
183
|
-
Regexp.last_match(:unexpected) : nil
|
184
|
-
@expected = @exception_s.match(/[,\s]expecting (?<expected>\S+)/) ?
|
185
|
-
Regexp.last_match(:expected) : nil
|
186
|
-
if !@expected && @exception_s.match(/(?<invalid>(?:break|next|retry|redo|yield))/)
|
187
|
-
@invalid = Regexp.last_match(:invalid)
|
188
170
|
end
|
189
171
|
end
|
190
172
|
|
@@ -194,23 +176,11 @@ module Eturem
|
|
194
176
|
@did_you_mean = Regexp.last_match.post_match.strip.scan(/\S+/)
|
195
177
|
return if @script.empty?
|
196
178
|
|
197
|
-
new_range = []
|
198
179
|
@did_you_mean.each do |name|
|
199
180
|
index = @script_lines.index { |line| line.include?(name) }
|
200
181
|
next unless index
|
201
182
|
highlight!(@script_lines[index], name, "\e[1;33m")
|
202
|
-
|
203
|
-
end
|
204
|
-
new_range.sort!
|
205
|
-
before = new_range.select { |i| i < @output_lines.first }
|
206
|
-
after = new_range.select { |i| i > @output_lines.last }
|
207
|
-
unless before.empty?
|
208
|
-
@output_lines.unshift(nil) if before.last + 1 != @output_lines.first
|
209
|
-
@output_lines.unshift(*before)
|
210
|
-
end
|
211
|
-
unless after.empty?
|
212
|
-
@output_lines.push(nil) if @output_lines.last + 1 != after.first
|
213
|
-
@output_lines.push(*after)
|
183
|
+
@output_lines.push(index)
|
214
184
|
end
|
215
185
|
end
|
216
186
|
|
@@ -220,14 +190,6 @@ module Eturem
|
|
220
190
|
backtrace_locations_shift
|
221
191
|
load_script unless old_path == @path
|
222
192
|
@output_lines = default_output_lines
|
223
|
-
if @exception_s.match(/given (?<given>\d+)\, expected (?<expected>[^)]+)/)
|
224
|
-
@given = Regexp.last_match(:given).to_i
|
225
|
-
@expected = Regexp.last_match(:expected)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
def prepare_type_error
|
230
|
-
@method = @label
|
231
193
|
end
|
232
194
|
|
233
195
|
def backtrace_locations_shift
|
@@ -255,7 +217,10 @@ module Eturem
|
|
255
217
|
end
|
256
218
|
@script.force_encoding(encoding)
|
257
219
|
end
|
258
|
-
|
220
|
+
if @@use_coderay
|
221
|
+
require "coderay"
|
222
|
+
@script = CodeRay.scan(@script, :ruby).terminal
|
223
|
+
end
|
259
224
|
@script_lines = @script.lines
|
260
225
|
@script_lines.unshift("")
|
261
226
|
@output_lines = default_output_lines
|
@@ -274,7 +239,7 @@ module Eturem
|
|
274
239
|
to = [@script_lines.size - 1, @lineno + @@after_line_num].min
|
275
240
|
(from..to).to_a
|
276
241
|
end
|
277
|
-
|
278
|
-
Eturem.eturem_class = self
|
279
242
|
end
|
243
|
+
|
244
|
+
@eturem_class = Base
|
280
245
|
end
|
data/lib/eturem/en.rb
CHANGED
data/lib/eturem/ja.rb
CHANGED
@@ -214,11 +214,10 @@ module Eturem
|
|
214
214
|
end
|
215
215
|
|
216
216
|
def exception_inspect
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
end
|
217
|
+
error_message = super
|
218
|
+
error_message = "#{@exception_s} (#{@exception.class})" if error_message.empty?
|
219
|
+
return (@path == "(eval)" ? "eval 中の" : %[ファイル"#{@path}"]) +
|
220
|
+
" #{@lineno}行目でエラーが発生しました。\n" + error_message
|
222
221
|
end
|
223
222
|
|
224
223
|
def no_memory_error_inspect
|
@@ -231,37 +230,50 @@ module Eturem
|
|
231
230
|
end
|
232
231
|
|
233
232
|
def syntax_error_inspect
|
234
|
-
if @
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
233
|
+
if @exception_s.match(/unexpected (?<unexpected>(?:','|[^,])+)/)
|
234
|
+
unexpected = transform_syntax_error_keyword(Regexp.last_match(:unexpected))
|
235
|
+
expecting = ["end-of-input"]
|
236
|
+
if @exception_s.match(/[,\s]expecting (?<expecting>\S+)/)
|
237
|
+
expecting = Regexp.last_match(:expecting).split(/\s+or\s+/).map do |exp|
|
238
|
+
transform_syntax_error_keyword(exp)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
if unexpected.match(/^'(.)'$/)
|
242
|
+
highlight!(@script_lines[@lineno], Regexp.last_match(1), "\e[1;31m\e[4m")
|
243
|
+
elsif
|
244
|
+
highlight!(@script_lines[@lineno], unexpected, "\e[1;31m\e[4m")
|
245
|
+
end
|
246
|
+
|
247
|
+
keywords = %w[if unless case while until for begin def class module do].select do |keyword|
|
248
|
+
@script.index(keyword)
|
249
|
+
end
|
250
|
+
keywords = "「#{keywords.join(' / ')}」"
|
251
|
+
keywords = "ifなど" if keywords == "「」"
|
252
|
+
if unexpected == "end-of-input"
|
253
|
+
ret = "(ただし、実際のエラーの原因はおそらくもっと前にあります。)\n" +
|
254
|
+
"構文エラーです。#{expecting.join('または')}が足りません。"
|
255
|
+
ret += "#{keywords}に対応する「end」があるか確認してください。" if expecting.include?("end")
|
256
|
+
return ret
|
257
|
+
elsif expecting.join == "end-of-input"
|
258
|
+
ret = "構文エラーです。余分な#{unexpected}があります。"
|
259
|
+
ret += "#{keywords}と「end」の対応関係を確認してください。" if unexpected == "end"
|
260
|
+
return ret
|
261
|
+
elsif !expecting.empty?
|
262
|
+
return "構文エラーです。#{expecting.join('または')}が来るべき場所に、" +
|
263
|
+
"#{unexpected}が来てしまいました。"
|
264
|
+
end
|
265
|
+
return ""
|
266
|
+
elsif @exception_s.match(/Invalid (?<invalid>(?:break|next|retry|redo|yield))/)
|
267
|
+
invalid = Regexp.last_match(:invalid)
|
268
|
+
highlight!(@script_lines[@lineno], invalid, "\e[1;31m\e[4m")
|
269
|
+
return "#{invalid} が不適切な場所にあります。"
|
270
|
+
elsif @exception_s.match(/unterminated string meets end of file/)
|
271
|
+
return "「\"」「'」が閉じられていません。"
|
272
|
+
elsif @exception_s.match(/unterminated regexp meets end of file/)
|
273
|
+
return "「/」が閉じられていません。"
|
274
|
+
else
|
275
|
+
return ""
|
262
276
|
end
|
263
|
-
return "構文エラーです。#{expected.join('または')}が来るべき場所に、" +
|
264
|
-
"#{unexpected}が来てしまいました。"
|
265
277
|
end
|
266
278
|
|
267
279
|
def transform_syntax_error_keyword(keyword)
|
@@ -282,20 +294,24 @@ module Eturem
|
|
282
294
|
end
|
283
295
|
|
284
296
|
def argument_error_inspect
|
285
|
-
if @given
|
297
|
+
if @exception_s.match(/given (?<given>\d+), expected (?<expected>[^)]+)/)
|
298
|
+
given = Regexp.last_match(:given).to_i
|
299
|
+
expected = Regexp.last_match(:expected)
|
286
300
|
ret = "引数の数が正しくありません。「#{@method}」は本来"
|
287
|
-
case
|
301
|
+
case expected
|
288
302
|
when "0"
|
289
303
|
ret += "引数が不要です"
|
290
304
|
when /^(\d+)\.\.(\d+)$/
|
291
305
|
ret += "#{Regexp.last_match(1)}~#{Regexp.last_match(2)}個の引数を取ります"
|
292
306
|
when /^(\d+)\+$/
|
293
307
|
ret += "#{Regexp.last_match(1)}個以上の引数を取ります"
|
308
|
+
else
|
309
|
+
ret += "#{expected}個の引数を取ります"
|
294
310
|
end
|
295
|
-
if
|
311
|
+
if given == 0
|
296
312
|
ret += "が、引数が1つも渡されていません。"
|
297
313
|
else
|
298
|
-
ret += "が、#{
|
314
|
+
ret += "が、#{given}個の引数が渡されています。"
|
299
315
|
end
|
300
316
|
return ret
|
301
317
|
else
|
@@ -328,7 +344,7 @@ module Eturem
|
|
328
344
|
end
|
329
345
|
|
330
346
|
def type_error_inspect
|
331
|
-
"「#{@
|
347
|
+
"「#{@label}」への引数のタイプ(型)が正しくありません。"
|
332
348
|
end
|
333
349
|
|
334
350
|
def zero_division_error_inspect
|
@@ -338,9 +354,9 @@ module Eturem
|
|
338
354
|
def system_stack_error_inspect
|
339
355
|
"システムスタックがあふれました。意図しない無限ループが生じている可能性があります。"
|
340
356
|
end
|
341
|
-
|
342
|
-
Eturem.eturem_class = self
|
343
357
|
end
|
358
|
+
|
359
|
+
@eturem_class = Ja
|
344
360
|
end
|
345
361
|
|
346
362
|
require "eturem"
|
data/lib/eturem/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eturem
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- nodai2hITC
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|