[前][次][番号順一覧][スレッド一覧]

ruby-changes:62338

From: aycabta <ko1@a...>
Date: Wed, 22 Jul 2020 02:42:13 +0900 (JST)
Subject: [ruby-changes:62338] c72a2fad97 (master): [ruby/irb] Simplify RubyLex.compile_with_errors_suppressed

https://git.ruby-lang.org/ruby.git/commit/?id=c72a2fad97

From c72a2fad9717a6090aa792c31eb0043886d0fb39 Mon Sep 17 00:00:00 2001
From: aycabta <aycabta@g...>
Date: Sun, 7 Jun 2020 23:29:01 +0900
Subject: [ruby/irb] Simplify RubyLex.compile_with_errors_suppressed

nobu-san reviewed,

https://github.com/ruby/irb/pull/106#pullrequestreview-423400033
> How about lexer = Ripper::Lexer.new(";\n#{code}", nil, 0)?
> Encoding pragma is effective only at the beginning.
> And the semicolon and newline will be skipped because the position is before
> the initial pos.

I employ the way.

Co-authored-by: Nobuyoshi Nakada <nobu@r...>

https://github.com/ruby/irb/commit/e593cc65b7

diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index d325c8d..9ee1348 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -155,8 +155,8 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L155
         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 02bc548..c7a47fd 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -31,14 +31,13 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L31
   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(?<shebang>#.*\n)?#\s*(?:encoding|coding)\s*:.*(?<nl>\n)?/
-      if e.message.match?(/unknown encoding name/) && code.match?(magic_comment_regexp)
-        code = code.gsub(magic_comment_regexp, "\\k<shebang>#\\k<nl>")
-        retry
-      end
+      code = ";\n#{code}"
+      line_no = 0
+      result = yield code, line_no
     end
     result
   end
@@ -90,8 +89,8 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L89
   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 https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L225
       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
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]