mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
[ruby/irb] Fix ripper_lex_without_warning duplicated heredoc token
https://github.com/ruby/irb/commit/45b539af39
This commit is contained in:
parent
31461c7e0e
commit
641310ce37
2 changed files with 17 additions and 8 deletions
|
@ -152,17 +152,13 @@ class RubyLex
|
||||||
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
|
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
|
||||||
if lexer.respond_to?(:scan) # Ruby 2.7+
|
if lexer.respond_to?(:scan) # Ruby 2.7+
|
||||||
tokens = []
|
tokens = []
|
||||||
pos_to_index = {}
|
|
||||||
lexer.scan.each do |t|
|
lexer.scan.each do |t|
|
||||||
next if t.pos.first == 0
|
next if t.pos.first == 0
|
||||||
if pos_to_index.has_key?(t.pos)
|
prev_tk = tokens.last
|
||||||
index = pos_to_index[t.pos]
|
position_overlapped = prev_tk && t.pos[0] == prev_tk.pos[0] && t.pos[1] < prev_tk.pos[1] + prev_tk.tok.bytesize
|
||||||
found_tk = tokens[index]
|
if position_overlapped
|
||||||
if ERROR_TOKENS.include?(found_tk.event) && !ERROR_TOKENS.include?(t.event)
|
tokens[-1] = t if ERROR_TOKENS.include?(prev_tk.event) && !ERROR_TOKENS.include?(t.event)
|
||||||
tokens[index] = t
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
pos_to_index[t.pos] = tokens.size
|
|
||||||
tokens << t
|
tokens << t
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -619,5 +619,18 @@ module TestIRB
|
||||||
pos_to_index[t.pos] = i
|
pos_to_index[t.pos] = i
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_unterminated_code
|
||||||
|
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.7.0')
|
||||||
|
pend 'This test needs Ripper::Lexer#scan to take broken tokens'
|
||||||
|
end
|
||||||
|
|
||||||
|
['do', '<<A'].each do |code|
|
||||||
|
tokens = RubyLex.ripper_lex_without_warning(code)
|
||||||
|
assert_equal(code, tokens.map(&:tok).join, "Cannot reconstruct code from tokens")
|
||||||
|
error_tokens = tokens.map(&:event).grep(/error/)
|
||||||
|
assert_empty(error_tokens, 'Error tokens must be ignored if there is corresponding non-error token')
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue