Skip to content

Commit

Permalink
cleanup and rbx fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rf- committed Nov 26, 2011
1 parent 8d9890f commit b20ce2f
Showing 1 changed file with 8 additions and 17 deletions.
25 changes: 8 additions & 17 deletions lib/pry/pry_instance.rb
Expand Up @@ -534,9 +534,10 @@ def pop_prompt
prompt_stack.size > 1 ? prompt_stack.pop : prompt
end

# Determine if a string of code is a valid Ruby expression.
# Determine if a string of code is a complete Ruby expression.
# @param [String] code The code to validate.
# @return [Boolean] Whether or not the code is a valid Ruby expression.
# @return [Boolean] Whether or not the code is a complete Ruby expression.
# @raise [SyntaxError] Any SyntaxError that does not represent incompleteness.
# @example
# valid_expression?("class Hello") #=> false
# valid_expression?("class Hello; end") #=> true
Expand All @@ -547,11 +548,12 @@ def valid_expression?(str)
Rubinius::Melbourne.parse_string(str, Pry.eval_path)
else
catch(:valid) {
eval("BEGIN{throw :valid, true}\n#{str}", binding, Pry.eval_path)
eval("BEGIN{throw :valid}\n#{str}", binding, Pry.eval_path)
}
end
true
rescue SyntaxError => e
if incomplete_user_input_exception?(e, caller)
if incomplete_user_input_exception?(e)
false
else
raise e
Expand All @@ -560,27 +562,16 @@ def valid_expression?(str)

# Check whether the exception indicates that the user should input more.
#
# The first part of the check verifies that the exception was raised from
# the input to the eval, taking care not to be confused if the input contains
# an eval() with incomplete syntax.
#
# @param [SyntaxError] the exception object that was raised.
# @param [Array<String>] The stack frame of the function that executed eval.
# @return [Boolean]
#
def incomplete_user_input_exception?(ex, stack)
# deliberate use of ^ instead of \A, error messages can be two lines long.
from_pry_input = /^#{Regexp.escape(Pry.eval_path)}/

return false unless SyntaxError === ex && ex.message =~ from_pry_input

def incomplete_user_input_exception?(ex)
case ex.message
when /unexpected (\$end|end-of-file|END_OF_FILE)/, # mri, jruby, ironruby
/unterminated (quoted string|string|regexp) meets end of file/, # "quoted string" is ironruby
/missing 'end' for/, /: expecting '[})\]]'$/, /can't find string ".*" anywhere before EOF/, /expecting keyword_end/ # rbx
backtrace = ex.backtrace
backtrace = backtrace.drop(1) if RUBY_VERSION =~ /^1\.8/ && RUBY_PLATFORM !~ /java/
backtrace.grep(from_pry_input).count <= stack.grep(from_pry_input).count
true
else
false
end
Expand Down

0 comments on commit b20ce2f

Please sign in to comment.