2019-04-25 08:16:21 -04:00
|
|
|
# frozen_string_literal: true
|
2019-05-25 09:52:10 -04:00
|
|
|
require 'reline'
|
2019-04-25 08:16:21 -04:00
|
|
|
require 'ripper'
|
2022-01-16 17:20:05 -05:00
|
|
|
require_relative 'ruby-lex'
|
2019-04-25 08:16:21 -04:00
|
|
|
|
|
|
|
module IRB # :nodoc:
|
|
|
|
module Color
|
|
|
|
CLEAR = 0
|
|
|
|
BOLD = 1
|
|
|
|
UNDERLINE = 4
|
2019-05-27 06:59:17 -04:00
|
|
|
REVERSE = 7
|
2019-04-25 08:16:21 -04:00
|
|
|
RED = 31
|
|
|
|
GREEN = 32
|
2019-05-25 23:47:29 -04:00
|
|
|
YELLOW = 33
|
2019-04-25 08:16:21 -04:00
|
|
|
BLUE = 34
|
|
|
|
MAGENTA = 35
|
|
|
|
CYAN = 36
|
|
|
|
|
|
|
|
TOKEN_KEYWORDS = {
|
2021-01-13 09:36:10 -05:00
|
|
|
on_kw: ['nil', 'self', 'true', 'false', '__FILE__', '__LINE__', '__ENCODING__'],
|
2019-04-25 08:16:21 -04:00
|
|
|
on_const: ['ENV'],
|
|
|
|
}
|
2019-05-26 13:29:20 -04:00
|
|
|
private_constant :TOKEN_KEYWORDS
|
2019-04-25 08:16:21 -04:00
|
|
|
|
2019-05-26 14:24:52 -04:00
|
|
|
# A constant of all-bit 1 to match any Ripper's state in #dispatch_seq
|
|
|
|
ALL = -1
|
|
|
|
private_constant :ALL
|
|
|
|
|
2019-04-26 05:28:54 -04:00
|
|
|
begin
|
2019-05-26 13:22:57 -04: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 05:28:54 -04:00
|
|
|
TOKEN_SEQ_EXPRS = {
|
2019-05-26 14:24:52 -04:00
|
|
|
on_CHAR: [[BLUE, BOLD], ALL],
|
2019-11-10 16:33:23 -05:00
|
|
|
on_backtick: [[RED, BOLD], ALL],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_comment: [[BLUE, BOLD], ALL],
|
2019-05-26 14:32:23 -04:00
|
|
|
on_const: [[BLUE, BOLD, UNDERLINE], ALL],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_embexpr_beg: [[RED], ALL],
|
|
|
|
on_embexpr_end: [[RED], ALL],
|
|
|
|
on_embvar: [[RED], ALL],
|
|
|
|
on_float: [[MAGENTA, BOLD], ALL],
|
2019-05-26 14:32:23 -04:00
|
|
|
on_gvar: [[GREEN, BOLD], ALL],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_heredoc_beg: [[RED], ALL],
|
|
|
|
on_heredoc_end: [[RED], ALL],
|
2019-05-26 13:29:20 -04:00
|
|
|
on_ident: [[BLUE, BOLD], Ripper::EXPR_ENDFN],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_imaginary: [[BLUE, BOLD], ALL],
|
|
|
|
on_int: [[BLUE, BOLD], ALL],
|
|
|
|
on_kw: [[GREEN], ALL],
|
|
|
|
on_label: [[MAGENTA], ALL],
|
2019-11-10 16:33:23 -05:00
|
|
|
on_label_end: [[RED, BOLD], ALL],
|
|
|
|
on_qsymbols_beg: [[RED, BOLD], ALL],
|
|
|
|
on_qwords_beg: [[RED, BOLD], ALL],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_rational: [[BLUE, BOLD], ALL],
|
|
|
|
on_regexp_beg: [[RED, BOLD], ALL],
|
|
|
|
on_regexp_end: [[RED, BOLD], ALL],
|
|
|
|
on_symbeg: [[YELLOW], ALL],
|
2019-11-10 16:41:34 -05:00
|
|
|
on_symbols_beg: [[RED, BOLD], ALL],
|
2019-11-10 16:33:23 -05:00
|
|
|
on_tstring_beg: [[RED, BOLD], ALL],
|
2019-05-26 14:24:52 -04:00
|
|
|
on_tstring_content: [[RED], ALL],
|
2019-11-10 16:33:23 -05:00
|
|
|
on_tstring_end: [[RED, BOLD], ALL],
|
|
|
|
on_words_beg: [[RED, BOLD], ALL],
|
2019-05-29 03:02:09 -04:00
|
|
|
on_parse_error: [[RED, REVERSE], ALL],
|
|
|
|
compile_error: [[RED, REVERSE], ALL],
|
2020-12-29 02:07:20 -05: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 08:09:32 -04:00
|
|
|
on___end__: [[GREEN], ALL],
|
2019-04-26 05:28:54 -04:00
|
|
|
}
|
|
|
|
rescue NameError
|
2019-05-26 13:29:20 -04:00
|
|
|
# Give up highlighting Ripper-incompatible older Ruby
|
2019-04-26 05:28:54 -04:00
|
|
|
TOKEN_SEQ_EXPRS = {}
|
|
|
|
end
|
2019-05-26 13:29:20 -04:00
|
|
|
private_constant :TOKEN_SEQ_EXPRS
|
2019-04-25 08:16:21 -04:00
|
|
|
|
2020-12-29 02:20:26 -05:00
|
|
|
ERROR_TOKENS = TOKEN_SEQ_EXPRS.keys.select { |k| k.to_s.end_with?('error') }
|
|
|
|
private_constant :ERROR_TOKENS
|
|
|
|
|
2019-04-25 08:16:21 -04:00
|
|
|
class << self
|
|
|
|
def colorable?
|
2021-04-06 23:07:48 -04:00
|
|
|
$stdout.tty? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
|
|
|
|
2019-10-14 20:54:23 -04:00
|
|
|
def inspect_colorable?(obj, seen: {}.compare_by_identity)
|
2019-04-25 08:36:48 -04:00
|
|
|
case obj
|
2019-04-28 08:51:35 -04:00
|
|
|
when String, Symbol, Regexp, Integer, Float, FalseClass, TrueClass, NilClass
|
|
|
|
true
|
2019-04-25 08:36:48 -04:00
|
|
|
when Hash
|
2019-10-15 00:58:13 -04: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 00:25:05 -04:00
|
|
|
end
|
2019-04-25 08:36:48 -04:00
|
|
|
when Array
|
2019-10-15 00:58:13 -04:00
|
|
|
without_circular_ref(obj, seen: seen) do
|
|
|
|
obj.all? { |o| inspect_colorable?(o, seen: seen) }
|
2019-10-15 00:25:05 -04:00
|
|
|
end
|
2019-04-28 08:51:35 -04:00
|
|
|
when Range
|
2019-10-15 00:58:13 -04:00
|
|
|
inspect_colorable?(obj.begin, seen: seen) && inspect_colorable?(obj.end, seen: seen)
|
2019-04-28 08:51:35 -04:00
|
|
|
when Module
|
|
|
|
!obj.name.nil?
|
2019-04-25 08:36:48 -04:00
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-07 12:01:16 -04:00
|
|
|
def clear(colorable: colorable?)
|
|
|
|
return '' unless colorable
|
2019-04-25 08:16:21 -04:00
|
|
|
"\e[#{CLEAR}m"
|
|
|
|
end
|
|
|
|
|
2021-04-07 12:01:16 -04:00
|
|
|
def colorize(text, seq, colorable: colorable?)
|
|
|
|
return text unless colorable
|
2019-05-29 08:59:34 -04:00
|
|
|
seq = seq.map { |s| "\e[#{const_get(s)}m" }.join('')
|
2021-04-07 12:01:16 -04:00
|
|
|
"#{seq}#{text}#{clear(colorable: colorable)}"
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
|
|
|
|
2019-05-30 20:44:14 -04:00
|
|
|
# If `complete` is false (code is incomplete), this does not warn compile_error.
|
2019-05-30 17:03:18 -04: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 12:01:16 -04:00
|
|
|
def colorize_code(code, complete: true, ignore_error: false, colorable: colorable?)
|
|
|
|
return code unless colorable
|
2019-04-25 08:16:21 -04:00
|
|
|
|
2019-05-25 23:47:29 -04:00
|
|
|
symbol_state = SymbolState.new
|
2019-04-25 08:16:21 -04:00
|
|
|
colored = +''
|
2019-05-25 00:21:22 -04:00
|
|
|
length = 0
|
2021-04-01 08:09:32 -04:00
|
|
|
end_seen = false
|
2019-05-25 23:47:29 -04:00
|
|
|
|
2019-06-03 11:35:58 -04:00
|
|
|
scan(code, allow_last_error: !complete) do |token, str, expr|
|
2020-12-29 02:20:26 -05: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-25 23:47:29 -04:00
|
|
|
in_symbol = symbol_state.scan_token(token)
|
2019-05-29 09:03:47 -04:00
|
|
|
str.each_line do |line|
|
|
|
|
line = Reline::Unicode.escape_for_print(line)
|
2020-12-29 02:25:01 -05:00
|
|
|
if seq = dispatch_seq(token, expr, line, in_symbol: in_symbol)
|
2019-05-27 06:59:17 -04:00
|
|
|
colored << seq.map { |s| "\e[#{s}m" }.join('')
|
2021-04-07 12:01:16 -04:00
|
|
|
colored << line.sub(/\Z/, clear(colorable: colorable))
|
2019-05-29 09:03:47 -04:00
|
|
|
else
|
|
|
|
colored << line
|
2019-05-25 00:21:22 -04:00
|
|
|
end
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
2019-05-29 09:03:47 -04:00
|
|
|
length += str.bytesize
|
2021-04-01 08:09:32 -04:00
|
|
|
end_seen = true if token == :on___end__
|
2019-04-25 08:16:21 -04:00
|
|
|
end
|
2019-05-25 00:21:22 -04:00
|
|
|
|
|
|
|
# give up colorizing incomplete Ripper tokens
|
2021-04-01 08:09:32 -04:00
|
|
|
unless end_seen or length == code.bytesize
|
2019-06-03 11:22:22 -04:00
|
|
|
return Reline::Unicode.escape_for_print(code)
|
|
|
|
end
|
2019-05-25 00:21:22 -04:00
|
|
|
|
2019-04-25 08:16:21 -04:00
|
|
|
colored
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-10-15 00:58:13 -04:00
|
|
|
def without_circular_ref(obj, seen:, &block)
|
2019-10-14 20:54:23 -04:00
|
|
|
return false if seen.key?(obj)
|
|
|
|
seen[obj] = true
|
2019-10-15 00:58:13 -04:00
|
|
|
block.call
|
|
|
|
ensure
|
2019-10-15 10:13:49 -04:00
|
|
|
seen.delete(obj)
|
2019-10-15 00:58:13 -04:00
|
|
|
end
|
|
|
|
|
2019-06-03 11:35:58 -04:00
|
|
|
def scan(code, allow_last_error:)
|
2019-06-03 11:14:55 -04:00
|
|
|
pos = [1, 0]
|
2019-05-30 17:17:56 -04:00
|
|
|
|
2019-11-13 01:10:05 -05:00
|
|
|
verbose, $VERBOSE = $VERBOSE, nil
|
2020-06-07 10:29:01 -04:00
|
|
|
RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no|
|
|
|
|
lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no)
|
2020-05-31 19:53:07 -04: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 11:29:45 -04:00
|
|
|
end
|
2019-06-03 11:14:55 -04:00
|
|
|
|
2020-05-31 19:53:07 -04: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 11:29:45 -04:00
|
|
|
end
|
2019-05-30 17:17:56 -04:00
|
|
|
end
|
2021-01-05 03:42:05 -05:00
|
|
|
ensure
|
2019-11-13 01:10:05 -05:00
|
|
|
$VERBOSE = verbose
|
2019-05-30 17:17:56 -04:00
|
|
|
end
|
|
|
|
|
2020-12-29 02:25:01 -05:00
|
|
|
def dispatch_seq(token, expr, str, in_symbol:)
|
2020-12-29 02:20:26 -05:00
|
|
|
if ERROR_TOKENS.include?(token)
|
2020-12-29 02:25:01 -05:00
|
|
|
TOKEN_SEQ_EXPRS[token][0]
|
2019-05-29 03:02:09 -04:00
|
|
|
elsif in_symbol
|
2019-05-26 00:39:11 -04:00
|
|
|
[YELLOW]
|
2019-04-25 08:16:21 -04:00
|
|
|
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
|
|
|
|
[CYAN, BOLD]
|
2019-05-26 14:24:52 -04:00
|
|
|
elsif (seq, exprs = TOKEN_SEQ_EXPRS[token]; (expr & (exprs || 0)) != 0)
|
2019-05-26 00:39:11 -04:00
|
|
|
seq
|
2019-04-25 08:16:21 -04:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-05-25 23:47:29 -04: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-10 16:54:44 -05:00
|
|
|
when :on_symbeg, :on_symbols_beg, :on_qsymbols_beg
|
2019-05-25 23:47:29 -04:00
|
|
|
@stack << true
|
2019-05-26 13:55:37 -04:00
|
|
|
when :on_ident, :on_op, :on_const, :on_ivar, :on_cvar, :on_gvar, :on_kw
|
2019-05-26 00:39:11 -04:00
|
|
|
if @stack.last # Pop only when it's Symbol
|
2019-05-25 23:47:29 -04: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 08:16:21 -04:00
|
|
|
end
|
|
|
|
end
|