pry--pry/lib/pry.rb

236 lines
4.7 KiB
Ruby
Raw Normal View History

# (C) John Mair (banisterfiend) 2010
# MIT License
direc = File.dirname(__FILE__)
require "method_source"
require "#{direc}/pry/version"
require "#{direc}/pry/input"
require "#{direc}/pry/output"
require "#{direc}/pry/commands"
2010-12-07 08:11:35 +00:00
class Pry
def self.start(target=TOPLEVEL_BINDING, options={})
options = {
:input => Pry.input,
:output => Pry.output
}.merge!(options)
new(options[:input], options[:output]).repl(target)
end
def self.view(obj)
case obj
when String, Array, Hash, Symbol, nil
obj.inspect
else
obj.to_s
end
end
def self.reset_defaults
self.input = Input.new
self.output = Output.new
self.commands = Commands.new(self.output)
self.default_prompt = proc do |v, nest|
if nest == 0
"pry(#{Pry.view(v)})> "
else
"pry(#{Pry.view(v)}):#{Pry.view(nest)}> "
end
end
self.wait_prompt = proc do |v, nest|
if nest == 0
"pry(#{Pry.view(v)})* "
else
"pry(#{Pry.view(v)}):#{Pry.view(nest)}* "
end
end
end
# class accessors
2010-12-08 11:39:06 +00:00
class << self
attr_reader :nesting
attr_accessor :last_result
attr_accessor :default_prompt, :wait_prompt
attr_accessor :input, :output
attr_accessor :commands
end
self.reset_defaults
@nesting = []
def @nesting.level
last.is_a?(Array) ? last.first : nil
2010-12-08 11:39:06 +00:00
end
attr_accessor :input, :output
attr_accessor :default_prompt, :wait_prompt
attr_accessor :commands
attr_reader :last_result
2010-12-08 11:39:06 +00:00
def initialize(input = Pry.input, output = Pry.output)
@input = input
@output = output
2010-12-12 00:35:07 +00:00
@default_prompt = Pry.default_prompt
@wait_prompt = Pry.wait_prompt
@commands = Commands.new(output)
end
def nesting
self.class.nesting
end
def nesting=(v)
self.class.nesting = v
end
2010-12-08 11:39:06 +00:00
2010-12-08 07:30:38 +00:00
# loop
def repl(target=TOPLEVEL_BINDING)
target = binding_for(target)
target_self = target.eval('self')
output.session_start(target_self)
2010-12-08 11:39:06 +00:00
nesting_level = nesting.size
# Make sure _ exists
target.eval("_ = Pry.last_result")
break_level = catch(:breakout) do
nesting << [nesting.size, target_self]
loop do
rep(target)
2010-12-08 07:30:38 +00:00
end
2010-12-07 08:11:35 +00:00
end
2010-12-08 11:39:06 +00:00
nesting.pop
output.session_end(target_self)
# we only enter here if :breakout has been thrown
if nesting_level != break_level
throw :breakout, break_level
end
target_self
end
2010-12-08 07:30:38 +00:00
# print
def rep(target=TOPLEVEL_BINDING)
target = binding_for(target)
output.print re(target)
2010-12-08 07:30:38 +00:00
end
2010-12-08 07:30:38 +00:00
# eval
def re(target=TOPLEVEL_BINDING)
target = binding_for(target)
Pry.last_result = target.eval r(target)
target.eval("_ = Pry.last_result")
rescue SystemExit => e
exit
rescue Exception => e
2010-12-08 07:30:38 +00:00
e
end
2010-12-08 07:30:38 +00:00
# read
def r(target=TOPLEVEL_BINDING)
target = binding_for(target)
2010-12-08 07:30:38 +00:00
eval_string = ""
loop do
val = input.read(prompt(eval_string, target, nesting.level))
eval_string << "#{val.chomp}\n"
2010-12-08 11:39:06 +00:00
process_commands(val, eval_string, target)
2010-12-08 07:30:38 +00:00
break eval_string if valid_expression?(eval_string)
end
end
def process_commands(val, eval_string, target)
def eval_string.clear() replace("") end
if action = commands.commands.find { |k, v| Array(k).any? { |a| a === val } }
options = {
:captures => $~ ? $~.captures : nil,
:eval_string => eval_string,
:target => target,
:val => val,
:nesting => nesting,
:output => output
}
action.last.call(options)
2010-12-07 08:11:35 +00:00
end
2010-12-08 07:30:38 +00:00
end
2010-12-07 08:11:35 +00:00
def prompt(eval_string, target, nest)
2010-12-08 14:57:50 +00:00
target_self = target.eval('self')
2010-12-08 07:30:38 +00:00
if eval_string.empty?
default_prompt.call(target_self, nest)
else
wait_prompt.call(target_self, nest)
end
2010-12-07 08:11:35 +00:00
end
if RUBY_VERSION =~ /1.9/
require 'ripper'
def valid_expression?(code)
!!Ripper::SexpBuilder.new(code).parse
end
2010-12-08 07:30:38 +00:00
else
require 'ruby_parser'
def valid_expression?(code)
RubyParser.new.parse(code)
rescue Racc::ParseError, SyntaxError
false
else
true
end
2010-12-08 07:30:38 +00:00
end
def binding_for(target)
if target.is_a?(Binding)
target
else
if target == TOPLEVEL_BINDING.eval('self')
TOPLEVEL_BINDING
else
target.__binding__
end
end
end
module ObjectExtensions
def pry(target=self)
Pry.start(target)
end
def __binding__
if is_a?(Module)
return class_eval "binding"
end
unless respond_to? :__binding_impl__
self.class.class_eval <<-EXTRA
def __binding_impl__
binding
end
EXTRA
end
__binding_impl__
end
end
end
class Object
include Pry::ObjectExtensions
end