| Class | IRB::Irb |
| In: |
lib/irb/ext/multi-irb.rb
lib/irb.rb |
| Parent: | Object |
irb interpriter main routine
| context | [R] | |
| scanner | [RW] |
# File lib/irb.rb, line 99
99: def initialize(workspace = nil, input_method = nil, output_method = nil)
100: @context = Context.new(self, workspace, input_method, output_method)
101: @context.main.extend ExtendCommandBundle
102: @signal_status = :IN_IRB
103:
104: @scanner = RubyLex.new
105: @scanner.exception_on_syntax_error = false
106: end
# File lib/irb.rb, line 110
110: def eval_input
111: @scanner.set_prompt do
112: |ltype, indent, continue, line_no|
113: if ltype
114: f = @context.prompt_s
115: elsif continue
116: f = @context.prompt_c
117: elsif indent > 0
118: f = @context.prompt_n
119: else @context.prompt_i
120: f = @context.prompt_i
121: end
122: f = "" unless f
123: if @context.prompting?
124: @context.io.prompt = p = prompt(f, ltype, indent, line_no)
125: else
126: @context.io.prompt = p = ""
127: end
128: if @context.auto_indent_mode
129: unless ltype
130: ind = prompt(@context.prompt_i, ltype, indent, line_no)[/.*\z/].size +
131: indent * 2 - p.size
132: ind += 2 if continue
133: @context.io.prompt = p + " " * ind if ind > 0
134: end
135: end
136: end
137:
138: @scanner.set_input(@context.io) do
139: signal_status(:IN_INPUT) do
140: if l = @context.io.gets
141: print l if @context.verbose?
142: else
143: if @context.ignore_eof? and @context.io.readable_atfer_eof?
144: l = "\n"
145: if @context.verbose?
146: printf "Use \"exit\" to leave %s\n", @context.ap_name
147: end
148: end
149: end
150: l
151: end
152: end
153:
154: @scanner.each_top_level_statement do |line, line_no|
155: signal_status(:IN_EVAL) do
156: begin
157: line.untaint
158: @context.evaluate(line, line_no)
159: output_value if @context.echo?
160: exc = nil
161: rescue Interrupt => exc
162: rescue SystemExit, SignalException
163: raise
164: rescue Exception => exc
165: end
166: if exc
167: print exc.class, ": ", exc, "\n"
168: if exc.backtrace[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && exc.class.to_s !~ /^IRB/
169: irb_bug = true
170: else
171: irb_bug = false
172: end
173:
174: messages = []
175: lasts = []
176: levels = 0
177: for m in exc.backtrace
178: m = @context.workspace.filter_backtrace(m) unless irb_bug
179: if m
180: if messages.size < @context.back_trace_limit
181: messages.push "\tfrom "+m
182: else
183: lasts.push "\tfrom "+m
184: if lasts.size > @context.back_trace_limit
185: lasts.shift
186: levels += 1
187: end
188: end
189: end
190: end
191: print messages.join("\n"), "\n"
192: unless lasts.empty?
193: printf "... %d levels...\n", levels if levels > 0
194: print lasts.join("\n")
195: end
196: print "Maybe IRB bug!!\n" if irb_bug
197: end
198: if $SAFE > 2
199: abort "Error: irb does not work for $SAFE level higher than 2"
200: end
201: end
202: end
203: end
# File lib/irb.rb, line 316
316: def inspect
317: ary = []
318: for iv in instance_variables
319: case iv
320: when "@signal_status"
321: ary.push format("%s=:%s", iv, @signal_status.id2name)
322: when "@context"
323: ary.push format("%s=%s", iv, eval(iv).__to_s__)
324: else
325: ary.push format("%s=%s", iv, eval(iv))
326: end
327: end
328: format("#<%s: %s>", self.class, ary.join(", "))
329: end
# File lib/irb.rb, line 308
308: def output_value
309: if @context.inspect?
310: printf @context.return_format, @context.last_value.inspect
311: else
312: printf @context.return_format, @context.last_value
313: end
314: end
# File lib/irb.rb, line 277
277: def prompt(prompt, ltype, indent, line_no)
278: p = prompt.dup
279: p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
280: case $2
281: when "N"
282: @context.irb_name
283: when "m"
284: @context.main.to_s
285: when "M"
286: @context.main.inspect
287: when "l"
288: ltype
289: when "i"
290: if $1
291: format("%" + $1 + "d", indent)
292: else
293: indent.to_s
294: end
295: when "n"
296: if $1
297: format("%" + $1 + "d", line_no)
298: else
299: line_no.to_s
300: end
301: when "%"
302: "%"
303: end
304: end
305: p
306: end
# File lib/irb.rb, line 244
244: def signal_handle
245: unless @context.ignore_sigint?
246: print "\nabort!!\n" if @context.verbose?
247: exit
248: end
249:
250: case @signal_status
251: when :IN_INPUT
252: print "^C\n"
253: raise RubyLex::TerminateLineInput
254: when :IN_EVAL
255: IRB.irb_abort(self)
256: when :IN_LOAD
257: IRB.irb_abort(self, LoadAbort)
258: when :IN_IRB
259: # ignore
260: else
261: # ignore other cases as well
262: end
263: end
# File lib/irb/ext/multi-irb.rb, line 214
214: def signal_handle
215: unless @context.ignore_sigint?
216: print "\nabort!!\n" if @context.verbose?
217: exit
218: end
219:
220: case @signal_status
221: when :IN_INPUT
222: print "^C\n"
223: IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput
224: when :IN_EVAL
225: IRB.irb_abort(self)
226: when :IN_LOAD
227: IRB.irb_abort(self, LoadAbort)
228: when :IN_IRB
229: # ignore
230: else
231: # ignore other cases as well
232: end
233: end
# File lib/irb.rb, line 265
265: def signal_status(status)
266: return yield if @signal_status == :IN_LOAD
267:
268: signal_status_back = @signal_status
269: @signal_status = status
270: begin
271: yield
272: ensure
273: @signal_status = signal_status_back
274: end
275: end
# File lib/irb.rb, line 235
235: def suspend_context(context)
236: @context, back_context = context, @context
237: begin
238: yield back_context
239: ensure
240: @context = back_context
241: end
242: end
# File lib/irb.rb, line 225
225: def suspend_input_method(input_method)
226: back_io = @context.io
227: @context.instance_eval{@io = input_method}
228: begin
229: yield back_io
230: ensure
231: @context.instance_eval{@io = back_io}
232: end
233: end
# File lib/irb.rb, line 205
205: def suspend_name(path = nil, name = nil)
206: @context.irb_path, back_path = path, @context.irb_path if path
207: @context.irb_name, back_name = name, @context.irb_name if name
208: begin
209: yield back_path, back_name
210: ensure
211: @context.irb_path = back_path if path
212: @context.irb_name = back_name if name
213: end
214: end