From 78ccab25306d15c325baa0761d9505ac23956f22 Mon Sep 17 00:00:00 2001 From: aycabta Date: Mon, 1 Jun 2020 08:53:07 +0900 Subject: [ruby/irb] Suppress incomplete coding magic comment error https://github.com/ruby/irb/commit/6a457edbd1 --- lib/irb/ruby-lex.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'lib/irb/ruby-lex.rb') diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 0c71d5ec03..5967cdb1ba 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -30,6 +30,18 @@ class RubyLex @prompt = nil end + def self.compile_with_errors_suppressed(code) + begin + result = yield code + rescue ArgumentError => e + if e.message.match?(/unknown encoding name/) && code.match?(/\A(?#.*\n)?#\s*coding\s*:.*(?\n)?/) + code = code.gsub(/\A(?#.*\n)?#\s*coding\s*:.*(?\n)?/, "\\k#\\k") + retry + end + end + result + end + # io functions def set_input(io, p = nil, &block) @io = io @@ -76,7 +88,10 @@ class RubyLex def ripper_lex_without_warning(code) verbose, $VERBOSE = $VERBOSE, nil - tokens = Ripper.lex(code) + tokens = nil + self.class.compile_with_errors_suppressed(code) do |inner_code| + tokens = Ripper.lex(inner_code) + end $VERBOSE = verbose tokens end @@ -210,7 +225,9 @@ class RubyLex when 'jruby' JRuby.compile_ir(code) else - RubyVM::InstructionSequence.compile(code) + self.class.compile_with_errors_suppressed(code) do |inner_code| + RubyVM::InstructionSequence.compile(inner_code) + end end rescue EncodingError # This is for a hash with invalid encoding symbol, {"\xAE": 1} -- cgit v1.2.3