2019-04-25 15:16:21 +03:00
|
|
|
# frozen_string_literal: true
|
2019-05-25 16:52:10 +03:00
|
|
|
require 'reline'
|
2019-04-25 15:16:21 +03:00
|
|
|
require 'ripper'
|
2022-01-17 01:20:05 +03:00
|
|
|
require_relative 'ruby-lex'
|
2019-04-25 15:16:21 +03:00
|
|
|
|
|
|
|
module IRB # :nodoc:
|
|
|
|
module Color
|
|
|
|
CLEAR = 0
|
|
|
|
BOLD = 1
|
|
|
|
UNDERLINE = 4
|
2019-05-27 13:59:17 +03:00
|
|
|
REVERSE = 7
|
2023-08-11 19:03:06 +03:00
|
|
|
BLACK = 30
|
2019-04-25 15:16:21 +03:00
|
|
|
RED = 31
|
|
|
|
GREEN = 32
|
2019-05-26 06:47:29 +03:00
|
|
|
YELLOW = 33
|
2019-04-25 15:16:21 +03:00
|
|
|
BLUE = 34
|
|
|
|
MAGENTA = 35
|
|
|
|
CYAN = 36
|
2023-08-11 19:03:06 +03:00
|
|
|
WHITE = 37
|
2019-04-25 15:16:21 +03:00
|
|
|
|
|
|
|
TOKEN_KEYWORDS = {
|
2021-01-13 17:36:10 +03:00
|
|
|
on_kw: ['nil', 'self', 'true', 'false', '__FILE__', '__LINE__', '__ENCODING__'],
|
2019-04-25 15:16:21 +03:00
|
|
|
on_const: ['ENV'],
|
|
|
|
}
|
2019-05-26 20:29:20 +03:00
|
|
|
private_constant :TOKEN_KEYWORDS
|
2019-04-25 15:16:21 +03:00
|
|
|
|
2019-05-26 21:24:52 +03:00
|
|
|
# A constant of all-bit 1 to match any Ripper's state in #dispatch_seq
|
|
|
|
ALL = -1
|
|
|
|
private_constant :ALL
|
|
|
|
|
2019-04-26 12:28:54 +03:00
|
|
|
begin
|
2019-05-26 20:22:57 +03:00
|
|
|
# Following pry's colors where possible, but sometimes having a compromise like making
|
|
|
|
# backtick and regexp as red (string's color, because they're sharing tokens).
|
2019-04-26 12:28:54 +03:00
|
|
|
TOKEN_SEQ_EXPRS = {
|
2019-05-26 21:24:52 +03:00
|
|
|
on_CHAR: [[BLUE, BOLD], ALL],
|
2019-11-11 00:33:23 +03:00
|
|
|
on_backtick: [[RED, BOLD], ALL],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_comment: [[BLUE, BOLD], ALL],
|
2019-05-26 21:32:23 +03:00
|
|
|
on_const: [[BLUE, BOLD, UNDERLINE], ALL],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_embexpr_beg: [[RED], ALL],
|
|
|
|
on_embexpr_end: [[RED], ALL],
|
|
|
|
on_embvar: [[RED], ALL],
|
|
|
|
on_float: [[MAGENTA, BOLD], ALL],
|
2019-05-26 21:32:23 +03:00
|
|
|
on_gvar: [[GREEN, BOLD], ALL],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_heredoc_beg: [[RED], ALL],
|
|
|
|
on_heredoc_end: [[RED], ALL],
|
2019-05-26 20:29:20 +03:00
|
|
|
on_ident: [[BLUE, BOLD], Ripper::EXPR_ENDFN],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_imaginary: [[BLUE, BOLD], ALL],
|
|
|
|
on_int: [[BLUE, BOLD], ALL],
|
|
|
|
on_kw: [[GREEN], ALL],
|
|
|
|
on_label: [[MAGENTA], ALL],
|
2019-11-11 00:33:23 +03:00
|
|
|
on_label_end: [[RED, BOLD], ALL],
|
|
|
|
on_qsymbols_beg: [[RED, BOLD], ALL],
|
|
|
|
on_qwords_beg: [[RED, BOLD], ALL],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_rational: [[BLUE, BOLD], ALL],
|
|
|
|
on_regexp_beg: [[RED, BOLD], ALL],
|
|
|
|
on_regexp_end: [[RED, BOLD], ALL],
|
|
|
|
on_symbeg: [[YELLOW], ALL],
|
2019-11-11 00:41:34 +03:00
|
|
|
on_symbols_beg: [[RED, BOLD], ALL],
|
2019-11-11 00:33:23 +03:00
|
|
|
on_tstring_beg: [[RED, BOLD], ALL],
|
2019-05-26 21:24:52 +03:00
|
|
|
on_tstring_content: [[RED], ALL],
|
2019-11-11 00:33:23 +03:00
|
|
|
on_tstring_end: [[RED, BOLD], ALL],
|
|
|
|
on_words_beg: [[RED, BOLD], ALL],
|
2019-05-29 10:02:09 +03:00
|
|
|
on_parse_error: [[RED, REVERSE], ALL],
|
|
|
|
compile_error: [[RED, REVERSE], ALL],
|
2020-12-29 10:07:20 +03:00
|
|
|
on_assign_error: [[RED, REVERSE], ALL],
|
|
|
|
on_alias_error: [[RED, REVERSE], ALL],
|
|
|
|
on_class_name_error:[[RED, REVERSE], ALL],
|
|
|
|
on_param_error: [[RED, REVERSE], ALL],
|
2021-04-01 15:09:32 +03:00
|
|
|
on___end__: [[GREEN], ALL],
|
2019-04-26 12:28:54 +03:00
|
|
|
}
|
|
|
|
rescue NameError
|
2019-05-26 20:29:20 +03:00
|
|
|
# Give up highlighting Ripper-incompatible older Ruby
|
2019-04-26 12:28:54 +03:00
|
|
|
TOKEN_SEQ_EXPRS = {}
|
|
|
|
end
|
2019-05-26 20:29:20 +03:00
|
|
|
private_constant :TOKEN_SEQ_EXPRS
|
2019-04-25 15:16:21 +03:00
|
|
|
|
2020-12-29 10:20:26 +03:00
|
|
|
ERROR_TOKENS = TOKEN_SEQ_EXPRS.keys.select { |k| k.to_s.end_with?('error') }
|
|
|
|
private_constant :ERROR_TOKENS
|
|
|
|
|
2019-04-25 15:16:21 +03:00
|
|
|
class << self
|
|
|
|
def colorable?
|
2024-05-01 17:52:49 +03:00
|
|
|
supported = $stdout.tty? && (/mswin|mingw/.match?(RUBY_PLATFORM) || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
|
2022-06-28 16:47:28 +03:00
|
|
|
|
|
|
|
# because ruby/debug also uses irb's color module selectively,
|
|
|
|
# irb won't be activated in that case.
|
|
|
|
if IRB.respond_to?(:conf)
|
2024-05-01 17:52:49 +03:00
|
|
|
supported && !!IRB.conf.fetch(:USE_COLORIZE, true)
|
2022-06-28 16:47:28 +03:00
|
|
|
else
|
|
|
|
supported
|
|
|
|
end
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
|
2019-10-15 03:54:23 +03:00
|
|
|
def inspect_colorable?(obj, seen: {}.compare_by_identity)
|
2019-04-25 15:36:48 +03:00
|
|
|
case obj
|
2019-04-28 15:51:35 +03:00
|
|
|
when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
|
|
|
|
true
|
2019-04-25 15:36:48 +03:00
|
|
|
when Hash
|
2019-10-15 07:58:13 +03:00
|
|
|
without_circular_ref(obj, seen: seen) do
|
|
|
|
obj.all? { |k, v| inspect_colorable?(k, seen: seen) && inspect_colorable?(v, seen: seen) }
|
2019-10-15 07:25:05 +03:00
|
|
|
end
|
2019-04-25 15:36:48 +03:00
|
|
|
when Array
|
2019-10-15 07:58:13 +03:00
|
|
|
without_circular_ref(obj, seen: seen) do
|
|
|
|
obj.all? { |o| inspect_colorable?(o, seen: seen) }
|
2019-10-15 07:25:05 +03:00
|
|
|
end
|
2019-04-28 15:51:35 +03:00
|
|
|
when Range
|
2019-10-15 07:58:13 +03:00
|
|
|
inspect_colorable?(obj.begin, seen: seen) && inspect_colorable?(obj.end, seen: seen)
|
2019-04-28 15:51:35 +03:00
|
|
|
when Module
|
|
|
|
!obj.name.nil?
|
2019-04-25 15:36:48 +03:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-07 19:01:16 +03:00
|
|
|
def clear(colorable: colorable?)
|
|
|
|
return '' unless colorable
|
2019-04-25 15:16:21 +03:00
|
|
|
"\e[#{CLEAR}m"
|
|
|
|
end
|
|
|
|
|
2021-04-07 19:01:16 +03:00
|
|
|
def colorize(text, seq, colorable: colorable?)
|
|
|
|
return text unless colorable
|
2019-05-29 15:59:34 +03:00
|
|
|
seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
|
2021-04-07 19:01:16 +03:00
|
|
|
"#{seq}#{text}#{clear(colorable: colorable)}"
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
|
2019-05-31 03:44:14 +03:00
|
|
|
# If `complete` is false (code is incomplete), this does not warn compile_error.
|
2019-05-31 00:03:18 +03:00
|
|
|
# This option is needed to avoid warning a user when the compile_error is happening
|
|
|
|
# because the input is not wrong but just incomplete.
|
2022-10-18 08:44:04 +03:00
|
|
|
def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?, local_variables: [])
|
2021-04-07 19:01:16 +03:00
|
|
|
return code unless colorable
|
2019-04-25 15:16:21 +03:00
|
|
|
|
2019-05-26 06:47:29 +03:00
|
|
|
symbol_state = SymbolState.new
|
2019-04-25 15:16:21 +03:00
|
|
|
colored = +''
|
2022-10-18 08:44:04 +03:00
|
|
|
lvars_code = RubyLex.generate_local_variables_assign_code(local_variables)
|
|
|
|
code_with_lvars = lvars_code ? "#{lvars_code}\n#{code}" : code
|
2019-05-26 06:47:29 +03:00
|
|
|
|
2022-10-18 08:44:04 +03:00
|
|
|
scan(code_with_lvars, allow_last_error: !complete) do |token, str, expr|
|
2022-08-07 14:58:17 +03:00
|
|
|
# handle uncolorable code
|
|
|
|
if token.nil?
|
|
|
|
colored << Reline::Unicode.escape_for_print(str)
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2020-12-29 10:20:26 +03:00
|
|
|
# IRB::ColorPrinter skips colorizing fragments with any invalid token
|
|
|
|
if ignore_error && ERROR_TOKENS.include?(token)
|
|
|
|
return Reline::Unicode.escape_for_print(code)
|
|
|
|
end
|
|
|
|
|
2019-05-26 06:47:29 +03:00
|
|
|
in_symbol = symbol_state.scan_token(token)
|
2019-05-29 16:03:47 +03:00
|
|
|
str.each_line do |line|
|
|
|
|
line = Reline::Unicode.escape_for_print(line)
|
2020-12-29 10:25:01 +03:00
|
|
|
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
|
2019-05-27 13:59:17 +03:00
|
|
|
colored << seq.map { |s| "\e[#{s}m" }.join('')
|
2021-04-07 19:01:16 +03:00
|
|
|
colored << line.sub(/\Z/, clear(colorable: colorable))
|
2019-05-29 16:03:47 +03:00
|
|
|
else
|
|
|
|
colored << line
|
2019-05-25 07:21:22 +03:00
|
|
|
end
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
2019-06-03 18:22:22 +03:00
|
|
|
end
|
2022-10-18 08:44:04 +03:00
|
|
|
|
|
|
|
if lvars_code
|
2022-10-18 09:00:23 +03:00
|
|
|
raise "#{lvars_code.dump} should have no \\n" if lvars_code.include?("\n")
|
|
|
|
colored.sub!(/\A.+\n/, '') # delete_prefix lvars_code with colors
|
2022-10-18 08:44:04 +03:00
|
|
|
end
|
2022-10-18 09:00:23 +03:00
|
|
|
colored
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-15 07:58:13 +03:00
|
|
|
def without_circular_ref(obj, seen:, &block)
|
2019-10-15 03:54:23 +03:00
|
|
|
return false if seen.key?(obj)
|
|
|
|
seen[obj] = true
|
2019-10-15 07:58:13 +03:00
|
|
|
block.call
|
|
|
|
ensure
|
2019-10-15 17:13:49 +03:00
|
|
|
seen.delete(obj)
|
2019-10-15 07:58:13 +03:00
|
|
|
end
|
|
|
|
|
2019-06-03 18:35:58 +03:00
|
|
|
def scan(code, allow_last_error:)
|
2019-11-13 09:10:05 +03:00
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
2020-06-07 17:29:01 +03:00
|
|
|
RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no|
|
|
|
|
lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no)
|
2022-08-07 14:58:17 +03:00
|
|
|
byte_pos = 0
|
|
|
|
line_positions = [0]
|
|
|
|
inner_code.lines.each do |line|
|
|
|
|
line_positions << line_positions.last + line.bytesize
|
|
|
|
end
|
|
|
|
|
|
|
|
on_scan = proc do |elem|
|
|
|
|
start_pos = line_positions[elem.pos[0] - 1] + elem.pos[1]
|
|
|
|
|
2022-09-19 08:14:10 +03:00
|
|
|
# yield uncolorable code
|
|
|
|
if byte_pos < start_pos
|
|
|
|
yield(nil, inner_code.byteslice(byte_pos...start_pos), nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
if byte_pos <= start_pos
|
|
|
|
str = elem.tok
|
|
|
|
yield(elem.event, str, elem.state)
|
|
|
|
byte_pos = start_pos + str.bytesize
|
|
|
|
end
|
2022-08-07 14:58:17 +03:00
|
|
|
end
|
|
|
|
|
2023-04-06 00:40:34 +03:00
|
|
|
lexer.scan.each do |elem|
|
|
|
|
next if allow_last_error and /meets end of file|unexpected end-of-input/ =~ elem.message
|
|
|
|
on_scan.call(elem)
|
2019-06-12 18:29:45 +03:00
|
|
|
end
|
2022-08-07 14:58:17 +03:00
|
|
|
# yield uncolorable DATA section
|
|
|
|
yield(nil, inner_code.byteslice(byte_pos...inner_code.bytesize), nil) if byte_pos < inner_code.bytesize
|
2019-05-31 00:17:56 +03:00
|
|
|
end
|
2021-01-05 11:42:05 +03:00
|
|
|
ensure
|
2019-11-13 09:10:05 +03:00
|
|
|
$VERBOSE = verbose
|
2019-05-31 00:17:56 +03:00
|
|
|
end
|
|
|
|
|
2020-12-29 10:25:01 +03:00
|
|
|
def dispatch_seq(token, expr, str, in_symbol:)
|
2020-12-29 10:20:26 +03:00
|
|
|
if ERROR_TOKENS.include?(token)
|
2020-12-29 10:25:01 +03:00
|
|
|
TOKEN_SEQ_EXPRS[token][0]
|
2019-05-29 10:02:09 +03:00
|
|
|
elsif in_symbol
|
2019-05-26 07:39:11 +03:00
|
|
|
[YELLOW]
|
2019-04-25 15:16:21 +03:00
|
|
|
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
|
|
|
|
[CYAN, BOLD]
|
2019-05-26 21:24:52 +03:00
|
|
|
elsif (seq, exprs = TOKEN_SEQ_EXPRS[token]; (expr & (exprs || 0)) != 0)
|
2019-05-26 07:39:11 +03:00
|
|
|
seq
|
2019-04-25 15:16:21 +03:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-26 06:47:29 +03:00
|
|
|
|
|
|
|
# A class to manage a state to know whether the current token is for Symbol or not.
|
|
|
|
class SymbolState
|
|
|
|
def initialize
|
|
|
|
# Push `true` to detect Symbol. `false` to increase the nest level for non-Symbol.
|
|
|
|
@stack = []
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return true if the token is a part of Symbol.
|
|
|
|
def scan_token(token)
|
|
|
|
prev_state = @stack.last
|
|
|
|
case token
|
2019-11-11 00:54:44 +03:00
|
|
|
when :on_symbeg, :on_symbols_beg, :on_qsymbols_beg
|
2019-05-26 06:47:29 +03:00
|
|
|
@stack << true
|
2023-02-12 23:27:27 +03:00
|
|
|
when :on_ident, :on_op, :on_const, :on_ivar, :on_cvar, :on_gvar, :on_kw, :on_backtick
|
2019-05-26 07:39:11 +03:00
|
|
|
if @stack.last # Pop only when it's Symbol
|
2019-05-26 06:47:29 +03:00
|
|
|
@stack.pop
|
|
|
|
return prev_state
|
|
|
|
end
|
|
|
|
when :on_tstring_beg
|
|
|
|
@stack << false
|
|
|
|
when :on_embexpr_beg
|
|
|
|
@stack << false
|
|
|
|
return prev_state
|
|
|
|
when :on_tstring_end # :on_tstring_end may close Symbol
|
|
|
|
@stack.pop
|
|
|
|
return prev_state
|
|
|
|
when :on_embexpr_end
|
|
|
|
@stack.pop
|
|
|
|
end
|
|
|
|
@stack.last
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private_constant :SymbolState
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
|
|
|
end
|