aboutsummaryrefslogtreecommitdiffstats
path: root/lib/irb/color.rb
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-05-27 19:59:17 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-05-27 23:08:51 +0900
commit8a2a5822caa7a9fc146d7de4db679986603d7a4f (patch)
treefcc90f131f5dff3cdd5d6dac9c45fcf66d9a06f4 /lib/irb/color.rb
parentb4365e75fd70618b79b3a71c1c13f202acd71368 (diff)
downloadruby-8a2a5822caa7a9fc146d7de4db679986603d7a4f.tar.gz
Colorize error part
Diffstat (limited to 'lib/irb/color.rb')
-rw-r--r--lib/irb/color.rb18
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index de91f29a12..aa4c60aa12 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -7,6 +7,7 @@ module IRB # :nodoc:
CLEAR = 0
BOLD = 1
UNDERLINE = 4
+ REVERSE = 7
RED = 31
GREEN = 32
YELLOW = 33
@@ -55,6 +56,7 @@ module IRB # :nodoc:
on_tstring_content: [[RED], ALL],
on_tstring_end: [[RED], ALL],
on_words_beg: [[RED], ALL],
+ on_parse_error: [[RED, REVERSE], ALL],
}
rescue NameError
# Give up highlighting Ripper-incompatible older Ruby
@@ -62,6 +64,14 @@ module IRB # :nodoc:
end
private_constant :TOKEN_SEQ_EXPRS
+ class Lexer < Ripper::Lexer
+ if method_defined?(:token)
+ def on_parse_error(mesg)
+ @buf.push Elem.new([lineno(), column()], __callee__, token(), state())
+ end
+ end
+ end
+
class << self
def colorable?
$stdout.tty? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
@@ -101,11 +111,15 @@ module IRB # :nodoc:
colored = +''
length = 0
- Ripper.lex(code).each do |(_line, _col), token, str, expr|
+ Lexer.new(code).parse.sort_by(&:pos).each do |elem|
+ token = elem.event
+ str = elem.tok
+ expr = elem.state
in_symbol = symbol_state.scan_token(token)
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
Reline::Unicode.escape_for_print(str).each_line do |line|
- colored << "#{seq.map { |s| "\e[#{s}m" }.join('')}#{line.sub(/\n?\z/, "#{clear}\\0")}"
+ colored << seq.map { |s| "\e[#{s}m" }.join('')
+ colored << line.sub(/\Z/, clear)
end
else
colored << Reline::Unicode.escape_for_print(str)