irb 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.travis.yml +6 -0
  4. data/Gemfile +5 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +55 -0
  7. data/Rakefile +10 -0
  8. data/bin/console +6 -0
  9. data/bin/setup +6 -0
  10. data/exe/irb +11 -0
  11. data/irb.gemspec +26 -0
  12. data/lib/irb.rb +798 -0
  13. data/lib/irb/cmd/chws.rb +34 -0
  14. data/lib/irb/cmd/fork.rb +39 -0
  15. data/lib/irb/cmd/help.rb +42 -0
  16. data/lib/irb/cmd/load.rb +67 -0
  17. data/lib/irb/cmd/nop.rb +39 -0
  18. data/lib/irb/cmd/pushws.rb +41 -0
  19. data/lib/irb/cmd/subirb.rb +43 -0
  20. data/lib/irb/completion.rb +244 -0
  21. data/lib/irb/context.rb +425 -0
  22. data/lib/irb/ext/change-ws.rb +46 -0
  23. data/lib/irb/ext/history.rb +119 -0
  24. data/lib/irb/ext/loader.rb +129 -0
  25. data/lib/irb/ext/multi-irb.rb +265 -0
  26. data/lib/irb/ext/save-history.rb +105 -0
  27. data/lib/irb/ext/tracer.rb +72 -0
  28. data/lib/irb/ext/use-loader.rb +74 -0
  29. data/lib/irb/ext/workspaces.rb +67 -0
  30. data/lib/irb/extend-command.rb +306 -0
  31. data/lib/irb/frame.rb +81 -0
  32. data/lib/irb/help.rb +37 -0
  33. data/lib/irb/init.rb +302 -0
  34. data/lib/irb/input-method.rb +192 -0
  35. data/lib/irb/inspector.rb +132 -0
  36. data/lib/irb/lc/.document +4 -0
  37. data/lib/irb/lc/error.rb +32 -0
  38. data/lib/irb/lc/help-message +49 -0
  39. data/lib/irb/lc/ja/encoding_aliases.rb +11 -0
  40. data/lib/irb/lc/ja/error.rb +31 -0
  41. data/lib/irb/lc/ja/help-message +52 -0
  42. data/lib/irb/locale.rb +182 -0
  43. data/lib/irb/magic-file.rb +38 -0
  44. data/lib/irb/notifier.rb +232 -0
  45. data/lib/irb/output-method.rb +92 -0
  46. data/lib/irb/ruby-lex.rb +1180 -0
  47. data/lib/irb/ruby-token.rb +267 -0
  48. data/lib/irb/slex.rb +282 -0
  49. data/lib/irb/src_encoding.rb +7 -0
  50. data/lib/irb/version.rb +17 -0
  51. data/lib/irb/workspace.rb +143 -0
  52. data/lib/irb/ws-for-case-2.rb +15 -0
  53. data/lib/irb/xmp.rb +170 -0
  54. metadata +125 -0
@@ -0,0 +1,425 @@
1
+ # frozen_string_literal: false
2
+ #
3
+ # irb/context.rb - irb context
4
+ # $Release Version: 0.9.6$
5
+ # $Revision$
6
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
+ #
8
+ # --
9
+ #
10
+ #
11
+ #
12
+ require_relative "workspace"
13
+ require_relative "inspector"
14
+ require_relative "input-method"
15
+ require_relative "output-method"
16
+
17
+ module IRB
18
+ # A class that wraps the current state of the irb session, including the
19
+ # configuration of IRB.conf.
20
+ class Context
21
+ # Creates a new IRB context.
22
+ #
23
+ # The optional +input_method+ argument:
24
+ #
25
+ # +nil+:: uses stdin or Readline
26
+ # +String+:: uses a File
27
+ # +other+:: uses this as InputMethod
28
+ def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
29
+ @irb = irb
30
+ if workspace
31
+ @workspace = workspace
32
+ else
33
+ @workspace = WorkSpace.new
34
+ end
35
+ @thread = Thread.current if defined? Thread
36
+
37
+ # copy of default configuration
38
+ @ap_name = IRB.conf[:AP_NAME]
39
+ @rc = IRB.conf[:RC]
40
+ @load_modules = IRB.conf[:LOAD_MODULES]
41
+
42
+ @use_readline = IRB.conf[:USE_READLINE]
43
+ @verbose = IRB.conf[:VERBOSE]
44
+ @io = nil
45
+
46
+ self.inspect_mode = IRB.conf[:INSPECT_MODE]
47
+ self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRACER]
48
+ self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER]
49
+ self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVAL_HISTORY]
50
+
51
+ @ignore_sigint = IRB.conf[:IGNORE_SIGINT]
52
+ @ignore_eof = IRB.conf[:IGNORE_EOF]
53
+
54
+ @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT]
55
+
56
+ self.prompt_mode = IRB.conf[:PROMPT_MODE]
57
+
58
+ if IRB.conf[:SINGLE_IRB] or !defined?(IRB::JobManager)
59
+ @irb_name = IRB.conf[:IRB_NAME]
60
+ else
61
+ @irb_name = IRB.conf[:IRB_NAME]+"#"+IRB.JobManager.n_jobs.to_s
62
+ end
63
+ @irb_path = "(" + @irb_name + ")"
64
+
65
+ case input_method
66
+ when nil
67
+ case use_readline?
68
+ when nil
69
+ if (defined?(ReadlineInputMethod) && STDIN.tty? &&
70
+ IRB.conf[:PROMPT_MODE] != :INF_RUBY)
71
+ @io = ReadlineInputMethod.new
72
+ else
73
+ @io = StdioInputMethod.new
74
+ end
75
+ when false
76
+ @io = StdioInputMethod.new
77
+ when true
78
+ if defined?(ReadlineInputMethod)
79
+ @io = ReadlineInputMethod.new
80
+ else
81
+ @io = StdioInputMethod.new
82
+ end
83
+ end
84
+
85
+ when String
86
+ @io = FileInputMethod.new(input_method)
87
+ @irb_name = File.basename(input_method)
88
+ @irb_path = input_method
89
+ else
90
+ @io = input_method
91
+ end
92
+ self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
93
+
94
+ if output_method
95
+ @output_method = output_method
96
+ else
97
+ @output_method = StdioOutputMethod.new
98
+ end
99
+
100
+ @echo = IRB.conf[:ECHO]
101
+ if @echo.nil?
102
+ @echo = true
103
+ end
104
+ self.debug_level = IRB.conf[:DEBUG_LEVEL]
105
+ end
106
+
107
+ # The top-level workspace, see WorkSpace#main
108
+ def main
109
+ @workspace.main
110
+ end
111
+
112
+ # The toplevel workspace, see #home_workspace
113
+ attr_reader :workspace_home
114
+ # WorkSpace in the current context
115
+ attr_accessor :workspace
116
+ # The current thread in this context
117
+ attr_reader :thread
118
+ # The current input method
119
+ #
120
+ # Can be either StdioInputMethod, ReadlineInputMethod, FileInputMethod or
121
+ # other specified when the context is created. See ::new for more
122
+ # information on +input_method+.
123
+ attr_accessor :io
124
+
125
+ # Current irb session
126
+ attr_accessor :irb
127
+ # A copy of the default <code>IRB.conf[:AP_NAME]</code>
128
+ attr_accessor :ap_name
129
+ # A copy of the default <code>IRB.conf[:RC]</code>
130
+ attr_accessor :rc
131
+ # A copy of the default <code>IRB.conf[:LOAD_MODULES]</code>
132
+ attr_accessor :load_modules
133
+ # Can be either name from <code>IRB.conf[:IRB_NAME]</code>, or the number of
134
+ # the current job set by JobManager, such as <code>irb#2</code>
135
+ attr_accessor :irb_name
136
+ # Can be either the #irb_name surrounded by parenthesis, or the
137
+ # +input_method+ passed to Context.new
138
+ attr_accessor :irb_path
139
+
140
+ # Whether +Readline+ is enabled or not.
141
+ #
142
+ # A copy of the default <code>IRB.conf[:USE_READLINE]</code>
143
+ #
144
+ # See #use_readline= for more information.
145
+ attr_reader :use_readline
146
+ # A copy of the default <code>IRB.conf[:INSPECT_MODE]</code>
147
+ attr_reader :inspect_mode
148
+
149
+ # A copy of the default <code>IRB.conf[:PROMPT_MODE]</code>
150
+ attr_reader :prompt_mode
151
+ # Standard IRB prompt
152
+ #
153
+ # See IRB@Customizing+the+IRB+Prompt for more information.
154
+ attr_accessor :prompt_i
155
+ # IRB prompt for continuated strings
156
+ #
157
+ # See IRB@Customizing+the+IRB+Prompt for more information.
158
+ attr_accessor :prompt_s
159
+ # IRB prompt for continuated statement (e.g. immediately after an +if+)
160
+ #
161
+ # See IRB@Customizing+the+IRB+Prompt for more information.
162
+ attr_accessor :prompt_c
163
+ # See IRB@Customizing+the+IRB+Prompt for more information.
164
+ attr_accessor :prompt_n
165
+ # Can be either the default <code>IRB.conf[:AUTO_INDENT]</code>, or the
166
+ # mode set by #prompt_mode=
167
+ #
168
+ # To enable auto-indentation in irb:
169
+ #
170
+ # IRB.conf[:AUTO_INDENT] = true
171
+ #
172
+ # or
173
+ #
174
+ # irb_context.auto_indent_mode = true
175
+ #
176
+ # or
177
+ #
178
+ # IRB.CurrentContext.auto_indent_mode = true
179
+ #
180
+ # See IRB@Configuration for more information.
181
+ attr_accessor :auto_indent_mode
182
+ # The format of the return statement, set by #prompt_mode= using the
183
+ # +:RETURN+ of the +mode+ passed to set the current #prompt_mode.
184
+ attr_accessor :return_format
185
+
186
+ # Whether <code>^C</code> (+control-c+) will be ignored or not.
187
+ #
188
+ # If set to +false+, <code>^C</code> will quit irb.
189
+ #
190
+ # If set to +true+,
191
+ #
192
+ # * during input: cancel input then return to top level.
193
+ # * during execute: abandon current execution.
194
+ attr_accessor :ignore_sigint
195
+ # Whether <code>^D</code> (+control-d+) will be ignored or not.
196
+ #
197
+ # If set to +false+, <code>^D</code> will quit irb.
198
+ attr_accessor :ignore_eof
199
+ # Whether to echo the return value to output or not.
200
+ #
201
+ # Uses IRB.conf[:ECHO] if available, or defaults to +true+.
202
+ #
203
+ # puts "hello"
204
+ # # hello
205
+ # #=> nil
206
+ # IRB.CurrentContext.echo = false
207
+ # puts "omg"
208
+ # # omg
209
+ attr_accessor :echo
210
+ # Whether verbose messages are displayed or not.
211
+ #
212
+ # A copy of the default <code>IRB.conf[:VERBOSE]</code>
213
+ attr_accessor :verbose
214
+ # The debug level of irb
215
+ #
216
+ # See #debug_level= for more information.
217
+ attr_reader :debug_level
218
+
219
+ # The limit of backtrace lines displayed as top +n+ and tail +n+.
220
+ #
221
+ # The default value is 16.
222
+ #
223
+ # Can also be set using the +--back-trace-limit+ command line option.
224
+ #
225
+ # See IRB@Command+line+options for more command line options.
226
+ attr_accessor :back_trace_limit
227
+
228
+ # Alias for #use_readline
229
+ alias use_readline? use_readline
230
+ # Alias for #rc
231
+ alias rc? rc
232
+ alias ignore_sigint? ignore_sigint
233
+ alias ignore_eof? ignore_eof
234
+ alias echo? echo
235
+
236
+ # Returns whether messages are displayed or not.
237
+ def verbose?
238
+ if @verbose.nil?
239
+ if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)
240
+ false
241
+ elsif !STDIN.tty? or @io.kind_of?(FileInputMethod)
242
+ true
243
+ else
244
+ false
245
+ end
246
+ else
247
+ @verbose
248
+ end
249
+ end
250
+
251
+ # Whether #verbose? is +true+, and +input_method+ is either
252
+ # StdioInputMethod or ReadlineInputMethod, see #io for more information.
253
+ def prompting?
254
+ verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) ||
255
+ (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod)))
256
+ end
257
+
258
+ # The return value of the last statement evaluated.
259
+ attr_reader :last_value
260
+
261
+ # Sets the return value from the last statement evaluated in this context
262
+ # to #last_value.
263
+ def set_last_value(value)
264
+ @last_value = value
265
+ @workspace.local_variable_set :_, value
266
+ end
267
+
268
+ # Sets the +mode+ of the prompt in this context.
269
+ #
270
+ # See IRB@Customizing+the+IRB+Prompt for more information.
271
+ def prompt_mode=(mode)
272
+ @prompt_mode = mode
273
+ pconf = IRB.conf[:PROMPT][mode]
274
+ @prompt_i = pconf[:PROMPT_I]
275
+ @prompt_s = pconf[:PROMPT_S]
276
+ @prompt_c = pconf[:PROMPT_C]
277
+ @prompt_n = pconf[:PROMPT_N]
278
+ @return_format = pconf[:RETURN]
279
+ if ai = pconf.include?(:AUTO_INDENT)
280
+ @auto_indent_mode = ai
281
+ else
282
+ @auto_indent_mode = IRB.conf[:AUTO_INDENT]
283
+ end
284
+ end
285
+
286
+ # Whether #inspect_mode is set or not, see #inspect_mode= for more detail.
287
+ def inspect?
288
+ @inspect_mode.nil? or @inspect_mode
289
+ end
290
+
291
+ # Whether #io uses a File for the +input_method+ passed when creating the
292
+ # current context, see ::new
293
+ def file_input?
294
+ @io.class == FileInputMethod
295
+ end
296
+
297
+ # Specifies the inspect mode with +opt+:
298
+ #
299
+ # +true+:: display +inspect+
300
+ # +false+:: display +to_s+
301
+ # +nil+:: inspect mode in non-math mode,
302
+ # non-inspect mode in math mode
303
+ #
304
+ # See IRB::Inspector for more information.
305
+ #
306
+ # Can also be set using the +--inspect+ and +--noinspect+ command line
307
+ # options.
308
+ #
309
+ # See IRB@Command+line+options for more command line options.
310
+ def inspect_mode=(opt)
311
+
312
+ if i = Inspector::INSPECTORS[opt]
313
+ @inspect_mode = opt
314
+ @inspect_method = i
315
+ i.init
316
+ else
317
+ case opt
318
+ when nil
319
+ if Inspector.keys_with_inspector(Inspector::INSPECTORS[true]).include?(@inspect_mode)
320
+ self.inspect_mode = false
321
+ elsif Inspector.keys_with_inspector(Inspector::INSPECTORS[false]).include?(@inspect_mode)
322
+ self.inspect_mode = true
323
+ else
324
+ puts "Can't switch inspect mode."
325
+ return
326
+ end
327
+ when /^\s*\{.*\}\s*$/
328
+ begin
329
+ inspector = eval "proc#{opt}"
330
+ rescue Exception
331
+ puts "Can't switch inspect mode(#{opt})."
332
+ return
333
+ end
334
+ self.inspect_mode = inspector
335
+ when Proc
336
+ self.inspect_mode = IRB::Inspector(opt)
337
+ when Inspector
338
+ prefix = "usr%d"
339
+ i = 1
340
+ while Inspector::INSPECTORS[format(prefix, i)]; i += 1; end
341
+ @inspect_mode = format(prefix, i)
342
+ @inspect_method = opt
343
+ Inspector.def_inspector(format(prefix, i), @inspect_method)
344
+ else
345
+ puts "Can't switch inspect mode(#{opt})."
346
+ return
347
+ end
348
+ end
349
+ print "Switch to#{unless @inspect_mode; ' non';end} inspect mode.\n" if verbose?
350
+ @inspect_mode
351
+ end
352
+
353
+ # Obsolete method.
354
+ #
355
+ # Can be set using the +--noreadline+ and +--readline+ command line
356
+ # options.
357
+ #
358
+ # See IRB@Command+line+options for more command line options.
359
+ def use_readline=(opt)
360
+ print "This method is obsolete."
361
+ print "Do nothing."
362
+ end
363
+
364
+ # Sets the debug level of irb
365
+ #
366
+ # Can also be set using the +--irb_debug+ command line option.
367
+ #
368
+ # See IRB@Command+line+options for more command line options.
369
+ def debug_level=(value)
370
+ @debug_level = value
371
+ RubyLex.debug_level = value
372
+ end
373
+
374
+ # Whether or not debug mode is enabled, see #debug_level=.
375
+ def debug?
376
+ @debug_level > 0
377
+ end
378
+
379
+ def evaluate(line, line_no, exception: nil) # :nodoc:
380
+ @line_no = line_no
381
+ if exception
382
+ line = "begin ::Kernel.raise _; rescue _.class; #{line}; end"
383
+ @workspace.local_variable_set(:_, exception)
384
+ end
385
+ set_last_value(@workspace.evaluate(self, line, irb_path, line_no))
386
+ end
387
+
388
+ def inspect_last_value # :nodoc:
389
+ @inspect_method.inspect_value(@last_value)
390
+ end
391
+
392
+ alias __exit__ exit
393
+ # Exits the current session, see IRB.irb_exit
394
+ def exit(ret = 0)
395
+ IRB.irb_exit(@irb, ret)
396
+ end
397
+
398
+ NOPRINTING_IVARS = ["@last_value"] # :nodoc:
399
+ NO_INSPECTING_IVARS = ["@irb", "@io"] # :nodoc:
400
+ IDNAME_IVARS = ["@prompt_mode"] # :nodoc:
401
+
402
+ alias __inspect__ inspect
403
+ def inspect # :nodoc:
404
+ array = []
405
+ for ivar in instance_variables.sort{|e1, e2| e1 <=> e2}
406
+ ivar = ivar.to_s
407
+ name = ivar.sub(/^@(.*)$/, '\1')
408
+ val = instance_eval(ivar)
409
+ case ivar
410
+ when *NOPRINTING_IVARS
411
+ array.push format("conf.%s=%s", name, "...")
412
+ when *NO_INSPECTING_IVARS
413
+ array.push format("conf.%s=%s", name, val.to_s)
414
+ when *IDNAME_IVARS
415
+ array.push format("conf.%s=:%s", name, val.id2name)
416
+ else
417
+ array.push format("conf.%s=%s", name, val.inspect)
418
+ end
419
+ end
420
+ array.join("\n")
421
+ end
422
+ alias __to_s__ to_s
423
+ alias to_s inspect
424
+ end
425
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: false
2
+ #
3
+ # irb/ext/cb.rb -
4
+ # $Release Version: 0.9.6$
5
+ # $Revision$
6
+ # by Keiju ISHITSUKA(keiju@ruby-lang.org)
7
+ #
8
+ # --
9
+ #
10
+ #
11
+ #
12
+
13
+ module IRB # :nodoc:
14
+ class Context
15
+
16
+ # Inherited from +TOPLEVEL_BINDING+.
17
+ def home_workspace
18
+ if defined? @home_workspace
19
+ @home_workspace
20
+ else
21
+ @home_workspace = @workspace
22
+ end
23
+ end
24
+
25
+ # Changes the current workspace to given object or binding.
26
+ #
27
+ # If the optional argument is omitted, the workspace will be
28
+ # #home_workspace which is inherited from +TOPLEVEL_BINDING+ or the main
29
+ # object, <code>IRB.conf[:MAIN_CONTEXT]</code> when irb was initialized.
30
+ #
31
+ # See IRB::WorkSpace.new for more information.
32
+ def change_workspace(*_main)
33
+ if _main.empty?
34
+ @workspace = home_workspace
35
+ return main
36
+ end
37
+
38
+ @workspace = WorkSpace.new(_main[0])
39
+
40
+ if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
41
+ main.extend ExtendCommandBundle
42
+ end
43
+ end
44
+ end
45
+ end
46
+