Skip to content
Browse files

Lots more refactoring. Work on workspaces, and a bunch else.

  • Loading branch information...
1 parent 926d081 commit efc94c38f6eebca4facdf578ee40d1fc0e3823b7 Yehuda Katz committed
Showing with 515 additions and 578 deletions.
  1. +3 −0 irb.rb
  2. +33 −33 irb/cmd/load.rb
  3. +12 −9 irb/cmd/pushws.rb
  4. +1 −2 irb/context.rb
  5. +15 −46 irb/ext/change-ws.rb
  6. +32 −33 irb/ext/history.rb
  7. +20 −21 irb/ext/loader.rb
  8. +1 −2 irb/ext/multi-irb.rb
  9. +16 −35 irb/ext/save-history.rb
  10. +16 −27 irb/ext/workspaces.rb
  11. +7 −8 irb/init.rb
  12. +1 −1 irb/input-method.rb
  13. +358 −361 irb/ruby-lex.rb
View
3 irb.rb
@@ -56,6 +56,9 @@ def start(path = nil)
self.main_context = irb.context
+ # TODO: Move into history module
+ main_context.save_history = conf[:SAVE_HISTORY]
+
require "irb/ext/multi-irb"
trap("SIGINT") { irb.signal_handle }
View
66 irb/cmd/load.rb
@@ -1,13 +1,13 @@
#
-# load.rb -
-# $Release Version: 0.9.5$
-# $Revision: 11708 $
-# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# load.rb -
+# $Release Version: 0.9.5$
+# $Revision: 11708 $
+# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
-#
+#
#
require "irb/cmd/nop.rb"
@@ -19,47 +19,47 @@ class Load<Nop
include IrbLoader
def execute(file_name, priv = nil)
-# return ruby_load(file_name) unless IRB.conf[:USE_LOADER]
- return irb_load(file_name, priv)
+# return ruby_load(file_name) unless IRB.conf[:USE_LOADER]
+ return irb_load(file_name, priv)
end
end
class Require<Nop
include IrbLoader
-
+
def execute(file_name)
-# return ruby_require(file_name) unless IRB.conf[:USE_LOADER]
+# return ruby_require(file_name) unless IRB.conf[:USE_LOADER]
+
+ rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?")
+ return false if $".find{|f| f =~ rex}
- rex = Regexp.new("#{Regexp.quote(file_name)}(\.o|\.rb)?")
- return false if $".find{|f| f =~ rex}
+ case file_name
+ when /\.rb$/
+ begin
+ if irb_load(file_name)
+ $".push file_name
+ return true
+ end
+ rescue LoadError
+ end
+ when /\.(so|o|sl)$/
+ return ruby_require(file_name)
+ end
- case file_name
- when /\.rb$/
- begin
- if irb_load(file_name)
- $".push file_name
- return true
- end
- rescue LoadError
- end
- when /\.(so|o|sl)$/
- return ruby_require(file_name)
- end
-
- begin
- irb_load(f = file_name + ".rb")
- $".push f
- return true
- rescue LoadError
- return ruby_require(file_name)
- end
+ begin
+ irb_load(f = file_name + ".rb")
+ $".push f
+ return true
+ rescue LoadError
+ return ruby_require(file_name)
+ end
end
end
class Source<Nop
include IrbLoader
def execute(file_name)
- source_file(file_name)
+ source_file(file_name)
end
end
end
View
21 irb/cmd/pushws.rb
@@ -1,9 +1,9 @@
#
# change-ws.rb -
-# $Release Version: 0.9.5$
-# $Revision: 11708 $
-# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# $Release Version: 0.9.5$
+# $Revision: 11708 $
+# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
@@ -17,21 +17,24 @@ module IRB
module ExtendCommand
class Workspaces<Nop
def execute(*obj)
- irb_context.workspaces.collect{|ws| ws.main}
+ workspaces = irb_context.workspaces.map {|w| w.main.inspect} + ["[green]#{irb_context.main.inspect}[/]"]
+ puts IRB.colorize("[blue]Workspaces\n----------[/]\n" <<
+ workspaces.join("\n"))
+ IRB::CommandResult
end
end
class PushWorkspace<Workspaces
def execute(*obj)
- irb_context.push_workspace(*obj)
- super
+ irb_context.push_workspace(*obj)
+ super
end
end
class PopWorkspace<Workspaces
def execute(*obj)
- irb_context.pop_workspace(*obj)
- super
+ irb_context.pop_workspace(*obj)
+ super
end
end
end
View
3 irb/context.rb
@@ -65,7 +65,6 @@ def initialize(irb, workspace = nil, input_method = nil, output_method = nil)
else
@io = input_method
end
- self.save_history = IRB.conf[:SAVE_HISTORY] if IRB.conf[:SAVE_HISTORY]
if output_method
@output_method = output_method
@@ -138,7 +137,7 @@ def set_last_value(value)
end
def irb_name
- @irb_name ||= "irb" + IRB.job_manager.current_job_ixZd
+ @irb_name ||= "irb" + IRB.job_manager.current_job_id
end
def irb_path
View
61 irb/ext/change-ws.rb
@@ -1,62 +1,31 @@
#
-# irb/ext/cb.rb -
-# $Release Version: 0.9.5$
-# $Revision: 11708 $
-# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# irb/ext/cb.rb -
+# $Release Version: 0.9.5$
+# $Revision: 11708 $
+# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
-#
+#
#
module IRB
class Context
def home_workspace
- if defined? @home_workspace
- @home_workspace
- else
- @home_workspace = @workspace
- end
+ @home_workspace ||= @workspace
end
- def change_workspace(*_main)
- if _main.empty?
- @workspace = home_workspace
- return main
- end
-
- @workspace = WorkSpace.new(_main[0])
-
- if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
- main.extend ExtendCommandBundle
+ def change_workspace(_main = nil)
+ unless _main
+ @workspace = home_workspace
+ return main
end
- end
-# def change_binding(*_main)
-# back = @workspace
-# @workspace = WorkSpace.new(*_main)
-# unless _main.empty?
-# begin
-# main.extend ExtendCommandBundle
-# 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
-# end
-# alias change_workspace change_binding
- end
+ @workspace = WorkSpace.new(_main)
+ @workspace.main.extend ExtendCommandBundle
+ end
+ end
end
View
65 irb/ext/history.rb
@@ -1,9 +1,9 @@
#
# history.rb -
-# $Release Version: 0.9.5$
-# $Revision: 11708 $
-# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# $Release Version: 0.9.5$
+# $Revision: 11708 $
+# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
@@ -21,10 +21,9 @@ class Context
def set_last_value(value)
_set_last_value(value)
-# @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
- if @eval_history #and !@eval_history_values.equal?(llv)
- @eval_history_values.push @line_no, @last_value
- @workspace.evaluate self, "__ = IRB.CurrentContext.instance_eval{@eval_history_values}"
+ if @eval_history
+ @eval_history_values.push @line_no, @last_value
+ @workspace.evaluate self, "__ = IRB.current_context.instance_eval{@eval_history_values}"
end
@last_value
@@ -33,16 +32,16 @@ def set_last_value(value)
attr_reader :eval_history
def eval_history=(no)
if no
- if defined?(@eval_history) && @eval_history
- @eval_history_values.size(no)
- else
- @eval_history_values = History.new(no)
- IRB.conf[:__TMP__EHV__] = @eval_history_values
- @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
- IRB.conf.delete(:__TMP_EHV__)
- end
+ if defined?(@eval_history) && @eval_history
+ @eval_history_values.size(no)
+ else
+ @eval_history_values = History.new(no)
+ IRB.conf[:__TMP__EHV__] = @eval_history_values
+ @workspace.evaluate(self, "__ = IRB.conf[:__TMP__EHV__]")
+ IRB.conf.delete(:__TMP_EHV__)
+ end
else
- @eval_history_values = nil
+ @eval_history_values = nil
end
@eval_history = no
end
@@ -58,20 +57,20 @@ def initialize(size = 16)
def size(size)
if size != 0 && size < @size
- @contents = @contents[@size - size .. @size]
+ @contents = @contents[@size - size .. @size]
end
@size = size
end
def [](idx)
begin
- if idx >= 0
- @contents.find{|no, val| no == idx}[1]
- else
- @contents[idx][1]
- end
+ if idx >= 0
+ @contents.find{|no, val| no == idx}[1]
+ else
+ @contents[idx][1]
+ end
rescue NameError
- nil
+ nil
end
end
@@ -84,22 +83,22 @@ def push(no, val)
def inspect
if @contents.empty?
- return real_inspect
+ return real_inspect
end
unless (last = @contents.pop)[1].equal?(self)
- @contents.push last
- last = nil
+ @contents.push last
+ last = nil
end
str = @contents.collect{|no, val|
- if val.equal?(self)
- "#{no} ...self-history..."
- else
- "#{no} #{val.inspect}"
- end
+ if val.equal?(self)
+ "#{no} ...self-history..."
+ else
+ "#{no} #{val.inspect}"
+ end
}.join("\n")
if str == ""
- str = "Empty."
+ str = "Empty."
end
@contents.push last if last
str
View
41 irb/ext/loader.rb
@@ -90,14 +90,14 @@ def irb_load(fn, priv = nil)
def search_file_from_ruby_path(fn)
if /^#{Regexp.quote(File::Separator)}/ =~ fn
- return fn if File.exist?(fn)
- return nil
+ return fn if File.exist?(fn)
+ return nil
end
for path in $:
- if File.exist?(f = File.join(path, fn))
- return f
- end
+ if File.exist?(f = File.join(path, fn))
+ return f
+ end
end
return nil
end
@@ -122,28 +122,27 @@ def source_file(path)
def load_file(path, priv = nil)
irb.with_path(path, File.basename(path)) do
-
- if priv
- ws = WorkSpace.new(Module.new)
- else
- ws = WorkSpace.new
- end
- irb.with_workspace(ws) do
- irb.with_input_method(FileInputMethod.new(path)) do |io|
- irb.with_signal_status(:IN_LOAD) do
- if old_io.kind_of?(FileInputMethod)
- irb.eval_input
- else
- begin
+ if priv
+ ws = WorkSpace.new(Module.new)
+ else
+ ws = WorkSpace.new
+ end
+ irb.with_workspace(ws) do
+ irb.with_input_method(FileInputMethod.new(path)) do |io|
+ irb.with_signal_status(:IN_LOAD) do
+ if old_io.kind_of?(FileInputMethod)
irb.eval_input
- rescue LoadAbort
- print "load abort!!\n"
+ else
+ begin
+ irb.eval_input
+ rescue LoadAbort
+ print "load abort!!\n"
+ end
end
end
end
end
end
- end
end
def old
View
3 irb/ext/multi-irb.rb
@@ -116,8 +116,7 @@ def delete(key)
@jobs[key] = nil
else
catch(:EXISTS) do
- @jobs.each_index do
- |i|
+ @jobs.each_index do |i|
if @jobs[i] and (@jobs[i][0] == key ||
@jobs[i][1] == key ||
@jobs[i][1].context.main.equal?(key))
View
51 irb/ext/save-history.rb
@@ -1,6 +1,6 @@
#!/usr/local/bin/ruby
#
-# save-history.rb -
+# save-history.rb -
# $Release Version: 0.9.5$
# $Revision: 11708 $
# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
@@ -8,10 +8,11 @@
#
# --
#
-#
+#
#
require "readline"
+require "pathname"
module IRB
module HistorySavingAbility
@@ -20,9 +21,7 @@ module HistorySavingAbility
class Context
def init_save_history
- unless (class<<@io;self;end).include?(HistorySavingAbility)
- @io.extend(HistorySavingAbility)
- end
+ HistorySavingAbility.load_history
end
def save_history
@@ -35,48 +34,30 @@ def save_history=(val)
context.init_save_history if val
end
- def history_file
- IRB.conf[:HISTORY_FILE]
- end
-
- def history_file=(hist)
- IRB.conf[:HISTORY_FILE] = hist
- end
+ attr_accessor :history_file
end
module HistorySavingAbility
include Readline
- def HistorySavingAbility.create_finalizer
- proc do
- if num = IRB.conf[:SAVE_HISTORY] and (num = num.to_i) > 0
- if hf = IRB.conf[:HISTORY_FILE]
- file = File.expand_path(hf)
- end
- file = IRB.rc_file("_history") unless file
- open(file, 'w' ) do |f|
- hist = HISTORY.to_a
- f.puts(hist[-num..-1] || hist)
- end
+ at_exit do
+ if (num = IRB.conf[:SAVE_HISTORY].to_i) > 0
+ File.open(history_file, 'w') do |f|
+ f.puts HISTORY.to_a.last(num).join("\n")
end
end
end
- def HistorySavingAbility.extended(obj)
- ObjectSpace.define_finalizer(obj, HistorySavingAbility.create_finalizer)
- obj.load_history
- obj
+ def self.history_file
+ config = IRB.main_context.history_file
+ config ? Pathname.new(config) : IRB.rc_file("_history")
end
- def load_history
- hist = IRB.conf[:HISTORY_FILE]
- hist = IRB.rc_file("_history") unless hist
- if File.exist?(hist)
- open(hist) do |f|
- f.each {|l| HISTORY << l.chomp}
- end
+ def self.load_history
+ hist = history_file
+ if hist.exist?
+ File.open(hist).each_line {|l| HISTORY << l.chomp }
end
end
end
end
-
View
43 irb/ext/workspaces.rb
@@ -1,13 +1,13 @@
#
-# push-ws.rb -
-# $Release Version: 0.9.5$
-# $Revision: 11708 $
-# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# push-ws.rb -
+# $Release Version: 0.9.5$
+# $Revision: 11708 $
+# $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
-#
+#
#
module IRB
@@ -18,37 +18,26 @@ def irb_level
end
def workspaces
- if defined? @workspaces
- @workspaces
- else
- @workspaces = []
- end
+ @workspaces ||= []
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
+ def push_workspace(_main = nil)
+ unless main
+ puts "[red]Please specify a workspace to push[/]"
+ return
end
workspaces.push @workspace
- @workspace = WorkSpace.new(@workspace.binding, _main[0])
- if !(class<<main;ancestors;end).include?(ExtendCommandBundle)
- main.extend ExtendCommandBundle
- end
+ @workspace = WorkSpace.new(@workspace.binding, _main)
+ main.extend ExtendCommandBundle
end
def pop_workspace
if workspaces.empty?
- print "workspace stack empty\n"
- return
+ puts "[red]No workspaces to pop[/]"
+ return
end
+
@workspace = workspaces.pop
end
end
View
15 irb/init.rb
@@ -10,6 +10,8 @@
#
#
+require "pathname"
+
module IRB
# initialize config
@@ -258,17 +260,14 @@ def IRB.run_config
def IRB.rc_file(ext = "rc")
possible = [".irb#{ext}", "_irb#{ext}", "$irb#{ext}", "irb.#{ext}"]
- possible.each do |file|
- if home = ENV["HOME"]
- home_file = File.join(home, file)
- return home_file if File.exist?(home_file)
- end
+ base = Pathname.new(ENV["HOME"] || Dir.pwd)
- pwd_file = File.join(Dir.pwd, file)
- return pwd_file if File.exist?(pwd_file)
+ possible.each do |file|
+ check = base.join(file)
+ return check if check.exist?
end
- ""
+ return base.join(".irb#{ext}")
end
end
View
2 irb/input-method.rb
@@ -93,7 +93,7 @@ def initialize
def gets
if l = readline(@prompt, false)
- HISTORY.push(l) if !l.empty?
+ HISTORY.push(l) unless l.empty?
@line[@line_no += 1] = l + "\n"
else
@eof = true
View
719 irb/ruby-lex.rb
@@ -1,13 +1,13 @@
#
# irb/ruby-lex.rb - ruby lexcal analyzer
-# $Release Version: 0.9.5$
-# $Revision: 16857 $
-# $Date: 2008-06-06 17:05:24 +0900 (Fri, 06 Jun 2008) $
-# by Keiju ISHITSUKA(keiju@ruby-lang.org)
+# $Release Version: 0.9.5$
+# $Revision: 16857 $
+# $Date: 2008-06-06 17:05:24 +0900 (Fri, 06 Jun 2008) $
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
-#
+#
#
require "e2mmap"
@@ -21,12 +21,12 @@ class RubyLex
def_exception(:AlreadyDefinedToken, "Already defined token(%s)")
def_exception(:TkReading2TokenNoKey, "key nothing(key='%s')")
def_exception(:TkSymbol2TokenNoKey, "key nothing(key='%s')")
- def_exception(:TkReading2TokenDuplicateError,
- "key duplicate(token_n='%s', key='%s')")
+ def_exception(:TkReading2TokenDuplicateError,
+ "key duplicate(token_n='%s', key='%s')")
def_exception(:SyntaxError, "%s")
def_exception(:TerminateLineInput, "Terminate Line Input")
-
+
include RubyToken
class << self
@@ -54,7 +54,7 @@ def initialize
@lex_state = EXPR_BEG
@space_seen = false
@here_header = false
-
+
@continue = false
@line = ""
@@ -85,7 +85,7 @@ def get_readed
else
@base_char_no += @readed.size
end
-
+
readed = @readed.join("")
@readed = []
readed
@@ -93,6 +93,7 @@ def get_readed
def getc
while @rests.empty?
+# return nil unless buf_input
@rests.push nil unless buf_input
end
c = @rests.shift
@@ -103,7 +104,7 @@ def getc
end
@seek += 1
if c == "\n"
- @line_no += 1
+ @line_no += 1
@char_no = 0
else
@char_no += 1
@@ -136,14 +137,14 @@ def ungetc(c = nil)
c2 = @here_readed.pop
end
c = c2 unless c
- @rests.unshift c #c =
+ @rests.unshift c #c =
@seek -= 1
if c == "\n"
- @line_no -= 1
+ @line_no -= 1
if idx = @readed.reverse.index("\n")
- @char_no = @readed.size - idx
+ @char_no = @readed.size - idx
else
- @char_no = @base_char_no + @readed.size
+ @char_no = @base_char_no + @readed.size
end
else
@char_no -= 1
@@ -216,59 +217,55 @@ def initialize_input
@lex_state = EXPR_BEG
@space_seen = false
@here_header = false
-
+
@continue = false
prompt
@line = ""
@exp_line_no = @line_no
end
-
+
def each_top_level_statement
initialize_input
catch(:TERM_INPUT) do
loop do
- begin
- @continue = false
- prompt
- unless l = lex
- throw :TERM_INPUT if @line == ''
- else
- #p l
- @line.concat l
- if @ltype or @continue or @indent > 0
- next
- end
- end
- if @line != "\n"
- yield @line, @exp_line_no
- end
- break unless l
- @line = ''
- @exp_line_no = @line_no
-
- @indent = 0
- @indent_stack = []
- prompt
- rescue TerminateLineInput
- initialize_input
- prompt
- get_readed
- end
+ begin
+ @continue = false
+ prompt
+ unless l = lex
+ throw :TERM_INPUT if @line == ''
+ else
+ #p l
+ @line.concat l
+ if @ltype or @continue or @indent > 0
+ next
+ end
+ end
+ if @line != "\n"
+ yield @line, @exp_line_no
+ end
+ break unless l
+ @line = ''
+ @exp_line_no = @line_no
+
+ @indent = 0
+ @indent_stack = []
+ prompt
+ rescue TerminateLineInput
+ initialize_input
+ prompt
+ get_readed
+ end
end
end
end
def lex
until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
- !@continue or
- tk.nil?)
- #p tk
- #p @lex_state
- #p self
+ !@continue or
+ tk.nil?)
end
line = get_readed
- # print self.inspect
if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
nil
else
@@ -284,11 +281,11 @@ def token
@prev_char_no = @char_no
begin
begin
- tk = @OP.match(self)
- @space_seen = tk.kind_of?(TkSPACE)
+ tk = @OP.match(self)
+ @space_seen = tk.kind_of?(TkSPACE)
rescue SyntaxError
- raise if @exception_on_syntax_error
- tk = TkError.new(@seek, @line_no, @char_no)
+ raise if @exception_on_syntax_error
+ tk = TkError.new(@seek, @line_no, @char_no)
end
end while @skip_space and tk.kind_of?(TkSPACE)
if @readed_auto_clean_up
@@ -297,7 +294,7 @@ def token
# Tracer.off
tk
end
-
+
ENINDENT_CLAUSE = [
"case", "class", "def", "do", "for", "if",
"module", "unless", "until", "while", "begin" #, "when"
@@ -314,7 +311,7 @@ def token
"W" => "]",
"s" => ":"
}
-
+
PERCENT_PAREN = {
"{" => "}",
"[" => "]",
@@ -354,12 +351,12 @@ def lex_init()
end
@OP.def_rule("=begin",
- proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
+ proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
|op, io|
@ltype = "="
until getc == "\n"; end
until peek_equal?("=end") && peek(4) =~ /\s/
- until getc == "\n"; end
+ until getc == "\n"; end
end
gets
@ltype = nil
@@ -370,32 +367,32 @@ def lex_init()
print "\\n\n" if RubyLex.debug?
case @lex_state
when EXPR_BEG, EXPR_FNAME, EXPR_DOT
- @continue = true
+ @continue = true
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
+ @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 = []
Token(TkNL)
end
- @OP.def_rules("*", "**",
- "=", "==", "===",
- "=~", "<=>",
- "<", "<=",
- ">", ">=", ">>") do
+ @OP.def_rules("*", "**",
+ "=", "==", "===",
+ "=~", "<=>",
+ "<", "<=",
+ ">", ">=", ">>") do
|op, io|
case @lex_state
when EXPR_FNAME, EXPR_DOT
- @lex_state = EXPR_ARG
+ @lex_state = EXPR_ARG
else
- @lex_state = EXPR_BEG
+ @lex_state = EXPR_BEG
end
Token(op)
end
@@ -410,20 +407,20 @@ def lex_init()
|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
+ (@lex_state != EXPR_ARG || @space_seen)
+ c = peek(0)
+ if /\S/ =~ c && (/["'`]/ =~ c || /[\w_]/ =~ c || c == "-")
+ tk = identify_here_document
+ end
end
unless tk
- tk = Token(op)
- case @lex_state
- when EXPR_FNAME, EXPR_DOT
- @lex_state = EXPR_ARG
- else
- @lex_state = EXPR_BEG
- end
+ tk = Token(op)
+ case @lex_state
+ when EXPR_FNAME, EXPR_DOT
+ @lex_state = EXPR_ARG
+ else
+ @lex_state = EXPR_BEG
+ end
end
tk
end
@@ -436,31 +433,31 @@ def lex_init()
@OP.def_rules("`") do
|op, io|
if @lex_state == EXPR_FNAME
- @lex_state = EXPR_END
- Token(op)
+ @lex_state = EXPR_END
+ Token(op)
else
- identify_string(op)
+ identify_string(op)
end
end
@OP.def_rules('?') do
|op, io|
if @lex_state == EXPR_END
- @lex_state = EXPR_BEG
- Token(TkQUESTION)
+ @lex_state = EXPR_BEG
+ Token(TkQUESTION)
else
- ch = getc
- if @lex_state == EXPR_ARG && ch =~ /\s/
- ungetc
- @lex_state = EXPR_BEG;
- Token(TkQUESTION)
- else
- if (ch == '\\')
- read_escape
- end
- @lex_state = EXPR_END
- Token(TkINTEGER)
- end
+ ch = getc
+ if @lex_state == EXPR_ARG && ch =~ /\s/
+ ungetc
+ @lex_state = EXPR_BEG;
+ Token(TkQUESTION)
+ else
+ if (ch == '\\')
+ read_escape
+ end
+ @lex_state = EXPR_END
+ Token(TkINTEGER)
+ end
end
end
@@ -469,9 +466,9 @@ def lex_init()
@lex_state = EXPR_BEG
Token(op)
end
-
- @OP.def_rules("+=", "-=", "*=", "**=",
- "&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
+
+ @OP.def_rules("+=", "-=", "*=", "**=",
+ "&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
|op, io|
@lex_state = EXPR_BEG
op =~ /^(.*)=$/
@@ -493,18 +490,18 @@ def lex_init()
@OP.def_rules("+", "-") do
|op, io|
catch(:RET) do
- if @lex_state == EXPR_ARG
- if @space_seen and peek(0) =~ /[0-9]/
- throw :RET, identify_number
- else
- @lex_state = EXPR_BEG
- end
- elsif @lex_state != EXPR_END and peek(0) =~ /[0-9]/
- throw :RET, identify_number
- else
- @lex_state = EXPR_BEG
- end
- Token(op)
+ if @lex_state == EXPR_ARG
+ if @space_seen and peek(0) =~ /[0-9]/
+ throw :RET, identify_number
+ else
+ @lex_state = EXPR_BEG
+ end
+ elsif @lex_state != EXPR_END and peek(0) =~ /[0-9]/
+ throw :RET, identify_number
+ else
+ @lex_state = EXPR_BEG
+ end
+ Token(op)
end
end
@@ -512,12 +509,12 @@ def lex_init()
|op, io|
@lex_state = EXPR_BEG
if peek(0) =~ /[0-9]/
- ungetc
- identify_number
+ ungetc
+ identify_number
else
- # for "obj.if" etc.
- @lex_state = EXPR_DOT
- Token(TkDOT)
+ # for "obj.if" etc.
+ @lex_state = EXPR_DOT
+ Token(TkDOT)
end
end
@@ -529,7 +526,7 @@ def lex_init()
lex_int2
end
-
+
def lex_int2
@OP.def_rules("]", "}", ")") do
|op, io|
@@ -542,11 +539,11 @@ def lex_int2
@OP.def_rule(":") do
|op, io|
if @lex_state == EXPR_END || peek(0) =~ /\s/
- @lex_state = EXPR_BEG
- Token(TkCOLON)
+ @lex_state = EXPR_BEG
+ Token(TkCOLON)
else
- @lex_state = EXPR_FNAME;
- Token(TkSYMBEG)
+ @lex_state = EXPR_FNAME;
+ Token(TkSYMBEG)
end
end
@@ -554,27 +551,27 @@ def lex_int2
|op, io|
# p @lex_state.id2name, @space_seen
if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
- @lex_state = EXPR_BEG
- Token(TkCOLON3)
+ @lex_state = EXPR_BEG
+ Token(TkCOLON3)
else
- @lex_state = EXPR_DOT
- Token(TkCOLON2)
+ @lex_state = EXPR_DOT
+ Token(TkCOLON2)
end
end
@OP.def_rule("/") do
|op, io|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
- identify_string(op)
+ identify_string(op)
elsif peek(0) == '='
- getc
- @lex_state = EXPR_BEG
- Token(TkOPASGN, "/") #/)
+ getc
+ @lex_state = EXPR_BEG
+ Token(TkOPASGN, "/") #/)
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
- identify_string(op)
- else
- @lex_state = EXPR_BEG
- Token("/") #/)
+ identify_string(op)
+ else
+ @lex_state = EXPR_BEG
+ Token("/") #/)
end
end
@@ -585,10 +582,10 @@ def lex_int2
end
# @OP.def_rules("^=") do
- # @lex_state = EXPR_BEG
- # Token(OP_ASGN, :^)
+ # @lex_state = EXPR_BEG
+ # Token(OP_ASGN, :^)
# end
-
+
@OP.def_rules(",") do
|op, io|
@lex_state = EXPR_BEG
@@ -598,10 +595,10 @@ def lex_int2
@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
+ until (@indent_stack.empty? ||
+ [TkLPAREN, TkLBRACK, TkLBRACE,
+ TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
+ @indent_stack.pop
end
Token(op)
end
@@ -617,16 +614,16 @@ def lex_int2
@lex_state = EXPR_BEG
Token("~")
end
-
+
@OP.def_rule("(") do
|op, io|
@indent += 1
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
- @lex_state = EXPR_BEG
- tk_c = TkfLPAREN
+ @lex_state = EXPR_BEG
+ tk_c = TkfLPAREN
else
- @lex_state = EXPR_BEG
- tk_c = TkLPAREN
+ @lex_state = EXPR_BEG
+ tk_c = TkLPAREN
end
@indent_stack.push tk_c
tk = Token(tk_c)
@@ -648,16 +645,16 @@ def lex_int2
|op, io|
@indent += 1
if @lex_state == EXPR_FNAME
- tk_c = TkfLBRACK
+ tk_c = TkfLBRACK
else
- if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
- tk_c = TkLBRACK
- elsif @lex_state == EXPR_ARG && @space_seen
- tk_c = TkLBRACK
- else
- tk_c = TkfLBRACK
- end
- @lex_state = EXPR_BEG
+ if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
+ tk_c = TkLBRACK
+ elsif @lex_state == EXPR_ARG && @space_seen
+ tk_c = TkLBRACK
+ else
+ tk_c = TkfLBRACK
+ end
+ @lex_state = EXPR_BEG
end
@indent_stack.push tk_c
Token(tk_c)
@@ -667,9 +664,9 @@ def lex_int2
|op, io|
@indent += 1
if @lex_state != EXPR_END && @lex_state != EXPR_ARG
- tk_c = TkLBRACE
+ tk_c = TkLBRACE
else
- tk_c = TkfLBRACE
+ tk_c = TkfLBRACE
end
@lex_state = EXPR_BEG
@indent_stack.push tk_c
@@ -679,27 +676,27 @@ def lex_int2
@OP.def_rule('\\') do
|op, io|
if getc == "\n"
- @space_seen = true
- @continue = true
- Token(TkSPACE)
+ @space_seen = true
+ @continue = true
+ Token(TkSPACE)
else
- ungetc
- Token("\\")
+ ungetc
+ Token("\\")
end
end
@OP.def_rule('%') do
|op, io|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
- identify_quotation
+ identify_quotation
elsif peek(0) == '='
- getc
- Token(TkOPASGN, :%)
+ getc
+ Token(TkOPASGN, :%)
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
- identify_quotation
+ identify_quotation
else
- @lex_state = EXPR_BEG
- Token("%") #))
+ @lex_state = EXPR_BEG
+ Token("%") #))
end
end
@@ -711,41 +708,41 @@ def lex_int2
@OP.def_rule('@') do
|op, io|
if peek(0) =~ /[\w_@]/
- ungetc
- identify_identifier
+ ungetc
+ identify_identifier
else
- Token("@")
+ Token("@")
end
end
- # @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
- # |op, io|
- # @indent += 1
- # @lex_state = EXPR_FNAME
- # # @lex_state = EXPR_END
- # # until @rests[0] == "\n" or @rests[0] == ";"
- # # rests.shift
- # # end
+ # @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
+ # |op, io|
+ # @indent += 1
+ # @lex_state = EXPR_FNAME
+ # # @lex_state = EXPR_END
+ # # until @rests[0] == "\n" or @rests[0] == ";"
+ # # rests.shift
+ # # end
# end
@OP.def_rule("") do
|op, io|
printf "MATCH: start %s: %s\n", op, io.inspect if RubyLex.debug?
if peek(0) =~ /[0-9]/
- t = identify_number
+ t = identify_number
elsif peek(0) =~ /[\w_]/
- t = identify_identifier
+ t = identify_identifier
end
printf "MATCH: end %s: %s\n", op, io.inspect if RubyLex.debug?
t
end
-
+
p @OP if RubyLex.debug?
end
-
+
def identify_gvar
@lex_state = EXPR_END
-
+
case ch = getc
when /[~_*$?!@\/\\;,=:<>".]/ #"
Token(TkGVAR, "$" + ch)
@@ -761,18 +758,18 @@ def identify_gvar
ungetc
ungetc
identify_identifier
- else
+ else
ungetc
Token("$")
end
end
-
+
def identify_identifier
token = ""
if peek(0) =~ /[$@]/
token.concat(c = getc)
if c == "@" and peek(0) == "@"
- token.concat getc
+ token.concat getc
end
end
@@ -781,7 +778,7 @@ def identify_identifier
token.concat ch
end
ungetc
-
+
if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
token.concat getc
end
@@ -799,67 +796,67 @@ def identify_identifier
@lex_state = EXPR_END
return Token(TkIVAR, token)
end
-
+
if @lex_state != EXPR_DOT
print token, "\n" if RubyLex.debug?
token_c, *trans = TkReading2Token[token]
if token_c
- # reserved word?
-
- if (@lex_state != EXPR_BEG &&
- @lex_state != EXPR_FNAME &&
- trans[1])
- # modifiers
- token_c = TkSymbol2Token[trans[1]]
- @lex_state = trans[0]
- else
- if @lex_state != EXPR_FNAME
- if ENINDENT_CLAUSE.include?(token)
- # check for ``class = val'' etc.
- 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
- @lex_state = EXPR_END
- end
- end
- return Token(token_c, token)
+ # reserved word?
+
+ if (@lex_state != EXPR_BEG &&
+ @lex_state != EXPR_FNAME &&
+ trans[1])
+ # modifiers
+ token_c = TkSymbol2Token[trans[1]]
+ @lex_state = trans[0]
+ else
+ if @lex_state != EXPR_FNAME
+ if ENINDENT_CLAUSE.include?(token)
+ # check for ``class = val'' etc.
+ 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
+ @lex_state = EXPR_END
+ end
+ end
+ return Token(token_c, token)
end
end
if @lex_state == EXPR_FNAME
@lex_state = EXPR_END
if peek(0) == '='
- token.concat getc
+ token.concat getc
end
elsif @lex_state == EXPR_BEG || @lex_state == EXPR_DOT
@lex_state = EXPR_ARG
@@ -887,13 +884,13 @@ def identify_here_document
lt = ch
quoted = ""
while (c = getc) && c != lt
- quoted.concat c
+ quoted.concat c
end
else
lt = '"'
quoted = ch.dup
while (c = getc) && c =~ /\w/
- quoted.concat c
+ quoted.concat c
end
ungetc
end
@@ -903,9 +900,9 @@ def identify_here_document
while ch = getc
reserve.push ch
if ch == "\\"
- reserve.push ch = getc
+ reserve.push ch = getc
elsif ch == "\n"
- break
+ break
end
end
@@ -913,7 +910,7 @@ def identify_here_document
while l = gets
l = l.sub(/(:?\r)?\n\z/, '')
if (indent ? l.strip : l) == quoted
- break
+ break
end
end
@@ -927,7 +924,7 @@ def identify_here_document
@lex_state = EXPR_END
Token(Ltype2Token[lt])
end
-
+
def identify_quotation
ch = getc
if lt = PERCENT_LTYPE[ch]
@@ -953,53 +950,53 @@ def identify_number
getc
case peek(0)
when /[xX]/
- ch = getc
- match = /[0-9a-fA-F_]/
+ ch = getc
+ match = /[0-9a-fA-F_]/
when /[bB]/
- ch = getc
- match = /[01_]/
+ ch = getc
+ match = /[01_]/
when /[oO]/
- ch = getc
- match = /[0-7_]/
+ ch = getc
+ match = /[0-7_]/
when /[dD]/
- ch = getc
- match = /[0-9_]/
+ ch = getc
+ match = /[0-9_]/
when /[0-7]/
- match = /[0-7_]/
+ match = /[0-7_]/
when /[89]/
- RubyLex.fail SyntaxError, "Illegal octal digit"
- else
- return Token(TkINTEGER)
+ RubyLex.fail SyntaxError, "Illegal octal digit"
+ else
+ return Token(TkINTEGER)
end
-
+
len0 = true
non_digit = false
while ch = getc
- if match =~ ch
- if ch == "_"
- if non_digit
- RubyLex.fail SyntaxError, "trailing `#{ch}' in number"
- else
- non_digit = ch
- end
- else
- non_digit = false
- len0 = false
- end
- else
- ungetc
- if len0
- RubyLex.fail SyntaxError, "numeric literal without digits"
- end
- if non_digit
- RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
- end
- break
- end
+ if match =~ ch
+ if ch == "_"
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{ch}' in number"
+ else
+ non_digit = ch
+ end
+ else
+ non_digit = false
+ len0 = false
+ end
+ else
+ ungetc
+ if len0
+ RubyLex.fail SyntaxError, "numeric literal without digits"
+ end
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ break
+ end
end
return Token(TkINTEGER)
end
-
+
type = TkINTEGER
allow_point = true
allow_e = true
@@ -1007,42 +1004,42 @@ def identify_number
while ch = getc
case ch
when /[0-9]/
- non_digit = false
+ non_digit = false
when "_"
- non_digit = ch
+ non_digit = ch
when allow_point && "."
- if non_digit
- RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
- end
- type = TkFLOAT
- if peek(0) !~ /[0-9]/
- type = TkINTEGER
- ungetc
- break
- end
- allow_point = false
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ type = TkFLOAT
+ if peek(0) !~ /[0-9]/
+ type = TkINTEGER
+ ungetc
+ break
+ end
+ allow_point = false
when allow_e && "e", allow_e && "E"
- if non_digit
- RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
- end
- type = TkFLOAT
- if peek(0) =~ /[+-]/
- getc
- end
- allow_e = false
- allow_point = false
- non_digit = ch
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ type = TkFLOAT
+ if peek(0) =~ /[+-]/
+ getc
+ end
+ allow_e = false
+ allow_point = false
+ non_digit = ch
else
- if non_digit
- RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
- end
- ungetc
- break
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
+ ungetc
+ break
end
end
Token(type)
end
-
+
def identify_string(ltype, quoted = ltype)
@ltype = ltype
@quoted = quoted
@@ -1050,30 +1047,30 @@ def identify_string(ltype, quoted = ltype)
begin
nest = 0
while ch = getc
- if @quoted == ch and nest == 0
- break
- elsif @ltype != "'" && @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
+ if @quoted == ch and nest == 0
+ break
+ elsif @ltype != "'" && @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|m|x|o|e|s|u|n/
- getc
- end
+ if peek(0) =~ /i|m|x|o|e|s|u|n/
+ getc
+ end
end
if subtype
- Token(DLtype2Token[ltype])
+ Token(DLtype2Token[ltype])
else
- Token(Ltype2Token[ltype])
+ Token(Ltype2Token[ltype])
end
ensure
@ltype = nil
@@ -1081,23 +1078,23 @@ def identify_string(ltype, quoted = ltype)
@lex_state = EXPR_END
end
end
-
+
def identify_comment
@ltype = "#"
while ch = getc
# if ch == "\\" #"
-# read_escape
+# read_escape
# end
if ch == "\n"
- @ltype = nil
- ungetc
- break
+ @ltype = nil
+ ungetc
+ break
end
end
return Token(TkCOMMENT)
end
-
+
def read_escape
case ch = getc
when "\n", "\r", "\f"
@@ -1105,45 +1102,45 @@ def read_escape
when /[0-7]/
ungetc ch
3.times do
- case ch = getc
- when /[0-7]/
- when nil
- break
- else
- ungetc
- break
- end
+ case ch = getc
+ when /[0-7]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
end
-
+
when "x"
2.times do
- case ch = getc
- when /[0-9a-fA-F]/
- when nil
- break
- else
- ungetc
- break
- end
+ case ch = getc
+ when /[0-9a-fA-F]/
+ when nil
+ break
+ else
+ ungetc
+ break
+ end
end
when "M"
if (ch = getc) != '-'
- ungetc
+ ungetc
else
- if (ch = getc) == "\\" #"
- read_escape
- end
+ if (ch = getc) == "\\" #"
+ read_escape
+ end
end
when "C", "c" #, "^"
if ch == "C" and (ch = getc) != "-"
- ungetc
+ ungetc
elsif (ch = getc) == "\\" #"
- read_escape
+ read_escape
end
else
- # other characters
+ # other characters
end
end
end

0 comments on commit efc94c3

Please sign in to comment.
Something went wrong with that request. Please try again.