mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
added # commands, all Pry commands begin with #
This commit is contained in:
parent
53e40e9865
commit
a3399b96ed
1 changed files with 59 additions and 33 deletions
92
lib/pry.rb
92
lib/pry.rb
|
@ -2,41 +2,67 @@ require 'rubygems'
|
|||
require 'readline'
|
||||
require 'ruby_parser'
|
||||
|
||||
class RubyParser
|
||||
def self.valid?(code)
|
||||
new.parse(code)
|
||||
rescue Racc::ParseError
|
||||
false
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def pry(target)
|
||||
eval_string = ""
|
||||
while true
|
||||
prompt = ""
|
||||
if eval_string.empty?
|
||||
prompt = "> "
|
||||
module Pry
|
||||
module RubyParserExtension
|
||||
def valid?(code)
|
||||
new.parse(code)
|
||||
rescue Racc::ParseError
|
||||
false
|
||||
else
|
||||
prompt = "* "
|
||||
true
|
||||
end
|
||||
|
||||
val = Readline.readline(prompt, true)
|
||||
eval_string += val
|
||||
|
||||
if val == "!"
|
||||
eval_string = ""
|
||||
puts "refreshing REPL state"
|
||||
break
|
||||
end
|
||||
|
||||
exit if val == "quit"
|
||||
break if RubyParser.valid?(eval_string)
|
||||
end
|
||||
begin
|
||||
puts "=> #{target.instance_eval(eval_string).inspect}"
|
||||
rescue StandardError => e
|
||||
puts "#{e.message}"
|
||||
|
||||
def self.repl_loop(target=TOPLEVEL_BINDING)
|
||||
repl(target, :loop => true)
|
||||
end
|
||||
|
||||
def self.repl(target=TOPLEVEL_BINDING, options={:loop => false})
|
||||
prompt = ""
|
||||
code = proc do
|
||||
eval_string = ""
|
||||
while true
|
||||
if eval_string.empty?
|
||||
prompt = "> "
|
||||
else
|
||||
prompt = "* "
|
||||
end
|
||||
|
||||
val = Readline.readline(prompt, true)
|
||||
eval_string += "#{val}\n"
|
||||
|
||||
if val == "#"
|
||||
elsif val == "#pop"
|
||||
puts "Poppping back"
|
||||
return
|
||||
elsif (_, new_target = val.split(/#target\s*\=\s*/)).size > 1
|
||||
target = target.eval(new_target)
|
||||
eval_string = ""
|
||||
puts "Context changed to #{target}"
|
||||
break
|
||||
end
|
||||
|
||||
abort if val == "abort"
|
||||
exit if val == "exit"
|
||||
exit if val == "quit"
|
||||
|
||||
break if RubyParser.valid?(eval_string)
|
||||
end
|
||||
begin
|
||||
puts "=> #{target.eval(eval_string).inspect}"
|
||||
rescue StandardError => e
|
||||
puts "#{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
if options[:loop]
|
||||
loop(&code)
|
||||
else
|
||||
code.call
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class RubyParser
|
||||
extend Pry::RubyParserExtension
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue