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

ruby-changes:56095

From: Takashi <ko1@a...>
Date: Thu, 13 Jun 2019 00:32:57 +0900 (JST)
Subject: [ruby-changes:56095] Takashi Kokubun: 1808029061 (trunk): make sync-default-gems GEM=irb

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

From 180802906190501e4eb9b9423adfb6116ceb334b Mon Sep 17 00:00:00 2001
From: Takashi Kokubun <takashikkbn@g...>
Date: Thu, 13 Jun 2019 00:29:45 +0900
Subject: make sync-default-gems GEM=irb

Upgrade IRB to https://github.com/ruby/irb/commit/41ea43a4a732e094acfa1b0fc1473fdcda9e6227

Mostly backport changes.

diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index b942299..6b48964 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -67,7 +67,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L67
 
     class << self
       def colorable?
-        $stdout.tty? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
+        $stdout.tty? && supported? && (/mswin|mingw/ =~ RUBY_PLATFORM || (ENV.key?('TERM') && ENV['TERM'] != 'dumb'))
       end
 
       def inspect_colorable?(obj)
@@ -132,24 +132,37 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L132
 
       private
 
+      # Ripper::Lexer::Elem#state is supported on Ruby 2.5+
+      def supported?
+        return @supported if defined?(@supported)
+        @supported = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.5.0')
+      end
+
       def scan(code, allow_last_error:)
         pos = [1, 0]
 
-        Ripper::Lexer.new(code).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
+        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
+              end
             end
-          end
 
