mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Ripper::Lexer: fallback parse error token to the previous one
This commit is contained in:
parent
8354cfd03b
commit
9593e76ac2
3 changed files with 13 additions and 3 deletions
|
@ -103,7 +103,17 @@ class Ripper
|
||||||
|
|
||||||
# parse the code and returns elements including errors.
|
# parse the code and returns elements including errors.
|
||||||
def scan
|
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
|
end
|
||||||
|
|
||||||
def parse
|
def parse
|
||||||
|
|
|
@ -137,7 +137,7 @@ module IRB # :nodoc:
|
||||||
|
|
||||||
Ripper::Lexer.new(code).scan.each do |elem|
|
Ripper::Lexer.new(code).scan.each do |elem|
|
||||||
str = elem.tok
|
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
|
next if ([elem.pos[0], elem.pos[1] + str.bytesize] <=> pos) <= 0
|
||||||
|
|
||||||
str.each_line do |line|
|
str.each_line do |line|
|
||||||
|
|
|
@ -41,7 +41,7 @@ module TestIRB
|
||||||
"'a\nb'" => "#{RED}'#{CLEAR}#{RED}a#{CLEAR}\n#{RED}b#{CLEAR}#{RED}'#{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}",
|
"4.5.6" => "#{MAGENTA}#{BOLD}4.5#{CLEAR}#{RED}#{REVERSE}.6#{CLEAR}",
|
||||||
"[1]]]\u0013" => "[1]]]^S",
|
"[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}",
|
"%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}",
|
"%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}}",
|
"{'a': 1}" => "{#{RED}'#{CLEAR}#{RED}a#{CLEAR}#{RED}':#{CLEAR} #{BLUE}#{BOLD}1#{CLEAR}}",
|
||||||
|
|
Loading…
Add table
Reference in a new issue