2011-08-23 19:53:49 -04:00
|
|
|
# coding: US-ASCII
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
#--
|
|
|
|
# irb/ruby-lex.rb - ruby lexcal analyzer
|
|
|
|
# $Release Version: 0.9.5$
|
|
|
|
# $Revision: 17979 $
|
|
|
|
# $Date: 2008-07-09 10:17:05 -0700 (Wed, 09 Jul 2008) $
|
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
|
|
|
#
|
|
|
|
#++
|
|
|
|
|
|
|
|
require "e2mmap"
|
|
|
|
require "irb/slex"
|
|
|
|
require "stringio"
|
|
|
|
|
|
|
|
##
|
|
|
|
# Ruby lexer adapted from irb.
|
|
|
|
#
|
|
|
|
# The internals are not documented because they are scary.
|
|
|
|
|
|
|
|
class RDoc::RubyLex
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Raised upon invalid input
|
|
|
|
|
|
|
|
class Error < RDoc::Error
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# :stopdoc:
|
|
|
|
|
|
|
|
extend Exception2MessageMapper
|
|
|
|
|
|
|
|
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(:SyntaxError, "%s")
|
|
|
|
|
|
|
|
def_exception(:TerminateLineInput, "Terminate Line Input")
|
|
|
|
|
|
|
|
include RDoc::RubyToken
|
|
|
|
include IRB
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
attr_accessor :continue
|
|
|
|
attr_accessor :lex_state
|
2010-04-01 03:45:16 -04:00
|
|
|
attr_reader :reader
|
|
|
|
|
|
|
|
class << self
|
|
|
|
attr_accessor :debug_level
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.debug?
|
|
|
|
@debug_level > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
self.debug_level = 0
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
##
|
|
|
|
# Returns an Array of +ruby+ tokens. See ::new for a description of
|
|
|
|
# +options+.
|
|
|
|
|
|
|
|
def self.tokenize ruby, options
|
|
|
|
tokens = []
|
|
|
|
|
|
|
|
scanner = RDoc::RubyLex.new ruby, options
|
|
|
|
scanner.exception_on_syntax_error = true
|
|
|
|
|
|
|
|
while token = scanner.token do
|
|
|
|
tokens << token
|
|
|
|
end
|
|
|
|
|
|
|
|
tokens
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new lexer for +content+. +options+ is an RDoc::Options, only
|
|
|
|
# +tab_width is used.
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def initialize(content, options)
|
|
|
|
lex_init
|
|
|
|
|
|
|
|
if /\t/ =~ content then
|
|
|
|
tab_width = options.tab_width
|
|
|
|
content = content.split(/\n/).map do |line|
|
|
|
|
1 while line.gsub!(/\t+/) {
|
|
|
|
' ' * (tab_width*$&.length - $`.length % tab_width)
|
|
|
|
} && $~
|
|
|
|
line
|
|
|
|
end.join("\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
content << "\n" unless content[-1, 1] == "\n"
|
|
|
|
|
|
|
|
set_input StringIO.new content
|
|
|
|
|
|
|
|
@base_char_no = 0
|
|
|
|
@char_no = 0
|
|
|
|
@exp_line_no = @line_no = 1
|
|
|
|
@here_readed = []
|
|
|
|
@readed = []
|
|
|
|
@rests = []
|
|
|
|
@seek = 0
|
|
|
|
|
|
|
|
@here_header = false
|
|
|
|
@indent = 0
|
|
|
|
@indent_stack = []
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
@space_seen = false
|
|
|
|
|
|
|
|
@continue = false
|
|
|
|
@line = ""
|
|
|
|
|
|
|
|
@skip_space = false
|
|
|
|
@readed_auto_clean_up = false
|
|
|
|
@exception_on_syntax_error = true
|
|
|
|
|
|
|
|
@prompt = nil
|
|
|
|
@prev_seek = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@ltype = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
# :stopdoc:
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def inspect # :nodoc:
|
2010-12-19 22:22:49 -05:00
|
|
|
"#<%s:0x%x pos %d lex_state %p space_seen %p>" % [
|
2010-04-01 03:45:16 -04:00
|
|
|
self.class, object_id,
|
2010-12-19 22:22:49 -05:00
|
|
|
@io.pos, @lex_state, @space_seen,
|
2010-04-01 03:45:16 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :skip_space
|
|
|
|
attr_accessor :readed_auto_clean_up
|
|
|
|
attr_accessor :exception_on_syntax_error
|
|
|
|
|
|
|
|
attr_reader :seek
|
|
|
|
attr_reader :char_no
|
|
|
|
attr_reader :line_no
|
|
|
|
attr_reader :indent
|
|
|
|
|
|
|
|
# io functions
|
|
|
|
def set_input(io, p = nil, &block)
|
|
|
|
@io = io
|
|
|
|
if p.respond_to?(:call)
|
|
|
|
@input = p
|
|
|
|
elsif block_given?
|
|
|
|
@input = block
|
|
|
|
else
|
|
|
|
@input = Proc.new{@io.gets}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_readed
|
2012-11-26 23:28:14 -05:00
|
|
|
if idx = @readed.rindex("\n")
|
|
|
|
@base_char_no = @readed.size - (idx + 1)
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
@base_char_no += @readed.size
|
|
|
|
end
|
|
|
|
|
|
|
|
readed = @readed.join("")
|
|
|
|
@readed = []
|
|
|
|
readed
|
|
|
|
end
|
|
|
|
|
|
|
|
def getc
|
|
|
|
while @rests.empty?
|
|
|
|
# return nil unless buf_input
|
|
|
|
@rests.push nil unless buf_input
|
|
|
|
end
|
|
|
|
c = @rests.shift
|
|
|
|
if @here_header
|
|
|
|
@here_readed.push c
|
|
|
|
else
|
|
|
|
@readed.push c
|
|
|
|
end
|
|
|
|
@seek += 1
|
|
|
|
if c == "\n"
|
2010-04-02 00:40:47 -04:00
|
|
|
@line_no += 1
|
2010-04-01 03:45:16 -04:00
|
|
|
@char_no = 0
|
|
|
|
else
|
|
|
|
@char_no += 1
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
c
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets
|
|
|
|
l = ""
|
|
|
|
while c = getc
|
|
|
|
l.concat(c)
|
|
|
|
break if c == "\n"
|
|
|
|
end
|
|
|
|
return nil if l == "" and c.nil?
|
|
|
|
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
|
2010-04-02 00:40:47 -04:00
|
|
|
@rests.unshift c #c =
|
2010-04-01 03:45:16 -04:00
|
|
|
@seek -= 1
|
|
|
|
if c == "\n"
|
2010-04-02 00:40:47 -04:00
|
|
|
@line_no -= 1
|
2012-11-26 23:28:14 -05:00
|
|
|
if idx = @readed.rindex("\n")
|
|
|
|
@char_no = idx + 1
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
|
|
|
@rests.concat line.split(//)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
private :buf_input
|
|
|
|
|
|
|
|
def set_prompt(p = nil, &block)
|
|
|
|
p = block if block_given?
|
|
|
|
if p.respond_to?(:call)
|
|
|
|
@prompt = p
|
|
|
|
else
|
|
|
|
@prompt = Proc.new{print p}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prompt
|
|
|
|
if @prompt
|
|
|
|
@prompt.call(@ltype, @indent, @continue, @line_no)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize_input
|
|
|
|
@ltype = nil
|
|
|
|
@quoted = nil
|
|
|
|
@indent = 0
|
|
|
|
@indent_stack = []
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
@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
|
|
|
|
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
|
|
|
|
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 => e
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "syntax error: #{e.message}" if
|
2010-04-01 03:45:16 -04:00
|
|
|
@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
|
|
|
|
get_readed
|
|
|
|
end
|
|
|
|
# Tracer.off
|
|
|
|
tk
|
|
|
|
end
|
|
|
|
|
|
|
|
ENINDENT_CLAUSE = [
|
|
|
|
"case", "class", "def", "do", "for", "if",
|
|
|
|
"module", "unless", "until", "while", "begin" #, "when"
|
|
|
|
]
|
|
|
|
|
|
|
|
DEINDENT_CLAUSE = ["end" #, "when"
|
|
|
|
]
|
|
|
|
|
|
|
|
PERCENT_LTYPE = {
|
|
|
|
"q" => "\'",
|
|
|
|
"Q" => "\"",
|
|
|
|
"x" => "\`",
|
|
|
|
"r" => "/",
|
|
|
|
"w" => "]",
|
|
|
|
"W" => "]",
|
|
|
|
"s" => ":"
|
|
|
|
}
|
|
|
|
|
|
|
|
PERCENT_PAREN = {
|
|
|
|
"{" => "}",
|
|
|
|
"[" => "]",
|
|
|
|
"<" => ">",
|
|
|
|
"(" => ")"
|
|
|
|
}
|
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
PERCENT_PAREN_REV = PERCENT_PAREN.invert
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
Ltype2Token = {
|
|
|
|
"\'" => TkSTRING,
|
|
|
|
"\"" => TkSTRING,
|
|
|
|
"\`" => TkXSTRING,
|
|
|
|
"/" => TkREGEXP,
|
|
|
|
"]" => TkDSTRING,
|
|
|
|
":" => TkSYMBOL
|
|
|
|
}
|
|
|
|
DLtype2Token = {
|
|
|
|
"\"" => TkDSTRING,
|
|
|
|
"\`" => TkDXSTRING,
|
|
|
|
"/" => TkDREGEXP,
|
|
|
|
}
|
|
|
|
|
|
|
|
def lex_init()
|
|
|
|
@OP = IRB::SLex.new
|
|
|
|
@OP.def_rules("\0", "\004", "\032") do |op, io|
|
2012-11-26 23:28:14 -05:00
|
|
|
Token(TkEND_OF_SCRIPT, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules(" ", "\t", "\f", "\r", "\13") do |op, io|
|
|
|
|
@space_seen = true
|
|
|
|
str = op
|
|
|
|
while (ch = getc) =~ /[ \t\f\r\13]/ do
|
|
|
|
str << ch
|
|
|
|
end
|
|
|
|
ungetc
|
|
|
|
Token TkSPACE, str
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("#") do |op, io|
|
|
|
|
identify_comment
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("=begin",
|
2010-04-02 00:40:47 -04:00
|
|
|
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
|
|
|
@ltype = "="
|
|
|
|
res = ''
|
2014-09-04 21:41:25 -04:00
|
|
|
nil until getc == "\n"
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2011-05-13 20:39:16 -04:00
|
|
|
until ( peek_equal?("=end") && peek(4) =~ /\s/ ) do
|
|
|
|
(ch = getc)
|
|
|
|
res << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
gets # consume =end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@ltype = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
Token(TkRD_COMMENT, res)
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("\n") do |op, io|
|
|
|
|
print "\\n\n" if RDoc::RubyLex.debug?
|
|
|
|
case @lex_state
|
2013-09-18 19:33:36 -04:00
|
|
|
when :EXPR_BEG, :EXPR_FNAME, :EXPR_DOT
|
2010-04-01 03:45:16 -04:00
|
|
|
@continue = true
|
|
|
|
else
|
|
|
|
@continue = false
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-02 00:40:47 -04:00
|
|
|
until (@indent_stack.empty? ||
|
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
2010-04-01 03:45:16 -04:00
|
|
|
TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
|
|
|
|
@indent_stack.pop
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@here_header = false
|
|
|
|
@here_readed = []
|
|
|
|
Token(TkNL)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("*", "**",
|
2010-04-02 00:40:47 -04:00
|
|
|
"=", "==", "===",
|
2010-04-01 03:45:16 -04:00
|
|
|
"=~", "<=>",
|
|
|
|
"<", "<=",
|
|
|
|
">", ">=", ">>") do
|
|
|
|
|op, io|
|
|
|
|
case @lex_state
|
2013-09-18 19:33:36 -04:00
|
|
|
when :EXPR_FNAME, :EXPR_DOT
|
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("!", "!=", "!~") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("<<") do
|
|
|
|
|op, io|
|
|
|
|
tk = nil
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state != :EXPR_END && @lex_state != :EXPR_CLASS &&
|
|
|
|
(@lex_state != :EXPR_ARG || @space_seen)
|
2010-04-01 03:45:16 -04:00
|
|
|
c = peek(0)
|
2010-04-05 23:01:52 -04:00
|
|
|
if /\S/ =~ c && (/["'`]/ =~ c || /\w/ =~ c || c == "-")
|
2010-04-01 03:45:16 -04:00
|
|
|
tk = identify_here_document
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless tk
|
|
|
|
tk = Token(op)
|
|
|
|
case @lex_state
|
2013-09-18 19:33:36 -04:00
|
|
|
when :EXPR_FNAME, :EXPR_DOT
|
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
tk
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("'", '"') do
|
|
|
|
|op, io|
|
|
|
|
identify_string(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("`") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_FNAME
|
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
else
|
|
|
|
identify_string(op)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules('?') do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_END
|
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkQUESTION)
|
|
|
|
else
|
|
|
|
ch = getc
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_ARG && ch =~ /\s/
|
2010-04-01 03:45:16 -04:00
|
|
|
ungetc
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG;
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkQUESTION)
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
|
|
|
Token(TkCHAR, "?#{ch}")
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("&", "&&", "|", "||") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("+=", "-=", "*=", "**=",
|
|
|
|
"&=", "|=", "^=", "<<=", ">>=", "||=", "&&=") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
op =~ /^(.*)=$/
|
|
|
|
Token(TkOPASGN, $1)
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@OP.def_rule("+@", proc{|op, io| @lex_state == :EXPR_FNAME}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@OP.def_rule("-@", proc{|op, io| @lex_state == :EXPR_FNAME}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("+", "-") do
|
|
|
|
|op, io|
|
|
|
|
catch(:RET) do
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
if @space_seen and peek(0) =~ /[0-9]/
|
2010-12-28 17:08:56 -05:00
|
|
|
throw :RET, identify_number(op)
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
elsif @lex_state != :EXPR_END and peek(0) =~ /[0-9]/
|
2010-12-28 17:08:56 -05:00
|
|
|
throw :RET, identify_number(op)
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule(".") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
if peek(0) =~ /[0-9]/
|
|
|
|
ungetc
|
|
|
|
identify_number
|
|
|
|
else
|
|
|
|
# for "obj.if" etc.
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_DOT
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkDOT)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("..", "...") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
lex_int2
|
|
|
|
end
|
|
|
|
|
|
|
|
def lex_int2
|
|
|
|
@OP.def_rules("]", "}", ")") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
@indent -= 1
|
|
|
|
@indent_stack.pop
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule(":") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_END || peek(0) =~ /\s/
|
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkCOLON)
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_FNAME;
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkSYMBEG)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("::") do
|
|
|
|
|op, io|
|
|
|
|
# p @lex_state.id2name, @space_seen
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_BEG or @lex_state == :EXPR_ARG && @space_seen
|
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkCOLON3)
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_DOT
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkCOLON2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("/") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_BEG || @lex_state == :EXPR_MID
|
2010-04-01 03:45:16 -04:00
|
|
|
identify_string(op)
|
|
|
|
elsif peek(0) == '='
|
|
|
|
getc
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(TkOPASGN, "/") #/)
|
2013-09-18 19:33:36 -04:00
|
|
|
elsif @lex_state == :EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
2010-04-01 03:45:16 -04:00
|
|
|
identify_string(op)
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("/") #/)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules("^") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("^")
|
|
|
|
end
|
|
|
|
|
|
|
|
# @OP.def_rules("^=") do
|
2013-09-18 19:33:36 -04:00
|
|
|
# @lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
# Token(OP_ASGN, :^)
|
|
|
|
# end
|
|
|
|
|
|
|
|
@OP.def_rules(",") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rules(";") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-02 00:40:47 -04:00
|
|
|
until (@indent_stack.empty? ||
|
|
|
|
[TkLPAREN, TkLBRACK, TkLBRACE,
|
2010-04-01 03:45:16 -04:00
|
|
|
TkfLPAREN, TkfLBRACK, TkfLBRACE].include?(@indent_stack.last))
|
|
|
|
@indent_stack.pop
|
|
|
|
end
|
|
|
|
Token(op)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("~") do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("~")
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@OP.def_rule("~@", proc{|op, io| @lex_state == :EXPR_FNAME}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("~")
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("(") do
|
|
|
|
|op, io|
|
|
|
|
@indent += 1
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_BEG || @lex_state == :EXPR_MID
|
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkfLPAREN
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkLPAREN
|
|
|
|
end
|
|
|
|
@indent_stack.push tk_c
|
2010-12-19 22:22:49 -05:00
|
|
|
Token tk_c
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@OP.def_rule("[]", proc{|op, io| @lex_state == :EXPR_FNAME}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("[]")
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@OP.def_rule("[]=", proc{|op, io| @lex_state == :EXPR_FNAME}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("[]=")
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("[") do
|
|
|
|
|op, io|
|
|
|
|
@indent += 1
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_FNAME
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkfLBRACK
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_BEG || @lex_state == :EXPR_MID
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkLBRACK
|
2013-09-18 19:33:36 -04:00
|
|
|
elsif @lex_state == :EXPR_ARG && @space_seen
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkLBRACK
|
|
|
|
else
|
|
|
|
tk_c = TkfLBRACK
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
@indent_stack.push tk_c
|
|
|
|
Token(tk_c)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("{") do
|
|
|
|
|op, io|
|
|
|
|
@indent += 1
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state != :EXPR_END && @lex_state != :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
tk_c = TkLBRACE
|
|
|
|
else
|
|
|
|
tk_c = TkfLBRACE
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
@indent_stack.push tk_c
|
|
|
|
Token(tk_c)
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('\\') do
|
|
|
|
|op, io|
|
|
|
|
if getc == "\n"
|
|
|
|
@space_seen = true
|
|
|
|
@continue = true
|
|
|
|
Token(TkSPACE)
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
Token("\\")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('%') do
|
|
|
|
|op, io|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_BEG || @lex_state == :EXPR_MID
|
2010-04-01 03:45:16 -04:00
|
|
|
identify_quotation
|
|
|
|
elsif peek(0) == '='
|
|
|
|
getc
|
|
|
|
Token(TkOPASGN, :%)
|
2013-09-18 19:33:36 -04:00
|
|
|
elsif @lex_state == :EXPR_ARG and @space_seen and peek(0) !~ /\s/
|
2010-04-01 03:45:16 -04:00
|
|
|
identify_quotation
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_BEG
|
2010-04-01 03:45:16 -04:00
|
|
|
Token("%") #))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('$') do
|
|
|
|
|op, io|
|
|
|
|
identify_gvar
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule('@') do
|
|
|
|
|op, io|
|
|
|
|
if peek(0) =~ /[\w@]/
|
|
|
|
ungetc
|
|
|
|
identify_identifier
|
|
|
|
else
|
|
|
|
Token("@")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-02 00:40:47 -04:00
|
|
|
# @OP.def_rule("def", proc{|op, io| /\s/ =~ io.peek(0)}) do
|
2010-04-01 03:45:16 -04:00
|
|
|
# |op, io|
|
|
|
|
# @indent += 1
|
2013-09-18 19:33:36 -04:00
|
|
|
# @lex_state = :EXPR_FNAME
|
|
|
|
# # @lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
# # until @rests[0] == "\n" or @rests[0] == ";"
|
|
|
|
# # rests.shift
|
|
|
|
# # end
|
|
|
|
# end
|
|
|
|
|
|
|
|
@OP.def_rule("_") do
|
2013-09-18 19:33:36 -04:00
|
|
|
if peek_match?(/_END__/) and @lex_state == :EXPR_BEG then
|
2012-11-26 23:28:14 -05:00
|
|
|
6.times { getc }
|
|
|
|
Token(TkEND_OF_SCRIPT, '__END__')
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
identify_identifier
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@OP.def_rule("") do
|
|
|
|
|op, io|
|
|
|
|
printf "MATCH: start %s: %s\n", op, io.inspect if RDoc::RubyLex.debug?
|
|
|
|
if peek(0) =~ /[0-9]/
|
|
|
|
t = identify_number
|
|
|
|
else
|
|
|
|
t = identify_identifier
|
|
|
|
end
|
|
|
|
printf "MATCH: end %s: %s\n", op, io.inspect if RDoc::RubyLex.debug?
|
|
|
|
t
|
|
|
|
end
|
|
|
|
|
|
|
|
p @OP if RDoc::RubyLex.debug?
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_gvar
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
case ch = getc
|
|
|
|
when /[~_*$?!@\/\\;,=:<>".]/ #"
|
|
|
|
Token(TkGVAR, "$" + ch)
|
|
|
|
when "-"
|
|
|
|
Token(TkGVAR, "$-" + getc)
|
|
|
|
when "&", "`", "'", "+"
|
|
|
|
Token(TkBACK_REF, "$"+ch)
|
|
|
|
when /[1-9]/
|
|
|
|
ref = ch
|
|
|
|
while (ch = getc) =~ /[0-9]/ do ref << ch end
|
|
|
|
ungetc
|
|
|
|
Token(TkNTH_REF, "$#{ref}")
|
|
|
|
when /\w/
|
|
|
|
ungetc
|
|
|
|
ungetc
|
|
|
|
identify_identifier
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
Token("$")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
IDENT_RE = if defined? Encoding then
|
2012-12-08 16:19:22 -05:00
|
|
|
eval '/[\w\u{0080}-\u{FFFFF}]/u' # 1.8 can't parse \u{}
|
2010-12-19 22:22:49 -05:00
|
|
|
else
|
|
|
|
/[\w\x80-\xFF]/
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def identify_identifier
|
|
|
|
token = ""
|
|
|
|
if peek(0) =~ /[$@]/
|
|
|
|
token.concat(c = getc)
|
|
|
|
if c == "@" and peek(0) == "@"
|
|
|
|
token.concat getc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
while (ch = getc) =~ IDENT_RE do
|
2010-04-01 03:45:16 -04:00
|
|
|
print " :#{ch}: " if RDoc::RubyLex.debug?
|
|
|
|
token.concat ch
|
|
|
|
end
|
|
|
|
|
|
|
|
ungetc
|
|
|
|
|
|
|
|
if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
|
|
|
|
token.concat getc
|
|
|
|
end
|
|
|
|
|
|
|
|
# almost fix token
|
|
|
|
|
|
|
|
case token
|
|
|
|
when /^\$/
|
|
|
|
return Token(TkGVAR, token)
|
|
|
|
when /^\@\@/
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
# p Token(TkCVAR, token)
|
|
|
|
return Token(TkCVAR, token)
|
|
|
|
when /^\@/
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
return Token(TkIVAR, token)
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state != :EXPR_DOT
|
2010-04-01 03:45:16 -04:00
|
|
|
print token, "\n" if RDoc::RubyLex.debug?
|
|
|
|
|
|
|
|
token_c, *trans = TkReading2Token[token]
|
|
|
|
if token_c
|
|
|
|
# reserved word?
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if (@lex_state != :EXPR_BEG &&
|
|
|
|
@lex_state != :EXPR_FNAME &&
|
2010-04-01 03:45:16 -04:00
|
|
|
trans[1])
|
|
|
|
# modifiers
|
|
|
|
token_c = TkSymbol2Token[trans[1]]
|
|
|
|
@lex_state = trans[0]
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state != :EXPR_FNAME
|
2010-04-01 03:45:16 -04:00
|
|
|
if ENINDENT_CLAUSE.include?(token)
|
2013-09-18 19:33:36 -04:00
|
|
|
valid = peek(0) != ':'
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# check for ``class = val'' etc.
|
|
|
|
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
|
2013-09-18 19:33:36 -04:00
|
|
|
end if valid
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
2012-11-26 23:28:14 -05:00
|
|
|
else
|
|
|
|
token_c = TkIDENTIFIER
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
elsif DEINDENT_CLAUSE.include?(token)
|
|
|
|
@indent -= 1
|
|
|
|
@indent_stack.pop
|
|
|
|
end
|
|
|
|
@lex_state = trans[0]
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return Token(token_c, token)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if @lex_state == :EXPR_FNAME
|
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
if peek(0) == '='
|
|
|
|
token.concat getc
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
elsif @lex_state == :EXPR_BEG || @lex_state == :EXPR_DOT ||
|
|
|
|
@lex_state == :EXPR_ARG
|
|
|
|
@lex_state = :EXPR_ARG
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
|
|
|
# if lt = PERCENT_LTYPE[ch]
|
|
|
|
if ch == "-"
|
|
|
|
ch = getc
|
|
|
|
indent = true
|
|
|
|
end
|
|
|
|
if /['"`]/ =~ ch
|
2012-12-18 02:31:36 -05:00
|
|
|
user_quote = lt = ch
|
2010-04-01 03:45:16 -04:00
|
|
|
quoted = ""
|
|
|
|
while (c = getc) && c != lt
|
|
|
|
quoted.concat c
|
|
|
|
end
|
|
|
|
else
|
2012-12-18 02:31:36 -05:00
|
|
|
user_quote = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
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
|
|
|
|
|
2012-12-18 02:31:36 -05:00
|
|
|
output_heredoc = reserve.join =~ /\A\r?\n\z/
|
|
|
|
|
|
|
|
if output_heredoc then
|
|
|
|
doc = '<<'
|
|
|
|
doc << '-' if indent
|
|
|
|
doc << "#{user_quote}#{quoted}#{user_quote}\n"
|
|
|
|
else
|
|
|
|
doc = '"'
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@here_header = false
|
|
|
|
while l = gets
|
2012-11-26 23:28:14 -05:00
|
|
|
l = l.sub(/(:?\r)?\n\z/, "\n")
|
|
|
|
if (indent ? l.strip : l.chomp) == quoted
|
2010-04-01 03:45:16 -04:00
|
|
|
break
|
|
|
|
end
|
|
|
|
doc << l
|
|
|
|
end
|
2012-12-18 02:31:36 -05:00
|
|
|
|
|
|
|
if output_heredoc then
|
2013-09-18 19:33:36 -04:00
|
|
|
raise Error, "Missing terminating #{quoted} for string" unless l
|
|
|
|
|
2012-12-18 02:31:36 -05:00
|
|
|
doc << l.chomp
|
|
|
|
else
|
|
|
|
doc << '"'
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
@here_header = true
|
|
|
|
@here_readed.concat reserve
|
|
|
|
while ch = reserve.pop
|
|
|
|
ungetc ch
|
|
|
|
end
|
|
|
|
|
2012-12-18 02:31:36 -05:00
|
|
|
token_class = output_heredoc ? RDoc::RubyLex::TkHEREDOC : Ltype2Token[lt]
|
2010-04-01 03:45:16 -04:00
|
|
|
@ltype = ltback
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2012-12-18 02:31:36 -05:00
|
|
|
Token(token_class, doc)
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def identify_quotation
|
2012-11-26 23:28:14 -05:00
|
|
|
type = ch = getc
|
|
|
|
if lt = PERCENT_LTYPE[type]
|
2010-04-01 03:45:16 -04:00
|
|
|
ch = getc
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif type =~ /\W/
|
|
|
|
type = nil
|
2010-04-01 03:45:16 -04:00
|
|
|
lt = "\""
|
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
return Token(TkMOD, '%')
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
# if ch !~ /\W/
|
|
|
|
# ungetc
|
|
|
|
# next
|
|
|
|
# end
|
|
|
|
#@ltype = lt
|
|
|
|
@quoted = ch unless @quoted = PERCENT_PAREN[ch]
|
2012-11-26 23:28:14 -05:00
|
|
|
identify_string(lt, @quoted, type)
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
def identify_number(op = "")
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
num = op
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
if peek(0) == "0" && peek(1) !~ /[.eE]/
|
|
|
|
num << getc
|
|
|
|
|
|
|
|
case peek(0)
|
|
|
|
when /[xX]/
|
|
|
|
ch = getc
|
|
|
|
match = /[0-9a-fA-F_]/
|
|
|
|
when /[bB]/
|
|
|
|
ch = getc
|
|
|
|
match = /[01_]/
|
|
|
|
when /[oO]/
|
|
|
|
ch = getc
|
|
|
|
match = /[0-7_]/
|
|
|
|
when /[dD]/
|
|
|
|
ch = getc
|
|
|
|
match = /[0-9_]/
|
|
|
|
when /[0-7]/
|
|
|
|
match = /[0-7_]/
|
|
|
|
when /[89]/
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "Illegal octal digit"
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
return Token(TkINTEGER, num)
|
|
|
|
end
|
|
|
|
|
|
|
|
num << ch if ch
|
|
|
|
|
|
|
|
len0 = true
|
|
|
|
non_digit = false
|
|
|
|
while ch = getc
|
|
|
|
num << ch
|
|
|
|
if match =~ ch
|
|
|
|
if ch == "_"
|
|
|
|
if non_digit
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "trailing `#{ch}' in number"
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
non_digit = ch
|
|
|
|
end
|
|
|
|
else
|
|
|
|
non_digit = false
|
|
|
|
len0 = false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
num[-1, 1] = ''
|
|
|
|
if len0
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "numeric literal without digits"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
if non_digit
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "trailing `#{non_digit}' in number"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return Token(TkINTEGER, num)
|
|
|
|
end
|
|
|
|
|
|
|
|
type = TkINTEGER
|
|
|
|
allow_point = true
|
|
|
|
allow_e = true
|
|
|
|
non_digit = false
|
|
|
|
while ch = getc
|
|
|
|
num << ch
|
|
|
|
case ch
|
|
|
|
when /[0-9]/
|
|
|
|
non_digit = false
|
|
|
|
when "_"
|
|
|
|
non_digit = ch
|
|
|
|
when allow_point && "."
|
|
|
|
if non_digit
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "trailing `#{non_digit}' in number"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
type = TkFLOAT
|
|
|
|
if peek(0) !~ /[0-9]/
|
|
|
|
type = TkINTEGER
|
|
|
|
ungetc
|
|
|
|
num[-1, 1] = ''
|
|
|
|
break
|
|
|
|
end
|
|
|
|
allow_point = false
|
|
|
|
when allow_e && "e", allow_e && "E"
|
|
|
|
if non_digit
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "trailing `#{non_digit}' in number"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
type = TkFLOAT
|
|
|
|
if peek(0) =~ /[+-]/
|
|
|
|
num << getc
|
|
|
|
end
|
|
|
|
allow_e = false
|
|
|
|
allow_point = false
|
|
|
|
non_digit = ch
|
|
|
|
else
|
|
|
|
if non_digit
|
2012-11-26 23:28:14 -05:00
|
|
|
raise Error, "trailing `#{non_digit}' in number"
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
ungetc
|
|
|
|
num[-1, 1] = ''
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Token(type, num)
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def identify_string(ltype, quoted = ltype, type = nil)
|
|
|
|
close = PERCENT_PAREN.values.include?(quoted)
|
2010-04-01 03:45:16 -04:00
|
|
|
@ltype = ltype
|
|
|
|
@quoted = quoted
|
2010-04-10 02:36:13 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
str = if ltype == quoted and %w[" ' /].include? ltype then
|
2010-04-10 02:36:13 -04:00
|
|
|
ltype.dup
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif RUBY_VERSION > '1.9' then
|
|
|
|
"%#{type or PERCENT_LTYPE.key ltype}#{PERCENT_PAREN_REV[quoted]}"
|
2010-04-10 02:36:13 -04:00
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
"%#{type or PERCENT_LTYPE.index ltype}#{PERCENT_PAREN_REV[quoted]}"
|
2010-04-10 02:36:13 -04:00
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
subtype = nil
|
|
|
|
begin
|
|
|
|
nest = 0
|
|
|
|
|
|
|
|
while ch = getc
|
|
|
|
str << ch
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if @quoted == ch and nest <= 0
|
2010-04-01 03:45:16 -04:00
|
|
|
break
|
|
|
|
elsif @ltype != "'" && @ltype != "]" && @ltype != ":" and ch == "#"
|
|
|
|
ch = getc
|
|
|
|
subtype = true
|
|
|
|
if ch == "{" then
|
|
|
|
str << ch << skip_inner_expression
|
2010-04-10 02:36:13 -04:00
|
|
|
next
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
elsif ch == '\\'
|
|
|
|
if %w[' /].include? @ltype then
|
|
|
|
case ch = getc
|
|
|
|
when "\\", "\n", "'"
|
|
|
|
when @ltype
|
|
|
|
str << ch
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2012-11-26 23:28:14 -05:00
|
|
|
str << read_escape
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
if close then
|
2010-04-01 03:45:16 -04:00
|
|
|
if PERCENT_PAREN[ch] == @quoted
|
|
|
|
nest += 1
|
|
|
|
elsif ch == @quoted
|
|
|
|
nest -= 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if @ltype == "/"
|
2013-11-18 19:47:33 -05:00
|
|
|
while peek(0) =~ /i|m|x|o|e|s|u|n/
|
|
|
|
str << getc
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if subtype
|
|
|
|
Token(DLtype2Token[ltype], str)
|
|
|
|
else
|
|
|
|
Token(Ltype2Token[ltype], str)
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
@ltype = nil
|
|
|
|
@quoted = nil
|
2013-09-18 19:33:36 -04:00
|
|
|
@lex_state = :EXPR_END
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def skip_inner_expression
|
|
|
|
res = ""
|
|
|
|
nest = 0
|
2010-04-10 02:36:13 -04:00
|
|
|
while ch = getc
|
2010-04-01 03:45:16 -04:00
|
|
|
res << ch
|
|
|
|
if ch == '}'
|
|
|
|
break if nest.zero?
|
|
|
|
nest -= 1
|
|
|
|
elsif ch == '{'
|
|
|
|
nest += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
res
|
|
|
|
end
|
|
|
|
|
|
|
|
def identify_comment
|
|
|
|
@ltype = "#"
|
|
|
|
|
|
|
|
comment = '#'
|
|
|
|
|
|
|
|
while ch = getc
|
|
|
|
# if ch == "\\" #"
|
|
|
|
# read_escape
|
|
|
|
# end
|
|
|
|
if ch == "\n"
|
|
|
|
@ltype = nil
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
comment << ch
|
|
|
|
end
|
|
|
|
|
|
|
|
return Token(TkCOMMENT, comment)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_escape
|
|
|
|
escape = ''
|
|
|
|
ch = getc
|
|
|
|
|
|
|
|
case ch
|
|
|
|
when "\n", "\r", "\f"
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
when "\\", "n", "t", "r", "f", "v", "a", "e", "b", "s" #"
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
when /[0-7]/
|
|
|
|
ungetc ch
|
|
|
|
3.times do
|
|
|
|
ch = getc
|
|
|
|
case ch
|
|
|
|
when /[0-7]/
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
when "x"
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
2.times do
|
|
|
|
ch = getc
|
|
|
|
case ch
|
|
|
|
when /[0-9a-fA-F]/
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
when nil
|
|
|
|
break
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
when "M"
|
|
|
|
escape << ch
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
ch = getc
|
2010-04-01 03:45:16 -04:00
|
|
|
if ch != '-'
|
|
|
|
ungetc
|
|
|
|
else
|
|
|
|
escape << ch
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
ch = getc
|
2010-04-01 03:45:16 -04:00
|
|
|
if ch == "\\" #"
|
2013-09-18 19:33:36 -04:00
|
|
|
ungetc
|
2010-04-01 03:45:16 -04:00
|
|
|
escape << read_escape
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
|
|
|
escape << ch
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
when "C", "c" #, "^"
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
|
|
|
|
|
|
|
if ch == "C"
|
|
|
|
ch = getc
|
|
|
|
|
|
|
|
if ch == "-"
|
|
|
|
escape << ch
|
|
|
|
ch = getc
|
|
|
|
escape << ch
|
|
|
|
|
|
|
|
escape << read_escape if ch == "\\"
|
|
|
|
else
|
|
|
|
ungetc
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
elsif (ch = getc) == "\\" #"
|
|
|
|
escape << ch << read_escape
|
|
|
|
end
|
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
escape << ch
|
|
|
|
|
2010-04-02 00:40:47 -04:00
|
|
|
# other characters
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
escape
|
|
|
|
end
|
|
|
|
|
|
|
|
# :startdoc:
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#RDoc::RubyLex.debug_level = 1
|
|
|
|
|