-          yield(elem.event, str, 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
 
diff --git a/lib/irb/irb.gemspec b/lib/irb/irb.gemspec
index d16d6b0..12633bf 100644
--- a/lib/irb/irb.gemspec
+++ b/lib/irb/irb.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec| https://github.com/ruby/ruby/blob/trunk/lib/irb/irb.gemspec#L21
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
   spec.require_paths = ["lib"]
 
+  spec.add_dependency "reline"
   spec.add_development_dependency "bundler"
   spec.add_development_dependency "rake"
 end
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index d77354f..a57a5df 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -127,7 +127,6 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L127
   end
 
   def process_continue
-    continued_bits = Ripper::EXPR_BEG | Ripper::EXPR_FNAME
     # last token is always newline
     if @tokens.size >= 2 and @tokens[-2][1] == :on_regexp_end
       # end of regexp literal
@@ -149,7 +148,7 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L148
       return true
     elsif @tokens.size >= 1 and @tokens[-1][1] == :on_heredoc_end # "EOH\n"
       return false
-    elsif @tokens.size >= 2 and @tokens[-2][3].anybits?(continued_bits)
+    elsif @tokens.size >= 2 and defined?(Ripper::EXPR_BEG) and @tokens[-2][3].anybits?(Ripper::EXPR_BEG | Ripper::EXPR_FNAME)
       # end of literal except for regexp
       return true
     end
@@ -221,19 +220,21 @@ class RubyLex https://github.com/ruby/ruby/blob/trunk/lib/irb/ruby-lex.rb#L220
       $VERBOSE = verbose
     end
 
-    last_lex_state = @tokens.last[3]
-    if last_lex_state.allbits?(Ripper::EXPR_BEG)
-      return false
-    elsif last_lex_state.allbits?(Ripper::EXPR_DOT)
-      return true
-    elsif last_lex_state.allbits?(Ripper::EXPR_CLASS)
-      return true
-    elsif last_lex_state.allbits?(Ripper::EXPR_FNAME)
-      return true
-    elsif last_lex_state.allbits?(Ripper::EXPR_VALUE)
-      return true
-    elsif last_lex_state.allbits?(Ripper::EXPR_ARG)
-      return false
+    if defined?(Ripper::EXPR_BEG)
+      last_lex_state = @tokens.last[3]
+      if last_lex_state.allbits?(Ripper::EXPR_BEG)
+        return false
+      elsif last_lex_state.allbits?(Ripper::EXPR_DOT)
+        return true
+      elsif last_lex_state.allbits?(Ripper::EXPR_CLASS)
+        return true
+      elsif last_lex_state.allbits?(Ripper::EXPR_FNAME)
+        return true
+      elsif last_lex_state.allbits?(Ripper::EXPR_VALUE)
+        return true
+      elsif last_lex_state.allbits?(Ripper::EXPR_ARG)
+        return false
+      end
     end
 
     false
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 6af2e24..60b7a9f 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -18,13 +18,8 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L18
     CYAN      = "\e[36m"
 
     def test_colorize_code
-      if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
-        assert_equal({}, IRB::Color::TOKEN_SEQ_EXPRS)
-        skip "this Ripper version is not supported"
-      end
-
       # Common behaviors. Warn parser error, but do not warn compile error.
-      {
+      tests = {
         "1" => "#{BLUE}#{BOLD}1#{CLEAR}",
         "2.3" => "#{MAGENTA}#{BOLD}2.3#{CLEAR}",
         "7r" => "#{BLUE}#{BOLD}7r#{CLEAR}",
@@ -39,22 +34,17 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L34
         '"foo#{a} #{b}"' => "#{RED}\"#{CLEAR}#{RED}foo#{CLEAR}#{RED}\#{#{CLEAR}a#{RED}}#{CLEAR}#{RED} #{CLEAR}#{RED}\#{#{CLEAR}b#{RED}}#{CLEAR}#{RED}\"#{CLEAR}",
         '/r#{e}g/' => "#{RED}#{BOLD}/#{CLEAR}#{RED}r#{CLEAR}#{RED}\#{#{CLEAR}e#{RED}}#{CLEAR}#{RED}g#{CLEAR}#{RED}#{BOLD}/#{CLEAR}",
         "'a\nb'" => "#{RED}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}'#{CLEAR}",
-        "4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}",
         "[1]]]\u0013" => "[1]]]^S",
-        "\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}#{RED}#{REVERSE}m#{CLEAR}\n",
         "%w[a b]" => "#{RED}%w[#{CLEAR}#{RED}a#{CLEAR} #{RED}b#{CLEAR}#{RED}]#{CLEAR}",
         "%i[c d]" => "#{RED}%i[#{CLEAR}#{RED}c#{CLEAR} #{RED}d#{CLEAR}#{RED}]#{CLEAR}",
         "{'a': 1}" => "{#{RED}'#{CLEAR}#{RED}a#{CLEAR}#{RED}':#{CLEAR} #{BLUE}#{BOLD}1#{CLEAR}}",
         ":Struct" => "#{YELLOW}:#{CLEAR}#{YELLOW}Struct#{CLEAR}",
-        "<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}",
         '"#{}"' => "#{RED}\"#{CLEAR}#{RED}\#{#{CLEAR}#{RED}}#{CLEAR}#{RED}\"#{CLEAR}",
         ':"a#{}b"' => "#{YELLOW}:\"#{CLEAR}#{YELLOW}a#{CLEAR}#{YELLOW}\#{#{CLEAR}#{YELLOW}}#{CLEAR}#{YELLOW}b#{CLEAR}#{YELLOW}\"#{CLEAR}",
         ':"a#{ def b; end; \'c\' + "#{ :d }" }e"' => "#{YELLOW}:\"#{CLEAR}#{YELLOW}a#{CLEAR}#{YELLOW}\#{#{CLEAR} #{GREEN}def#{CLEAR} #{BLUE}#{BOLD}b#{CLEAR}; #{GREEN}end#{CLEAR}; #{RED}'#{CLEAR}#{RED}c#{CLEAR}#{RED}'#{CLEAR} + #{RED}\"#{CLEAR}#{RED}\#{#{CLEAR} #{YELLOW}:#{CLEAR}#{YELLOW}d#{CLEAR} #{RED}}#{CLEAR}#{RED}\"#{CLEAR} #{YELLOW}}#{CLEAR}#{YELLOW}e#{CLEAR}#{YELLOW}\"#{CLEAR}",
         "[__FILE__, __LINE__]" => "[#{CYAN}#{BOLD}__FILE__#{CLEAR}, #{CYAN}#{BOLD}__LINE__#{CLEAR}]",
         ":self" => "#{YELLOW}:#{CLEAR}#{YELLOW}self#{CLEAR}",
         ":class" => "#{YELLOW}:#{CLEAR}#{YELLOW}class#{CLEAR}",
-        ":@1" => "#{YELLOW}:#{CLEAR}#{RED}#{REVERSE}@1#{CLEAR}",
-        "@@1" => "#{RED}#{REVERSE}@@1#{CLEAR}",
         "[:end, 2]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}end#{CLEAR}, #{BLUE}#{BOLD}2#{CLEAR}]",
         "[:>, 3]" => "[#{YELLOW}:#{CLEAR}#{YELLOW}>#{CLEAR}, #{BLUE}#{BOLD}3#{CLEAR}]",
         ":Hello ? world : nil" => "#{YELLOW}:#{CLEAR}#{YELLOW}Hello#{CLEAR} ? world : #{CYAN}#{BOLD}nil#{CLEAR}",
@@ -69,16 +59,37 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L59
         "\t" => "\t", # not ^I
         "foo(*%W(bar))" => "foo(*#{RED}%W(#{CLEAR}#{RED}bar#{CLEAR}#{RED})#{CLEAR})",
         "$stdout" => "#{GREEN}#{BOLD}$stdout#{CLEAR}",
-      }.each do |code, result|
-        actual = with_term { IRB::Color.colorize_code(code, complete: true) }
-        assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: true)\nResult: #{humanized_literal(actual)}")
+      }
 
-        actual = with_term { IRB::Color.colorize_code(code, complete: false) }
-        assert_equal(result, actual, "Case: colorize_code(#{code.dump}, complete: false)\nResult: #{humanized_literal(actual)}")
+      if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.7.0')
+        tests.merge!({
+          "4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}",
+          "\e[0m\n" => "#{RED}#{REVERSE}^[#{CLEAR}[#{BLUE}#{BOLD}0#{CLEAR}#{RED}#{REVERSE}m#{CLEAR}\n",
+          "<<EOS\nhere\nEOS" => "#{RED}<<EOS#{CLEAR}\n#{RED}here#{CLEAR}\n#{RED}EOS#{CLEAR}",
+          ":@1" => "#{YELLOW}:#{CLEAR}#{RED}#{REVERSE}@1#{CLEAR}",
+          "@@1" => "#{RED}#{REVERSE}@@1#{CLEAR}",
+        })
+      end
+
+      tests.each do |code, result|
+        if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.5.0')
+          # colorize_code is supported only for Ruby 2.5+. Just return the original code in 2.4-.
+          actual = with_term { IRB::Color.colorize_code(code) }
+          assert_equal(code, actual)
+        else
+          actual = with_term { IRB::Color.colorize_code(code, complete: true) }
+          assert_equal(result, actual,  (... truncated)

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

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