eturem 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b90412ed6406c24b2fa9ba675d7650ff9870b936
4
+ data.tar.gz: 7b2572ea2cb36ce486e5c372ea702aa18f3f1a76
5
+ SHA512:
6
+ metadata.gz: 6de0d89d9107da3e802477bec66f902acbd7ad8ddc252ad9abb154cb830356fc5defe2927902ff1411bcd0e00630dfc37d83e47331112d777c5bc661558f8c32
7
+ data.tar.gz: 9a957f69850f9c9226582db4be79dcf14d7a9960e8a64b772778632a5c90bf81e74b408b918bf6112d4adf6f489fabf17139dc78bcc90fd275d019a9b787535e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /Gemfile.lock
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nodai2h.itc@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in eturem.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 nodai2hITC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.ja.md ADDED
@@ -0,0 +1,133 @@
1
+ # Eturem
2
+
3
+ Ruby のエラーメッセージを、初心者にわかりやすく表示するための gem です。
4
+
5
+ Easy To Understand Ruby Error Message の略。
6
+
7
+ ## インストールと使用方法
8
+
9
+ $ gem install eturem
10
+
11
+ でインストールし、
12
+
13
+ $ ruby -returem/ja <your_script.rb>
14
+
15
+ と使用すればよいのですが、最初に書いたとおり初心者が使用することを想定した gem ですので、そんなことを初心者に強いるのは酷というもの。だれか詳しい人が、事前に ```gem install eturem``` した上で、環境変数 RUBYOPT に ```-returem/ja``` を追加しておいてあげましょう。
16
+
17
+ ## 使用するとどうなるか
18
+
19
+ * エラーメッセージが日本語で表示される。
20
+ * エラー箇所周辺が表示される。
21
+ * エラーの種類によっては、原因を特定するためのさらなる情報が表示される。
22
+
23
+ ### 例1:SyntaxError
24
+
25
+ ```ruby
26
+ if gets.to_i == 1
27
+ if gets.to_i == 2
28
+ puts "なんたらかんたら"
29
+ # 内側の if に対応する end を忘れてしまった!
30
+ end
31
+ ```
32
+
33
+ 通常の環境で実行すると、次のようなエラーが表示されます。
34
+
35
+ ```
36
+ example1.rb:5: syntax error, unexpected end-of-input, expecting keyword_end
37
+ ```
38
+
39
+ 英語の苦手な人ではこの時点で拒否反応が出るでしょうし、そうでなくとも「end-of-input」や「keyword_end」が何を意味しているのか、初心者には掴みにくいのではないでしょうか。
40
+
41
+ Eturem を使用すると、次のようなエラー表示になります。
42
+
43
+ ```
44
+ ファイル"example1.rb" 5行目でエラーが発生しました。(ただし、エラーの原因はおそらくもっと前にあります。)
45
+ 構文エラーです。「end」が足りません。「if」に対応する「end」があるか確認してください。
46
+ 3: puts "なんたらかんたら"
47
+ 4: # 内側の if に対応する end を忘れてしまった!
48
+ => 5: end
49
+ ```
50
+
51
+ このように、日本語でわかりやすくエラーを表示してくれます。
52
+
53
+ ### 例2:NameError
54
+
55
+ ```ruby
56
+ prayer_life = 100
57
+ # ↑スペルミス!
58
+ # 中略
59
+ # ↓このスペルは正しいが、上でミスしたことでエラー発生。
60
+ if player_life > 0
61
+ # 後略
62
+ ```
63
+
64
+ 通常の環境で実行すると、次のようなエラーが表示されます。
65
+
66
+ ```
67
+ example2.rb:5:in `<main>': undefined local variable or method `player_life' for main:Object (NameError)
68
+ Did you mean? prayer_life
69
+ ```
70
+
71
+ did_you_mean のおかげで昔より格段にわかりやすくなったとはいえ、それでも英語に壁を感じる人はいますし、またこの例の場合実際にミスをしたのは1行目にもかかわらず「5行目でエラー」と表示されてしまうため、「え?5行目を何度見てもミスなんて無いよ?」と困ってしまう人もいるでしょう。
72
+
73
+ Eturem を使用すると、次のようなエラー表示になります。(実際には色付き)
74
+
75
+ ```
76
+ ファイル"example2.rb" 5行目でエラーが発生しました。
77
+ 変数/メソッド「player_life」は存在しません。「prayer_life」の入力ミスではありませんか?
78
+ 1: prayer_life = 100
79
+ :
80
+ 3: # 中略
81
+ 4: # ↓このスペルは正しいが、上でミスしたことでエラー発生。
82
+ => 5: if player_life > 0
83
+ 6: # 後略
84
+ ```
85
+
86
+ このように、エラー発生箇所周辺だけではなく、did_you_mean がサジェストしてくれた変数の使用行も同時に表示してくれるので、ミスをしたのが実は1行目であることに気付きやすくなるのではないでしょうか。
87
+
88
+ ### 例2:ArgumentError
89
+
90
+ ```ruby
91
+ def foo(a, b)
92
+ end
93
+ # 中略
94
+ foo(1)
95
+ ```
96
+
97
+ 通常の環境で実行すると、次のようなエラーが表示されます。
98
+
99
+ ```
100
+ Traceback (most recent call last):
101
+ 1: from example3.rb:4:in `<main>'
102
+ example3.rb:1:in `foo': wrong number of arguments (given 1, expected 2) (ArgumentError)
103
+ ```
104
+
105
+ このように、ArgumentError のエラー発生行は、メソッド定義行(この場合1行目)になってしまいます。しかし実際に ArgumentError が発生するときの原因は、メソッド定義部分ではなく、呼び出し部分ではないでしょうか?
106
+
107
+ この例の場合 Traceback に「from example3.rb:4」と表示されてはいますが、初心者にはやはりわかりにくいのではないかと思われます。
108
+
109
+ Eturem を使用すると、次のようなエラー表示になります。
110
+
111
+ ```
112
+ ファイル"example3.rb" 4行目でエラーが発生しました。
113
+ 引数の数が正しくありません。「foo」は本来2個の引数を取りますが、1個の引数が渡されています。
114
+ 2: end
115
+ 3: # 中略
116
+ => 4: foo(1)
117
+ ```
118
+
119
+ このように、呼び出し行をエラー発生行として表示してくれます。
120
+
121
+ ## Contributing
122
+
123
+ 「こう表示した方がよりわかりやすいのでは?」等のご意見ありましたら、よろしく御願いします。
124
+
125
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nodai2hITC/eturem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
126
+
127
+ ## License
128
+
129
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
130
+
131
+ ## Code of Conduct
132
+
133
+ Everyone interacting in the Eturem project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nodai2hITC/eturem/blob/master/CODE_OF_CONDUCT.md).
data/README.md ADDED
@@ -0,0 +1,27 @@
1
+ # Eturem
2
+
3
+ Easy To Understand Ruby Error Message.
4
+
5
+ [日本語説明(こちらの方が詳細)](https://github.com/nodai2hITC/eturem/blob/master/README.ja.md)
6
+
7
+ ## Installation
8
+
9
+ install it yourself as:
10
+
11
+ $ gem install eturem
12
+
13
+ ## Usage
14
+
15
+ $ ruby -returem <your_script.rb>
16
+
17
+ ## Contributing
18
+
19
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nodai2hITC/eturem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
20
+
21
+ ## License
22
+
23
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
24
+
25
+ ## Code of Conduct
26
+
27
+ Everyone interacting in the Eturem project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/nodai2hITC/eturem/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "eturem"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/eturem.gemspec ADDED
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "eturem/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "eturem"
8
+ spec.version = Eturem::VERSION
9
+ spec.authors = ["nodai2hITC"]
10
+ spec.email = ["nodai2h.itc@gmail.com"]
11
+
12
+ spec.summary = %q{Easy To Understand Ruby Error Message.}
13
+ spec.description = %q{Easy To Understand Ruby Error Message.}
14
+ spec.homepage = "https://github.com/nodai2hITC/eturem"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "minitest", "~> 5.0"
36
+ end
@@ -0,0 +1,197 @@
1
+ module Eturem
2
+ def self.set_eturem_class(klass)
3
+ @eturem_class = klass
4
+ end
5
+
6
+ def self.output_error(exception, eturem_path)
7
+ @eturem_class.new(exception, eturem_path).output_error
8
+ end
9
+
10
+ class Base
11
+ def initialize(exception, eturem_path)
12
+ @exception = exception
13
+ @exception_s = exception.to_s
14
+ @eturem_path = eturem_path
15
+ @locations = @exception.backtrace_locations.select do |location|
16
+ path = File.absolute_path(location.path)
17
+ path !~ /\/rubygems\/core_ext\/kernel_require\.rb$/ &&
18
+ path != @eturem_path &&
19
+ File.basename(File.dirname(path)) != "eturem"
20
+ end
21
+ @locations = @locations[0, max_backtrace]
22
+ if @exception_s.match(/\A(?<path>[^:]+)\:(?<lineno>\d+)/)
23
+ @lineno_in_exception_s = true
24
+ @path = Regexp.last_match(:path)
25
+ @lineno = Regexp.last_match(:lineno).to_i
26
+ else
27
+ @lineno_in_exception_s = false
28
+ @path = @locations.first.path
29
+ @lineno = @locations.first.lineno
30
+ end
31
+ @script = ""
32
+ if @path && File.exist?(@path)
33
+ @script = File.binread(@path)
34
+ encoding = @script.lines[0..2].join("\n").match(/coding:\s*(?<encoding>\S+)/) ?
35
+ Regexp.last_match(:encoding) : "UTF-8"
36
+ @script.force_encoding(encoding)
37
+ @script.encode("UTF-8")
38
+ end
39
+ @script_lines = @script.lines
40
+ @script_lines.unshift("")
41
+ @range = default_range
42
+ prepare
43
+ end
44
+
45
+ def prepare
46
+ case @exception
47
+ when SyntaxError
48
+ return unless @exception_s.match(/unexpected (?<unexpected>\S+)\,\s*expecting (?<expected>\S+)/)
49
+ @unexpected = Regexp.last_match(:unexpected)
50
+ @expected = Regexp.last_match(:expected)
51
+ when NameError
52
+ highlight!(@script_lines[@lineno], @exception.name.to_s, "\e[31m\e[4m")
53
+ return unless @exception_s.match(/Did you mean\?/)
54
+ @did_you_mean = Regexp.last_match.post_match.strip.split(/\s+/)
55
+ new_range = []
56
+ @did_you_mean.each do |did_you_mean|
57
+ index = @script_lines.index{|line| line.match(did_you_mean)}
58
+ next unless index
59
+ highlight!(@script_lines[index], did_you_mean, "\e[33m")
60
+ new_range.push(index)
61
+ end
62
+ before = new_range.select{|i| i < @range.min}
63
+ after = new_range.select{|i| i > @range.max}
64
+ unless before.empty?
65
+ @range.unshift(0) unless @range.include?(before.max + 1)
66
+ @range.unshift(*before.sort)
67
+ end
68
+ unless after.empty?
69
+ @range.push(0) unless @range.include?(after.min - 1)
70
+ @range.push(*after.sort)
71
+ end
72
+ when ArgumentError
73
+ @method = @locations.first.label
74
+ shift_locations
75
+ return unless @exception_s.match(/given (?<given>\d+)\, expected (?<expected>[^)]+)/)
76
+ @given = Regexp.last_match(:given).to_i
77
+ @expected = Regexp.last_match(:expected)
78
+ when TypeError
79
+ @method = @locations.first.label
80
+ shift_locations
81
+ end
82
+ end
83
+
84
+ def shift_locations
85
+ @locations.shift
86
+ @path = @locations.first.path
87
+ @lineno = @locations.first.lineno
88
+ @range = default_range
89
+ end
90
+
91
+ def default_range
92
+ from = [1, @lineno - before_line].max
93
+ to = [@script_lines.size - 1, @lineno + after_line].min
94
+ (from..to).to_a
95
+ end
96
+
97
+ def output_error
98
+ output_backtrace
99
+ output_exception
100
+ output_script
101
+ end
102
+
103
+ # backtrace
104
+ def output_backtrace
105
+ return if backtrace.empty?
106
+
107
+ puts traceback_most_recent_call_last
108
+ backtrace.each_with_index do |location, i|
109
+ label =
110
+ $0 == location.path && location.label == "<top (required)>" ?
111
+ "<main>" : location.label
112
+ puts sprintf("%9d: %s", backtrace.size - i, location_inspect(location, label))
113
+ end
114
+ end
115
+
116
+ def backtrace
117
+ get_backtrace unless @backtrace
118
+ return @backtrace
119
+ end
120
+
121
+ def get_backtrace
122
+ @backtrace = @locations.reverse.dup
123
+ @backtrace.pop unless @lineno_in_exception_s
124
+ end
125
+
126
+ def max_backtrace
127
+ 16
128
+ end
129
+
130
+ def traceback_most_recent_call_last
131
+ "Traceback (most recent call last):"
132
+ end
133
+
134
+ def location_inspect(location, label = nil)
135
+ "from #{location.path}:#{location.lineno}:in `#{label || location.label}'"
136
+ end
137
+
138
+ # exception
139
+ def output_exception
140
+ puts exception_inspect
141
+ end
142
+
143
+ def exception_inspect
144
+ original_exception_inspect
145
+ end
146
+
147
+ def original_exception_inspect
148
+ if @lineno_in_exception_s
149
+ return @exception_s
150
+ else
151
+ label = @locations.first.label
152
+ label = "<main>" if $0 == @path && label == "<top (required)>"
153
+ location_str = "#{@path}:#{@lineno}:in `#{label}'"
154
+ if @exception_s.match(/\A(.*?)\n/)
155
+ matched = Regexp.last_match
156
+ return "#{location_str}: #{matched[1]} (#{@exception.class})\n#{matched.post_match}"
157
+ else
158
+ return "#{location_str}: #{@exception} (#{@exception.class})"
159
+ end
160
+ end
161
+ end
162
+
163
+ # script
164
+ def output_script
165
+ max_lineno_length = @range.max.to_s.length
166
+ @range.each do |i|
167
+ if i == 0
168
+ puts " \e[34m#{" " * max_lineno_length} :\e[0m"
169
+ else
170
+ if @lineno == i
171
+ puts sprintf(" => \e[34m%#{max_lineno_length}d:\e[0m %s", i, @script_lines[i])
172
+ else
173
+ puts sprintf(" \e[34m%#{max_lineno_length}d:\e[0m %s", i, @script_lines[i])
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ def before_line
180
+ 2
181
+ end
182
+
183
+ def after_line
184
+ 2
185
+ end
186
+
187
+ def highlight(str, keyword, color)
188
+ str.gsub(keyword){ color + ($1 || $&) + "\e[0m" }
189
+ end
190
+
191
+ def highlight!(str, keyword, color)
192
+ str.gsub!(keyword){ color + ($1 || $&) + "\e[0m" }
193
+ end
194
+ end
195
+
196
+ set_eturem_class Base
197
+ end
data/lib/eturem/ja.rb ADDED
@@ -0,0 +1,126 @@
1
+ # coding: utf-8
2
+
3
+ require "eturem/base"
4
+
5
+ module Eturem
6
+ class Ja < Base
7
+ def prepare
8
+ super
9
+ case @exception.class.to_s
10
+ when "DXRuby::DXRubyError"
11
+ @exception_s.force_encoding("sjis") if @exception_s.encoding == Encoding::ASCII_8BIT
12
+ @exception_s.encode!("UTF-8")
13
+ end
14
+ end
15
+
16
+ def traceback_most_recent_call_last
17
+ "エラー発生までの流れ:"
18
+ end
19
+
20
+ def location_inspect(location, label)
21
+ "\"#{location.path}\" #{location.lineno}行目: '#{label}'"
22
+ end
23
+
24
+ def output_exception
25
+ puts exception_inspect
26
+ end
27
+
28
+ def exception_inspect
29
+ ret = original_exception_inspect.chomp + "\n" + "ファイル\"#{@path}\" #{@lineno}行目でエラーが発生しました。\n"
30
+ case @exception
31
+ when LoadError
32
+ return ret +
33
+ "ファイル/ライブラリ '#{@exception.path}' が見つかりません。" +
34
+ "ファイル/ライブラリ名を確認してください。"
35
+ when SyntaxError
36
+ return syntax_error_inspect(ret)
37
+ when Interrupt
38
+ return ret + "プログラムが途中で強制終了されました。"
39
+ when ArgumentError
40
+ if @given
41
+ return ret +
42
+ "引数の数が正しくありません。" +
43
+ "「#{@method}」は本来" +
44
+ (@expected =~ /^0$/ ? "引数が不要ですが、" : "#{@expected}個の引数を取りますが、") +
45
+ "#{@given}個の引数が渡されています。"
46
+ else
47
+ return ret + "「#{@method}」への引数の数が正しくありません。"
48
+ end
49
+ when NameError, NoMethodError
50
+ return name_error_inspect(ret)
51
+ when TypeError
52
+ return ret + "「#{@method}」への引数のタイプが正しくありません。"
53
+ when ZeroDivisionError
54
+ return ret + "割る数が 0 での割り算はできません。"
55
+ else
56
+ return ret
57
+ end
58
+ end
59
+
60
+ def syntax_error_inspect(ret)
61
+ unexpected = transform_syntax_error_keyword(@unexpected)
62
+ expected = transform_syntax_error_keyword(@expected)
63
+ unexpected.each do |keyword|
64
+ next unless keyword.match(/^「\s*(\S+)\s*」$/)
65
+ highlight!(@script_lines[@lineno], Regexp.last_match(1), "\e[31m\e[4m")
66
+ end
67
+ keywords = get_keywords_from_script(%w[if unless case while until for begin def class module do])
68
+
69
+ if @expected == "end-of-input" || @expected == "$end"
70
+ ret += "構文エラーです。余分な#{unexpected.join('または')}があります。"
71
+ ret += "「#{keywords.join(' / ')}」と「end」の対応関係を確認してください。" if @unexpected == "keyword_end"
72
+ return ret
73
+ elsif @unexpected == "end-of-input" || @unexpected == "$end"
74
+ ret = ret.chomp + "(ただし、エラーの原因はおそらくもっと前にあります。)\n" +
75
+ "構文エラーです。#{expected.join('または')}が足りません。"
76
+ ret += "「#{keywords.join(' / ')}」に対応する「end」があるか確認してください。" if @expected == "keyword_end"
77
+ return ret
78
+ end
79
+ return ret + "構文エラーです。" +
80
+ "#{unexpected.join('または')}より前のどこかに、" +
81
+ "#{expected.join('または')}を入れる必要があります。"
82
+ end
83
+
84
+ def transform_syntax_error_keyword(keyword)
85
+ keyword.split(/\s+or\s+/).map do |word|
86
+ case word
87
+ when "end-of-input", "$end"
88
+ "end-of-input"
89
+ when /^keyword\_/
90
+ "「#{word.sub("keyword_", "")}」"
91
+ when "'\\n'"
92
+ "改行"
93
+ when /^'[^']+'$/
94
+ "「 #{word.gsub("'", "")} 」"
95
+ else
96
+ word
97
+ end
98
+ end
99
+ end
100
+
101
+ def get_keywords_from_script(keywords)
102
+ keywords.select { |keyword| @script.index(keyword) }
103
+ end
104
+
105
+ def name_error_inspect(ret)
106
+ ret = ret.lines.first + ret.lines.last
107
+ if @exception.name.to_s.encode("UTF-8") == " "
108
+ @range = default_range
109
+ ret += "スクリプト中に全角空白が混じっています。"
110
+ else
111
+ ret += "#{@exception.is_a?(NoMethodError) ? "" : "変数/"}メソッド"
112
+ ret += "「\e[31m\e[4m#{@exception.name}\e[0m」は存在しません。"
113
+ if @did_you_mean
114
+ did_you_mean = @did_you_mean.map{|d| "\e[33m#{d}\e[0m"}.join(" / ")
115
+ ret += "「#{did_you_mean}」の入力ミスではありませんか?"
116
+ end
117
+ end
118
+ return ret
119
+ end
120
+ end
121
+
122
+ set_eturem_class Ja
123
+ end
124
+
125
+ require "eturem"
126
+
@@ -0,0 +1,3 @@
1
+ module Eturem
2
+ VERSION = "0.1.0"
3
+ end
data/lib/eturem.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "eturem/base"
2
+ require "eturem/version"
3
+
4
+ begin
5
+ load $0
6
+ rescue Exception => exception
7
+ exit if exception.is_a? SystemExit
8
+ Eturem.output_error(exception, File.absolute_path(__FILE__))
9
+ end
10
+
11
+ exit
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: eturem
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - nodai2hITC
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Easy To Understand Ruby Error Message.
56
+ email:
57
+ - nodai2h.itc@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.ja.md
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - eturem.gemspec
72
+ - lib/eturem.rb
73
+ - lib/eturem/base.rb
74
+ - lib/eturem/ja.rb
75
+ - lib/eturem/version.rb
76
+ homepage: https://github.com/nodai2hITC/eturem
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.12
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Easy To Understand Ruby Error Message.
100
+ test_files: []