mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
c7f3c222c9
commit
8187ffa461
1 changed files with 11 additions and 28 deletions
|
@ -56,7 +56,7 @@ module IRB # :nodoc:
|
||||||
on_tstring_content: [[RED], ALL],
|
on_tstring_content: [[RED], ALL],
|
||||||
on_tstring_end: [[RED], ALL],
|
on_tstring_end: [[RED], ALL],
|
||||||
on_words_beg: [[RED], ALL],
|
on_words_beg: [[RED], ALL],
|
||||||
ERROR: [[RED, REVERSE], ALL],
|
on_parse_error: [[RED, REVERSE], ALL],
|
||||||
}
|
}
|
||||||
rescue NameError
|
rescue NameError
|
||||||
# Give up highlighting Ripper-incompatible older Ruby
|
# Give up highlighting Ripper-incompatible older Ruby
|
||||||
|
@ -66,16 +66,9 @@ module IRB # :nodoc:
|
||||||
|
|
||||||
class Lexer < Ripper::Lexer
|
class Lexer < Ripper::Lexer
|
||||||
if method_defined?(:token)
|
if method_defined?(:token)
|
||||||
def self.lex(code)
|
def on_parse_error(mesg)
|
||||||
new(code).parse.sort_by {|elem| elem.pos.dup << elem.event}
|
@buf.push Elem.new([lineno(), column()], __callee__, token(), state())
|
||||||
end
|
end
|
||||||
|
|
||||||
def on_error(mesg)
|
|
||||||
# :ERROR comes before other :on_ symbols
|
|
||||||
@buf.push Elem.new([lineno(), column()], :ERROR, token(), state())
|
|
||||||
end
|
|
||||||
alias compile_error on_error
|
|
||||||
alias on_parse_error on_error
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -117,33 +110,25 @@ module IRB # :nodoc:
|
||||||
symbol_state = SymbolState.new
|
symbol_state = SymbolState.new
|
||||||
colored = +''
|
colored = +''
|
||||||
length = 0
|
length = 0
|
||||||
pos = [1, 0]
|
|
||||||
|
|
||||||
Lexer.lex(code).each do |elem|
|
Lexer.new(code).parse.sort_by(&:pos).each do |elem|
|
||||||
token = elem.event
|
token = elem.event
|
||||||
str = elem.tok
|
str = elem.tok
|
||||||
expr = elem.state
|
expr = elem.state
|
||||||
in_symbol = symbol_state.scan_token(token)
|
in_symbol = symbol_state.scan_token(token)
|
||||||
next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
|
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
|
||||||
Reline::Unicode.escape_for_print(str).each_line do |line|
|
Reline::Unicode.escape_for_print(str).each_line do |line|
|
||||||
if seq = dispatch_seq(token, expr, str, in_symbol: in_symbol)
|
|
||||||
colored << seq.map { |s| "\e[#{s}m" }.join('')
|
colored << seq.map { |s| "\e[#{s}m" }.join('')
|
||||||
colored << line.sub(/\Z/, clear)
|
colored << line.sub(/\Z/, clear)
|
||||||
else
|
|
||||||
colored << line
|
|
||||||
end
|
|
||||||
if line.end_with?("\n")
|
|
||||||
pos[0] += 1
|
|
||||||
pos[1] = 0
|
|
||||||
else
|
|
||||||
pos[1] += line.bytesize
|
|
||||||
end
|
end
|
||||||
|
else
|
||||||
|
colored << Reline::Unicode.escape_for_print(str)
|
||||||
end
|
end
|
||||||
length += str.bytesize
|
length += str.length
|
||||||
end
|
end
|
||||||
|
|
||||||
# give up colorizing incomplete Ripper tokens
|
# give up colorizing incomplete Ripper tokens
|
||||||
return code if length != code.bytesize
|
return code if length != code.length
|
||||||
|
|
||||||
colored
|
colored
|
||||||
end
|
end
|
||||||
|
@ -151,9 +136,7 @@ module IRB # :nodoc:
|
||||||
private
|
private
|
||||||
|
|
||||||
def dispatch_seq(token, expr, str, in_symbol:)
|
def dispatch_seq(token, expr, str, in_symbol:)
|
||||||
if token == :ERROR
|
if in_symbol
|
||||||
TOKEN_SEQ_EXPRS[token][0]
|
|
||||||
elsif in_symbol
|
|
||||||
[YELLOW]
|
[YELLOW]
|
||||||
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
|
elsif TOKEN_KEYWORDS.fetch(token, []).include?(str)
|
||||||
[CYAN, BOLD]
|
[CYAN, BOLD]
|
||||||
|
|
Loading…
Add table
Reference in a new issue