2 # irb.rb - irb main module
3 # $Release Version: 0.9.5 $
5 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
15 require "irb/extend-command"
16 #require "irb/workspace"
18 require "irb/ruby-lex"
19 require "irb/input-method"
27 class Abort < Exception;end
38 if v = @CONF[:VERSION] then return v end
41 rv = @RELEASE_VERSION.sub(/\.0/, "")
42 @CONF[:VERSION] = format("irb %s(%s)", rv, @LAST_UPDATE_DATE)
45 def IRB.CurrentContext
46 IRB.conf[:MAIN_CONTEXT]
49 # initialize IRB and start TOP_LEVEL irb
50 def IRB.start(ap_path = nil)
51 $0 = File::basename(ap_path, ".rb") if ap_path
56 irb = Irb.new(nil, @CONF[:SCRIPT])
61 @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
62 @CONF[:MAIN_CONTEXT] = irb.context
74 def IRB.irb_exit(irb, ret)
78 def IRB.irb_abort(irb, exception = Abort)
80 irb.context.thread.raise exception, "abort then interrupt!!"
82 raise exception, "abort then interrupt!!"
87 # irb interpreter main routine
90 def initialize(workspace = nil, input_method = nil, output_method = nil)
91 @context = Context.new(self, workspace, input_method, output_method)
92 @context.main.extend ExtendCommandBundle
93 @signal_status = :IN_IRB
95 @scanner = RubyLex.new
96 @scanner.exception_on_syntax_error = false
99 attr_accessor :scanner
102 @scanner.set_prompt do
103 |ltype, indent, continue, line_no|
105 f = @context.prompt_s
107 f = @context.prompt_c
109 f = @context.prompt_n
110 else @context.prompt_i
111 f = @context.prompt_i
114 if @context.prompting?
115 @context.io.prompt = p = prompt(f, ltype, indent, line_no)
117 @context.io.prompt = p = ""
119 if @context.auto_indent_mode
121 ind = prompt(@context.prompt_i, ltype, indent, line_no)[/.*\z/].size +
124 @context.io.prompt = p + " " * ind if ind > 0
129 @scanner.set_input(@context.io) do
130 signal_status(:IN_INPUT) do
131 if l = @context.io.gets
132 print l if @context.verbose?
134 if @context.ignore_eof? and @context.io.readable_atfer_eof?
137 printf "Use \"exit\" to leave %s\n", @context.ap_name
145 @scanner.each_top_level_statement do |line, line_no|
146 signal_status(:IN_EVAL) do
149 @context.evaluate(line, line_no)
150 output_value if @context.echo?
152 rescue Interrupt => exc
153 rescue SystemExit, SignalException
155 rescue Exception => exc
158 print exc.class, ": ", exc, "\n"
159 if exc.backtrace[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/ &&
160 !(SyntaxError === exc)
169 for m in exc.backtrace
170 m = @context.workspace.filter_backtrace(m) unless irb_bug
172 if messages.size < @context.back_trace_limit
173 messages.push "\tfrom "+m
175 lasts.push "\tfrom "+m
176 if lasts.size > @context.back_trace_limit
183 print messages.join("\n"), "\n"
185 printf "... %d levels...\n", levels if levels > 0
186 print lasts.join("\n")
188 print "Maybe IRB bug!!\n" if irb_bug
191 abort "Error: irb does not work for $SAFE level higher than 2"
197 def suspend_name(path = nil, name = nil)
198 @context.irb_path, back_path = path, @context.irb_path if path
199 @context.irb_name, back_name = name, @context.irb_name if name
201 yield back_path, back_name
203 @context.irb_path = back_path if path
204 @context.irb_name = back_name if name
208 def suspend_workspace(workspace)
209 @context.workspace, back_workspace = workspace, @context.workspace
213 @context.workspace = back_workspace
217 def suspend_input_method(input_method)
218 back_io = @context.io
219 @context.instance_eval{@io = input_method}
223 @context.instance_eval{@io = back_io}
227 def suspend_context(context)
228 @context, back_context = context, @context
232 @context = back_context
237 unless @context.ignore_sigint?
238 print "\nabort!!\n" if @context.verbose?
245 raise RubyLex::TerminateLineInput
249 IRB.irb_abort(self, LoadAbort)
253 # ignore other cases as well
257 def signal_status(status)
258 return yield if @signal_status == :IN_LOAD
260 signal_status_back = @signal_status
261 @signal_status = status
265 @signal_status = signal_status_back
269 def prompt(prompt, ltype, indent, line_no)
271 p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
278 @context.main.inspect
283 format("%" + $1 + "d", indent)
289 format("%" + $1 + "d", line_no)
302 printf @context.return_format, @context.last_value.inspect
304 printf @context.return_format, @context.last_value
310 for iv in instance_variables
312 when "@signal_status"
313 ary.push format("%s=:%s", iv, @signal_status.id2name)
315 ary.push format("%s=%s", iv, eval(iv).__to_s__)
317 ary.push format("%s=%s", iv, eval(iv))
320 format("#<%s: %s>", self.class, ary.join(", "))
326 IRB.version unless self[:VERSION]
329 for k, v in sort{|a1, a2| a1[0].id2name <=> a2[0].id2name}
331 when :MAIN_CONTEXT, :__TMP__EHV__
332 array.push format("CONF[:%s]=...myself...", k.id2name)
336 ss = vv.collect{|kkk, vvv| ":#{kkk.id2name}=>#{vvv.inspect}"}
337 format(":%s=>{%s}", kk.id2name, ss.join(", "))
339 array.push format("CONF[:%s]={%s}", k.id2name, s.join(", "))
341 array.push format("CONF[:%s]=%s", k.id2name, v.inspect)