Class IRB::Irb
In: lib/irb/ext/multi-irb.rb
lib/irb.rb
Parent: Object

irb interpriter main routine

Methods

Attributes

context  [R] 
scanner  [RW] 

Public Class methods

[Source]

    # 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

Public Instance methods

[Source]

     # 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:           rescue StandardError, ScriptError, Abort
153:             $! = RuntimeError.new("unknown exception raised") unless $!
154:             print $!.class, ": ", $!, "\n"
155:             if  $@[0] =~ /irb(2)?(\/.*|-.*|\.rb)?:/ && $!.class.to_s !~ /^IRB/
156:               irb_bug = true 
157:             else
158:               irb_bug = false
159:             end
160:             
161:             messages = []
162:             lasts = []
163:             levels = 0
164:             for m in $@
165:               m = @context.workspace.filter_backtrace(m) unless irb_bug
166:               if m
167:                 if messages.size < @context.back_trace_limit
168:                   messages.push "\tfrom "+m
169:                 else
170:                   lasts.push "\tfrom "+m
171:                   if lasts.size > @context.back_trace_limit
172:                     lasts.shift 
173:                     levels += 1
174:                   end
175:                 end
176:               end
177:             end
178:             print messages.join("\n"), "\n"
179:             unless lasts.empty?
180:               printf "... %d levels...\n", levels if levels > 0
181:               print lasts.join("\n")
182:             end
183:             print "Maybe IRB bug!!\n" if irb_bug
184:           end
185:           if $SAFE > 2
186:             warn "Error: irb does not work for $SAFE level higher than 2"
187:             exit 1
188:           end
189:         end
190:       end
191:     end

[Source]

     # File lib/irb.rb, line 304
304:     def inspect
305:       ary = []
306:       for iv in instance_variables
307:         case iv
308:         when "@signal_status"
309:           ary.push format("%s=:%s", iv, @signal_status.id2name)
310:         when "@context"
311:           ary.push format("%s=%s", iv, eval(iv).__to_s__)
312:         else
313:           ary.push format("%s=%s", iv, eval(iv))
314:         end
315:       end
316:       format("#<%s: %s>", self.class, ary.join(", "))
317:     end

[Source]

     # File lib/irb.rb, line 296
296:     def output_value
297:       if @context.inspect?
298:         printf @context.return_format, @context.last_value.inspect
299:       else
300:         printf @context.return_format, @context.last_value
301:       end
302:     end

[Source]

     # File lib/irb.rb, line 265
265:     def prompt(prompt, ltype, indent, line_no)
266:       p = prompt.dup
267:       p.gsub!(/%([0-9]+)?([a-zA-Z])/) do
268:         case $2
269:         when "N"
270:           @context.irb_name
271:         when "m"
272:           @context.main.to_s
273:         when "M"
274:           @context.main.inspect
275:         when "l"
276:           ltype
277:         when "i"
278:           if $1 
279:             format("%" + $1 + "d", indent)
280:           else
281:             indent.to_s
282:           end
283:         when "n"
284:           if $1 
285:             format("%" + $1 + "d", line_no)
286:           else
287:             line_no.to_s
288:           end
289:         when "%"
290:           "%"
291:         end
292:       end
293:       p
294:     end

[Source]

     # File lib/irb.rb, line 232
232:     def signal_handle
233:       unless @context.ignore_sigint?
234:         print "\nabort!!\n" if @context.verbose?
235:         exit
236:       end
237: 
238:       case @signal_status
239:       when :IN_INPUT
240:         print "^C\n"
241:         raise RubyLex::TerminateLineInput
242:       when :IN_EVAL
243:         IRB.irb_abort(self)
244:       when :IN_LOAD
245:         IRB.irb_abort(self, LoadAbort)
246:       when :IN_IRB
247:         # ignore
248:       else
249:         # ignore other cases as well
250:       end
251:     end

[Source]

     # 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

[Source]

     # File lib/irb.rb, line 253
253:     def signal_status(status)
254:       return yield if @signal_status == :IN_LOAD
255: 
256:       signal_status_back = @signal_status
257:       @signal_status = status
258:       begin
259:         yield
260:       ensure
261:         @signal_status = signal_status_back
262:       end
263:     end

[Source]

     # File lib/irb.rb, line 223
223:     def suspend_context(context)
224:       @context, back_context = context, @context
225:       begin
226:         yield back_context
227:       ensure
228:         @context = back_context
229:       end
230:     end

[Source]

     # File lib/irb.rb, line 213
213:     def suspend_input_method(input_method)
214:       back_io = @context.io
215:       @context.instance_eval{@io = input_method}
216:       begin
217:         yield back_io
218:       ensure
219:         @context.instance_eval{@io = back_io}
220:       end
221:     end

[Source]

     # File lib/irb.rb, line 193
193:     def suspend_name(path = nil, name = nil)
194:       @context.irb_path, back_path = path, @context.irb_path if path
195:       @context.irb_name, back_name = name, @context.irb_name if name
196:       begin
197:         yield back_path, back_name
198:       ensure
199:         @context.irb_path = back_path if path
200:         @context.irb_name = back_name if name
201:       end
202:     end

[Source]

     # File lib/irb.rb, line 204
204:     def suspend_workspace(workspace)
205:       @context.workspace, back_workspace = workspace, @context.workspace
206:       begin
207:         yield back_workspace
208:       ensure
209:         @context.workspace = back_workspace
210:       end
211:     end

[Validate]