summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-04 00:14:55 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-06-04 00:14:55 +0900
commit0c459af7c233adb5f44022350bfe8fa132d8053e (patch)
tree0ce22c47f876f23d88113c2282d3e079dd69f027 /lib
parent928377d2c5153333445d58710534b471042ffb46 (diff)
Colorize error characters
* lib/irb/color.rb (IRB::Color.scan): ignore "incomplete end of input" error only, to colorize invalid characters, e.g., control characters, and invalid symbols, as errors.
Diffstat (limited to 'lib')
-rw-r--r--lib/irb/color.rb44
1 files changed, 15 insertions, 29 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index 5e58c2509f..c2d0084446 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -131,28 +131,23 @@ module IRB # :nodoc:
private
def scan(code, detect_compile_error:)
- if detect_compile_error
- pos = [1, 0]
-
- Ripper::Lexer.new(code).scan.each do |elem|
- str = elem.tok
- next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
-
- str.each_line do |line|
- if line.end_with?("\n")
- pos[0] += 1
- pos[1] = 0
- else
- pos[1] += line.bytesize
- end
- end
+ pos = [1, 0]
- yield(elem.event, str, elem.state)
- end
- else
- ParseErrorLexer.new(code).parse.sort_by(&:pos).each do |elem|
- yield(elem.event, elem.tok, elem.state)
+ Ripper::Lexer.new(code).scan.each do |elem|
+ str = elem.tok
+ next if !detect_compile_error and elem.message&.end_with?("meets end of file")
+ next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
+
+ str.each_line do |line|
+ if line.end_with?("\n")
+ pos[0] += 1
+ pos[1] = 0
+ else
+ pos[1] += line.bytesize
+ end
end
+
+ yield(elem.event, str, elem.state)
end
end
@@ -171,15 +166,6 @@ module IRB # :nodoc:
end
end
- class ParseErrorLexer < Ripper::Lexer
- if method_defined?(:token)
- def on_parse_error(mesg)
- @buf.push Elem.new([lineno(), column()], __callee__, token(), state())
- end
- end
- end
- private_constant :ParseErrorLexer
-
# A class to manage a state to know whether the current token is for Symbol or not.
class SymbolState
def initialize