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

ruby-changes:56093

From: Nobuyoshi <ko1@a...>
Date: Wed, 12 Jun 2019 15:29:05 +0900 (JST)
Subject: [ruby-changes:56093] Nobuyoshi Nakada: 9593e76ac2 (trunk): Ripper::Lexer: fallback parse error token to the previous one

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

From 9593e76ac2cfd9366b8b904df3fc3e1047af3aee Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Wed, 12 Jun 2019 15:23:47 +0900
Subject: Ripper::Lexer: fallback parse error token to the previous one


diff --git a/ext/ripper/lib/ripper/lexer.rb b/ext/ripper/lib/ripper/lexer.rb
index 696471a..3a2138f 100644
--- a/ext/ripper/lib/ripper/lexer.rb
+++ b/ext/ripper/lib/ripper/lexer.rb
@@ -103,7 +103,17 @@ class Ripper https://github.com/ruby/ruby/blob/trunk/ext/ripper/lib/ripper/lexer.rb#L103
 
     # parse the code and returns elements including errors.
     def scan
-      (parse() + errors + @stack.flatten).uniq.sort_by {|e| [*e.pos, (e.message ? -1 : 0)]}
+      result = (parse() + errors + @stack.flatten).uniq.sort_by {|e| [*e.pos, (e.message ? -1 : 0)]}
+      result.each_with_index do |e, i|
+        if e.event == :on_parse_error and e.tok.empty? and (pre = result[i-1]) and
+          pre.pos[0] == e.pos[0] and (pre.pos[1] + pre.tok.size) == e.pos[1]
+          e.tok = pre.tok
+          e.pos[1] = pre.pos[1]
+          result[i-1] = e
+          result[i] = pre
+        end
+      end
+      result
     end
 
     def parse
diff --git a/lib/irb/color.rb b/lib/irb/color.rb
index fa8502c..b942299 100644
--- a/lib/irb/color.rb
+++ b/lib/irb/color.rb
@@ -137,7 +137,7 @@ module IRB # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/irb/color.rb#L137
 
         Ripper::Lexer.new(code).scan.each do |elem|
           str = elem.tok
-          next if allow_last_error and elem.message&.end_with?('meets end of file')
+          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|
diff --git a/test/irb/test_color.rb b/test/irb/test_color.rb
index 916443d..644f372 100644
--- a/test/irb/test_color.rb
+++ b/test/irb/test_color.rb
@@ -41,7 +41,7 @@ module TestIRB https://github.com/ruby/ruby/blob/trunk/test/irb/test_color.rb#L41
         "'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}m\n",
+        "\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}}",
-- 
cgit v0.10.2


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

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