pry 0.6.7pre4-i386-mingw32 → 0.6.8-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,316 +1,316 @@
1
- class Pry
2
-
3
- # The list of configuration options.
4
- CONFIG_OPTIONS = [:input, :output, :commands, :print,
5
- :prompt, :hooks]
6
-
7
- attr_accessor *CONFIG_OPTIONS
8
-
9
- # Create a new `Pry` object.
10
- # @param [Hash] options The optional configuration parameters.
11
- # @option options [#readline] :input The object to use for input.
12
- # @option options [#puts] :output The object to use for output.
13
- # @option options [Pry::CommandBase] :commands The object to use for
14
- # commands. (see commands.rb)
15
- # @option options [Hash] :hooks The defined hook Procs (see hooks.rb)
16
- # @option options [Array<Proc>] :default_prompt The array of Procs
17
- # to use for the prompts. (see prompts.rb)
18
- # @option options [Proc] :print The Proc to use for the 'print'
19
- # component of the REPL. (see print.rb)
20
- def initialize(options={})
21
-
22
- default_options = {}
23
- CONFIG_OPTIONS.each { |v| default_options[v] = Pry.send(v) }
24
- default_options.merge!(options)
25
-
26
- CONFIG_OPTIONS.each do |key|
27
- instance_variable_set("@#{key}", default_options[key])
28
- end
29
- end
30
-
31
- # Get nesting data.
32
- # This method should not need to be accessed directly.
33
- # @return [Array] The unparsed nesting information.
34
- def nesting
35
- self.class.nesting
36
- end
37
-
38
- # Set nesting data.
39
- # This method should not need to be accessed directly.
40
- # @param v nesting data.
41
- def nesting=(v)
42
- self.class.nesting = v
43
- end
44
-
45
- # Return parent of current Pry session.
46
- # @return [Pry] The parent of the current Pry session.
47
- def parent
48
- idx = Pry.sessions.index(self)
49
-
50
- if idx > 0
51
- Pry.sessions[idx - 1]
52
- else
53
- nil
54
- end
55
- end
56
-
57
- # Execute the hook `hook_name`, if it is defined.
58
- # @param [Symbol] hook_name The hook to execute
59
- # @param [Array] args The arguments to pass to the hook.
60
- def exec_hook(hook_name, *args, &block)
61
- hooks[hook_name].call(*args, &block) if hooks[hook_name]
62
- end
63
-
64
- # Start a read-eval-print-loop.
65
- # If no parameter is given, default to top-level (main).
66
- # @param [Object, Binding] target The receiver of the Pry session
67
- # @return [Object] The target of the Pry session or an explictly given
68
- # return value. If given return value is `nil` or no return value
69
- # is specified then `target` will be returned.
70
- # @example
71
- # Pry.new.repl(Object.new)
72
- def repl(target=TOPLEVEL_BINDING)
73
- target = Pry.binding_for(target)
74
- target_self = target.eval('self')
75
-
76
- exec_hook :before_session, output, target_self
77
-
78
- # cannot rely on nesting.level as
79
- # nesting.level changes with new sessions
80
- nesting_level = nesting.size
81
-
82
- Pry.active_instance = self
83
-
84
- # Make sure special locals exist
85
- target.eval("_pry_ = Pry.active_instance")
86
- target.eval("_ = Pry.last_result")
87
-
88
- break_data = catch(:breakout) do
89
- nesting.push [nesting.size, target_self, self]
90
- loop do
91
- rep(target)
92
- end
93
- end
94
-
95
- nesting.pop
96
-
97
- exec_hook :after_session, output, target_self
98
-
99
- # If break_data is an array, then the last element is the return value
100
- break_level, return_value = Array(break_data)
101
-
102
- # keep throwing until we reach the desired nesting level
103
- if nesting_level != break_level
104
- throw :breakout, break_data
105
- end
106
-
107
- # if one was provided, return the return value
108
- return return_value if return_value
109
-
110
- # otherwise return the target_self
111
- target_self
112
- end
113
-
114
- # Perform a read-eval-print.
115
- # If no parameter is given, default to top-level (main).
116
- # @param [Object, Binding] target The receiver of the read-eval-print
117
- # @example
118
- # Pry.new.rep(Object.new)
119
- def rep(target=TOPLEVEL_BINDING)
120
- target = Pry.binding_for(target)
121
- print.call output, re(target)
122
- end
123
-
124
- # Perform a read-eval
125
- # If no parameter is given, default to top-level (main).
126
- # @param [Object, Binding] target The receiver of the read-eval-print
127
- # @return [Object] The result of the eval or an `Exception` object in case of error.
128
- # @example
129
- # Pry.new.re(Object.new)
130
- def re(target=TOPLEVEL_BINDING)
131
- target = Pry.binding_for(target)
132
-
133
- if input == Readline
134
- # Readline tab completion
135
- Readline.completion_proc = Pry::InputCompleter.build_completion_proc(target, commands.commands.keys)
136
- end
137
-
138
- # eval the expression and save to last_result
139
- Pry.last_result = target.eval r(target)
140
-
141
- # save the pry instance to active_instance
142
- Pry.active_instance = self
143
-
144
- # define locals _pry_ and _ (active instance and last expression)
145
- target.eval("_pry_ = Pry.active_instance")
146
- target.eval("_ = Pry.last_result")
147
- rescue SystemExit => e
148
- exit
149
- rescue Exception => e
150
- e
151
- end
152
-
153
- # Perform a read.
154
- # If no parameter is given, default to top-level (main).
155
- # This is a multi-line read; so the read continues until a valid
156
- # Ruby expression is received.
157
- # Pry commands are also accepted here and operate on the target.
158
- # @param [Object, Binding] target The receiver of the read.
159
- # @return [String] The Ruby expression.
160
- # @example
161
- # Pry.new.r(Object.new)
162
- def r(target=TOPLEVEL_BINDING)
163
- target = Pry.binding_for(target)
164
- eval_string = ""
165
-
166
- loop do
167
- current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
168
-
169
- val = readline(current_prompt)
170
-
171
- # exit pry if we receive EOF character
172
- if !val
173
- output.puts
174
- throw(:breakout, nesting.level)
175
- end
176
-
177
- val.chomp!
178
-
179
- Pry.cmd_ret_value = process_commands(val, eval_string, target)
180
-
181
- if Pry.cmd_ret_value
182
- eval_string << "Pry.cmd_ret_value\n"
183
- else
184
- eval_string << "#{val}\n"
185
- end
186
-
187
- break eval_string if valid_expression?(eval_string)
188
- end
189
- end
190
-
191
- # Process Pry commands. Pry commands are not Ruby methods and are evaluated
192
- # prior to Ruby expressions.
193
- # Commands can be modified/configured by the user: see `Pry::Commands`
194
- # This method should not need to be invoked directly - it is called
195
- # by `Pry#r`.
196
- # @param [String] val The current line of input.
197
- # @param [String] eval_string The cumulative lines of input for
198
- # multi-line input.
199
- # @param [Binding] target The receiver of the commands.
200
- def process_commands(val, eval_string, target)
201
- def val.clear() replace("") end
202
- def eval_string.clear() replace("") end
203
-
204
- pattern, cmd_data = commands.commands.find do |name, cmd_data|
205
- /^#{name}(?!\S)(?:\s+(.+))?/ =~ val
206
- end
207
-
208
- # no command was matched, so return to caller
209
- return if !pattern
210
-
211
- args_string = $1
212
- args = args_string ? Shellwords.shellwords(args_string) : []
213
- action = cmd_data[:action]
214
- keep_retval = cmd_data[:keep_retval]
215
-
216
- options = {
217
- :val => val,
218
- :eval_string => eval_string,
219
- :nesting => nesting,
220
- :commands => commands.commands
221
- }
222
-
223
- # set some useful methods to be used by the action blocks
224
- commands.opts = options
225
- commands.target = target
226
- commands.output = output
227
-
228
- case action.arity <=> 0
229
- when -1
230
-
231
- # Use instance_exec() to make the `opts` method, etc available
232
- ret_value = commands.instance_exec(*args, &action)
233
- when 1, 0
234
-
235
- # ensure that we get the right number of parameters
236
- # since 1.8.7 complains about incorrect arity (1.9.2
237
- # doesn't care)
238
- args_with_corrected_arity = args.values_at *0..(action.arity - 1)
239
- ret_value = commands.instance_exec(*args_with_corrected_arity, &action)
240
- end
241
-
242
- # a command was processed so we can now clear the input string
243
- val.clear
244
-
245
- # return value of block only if :keep_retval is true
246
- ret_value if keep_retval
247
- end
248
-
249
- # Returns the next line of input to be used by the pry instance.
250
- # This method should not need to be invoked directly.
251
- # @param [String] current_prompt The prompt to use for input.
252
- # @return [String] The next line of input.
253
- def readline(current_prompt="> ")
254
-
255
- if input == Readline
256
-
257
- # Readline must be treated differently
258
- # as it has a second parameter.
259
- input.readline(current_prompt, true)
260
- else
261
- if input.method(:readline).arity == 1
262
- input.readline(current_prompt)
263
- else
264
- input.readline
265
- end
266
- end
267
- end
268
-
269
- # Returns the appropriate prompt to use.
270
- # This method should not need to be invoked directly.
271
- # @param [Boolean] first_line Whether this is the first line of input
272
- # (and not multi-line input).
273
- # @param [Object] target_self The receiver of the Pry session.
274
- # @return [String] The prompt.
275
- def select_prompt(first_line, target_self)
276
-
277
- if first_line
278
- Array(prompt).first.call(target_self, nesting.level)
279
- else
280
- Array(prompt).last.call(target_self, nesting.level)
281
- end
282
- end
283
-
284
- if RUBY_VERSION =~ /1.9/
285
- require 'ripper'
286
-
287
- # Determine if a string of code is a valid Ruby expression.
288
- # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
289
- # @param [String] code The code to validate.
290
- # @return [Boolean] Whether or not the code is a valid Ruby expression.
291
- # @example
292
- # valid_expression?("class Hello") #=> false
293
- # valid_expression?("class Hello; end") #=> true
294
- def valid_expression?(code)
295
- !!Ripper::SexpBuilder.new(code).parse
296
- end
297
-
298
- else
299
- require 'ruby_parser'
300
-
301
- # Determine if a string of code is a valid Ruby expression.
302
- # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
303
- # @param [String] code The code to validate.
304
- # @return [Boolean] Whether or not the code is a valid Ruby expression.
305
- # @example
306
- # valid_expression?("class Hello") #=> false
307
- # valid_expression?("class Hello; end") #=> true
308
- def valid_expression?(code)
309
- RubyParser.new.parse(code)
310
- rescue Racc::ParseError, SyntaxError
311
- false
312
- else
313
- true
314
- end
315
- end
316
- end
1
+ class Pry
2
+
3
+ # The list of configuration options.
4
+ CONFIG_OPTIONS = [:input, :output, :commands, :print,
5
+ :prompt, :hooks]
6
+
7
+ attr_accessor *CONFIG_OPTIONS
8
+
9
+ # Create a new `Pry` object.
10
+ # @param [Hash] options The optional configuration parameters.
11
+ # @option options [#readline] :input The object to use for input.
12
+ # @option options [#puts] :output The object to use for output.
13
+ # @option options [Pry::CommandBase] :commands The object to use for
14
+ # commands. (see commands.rb)
15
+ # @option options [Hash] :hooks The defined hook Procs (see hooks.rb)
16
+ # @option options [Array<Proc>] :default_prompt The array of Procs
17
+ # to use for the prompts. (see prompts.rb)
18
+ # @option options [Proc] :print The Proc to use for the 'print'
19
+ # component of the REPL. (see print.rb)
20
+ def initialize(options={})
21
+
22
+ default_options = {}
23
+ CONFIG_OPTIONS.each { |v| default_options[v] = Pry.send(v) }
24
+ default_options.merge!(options)
25
+
26
+ CONFIG_OPTIONS.each do |key|
27
+ instance_variable_set("@#{key}", default_options[key])
28
+ end
29
+ end
30
+
31
+ # Get nesting data.
32
+ # This method should not need to be accessed directly.
33
+ # @return [Array] The unparsed nesting information.
34
+ def nesting
35
+ self.class.nesting
36
+ end
37
+
38
+ # Set nesting data.
39
+ # This method should not need to be accessed directly.
40
+ # @param v nesting data.
41
+ def nesting=(v)
42
+ self.class.nesting = v
43
+ end
44
+
45
+ # Return parent of current Pry session.
46
+ # @return [Pry] The parent of the current Pry session.
47
+ def parent
48
+ idx = Pry.sessions.index(self)
49
+
50
+ if idx > 0
51
+ Pry.sessions[idx - 1]
52
+ else
53
+ nil
54
+ end
55
+ end
56
+
57
+ # Execute the hook `hook_name`, if it is defined.
58
+ # @param [Symbol] hook_name The hook to execute
59
+ # @param [Array] args The arguments to pass to the hook.
60
+ def exec_hook(hook_name, *args, &block)
61
+ hooks[hook_name].call(*args, &block) if hooks[hook_name]
62
+ end
63
+
64
+ # Start a read-eval-print-loop.
65
+ # If no parameter is given, default to top-level (main).
66
+ # @param [Object, Binding] target The receiver of the Pry session
67
+ # @return [Object] The target of the Pry session or an explictly given
68
+ # return value. If given return value is `nil` or no return value
69
+ # is specified then `target` will be returned.
70
+ # @example
71
+ # Pry.new.repl(Object.new)
72
+ def repl(target=TOPLEVEL_BINDING)
73
+ target = Pry.binding_for(target)
74
+ target_self = target.eval('self')
75
+
76
+ exec_hook :before_session, output, target
77
+
78
+ # cannot rely on nesting.level as
79
+ # nesting.level changes with new sessions
80
+ nesting_level = nesting.size
81
+
82
+ Pry.active_instance = self
83
+
84
+ # Make sure special locals exist
85
+ target.eval("_pry_ = Pry.active_instance")
86
+ target.eval("_ = Pry.last_result")
87
+
88
+ break_data = catch(:breakout) do
89
+ nesting.push [nesting.size, target_self, self]
90
+ loop do
91
+ rep(target)
92
+ end
93
+ end
94
+
95
+ nesting.pop
96
+
97
+ exec_hook :after_session, output, target
98
+
99
+ # If break_data is an array, then the last element is the return value
100
+ break_level, return_value = Array(break_data)
101
+
102
+ # keep throwing until we reach the desired nesting level
103
+ if nesting_level != break_level
104
+ throw :breakout, break_data
105
+ end
106
+
107
+ # if one was provided, return the return value
108
+ return return_value if return_value
109
+
110
+ # otherwise return the target_self
111
+ target_self
112
+ end
113
+
114
+ # Perform a read-eval-print.
115
+ # If no parameter is given, default to top-level (main).
116
+ # @param [Object, Binding] target The receiver of the read-eval-print
117
+ # @example
118
+ # Pry.new.rep(Object.new)
119
+ def rep(target=TOPLEVEL_BINDING)
120
+ target = Pry.binding_for(target)
121
+ print.call output, re(target)
122
+ end
123
+
124
+ # Perform a read-eval
125
+ # If no parameter is given, default to top-level (main).
126
+ # @param [Object, Binding] target The receiver of the read-eval-print
127
+ # @return [Object] The result of the eval or an `Exception` object in case of error.
128
+ # @example
129
+ # Pry.new.re(Object.new)
130
+ def re(target=TOPLEVEL_BINDING)
131
+ target = Pry.binding_for(target)
132
+
133
+ if input == Readline
134
+ # Readline tab completion
135
+ Readline.completion_proc = Pry::InputCompleter.build_completion_proc(target, commands.commands.keys)
136
+ end
137
+
138
+ # eval the expression and save to last_result
139
+ Pry.last_result = target.eval r(target), __FILE__, __LINE__
140
+
141
+ # save the pry instance to active_instance
142
+ Pry.active_instance = self
143
+
144
+ # define locals _pry_ and _ (active instance and last expression)
145
+ target.eval("_pry_ = Pry.active_instance")
146
+ target.eval("_ = Pry.last_result")
147
+ rescue SystemExit => e
148
+ exit
149
+ rescue Exception => e
150
+ e
151
+ end
152
+
153
+ # Perform a read.
154
+ # If no parameter is given, default to top-level (main).
155
+ # This is a multi-line read; so the read continues until a valid
156
+ # Ruby expression is received.
157
+ # Pry commands are also accepted here and operate on the target.
158
+ # @param [Object, Binding] target The receiver of the read.
159
+ # @return [String] The Ruby expression.
160
+ # @example
161
+ # Pry.new.r(Object.new)
162
+ def r(target=TOPLEVEL_BINDING)
163
+ target = Pry.binding_for(target)
164
+ eval_string = ""
165
+
166
+ loop do
167
+ current_prompt = select_prompt(eval_string.empty?, target.eval('self'))
168
+
169
+ val = readline(current_prompt)
170
+
171
+ # exit pry if we receive EOF character
172
+ if !val
173
+ output.puts
174
+ throw(:breakout, nesting.level)
175
+ end
176
+
177
+ val.chomp!
178
+
179
+ Pry.cmd_ret_value = process_commands(val, eval_string, target)
180
+
181
+ if Pry.cmd_ret_value
182
+ eval_string << "Pry.cmd_ret_value\n"
183
+ else
184
+ eval_string << "#{val}\n"
185
+ end
186
+
187
+ break eval_string if valid_expression?(eval_string)
188
+ end
189
+ end
190
+
191
+ # Process Pry commands. Pry commands are not Ruby methods and are evaluated
192
+ # prior to Ruby expressions.
193
+ # Commands can be modified/configured by the user: see `Pry::Commands`
194
+ # This method should not need to be invoked directly - it is called
195
+ # by `Pry#r`.
196
+ # @param [String] val The current line of input.
197
+ # @param [String] eval_string The cumulative lines of input for
198
+ # multi-line input.
199
+ # @param [Binding] target The receiver of the commands.
200
+ def process_commands(val, eval_string, target)
201
+ def val.clear() replace("") end
202
+ def eval_string.clear() replace("") end
203
+
204
+ pattern, cmd_data = commands.commands.find do |name, cmd_data|
205
+ /^#{name}(?!\S)(?:\s+(.+))?/ =~ val
206
+ end
207
+
208
+ # no command was matched, so return to caller
209
+ return if !pattern
210
+
211
+ args_string = $1
212
+ args = args_string ? Shellwords.shellwords(args_string) : []
213
+ action = cmd_data[:action]
214
+ keep_retval = cmd_data[:keep_retval]
215
+
216
+ options = {
217
+ :val => val,
218
+ :eval_string => eval_string,
219
+ :nesting => nesting,
220
+ :commands => commands.commands
221
+ }
222
+
223
+ # set some useful methods to be used by the action blocks
224
+ commands.opts = options
225
+ commands.target = target
226
+ commands.output = output
227
+
228
+ case action.arity <=> 0
229
+ when -1
230
+
231
+ # Use instance_exec() to make the `opts` method, etc available
232
+ ret_value = commands.instance_exec(*args, &action)
233
+ when 1, 0
234
+
235
+ # ensure that we get the right number of parameters
236
+ # since 1.8.7 complains about incorrect arity (1.9.2
237
+ # doesn't care)
238
+ args_with_corrected_arity = args.values_at *0..(action.arity - 1)
239
+ ret_value = commands.instance_exec(*args_with_corrected_arity, &action)
240
+ end
241
+
242
+ # a command was processed so we can now clear the input string
243
+ val.clear
244
+
245
+ # return value of block only if :keep_retval is true
246
+ ret_value if keep_retval
247
+ end
248
+
249
+ # Returns the next line of input to be used by the pry instance.
250
+ # This method should not need to be invoked directly.
251
+ # @param [String] current_prompt The prompt to use for input.
252
+ # @return [String] The next line of input.
253
+ def readline(current_prompt="> ")
254
+
255
+ if input == Readline
256
+
257
+ # Readline must be treated differently
258
+ # as it has a second parameter.
259
+ input.readline(current_prompt, true)
260
+ else
261
+ if input.method(:readline).arity == 1
262
+ input.readline(current_prompt)
263
+ else
264
+ input.readline
265
+ end
266
+ end
267
+ end
268
+
269
+ # Returns the appropriate prompt to use.
270
+ # This method should not need to be invoked directly.
271
+ # @param [Boolean] first_line Whether this is the first line of input
272
+ # (and not multi-line input).
273
+ # @param [Object] target_self The receiver of the Pry session.
274
+ # @return [String] The prompt.
275
+ def select_prompt(first_line, target_self)
276
+
277
+ if first_line
278
+ Array(prompt).first.call(target_self, nesting.level)
279
+ else
280
+ Array(prompt).last.call(target_self, nesting.level)
281
+ end
282
+ end
283
+
284
+ if RUBY_VERSION =~ /1.9/
285
+ require 'ripper'
286
+
287
+ # Determine if a string of code is a valid Ruby expression.
288
+ # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
289
+ # @param [String] code The code to validate.
290
+ # @return [Boolean] Whether or not the code is a valid Ruby expression.
291
+ # @example
292
+ # valid_expression?("class Hello") #=> false
293
+ # valid_expression?("class Hello; end") #=> true
294
+ def valid_expression?(code)
295
+ !!Ripper::SexpBuilder.new(code).parse
296
+ end
297
+
298
+ else
299
+ require 'ruby_parser'
300
+
301
+ # Determine if a string of code is a valid Ruby expression.
302
+ # Ruby 1.9 uses Ripper, Ruby 1.8 uses RubyParser.
303
+ # @param [String] code The code to validate.
304
+ # @return [Boolean] Whether or not the code is a valid Ruby expression.
305
+ # @example
306
+ # valid_expression?("class Hello") #=> false
307
+ # valid_expression?("class Hello; end") #=> true
308
+ def valid_expression?(code)
309
+ RubyParser.new.parse(code)
310
+ rescue Racc::ParseError, SyntaxError
311
+ false
312
+ else
313
+ true
314
+ end
315
+ end
316
+ end