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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cafde349114833d7d2ec857ff349c9b90be5d1ec
4
- data.tar.gz: d80d846f4b2e628dab97a0e81cec9f64d3a162f9
3
+ metadata.gz: 64d7d985440e56ceded0d1713d1bb8e3a7f42285
4
+ data.tar.gz: f8a30a97dba553b7e7c2f0de962b2551e6ff383a
5
5
  SHA512:
6
- metadata.gz: e7614e84b0e772b45d733c2fa25587855c314c7b10bdaf5052a4647ab2a666da4c08b233dfafb921ca7f8a3bcfd47833232a9488146d14221d242adf7d81b884
7
- data.tar.gz: 2c6cc910538e61fb935788b9f47390129f7d1896caeebb170bd14dde8c986232b04eaa1d91d2c190deb3a7f42d941a9d47bbf4b7afbc24e872d1bee0953c7131
6
+ metadata.gz: fc2a5be8cd2f5471f15f0a635a6dac10e3e28eccaf169f792d64c34cd854c7b9798b2f96049ee47ffca20daf07e7885cc9677549608b161aed2478201634b051
7
+ data.tar.gz: d62c5c694dbf69cee649e34dbf1cb36c2223c0cc78bf375d8d07c41d03a381fce26a3167378f42235af36f1d53f33ce0bdb9ec60b9987a2292efe7d57e6a94f9
@@ -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.eturem_class.output_backtrace = output_backtrace
28
- Eturem.eturem_class.output_original = output_original
29
- Eturem.eturem_class.output_script = output_script
30
- Eturem.eturem_class.use_coderay = use_coderay
31
- Eturem.eturem_class.max_backtrace = max_backtrace
32
- Eturem.eturem_class.before_line_num = before_line_num
33
- Eturem.eturem_class.after_line_num = after_line_num
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
- require "coderay" if use_coderay
36
- exception = Eturem.load($0)
37
- exception.output if exception
39
+ exception = Eturem.load_and_output($0, debug)
38
40
  exit
39
41
  end
@@ -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.expand_path(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.eturem_class
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.output_backtrace=(value)
52
- @@output_backtrace = value
53
- end
54
-
55
- def self.output_original=(value)
56
- @@output_original = value
57
- end
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 self.after_line_num=(value)
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 $0 == location.path
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
- # output backtrace + error message + script
108
- def output
109
- output_backtrace if @@output_backtrace
102
+ def inspect
103
+ str = ""
104
+ str = backtrace_inspect if @@output_backtrace
110
105
  error_message = exception_inspect
111
- if error_message.to_s.empty?
112
- puts original_exception_inspect
106
+ if error_message.empty?
107
+ str += original_exception_inspect
113
108
  else
114
- puts original_exception_inspect if @@output_original
115
- puts error_message
109
+ str += original_exception_inspect + "\n" if @@output_original
110
+ str += error_message + "\n"
116
111
  end
117
- output_script if @@output_script
112
+ str += script_inspect if @@output_script
113
+ return str
118
114
  end
119
115
 
120
- def output_backtrace
121
- return if @backtrace_locations.empty?
116
+ def backtrace_inspect
117
+ return "" if @backtrace_locations.empty?
122
118
 
123
- puts traceback_most_recent_call_last
119
+ str = traceback_most_recent_call_last + "\n"
124
120
  @backtrace_locations[0, @@max_backtrace].reverse.each_with_index do |location, i|
125
- puts sprintf("%9d: %s", @backtrace_locations.size - i, location_inspect(location))
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) : nil
132
+ return method ? public_send(method) : ""
136
133
  end
137
134
  end
138
- return nil
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
- if @exception_s.match(/\A(?<first_line>.*?)\n/)
147
- return "#{location_str}: #{Regexp.last_match(:first_line)} (#{@exception.class})\n" +
148
- "#{Regexp.last_match.post_match}"
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 output_script
156
- return if @script.empty?
149
+ def script_inspect
150
+ return "" if @script.empty?
157
151
 
158
- max_lineno_length = @output_lines.compact.max.to_s.length
159
- @output_lines.each do |i|
160
- if @lineno == i
161
- puts sprintf("\e[0m => \e[1;34m%#{max_lineno_length}d\e[0m: %s", i, @script_lines[i])
162
- elsif i
163
- puts sprintf("\e[0m \e[1;34m%#{max_lineno_length}d\e[0m: %s", i, @script_lines[i])
164
- else
165
- puts "\e[0m #{" " * max_lineno_length} :"
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
- new_range.push(index)
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
- @script = CodeRay.scan(@script, :ruby).terminal if @@use_coderay
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
@@ -208,9 +208,9 @@ module Eturem
208
208
  def exception_inspect
209
209
  super
210
210
  end
211
-
212
- Eturem.eturem_class = self
213
211
  end
212
+
213
+ @eturem_class = En
214
214
  end
215
215
 
216
216
  require "eturem"
@@ -214,11 +214,10 @@ module Eturem
214
214
  end
215
215
 
216
216
  def exception_inspect
217
- if @path == "(eval)"
218
- return %[eval 中の #{@lineno}行目でエラーが発生しました。\n] + super.to_s
219
- else
220
- return %[ファイル"#{@path}" #{@lineno}行目でエラーが発生しました。\n] + super.to_s
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 @invalid
235
- highlight!(@script_lines[@lineno], @invalid, "\e[1;31m\e[4m")
236
- return "#{@invalid} が不適切な場所にあります。"
237
- end
238
-
239
- @unexpected ||= "end-of-input"
240
- @expected ||= "end-of-input"
241
- if @unexpected.match(/^'(.)'$/)
242
- highlight!(@script_lines[@lineno], Regexp.last_match(1), "\e[1;31m\e[4m")
243
- elsif @unexpected.match(/^(?:keyword|modifier)_/)
244
- highlight!(@script_lines[@lineno], Regexp.last_match.post_match, "\e[1;31m\e[4m")
245
- end
246
- unexpected = transform_syntax_error_keyword(@unexpected)
247
- expected = @expected.split(/\s+or\s+/).map{ |ex| transform_syntax_error_keyword(ex) }
248
- keywords = %w[if unless case while until for begin def class module do].select{ |keyword|
249
- @script.index(keyword)
250
- }.join(" / ")
251
- keywords = keywords.empty? ? "ifなど" : "「#{keywords}」"
252
-
253
- if expected.join == "end-of-input"
254
- ret = "構文エラーです。余分な#{unexpected}があります。"
255
- ret += "#{keywords}と「end」の対応関係を確認してください。" if unexpected == "end"
256
- return ret
257
- elsif unexpected == "end-of-input"
258
- ret = "(ただし、実際のエラーの原因はおそらくもっと前にあります。)\n" +
259
- "構文エラーです。#{expected.join('または')}が足りません。"
260
- ret += "#{keywords}に対応する「end」があるか確認してください。" if expected.include?("end")
261
- return ret
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 @expected
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 @given == 0
311
+ if given == 0
296
312
  ret += "が、引数が1つも渡されていません。"
297
313
  else
298
- ret += "が、#{@given}個の引数が渡されています。"
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
- "「#{@method}」への引数のタイプ(型)が正しくありません。"
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"
@@ -1,3 +1,3 @@
1
1
  module Eturem
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
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.2
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-08-18 00:00:00.000000000 Z
11
+ date: 2018-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler