2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
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"
|
|
|
|
|
2012-12-13 00:22:30 -05:00
|
|
|
# :stopdoc:
|
2000-05-12 05:07:57 -04:00
|
|
|
class RubyLex
|
|
|
|
|
|
|
|
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,
|
2014-08-08 21:36:49 -04:00
|
|
|
"key duplicate(token_n='%s', key='%s')")
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2012-12-27 09:48:42 -05:00
|
|
|
@post_symbeg = 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
|
2012-12-25 11:04:52 -05:00
|
|
|
if idx = @readed.rindex("\n")
|
|
|
|
@base_char_no = @readed.size - (idx + 1)
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
|
|
@rests.push nil unless buf_input
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
c = @rests.shift
|
|
|
|
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 =
|
2010-11-03 01:23:56 -04:00
|
|
|
@seek -= 1
|
2000-05-12 05:07:57 -04:00
|
|
|
if c == "\n"
|
2009-03-05 22:56:38 -05:00
|
|
|
@line_no -= 1
|
2012-12-25 11:04:52 -05:00
|
|
|
if idx = @readed.rindex("\n")
|
2014-08-08 21:36:49 -04:00
|
|
|
@char_no = idx + 1
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@char_no = @base_char_no + @readed.size
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2012-12-27 09:48:42 -05:00
|
|
|
@post_symbeg = 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
|
2014-08-08 21:36:49 -04:00
|
|
|
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"
|
2010-11-03 01:23:56 -04:00
|
|
|
@line.force_encoding(@io.encoding)
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def lex
|
|
|
|
until (((tk = token).kind_of?(TkNL) || tk.kind_of?(TkEND_OF_SCRIPT)) &&
|
2014-08-08 21:36:49 -04:00
|
|
|
!@continue or
|
|
|
|
tk.nil?)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
line = get_readed
|
|
|
|
if line == "" and tk.kind_of?(TkEND_OF_SCRIPT) || tk.nil?
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def token
|
|
|
|
@prev_seek = @seek
|
|
|
|
@prev_line_no = @line_no
|
|
|
|
@prev_char_no = @char_no
|
|
|
|
begin
|
|
|
|
begin
|
2014-08-08 21:36:49 -04:00
|
|
|
tk = @OP.match(self)
|
|
|
|
@space_seen = tk.kind_of?(TkSPACE)
|
|
|
|
@lex_state = EXPR_END if @post_symbeg && tk.kind_of?(TkOp)
|
|
|
|
@post_symbeg = tk.kind_of?(TkSYMBEG)
|
2000-05-12 05:07:57 -04:00
|
|
|
rescue SyntaxError
|
2014-08-08 21:36:49 -04:00
|
|
|
raise if @exception_on_syntax_error
|
|
|
|
tk = TkError.new(@seek, @line_no, @char_no)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end while @skip_space and tk.kind_of?(TkSPACE)
|
|
|
|
if @readed_auto_clean_up
|
|
|
|
get_readed
|
|
|
|
end
|
|
|
|
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",
|
2014-08-08 22:02:58 -04:00
|
|
|
"module", "unless", "until", "while", "begin"
|
2000-05-12 05:07:57 -04:00
|
|
|
]
|
2014-08-08 22:02:58 -04:00
|
|
|
DEINDENT_CLAUSE = ["end"
|
2000-05-12 05:07:57 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
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" => "]",
|
2012-12-23 01:25:22 -05:00
|
|
|
"i" => "]",
|
|
|
|
"I" => "]",
|
2003-08-04 23:08:16 -04:00
|
|
|
"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",
|
2014-08-08 21:36:49 -04: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/
|
2014-08-08 21:36:49 -04:00
|
|
|
until getc == "\n"; end
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
@continue = true
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@continue = false
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
until (@indent_stack.empty? ||
|
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
|
|
|
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("*", "**",
|
2014-08-08 21:36:49 -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
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_ARG
|
2002-07-09 07:17:17 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
2002-07-09 07:17:17 -04:00
|
|
|
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 &&
|
2014-08-08 21:36:49 -04:00
|
|
|
(@lex_state != EXPR_ARG || @space_seen)
|
|
|
|
c = peek(0)
|
2015-12-17 20:25:08 -05:00
|
|
|
if /\S/ =~ c && (/["'`]/ =~ c || /\w/ =~ c || c == "-" || c == "~")
|
2014-08-08 21:36:49 -04:00
|
|
|
tk = identify_here_document
|
|
|
|
end
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
unless tk
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_END
|
|
|
|
Token(op)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
identify_string(op)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules('?') do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_END
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkQUESTION)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
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("+=", "-=", "*=", "**=",
|
2014-08-08 21:36:49 -04:00
|
|
|
"&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
|
2000-05-12 05:07:57 -04:00
|
|
|
|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
|
2014-08-08 21:36:49 -04:00
|
|
|
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)
|
2000-05-12 05:07:57 -04:00
|
|
|
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]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ungetc
|
|
|
|
identify_number
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
# for "obj.if" etc.
|
|
|
|
@lex_state = EXPR_DOT
|
|
|
|
Token(TkDOT)
|
2000-05-12 05:07:57 -04:00
|
|
|
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/
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkCOLON)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_FNAME
|
|
|
|
Token(TkSYMBEG)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
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_BEG or @lex_state == EXPR_ARG && @space_seen
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkCOLON3)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_DOT
|
|
|
|
Token(TkCOLON2)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("/") do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
|
2014-08-08 21:36:49 -04:00
|
|
|
identify_string(op)
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif peek(0) == '='
|
2014-08-08 21:36:49 -04:00
|
|
|
getc
|
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token(TkOPASGN, "/") #/)
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
2014-08-08 21:36:49 -04:00
|
|
|
identify_string(op)
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("/") #/)
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
|
|
|
|
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? ||
|
2014-08-08 21:36:49 -04:00
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
|
|
|
TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
|
|
|
|
@indent_stack.pop
|
2002-07-09 07:17:17 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
tk_c = TkfLPAREN
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
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
|
2010-07-11 12:59:10 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
tk_c = TkfLBRACK
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
tk_c = TkLBRACE
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -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"
|
2014-08-08 21:36:49 -04:00
|
|
|
@space_seen = true
|
|
|
|
@continue = true
|
|
|
|
Token(TkSPACE)
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
read_escape
|
|
|
|
Token("\\")
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('%') do
|
|
|
|
|op, io|
|
|
|
|
if @lex_state == EXPR_BEG || @lex_state == EXPR_MID
|
2014-08-08 21:36:49 -04:00
|
|
|
identify_quotation
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif peek(0) == '='
|
2014-08-08 21:36:49 -04:00
|
|
|
getc
|
|
|
|
Token(TkOPASGN, :%)
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif @lex_state == EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
2014-08-08 21:36:49 -04:00
|
|
|
identify_quotation
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
@lex_state = EXPR_BEG
|
|
|
|
Token("%") #))
|
2000-05-12 05:07:57 -04:00
|
|
|
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|
|
2009-10-10 21:27:11 -04:00
|
|
|
if peek(0) =~ /[\w@]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ungetc
|
|
|
|
identify_identifier
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
Token("@")
|
2000-05-12 05:07:57 -04:00
|
|
|
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]/
|
2014-08-08 21:36:49 -04:00
|
|
|
t = identify_number
|
2010-04-17 13:15:20 -04:00
|
|
|
elsif peek(0) =~ /[^\x00-\/:-@\[-^`{-\x7F]/
|
2014-08-08 21:36:49 -04:00
|
|
|
t = identify_identifier
|
2000-05-12 05:07:57 -04:00
|
|
|
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) == "@"
|
2014-08-08 21:36:49 -04:00
|
|
|
token.concat getc
|
2002-07-09 07:17:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-17 13:15:20 -04:00
|
|
|
while (ch = getc) =~ /[^\x00-\/:-@\[-^`{-\x7F]/
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
# 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
|
|
|
|
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)
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @lex_state == EXPR_FNAME
|
|
|
|
@lex_state = EXPR_END
|
|
|
|
if peek(0) == '='
|
2014-08-08 21:36:49 -04:00
|
|
|
token.concat getc
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2015-12-17 20:25:08 -05:00
|
|
|
if ch == "-" || ch == "~"
|
2000-07-25 02:03:50 -04:00
|
|
|
ch = getc
|
|
|
|
indent = true
|
|
|
|
end
|
|
|
|
if /['"`]/ =~ ch
|
|
|
|
lt = ch
|
2000-05-12 05:07:57 -04:00
|
|
|
quoted = ""
|
|
|
|
while (c = getc) && c != lt
|
2014-08-08 21:36:49 -04:00
|
|
|
quoted.concat c
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
lt = '"'
|
|
|
|
quoted = ch.dup
|
|
|
|
while (c = getc) && c =~ /\w/
|
2014-08-08 21:36:49 -04:00
|
|
|
quoted.concat c
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
ungetc
|
|
|
|
end
|
|
|
|
|
|
|
|
ltback, @ltype = @ltype, lt
|
|
|
|
reserve = []
|
|
|
|
while ch = getc
|
|
|
|
reserve.push ch
|
|
|
|
if ch == "\\"
|
2014-08-08 21:36:49 -04:00
|
|
|
reserve.push ch = getc
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif ch == "\n"
|
2014-08-08 21:36:49 -04:00
|
|
|
break
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@here_header = false
|
2012-12-27 07:35:28 -05:00
|
|
|
|
|
|
|
line = ""
|
|
|
|
while ch = getc
|
|
|
|
if ch == "\n"
|
2014-08-08 21:36:49 -04:00
|
|
|
if line == quoted
|
|
|
|
break
|
|
|
|
end
|
|
|
|
line = ""
|
2012-12-27 07:35:28 -05:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
line.concat ch unless indent && line == "" && /\s/ =~ ch
|
|
|
|
if @ltype != "'" && ch == "#" && peek(0) == "{"
|
|
|
|
identify_string_dvar
|
|
|
|
end
|
2005-04-13 11:27:09 -04:00
|
|
|
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
|
|
|
|
@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]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[0-9a-fA-F_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[bB]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[01_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[oO]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[0-7_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[dD]/
|
2014-08-08 21:36:49 -04:00
|
|
|
ch = getc
|
|
|
|
match = /[0-9_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[0-7]/
|
2014-08-08 21:36:49 -04:00
|
|
|
match = /[0-7_]/
|
2005-06-30 06:11:14 -04:00
|
|
|
when /[89]/
|
2014-08-08 21:36:49 -04:00
|
|
|
RubyLex.fail SyntaxError, "Invalid octal digit"
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2014-08-08 21:36:49 -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
|
2014-08-08 21:36:49 -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
|
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
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]/
|
2014-08-08 21:36:49 -04:00
|
|
|
non_digit = false
|
2005-04-13 11:27:09 -04:00
|
|
|
when "_"
|
2014-08-08 21:36:49 -04:00
|
|
|
non_digit = ch
|
2000-05-12 05:07:57 -04:00
|
|
|
when allow_point && "."
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
when allow_e && "e", allow_e && "E"
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
if non_digit
|
|
|
|
RubyLex.fail SyntaxError, "trailing `#{non_digit}' in number"
|
|
|
|
end
|
|
|
|
ungetc
|
|
|
|
break
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
if @quoted == ch and nest == 0
|
|
|
|
break
|
|
|
|
elsif @ltype != "'" && ch == "#" && peek(0) == "{"
|
|
|
|
identify_string_dvar
|
|
|
|
elsif @ltype != "'" && @ltype != "]" && @ltype != ":" and ch == "#"
|
|
|
|
subtype = true
|
|
|
|
elsif ch == '\\' and @ltype == "'" #'
|
|
|
|
case ch = getc
|
|
|
|
when "\\", "\n", "'"
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
end
|
|
|
|
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
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
if @ltype == "/"
|
2010-11-03 01:23:56 -04:00
|
|
|
while /[imxoesun]/ =~ peek(0)
|
2014-08-08 21:36:49 -04:00
|
|
|
getc
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
if subtype
|
2014-08-08 21:36:49 -04:00
|
|
|
Token(DLtype2Token[ltype])
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
Token(Ltype2Token[ltype])
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2009-07-23 04:35:22 -04:00
|
|
|
loop do
|
2014-08-08 21:36:49 -04:00
|
|
|
@continue = false
|
|
|
|
prompt
|
|
|
|
tk = token
|
|
|
|
if @ltype or @continue or @indent >= 0
|
|
|
|
next
|
|
|
|
end
|
|
|
|
break if tk.kind_of?(TkRBRACE)
|
2009-07-23 04:35:22 -04:00
|
|
|
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
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
def identify_comment
|
|
|
|
@ltype = "#"
|
|
|
|
|
|
|
|
while ch = getc
|
|
|
|
if ch == "\n"
|
2014-08-08 21:36:49 -04:00
|
|
|
@ltype = nil
|
|
|
|
ungetc
|
|
|
|
break
|
2000-05-12 05:07:57 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
case ch = getc
|
|
|
|
when /[0-7]/
|
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2000-05-12 05:07:57 -04:00
|
|
|
when "x"
|
|
|
|
2.times do
|
2014-08-08 21:36:49 -04:00
|
|
|
case ch = getc
|
|
|
|
when /[0-9a-fA-F]/
|
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
when "M"
|
|
|
|
if (ch = getc) != '-'
|
2014-08-08 21:36:49 -04:00
|
|
|
ungetc
|
2000-05-12 05:07:57 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
if (ch = getc) == "\\" #"
|
|
|
|
read_escape
|
|
|
|
end
|
2000-05-12 05:07:57 -04:00
|
|
|
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) != "-"
|
2014-08-08 21:36:49 -04:00
|
|
|
ungetc
|
2000-05-12 05:07:57 -04:00
|
|
|
elsif (ch = getc) == "\\" #"
|
2014-08-08 21:36:49 -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
|
2012-12-13 00:22:30 -05:00
|
|
|
# :startdoc:
|