summaryrefslogtreecommitdiff
path: root/lib/irb/ruby-lex.rb
diff options
context:
space:
mode:
authorkeiju <keiju@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-04-19 19:24:59 +0000
committerkeiju <keiju@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2005-04-19 19:24:59 +0000
commitf9fb21448bc4c9b1d7e9fb135dc37593974a3498 (patch)
tree67e5782ca3ba385be9ecfe5ca6370c012fb36df6 /lib/irb/ruby-lex.rb
parent40906c5d5d09837d005a7503fcf8c3f96cee111d (diff)
* lib/irb.rb lib/irb/* doc/irb: IRB 0.9.5
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@8357 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/irb/ruby-lex.rb')
-rw-r--r--lib/irb/ruby-lex.rb84
1 files changed, 69 insertions, 15 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb
index e25af30546..4c51afbc28 100644
--- a/lib/irb/ruby-lex.rb
+++ b/lib/irb/ruby-lex.rb
@@ -1,9 +1,9 @@
#
# irb/ruby-lex.rb - ruby lexcal analizer
-# $Release Version: 0.9$
+# $Release Version: 0.9.5$
# $Revision$
# $Date$
-# by Keiju ISHITSUKA(keiju@ishitsuka.com)
+# by Keiju ISHITSUKA(keiju@ruby-lang.org)
#
# --
#
@@ -51,13 +51,18 @@ class RubyLex
@indent = 0
@indent_stack = []
+ @lex_state = EXPR_BEG
+ @space_seen = false
+ @here_header = false
+
+ @continue = false
+ @line = ""
@skip_space = false
@readed_auto_clean_up = false
@exception_on_syntax_error = true
+
@prompt = nil
- @here_header = nil
- @lex_state = nil
end
attr_accessor :skip_space
@@ -95,7 +100,8 @@ class RubyLex
def getc
while @rests.empty?
- return nil unless buf_input
+# return nil unless buf_input
+ @rests.push nil unless buf_input
end
c = @rests.shift
if @here_header
@@ -116,9 +122,10 @@ class RubyLex
def gets
l = ""
while c = getc
- l.concat c
+ l.concat(c)
break if c == "\n"
end
+ return nil if l == "" and c.nil?
l
end
@@ -186,7 +193,7 @@ class RubyLex
end
private :buf_input
- def set_prompt(p=nil, &block)
+ def set_prompt(p = nil, &block)
p = block if block_given?
if p.respond_to?(:call)
@prompt = p
@@ -280,7 +287,7 @@ class RubyLex
tk = @OP.match(self)
@space_seen = tk.kind_of?(TkSPACE)
rescue SyntaxError
- abort if @exception_on_syntax_error
+ raise if @exception_on_syntax_error
tk = TkError.new(@seek, @line_no, @char_no)
end
end while @skip_space and tk.kind_of?(TkSPACE)
@@ -330,7 +337,7 @@ class RubyLex
}
def lex_init()
- @OP = SLex.new
+ @OP = IRB::SLex.new
@OP.def_rules("\0", "\004", "\032") do
Token(TkEND_OF_SCRIPT)
end
@@ -429,6 +436,7 @@ class RubyLex
@OP.def_rules("`") do
|op, io|
if @lex_state == EXPR_FNAME
+ @lex_state = EXPR_END
Token(op)
else
identify_string(op)
@@ -472,11 +480,13 @@ class RubyLex
@OP.def_rule("+@", proc{@lex_state == EXPR_FNAME}) do
|op, io|
+ @lex_state = EXPR_ARG
Token(op)
end
@OP.def_rule("-@", proc{@lex_state == EXPR_FNAME}) do
|op, io|
+ @lex_state = EXPR_ARG
Token(op)
end
@@ -616,10 +626,12 @@ class RubyLex
end
@OP.def_rule("[]", proc{@lex_state == EXPR_FNAME}) do
+ @lex_state = EXPR_ARG
Token("[]")
end
@OP.def_rule("[]=", proc{@lex_state == EXPR_FNAME}) do
+ @lex_state = EXPR_ARG
Token("[]=")
end
@@ -790,14 +802,13 @@ class RubyLex
else
if @lex_state != EXPR_FNAME
if ENINDENT_CLAUSE.include?(token)
- # check for ``class = val''.
+ # 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*(([+-\/*&\|^]|<<|>>|\|\||\&\&)=|\&\&|\|\|)/)
-# valid = false if peek_match?(/^\s*(([+-\/*&\|^]|<<|>>|\|\||\&\&)?=|\&\&|\|\|)/)
when "do"
valid = false if peek_match?(/^\s*([+-\/*]?=|\*|<|>|\&)/)
when *ENINDENT_CLAUSE
@@ -885,7 +896,11 @@ class RubyLex
end
@here_header = false
- while (l = gets.chomp) && (indent ? l.strip : l) != quoted
+ while l = gets
+ l = l.sub(/(:?\r)?\n\z/, '')
+ if (indent ? l.strip : l) == quoted
+ break
+ end
end
@here_header = true
@@ -920,19 +935,43 @@ class RubyLex
def identify_number
@lex_state = EXPR_END
- if ch = getc
+ if peek(0) == "0" && peek(1) !~ /[.eE]/
+ getc
if /[xX]/ =~ peek(0)
ch = getc
match = /[0-9a-fA-F_]/
elsif /[bB]/ =~ peek(0)
ch = getc
match = /[01_]/
+ elsif /[oO]/ =~ peek(0)
+ ch = getc
+ match = /[0-7_]/
else
match = /[0-7_]/
end
+
+ len0 = true
+ non_digit = false
while ch = getc
- if ch !~ match
+ 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
@@ -942,24 +981,39 @@ class RubyLex
type = TkINTEGER
allow_point = true
allow_e = true
+ non_digit = false
while ch = getc
case ch
- when /[0-9_]/
+ when /[0-9]/
+ non_digit = false
+ when "_"
+ 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
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
else
+ if non_digit
+ RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
+ end
ungetc
break
end