[ruby/irb] Change ripper_lex_without_warning to a class method

https://github.com/ruby/irb/commit/d9f8abc17e
This commit is contained in:
aycabta 2021-03-24 14:33:53 +09:00 коммит произвёл git
Родитель c9d0053e67
Коммит 0259ee6008
2 изменённых файлов: 9 добавлений и 11 удалений

Просмотреть файл

@ -60,7 +60,7 @@ class RubyLex
@io.dynamic_prompt do |lines| @io.dynamic_prompt do |lines|
lines << '' if lines.empty? lines << '' if lines.empty?
result = [] result = []
tokens = ripper_lex_without_warning(lines.map{ |l| l + "\n" }.join) tokens = self.class.ripper_lex_without_warning(lines.map{ |l| l + "\n" }.join)
code = String.new code = String.new
partial_tokens = [] partial_tokens = []
unprocessed_tokens = [] unprocessed_tokens = []
@ -115,10 +115,10 @@ class RubyLex
:on_param_error :on_param_error
] ]
def ripper_lex_without_warning(code) def self.ripper_lex_without_warning(code)
verbose, $VERBOSE = $VERBOSE, nil verbose, $VERBOSE = $VERBOSE, nil
tokens = nil tokens = nil
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no| compile_with_errors_suppressed(code) do |inner_code, line_no|
lexer = Ripper::Lexer.new(inner_code, '-', line_no) lexer = Ripper::Lexer.new(inner_code, '-', line_no)
if lexer.respond_to?(:scan) # Ruby 2.7+ if lexer.respond_to?(:scan) # Ruby 2.7+
tokens = [] tokens = []
@ -168,7 +168,7 @@ class RubyLex
if @io.respond_to?(:auto_indent) and context.auto_indent_mode if @io.respond_to?(:auto_indent) and context.auto_indent_mode
@io.auto_indent do |lines, line_index, byte_pointer, is_newline| @io.auto_indent do |lines, line_index, byte_pointer, is_newline|
if is_newline if is_newline
@tokens = ripper_lex_without_warning(lines[0..line_index].join("\n")) @tokens = self.class.ripper_lex_without_warning(lines[0..line_index].join("\n"))
prev_spaces = find_prev_spaces(line_index) prev_spaces = find_prev_spaces(line_index)
depth_difference = check_newline_depth_difference depth_difference = check_newline_depth_difference
depth_difference = 0 if depth_difference < 0 depth_difference = 0 if depth_difference < 0
@ -177,7 +177,7 @@ class RubyLex
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
last_line = lines[line_index]&.byteslice(0, byte_pointer) last_line = lines[line_index]&.byteslice(0, byte_pointer)
code += last_line if last_line code += last_line if last_line
@tokens = ripper_lex_without_warning(code) @tokens = self.class.ripper_lex_without_warning(code)
corresponding_token_depth = check_corresponding_token_depth corresponding_token_depth = check_corresponding_token_depth
if corresponding_token_depth if corresponding_token_depth
corresponding_token_depth corresponding_token_depth
@ -190,7 +190,7 @@ class RubyLex
end end
def check_state(code, tokens = nil) def check_state(code, tokens = nil)
tokens = ripper_lex_without_warning(code) unless tokens tokens = self.class.ripper_lex_without_warning(code) unless tokens
ltype = process_literal_type(tokens) ltype = process_literal_type(tokens)
indent = process_nesting_level(tokens) indent = process_nesting_level(tokens)
continue = process_continue(tokens) continue = process_continue(tokens)
@ -256,7 +256,7 @@ class RubyLex
end end
code = @line + (line.nil? ? '' : line) code = @line + (line.nil? ? '' : line)
code.gsub!(/\s*\z/, '').concat("\n") code.gsub!(/\s*\z/, '').concat("\n")
@tokens = ripper_lex_without_warning(code) @tokens = self.class.ripper_lex_without_warning(code)
@continue = process_continue @continue = process_continue
@code_block_open = check_code_block(code) @code_block_open = check_code_block(code)
@indent = process_nesting_level @indent = process_nesting_level

Просмотреть файл

@ -558,8 +558,7 @@ module TestIRB
skip 'This test needs Ripper::Lexer#scan to take broken tokens' skip 'This test needs Ripper::Lexer#scan to take broken tokens'
end end
ruby_lex = RubyLex.new tokens = RubyLex.ripper_lex_without_warning('%wwww')
tokens = ruby_lex.ripper_lex_without_warning('%wwww')
pos_to_index = {} pos_to_index = {}
tokens.each_with_index { |t, i| tokens.each_with_index { |t, i|
assert_nil(pos_to_index[t[0]], "There is already another token in the position of #{t.inspect}.") assert_nil(pos_to_index[t[0]], "There is already another token in the position of #{t.inspect}.")
@ -572,8 +571,7 @@ module TestIRB
skip 'This test needs Ripper::Lexer#scan to take broken tokens' skip 'This test needs Ripper::Lexer#scan to take broken tokens'
end end
ruby_lex = RubyLex.new tokens = RubyLex.ripper_lex_without_warning(<<~EOC.chomp)
tokens = ruby_lex.ripper_lex_without_warning(<<~EOC.chomp)
def foo def foo
%wwww %wwww
end end