diff --git a/lib/irb/color.rb b/lib/irb/color.rb index d325c8dede..9ee1348671 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -155,8 +155,8 @@ module IRB # :nodoc: pos = [1, 0] verbose, $VERBOSE = $VERBOSE, nil - RubyLex.compile_with_errors_suppressed(code) do |inner_code| - lexer = Ripper::Lexer.new(inner_code) + RubyLex.compile_with_errors_suppressed(code) do |inner_code, line_no| + lexer = Ripper::Lexer.new(inner_code, '(ripper)', line_no) if lexer.respond_to?(:scan) # Ruby 2.7+ lexer.scan.each do |elem| str = elem.tok diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 02bc548809..c7a47fd99d 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -31,14 +31,13 @@ class RubyLex end def self.compile_with_errors_suppressed(code) + line_no = 1 begin - result = yield code + result = yield code, line_no rescue ArgumentError => e - magic_comment_regexp = /\A(?#.*\n)?#\s*(?:encoding|coding)\s*:.*(?\n)?/ - if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp) - code = code.gsub(magic_comment_regexp, "\\k#\\k") - retry - end + code = ";\n#{code}" + line_no = 0 + result = yield code, line_no end result end @@ -90,8 +89,8 @@ class RubyLex def ripper_lex_without_warning(code) verbose, $VERBOSE = $VERBOSE, nil tokens = nil - self.class.compile_with_errors_suppressed(code) do |inner_code| - tokens = Ripper.lex(inner_code) + self.class.compile_with_errors_suppressed(code) do |inner_code, line_no| + tokens = Ripper.lex(inner_code, '-', line_no) end $VERBOSE = verbose tokens @@ -226,8 +225,8 @@ class RubyLex when 'jruby' JRuby.compile_ir(code) else - self.class.compile_with_errors_suppressed(code) do |inner_code| - RubyVM::InstructionSequence.compile(inner_code) + self.class.compile_with_errors_suppressed(code) do |inner_code, line_no| + RubyVM::InstructionSequence.compile(inner_code, nil, nil, line_no) end end rescue EncodingError