From 6a457edbd1f2421868f2b6807347d2aa502a01a5 Mon Sep 17 00:00:00 2001 From: aycabta Date: Mon, 1 Jun 2020 08:53:07 +0900 Subject: [PATCH 1/2] Suppress incomplete coding magic comment error --- lib/irb/color.rb | 40 ++++++++++++++++++++------------------- lib/irb/ruby-lex.rb | 21 ++++++++++++++++++-- test/irb/test_ruby_lex.rb | 13 +++++++++++++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/lib/irb/color.rb b/lib/irb/color.rb index d2b9674a7..d325c8ded 100644 --- a/lib/irb/color.rb +++ b/lib/irb/color.rb @@ -155,27 +155,29 @@ def scan(code, allow_last_error:) pos = [1, 0] verbose, $VERBOSE = $VERBOSE, nil - lexer = Ripper::Lexer.new(code) - 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 + RubyLex.compile_with_errors_suppressed(code) do |inner_code| + lexer = Ripper::Lexer.new(inner_code) + 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 end - end - yield(elem.event, str, elem.state) - end - else - lexer.parse.each do |elem| - yield(elem.event, elem.tok, elem.state) + yield(elem.event, str, elem.state) + end + else + lexer.parse.each do |elem| + yield(elem.event, elem.tok, elem.state) + end end end $VERBOSE = verbose diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 0c71d5ec0..5967cdb1b 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -30,6 +30,18 @@ def initialize @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 @@ def set_prompt(p = nil, &block) 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 @@ def check_code_block(code) 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} diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index dd5a1f7ca..367d1e204 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -126,5 +126,18 @@ def test_a_closed_brace_and_not_closed_brace_in_a_line assert_indenting(lines, row.new_line_spaces, true) end end + + def test_incomplete_coding_magic_comment + input_with_correct_indents = [ + Row.new(%q(#coding:u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end end end From 443e90af80277c4892316b7ec588f50c89df14b0 Mon Sep 17 00:00:00 2001 From: aycabta Date: Mon, 1 Jun 2020 19:22:50 +0900 Subject: [PATCH 2/2] Suppress incomplete encoding magic comment error --- lib/irb/ruby-lex.rb | 5 +++-- test/irb/test_ruby_lex.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 5967cdb1b..02bc54880 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -34,8 +34,9 @@ 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") + 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 end diff --git a/test/irb/test_ruby_lex.rb b/test/irb/test_ruby_lex.rb index 367d1e204..bc5b4feaf 100644 --- a/test/irb/test_ruby_lex.rb +++ b/test/irb/test_ruby_lex.rb @@ -139,5 +139,18 @@ def test_incomplete_coding_magic_comment assert_indenting(lines, row.new_line_spaces, true) end end + + def test_incomplete_encoding_magic_comment + input_with_correct_indents = [ + Row.new(%q(#encoding:u), nil, 0), + ] + + lines = [] + input_with_correct_indents.each do |row| + lines << row.content + assert_indenting(lines, row.current_line_spaces, false) + assert_indenting(lines, row.new_line_spaces, true) + end + end end end