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
|
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
|
|
|
|
|
|
|
|
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?
|
2022-06-28 16:30:36 +03:00
|
|
|
$stdout.tty? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb')) && IRB.conf.fetch(:USE_COLORIZE, true)
|
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.
|
2021-04-07 19:01:16 +03:00
|
|
|
def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?)
|
|
|
|
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 = +''
|
2019-05-25 07:21:22 +03:00
|
|
|
length = 0
|
2021-04-01 15:09:32 +03:00
|
|
|
end_seen = false
|
2019-05-26 06:47:29 +03:00
|
|
|
|
2019-06-03 18:35:58 +03:00
|
|
|
scan(code, allow_last_error: !complete) do |token, str, expr|
|
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-05-29 16:03:47 +03:00
|
|
|
length += str.bytesize
|
2021-04-01 15:09:32 +03:00
|
|
|
end_seen = true if token == :on___end__
|
2019-04-25 15:16:21 +03:00
|
|
|
end
|
2019-05-25 07:21:22 +03:00
|
|
|
|
|
|
|
# give up colorizing incomplete Ripper tokens
|
2021-04-01 15:09:32 +03:00
|
|
|
unless end_seen or length == code.bytesize
|
2019-06-03 18:22:22 +03:00
|
|
|
return Reline::Unicode.escape_for_print(code)
|
|
|
|
end
|
2019-05-25 07:21:22 +03:00
|
|
|
|
2019-04-25 15:16:21 +03:00
|
|
|
colored
|
|
|
|
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-06-03 18:14:55 +03:00
|
|
|
pos = [1, 0]
|
2019-05-31 00:17:56 +03:00
|
|
|
|
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)
|
2020-06-01 02:53:07 +03:00
|
|
|
if lexer.respond_to?(:scan) # Ruby 2.7+
|
|
|
|
lexer.scan.each do |elem|
|
|
|
|
str = elem.tok
|
|
|
|
next if allow_last_error and /meets end of file|unexpected end-of-input/ =~ elem.message
|
|
|
|
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
|
2019-06-12 18:29:45 +03:00
|
|
|
end
|
2019-06-03 18:14:55 +03:00
|
|
|
|
2020-06-01 02:53:07 +03:00
|
|
|
yield(elem.event, str, elem.state)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
lexer.parse.each do |elem|
|
|
|
|
yield(elem.event, elem.tok, elem.state)
|
|
|
|
end
|
2019-06-12 18:29:45 +03:00
|
|
|
end
|
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
|
2019-05-26 20:55:37 +03:00
|
|
|
when :on_ident, :on_op, :on_const, :on_ivar, :on_cvar, :on_gvar, :on_kw
|
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
|