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