2000-05-12 05:07:57 -04:00
|
|
|
#
|
2008-06-04 05:37:38 -04:00
|
|
|
# irb/ruby-lex.rb - ruby lexcal analyzer
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2000-05-12 05:07:57 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2000-05-12 05:07:57 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
require "irb/slex"
|
|
|
|
require "irb/ruby-token"
|
|
|
|
|
|
|
|
class RubyLex
|
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
|
|
|
|
extend Exception2MessageMapper
|
|
|
|
def_exception(:AlreadyDefinedToken, "Already defined token(%s)")
|
|
|
|
def_exception(:TkReading2TokenNoKey, "key nothing(key='%s')")
|
|
|
|
def_exception(:TkSymbol2TokenNoKey, "key nothing(key='%s')")
|
2009-03-05 22:56:38 -05:00
|
|
|
def_exception(:TkReading2TokenDuplicateError,
|
2000-05-12 05:07:57 -04:00
|
|
|
"key duplicate(token_n='%s', key='%s')")
|
|
|
|
def_exception(:SyntaxError, "%s")
|
2001-05-08 05:28:19 -04:00
|
|
|
|
|
|
|
def_exception(:TerminateLineInput, "Terminate Line Input")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
include RubyToken
|
|
|
|
|
|
|
|
class << self
|
2001-02-02 06:38:20 -05:00
|
|
|
attr_accessor :debug_level
|
2000-05-12 05:07:57 -04:00
|
|
|
def debug?
|
|
|
|
@debug_level > 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@debug_level = 0
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
lex_init
|
|
|
|
set_input(STDIN)
|
|
|
|
|
|
|
|
@seek = 0
|
|
|
|
@exp_line_no = @line_no = 1
|
|
|
|
@base_char_no = 0
|
|
|
|
@char_no = 0
|
|
|
|
@rests = []
|
|
|
|
@readed = []
|
|
|
|
@here_readed = []
|
|
|
|
|
|
|
|
@indent = 0
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack = []
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
@space_seen = false
|
|
|
|
@here_header = false
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
@continue = false
|
|
|
|
@line = ""
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
@skip_space = false
|
|
|
|
@readed_auto_clean_up = false
|
|
|
|
@exception_on_syntax_error = true
|
2005-04-13 11:27:09 -04:00
|
|
|
|
2004-11-03 06:34:57 -05:00
|
|
|
@prompt = nil
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2001-02-02 06:38:20 -05:00
|
|
|
attr_accessor :skip_space
|
|
|
|
attr_accessor :readed_auto_clean_up
|
|
|
|
attr_accessor :exception_on_syntax_error
|
2000-05-12 05:07:57 -04:00
|
|
|
|
2001-02-02 06:38:20 -05:00
|
|
|
attr_reader :seek
|
|
|
|
attr_reader :char_no
|
|
|
|
attr_reader :line_no
|
|
|
|
attr_reader :indent
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
# io functions
|
2003-06-10 01:36:35 -04:00
|
|
|
def set_input(io, p = nil, &block)
|
2000-05-12 05:07:57 -04:00
|
|
|
@io = io
|
2003-06-10 01:22:17 -04:00
|
|
|
if p.respond_to?(:call)
|
2000-05-12 05:07:57 -04:00
|
|
|
@input = p
|
2003-06-10 01:36:35 -04:00
|
|
|
elsif block_given?
|
|
|
|
@input = block
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2003-06-16 03:14:50 -04:00
|
|
|
@input = Proc.new{@io.gets}
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_readed
|
|
|
|
if idx = @readed.reverse.index("\n")
|
|
|
|
@base_char_no = idx
|
|
|
|
else
|
|
|
|
@base_char_no += @readed.size
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
readed = @readed.join("")
|
|
|
|
@readed = []
|
|
|
|
readed
|
|
|
|
end
|
|
|
|
|
|
|
|
def getc
|
|
|
|
while @rests.empty?
|
2005-04-14 05:58:18 -04:00
|
|
|
# return nil unless buf_input
|
|
|
|
@rests.push nil unless buf_input
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
c = @rests.shift
|
2006-09-01 00:15:45 -04:00
|
|
|
return if c == nil
|
2000-05-12 05:07:57 -04:00
|
|
|
if @here_header
|
|
|
|
@here_readed.push c
|
|
|
|
else
|
|
|
|
@readed.push c
|
|
|
|
end
|
|
|
|
@seek += 1
|
|
|
|
if c == "\n"
|
2009-03-05 22:56:38 -05:00
|
|
|
@line_no += 1
|
2000-05-12 05:07:57 -04:00
|
|
|
@char_no = 0
|
|
|
|
else
|
|
|
|
@char_no += 1
|
|
|
|
end
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets
|
|
|
|
l = ""
|
|
|
|
while c = getc
|
2005-04-14 05:58:18 -04:00
|
|
|
l.concat(c)
|
2000-05-12 05:07:57 -04:00
|
|
|
break if c == "\n"
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
return nil if l == "" and c.nil?
|
2000-05-12 05:07:57 -04:00
|
|
|
l
|
|
|
|
end
|
|
|
|
|
|
|
|
def eof?
|
|
|
|
@io.eof?
|
|
|
|
end
|
|
|
|
|
|
|
|
def getc_of_rests
|
|
|
|
if @rests.empty?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ungetc(c = nil)
|
|
|
|
if @here_readed.empty?
|
|
|
|
c2 = @readed.pop
|
|
|
|
else
|
|
|
|
c2 = @here_readed.pop
|
|
|
|
end
|
|
|
|
c = c2 unless c
|
2009-03-05 22:56:38 -05:00
|
|
|
@rests.unshift c #c =
|
2000-05-12 05:07:57 -04:00
|
|
|
@seek -= 1
|
|
|
|
if c == "\n"
|
2009-03-05 22:56:38 -05:00
|
|
|
@line_no -= 1
|
2000-05-12 05:07:57 -04:00
|
|
|
if idx = @readed.reverse.index("\n")
|
|
|
|
@char_no = @readed.size - idx
|
|
|
|
else
|
|
|
|
@char_no = @base_char_no + @readed.size
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@char_no -= 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def peek_equal?(str)
|
|
|
|
chrs = str.split(//)
|
|
|
|
until @rests.size >= chrs.size
|
|
|
|
return false unless buf_input
|
|
|
|
end
|
|
|
|
@rests[0, chrs.size] == chrs
|
|
|
|
end
|
|
|
|
|
|
|
|
def peek_match?(regexp)
|
|
|
|
while @rests.empty?
|
|
|
|
return false unless buf_input
|
|
|
|
end
|
|
|
|
regexp =~ @rests.join("")
|
|
|
|
end
|
|
|
|
|
|
|
|
def peek(i = 0)
|
|
|
|
while @rests.size <= i
|
|
|
|
return nil unless buf_input
|
|
|
|
end
|
|
|
|
@rests[i]
|
|
|
|
end
|
|
|
|
|
|
|
|
def buf_input
|
|
|
|
prompt
|
|
|
|
line = @input.call
|
|
|
|
return nil unless line
|
2008-01-27 02:43:31 -05:00
|
|
|
@rests.concat line.chars.to_a
|
2000-05-12 05:07:57 -04:00
|
|
|
true
|
|
|
|
end
|
|
|
|
private :buf_input
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
def set_prompt(p = nil, &block)
|
2003-06-10 01:36:35 -04:00
|
|
|
p = block if block_given?
|
2003-06-10 01:22:17 -04:00
|
|
|
if p.respond_to?(:call)
|
2000-05-12 05:07:57 -04:00
|
|
|
@prompt = p
|
|
|
|
else
|
2003-06-16 03:14:50 -04:00
|
|
|
@prompt = Proc.new{print p}
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prompt
|
|
|
|
if @prompt
|
|
|
|
@prompt.call(@ltype, @indent, @continue, @line_no)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_input
|
|
|
|
@ltype = nil
|
|
|
|
@quoted = nil
|
|
|
|
@indent = 0
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack = []
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
@space_seen = false
|
|
|
|
@here_header = false
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-02-02 06:38:20 -05:00
|
|
|
@continue = false
|
2001-04-30 13:54:55 -04:00
|
|
|
prompt
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
@line = ""
|
|
|
|
@exp_line_no = @line_no
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def each_top_level_statement
|
|
|
|
initialize_input
|
2001-05-08 05:28:19 -04:00
|
|
|
catch(:TERM_INPUT) do
|
|
|
|
loop do
|
|
|
|
begin
|
|
|
|
@continue = false
|
|
|
|
prompt
|
|
|
|
unless l = lex
|
|
|
|
throw :TERM_INPUT if @line == ''
|
|
|
|
else
|
|
|
|
@line.concat l
|
|
|
|
if @ltype or @continue or @indent > 0
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if @line != "\n"
|
2008-12-18 08:09:26 -05:00
|
|
|
@line.force_encoding(@io.encoding)
|
2001-05-08 05:28:19 -04:00
|
|
|
yield @line, @exp_line_no
|
|
|
|
end
|
|
|
|
break unless l
|
|
|
|
@line = ''
|
|
|
|
@exp_line_no = @line_no
|
|
|
|
|
|
|
|
@indent = 0
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack = []
|
2001-05-08 05:28:19 -04:00
|
|
|
prompt
|
|
|
|
rescue TerminateLineInput
|
|
|
|
initialize_input
|
|
|
|
prompt
|
|
|
|
get_readed
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lex
|
|
|
|
until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
|
|
|
|
!@continue or
|
|
|
|
tk.nil?)
|
2001-04-30 13:54:55 -04:00
|
|
|
#p tk
|
2002-07-09 07:17:17 -04:00
|
|
|
#p @lex_state
|
2001-04-30 13:54:55 -04:00
|
|
|
#p self
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
line = get_readed
|
|
|
|
# print self.inspect
|
|
|
|
if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def token
|
|
|
|
# require "tracer"
|
|
|
|
# Tracer.on
|
|
|
|
@prev_seek = @seek
|
|
|
|
@prev_line_no = @line_no
|
|
|
|
@prev_char_no = @char_no
|
|
|
|
begin
|
|
|
|
begin
|
|
|
|
tk = @OP.match(self)
|
|
|
|
@space_seen = tk.kind_of?(TkSPACE)
|
|
|
|
rescue SyntaxError
|
2005-04-13 11:27:09 -04:00
|
|
|
raise if @exception_on_syntax_error
|
2000-05-12 05:07:57 -04:00
|
|
|
tk = TkError.new(@seek, @line_no, @char_no)
|
|
|
|
end
|
|
|
|
end while @skip_space and tk.kind_of?(TkSPACE)
|
|
|
|
if @readed_auto_clean_up
|
|
|
|
get_readed
|
|
|
|
end
|
|
|
|
# Tracer.off
|
|
|
|
tk
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
ENINDENT_CLAUSE = [
|
|
|
|
"case", "class", "def", "do", "for", "if",
|
|
|
|
"module", "unless", "until", "while", "begin" #, "when"
|
|
|
|
]
|
|
|
|
DEINDENT_CLAUSE = ["end" #, "when"
|
|
|
|
]
|
|
|
|
|
|
|
|
PERCENT_LTYPE = {
|
|
|
|
"q" => "\'",
|
|
|
|
"Q" => "\"",
|
|
|
|
"x" => "\`",
|
2006-02-28 08:47:48 -05:00
|
|
|
"r" => "/",
|
2003-08-04 22:23:07 -04:00
|
|
|
"w" => "]",
|
2003-08-04 23:08:16 -04:00
|
|
|
"W" => "]",
|
|
|
|
"s" => ":"
|
2000-05-12 05:07:57 -04:00
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
PERCENT_PAREN = {
|
|
|
|
"{" => "}",
|
|
|
|
"[" => "]",
|
|
|
|
"<" => ">",
|
|
|
|
"(" => ")"
|
|
|
|
}
|
|
|
|
|
|
|
|
Ltype2Token = {
|
|
|
|
"\'" => TkSTRING,
|
|
|
|
"\"" => TkSTRING,
|
|
|
|
"\`" => TkXSTRING,
|
2006-02-28 08:47:48 -05:00
|
|
|
"/" => TkREGEXP,
|
2003-08-04 23:08:16 -04:00
|
|
|
"]" => TkDSTRING,
|
|
|
|
":" => TkSYMBOL
|
2000-05-12 05:07:57 -04:00
|
|
|
}
|
|
|
|
DLtype2Token = {
|
|
|
|
"\"" => TkDSTRING,
|
|
|
|
"\`" => TkDXSTRING,
|
2006-02-28 08:47:48 -05:00
|
|
|
"/" => TkDREGEXP,
|
2000-05-12 05:07:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
def lex_init()
|
2005-04-13 11:27:09 -04:00
|
|
|
@OP = IRB::SLex.new
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rules("\0", "\004", "\032") do |op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
Token(TkEND_OF_SCRIPT)
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rules(" ", "\t", "\f", "\r", "\13") do |op, io|
|
2001-02-02 06:38:20 -05:00
|
|
|
@space_seen = true
|
2000-05-12 05:07:57 -04:00
|
|
|
while getc =~ /[ \t\f\r\13]/; end
|
|
|
|
ungetc
|
|
|
|
Token(TkSPACE)
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("#") do |op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
identify_comment
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("=begin",
|
2009-03-05 22:56:38 -05:00
|
|
|
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
|
2000-05-12 05:07:57 -04:00
|
|
|
|op, io|
|
|
|
|
@ltype = "="
|
|
|
|
until getc == "\n"; end
|
|
|
|
until peek_equal?("=end") && peek(4) =~ /\s/
|
|
|
|
until getc == "\n"; end
|
|
|
|
end
|
2001-04-30 13:54:55 -04:00
|
|
|
gets
|
2000-05-12 05:07:57 -04:00
|
|
|
@ltype = nil
|
|
|
|
Token(TkRD_COMMENT)
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("\n") do |op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
print "\\n\n" if RubyLex.debug?
|
|
|
|
case @lex_state
|
|
|
|
when EXPR_BEG, EXPR_FNAME, EXPR_DOT
|
2001-02-02 06:38:20 -05:00
|
|
|
@continue = true
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2001-02-02 06:38:20 -05:00
|
|
|
@continue = false
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
2009-03-05 22:56:38 -05:00
|
|
|
until (@indent_stack.empty? ||
|
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
2002-07-09 07:17:17 -04:00
|
|
|
TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
|
|
|
|
@indent_stack.pop
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
@here_header = false
|
|
|
|
@here_readed = []
|
|
|
|
Token(TkNL)
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
@OP.def_rules("*", "**",
|
|
|
|
"=", "==", "===",
|
|
|
|
"=~", "<=>",
|
2000-05-12 05:07:57 -04:00
|
|
|
"<", "<=",
|
2009-04-03 01:04:02 -04:00
|
|
|
">", ">=", ">>",
|
|
|
|
"!", "!=", "!~") do
|
2000-05-12 05:07:57 -04:00
|
|
|
|op, io|
|
2002-07-09 07:17:17 -04:00
|
|
|
case @lex_state
|
|
|
|
when EXPR_FNAME, EXPR_DOT
|
|
|
|
@lex_state = EXPR_ARG
|
|
|
|
else
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
end
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
@OP.def_rules("<<") do
|
|
|
|
|op, io|
|
2002-07-09 07:17:17 -04:00
|
|
|
tk = nil
|
2000-05-12 05:07:57 -04:00
|
|
|
if @lex_state != EXPR_END && @lex_state != EXPR_CLASS &&
|
|
|
|
(@lex_state != EXPR_ARG || @space_seen)
|
|
|
|
c = peek(0)
|
2009-08-16 08:32:35 -04:00
|
|
|
if /\S/ =~ c && (/["'`]/ =~ c || /\w/ =~ c || c == "-")
|
2000-07-25 02:03:50 -04:00
|
|
|
tk = identify_here_document
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
unless tk
|
|
|
|
tk = Token(op)
|
|
|
|
case @lex_state
|
|
|
|
when EXPR_FNAME, EXPR_DOT
|
|
|
|
@lex_state = EXPR_ARG
|
|
|
|
else
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
tk
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("'", '"') do
|
|
|
|
|op, io|
|
|
|
|
identify_string(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("`") do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_FNAME
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_END
|
2000-05-12 05:07:57 -04:00
|
|
|
Token(op)
|
|
|
|
else
|
|
|
|
identify_string(op)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules('?') do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_END
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkQUESTION)
|
|
|
|
else
|
|
|
|
ch = getc
|
2002-07-09 07:17:17 -04:00
|
|
|
if @lex_state == EXPR_ARG && ch =~ /\s/
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
|
|
|
@lex_state = EXPR_BEG;
|
|
|
|
Token(TkQUESTION)
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
if (ch == '\\')
|
2000-05-12 05:07:57 -04:00
|
|
|
read_escape
|
|
|
|
end
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
Token(TkINTEGER)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("&", "&&", "|", "||") do
|
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(op)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
@OP.def_rules("+=", "-=", "*=", "**=",
|
2000-05-12 05:07:57 -04:00
|
|
|
"&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
|
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
op =~ /^(.*)=$/
|
|
|
|
Token(TkOPASGN, $1)
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("+@", proc{|op, io| @lex_state == EXPR_FNAME}) do
|
2002-07-09 07:17:17 -04:00
|
|
|
|op, io|
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_ARG
|
2002-07-09 07:17:17 -04:00
|
|
|
Token(op)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("-@", proc{|op, io| @lex_state == EXPR_FNAME}) do
|
2002-07-09 07:17:17 -04:00
|
|
|
|op, io|
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_ARG
|
2002-07-09 07:17:17 -04:00
|
|
|
Token(op)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule(".") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
if peek(0) =~ /[0-9]/
|
|
|
|
ungetc
|
|
|
|
identify_number
|
|
|
|
else
|
2001-05-03 04:56:49 -04:00
|
|
|
# for "obj.if" etc.
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_DOT
|
|
|
|
Token(TkDOT)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("..", "...") do
|
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
lex_int2
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def lex_int2
|
|
|
|
@OP.def_rules("]", "}", ")") do
|
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
@indent -= 1
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack.pop
|
2000-05-12 05:07:57 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule(":") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
if @lex_state == EXPR_END || peek(0) =~ /\s/
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkCOLON)
|
|
|
|
else
|
|
|
|
@lex_state = EXPR_FNAME;
|
|
|
|
Token(TkSYMBEG)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("::") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
# p @lex_state.id2name, @space_seen
|
|
|
|
if @lex_state == EXPR_BEG or @lex_state == EXPR_ARG && @space_seen
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkCOLON3)
|
|
|
|
else
|
|
|
|
@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)
|
|
|
|
elsif peek(0) == '='
|
|
|
|
getc
|
|
|
|
@lex_state = EXPR_BEG
|
2002-07-09 07:17:17 -04:00
|
|
|
Token(TkOPASGN, "/") #/)
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
|
|
|
identify_string(op)
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("/") #/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("^") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("^")
|
|
|
|
end
|
|
|
|
|
|
|
|
# @OP.def_rules("^=") do
|
|
|
|
# @lex_state = EXPR_BEG
|
|
|
|
# Token(OP_ASGN, :^)
|
|
|
|
# end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
@OP.def_rules(",") do
|
2000-05-12 05:07:57 -04:00
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
@OP.def_rules(";") do
|
|
|
|
|op, io|
|
|
|
|
@lex_state = EXPR_BEG
|
2009-03-05 22:56:38 -05:00
|
|
|
until (@indent_stack.empty? ||
|
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
2002-07-09 07:17:17 -04:00
|
|
|
TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
|
|
|
|
@indent_stack.pop
|
|
|
|
end
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
@OP.def_rule("~") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("~")
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("~@", proc{|op, io| @lex_state == EXPR_FNAME}) do
|
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("~")
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
@OP.def_rule("(") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@indent += 1
|
|
|
|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
|
|
|
|
@lex_state = EXPR_BEG
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkfLPAREN
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
|
|
|
@lex_state = EXPR_BEG
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkLPAREN
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack.push tk_c
|
|
|
|
tk = Token(tk_c)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("[]", proc{|op, io| @lex_state == EXPR_FNAME}) do
|
|
|
|
|op, io|
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_ARG
|
2000-05-12 05:07:57 -04:00
|
|
|
Token("[]")
|
|
|
|
end
|
|
|
|
|
2006-08-04 06:06:17 -04:00
|
|
|
@OP.def_rule("[]=", proc{|op, io| @lex_state == EXPR_FNAME}) do
|
|
|
|
|op, io|
|
2005-04-13 11:27:09 -04:00
|
|
|
@lex_state = EXPR_ARG
|
2000-05-12 05:07:57 -04:00
|
|
|
Token("[]=")
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("[") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@indent += 1
|
|
|
|
if @lex_state == EXPR_FNAME
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkfLBRACK
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
|
|
|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkLBRACK
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif @lex_state == EXPR_ARG && @space_seen
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkLBRACK
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkfLBRACK
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack.push tk_c
|
|
|
|
Token(tk_c)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("{") do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
@indent += 1
|
|
|
|
if @lex_state != EXPR_END && @lex_state != EXPR_ARG
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkLBRACE
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2002-07-09 07:17:17 -04:00
|
|
|
tk_c = TkfLBRACE
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
@lex_state = EXPR_BEG
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack.push tk_c
|
|
|
|
Token(tk_c)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('\\') do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
if getc == "\n"
|
|
|
|
@space_seen = true
|
|
|
|
@continue = true
|
|
|
|
Token(TkSPACE)
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
Token("\\")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('%') do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
|
|
|
|
identify_quotation
|
|
|
|
elsif peek(0) == '='
|
|
|
|
getc
|
2001-01-18 03:43:14 -05:00
|
|
|
Token(TkOPASGN, :%)
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
|
|
|
identify_quotation
|
|
|
|
else
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("%") #))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('$') do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2000-05-12 05:07:57 -04:00
|
|
|
identify_gvar
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('@') do
|
2006-08-04 06:06:17 -04:00
|
|
|
|op, io|
|
2002-07-09 07:17:17 -04:00
|
|
|
if peek(0) =~ /[\w_@]/
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
|
|
|
identify_identifier
|
|
|
|
else
|
|
|
|
Token("@")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
|
2000-05-12 05:07:57 -04:00
|
|
|
# |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
|
2009-08-16 08:32:35 -04:00
|
|
|
elsif peek(0) =~ /\w/
|
2000-05-12 05:07:57 -04:00
|
|
|
t = identify_identifier
|
|
|
|
end
|
|
|
|
printf "MATCH: end %s: %s\n", op, io.inspect if RubyLex.debug?
|
|
|
|
t
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
p @OP if RubyLex.debug?
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_gvar
|
|
|
|
@lex_state = EXPR_END
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
case ch = getc
|
2000-05-13 13:22:08 -04:00
|
|
|
when /[~_*$?!@\/\\;,=:<>".]/ #"
|
2000-05-12 05:07:57 -04:00
|
|
|
Token(TkGVAR, "$" + ch)
|
|
|
|
when "-"
|
|
|
|
Token(TkGVAR, "$-" + getc)
|
|
|
|
when "&", "`", "'", "+"
|
|
|
|
Token(TkBACK_REF, "$"+ch)
|
|
|
|
when /[1-9]/
|
|
|
|
while getc =~ /[0-9]/; end
|
|
|
|
ungetc
|
|
|
|
Token(TkNTH_REF)
|
|
|
|
when /\w/
|
|
|
|
ungetc
|
|
|
|
ungetc
|
|
|
|
identify_identifier
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
|
|
|
Token("$")
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_identifier
|
|
|
|
token = ""
|
2002-07-09 07:17:17 -04:00
|
|
|
if peek(0) =~ /[$@]/
|
2002-07-29 02:14:10 -04:00
|
|
|
token.concat(c = getc)
|
2002-07-09 07:17:17 -04:00
|
|
|
if c == "@" and peek(0) == "@"
|
|
|
|
token.concat getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
while (ch = getc) =~ /\w|_/
|
|
|
|
print ":", ch, ":" if RubyLex.debug?
|
|
|
|
token.concat ch
|
|
|
|
end
|
|
|
|
ungetc
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2002-07-09 07:17:17 -04:00
|
|
|
if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
|
2000-05-12 05:07:57 -04:00
|
|
|
token.concat getc
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
|
2001-04-30 13:54:55 -04:00
|
|
|
# almost fix token
|
2000-05-12 05:07:57 -04:00
|
|
|
|
|
|
|
case token
|
|
|
|
when /^\$/
|
|
|
|
return Token(TkGVAR, token)
|
2002-07-09 07:17:17 -04:00
|
|
|
when /^\@\@/
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
# p Token(TkCVAR, token)
|
|
|
|
return Token(TkCVAR, token)
|
2000-05-12 05:07:57 -04:00
|
|
|
when /^\@/
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
return Token(TkIVAR, token)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
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)
|
2005-04-13 11:27:09 -04:00
|
|
|
# check for ``class = val'' etc.
|
2002-07-09 07:17:17 -04:00
|
|
|
valid = true
|
|
|
|
case token
|
|
|
|
when "class"
|
2003-10-04 13:51:11 -04:00
|
|
|
valid = false unless peek_match?(/^\s*(<<|\w|::)/)
|
2002-07-09 07:17:17 -04:00
|
|
|
when "def"
|
2009-04-03 01:04:02 -04:00
|
|
|
valid = false if peek_match?(/^\s*(([+\-\/*&\|^]|<<|>>|\|\||\&\&)=|\&\&|\|\|)/)
|
2002-07-09 07:17:17 -04:00
|
|
|
when "do"
|
2009-04-03 01:04:02 -04:00
|
|
|
valid = false if peek_match?(/^\s*([+\-\/*]?=|\*|<|>|\&)/)
|
2002-07-09 07:17:17 -04:00
|
|
|
when *ENINDENT_CLAUSE
|
2009-04-03 01:04:02 -04:00
|
|
|
valid = false if peek_match?(/^\s*([+\-\/*]?=|\*|<|>|\&|\|)/)
|
2002-07-09 07:17:17 -04:00
|
|
|
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
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif DEINDENT_CLAUSE.include?(token)
|
|
|
|
@indent -= 1
|
2002-07-09 07:17:17 -04:00
|
|
|
@indent_stack.pop
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
|
|
|
end
|
|
|
|
elsif @lex_state == EXPR_BEG || @lex_state == EXPR_DOT
|
|
|
|
@lex_state = EXPR_ARG
|
|
|
|
else
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
end
|
|
|
|
|
|
|
|
if token[0, 1] =~ /[A-Z]/
|
|
|
|
return Token(TkCONSTANT, token)
|
|
|
|
elsif token[token.size - 1, 1] =~ /[!?]/
|
|
|
|
return Token(TkFID, token)
|
|
|
|
else
|
|
|
|
return Token(TkIDENTIFIER, token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_here_document
|
|
|
|
ch = getc
|
2001-04-30 13:54:55 -04:00
|
|
|
# if lt = PERCENT_LTYPE[ch]
|
2000-07-25 02:03:50 -04:00
|
|
|
if ch == "-"
|
|
|
|
ch = getc
|
|
|
|
indent = true
|
|
|
|
end
|
|
|
|
if /['"`]/ =~ ch
|
|
|
|
lt = ch
|
2000-05-12 05:07:57 -04:00
|
|
|
quoted = ""
|
|
|
|
while (c = getc) && c != lt
|
|
|
|
quoted.concat c
|
|
|
|
end
|
|
|
|
else
|
|
|
|
lt = '"'
|
|
|
|
quoted = ch.dup
|
|
|
|
while (c = getc) && c =~ /\w/
|
|
|
|
quoted.concat c
|
|
|
|
end
|
|
|
|
ungetc
|
|
|
|
end
|
|
|
|
|
|
|
|
ltback, @ltype = @ltype, lt
|
|
|
|
reserve = []
|
|
|
|
while ch = getc
|
|
|
|
reserve.push ch
|
|
|
|
if ch == "\\"
|
|
|
|
reserve.push ch = getc
|
|
|
|
elsif ch == "\n"
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@here_header = false
|
2005-04-13 11:27:09 -04:00
|
|
|
while l = gets
|
|
|
|
l = l.sub(/(:?\r)?\n\z/, '')
|
|
|
|
if (indent ? l.strip : l) == quoted
|
|
|
|
break
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@here_header = true
|
|
|
|
@here_readed.concat reserve
|
|
|
|
while ch = reserve.pop
|
|
|
|
ungetc ch
|
|
|
|
end
|
|
|
|
|
|
|
|
@ltype = ltback
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
Token(Ltype2Token[lt])
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_quotation
|
|
|
|
ch = getc
|
|
|
|
if lt = PERCENT_LTYPE[ch]
|
|
|
|
ch = getc
|
|
|
|
elsif ch =~ /\W/
|
|
|
|
lt = "\""
|
|
|
|
else
|
|
|
|
RubyLex.fail SyntaxError, "unknown type of %string"
|
|
|
|
end
|
|
|
|
# if ch !~ /\W/
|
|
|
|
# ungetc
|
|
|
|
# next
|
|
|
|
# end
|
|
|
|
#@ltype = lt
|
|
|
|
@quoted = ch unless @quoted = PERCENT_PAREN[ch]
|
|
|
|
identify_string(lt, @quoted)
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_number
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
|
2005-04-13 11:27:09 -04:00
|
|
|
if peek(0) == "0" && peek(1) !~ /[.eE]/
|
|
|
|
getc
|
2005-06-30 06:11:14 -04:00
|
|
|
case peek(0)
|
|
|
|
when /[xX]/
|
2000-05-12 05:07:57 -04:00
|
|
|
ch = getc
|
2002-07-09 07:17:17 -04:00
|
|
|
match = /[0-9a-fA-F_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[bB]/
|
2002-07-09 07:17:17 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[01_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[oO]/
|
2005-04-13 11:27:09 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[0-7_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[dD]/
|
|
|
|
ch = getc
|
|
|
|
match = /[0-9_]/
|
|
|
|
when /[0-7]/
|
2000-05-12 05:07:57 -04:00
|
|
|
match = /[0-7_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[89]/
|
2007-12-27 03:58:03 -05:00
|
|
|
RubyLex.fail SyntaxError, "Invalid octal digit"
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2005-06-30 06:11:14 -04:00
|
|
|
return Token(TkINTEGER)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-06-30 06:11:14 -04:00
|
|
|
len0 = true
|
|
|
|
non_digit = false
|
2000-05-12 05:07:57 -04:00
|
|
|
while ch = getc
|
2005-04-13 11:27:09 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
2005-04-13 11:27:09 -04:00
|
|
|
if len0
|
|
|
|
RubyLex.fail SyntaxError, "numeric literal without digits"
|
|
|
|
end
|
|
|
|
if non_digit
|
|
|
|
RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return Token(TkINTEGER)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
type = TkINTEGER
|
2001-02-02 06:38:20 -05:00
|
|
|
allow_point = true
|
|
|
|
allow_e = true
|
2005-04-13 11:27:09 -04:00
|
|
|
non_digit = false
|
2000-05-12 05:07:57 -04:00
|
|
|
while ch = getc
|
|
|
|
case ch
|
2005-04-13 11:27:09 -04:00
|
|
|
when /[0-9]/
|
|
|
|
non_digit = false
|
|
|
|
when "_"
|
|
|
|
non_digit = ch
|
2000-05-12 05:07:57 -04:00
|
|
|
when allow_point && "."
|
2005-04-13 11:27:09 -04:00
|
|
|
if non_digit
|
|
|
|
RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
type = TkFLOAT
|
|
|
|
if peek(0) !~ /[0-9]/
|
2005-04-13 11:27:09 -04:00
|
|
|
type = TkINTEGER
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
allow_point = false
|
|
|
|
when allow_e && "e", allow_e && "E"
|
2005-04-13 11:27:09 -04:00
|
|
|
if non_digit
|
|
|
|
RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
type = TkFLOAT
|
2003-03-29 01:39:50 -05:00
|
|
|
if peek(0) =~ /[+-]/
|
2000-05-12 05:07:57 -04:00
|
|
|
getc
|
|
|
|
end
|
|
|
|
allow_e = false
|
|
|
|
allow_point = false
|
2005-04-13 11:27:09 -04:00
|
|
|
non_digit = ch
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2005-04-13 11:27:09 -04:00
|
|
|
if non_digit
|
|
|
|
RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
Token(type)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_string(ltype, quoted = ltype)
|
|
|
|
@ltype = ltype
|
|
|
|
@quoted = quoted
|
|
|
|
subtype = nil
|
|
|
|
begin
|
2002-07-09 07:17:17 -04:00
|
|
|
nest = 0
|
|
|
|
while ch = getc
|
|
|
|
if @quoted == ch and nest == 0
|
2000-05-12 05:07:57 -04:00
|
|
|
break
|
2009-07-23 04:35:22 -04:00
|
|
|
elsif ch == "#" and peek(0) == "{"
|
|
|
|
identify_string_dvar
|
2004-07-17 02:28:10 -04:00
|
|
|
elsif @ltype != "'" && @ltype != "]" && @ltype != ":" and ch == "#"
|
2000-05-12 05:07:57 -04:00
|
|
|
subtype = true
|
2007-08-06 11:11:33 -04:00
|
|
|
elsif ch == '\\' and @ltype == "'" #'
|
|
|
|
case ch = getc
|
|
|
|
when "\\", "\n", "'"
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif ch == '\\' #'
|
|
|
|
read_escape
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
if PERCENT_PAREN.values.include?(@quoted)
|
2002-07-09 07:17:17 -04:00
|
|
|
if PERCENT_PAREN[ch] == @quoted
|
|
|
|
nest += 1
|
|
|
|
elsif ch == @quoted
|
|
|
|
nest -= 1
|
|
|
|
end
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
if @ltype == "/"
|
2002-07-09 07:17:17 -04:00
|
|
|
if peek(0) =~ /i|m|x|o|e|s|u|n/
|
2000-05-12 05:07:57 -04:00
|
|
|
getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if subtype
|
|
|
|
Token(DLtype2Token[ltype])
|
|
|
|
else
|
|
|
|
Token(Ltype2Token[ltype])
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@ltype = nil
|
|
|
|
@quoted = nil
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-07-23 04:35:22 -04:00
|
|
|
def identify_string_dvar
|
|
|
|
begin
|
|
|
|
getc
|
|
|
|
|
|
|
|
reserve_continue = @continue
|
|
|
|
reserve_ltype = @ltype
|
|
|
|
reserve_indent = @indent
|
|
|
|
reserve_indent_stack = @indent_stack
|
|
|
|
reserve_state = @lex_state
|
|
|
|
reserve_quoted = @quoted
|
|
|
|
|
|
|
|
@ltype = nil
|
|
|
|
@quoted = nil
|
|
|
|
@indent = 0
|
|
|
|
@indent_stack = []
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
|
|
|
|
loop do
|
|
|
|
@continue = false
|
|
|
|
prompt
|
|
|
|
tk = token
|
|
|
|
if @ltype or @continue or @indent > 0
|
|
|
|
next
|
|
|
|
end
|
|
|
|
break if tk.kind_of?(TkRBRACE)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@continue = reserve_continue
|
|
|
|
@ltype = reserve_ltype
|
|
|
|
@indent = reserve_indent
|
|
|
|
@indent_stack = reserve_indent_stack
|
|
|
|
@lex_state = reserve_state
|
|
|
|
@quoted = reserve_quoted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_comment
|
|
|
|
@ltype = "#"
|
|
|
|
|
|
|
|
while ch = getc
|
2002-07-09 07:17:17 -04:00
|
|
|
# if ch == "\\" #"
|
|
|
|
# read_escape
|
|
|
|
# end
|
2000-05-12 05:07:57 -04:00
|
|
|
if ch == "\n"
|
|
|
|
@ltype = nil
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return Token(TkCOMMENT)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def read_escape
|
|
|
|
case ch = getc
|
|
|
|
when "\n", "\r", "\f"
|
2006-02-28 08:47:48 -05:00
|
|
|
when "\\", "n", "t", "r", "f", "v", "a", "e", "b", "s" #"
|
2000-05-12 05:07:57 -04:00
|
|
|
when /[0-7]/
|
|
|
|
ungetc ch
|
|
|
|
3.times do
|
|
|
|
case ch = getc
|
|
|
|
when /[0-7]/
|
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
when "x"
|
|
|
|
2.times do
|
|
|
|
case ch = getc
|
|
|
|
when /[0-9a-fA-F]/
|
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
when "M"
|
|
|
|
if (ch = getc) != '-'
|
|
|
|
ungetc
|
|
|
|
else
|
|
|
|
if (ch = getc) == "\\" #"
|
2001-06-27 11:35:04 -04:00
|
|
|
read_escape
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-28 08:47:48 -05:00
|
|
|
when "C", "c" #, "^"
|
2000-05-12 05:07:57 -04:00
|
|
|
if ch == "C" and (ch = getc) != "-"
|
|
|
|
ungetc
|
|
|
|
elsif (ch = getc) == "\\" #"
|
2001-06-27 11:35:04 -04:00
|
|
|
read_escape
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
# other characters
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|