Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 46 additions & 46 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,48 @@ def defined? do
yield
]

SYNTAX_ERROR_PATTERNS = {
# "syntax error, unexpected keyword_end"
#
# example:
# if (
# end
#
# example:
# end
"unexpected keyword_end" => :unrecoverable_error,
# "syntax error, unexpected '.'"
#
# example:
# .
"unexpected '.'" => :unrecoverable_error,
# "syntax error, unexpected tREGEXP_BEG, expecting keyword_do or '{' or '('"
#
# example:
# method / f /
"unexpected tREGEXP_BEG" => :unrecoverable_error,
# "unterminated regexp meets end of file"
#
# example:
# /
#
# "unterminated string meets end of file"
#
# example:
# '
"unterminated string meets end of file" => :recoverable_error,
"unterminated regexp meets end of file" => :recoverable_error,
# "syntax error, unexpected end-of-input, expecting keyword_end"
#
# example:
# if true
# hoge
# if false
# fuga
# end
"unexpected end-of-input" => :recoverable_error,
}.freeze

class TerminateLineInput < StandardError
def initialize
super("Terminate Line Input")
Expand Down Expand Up @@ -252,53 +294,11 @@ def check_code_syntax(code, local_variables:)
# This is for a hash with invalid encoding symbol, {"\xAE": 1}
:unrecoverable_error
rescue SyntaxError => e
case e.message
when /unexpected keyword_end/
# "syntax error, unexpected keyword_end"
#
# example:
# if (
# end
#
# example:
# end
return :unrecoverable_error
when /unexpected '\.'/
# "syntax error, unexpected '.'"
#
# example:
# .
return :unrecoverable_error
when /unexpected tREGEXP_BEG/
# "syntax error, unexpected tREGEXP_BEG, expecting keyword_do or '{' or '('"
#
# example:
# method / f /
return :unrecoverable_error
when /unterminated (?:string|regexp) meets end of file/
# "unterminated regexp meets end of file"
#
# example:
# /
#
# "unterminated string meets end of file"
#
# example:
# '
return :recoverable_error
when /unexpected end-of-input/
# "syntax error, unexpected end-of-input, expecting keyword_end"
#
# example:
# if true
# hoge
# if false
# fuga
# end
return :recoverable_error
else
return :other_error
SYNTAX_ERROR_PATTERNS.each do |pattern, error|
return error if e.message.include?(pattern)
end

return :other_error
ensure
$VERBOSE = verbose
end
Expand Down
24 changes: 24 additions & 0 deletions test/irb/command/test_show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,29 @@ class B
assert_match(%r[#{@ruby_file.to_path}:7\s+Z = 1], out)
assert_match(%r[#{@ruby_file.to_path}:8\s+Array = 1], out)
end

def test_show_source_with_prism_returns_invalid_utf8_string
write_ruby <<~RUBY
class A
def call
if true
nil
# あああああああああああああああああああああああ
# あああああああああああああああああああああああ
end
end
end

binding.irb
RUBY

out = run_ruby_file do
type "inst = A.new"
type "show_source inst.call"
type "exit"
end

assert_match(/def call/, out)
end
end
end
Loading