From af064b04b1622897995fe1177aabfb60db90e6f7 Mon Sep 17 00:00:00 2001 From: keiju Date: Tue, 9 Jul 2002 11:17:17 +0000 Subject: * irb 0.9 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 4 + lib/irb.rb | 136 ++++++++++++++----------- lib/irb/cmd/chws.rb | 33 ++++++ lib/irb/cmd/fork.rb | 25 +++++ lib/irb/cmd/load.rb | 67 +++++++++++++ lib/irb/cmd/nop.rb | 39 ++++++++ lib/irb/cmd/pushws.rb | 39 ++++++++ lib/irb/cmd/subirb.rb | 43 ++++++++ lib/irb/completion.rb | 21 +++- lib/irb/context.rb | 164 ++++++++++-------------------- lib/irb/ext/change-ws.rb | 62 ++++++++++++ lib/irb/ext/history.rb | 110 ++++++++++++++++++++ lib/irb/ext/loader.rb | 106 ++++++++++++++++++++ lib/irb/ext/math-mode.rb | 37 +++++++ lib/irb/ext/multi-irb.rb | 241 ++++++++++++++++++++++++++++++++++++++++++++ lib/irb/ext/tracer.rb | 61 ++++++++++++ lib/irb/ext/use-loader.rb | 65 ++++++++++++ lib/irb/ext/workspaces.rb | 56 +++++++++++ lib/irb/extend-command.rb | 243 +++++++++++++++++++++++++++++++-------------- lib/irb/frame.rb | 2 +- lib/irb/help.rb | 2 +- lib/irb/init.rb | 58 ++++------- lib/irb/input-method.rb | 6 +- lib/irb/lc/error.rb | 2 +- lib/irb/lc/help-message | 2 +- lib/irb/lc/ja/error.rb | 2 +- lib/irb/lc/ja/help-message | 2 +- lib/irb/loader.rb | 118 ---------------------- lib/irb/locale.rb | 3 +- lib/irb/multi-irb.rb | 232 ------------------------------------------- lib/irb/ruby-lex.rb | 161 ++++++++++++++++++++++++------ lib/irb/ruby-token.rb | 3 +- lib/irb/slex.rb | 5 +- lib/irb/version.rb | 6 +- lib/irb/workspace.rb | 17 ++-- lib/irb/ws-for-case-2.rb | 2 +- lib/irb/xmp.rb | 12 ++- 37 files changed, 1488 insertions(+), 699 deletions(-) create mode 100644 lib/irb/cmd/chws.rb create mode 100644 lib/irb/cmd/fork.rb create mode 100644 lib/irb/cmd/load.rb create mode 100644 lib/irb/cmd/nop.rb create mode 100644 lib/irb/cmd/pushws.rb create mode 100644 lib/irb/cmd/subirb.rb create mode 100644 lib/irb/ext/change-ws.rb create mode 100644 lib/irb/ext/history.rb create mode 100644 lib/irb/ext/loader.rb create mode 100644 lib/irb/ext/math-mode.rb create mode 100644 lib/irb/ext/multi-irb.rb create mode 100644 lib/irb/ext/tracer.rb create mode 100644 lib/irb/ext/use-loader.rb create mode 100644 lib/irb/ext/workspaces.rb delete mode 100644 lib/irb/loader.rb delete mode 100644 lib/irb/multi-irb.rb diff --git a/ChangeLog b/ChangeLog index 4a6bffcb2c..806c27b054 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +Tue Jul 9 20:03:55 2002 Keiju Ishitsuka + + * irb 0.9 + Fri Jul 5 08:59:15 2002 Michal Rokos * enum.c: Fix bug in enum_sort_by and some code indents diff --git a/lib/irb.rb b/lib/irb.rb index 1b8444b5b3..b5f12a66f8 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -1,6 +1,6 @@ # # irb.rb - irb main module -# $Release Version: 0.7.4 $ +# $Release Version: 0.9 $ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -14,7 +14,7 @@ require "e2mmap" require "irb/init" require "irb/context" require "irb/extend-command" -require "irb/workspace" +#require "irb/workspace" require "irb/ruby-lex" require "irb/input-method" @@ -43,13 +43,15 @@ module IRB @CONF[:VERSION] = format("irb %s(%s)", rv, @LAST_UPDATE_DATE) end + def IRB.CurrentContext + IRB.conf[:MAIN_CONTEXT] + end + # initialize IRB and start TOP_LEVEL irb def IRB.start(ap_path = nil) $0 = File::basename(ap_path, ".rb") if ap_path IRB.initialize(ap_path) - IRB.parse_opts - IRB.load_modules if @CONF[:SCRIPT] irb = Irb.new(nil, @CONF[:SCRIPT]) @@ -67,7 +69,7 @@ module IRB catch(:IRB_EXIT) do irb.eval_input end - print "\n" +# print "\n" end def IRB.irb_exit(irb, ret) @@ -88,7 +90,7 @@ module IRB class Irb def initialize(workspace = nil, input_method = nil) @context = Context.new(self, workspace, input_method) - @context.main.extend ExtendCommand + @context.main.extend ExtendCommandBundle @signal_status = :IN_IRB @scanner = RubyLex.new @@ -98,20 +100,6 @@ module IRB attr_accessor :scanner def eval_input - @scanner.set_input(@context.io) do - signal_status(:IN_INPUT) do - unless l = @context.io.gets - if @context.ignore_eof? and @context.io.readable_atfer_eof? - l = "\n" - if @context.verbose? - printf "Use \"exit\" to leave %s\n", @context.ap_name - end - end - end - l - end - end - @scanner.set_prompt do |ltype, indent, continue, line_no| if ltype @@ -122,7 +110,11 @@ module IRB f = @context.prompt_i end f = "" unless f - @context.io.prompt = p = prompt(f, ltype, indent, line_no) + if @context.prompting? + @context.io.prompt = p = prompt(f, ltype, indent, line_no) + else + @context.io.prompt = p = "" + end if @context.auto_indent_mode unless ltype ind = prompt(@context.prompt_i, ltype, indent, line_no).size + @@ -133,22 +125,28 @@ module IRB end end + @scanner.set_input(@context.io) do + signal_status(:IN_INPUT) do + if l = @context.io.gets + print l if @context.verbose? + else + if @context.ignore_eof? and @context.io.readable_atfer_eof? + l = "\n" + if @context.verbose? + printf "Use \"exit\" to leave %s\n", @context.ap_name + end + end + end + l + end + end + @scanner.each_top_level_statement do |line, line_no| signal_status(:IN_EVAL) do begin - trace_in do - @context._ = @context.workspace.evaluate(line, - @context.irb_path, - line_no) -# @context._ = irb_eval(line, @context.bind, @context.irb_path, line_no) - end - - if @context.inspect? - printf @context.return_format, @context._.inspect - else - printf @context.return_format, @context._ - end + @context.evaluate(line, line_no) + output_value if @context.echo? rescue StandardError, ScriptError, Abort $! = RuntimeError.new("unknown exception raised") unless $! print $!.type, ": ", $!, "\n" @@ -186,19 +184,44 @@ module IRB end end -# def irb_eval(line, bind, path, line_no) -# id, str = catch(:IRB_TOPLEVEL_EVAL){ -# return eval(line, bind, path, line_no) -# } -# case id -# when :EVAL_TOPLEVEL -# eval(str, bind, "(irb_internal)", 1) -# when :EVAL_CONTEXT -# @context.instance_eval(str) -# else -# IRB.fail IllegalParameter -# end -# end + def suspend_name(path = nil, name = nil) + @context.irb_path, back_path = path, @context.irb_path if path + @context.irb_name, back_name = name, @context.irb_name if name + begin + yield back_path, back_name + ensure + @context.irb_path = back_path if path + @context.irb_name = back_name if name + end + end + + def suspend_workspace(workspace) + @context.workspace, back_workspace = workspace, @context.workspace + begin + yield back_workspace + ensure + @context.workspace = back_workspace + end + end + + def suspend_input_method(input_method) + back_io = @context.io + @context.instance_eval{@io = input_method} + begin + yield back_io + ensure + @context.instance_eval{@io = back_io} + end + end + + def suspend_context(context) + @context, back_context = context, @context + begin + yield back_context + ensure + @context = back_context + end + end def signal_handle unless @context.ignore_sigint? @@ -233,15 +256,6 @@ module IRB end end - def trace_in - Tracer.on if @context.use_tracer? - begin - yield - ensure - Tracer.off if @context.use_tracer? - end - end - def prompt(prompt, ltype, indent, line_no) p = prompt.dup p.gsub!(/%([0-9]+)?([a-zA-Z])/) do @@ -273,6 +287,14 @@ module IRB p end + def output_value + if @context.inspect? + printf @context.return_format, @context.last_value.inspect + else + printf @context.return_format, @context.last_value + end + end + def inspect ary = [] for iv in instance_variables @@ -296,8 +318,8 @@ module IRB array = [] for k, v in sort{|a1, a2| a1[0].id2name <=> a2[0].id2name} case k - when :MAIN_CONTEXT - next + when :MAIN_CONTEXT, :__TMP__EHV__ + array.push format("CONF[:%s]=...myself...", k.id2name) when :PROMPT s = v.collect{ |kk, vv| diff --git a/lib/irb/cmd/chws.rb b/lib/irb/cmd/chws.rb new file mode 100644 index 0000000000..84f06014ba --- /dev/null +++ b/lib/irb/cmd/chws.rb @@ -0,0 +1,33 @@ +# +# change-ws.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# + +require "irb/cmd/nop.rb" +require "irb/ext/change-ws.rb" + +module IRB + module ExtendCommand + + class CurrentWorkingWorkspace<=;|&{(" +end +Readline.completion_append_character = nil Readline.completion_proc = IRB::InputCompletor::CompletionProc diff --git a/lib/irb/context.rb b/lib/irb/context.rb index ffc77de875..1c504451cb 100644 --- a/lib/irb/context.rb +++ b/lib/irb/context.rb @@ -1,6 +1,6 @@ # # irb/context.rb - irb context -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -9,6 +9,8 @@ # # # +require "irb/workspace" + module IRB class Context # @@ -22,33 +24,30 @@ module IRB if workspace @workspace = workspace else - @workspace = WorkSpace.new unless workspace + @workspace = WorkSpace.new end @thread = Thread.current if defined? Thread - @irb_level = 0 +# @irb_level = 0 # copy of default configuration @ap_name = IRB.conf[:AP_NAME] @rc = IRB.conf[:RC] @load_modules = IRB.conf[:LOAD_MODULES] - self.math_mode = IRB.conf[:MATH_MODE] @use_readline = IRB.conf[:USE_READLINE] @inspect_mode = IRB.conf[:INSPECT_MODE] - self.use_tracer = IRB.conf[:USE_TRACER] -# @use_loader = IRB.conf[:USE_LOADER] - self.prompt_mode = IRB.conf[:PROMPT_MODE] - + self.math_mode = IRB.conf[:MATH_MODE] if IRB.conf[:MATH_MODE] + self.use_tracer = IRB.conf[:USE_TRACER] if IRB.conf[:USE_TRASER] + self.use_loader = IRB.conf[:USE_LOADER] if IRB.conf[:USE_LOADER] + self.eval_history = IRB.conf[:EVAL_HISTORY] if IRB.conf[:EVEL_HISTORY] + @ignore_sigint = IRB.conf[:IGNORE_SIGINT] @ignore_eof = IRB.conf[:IGNORE_EOF] @back_trace_limit = IRB.conf[:BACK_TRACE_LIMIT] - debug_level = IRB.conf[:DEBUG_LEVEL] - @verbose = IRB.conf[:VERBOSE] - - @tracer_initialized = false + self.prompt_mode = IRB.conf[:PROMPT_MODE] if IRB.conf[:SINGLE_IRB] or !defined?(JobManager) @irb_name = IRB.conf[:IRB_NAME] @@ -59,7 +58,7 @@ module IRB case input_method when nil - if (use_readline.nil? && IRB.conf[:PROMPT_MODE] != :INF_RUBY || + if (use_readline.nil? && IRB.conf[:PROMPT_MODE] != :INF_RUBY && STDIN.tty? || use_readline?) @io = ReadlineInputMethod.new else @@ -72,18 +71,24 @@ module IRB else @io = input_method end + + @verbose = IRB.conf[:VERBOSE] + @echo = IRB.conf[:ECHO] + if @echo.nil? + @echo = true + end + @debug_level = IRB.conf[:DEBUG_LEVEL] end def main @workspace.main end + attr_reader :workspace_home attr_accessor :workspace attr_reader :thread attr_accessor :io - attr_reader :_ - attr_accessor :irb attr_accessor :ap_name attr_accessor :rc @@ -91,14 +96,8 @@ module IRB attr_accessor :irb_name attr_accessor :irb_path - attr_accessor :math_mode attr_accessor :use_readline attr_reader :inspect_mode - attr_reader :use_tracer -# attr :use_loader - - attr_reader :debug_level - attr_accessor :verbose attr_reader :prompt_mode attr_accessor :prompt_i @@ -109,33 +108,43 @@ module IRB attr_accessor :ignore_sigint attr_accessor :ignore_eof + attr_accessor :echo + attr_accessor :verbose + attr_reader :debug_level attr_accessor :back_trace_limit -# alias use_loader? use_loader - alias use_tracer? use_tracer alias use_readline? use_readline alias rc? rc - alias math? math_mode - alias verbose? verbose alias ignore_sigint? ignore_sigint alias ignore_eof? ignore_eof + alias echo? echo + + def verbose? + if @verbose.nil? + if defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod) + false + elsif !STDIN.tty? or @io.kind_of?(FileInputMethod) + true + else + false + end + end + end - def _=(value) - @_ = value - @workspace.evaluate "_ = IRB.conf[:MAIN_CONTEXT]._" + def prompting? + verbose? || (STDIN.tty? && @io.kind_of?(StdioInputMethod) || + (defined?(ReadlineInputMethod) && @io.kind_of?(ReadlineInputMethod))) end - def irb_name - if @irb_level == 0 - @irb_name - elsif @irb_name =~ /#[0-9]*$/ - @irb_name + "." + @irb_level.to_s - else - @irb_name + "#0." + @irb_level.to_s - end + attr_reader :last_value + + def set_last_value(value) + @last_value = value end + attr_reader :irb_name + def prompt_mode=(mode) @prompt_mode = mode pconf = IRB.conf[:PROMPT][mode] @@ -151,42 +160,13 @@ module IRB end def inspect? - @inspect_mode.nil? && !@math_mode or @inspect_mode + @inspect_mode.nil? or @inspect_mode end def file_input? @io.type == FileInputMethod end - def use_tracer=(opt) - if opt - IRB.initialize_tracer - unless @tracer_initialized - Tracer.set_get_line_procs(@irb_path) { - |line_no| - @io.line(line_no) - } - @tracer_initialized = true - end - elsif !opt && @use_tracer - Tracer.off - end - @use_tracer=opt - end - - def use_loader - IRB.conf[:USE_LOADER] - end - - def use_loader=(opt) - IRB.conf[:USE_LOADER] = opt - if opt - IRB.initialize_loader - end - print "Switch to load/require#{unless use_loader; ' non';end} trace mode.\n" if verbose? - opt - end - def inspect_mode=(opt) if opt @inspect_mode = opt @@ -197,20 +177,6 @@ module IRB @inspect_mode end - def math_mode=(opt) - if @math_mode == true && opt == false - IRB.fail CantRetuenNormalMode - return - end - - @math_mode = opt - if math_mode - IRB.initialize_mathn - main.instance_eval("include Math") - print "start math mode\n" if verbose? - end - end - def use_readline=(opt) @use_readline = opt print "use readline module\n" if @use_readline @@ -226,41 +192,19 @@ module IRB @debug_level > 0 end - def change_binding(*_main) - back = @workspace - @workspace = WorkSpace.new(*_main) - unless _main.empty? - begin - main.extend ExtendCommand - rescue - print "can't change binding to: ", main.inspect, "\n" - @workspace = back - return nil - end - end - @irb_level += 1 - begin - catch(:SU_EXIT) do - @irb.eval_input - end - ensure - @irb_level -= 1 - @workspace = back - end + def evaluate(line, line_no) + @line_no = line_no + set_last_value(@workspace.evaluate(self, line, irb_path, line_no)) +# @workspace.evaluate("_ = IRB.conf[:MAIN_CONTEXT]._") +# @_ = @workspace.evaluate(line, irb_path, line_no) end - alias change_workspace change_binding - alias __exit__ exit def exit(ret = 0) - if @irb_level == 0 - IRB.irb_exit(@irb, ret) - else - throw :SU_EXIT, ret - end + IRB.irb_exit(@irb, ret) end - NOPRINTING_IVARS = ["@_"] + NOPRINTING_IVARS = ["@last_value"] NO_INSPECTING_IVARS = ["@irb", "@io"] IDNAME_IVARS = ["@prompt_mode"] @@ -272,7 +216,7 @@ module IRB val = instance_eval(ivar) case ivar when *NOPRINTING_IVARS - next + array.push format("conf.%s=%s", name, "...") when *NO_INSPECTING_IVARS array.push format("conf.%s=%s", name, val.to_s) when *IDNAME_IVARS diff --git a/lib/irb/ext/change-ws.rb b/lib/irb/ext/change-ws.rb new file mode 100644 index 0000000000..f15391e202 --- /dev/null +++ b/lib/irb/ext/change-ws.rb @@ -0,0 +1,62 @@ +# +# irb/ext/cb.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# + +module IRB + class Context + + def home_workspace + if defined? @home_workspace + @home_workspace + else + @home_workspace = @workspace + end + end + + def change_workspace(*_main) + if _main.empty? + @workspace = home_workspace + return main + end + + @workspace = WorkSpace.new(_main[0]) + + if !(class<= 0 + @contents.find{|no, val| no == idx}[1] + else + @contents[idx][1] + end + rescue NameError + nil + end + end + + def push(no, val) + @contents.push [no, val] + @contents.shift if @size != 0 && @contents.size > @size + end + + alias real_inspect inspect + + def inspect + if @contents.empty? + return real_inspect + end + + unless (last = @contents.pop)[1].equal?(self) + @contents.push last + last = nil + end + str = @contents.collect{|no, val| + if val.equal?(self) + "#{no} ...self-history..." + else + "#{no} #{val.inspect}" + end + }.join("\n") + if str == "" + str = "Empty." + end + @contents.push last if last + str + end + end +end + + diff --git a/lib/irb/ext/loader.rb b/lib/irb/ext/loader.rb new file mode 100644 index 0000000000..26e36e1209 --- /dev/null +++ b/lib/irb/ext/loader.rb @@ -0,0 +1,106 @@ +module IRB + class LoadAbort < Exception;end + + module IrbLoader + @RCS_ID='-$Id$-' + alias ruby_load load + alias ruby_require require + + def irb_load(fn, priv = nil) + path = search_file_from_ruby_path(fn) + raise LoadError, "No such file to load -- #{fn}" unless path + + load_file(path, priv) + end + + def search_file_from_ruby_path(fn) + if /^#{Regexp.quote(File::Separator)}/ =~ fn + return fn if File.exist?(fn) + return nil + end + + for path in $: + if File.exist?(f = File.join(path, fn)) + return f + end + end + return nil + end + + def source_file(path) + irb.suspend_name(path, File.basename(path)) do + irb.suspend_input_method(FileInputMethod.new(path)) do + |back_io| + irb.signal_status(:IN_LOAD) do + if back_io.kind_of?(FileInputMethod) + irb.eval_input + else + begin + irb.eval_input + rescue LoadAbort + print "load abort!!\n" + end + end + end + end + end + end + + def load_file(path, priv = nil) + irb.suspend_name(path, File.basename(path)) do + + if priv + ws = WorkSpace.new(Module.new) + else + ws = WorkSpace.new + end + irb.suspend_workspace(ws) do + irb.suspend_input_method(FileInputMethod.new(path)) do + |back_io| + irb.signal_status(:IN_LOAD) do +# p irb.conf + if back_io.kind_of?(FileInputMethod) + irb.eval_input + else + begin + irb.eval_input + rescue LoadAbort + print "load abort!!\n" + end + end + end + end + end + end + end + + def old + back_io = @io + back_path = @irb_path + back_name = @irb_name + back_scanner = @irb.scanner + begin + @io = FileInputMethod.new(path) + @irb_name = File.basename(path) + @irb_path = path + @irb.signal_status(:IN_LOAD) do + if back_io.kind_of?(FileInputMethod) + @irb.eval_input + else + begin + @irb.eval_input + rescue LoadAbort + print "load abort!!\n" + end + end + end + ensure + @io = back_io + @irb_name = back_name + @irb_path = back_path + @irb.scanner = back_scanner + end + end + end +end + diff --git a/lib/irb/ext/math-mode.rb b/lib/irb/ext/math-mode.rb new file mode 100644 index 0000000000..a98664448e --- /dev/null +++ b/lib/irb/ext/math-mode.rb @@ -0,0 +1,37 @@ +# +# math-mode.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# +require "mathn" + +module IRB + class Context + attr_reader :math_mode + alias math? math_mode + + def math_mode=(opt) + if @math_mode == true && opt == false + IRB.fail CantRetuenNormalMode + return + end + + @math_mode = opt + if math_mode + main.extend Math + print "start math mode\n" if verbose? + end + end + + def inspect? + @inspect_mode.nil? && !@math_mode or @inspect_mode + end + end +end + diff --git a/lib/irb/ext/multi-irb.rb b/lib/irb/ext/multi-irb.rb new file mode 100644 index 0000000000..86569ae333 --- /dev/null +++ b/lib/irb/ext/multi-irb.rb @@ -0,0 +1,241 @@ +# +# irb/multi-irb.rb - multiple irb module +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(keiju@ishitsuka.com) +# +# -- +# +# +# +IRB.fail CanNotGoMultiIrbMode unless defined?(Thread) +require "thread" + +module IRB + # job management class + class JobManager + @RCS_ID='-$Id$-' + + def initialize + # @jobs = [[thread, irb],...] + @jobs = [] + @current_job = nil + end + + attr_accessor :current_job + + def n_jobs + @jobs.size + end + + def thread(key) + th, irb = search(key) + th + end + + def irb(key) + th, irb = search(key) + irb + end + + def main_thread + @jobs[0][0] + end + + def main_irb + @jobs[0][1] + end + + def insert(irb) + @jobs.push [Thread.current, irb] + end + + def switch(key) + th, irb = search(key) + IRB.fail IrbAlreadyDead unless th.alive? + IRB.fail IrbSwitchToCurrentThread if th == Thread.current + @current_job = irb + th.run + Thread.stop + @current_job = irb(Thread.current) + end + + def kill(*keys) + for key in keys + th, irb = search(key) + IRB.fail IrbAlreadyDead unless th.alive? + th.exit + end + end + + def search(key) + case key + when Integer + @jobs[key] + when Irb + @jobs.find{|k, v| v.equal?(key)} + when Thread + @jobs.assoc(key) + else + assoc = @jobs.find{|k, v| v.context.main.equal?(key)} + IRB.fail NoSuchJob, key if assoc.nil? + assoc + end + end + + def delete(key) + case key + when Integer + IRB.fail NoSuchJob, key unless @jobs[key] + @jobs[key] = nil + else + catch(:EXISTS) do + @jobs.each_index do + |i| + if @jobs[i] and (@jobs[i][0] == key || + @jobs[i][1] == key || + @jobs[i][1].context.main.equal?(key)) + @jobs[i] = nil + throw :EXISTS + end + end + IRB.fail NoSuchJob, key + end + end + until assoc = @jobs.pop; end unless @jobs.empty? + @jobs.push assoc + end + + def inspect + ary = [] + @jobs.each_index do + |i| + th, irb = @jobs[i] + next if th.nil? + + if th.alive? + if th.stop? + t_status = "stop" + else + t_status = "running" + end + else + t_status = "exited" + end + ary.push format("#%d->%s on %s (%s: %s)", + i, + irb.context.irb_name, + irb.context.main, + th, + t_status) + end + ary.join("\n") + end + end + + @JobManager = JobManager.new + + def IRB.JobManager + @JobManager + end + + def IRB.CurrentContext + IRB.JobManager.irb(Thread.current).context + end + + # invoke multi-irb + def IRB.irb(file = nil, *main) + workspace = WorkSpace.new(*main) + parent_thread = Thread.current + Thread.start do + begin + irb = Irb.new(workspace, file) + rescue + print "Subirb can't start with context(self): ", workspace.main.inspect, "\n" + print "return to main irb\n" + Thread.pass + Thread.main.wakeup + Thread.exit + end + @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] + @JobManager.insert(irb) + @JobManager.current_job = irb + begin + system_exit = false + catch(:IRB_EXIT) do + irb.eval_input + end + rescue SystemExit + system_exit = true + raise + #fail + ensure + unless system_exit + @JobManager.delete(irb) + if parent_thread.alive? + @JobManager.current_job = @JobManager.irb(parent_thread) + parent_thread.run + else + @JobManager.current_job = @JobManager.main_irb + @JobManager.main_thread.run + end + end + end + end + Thread.stop + @JobManager.current_job = @JobManager.irb(Thread.current) + end + +# class Context +# def set_last_value(value) +# @last_value = value +# @workspace.evaluate "_ = IRB.JobManager.irb(Thread.current).context.last_value" +# if @eval_history #and !@__.equal?(@last_value) +# @eval_history_values.push @line_no, @last_value +# @workspace.evaluate "__ = IRB.JobManager.irb(Thread.current).context.instance_eval{@eval_history_values}" +# end +# @last_value +# end +# end + +# module ExtendCommand +# def irb_context +# IRB.JobManager.irb(Thread.current).context +# end +# # alias conf irb_context +# end + + @CONF[:SINGLE_IRB_MODE] = false + @JobManager.insert(@CONF[:MAIN_CONTEXT].irb) + @JobManager.current_job = @CONF[:MAIN_CONTEXT].irb + + class Irb + def signal_handle + unless @context.ignore_sigint? + print "\nabort!!\n" if @context.verbose? + exit + end + + case @signal_status + when :IN_INPUT + print "^C\n" + IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput + when :IN_EVAL + IRB.irb_abort(self) + when :IN_LOAD + IRB.irb_abort(self, LoadAbort) + when :IN_IRB + # ignore + else + # ignore other cases as well + end + end + end + + trap("SIGINT") do + @JobManager.current_job.signal_handle + Thread.stop + end + +end diff --git a/lib/irb/ext/tracer.rb b/lib/irb/ext/tracer.rb new file mode 100644 index 0000000000..b2528f74b2 --- /dev/null +++ b/lib/irb/ext/tracer.rb @@ -0,0 +1,61 @@ +# +# irb/lib/tracer.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# +require "tracer" + +module IRB + + # initialize tracing function + def IRB.initialize_tracer + Tracer.verbose = false + Tracer.add_filter { + |event, file, line, id, binding, *rests| + /^#{Regexp.quote(@CONF[:IRB_LIB_PATH])}/ !~ file and + File::basename(file) != "irb.rb" + } + end + + class Context + attr_reader :use_tracer + alias use_tracer? use_tracer + + def use_tracer=(opt) + if opt + Tracer.set_get_line_procs(@irb_path) { + |line_no, *rests| + @io.line(line_no) + } + elsif !opt && @use_tracer + Tracer.off + end + @use_tracer=opt + end + end + + class WorkSpace + alias __evaluate__ evaluate + def evaluate(context, statements, file = nil, line = nil) + if context.use_tracer? && file != nil && line != nil + Tracer.on + begin + __evaluate__(context, statements, file, line) + ensure + Tracer.off + end + else + __evaluate__(context, statements, file || __FILE__, line || __LINE__) + end + end + end + + IRB.initialize_tracer +end + diff --git a/lib/irb/ext/use-loader.rb b/lib/irb/ext/use-loader.rb new file mode 100644 index 0000000000..a81b3aca7b --- /dev/null +++ b/lib/irb/ext/use-loader.rb @@ -0,0 +1,65 @@ +# +# use-loader.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# + +require "irb/cmd/load" +require "irb/ext/loader" + +class Object + alias __original__load__IRB_use_loader__ load + alias __original__require__IRB_use_loader__ require +end + +module IRB + module ExtendCommandBundle + def irb_load(*opts, &b) + ExtendCommand::Load.execute(irb_context, *opts, &b) + end + def irb_require(*opts, &b) + ExtendCommand::Require.execute(irb_context, *opts, &b) + end + end + + class Context + + IRB.conf[:USE_LOADER] = false + + def use_loader + IRB.conf[:USE_LOADER] + end + + alias use_loader? use_loader + + def use_loader=(opt) + + if IRB.conf[:USE_LOADER] != opt + IRB.conf[:USE_LOADER] = opt + if opt + if !$".include?("irb/cmd/load") + end + (class<<@workspace.main;self;end).instance_eval { + alias_method :load, :irb_load + alias_method :require, :irb_require + } + else + (class<<@workspace.main;self;end).instance_eval { + alias_method :load, :__original__load__IRB_use_loader__ + alias_method :require, :__original__require__IRB_use_loader__ + } + end + end + print "Switch to load/require#{unless use_loader; ' non';end} trace mode.\n" if verbose? + opt + end + end +end + + diff --git a/lib/irb/ext/workspaces.rb b/lib/irb/ext/workspaces.rb new file mode 100644 index 0000000000..2fe8f8a87b --- /dev/null +++ b/lib/irb/ext/workspaces.rb @@ -0,0 +1,56 @@ +# +# push-ws.rb - +# $Release Version: 0.9$ +# $Revision$ +# $Date$ +# by Keiju ISHITSUKA(Nihon Rational Software Co.,Ltd) +# +# -- +# +# +# + +module IRB + class Context + + def irb_level + workspace_stack.size + end + + def workspaces + if defined? @workspaces + @workspaces + else + @workspaces = [] + end + end + + def push_workspace(*_main) + if _main.empty? + if workspaces.empty? + print "No other workspace\n" + return nil + end + ws = workspaces.pop + workspaces.push @workspace + @workspace = ws + return workspaces + end + + workspaces.push @workspace + @workspace = WorkSpace.new(@workspace.binding, _main[0]) + if !(class<%s on %s (%s: %s)", - i, - irb.context.irb_name, - irb.context.main, - th, - t_status) - end - ary.join("\n") - end - end - - @JobManager = JobManager.new - - def IRB.JobManager - @JobManager - end - - # invoke multi-irb - def IRB.irb(file = nil, *main) - workspace = WorkSpace.new(*main) - parent_thread = Thread.current - Thread.start do - begin - irb = Irb.new(workspace, file) - rescue - print "Subirb can't start with context(self): ", workspace.main.inspect, "\n" - print "return to main irb\n" - Thread.pass - Thread.main.wakeup - Thread.exit - end - @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC] - @JobManager.insert(irb) - @JobManager.current_job = irb - begin - system_exit = false - catch(:IRB_EXIT) do - irb.eval_input - end - rescue SystemExit - system_exit = true - raise - #fail - ensure - unless system_exit - @JobManager.delete(irb) - if parent_thread.alive? - @JobManager.current_job = @JobManager.irb(parent_thread) - parent_thread.run - else - @JobManager.current_job = @JobManager.main_irb - @JobManager.main_thread.run - end - end - end - end - Thread.stop - @JobManager.current_job = @JobManager.irb(Thread.current) - end - - class Context - def _=(value) - @_ = value - @workspace.evaluate "_ = IRB.JobManager.irb(Thread.current).context._" - end - end - - module ExtendCommand - def irb_context - IRB.JobManager.irb(Thread.current).context - end -# alias conf irb_context - end - - @CONF[:SINGLE_IRB_MODE] = false - @JobManager.insert(@CONF[:MAIN_CONTEXT].irb) - @JobManager.current_job = @CONF[:MAIN_CONTEXT].irb - - class Irb - def signal_handle - unless @context.ignore_sigint? - print "\nabort!!\n" if @context.verbose? - exit - end - - case @signal_status - when :IN_INPUT - print "^C\n" - IRB.JobManager.thread(self).raise RubyLex::TerminateLineInput - when :IN_EVAL - IRB.irb_abort(self) - when :IN_LOAD - IRB.irb_abort(self, LoadAbort) - when :IN_IRB - # ignore - else - # ignore - end - end - end - - trap("SIGINT") do - @JobManager.current_job.signal_handle - Thread.stop - end - -end diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 3647617849..e96edef30b 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -1,6 +1,6 @@ # # irb/ruby-lex.rb - ruby lexcal analizer -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -50,6 +50,7 @@ class RubyLex @here_readed = [] @indent = 0 + @indent_stack = [] @skip_space = false @readed_auto_clean_up = false @@ -200,6 +201,7 @@ class RubyLex @ltype = nil @quoted = nil @indent = 0 + @indent_stack = [] @lex_state = EXPR_BEG @space_seen = false @here_header = false @@ -235,6 +237,7 @@ class RubyLex @exp_line_no = @line_no @indent = 0 + @indent_stack = [] prompt rescue TerminateLineInput initialize_input @@ -250,6 +253,7 @@ class RubyLex !@continue or tk.nil?) #p tk + #p @lex_state #p self end line = get_readed @@ -356,6 +360,11 @@ class RubyLex else @continue = false @lex_state = EXPR_BEG + until (@indent_stack.empty? || + [TkLPAREN, TkLBRACK, TkLBRACE, + TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last)) + @indent_stack.pop + end end @here_header = false @here_readed = [] @@ -363,26 +372,44 @@ class RubyLex end @OP.def_rules("*", "**", - "!", "!=", "!~", "=", "==", "===", "=~", "<=>", "<", "<=", ">", ">=", ">>") do |op, io| - @lex_state = EXPR_BEG + case @lex_state + when EXPR_FNAME, EXPR_DOT + @lex_state = EXPR_ARG + else + @lex_state = EXPR_BEG + end + Token(op) + end + + @OP.def_rules("!", "!=", "!~") do + |op, io| + #@lex_state = EXPR_BEG Token(op) end @OP.def_rules("<<") do |op, io| + tk = nil if @lex_state != EXPR_END && @lex_state != EXPR_CLASS && (@lex_state != EXPR_ARG || @space_seen) c = peek(0) if /\S/ =~ c && (/["'`]/ =~ c || /[\w_]/ =~ c || c == "-") tk = identify_here_document end - else - tk = Token(op) + end + unless tk + tk = Token(op) + case @lex_state + when EXPR_FNAME, EXPR_DOT + @lex_state = EXPR_ARG + else + @lex_state = EXPR_BEG + end end tk end @@ -408,7 +435,7 @@ class RubyLex Token(TkQUESTION) else ch = getc - if @lex_state == EXPR_ARG && ch !~ /\s/ + if @lex_state == EXPR_ARG && ch =~ /\s/ ungetc @lex_state = EXPR_BEG; Token(TkQUESTION) @@ -437,11 +464,13 @@ class RubyLex end @OP.def_rule("+@", proc{@lex_state == EXPR_FNAME}) do - Token(TkUPLUS) + |op, io| + Token(op) end @OP.def_rule("-@", proc{@lex_state == EXPR_FNAME}) do - Token(TkUMINUS) + |op, io| + Token(op) end @OP.def_rules("+", "-") do @@ -488,6 +517,7 @@ class RubyLex |op, io| @lex_state = EXPR_END @indent -= 1 + @indent_stack.pop Token(op) end @@ -519,7 +549,7 @@ class RubyLex elsif peek(0) == '=' getc @lex_state = EXPR_BEG - Token(TkOPASGN, :/) #/) + Token(TkOPASGN, "/") #/) elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/ identify_string(op) else @@ -538,18 +568,29 @@ class RubyLex # Token(OP_ASGN, :^) # end - @OP.def_rules(",", ";") do + @OP.def_rules(",") do |op, io| @lex_state = EXPR_BEG Token(op) end + @OP.def_rules(";") do + |op, io| + @lex_state = EXPR_BEG + until (@indent_stack.empty? || + [TkLPAREN, TkLBRACK, TkLBRACE, + TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last)) + @indent_stack.pop + end + Token(op) + end + @OP.def_rule("~") do @lex_state = EXPR_BEG Token("~") end - @OP.def_rule("~@", proc{@lex_state = EXPR_FNAME}) do + @OP.def_rule("~@", proc{@lex_state == EXPR_FNAME}) do @lex_state = EXPR_BEG Token("~") end @@ -558,11 +599,13 @@ class RubyLex @indent += 1 if @lex_state == EXPR_BEG || @lex_state == EXPR_MID @lex_state = EXPR_BEG - Token(TkfLPAREN) + tk_c = TkfLPAREN else @lex_state = EXPR_BEG - Token(TkLPAREN) + tk_c = TkLPAREN end + @indent_stack.push tk_c + tk = Token(tk_c) end @OP.def_rule("[]", proc{@lex_state == EXPR_FNAME}) do @@ -576,29 +619,31 @@ class RubyLex @OP.def_rule("[") do @indent += 1 if @lex_state == EXPR_FNAME - Token(TkfLBRACK) + tk_c = TkfLBRACK else if @lex_state == EXPR_BEG || @lex_state == EXPR_MID - t = Token(TkLBRACK) + tk_c = TkLBRACK elsif @lex_state == EXPR_ARG && @space_seen - t = Token(TkLBRACK) + tk_c = TkLBRACK else - t = Token(TkfLBRACK) + tk_c = TkfLBRACK end @lex_state = EXPR_BEG - t end + @indent_stack.push tk_c + Token(tk_c) end @OP.def_rule("{") do @indent += 1 if @lex_state != EXPR_END && @lex_state != EXPR_ARG - t = Token(TkLBRACE) + tk_c = TkLBRACE else - t = Token(TkfLBRACE) + tk_c = TkfLBRACE end @lex_state = EXPR_BEG - t + @indent_stack.push tk_c + Token(tk_c) end @OP.def_rule('\\') do @@ -632,7 +677,7 @@ class RubyLex end @OP.def_rule('@') do - if peek(0) =~ /[\w_]/ + if peek(0) =~ /[\w_@]/ ungetc identify_identifier else @@ -691,21 +736,32 @@ class RubyLex def identify_identifier token = "" - token.concat getc if peek(0) =~ /[$@]/ + if peek(0) =~ /[$@]/ + token.concat (c = getc) + if c == "@" and peek(0) == "@" + token.concat getc + end + end + while (ch = getc) =~ /\w|_/ print ":", ch, ":" if RubyLex.debug? token.concat ch end ungetc - if ch == "!" or ch == "?" + if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "=" token.concat getc end + # almost fix token case token when /^\$/ return Token(TkGVAR, token) + when /^\@\@/ + @lex_state = EXPR_END + # p Token(TkCVAR, token) + return Token(TkCVAR, token) when /^\@/ @lex_state = EXPR_END return Token(TkIVAR, token) @@ -727,9 +783,37 @@ class RubyLex else if @lex_state != EXPR_FNAME if ENINDENT_CLAUSE.include?(token) - @indent += 1 + # check for ``class = val''. + valid = true + case token + when "class" + valid = false unless peek_match?(/^\s*(<<|\w)/) + + when "def" + valid = false if peek_match?(/^\s*(([+-\/*&\|^]|<<|>>|\|\||\&\&)?=|\&\&|\|\|)/) + when "do" + valid = false if peek_match?(/^\s*([+-\/*]?=|\*|<|>|\&)/) + when *ENINDENT_CLAUSE + valid = false if peek_match?(/^\s*([+-\/*]?=|\*|<|>|\&|\|)/) + else + # no nothing + end + if valid + if token == "do" + if ![TkFOR, TkWHILE, TkUNTIL].include?(@indent_stack.last) + @indent += 1 + @indent_stack.push token_c + end + else + @indent += 1 + @indent_stack.push token_c + end +# p @indent_stack + end + elsif DEINDENT_CLAUSE.include?(token) @indent -= 1 + @indent_stack.pop end @lex_state = trans[0] else @@ -830,9 +914,12 @@ class RubyLex @lex_state = EXPR_END if ch = getc - if peek(0) == "x" + if /[xX]/ =~ peek(0) ch = getc - match = /[0-9a-f_]/ + match = /[0-9a-fA-F_]/ + elsif /[bB]/ =~ peek(0) + ch = getc + match = /[01_]/ else match = /[0-7_]/ end @@ -878,17 +965,25 @@ class RubyLex @quoted = quoted subtype = nil begin - while ch = getc - if @quoted == ch + nest = 0 + while ch = getc + if @quoted == ch and nest == 0 break elsif @ltype != "'" && @ltype != "]" and ch == "#" subtype = true elsif ch == '\\' #' read_escape end + if PERCENT_PAREN.values.include?(@quoted) + if PERCENT_PAREN[ch] == @quoted + nest += 1 + elsif ch == @quoted + nest -= 1 + end + end end if @ltype == "/" - if peek(0) =~ /i|o|n|e|s/ + if peek(0) =~ /i|m|x|o|e|s|u|n/ getc end end @@ -908,9 +1003,9 @@ class RubyLex @ltype = "#" while ch = getc - if ch == "\\" #" - read_escape - end +# if ch == "\\" #" +# read_escape +# end if ch == "\n" @ltype = nil ungetc diff --git a/lib/irb/ruby-token.rb b/lib/irb/ruby-token.rb index 373c8defea..55c1bd11f3 100644 --- a/lib/irb/ruby-token.rb +++ b/lib/irb/ruby-token.rb @@ -1,6 +1,6 @@ # # irb/ruby-token.rb - ruby tokens -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -155,6 +155,7 @@ module RubyToken [:TkIDENTIFIER, TkId], [:TkFID, TkId], [:TkGVAR, TkId], + [:TkCVAR, TkId], [:TkIVAR, TkId], [:TkCONSTANT, TkId], diff --git a/lib/irb/slex.rb b/lib/irb/slex.rb index 26008906e5..1cf23255ad 100644 --- a/lib/irb/slex.rb +++ b/lib/irb/slex.rb @@ -1,6 +1,6 @@ # # irb/slex.rb - symple lex analizer -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishituska.com) @@ -68,8 +68,7 @@ class SLex case token when Array when String - token = token.split(//) - match(token.split(//)) + return match(token.split(//)) else return @head.match_io(token) end diff --git a/lib/irb/version.rb b/lib/irb/version.rb index 367cc21046..a9e37f7e7b 100644 --- a/lib/irb/version.rb +++ b/lib/irb/version.rb @@ -1,6 +1,6 @@ # # irb/version.rb - irb version definition file -# $Release Version: 0.7.4$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -11,6 +11,6 @@ # module IRB - @RELEASE_VERSION = "0.7.4" - @LAST_UPDATE_DATE = "01/05/08" + @RELEASE_VERSION = "0.9" + @LAST_UPDATE_DATE = "02/07/03" end diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 68559a1173..6dcb450fd7 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -1,6 +1,6 @@ # # irb/workspace-binding.rb - -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) @@ -11,11 +11,12 @@ # module IRB class WorkSpace - # create new workspace. - # set self to main if specified, otherwise inherit main - # from TOPLEVEL_BINDING. + # create new workspace. set self to main if specified, otherwise + # inherit main from TOPLEVEL_BINDING. def initialize(*main) - if IRB.conf[:SINGLE_IRB] + if main[0].kind_of?(Binding) + @binding = main.shift + elsif IRB.conf[:SINGLE_IRB] @binding = TOPLEVEL_BINDING else case IRB.conf[:CONTEXT_MODE] @@ -76,10 +77,10 @@ EOF attr_reader :binding attr_reader :main - def evaluate(statements, file = __FILE__, line = __LINE__) - eval statements, @binding, file, line + def evaluate(context, statements, file = __FILE__, line = __LINE__) + eval(statements, @binding, file, line) end - + # error message manupilator def filter_backtrace(bt) case IRB.conf[:CONTEXT_MODE] diff --git a/lib/irb/ws-for-case-2.rb b/lib/irb/ws-for-case-2.rb index 8cfa87ae3d..1c58fb7dda 100644 --- a/lib/irb/ws-for-case-2.rb +++ b/lib/irb/ws-for-case-2.rb @@ -1,6 +1,6 @@ # # irb/ws-for-case-2.rb - -# $Release Version: 0.7.3$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(keiju@ishitsuka.com) diff --git a/lib/irb/xmp.rb b/lib/irb/xmp.rb index e0bcee4bdb..4bcc2ca22f 100644 --- a/lib/irb/xmp.rb +++ b/lib/irb/xmp.rb @@ -1,6 +1,6 @@ # # xmp.rb - irb version of gotoken xmp -# $Release Version: 0.7.1$ +# $Release Version: 0.9$ # $Revision$ # $Date$ # by Keiju ISHITSUKA(Nippon Rational Inc.) @@ -10,21 +10,23 @@ # # -require "irb/irb" +require "irb" require "irb/frame" class XMP @RCS_ID='-$Id$-' def initialize(bind = nil) + IRB.init_config(nil) #IRB.parse_opts #IRB.load_modules + IRB.conf[:PROMPT_MODE] = :XMP + bind = IRB::Frame.top(1) unless bind - main = eval("self", bind) + ws = IRB::WorkSpace.new(bind) @io = StringInputMethod.new - @irb = IRB::Irb.new(main, bind, @io) - @irb.context.prompt_mode = :XMP + @irb = IRB::Irb.new(ws, @io) @irb.context.ignore_sigint = false # IRB.conf[:IRB_RC].call(@irb.context) if IRB.conf[:IRB_RC] -- cgit v1.2.3