2017-11-27 05:45:24 -05:00
|
|
|
# frozen_string_literal: true
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2003-12-01 02:12:49 -05:00
|
|
|
# This file contains stuff stolen outright from:
|
|
|
|
#
|
2008-01-13 22:34:05 -05:00
|
|
|
# rtags.rb -
|
2008-06-04 05:37:38 -04:00
|
|
|
# ruby-lex.rb - ruby lexcal analyzer
|
2008-01-13 22:34:05 -05:00
|
|
|
# ruby-token.rb - ruby tokens
|
2008-07-17 20:46:16 -04:00
|
|
|
# by Keiju ISHITSUKA (Nippon Rational Inc.)
|
2003-12-01 02:12:49 -05:00
|
|
|
#
|
|
|
|
|
2008-01-07 01:56:46 -05:00
|
|
|
##
|
2008-07-17 20:46:16 -04:00
|
|
|
# Extracts code elements from a source file returning a TopLevel object
|
2008-01-07 01:56:46 -05:00
|
|
|
# containing the constituent file elements.
|
2003-12-01 02:12:49 -05:00
|
|
|
#
|
|
|
|
# This file is based on rtags
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
|
|
|
# RubyParser understands how to document:
|
|
|
|
# * classes
|
|
|
|
# * modules
|
|
|
|
# * methods
|
|
|
|
# * constants
|
|
|
|
# * aliases
|
|
|
|
# * private, public, protected
|
|
|
|
# * private_class_function, public_class_function
|
2017-09-11 23:42:54 -04:00
|
|
|
# * private_constant, public_constant
|
2008-07-17 20:46:16 -04:00
|
|
|
# * module_function
|
|
|
|
# * attr, attr_reader, attr_writer, attr_accessor
|
|
|
|
# * extra accessors given on the command line
|
|
|
|
# * metaprogrammed methods
|
|
|
|
# * require
|
|
|
|
# * include
|
|
|
|
#
|
|
|
|
# == Method Arguments
|
|
|
|
#
|
|
|
|
#--
|
|
|
|
# NOTE: I don't think this works, needs tests, remove the paragraph following
|
|
|
|
# this block when known to work
|
|
|
|
#
|
|
|
|
# The parser extracts the arguments from the method definition. You can
|
|
|
|
# override this with a custom argument definition using the :args: directive:
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # This method tries over and over until it is tired
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# def go_go_go(thing_to_try, tries = 10) # :args: thing_to_try
|
|
|
|
# puts thing_to_try
|
|
|
|
# go_go_go thing_to_try, tries - 1
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# If you have a more-complex set of overrides you can use the :call-seq:
|
|
|
|
# directive:
|
|
|
|
#++
|
|
|
|
#
|
|
|
|
# The parser extracts the arguments from the method definition. You can
|
|
|
|
# override this with a custom argument definition using the :call-seq:
|
|
|
|
# directive:
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # This method can be called with a range or an offset and length
|
|
|
|
# #
|
|
|
|
# # :call-seq:
|
|
|
|
# # my_method(Range)
|
|
|
|
# # my_method(offset, length)
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# def my_method(*args)
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# The parser extracts +yield+ expressions from method bodies to gather the
|
|
|
|
# yielded argument names. If your method manually calls a block instead of
|
|
|
|
# yielding or you want to override the discovered argument names use
|
|
|
|
# the :yields: directive:
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # My method is awesome
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# def my_method(&block) # :yields: happy, times
|
|
|
|
# block.call 1, 2
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# == Metaprogrammed Methods
|
|
|
|
#
|
|
|
|
# To pick up a metaprogrammed method, the parser looks for a comment starting
|
|
|
|
# with '##' before an identifier:
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # This is a meta-programmed method!
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# add_my_method :meta_method, :arg1, :arg2
|
|
|
|
#
|
|
|
|
# The parser looks at the token after the identifier to determine the name, in
|
|
|
|
# this example, :meta_method. If a name cannot be found, a warning is printed
|
|
|
|
# and 'unknown is used.
|
|
|
|
#
|
|
|
|
# You can force the name of a method using the :method: directive:
|
|
|
|
#
|
|
|
|
# ##
|
2012-11-26 23:28:14 -05:00
|
|
|
# # :method: some_method!
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
|
|
|
# By default, meta-methods are instance methods. To indicate that a method is
|
|
|
|
# a singleton method instead use the :singleton-method: directive:
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # :singleton-method:
|
|
|
|
#
|
|
|
|
# You can also use the :singleton-method: directive with a name:
|
|
|
|
#
|
|
|
|
# ##
|
2012-11-26 23:28:14 -05:00
|
|
|
# # :singleton-method: some_method!
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
2013-09-18 19:33:36 -04:00
|
|
|
# You can define arguments for metaprogrammed methods via either the
|
|
|
|
# :call-seq:, :arg: or :args: directives.
|
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# Additionally you can mark a method as an attribute by
|
|
|
|
# using :attr:, :attr_reader:, :attr_writer: or :attr_accessor:. Just like
|
|
|
|
# for :method:, the name is optional.
|
|
|
|
#
|
|
|
|
# ##
|
|
|
|
# # :attr_reader: my_attr_name
|
|
|
|
#
|
|
|
|
# == Hidden methods and attributes
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
|
|
|
# You can provide documentation for methods that don't appear using
|
2010-04-01 03:45:16 -04:00
|
|
|
# the :method:, :singleton-method: and :attr: directives:
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
|
|
|
# ##
|
2010-04-01 03:45:16 -04:00
|
|
|
# # :attr_writer: ghost_writer
|
|
|
|
# # There is an attribute here, but you can't see it!
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# ##
|
2008-07-17 20:46:16 -04:00
|
|
|
# # :method: ghost_method
|
|
|
|
# # There is a method here, but you can't see it!
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# ##
|
|
|
|
# # this is a comment for a regular method
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2008-07-17 20:46:16 -04:00
|
|
|
# def regular_method() end
|
|
|
|
#
|
|
|
|
# Note that by default, the :method: directive will be ignored if there is a
|
|
|
|
# standard rdocable item following it.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
require 'ripper'
|
2018-08-26 21:30:18 -04:00
|
|
|
require_relative 'ripper_state_lex'
|
2017-09-11 23:42:54 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
class RDoc::Parser::Ruby < RDoc::Parser
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-04-10 02:36:13 -04:00
|
|
|
parse_files_matching(/\.rbw?$/)
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
include RDoc::TokenStream
|
2010-04-01 03:45:16 -04:00
|
|
|
include RDoc::Parser::RubyTools
|
|
|
|
|
|
|
|
##
|
|
|
|
# RDoc::NormalClass type
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
NORMAL = "::"
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
##
|
|
|
|
# RDoc::SingleClass type
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
SINGLE = "<<"
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Creates a new Ruby parser.
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def initialize(top_level, file_name, content, options, stats)
|
2008-07-17 20:46:16 -04:00
|
|
|
super
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
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
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
@size = 0
|
|
|
|
@token_listeners = nil
|
2018-03-26 01:56:26 -04:00
|
|
|
content = RDoc::Encoding.remove_magic_comment content
|
2018-08-26 21:30:18 -04:00
|
|
|
@scanner = RDoc::Parser::RipperStateLex.parse(content)
|
2017-12-23 18:33:09 -05:00
|
|
|
@content = content
|
2017-09-11 23:42:54 -04:00
|
|
|
@scanner_point = 0
|
2010-04-01 03:45:16 -04:00
|
|
|
@prev_seek = nil
|
2012-11-26 23:28:14 -05:00
|
|
|
@markup = @options.markup
|
2013-09-18 19:33:36 -04:00
|
|
|
@track_visibility = :nodoc != @options.visibility
|
2016-09-07 18:23:38 -04:00
|
|
|
@encoding = @options.encoding
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
reset
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
def tk_nl?(tk)
|
|
|
|
:on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Retrieves the read token stream and replaces +pattern+ with +replacement+
|
|
|
|
# using gsub. If the result is only a ";" returns an empty string.
|
|
|
|
|
|
|
|
def get_tkread_clean pattern, replacement # :nodoc:
|
|
|
|
read = get_tkread.gsub(pattern, replacement).strip
|
|
|
|
return '' if read == ';'
|
|
|
|
read
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2014-09-04 21:41:25 -04:00
|
|
|
# Extracts the visibility information for the visibility token +tk+
|
|
|
|
# and +single+ class type identifier.
|
2013-09-18 19:33:36 -04:00
|
|
|
#
|
|
|
|
# Returns the visibility type (a string), the visibility (a symbol) and
|
|
|
|
# +singleton+ if the methods following should be converted to singleton
|
|
|
|
# methods.
|
|
|
|
|
2014-09-04 21:41:25 -04:00
|
|
|
def get_visibility_information tk, single # :nodoc:
|
2017-09-11 23:42:54 -04:00
|
|
|
vis_type = tk[:text]
|
2014-09-04 21:41:25 -04:00
|
|
|
singleton = single == SINGLE
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
vis =
|
|
|
|
case vis_type
|
|
|
|
when 'private' then :private
|
|
|
|
when 'protected' then :protected
|
|
|
|
when 'public' then :public
|
|
|
|
when 'private_class_method' then
|
|
|
|
singleton = true
|
|
|
|
:private
|
|
|
|
when 'public_class_method' then
|
|
|
|
singleton = true
|
|
|
|
:public
|
|
|
|
when 'module_function' then
|
|
|
|
singleton = true
|
|
|
|
:public
|
|
|
|
else
|
|
|
|
raise RDoc::Error, "Invalid visibility: #{tk.name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
return vis_type, vis, singleton
|
|
|
|
end
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
##
|
|
|
|
# Look for the first comment in a file that isn't a shebang line.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def collect_first_comment
|
|
|
|
skip_tkspace
|
2017-11-27 05:45:24 -05:00
|
|
|
comment = ''.dup
|
|
|
|
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
|
2008-01-13 22:34:05 -05:00
|
|
|
first_line = true
|
2017-09-11 23:42:54 -04:00
|
|
|
first_comment_tk_kind = nil
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no = nil
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
tk = get_tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
while tk && (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])
|
2018-08-26 21:30:18 -04:00
|
|
|
comment_body = retrieve_comment_body(tk)
|
|
|
|
if first_line and comment_body =~ /\A#!/ then
|
2008-06-21 02:57:28 -04:00
|
|
|
skip_tkspace
|
|
|
|
tk = get_tk
|
2018-08-26 21:30:18 -04:00
|
|
|
elsif first_line and comment_body =~ /\A#\s*-\*-/ then
|
2008-06-21 02:57:28 -04:00
|
|
|
first_line = false
|
2008-01-13 22:34:05 -05:00
|
|
|
skip_tkspace
|
|
|
|
tk = get_tk
|
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
break if first_comment_tk_kind and not first_comment_tk_kind === tk[:kind]
|
|
|
|
first_comment_tk_kind = tk[:kind]
|
2014-09-04 21:41:25 -04:00
|
|
|
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no = tk[:line_no] if first_line
|
2008-06-21 02:57:28 -04:00
|
|
|
first_line = false
|
2018-08-26 21:30:18 -04:00
|
|
|
comment << comment_body
|
2008-01-13 22:34:05 -05:00
|
|
|
tk = get_tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_nl === tk then
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2003-12-01 02:12:49 -05:00
|
|
|
tk = get_tk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
unget_tk tk
|
|
|
|
|
2019-11-26 21:58:38 -05:00
|
|
|
new_comment comment, line_no
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Consumes trailing whitespace from the token stream
|
|
|
|
|
|
|
|
def consume_trailing_spaces # :nodoc:
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new attribute in +container+ with +name+.
|
|
|
|
|
|
|
|
def create_attr container, single, name, rw, comment # :nodoc:
|
|
|
|
att = RDoc::Attr.new get_tkread, name, rw, comment, single == SINGLE
|
|
|
|
record_location att
|
|
|
|
|
|
|
|
container.add_attribute att
|
|
|
|
@stats.add_attribute att
|
|
|
|
|
|
|
|
att
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a module alias in +container+ at +rhs_name+ (or at the top-level
|
|
|
|
# for "::") with the name from +constant+.
|
|
|
|
|
|
|
|
def create_module_alias container, constant, rhs_name # :nodoc:
|
|
|
|
mod = if rhs_name =~ /^::/ then
|
|
|
|
@store.find_class_or_module rhs_name
|
|
|
|
else
|
|
|
|
container.find_module_named rhs_name
|
|
|
|
end
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
container.add_module_alias mod, rhs_name, constant, @top_level
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Aborts with +msg+
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def error(msg)
|
|
|
|
msg = make_message msg
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
abort msg
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2017-11-27 05:45:24 -05:00
|
|
|
# Looks for a true or false token.
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def get_bool
|
|
|
|
skip_tkspace
|
|
|
|
tk = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_kw == tk[:kind] && 'true' == tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
true
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_kw == tk[:kind] && ('false' == tk[:text] || 'nil' == tk[:text])
|
2008-07-17 20:46:16 -04:00
|
|
|
false
|
2008-01-13 22:34:05 -05:00
|
|
|
else
|
2008-07-17 20:46:16 -04:00
|
|
|
unget_tk tk
|
|
|
|
true
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2008-01-13 22:34:05 -05:00
|
|
|
# Look for the name of a class of module (optionally with a leading :: or
|
2010-12-19 22:22:49 -05:00
|
|
|
# with :: separated named) and return the ultimate name, the associated
|
|
|
|
# container, and the given name (with the ::).
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2012-12-07 00:22:50 -05:00
|
|
|
def get_class_or_module container, ignore_constants = false
|
2008-01-13 22:34:05 -05:00
|
|
|
skip_tkspace
|
|
|
|
name_t = get_tk
|
2017-11-27 05:45:24 -05:00
|
|
|
given_name = ''.dup
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
# class ::A -> A is in the top level
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_op == name_t[:kind] and '::' == name_t[:text] then # bug
|
2008-01-13 22:34:05 -05:00
|
|
|
name_t = get_tk
|
|
|
|
container = @top_level
|
2010-12-19 22:22:49 -05:00
|
|
|
given_name << '::'
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2017-09-11 23:42:54 -04:00
|
|
|
given_name << name_t[:text]
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
is_self = name_t[:kind] == :on_op && name_t[:text] == '<<'
|
2018-03-26 01:56:26 -04:00
|
|
|
new_modules = []
|
2017-09-11 23:42:54 -04:00
|
|
|
while !is_self && (tk = peek_tk) and :on_op == tk[:kind] and '::' == tk[:text] do
|
2008-01-13 22:34:05 -05:00
|
|
|
prev_container = container
|
2017-09-11 23:42:54 -04:00
|
|
|
container = container.find_module_named name_t[:text]
|
2012-12-07 00:22:50 -05:00
|
|
|
container ||=
|
|
|
|
if ignore_constants then
|
2018-03-26 01:56:26 -04:00
|
|
|
c = RDoc::NormalModule.new name_t[:text]
|
2018-05-07 22:07:53 -04:00
|
|
|
c.store = @store
|
2018-03-26 01:56:26 -04:00
|
|
|
new_modules << [prev_container, c]
|
|
|
|
c
|
2012-12-07 00:22:50 -05:00
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
c = prev_container.add_module RDoc::NormalModule, name_t[:text]
|
2012-12-07 00:22:50 -05:00
|
|
|
c.ignore unless prev_container.document_children
|
2013-09-18 19:33:36 -04:00
|
|
|
@top_level.add_to_classes_or_modules c
|
2012-12-07 00:22:50 -05:00
|
|
|
c
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location container
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
get_tk
|
2019-12-23 17:41:37 -05:00
|
|
|
skip_tkspace
|
2019-12-23 17:00:35 -05:00
|
|
|
if :on_lparen == peek_tk[:kind] # ProcObjectInConstant::()
|
|
|
|
parse_method_or_yield_parameters
|
|
|
|
break
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
name_t = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
unless :on_const == name_t[:kind] || :on_ident == name_t[:kind]
|
|
|
|
raise RDoc::Error, "Invalid class or module definition: #{given_name}"
|
|
|
|
end
|
2017-10-09 22:01:00 -04:00
|
|
|
if prev_container == container and !ignore_constants
|
|
|
|
given_name = name_t[:text]
|
|
|
|
else
|
2017-11-27 05:45:24 -05:00
|
|
|
given_name << '::' + name_t[:text]
|
2017-10-09 22:01:00 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
return [container, name_t, given_name, new_modules]
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Return a superclass, which can be either a constant of an expression
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def get_class_specification
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = peek_tk
|
|
|
|
if tk.nil?
|
|
|
|
return ''
|
|
|
|
elsif :on_kw == tk[:kind] && 'self' == tk[:text]
|
|
|
|
return 'self'
|
|
|
|
elsif :on_gvar == tk[:kind]
|
|
|
|
return ''
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
res = get_constant
|
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2004-02-23 16:10:42 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
get_tkread # empty out read buffer
|
2004-02-23 16:10:42 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
tk = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
return res unless tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:kind]
|
|
|
|
when :on_nl, :on_comment, :on_embdoc, :on_semicolon then
|
2008-01-13 22:34:05 -05:00
|
|
|
unget_tk(tk)
|
2008-07-17 20:46:16 -04:00
|
|
|
return res
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
res += parse_call_parameters(tk)
|
|
|
|
res
|
|
|
|
end
|
2004-02-23 16:10:42 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Parse a constant, which might be qualified by one or more class or module
|
|
|
|
# names
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def get_constant
|
|
|
|
res = ""
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
while tk && ((:on_op == tk[:kind] && '::' == tk[:text]) || :on_const == tk[:kind]) do
|
|
|
|
res += tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
unget_tk(tk)
|
|
|
|
res
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2018-08-26 21:30:18 -04:00
|
|
|
# Get an included module that may be surrounded by parens
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2018-08-26 21:30:18 -04:00
|
|
|
def get_included_module_with_optional_parens
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2018-08-26 21:30:18 -04:00
|
|
|
get_tkread
|
|
|
|
tk = get_tk
|
|
|
|
end_token = get_end_token tk
|
|
|
|
return '' unless end_token
|
2011-06-16 00:59:24 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
nest = 0
|
2018-08-26 21:30:18 -04:00
|
|
|
continue = false
|
|
|
|
only_constant = true
|
2011-06-16 00:59:24 -04:00
|
|
|
|
2018-08-26 21:30:18 -04:00
|
|
|
while tk != nil do
|
|
|
|
is_element_of_constant = false
|
|
|
|
case tk[:kind]
|
|
|
|
when :on_semicolon then
|
|
|
|
break if nest == 0
|
|
|
|
when :on_lbracket then
|
|
|
|
nest += 1
|
|
|
|
when :on_rbracket then
|
|
|
|
nest -= 1
|
|
|
|
when :on_lbrace then
|
|
|
|
nest += 1
|
|
|
|
when :on_rbrace then
|
|
|
|
nest -= 1
|
|
|
|
if nest <= 0
|
|
|
|
# we might have a.each { |i| yield i }
|
|
|
|
unget_tk(tk) if nest < 0
|
|
|
|
break
|
|
|
|
end
|
|
|
|
when :on_lparen then
|
|
|
|
nest += 1
|
|
|
|
when end_token[:kind] then
|
|
|
|
if end_token[:kind] == :on_rparen
|
|
|
|
nest -= 1
|
|
|
|
break if nest <= 0
|
|
|
|
else
|
|
|
|
break if nest <= 0
|
|
|
|
end
|
|
|
|
when :on_rparen then
|
|
|
|
nest -= 1
|
|
|
|
when :on_comment, :on_embdoc then
|
|
|
|
@read.pop
|
|
|
|
if :on_nl == end_token[:kind] and "\n" == tk[:text][-1] and
|
|
|
|
(!continue or (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) != 0) then
|
|
|
|
break if !continue and nest <= 0
|
|
|
|
end
|
|
|
|
when :on_comma then
|
|
|
|
continue = true
|
|
|
|
when :on_ident then
|
|
|
|
continue = false if continue
|
|
|
|
when :on_kw then
|
|
|
|
case tk[:text]
|
|
|
|
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
|
|
|
|
nest += 1
|
|
|
|
when 'if', 'unless', 'while', 'until', 'rescue'
|
|
|
|
# postfix if/unless/while/until/rescue must be EXPR_LABEL
|
|
|
|
nest += 1 unless (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) != 0
|
|
|
|
when 'end'
|
|
|
|
nest -= 1
|
|
|
|
break if nest == 0
|
|
|
|
end
|
|
|
|
when :on_const then
|
|
|
|
is_element_of_constant = true
|
|
|
|
when :on_op then
|
|
|
|
is_element_of_constant = true if '::' == tk[:text]
|
|
|
|
end
|
|
|
|
only_constant = false unless is_element_of_constant
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2018-08-26 21:30:18 -04:00
|
|
|
if only_constant
|
|
|
|
get_tkread_clean(/\s+/, ' ')
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Little hack going on here. In the statement:
|
|
|
|
#
|
|
|
|
# f = 2*(1+yield)
|
|
|
|
#
|
|
|
|
# We see the RPAREN as the next token, so we need to exit early. This still
|
|
|
|
# won't catch all cases (such as "a = yield + 1"
|
|
|
|
|
|
|
|
def get_end_token tk # :nodoc:
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:kind]
|
|
|
|
when :on_lparen
|
2018-08-26 21:30:18 -04:00
|
|
|
token = RDoc::Parser::RipperStateLex::Token.new
|
|
|
|
token[:kind] = :on_rparen
|
|
|
|
token[:text] = ')'
|
|
|
|
token
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_rparen
|
2013-09-18 19:33:36 -04:00
|
|
|
nil
|
|
|
|
else
|
2018-08-26 21:30:18 -04:00
|
|
|
token = RDoc::Parser::RipperStateLex::Token.new
|
|
|
|
token[:kind] = :on_nl
|
|
|
|
token[:text] = "\n"
|
|
|
|
token
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Retrieves the method container for a singleton method.
|
|
|
|
|
|
|
|
def get_method_container container, name_t # :nodoc:
|
|
|
|
prev_container = container
|
2017-09-11 23:42:54 -04:00
|
|
|
container = container.find_module_named(name_t[:text])
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
unless container then
|
|
|
|
constant = prev_container.constants.find do |const|
|
2017-09-11 23:42:54 -04:00
|
|
|
const.name == name_t[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if constant then
|
|
|
|
parse_method_dummy prev_container
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
unless container then
|
|
|
|
# TODO seems broken, should starting at Object in @store
|
2017-09-11 23:42:54 -04:00
|
|
|
obj = name_t[:text].split("::").inject(Object) do |state, item|
|
2013-09-18 19:33:36 -04:00
|
|
|
state.const_get(item)
|
|
|
|
end rescue nil
|
|
|
|
|
|
|
|
type = obj.class == Class ? RDoc::NormalClass : RDoc::NormalModule
|
|
|
|
|
|
|
|
unless [Class, Module].include?(obj.class) then
|
2017-09-11 23:42:54 -04:00
|
|
|
warn("Couldn't find #{name_t[:text]}. Assuming it's a module")
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
if type == RDoc::NormalClass then
|
|
|
|
sclass = obj.superclass ? obj.superclass.name : nil
|
2017-09-11 23:42:54 -04:00
|
|
|
container = prev_container.add_class type, name_t[:text], sclass
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
container = prev_container.add_module type, name_t[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
record_location container
|
|
|
|
end
|
|
|
|
|
|
|
|
container
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Extracts a name or symbol from the token stream.
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def get_symbol_or_name
|
|
|
|
tk = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:kind]
|
|
|
|
when :on_symbol then
|
|
|
|
text = tk[:text].sub(/^:/, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
next_tk = peek_tk
|
|
|
|
if next_tk && :on_op == next_tk[:kind] && '=' == next_tk[:text] then
|
2010-04-01 03:45:16 -04:00
|
|
|
get_tk
|
|
|
|
text << '='
|
|
|
|
end
|
|
|
|
|
|
|
|
text
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_ident, :on_const, :on_gvar, :on_cvar, :on_ivar, :on_op, :on_kw then
|
|
|
|
tk[:text]
|
|
|
|
when :on_tstring, :on_dstring then
|
|
|
|
tk[:text][1..-2]
|
2008-01-13 22:34:05 -05:00
|
|
|
else
|
2010-04-01 03:45:16 -04:00
|
|
|
raise RDoc::Error, "Name or symbol expected (got #{tk})"
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Marks containers between +container+ and +ancestor+ as ignored
|
|
|
|
|
|
|
|
def suppress_parents container, ancestor # :nodoc:
|
|
|
|
while container and container != ancestor do
|
|
|
|
container.suppress unless container.documented?
|
|
|
|
container = container.parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Look for directives in a normal comment block:
|
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# # :stopdoc:
|
|
|
|
# # Don't display comment from this point forward
|
2008-07-17 20:46:16 -04:00
|
|
|
#
|
2010-12-19 22:22:49 -05:00
|
|
|
# This routine modifies its +comment+ parameter.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
def look_for_directives_in container, comment
|
|
|
|
@preprocess.handle comment, container do |directive, param|
|
2008-07-17 20:46:16 -04:00
|
|
|
case directive
|
2010-04-01 03:45:16 -04:00
|
|
|
when 'method', 'singleton-method',
|
|
|
|
'attr', 'attr_accessor', 'attr_reader', 'attr_writer' then
|
|
|
|
false # handled elsewhere
|
2008-07-17 20:46:16 -04:00
|
|
|
when 'section' then
|
2017-11-27 05:45:24 -05:00
|
|
|
break unless container.kind_of?(RDoc::Context)
|
|
|
|
container.set_current_section param, comment.dup
|
2012-11-26 23:28:14 -05:00
|
|
|
comment.text = ''
|
2008-07-17 20:46:16 -04:00
|
|
|
break
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
comment.remove_private
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Adds useful info about the parser to +message+
|
|
|
|
|
|
|
|
def make_message message
|
2017-11-27 05:45:24 -05:00
|
|
|
prefix = "#{@file_name}:".dup
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = peek_tk
|
|
|
|
prefix << "#{tk[:line_no]}:#{tk[:char_no]}:" if tk
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
"#{prefix} #{message}"
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Creates a comment with the correct format
|
|
|
|
|
2019-11-26 21:58:38 -05:00
|
|
|
def new_comment comment, line_no = nil
|
2019-01-21 14:46:46 -05:00
|
|
|
c = RDoc::Comment.new comment, @top_level, :ruby
|
2019-11-26 21:58:38 -05:00
|
|
|
c.line = line_no
|
2012-11-26 23:28:14 -05:00
|
|
|
c.format = @markup
|
|
|
|
c
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates an RDoc::Attr for the name following +tk+, setting the comment to
|
|
|
|
# +comment+.
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_attr(context, single, tk, comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
line_no = tk[:line_no]
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
args = parse_symbol_arg 1
|
2010-12-19 22:22:49 -05:00
|
|
|
if args.size > 0 then
|
2008-07-17 20:46:16 -04:00
|
|
|
name = args[0]
|
|
|
|
rw = "R"
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_comma == tk[:kind] then
|
2008-07-17 20:46:16 -04:00
|
|
|
rw = "RW" if get_bool
|
2008-01-13 22:34:05 -05:00
|
|
|
else
|
2008-07-17 20:46:16 -04:00
|
|
|
unget_tk tk
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
att = create_attr context, single, name, rw, comment
|
2011-02-01 19:32:30 -05:00
|
|
|
att.line = line_no
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
read_documentation_modifiers att, RDoc::ATTR_MODIFIERS
|
|
|
|
else
|
2010-12-19 22:22:49 -05:00
|
|
|
warn "'attr' ignored - looks like a variable"
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Creates an RDoc::Attr for each attribute listed after +tk+, setting the
|
|
|
|
# comment for each to +comment+.
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_attr_accessor(context, single, tk, comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
line_no = tk[:line_no]
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
args = parse_symbol_arg
|
|
|
|
rw = "?"
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
tmp = RDoc::CodeObject.new
|
|
|
|
read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
|
2011-06-29 17:17:31 -04:00
|
|
|
# TODO In most other places we let the context keep track of document_self
|
|
|
|
# and add found items appropriately but here we do not. I'm not sure why.
|
2013-09-18 19:33:36 -04:00
|
|
|
return if @track_visibility and not tmp.document_self
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
when "attr_reader" then rw = "R"
|
|
|
|
when "attr_writer" then rw = "W"
|
|
|
|
when "attr_accessor" then rw = "RW"
|
|
|
|
else
|
2010-04-01 03:45:16 -04:00
|
|
|
rw = '?'
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
for name in args
|
2013-09-18 19:33:36 -04:00
|
|
|
att = create_attr context, single, name, rw, comment
|
2011-02-01 19:32:30 -05:00
|
|
|
att.line = line_no
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Parses an +alias+ in +context+ with +comment+
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_alias(context, single, tk, comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
line_no = tk[:line_no]
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
skip_tkspace
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_lparen === peek_tk[:kind] then
|
2008-07-17 20:46:16 -04:00
|
|
|
get_tk
|
|
|
|
skip_tkspace
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
new_name = get_symbol_or_name
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
skip_tkspace
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_comma === peek_tk[:kind] then
|
2008-07-17 20:46:16 -04:00
|
|
|
get_tk
|
|
|
|
skip_tkspace
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
begin
|
|
|
|
old_name = get_symbol_or_name
|
|
|
|
rescue RDoc::Error
|
|
|
|
return
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
al = RDoc::Alias.new(get_tkread, old_name, new_name, comment,
|
|
|
|
single == SINGLE)
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location al
|
2011-02-01 19:32:30 -05:00
|
|
|
al.line = line_no
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
read_documentation_modifiers al, RDoc::ATTR_MODIFIERS
|
2011-06-29 17:17:31 -04:00
|
|
|
context.add_alias al
|
2010-12-19 22:22:49 -05:00
|
|
|
@stats.add_alias al
|
|
|
|
|
|
|
|
al
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Extracts call parameters from the token stream.
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_call_parameters(tk)
|
2017-09-11 23:42:54 -04:00
|
|
|
end_token = case tk[:kind]
|
|
|
|
when :on_lparen
|
|
|
|
:on_rparen
|
|
|
|
when :on_rparen
|
2008-01-13 22:34:05 -05:00
|
|
|
return ""
|
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
:on_nl
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
nest = 0
|
|
|
|
|
|
|
|
loop do
|
2017-09-11 23:42:54 -04:00
|
|
|
break if tk.nil?
|
|
|
|
case tk[:kind]
|
|
|
|
when :on_semicolon
|
2010-04-01 03:45:16 -04:00
|
|
|
break
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_lparen
|
2010-04-01 03:45:16 -04:00
|
|
|
nest += 1
|
|
|
|
when end_token
|
2017-09-11 23:42:54 -04:00
|
|
|
if end_token == :on_rparen
|
2010-04-01 03:45:16 -04:00
|
|
|
nest -= 1
|
2018-08-26 21:30:18 -04:00
|
|
|
break if RDoc::Parser::RipperStateLex.end?(tk) and nest <= 0
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2018-08-26 21:30:18 -04:00
|
|
|
break if RDoc::Parser::RipperStateLex.end?(tk)
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_comment, :on_embdoc
|
2010-04-01 03:45:16 -04:00
|
|
|
unget_tk(tk)
|
|
|
|
break
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_op
|
|
|
|
if tk[:text] =~ /^(.{1,2})?=$/
|
|
|
|
unget_tk(tk)
|
|
|
|
break
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
tk = get_tk
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
get_tkread_clean "\n", " "
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Parses a class in +context+ with +comment+
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def parse_class container, single, tk, comment
|
2017-09-11 23:42:54 -04:00
|
|
|
line_no = tk[:line_no]
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
declaration_context = container
|
2018-03-26 01:56:26 -04:00
|
|
|
container, name_t, given_name, = get_class_or_module container
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if name_t[:kind] == :on_const
|
|
|
|
cls = parse_class_regular container, declaration_context, single,
|
|
|
|
name_t, given_name, comment
|
|
|
|
elsif name_t[:kind] == :on_op && name_t[:text] == '<<'
|
|
|
|
case name = get_class_specification
|
|
|
|
when 'self', container.name
|
|
|
|
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
|
|
|
|
parse_statements container, SINGLE
|
|
|
|
return # don't update line
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
cls = parse_class_singleton container, name, comment
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
else
|
|
|
|
warn "Expected class name or '<<'. Got #{name_t[:kind]}: #{name_t[:text].inspect}"
|
|
|
|
return
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
cls.line = line_no
|
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
# after end modifiers
|
|
|
|
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
cls
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Parses and creates a regular class
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_class_regular container, declaration_context, single, # :nodoc:
|
|
|
|
name_t, given_name, comment
|
|
|
|
superclass = '::Object'
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if given_name =~ /^::/ then
|
|
|
|
declaration_context = @top_level
|
|
|
|
given_name = $'
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = peek_tk
|
|
|
|
if tk[:kind] == :on_op && tk[:text] == '<' then
|
2013-09-18 19:33:36 -04:00
|
|
|
get_tk
|
|
|
|
skip_tkspace
|
|
|
|
superclass = get_class_specification
|
|
|
|
superclass = '(unknown)' if superclass.empty?
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
cls_type = single == SINGLE ? RDoc::SingleClass : RDoc::NormalClass
|
|
|
|
cls = declaration_context.add_class cls_type, given_name, superclass
|
|
|
|
cls.ignore unless container.document_children
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
|
|
|
|
record_location cls
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
cls.add_comment comment, @top_level
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@top_level.add_to_classes_or_modules cls
|
|
|
|
@stats.add_class cls
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
suppress_parents container, declaration_context unless cls.document_self
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_statements cls
|
|
|
|
|
|
|
|
cls
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parses a singleton class in +container+ with the given +name+ and
|
|
|
|
# +comment+.
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_class_singleton container, name, comment # :nodoc:
|
|
|
|
other = @store.find_class_named name
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
unless other then
|
|
|
|
if name =~ /^::/ then
|
|
|
|
name = $'
|
|
|
|
container = @top_level
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
other = container.add_module RDoc::NormalModule, name
|
|
|
|
record_location other
|
|
|
|
|
|
|
|
# class << $gvar
|
|
|
|
other.ignore if name.empty?
|
|
|
|
|
|
|
|
other.add_comment comment, @top_level
|
|
|
|
end
|
|
|
|
|
|
|
|
# notify :nodoc: all if not a constant-named class/module
|
|
|
|
# (and remove any comment)
|
|
|
|
unless name =~ /\A(::)?[A-Z]/ then
|
|
|
|
other.document_self = nil
|
|
|
|
other.document_children = false
|
|
|
|
other.clear_comment
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
@top_level.add_to_classes_or_modules other
|
|
|
|
@stats.add_class other
|
|
|
|
|
|
|
|
read_documentation_modifiers other, RDoc::CLASS_MODIFIERS
|
|
|
|
parse_statements(other, SINGLE)
|
|
|
|
|
|
|
|
other
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2012-12-07 00:22:50 -05:00
|
|
|
# Parses a constant in +context+ with +comment+. If +ignore_constants+ is
|
|
|
|
# true, no found constants will be added to RDoc.
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-12-07 00:22:50 -05:00
|
|
|
def parse_constant container, tk, comment, ignore_constants = false
|
2017-09-11 23:42:54 -04:00
|
|
|
line_no = tk[:line_no]
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = tk[:text]
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
return unless name =~ /^\w+$/
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
new_modules = []
|
2017-10-09 22:01:00 -04:00
|
|
|
if :on_op == peek_tk[:kind] && '::' == peek_tk[:text] then
|
2012-12-07 00:22:50 -05:00
|
|
|
unget_tk tk
|
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
container, name_t, _, new_modules = get_class_or_module container, true
|
2012-12-07 00:22:50 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = name_t[:text]
|
2012-12-07 00:22:50 -05:00
|
|
|
end
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
is_array_or_hash = false
|
2017-10-09 22:01:00 -04:00
|
|
|
if peek_tk && :on_lbracket == peek_tk[:kind]
|
|
|
|
get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
nest = 1
|
|
|
|
while bracket_tk = get_tk
|
|
|
|
case bracket_tk[:kind]
|
|
|
|
when :on_lbracket
|
|
|
|
nest += 1
|
|
|
|
when :on_rbracket
|
|
|
|
nest -= 1
|
|
|
|
break if nest == 0
|
|
|
|
end
|
|
|
|
end
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2017-09-11 23:42:54 -04:00
|
|
|
is_array_or_hash = true
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2017-10-09 22:01:00 -04:00
|
|
|
unless peek_tk && :on_op == peek_tk[:kind] && '=' == peek_tk[:text] then
|
2017-09-11 23:42:54 -04:00
|
|
|
return false
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
2017-10-09 22:01:00 -04:00
|
|
|
get_tk
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
unless ignore_constants
|
|
|
|
new_modules.each do |prev_c, new_module|
|
|
|
|
prev_c.add_module_by_normal_module new_module
|
|
|
|
new_module.ignore unless prev_c.document_children
|
|
|
|
@top_level.add_to_classes_or_modules new_module
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
value = ''
|
|
|
|
con = RDoc::Constant.new name, value, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
body = parse_constant_body container, con, is_array_or_hash
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless body
|
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
con.value = body
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location con
|
|
|
|
con.line = line_no
|
|
|
|
read_documentation_modifiers con, RDoc::CONSTANT_MODIFIERS
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
return if is_array_or_hash
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@stats.add_constant con
|
2014-09-04 21:41:25 -04:00
|
|
|
container.add_constant con
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
def parse_constant_body container, constant, is_array_or_hash # :nodoc:
|
2013-09-18 19:33:36 -04:00
|
|
|
nest = 0
|
2017-11-27 05:45:24 -05:00
|
|
|
rhs_name = ''.dup
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
get_tkread
|
|
|
|
|
|
|
|
tk = get_tk
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
body = nil
|
2008-01-13 22:34:05 -05:00
|
|
|
loop do
|
2017-09-11 23:42:54 -04:00
|
|
|
break if tk.nil?
|
|
|
|
if :on_semicolon == tk[:kind] then
|
2010-12-19 22:22:49 -05:00
|
|
|
break if nest <= 0
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif [:on_tlambeg, :on_lparen, :on_lbrace, :on_lbracket].include?(tk[:kind]) then
|
|
|
|
nest += 1
|
|
|
|
elsif (:on_kw == tk[:kind] && 'def' == tk[:text]) then
|
2010-04-01 03:45:16 -04:00
|
|
|
nest += 1
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif (:on_kw == tk[:kind] && %w{do if unless case begin}.include?(tk[:text])) then
|
2018-08-26 21:30:18 -04:00
|
|
|
if (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) == 0
|
2017-09-11 23:42:54 -04:00
|
|
|
nest += 1
|
|
|
|
end
|
|
|
|
elsif [:on_rparen, :on_rbrace, :on_rbracket].include?(tk[:kind]) ||
|
|
|
|
(:on_kw == tk[:kind] && 'end' == tk[:text]) then
|
2010-04-01 03:45:16 -04:00
|
|
|
nest -= 1
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) then
|
|
|
|
unget_tk tk
|
2018-08-26 21:30:18 -04:00
|
|
|
if nest <= 0 and RDoc::Parser::RipperStateLex.end?(tk) then
|
2017-09-11 23:42:54 -04:00
|
|
|
body = get_tkread_clean(/^[ \t]+/, '')
|
|
|
|
read_documentation_modifiers constant, RDoc::CONSTANT_MODIFIERS
|
2003-12-01 02:12:49 -05:00
|
|
|
break
|
2012-11-26 23:28:14 -05:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
read_documentation_modifiers constant, RDoc::CONSTANT_MODIFIERS
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_const == tk[:kind] then
|
|
|
|
rhs_name << tk[:text]
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
next_tk = peek_tk
|
|
|
|
if nest <= 0 and (next_tk.nil? || :on_nl == next_tk[:kind]) then
|
|
|
|
create_module_alias container, constant, rhs_name unless is_array_or_hash
|
2010-04-01 03:45:16 -04:00
|
|
|
break
|
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_nl == tk[:kind] then
|
2018-08-26 21:30:18 -04:00
|
|
|
if nest <= 0 and RDoc::Parser::RipperStateLex.end?(tk) then
|
2010-04-01 03:45:16 -04:00
|
|
|
unget_tk tk
|
|
|
|
break
|
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_op == tk[:kind] && '::' == tk[:text]
|
2010-04-01 03:45:16 -04:00
|
|
|
rhs_name << '::'
|
|
|
|
end
|
|
|
|
tk = get_tk
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
body ? body : get_tkread_clean(/^[ \t]+/, '')
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Generates an RDoc::Method or RDoc::Attr from +comment+ by looking for
|
|
|
|
# :method: or :attr: directives in +comment+.
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def parse_comment container, tk, comment
|
|
|
|
return parse_comment_tomdoc container, tk, comment if @markup == 'tomdoc'
|
2017-09-11 23:42:54 -04:00
|
|
|
column = tk[:char_no]
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no = comment.line.nil? ? tk[:line_no] : comment.line
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
comment.text = comment.text.sub(/(^# +:?)(singleton-)(method:)/, '\1\3')
|
|
|
|
singleton = !!$~
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
co =
|
2017-11-27 05:45:24 -05:00
|
|
|
if (comment.text = comment.text.sub(/^# +:?method: *(\S*).*?\n/i, '')) && !!$~ then
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no += $`.count("\n")
|
2017-11-27 05:45:24 -05:00
|
|
|
parse_comment_ghost container, comment.text, $1, column, line_no, comment
|
|
|
|
elsif (comment.text = comment.text.sub(/# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i, '')) && !!$~ then
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_comment_attr container, $1, $3, comment
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if co then
|
|
|
|
co.singleton = singleton
|
|
|
|
co.line = line_no
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
true
|
|
|
|
end
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Parse a comment that is describing an attribute in +container+ with the
|
|
|
|
# given +name+ and +comment+.
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_comment_attr container, type, name, comment # :nodoc:
|
|
|
|
return if name.empty?
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
rw = case type
|
|
|
|
when 'attr_reader' then 'R'
|
|
|
|
when 'attr_writer' then 'W'
|
|
|
|
else 'RW'
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
create_attr container, NORMAL, name, rw, comment
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_comment_ghost container, text, name, column, line_no, # :nodoc:
|
|
|
|
comment
|
|
|
|
name = nil if name.empty?
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
meth = RDoc::GhostMethod.new get_tkread, name
|
|
|
|
record_location meth
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
meth.start_collecting_tokens
|
2018-08-26 21:30:18 -04:00
|
|
|
indent = RDoc::Parser::RipperStateLex::Token.new(1, 1, :on_sp, ' ' * column)
|
|
|
|
position_comment = RDoc::Parser::RipperStateLex::Token.new(line_no, 1, :on_comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
position_comment[:text] = "# File #{@top_level.relative_name}, line #{line_no}"
|
2018-08-26 21:30:18 -04:00
|
|
|
newline = RDoc::Parser::RipperStateLex::Token.new(0, 0, :on_nl, "\n")
|
2017-09-11 23:42:54 -04:00
|
|
|
meth.add_tokens [position_comment, newline, indent]
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
meth.params =
|
|
|
|
if text.sub!(/^#\s+:?args?:\s*(.*?)\s*$/i, '') then
|
|
|
|
$1
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
comment.normalize
|
|
|
|
comment.extract_call_seq meth
|
2010-12-28 17:08:56 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless meth.name
|
|
|
|
|
|
|
|
container.add_method meth
|
|
|
|
|
|
|
|
meth.comment = comment
|
|
|
|
|
|
|
|
@stats.add_method meth
|
|
|
|
|
|
|
|
meth
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
|
|
|
# Creates an RDoc::Method on +container+ from +comment+ if there is a
|
|
|
|
# Signature section in the comment
|
|
|
|
|
|
|
|
def parse_comment_tomdoc container, tk, comment
|
|
|
|
return unless signature = RDoc::TomDoc.signature(comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
column = tk[:char_no]
|
|
|
|
line_no = tk[:line_no]
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
name, = signature.split %r%[ \(]%, 2
|
|
|
|
|
|
|
|
meth = RDoc::GhostMethod.new get_tkread, name
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location meth
|
2012-11-26 23:28:14 -05:00
|
|
|
meth.line = line_no
|
|
|
|
|
|
|
|
meth.start_collecting_tokens
|
2018-08-26 21:30:18 -04:00
|
|
|
indent = RDoc::Parser::RipperStateLex::Token.new(1, 1, :on_sp, ' ' * column)
|
|
|
|
position_comment = RDoc::Parser::RipperStateLex::Token.new(line_no, 1, :on_comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
position_comment[:text] = "# File #{@top_level.relative_name}, line #{line_no}"
|
2018-08-26 21:30:18 -04:00
|
|
|
newline = RDoc::Parser::RipperStateLex::Token.new(0, 0, :on_nl, "\n")
|
2017-09-11 23:42:54 -04:00
|
|
|
meth.add_tokens [position_comment, newline, indent]
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
meth.call_seq = signature
|
|
|
|
|
|
|
|
comment.normalize
|
|
|
|
|
|
|
|
return unless meth.name
|
|
|
|
|
|
|
|
container.add_method meth
|
|
|
|
|
|
|
|
meth.comment = comment
|
|
|
|
|
|
|
|
@stats.add_method meth
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2013-09-18 19:33:36 -04:00
|
|
|
# Parses an +include+ or +extend+, indicated by the +klass+ and adds it to
|
|
|
|
# +container+ # with +comment+
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_extend_or_include klass, container, comment # :nodoc:
|
2008-07-17 20:46:16 -04:00
|
|
|
loop do
|
|
|
|
skip_tkspace_comment
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2018-08-26 21:30:18 -04:00
|
|
|
name = get_included_module_with_optional_parens
|
2011-06-16 00:59:24 -04:00
|
|
|
|
|
|
|
unless name.empty? then
|
2013-09-18 19:33:36 -04:00
|
|
|
obj = container.add klass, name, comment
|
|
|
|
record_location obj
|
2011-06-16 00:59:24 -04:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
return if peek_tk.nil? || :on_comma != peek_tk[:kind]
|
2011-06-16 00:59:24 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
get_tk
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2021-06-29 07:01:46 -04:00
|
|
|
##
|
|
|
|
# Parses an +included+ with a block feature of ActiveSupport::Concern.
|
|
|
|
|
|
|
|
def parse_included_with_activesupport_concern container, comment # :nodoc:
|
|
|
|
skip_tkspace_without_nl
|
|
|
|
tk = get_tk
|
|
|
|
unless tk[:kind] == :on_lbracket || (tk[:kind] == :on_kw && tk[:text] == 'do')
|
|
|
|
unget_tk tk
|
|
|
|
return nil # should be a block
|
|
|
|
end
|
|
|
|
|
|
|
|
parse_statements container
|
|
|
|
|
|
|
|
container
|
|
|
|
end
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
##
|
2013-09-18 19:33:36 -04:00
|
|
|
# Parses identifiers that can create new methods or change visibility.
|
|
|
|
#
|
|
|
|
# Returns true if the comment was not consumed.
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_identifier container, single, tk, comment # :nodoc:
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
when 'private', 'protected', 'public', 'private_class_method',
|
|
|
|
'public_class_method', 'module_function' then
|
|
|
|
parse_visibility container, single, tk
|
|
|
|
return true
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'private_constant', 'public_constant'
|
|
|
|
parse_constant_visibility container, single, tk
|
|
|
|
return true
|
2013-09-18 19:33:36 -04:00
|
|
|
when 'attr' then
|
|
|
|
parse_attr container, single, tk, comment
|
|
|
|
when /^attr_(reader|writer|accessor)$/ then
|
|
|
|
parse_attr_accessor container, single, tk, comment
|
|
|
|
when 'alias_method' then
|
|
|
|
parse_alias container, single, tk, comment
|
|
|
|
when 'require', 'include' then
|
|
|
|
# ignore
|
|
|
|
else
|
|
|
|
if comment.text =~ /\A#\#$/ then
|
|
|
|
case comment.text
|
|
|
|
when /^# +:?attr(_reader|_writer|_accessor)?:/ then
|
|
|
|
parse_meta_attr container, single, tk, comment
|
|
|
|
else
|
|
|
|
method = parse_meta_method container, single, tk, comment
|
|
|
|
method.params = container.params if
|
|
|
|
container.params
|
|
|
|
method.block_params = container.block_params if
|
|
|
|
container.block_params
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
false
|
2012-11-26 23:28:14 -05:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
|
|
|
# Parses a meta-programmed attribute and creates an RDoc::Attr.
|
|
|
|
#
|
|
|
|
# To create foo and bar attributes on class C with comment "My attributes":
|
|
|
|
#
|
|
|
|
# class C
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# ##
|
|
|
|
# # :attr:
|
|
|
|
# #
|
|
|
|
# # My attributes
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# my_attr :foo, :bar
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
# To create a foo attribute on class C with comment "My attribute":
|
|
|
|
#
|
|
|
|
# class C
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# ##
|
|
|
|
# # :attr: foo
|
|
|
|
# #
|
|
|
|
# # My attribute
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# my_attr :foo, :bar
|
2010-04-02 00:40:47 -04:00
|
|
|
#
|
2010-04-01 03:45:16 -04:00
|
|
|
# end
|
|
|
|
|
|
|
|
def parse_meta_attr(context, single, tk, comment)
|
|
|
|
args = parse_symbol_arg
|
|
|
|
rw = "?"
|
|
|
|
|
|
|
|
# If nodoc is given, don't document any of them
|
|
|
|
|
|
|
|
tmp = RDoc::CodeObject.new
|
|
|
|
read_documentation_modifiers tmp, RDoc::ATTR_MODIFIERS
|
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
regexp = /^# +:?(attr(_reader|_writer|_accessor)?): *(\S*).*?\n/i
|
|
|
|
if regexp =~ comment.text then
|
|
|
|
comment.text = comment.text.sub(regexp, '')
|
2010-04-01 03:45:16 -04:00
|
|
|
rw = case $1
|
|
|
|
when 'attr_reader' then 'R'
|
|
|
|
when 'attr_writer' then 'W'
|
|
|
|
else 'RW'
|
|
|
|
end
|
|
|
|
name = $3 unless $3.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
if name then
|
2013-09-18 19:33:36 -04:00
|
|
|
att = create_attr context, single, name, rw, comment
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
args.each do |attr_name|
|
2013-09-18 19:33:36 -04:00
|
|
|
att = create_attr context, single, attr_name, rw, comment
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
att
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Parses a meta-programmed method
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_meta_method(container, single, tk, comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
column = tk[:char_no]
|
|
|
|
line_no = tk[:line_no]
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
start_collecting_tokens
|
|
|
|
add_token tk
|
|
|
|
add_token_listener self
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
comment.text = comment.text.sub(/(^# +:?)(singleton-)(method:)/, '\1\3')
|
|
|
|
singleton = !!$~
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
name = parse_meta_method_name comment, tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless name
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
meth = RDoc::MetaMethod.new get_tkread, name
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location meth
|
2011-02-01 19:32:30 -05:00
|
|
|
meth.line = line_no
|
2008-07-17 20:46:16 -04:00
|
|
|
meth.singleton = singleton
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
remove_token_listener self
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
meth.start_collecting_tokens
|
2018-08-26 21:30:18 -04:00
|
|
|
indent = RDoc::Parser::RipperStateLex::Token.new(1, 1, :on_sp, ' ' * column)
|
|
|
|
position_comment = RDoc::Parser::RipperStateLex::Token.new(line_no, 1, :on_comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
position_comment[:text] = "# File #{@top_level.relative_name}, line #{line_no}"
|
2018-08-26 21:30:18 -04:00
|
|
|
newline = RDoc::Parser::RipperStateLex::Token.new(0, 0, :on_nl, "\n")
|
2017-09-11 23:42:54 -04:00
|
|
|
meth.add_tokens [position_comment, newline, indent]
|
2008-07-17 20:46:16 -04:00
|
|
|
meth.add_tokens @token_stream
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_meta_method_params container, single, meth, tk, comment
|
|
|
|
|
|
|
|
meth.comment = comment
|
|
|
|
|
|
|
|
@stats.add_method meth
|
|
|
|
|
|
|
|
meth
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parses the name of a metaprogrammed method. +comment+ is used to
|
|
|
|
# determine the name while +tk+ is used in an error message if the name
|
|
|
|
# cannot be determined.
|
|
|
|
|
|
|
|
def parse_meta_method_name comment, tk # :nodoc:
|
|
|
|
if comment.text.sub!(/^# +:?method: *(\S*).*?\n/i, '') then
|
|
|
|
return $1 unless $1.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
name_t = get_tk
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_symbol == name_t[:kind] then
|
|
|
|
name_t[:text][1..-1]
|
|
|
|
elsif :on_tstring == name_t[:kind] then
|
|
|
|
name_t[:text][1..-2]
|
|
|
|
elsif :on_op == name_t[:kind] && '=' == name_t[:text] then # ignore
|
2013-09-18 19:33:36 -04:00
|
|
|
remove_token_listener self
|
|
|
|
|
|
|
|
nil
|
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
warn "unknown name token #{name_t.inspect} for meta-method '#{tk[:text]}'"
|
2013-09-18 19:33:36 -04:00
|
|
|
'unknown'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parses the parameters and block for a meta-programmed method.
|
|
|
|
|
|
|
|
def parse_meta_method_params container, single, meth, tk, comment # :nodoc:
|
2010-04-01 03:45:16 -04:00
|
|
|
token_listener meth do
|
|
|
|
meth.params = ''
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2017-11-27 05:45:24 -05:00
|
|
|
look_for_directives_in meth, comment
|
2012-11-26 23:28:14 -05:00
|
|
|
comment.normalize
|
|
|
|
comment.extract_call_seq meth
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2011-06-29 17:17:31 -04:00
|
|
|
container.add_method meth
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
last_tk = tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
while tk = get_tk do
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_semicolon == tk[:kind] then
|
2010-04-01 03:45:16 -04:00
|
|
|
break
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_nl == tk[:kind] then
|
|
|
|
break unless last_tk and :on_comma == last_tk[:kind]
|
|
|
|
elsif :on_sp == tk[:kind] then
|
2010-04-01 03:45:16 -04:00
|
|
|
# expression continues
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_kw == tk[:kind] && 'do' == tk[:text] then
|
2011-02-01 19:32:30 -05:00
|
|
|
parse_statements container, single, meth
|
|
|
|
break
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
|
|
|
last_tk = tk
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
2010-04-01 03:45:16 -04:00
|
|
|
# Parses a normal method defined by +def+
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_method(container, single, tk, comment)
|
2013-09-18 19:33:36 -04:00
|
|
|
singleton = nil
|
|
|
|
added_container = false
|
2010-04-01 03:45:16 -04:00
|
|
|
name = nil
|
2017-09-11 23:42:54 -04:00
|
|
|
column = tk[:char_no]
|
|
|
|
line_no = tk[:line_no]
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
start_collecting_tokens
|
2010-04-01 03:45:16 -04:00
|
|
|
add_token tk
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
token_listener self do
|
2013-09-18 19:33:36 -04:00
|
|
|
prev_container = container
|
|
|
|
name, container, singleton = parse_method_name container
|
|
|
|
added_container = container != prev_container
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
return unless name
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
meth = RDoc::AnyMethod.new get_tkread, name
|
2017-11-27 05:45:24 -05:00
|
|
|
look_for_directives_in meth, comment
|
2014-09-04 21:41:25 -04:00
|
|
|
meth.singleton = single == SINGLE ? true : singleton
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location meth
|
2011-02-01 19:32:30 -05:00
|
|
|
meth.line = line_no
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
meth.start_collecting_tokens
|
2018-08-26 21:30:18 -04:00
|
|
|
indent = RDoc::Parser::RipperStateLex::Token.new(1, 1, :on_sp, ' ' * column)
|
|
|
|
token = RDoc::Parser::RipperStateLex::Token.new(line_no, 1, :on_comment)
|
2017-09-11 23:42:54 -04:00
|
|
|
token[:text] = "# File #{@top_level.relative_name}, line #{line_no}"
|
2018-08-26 21:30:18 -04:00
|
|
|
newline = RDoc::Parser::RipperStateLex::Token.new(0, 0, :on_nl, "\n")
|
2017-09-11 23:42:54 -04:00
|
|
|
meth.add_tokens [token, newline, indent]
|
2008-07-17 20:46:16 -04:00
|
|
|
meth.add_tokens @token_stream
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_method_params_and_body container, single, meth, added_container
|
|
|
|
|
|
|
|
comment.normalize
|
|
|
|
comment.extract_call_seq meth
|
|
|
|
|
|
|
|
meth.comment = comment
|
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
# after end modifiers
|
|
|
|
read_documentation_modifiers meth, RDoc::METHOD_MODIFIERS
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
@stats.add_method meth
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Parses the parameters and body of +meth+
|
|
|
|
|
|
|
|
def parse_method_params_and_body container, single, meth, added_container
|
2010-04-01 03:45:16 -04:00
|
|
|
token_listener meth do
|
|
|
|
parse_method_parameters meth
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if meth.document_self or not @track_visibility then
|
2010-04-01 03:45:16 -04:00
|
|
|
container.add_method meth
|
|
|
|
elsif added_container then
|
|
|
|
container.document_self = false
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
# Having now read the method parameters and documentation modifiers, we
|
|
|
|
# now know whether we have to rename #initialize to ::new
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
if meth.name == "initialize" && !meth.singleton then
|
2010-04-01 03:45:16 -04:00
|
|
|
if meth.dont_rename_initialize then
|
|
|
|
meth.visibility = :protected
|
|
|
|
else
|
|
|
|
meth.singleton = true
|
|
|
|
meth.name = "new"
|
|
|
|
meth.visibility = :public
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
parse_statements container, single, meth
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2012-12-20 01:23:28 -05:00
|
|
|
##
|
|
|
|
# Parses a method that needs to be ignored.
|
|
|
|
|
|
|
|
def parse_method_dummy container
|
|
|
|
dummy = RDoc::Context.new
|
|
|
|
dummy.parent = container
|
|
|
|
dummy.store = container.store
|
|
|
|
skip_method dummy
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Parses the name of a method in +container+.
|
|
|
|
#
|
|
|
|
# Returns the method name, the container it is in (for def Foo.name) and if
|
|
|
|
# it is a singleton or regular method.
|
|
|
|
|
|
|
|
def parse_method_name container # :nodoc:
|
|
|
|
skip_tkspace
|
|
|
|
name_t = get_tk
|
2018-10-17 02:28:20 -04:00
|
|
|
back_tk = skip_tkspace_without_nl
|
2013-09-18 19:33:36 -04:00
|
|
|
singleton = false
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
dot = get_tk
|
|
|
|
if dot[:kind] == :on_period || (dot[:kind] == :on_op && dot[:text] == '::') then
|
2013-09-18 19:33:36 -04:00
|
|
|
singleton = true
|
|
|
|
|
|
|
|
name, container = parse_method_name_singleton container, name_t
|
|
|
|
else
|
|
|
|
unget_tk dot
|
|
|
|
back_tk.reverse_each do |token|
|
|
|
|
unget_tk token
|
|
|
|
end
|
|
|
|
|
|
|
|
name = parse_method_name_regular container, name_t
|
|
|
|
end
|
|
|
|
|
|
|
|
return name, container, singleton
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# For the given +container+ and initial name token +name_t+ the method name
|
|
|
|
# is parsed from the token stream for a regular method.
|
|
|
|
|
|
|
|
def parse_method_name_regular container, name_t # :nodoc:
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_op == name_t[:kind] && (%w{* & [] []= <<}.include?(name_t[:text])) then
|
|
|
|
name_t[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
unless [:on_kw, :on_const, :on_ident].include?(name_t[:kind]) then
|
2013-09-18 19:33:36 -04:00
|
|
|
warn "expected method name token, . or ::, got #{name_t.inspect}"
|
|
|
|
skip_method container
|
|
|
|
return
|
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
name_t[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# For the given +container+ and initial name token +name_t+ the method name
|
|
|
|
# and the new +container+ (if necessary) are parsed from the token stream
|
|
|
|
# for a singleton method.
|
|
|
|
|
|
|
|
def parse_method_name_singleton container, name_t # :nodoc:
|
|
|
|
skip_tkspace
|
|
|
|
name_t2 = get_tk
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if (:on_kw == name_t[:kind] && 'self' == name_t[:text]) || (:on_op == name_t[:kind] && '%' == name_t[:text]) then
|
2017-11-27 05:45:24 -05:00
|
|
|
# NOTE: work around '[' being consumed early
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_lbracket == name_t2[:kind]
|
|
|
|
get_tk
|
|
|
|
name = '[]'
|
|
|
|
else
|
|
|
|
name = name_t2[:text]
|
|
|
|
end
|
|
|
|
elsif :on_const == name_t[:kind] then
|
|
|
|
name = name_t2[:text]
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
container = get_method_container container, name_t
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
return unless container
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name
|
|
|
|
elsif :on_ident == name_t[:kind] || :on_ivar == name_t[:kind] || :on_gvar == name_t[:kind] then
|
|
|
|
parse_method_dummy container
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = nil
|
|
|
|
elsif (:on_kw == name_t[:kind]) && ('true' == name_t[:text] || 'false' == name_t[:text] || 'nil' == name_t[:text]) then
|
|
|
|
klass_name = "#{name_t[:text].capitalize}Class"
|
|
|
|
container = @store.find_class_named klass_name
|
|
|
|
container ||= @top_level.add_class RDoc::NormalClass, klass_name
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = name_t2[:text]
|
|
|
|
else
|
|
|
|
warn "unexpected method name token #{name_t.inspect}"
|
|
|
|
# break
|
|
|
|
skip_method container
|
2013-09-18 19:33:36 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = nil
|
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
return name, container
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Extracts +yield+ parameters from +method+
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_method_or_yield_parameters(method = nil,
|
|
|
|
modifiers = RDoc::METHOD_MODIFIERS)
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
2013-09-18 19:33:36 -04:00
|
|
|
end_token = get_end_token tk
|
|
|
|
return '' unless end_token
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
nest = 0
|
2017-09-11 23:42:54 -04:00
|
|
|
continue = false
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
while tk != nil do
|
|
|
|
case tk[:kind]
|
|
|
|
when :on_semicolon then
|
2010-12-19 22:22:49 -05:00
|
|
|
break if nest == 0
|
2018-08-26 21:30:18 -04:00
|
|
|
when :on_lbracket then
|
|
|
|
nest += 1
|
|
|
|
when :on_rbracket then
|
|
|
|
nest -= 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_lbrace then
|
2010-04-01 03:45:16 -04:00
|
|
|
nest += 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_rbrace then
|
2010-04-01 03:45:16 -04:00
|
|
|
nest -= 1
|
2010-12-19 22:22:49 -05:00
|
|
|
if nest <= 0
|
|
|
|
# we might have a.each { |i| yield i }
|
|
|
|
unget_tk(tk) if nest < 0
|
|
|
|
break
|
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_lparen then
|
2010-04-01 03:45:16 -04:00
|
|
|
nest += 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when end_token[:kind] then
|
|
|
|
if end_token[:kind] == :on_rparen
|
2008-07-17 20:46:16 -04:00
|
|
|
nest -= 1
|
2012-11-29 18:34:29 -05:00
|
|
|
break if nest <= 0
|
2010-04-01 03:45:16 -04:00
|
|
|
else
|
2017-09-11 23:42:54 -04:00
|
|
|
break
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_rparen then
|
2010-12-19 22:22:49 -05:00
|
|
|
nest -= 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_comment, :on_embdoc then
|
2010-04-01 03:45:16 -04:00
|
|
|
@read.pop
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_nl == end_token[:kind] and "\n" == tk[:text][-1] and
|
2018-08-26 21:30:18 -04:00
|
|
|
(!continue or (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) != 0) then
|
2017-09-11 23:42:54 -04:00
|
|
|
if method && method.block_params.nil? then
|
|
|
|
unget_tk tk
|
|
|
|
read_documentation_modifiers method, modifiers
|
|
|
|
end
|
|
|
|
break if !continue and nest <= 0
|
|
|
|
end
|
|
|
|
when :on_comma then
|
|
|
|
continue = true
|
|
|
|
when :on_ident then
|
|
|
|
continue = false if continue
|
2010-04-01 03:45:16 -04:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
|
|
|
end
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
get_tkread_clean(/\s+/, ' ')
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
##
|
|
|
|
# Capture the method's parameters. Along the way, look for a comment
|
|
|
|
# containing:
|
|
|
|
#
|
|
|
|
# # yields: ....
|
|
|
|
#
|
|
|
|
# and add this as the block_params for the method
|
|
|
|
|
2012-12-08 16:19:22 -05:00
|
|
|
def parse_method_parameters method
|
2010-04-01 03:45:16 -04:00
|
|
|
res = parse_method_or_yield_parameters method
|
|
|
|
|
|
|
|
res = "(#{res})" unless res =~ /\A\(/
|
2008-07-17 20:46:16 -04:00
|
|
|
method.params = res unless method.params
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-12-08 16:19:22 -05:00
|
|
|
return if method.block_params
|
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-12-08 16:19:22 -05:00
|
|
|
read_documentation_modifiers method, RDoc::METHOD_MODIFIERS
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Parses an RDoc::NormalModule in +container+ with +comment+
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def parse_module container, single, tk, comment
|
2010-12-19 22:22:49 -05:00
|
|
|
container, name_t, = get_class_or_module container
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = name_t[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
mod = container.add_module RDoc::NormalModule, name
|
2012-11-26 23:28:14 -05:00
|
|
|
mod.ignore unless container.document_children
|
2013-09-18 19:33:36 -04:00
|
|
|
record_location mod
|
2008-07-21 14:35:14 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
read_documentation_modifiers mod, RDoc::CLASS_MODIFIERS
|
2011-06-29 17:17:31 -04:00
|
|
|
mod.add_comment comment, @top_level
|
2012-11-26 23:28:14 -05:00
|
|
|
parse_statements mod
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-08-29 07:52:50 -04:00
|
|
|
# after end modifiers
|
|
|
|
read_documentation_modifiers mod, RDoc::CLASS_MODIFIERS
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
@stats.add_module mod
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Parses an RDoc::Require in +context+ containing +comment+
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
def parse_require(context, comment)
|
|
|
|
skip_tkspace_comment
|
|
|
|
tk = get_tk
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_lparen == tk[:kind] then
|
2003-12-01 02:12:49 -05:00
|
|
|
skip_tkspace_comment
|
|
|
|
tk = get_tk
|
|
|
|
end
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
name = tk[:text][1..-2] if :on_tstring == tk[:kind]
|
2010-04-01 03:45:16 -04:00
|
|
|
|
|
|
|
if name then
|
2010-12-19 22:22:49 -05:00
|
|
|
@top_level.add_require RDoc::Require.new(name, comment)
|
2004-10-09 23:35:51 -04:00
|
|
|
else
|
2010-04-01 03:45:16 -04:00
|
|
|
unget_tk tk
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-07 00:22:50 -05:00
|
|
|
##
|
|
|
|
# Parses a rescue
|
|
|
|
|
|
|
|
def parse_rescue
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-12-07 00:22:50 -05:00
|
|
|
|
|
|
|
while tk = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:kind]
|
|
|
|
when :on_nl, :on_semicolon, :on_comment then
|
2012-12-07 00:22:50 -05:00
|
|
|
break
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_comma then
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-12-07 00:22:50 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
get_tk if :on_nl == peek_tk[:kind]
|
2012-12-07 00:22:50 -05:00
|
|
|
end
|
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-12-07 00:22:50 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-26 21:30:18 -04:00
|
|
|
##
|
|
|
|
# Retrieve comment body without =begin/=end
|
|
|
|
|
|
|
|
def retrieve_comment_body(tk)
|
|
|
|
if :on_embdoc == tk[:kind]
|
|
|
|
tk[:text].gsub(/\A=begin.*\n/, '').gsub(/=end\n?\z/, '')
|
|
|
|
else
|
|
|
|
tk[:text]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
##
|
2013-07-18 22:26:08 -04:00
|
|
|
# The core of the Ruby parser.
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_statements(container, single = NORMAL, current_method = nil,
|
2012-11-26 23:28:14 -05:00
|
|
|
comment = new_comment(''))
|
|
|
|
raise 'no' unless RDoc::Comment === comment
|
2017-11-27 05:45:24 -05:00
|
|
|
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
|
2011-02-01 19:32:30 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
nest = 1
|
|
|
|
save_visibility = container.visibility
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
non_comment_seen = true
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
while tk = get_tk do
|
|
|
|
keep_comment = false
|
2011-02-01 19:32:30 -05:00
|
|
|
try_parse_comment = false
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
non_comment_seen = true unless (:on_comment == tk[:kind] or :on_embdoc == tk[:kind])
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:kind]
|
|
|
|
when :on_nl, :on_ignored_nl, :on_comment, :on_embdoc then
|
|
|
|
if :on_nl == tk[:kind] or :on_ignored_nl == tk[:kind]
|
|
|
|
skip_tkspace
|
|
|
|
tk = get_tk
|
|
|
|
else
|
|
|
|
past_tokens = @read.size > 1 ? @read[0..-2] : []
|
|
|
|
nl_position = 0
|
|
|
|
past_tokens.reverse.each_with_index do |read_tk, i|
|
|
|
|
if read_tk =~ /^\n$/ then
|
|
|
|
nl_position = (past_tokens.size - 1) - i
|
|
|
|
break
|
|
|
|
elsif read_tk =~ /^#.*\n$/ then
|
|
|
|
nl_position = ((past_tokens.size - 1) - i) + 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
comment_only_line = past_tokens[nl_position..-1].all?{ |c| c =~ /^\s+$/ }
|
|
|
|
unless comment_only_line then
|
|
|
|
tk = get_tk
|
|
|
|
end
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) then
|
2008-07-17 20:46:16 -04:00
|
|
|
if non_comment_seen then
|
|
|
|
# Look for RDoc in a comment about to be thrown away
|
2010-12-28 17:08:56 -05:00
|
|
|
non_comment_seen = parse_comment container, tk, comment unless
|
|
|
|
comment.empty?
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
comment = ''
|
2017-11-27 05:45:24 -05:00
|
|
|
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
|
|
|
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no = nil
|
2017-09-11 23:42:54 -04:00
|
|
|
while tk and (:on_comment == tk[:kind] or :on_embdoc == tk[:kind]) do
|
2018-08-26 21:30:18 -04:00
|
|
|
comment_body = retrieve_comment_body(tk)
|
2019-11-26 21:58:38 -05:00
|
|
|
line_no = tk[:line_no] if comment.empty?
|
2018-08-26 21:30:18 -04:00
|
|
|
comment += comment_body
|
2019-08-15 22:36:47 -04:00
|
|
|
comment << "\n" unless comment_body =~ /\n\z/
|
2011-06-16 00:59:24 -04:00
|
|
|
|
2019-08-15 22:36:47 -04:00
|
|
|
if comment_body.size > 1 && comment_body =~ /\n\z/ then
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl # leading spaces
|
2011-06-16 00:59:24 -04:00
|
|
|
end
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = get_tk
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2019-11-26 21:58:38 -05:00
|
|
|
comment = new_comment comment, line_no
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
unless comment.empty? then
|
|
|
|
look_for_directives_in container, comment
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
if container.done_documenting then
|
2013-09-18 19:33:36 -04:00
|
|
|
throw :eof if RDoc::TopLevel === container
|
2008-07-17 20:46:16 -04:00
|
|
|
container.ongoing_visibility = save_visibility
|
|
|
|
end
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
keep_comment = true
|
|
|
|
else
|
|
|
|
non_comment_seen = true
|
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
unget_tk tk
|
2008-07-17 20:46:16 -04:00
|
|
|
keep_comment = true
|
2017-02-24 02:39:37 -05:00
|
|
|
container.current_line_visibility = nil
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_kw then
|
|
|
|
case tk[:text]
|
|
|
|
when 'class' then
|
|
|
|
parse_class container, single, tk, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'module' then
|
|
|
|
parse_module container, single, tk, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'def' then
|
|
|
|
parse_method container, single, tk, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'alias' then
|
|
|
|
parse_alias container, single, tk, comment unless current_method
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'yield' then
|
|
|
|
if current_method.nil? then
|
|
|
|
warn "Warning: yield outside of method" if container.document_self
|
|
|
|
else
|
|
|
|
parse_yield container, single, tk, current_method
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'until', 'while' then
|
2018-08-26 21:30:18 -04:00
|
|
|
if (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) == 0
|
2017-09-11 23:42:54 -04:00
|
|
|
nest += 1
|
|
|
|
skip_optional_do_after_expression
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
# Until and While can have a 'do', which shouldn't increase the nesting.
|
|
|
|
# We can't solve the general case, but we can handle most occurrences by
|
|
|
|
# ignoring a do at the end of a line.
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
# 'for' is trickier
|
|
|
|
when 'for' then
|
|
|
|
nest += 1
|
|
|
|
skip_for_variable
|
|
|
|
skip_optional_do_after_expression
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'case', 'do', 'if', 'unless', 'begin' then
|
2018-08-26 21:30:18 -04:00
|
|
|
if (tk[:state] & RDoc::Parser::RipperStateLex::EXPR_LABEL) == 0
|
2017-09-11 23:42:54 -04:00
|
|
|
nest += 1
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'super' then
|
|
|
|
current_method.calls_super = true if current_method
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'rescue' then
|
|
|
|
parse_rescue
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when 'end' then
|
|
|
|
nest -= 1
|
|
|
|
if nest == 0 then
|
|
|
|
container.ongoing_visibility = save_visibility
|
|
|
|
|
|
|
|
parse_comment container, tk, comment unless comment.empty?
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
when :on_const then
|
|
|
|
unless parse_constant container, tk, comment, current_method then
|
|
|
|
try_parse_comment = true
|
|
|
|
end
|
2012-12-07 00:22:50 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_ident then
|
2008-07-17 20:46:16 -04:00
|
|
|
if nest == 1 and current_method.nil? then
|
2013-09-18 19:33:36 -04:00
|
|
|
keep_comment = parse_identifier container, single, tk, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
case tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
when "require" then
|
|
|
|
parse_require container, comment
|
|
|
|
when "include" then
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_extend_or_include RDoc::Include, container, comment
|
2012-11-26 23:28:14 -05:00
|
|
|
when "extend" then
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_extend_or_include RDoc::Extend, container, comment
|
2021-06-29 07:01:46 -04:00
|
|
|
when "included" then
|
|
|
|
parse_included_with_activesupport_concern container, comment
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-28 17:08:56 -05:00
|
|
|
else
|
2011-02-01 19:32:30 -05:00
|
|
|
try_parse_comment = nest == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
if try_parse_comment then
|
2010-12-28 17:08:56 -05:00
|
|
|
non_comment_seen = parse_comment container, tk, comment unless
|
|
|
|
comment.empty?
|
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
keep_comment = false
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2011-02-01 19:32:30 -05:00
|
|
|
unless keep_comment then
|
2012-11-26 23:28:14 -05:00
|
|
|
comment = new_comment ''
|
2017-11-27 05:45:24 -05:00
|
|
|
comment = RDoc::Encoding.change_encoding comment, @encoding if @encoding
|
2012-11-26 23:28:14 -05:00
|
|
|
container.params = nil
|
|
|
|
container.block_params = nil
|
2011-02-01 19:32:30 -05:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
consume_trailing_spaces
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
2012-11-26 23:28:14 -05:00
|
|
|
|
|
|
|
container.params = nil
|
|
|
|
container.block_params = nil
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Parse up to +no+ symbol arguments
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def parse_symbol_arg(no = nil)
|
|
|
|
skip_tkspace_comment
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = get_tk
|
|
|
|
if tk[:kind] == :on_lparen
|
2013-09-18 19:33:36 -04:00
|
|
|
parse_symbol_arg_paren no
|
|
|
|
else
|
|
|
|
parse_symbol_arg_space no, tk
|
|
|
|
end
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Parses up to +no+ symbol arguments surrounded by () and places them in
|
|
|
|
# +args+.
|
|
|
|
|
|
|
|
def parse_symbol_arg_paren no # :nodoc:
|
|
|
|
args = []
|
|
|
|
|
|
|
|
loop do
|
|
|
|
skip_tkspace_comment
|
|
|
|
if tk1 = parse_symbol_in_arg
|
|
|
|
args.push tk1
|
|
|
|
break if no and args.size >= no
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
skip_tkspace_comment
|
2017-09-11 23:42:54 -04:00
|
|
|
case (tk2 = get_tk)[:kind]
|
|
|
|
when :on_rparen
|
2013-09-18 19:33:36 -04:00
|
|
|
break
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_comma
|
2013-09-18 19:33:36 -04:00
|
|
|
else
|
|
|
|
warn("unexpected token: '#{tk2.inspect}'") if $DEBUG_RDOC
|
|
|
|
break
|
2008-01-13 22:34:05 -05:00
|
|
|
end
|
2013-09-18 19:33:36 -04:00
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
args
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Parses up to +no+ symbol arguments separated by spaces and places them in
|
|
|
|
# +args+.
|
2008-01-13 22:34:05 -05:00
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
def parse_symbol_arg_space no, tk # :nodoc:
|
|
|
|
args = []
|
|
|
|
|
|
|
|
unget_tk tk
|
|
|
|
if tk = parse_symbol_in_arg
|
|
|
|
args.push tk
|
|
|
|
return args if no and args.size >= no
|
|
|
|
end
|
|
|
|
|
|
|
|
loop do
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2013-09-18 19:33:36 -04:00
|
|
|
|
|
|
|
tk1 = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
if tk1.nil? || :on_comma != tk1[:kind] then
|
2013-09-18 19:33:36 -04:00
|
|
|
unget_tk tk1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
skip_tkspace_comment
|
|
|
|
if tk = parse_symbol_in_arg
|
|
|
|
args.push tk
|
|
|
|
break if no and args.size >= no
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
args
|
|
|
|
end
|
2003-12-01 02:12:49 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Returns symbol text from the next token
|
|
|
|
|
2008-01-13 22:34:05 -05:00
|
|
|
def parse_symbol_in_arg
|
2017-09-11 23:42:54 -04:00
|
|
|
tk = get_tk
|
|
|
|
if :on_symbol == tk[:kind] then
|
|
|
|
tk[:text].sub(/^:/, '')
|
|
|
|
elsif :on_tstring == tk[:kind] then
|
|
|
|
tk[:text][1..-2]
|
|
|
|
elsif :on_dstring == tk[:kind] or :on_ident == tk[:kind] then
|
2010-12-19 22:22:49 -05:00
|
|
|
nil # ignore
|
2008-01-13 22:34:05 -05:00
|
|
|
else
|
|
|
|
warn("Expected symbol or string, got #{tk.inspect}") if $DEBUG_RDOC
|
|
|
|
nil
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2011-06-29 17:17:31 -04:00
|
|
|
# Parses statements in the top-level +container+
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
def parse_top_level_statements container
|
2008-07-17 20:46:16 -04:00
|
|
|
comment = collect_first_comment
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
look_for_directives_in container, comment
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
throw :eof if container.done_documenting
|
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
@markup = comment.format
|
|
|
|
|
2011-06-29 17:17:31 -04:00
|
|
|
# HACK move if to RDoc::Context#comment=
|
2010-12-19 22:22:49 -05:00
|
|
|
container.comment = comment if container.document_self unless comment.empty?
|
2011-07-30 20:19:00 -04:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
parse_statements container, NORMAL, nil, comment
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Determines the visibility in +container+ from +tk+
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_visibility(container, single, tk)
|
2014-09-04 21:41:25 -04:00
|
|
|
vis_type, vis, singleton = get_visibility_information tk, single
|
2008-07-17 20:46:16 -04:00
|
|
|
|
|
|
|
skip_tkspace_comment false
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
ptk = peek_tk
|
|
|
|
# Ryan Davis suggested the extension to ignore modifiers, because he
|
|
|
|
# often writes
|
|
|
|
#
|
|
|
|
# protected unless $TESTING
|
|
|
|
#
|
|
|
|
if [:on_nl, :on_semicolon].include?(ptk[:kind]) || (:on_kw == ptk[:kind] && (['if', 'unless'].include?(ptk[:text]))) then
|
2008-07-17 20:46:16 -04:00
|
|
|
container.ongoing_visibility = vis
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_kw == ptk[:kind] && 'def' == ptk[:text]
|
2017-02-24 02:39:37 -05:00
|
|
|
container.current_line_visibility = vis
|
2008-07-17 20:46:16 -04:00
|
|
|
else
|
2013-09-18 19:33:36 -04:00
|
|
|
update_visibility container, vis_type, vis, singleton
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
##
|
|
|
|
# Parses a Module#private_constant or Module#public_constant call from +tk+.
|
|
|
|
|
|
|
|
def parse_constant_visibility(container, single, tk)
|
|
|
|
args = parse_symbol_arg
|
|
|
|
case tk[:text]
|
|
|
|
when 'private_constant'
|
|
|
|
vis = :private
|
|
|
|
when 'public_constant'
|
|
|
|
vis = :public
|
|
|
|
else
|
|
|
|
raise RDoc::Error, 'Unreachable'
|
|
|
|
end
|
|
|
|
container.set_constant_visibility_for args, vis
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Determines the block parameter for +context+
|
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def parse_yield(context, single, tk, method)
|
2010-04-10 02:36:13 -04:00
|
|
|
return if method.block_params
|
|
|
|
|
|
|
|
get_tkread
|
|
|
|
method.block_params = parse_method_or_yield_parameters
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Directives are modifier comments that can appear after class, module, or
|
|
|
|
# method names. For example:
|
|
|
|
#
|
|
|
|
# def fred # :yields: a, b
|
|
|
|
#
|
|
|
|
# or:
|
|
|
|
#
|
|
|
|
# class MyClass # :nodoc:
|
|
|
|
#
|
2012-11-26 23:28:14 -05:00
|
|
|
# We return the directive name and any parameters as a two element array if
|
|
|
|
# the name is in +allowed+. A directive can be found anywhere up to the end
|
|
|
|
# of the current line.
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
def read_directive allowed
|
2012-11-26 23:28:14 -05:00
|
|
|
tokens = []
|
2010-04-01 03:45:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
while tk = get_tk do
|
|
|
|
tokens << tk
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if :on_nl == tk[:kind] or (:on_kw == tk[:kind] && 'def' == tk[:text]) then
|
2013-09-18 19:33:36 -04:00
|
|
|
return
|
2017-09-11 23:42:54 -04:00
|
|
|
elsif :on_comment == tk[:kind] or :on_embdoc == tk[:kind] then
|
|
|
|
return unless tk[:text] =~ /\s*:?([\w-]+):\s*(.*)/
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
directive = $1.downcase
|
|
|
|
|
|
|
|
return [directive, $2] if allowed.include? directive
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ensure
|
2017-09-11 23:42:54 -04:00
|
|
|
unless tokens.length == 1 and (:on_comment == tokens.first[:kind] or :on_embdoc == tokens.first[:kind]) then
|
2012-11-26 23:28:14 -05:00
|
|
|
tokens.reverse_each do |token|
|
|
|
|
unget_tk token
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Handles directives following the definition for +context+ (any
|
|
|
|
# RDoc::CodeObject) if the directives are +allowed+ at this point.
|
|
|
|
#
|
|
|
|
# See also RDoc::Markup::PreProcess#handle_directive
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def read_documentation_modifiers context, allowed
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2012-11-26 23:28:14 -05:00
|
|
|
directive, value = read_directive allowed
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
return unless directive
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2011-07-30 20:19:00 -04:00
|
|
|
@preprocess.handle_directive '', directive, value, context do |dir, param|
|
|
|
|
if %w[notnew not_new not-new].include? dir then
|
|
|
|
context.dont_rename_initialize = true
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Records the location of this +container+ in the file for this parser and
|
|
|
|
# adds it to the list of classes and modules in the file.
|
|
|
|
|
|
|
|
def record_location container # :nodoc:
|
|
|
|
case container
|
|
|
|
when RDoc::ClassModule then
|
|
|
|
@top_level.add_to_classes_or_modules container
|
|
|
|
end
|
|
|
|
|
|
|
|
container.record_location @top_level
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2013-07-18 22:26:08 -04:00
|
|
|
# Scans this Ruby file for Ruby constructs
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2008-07-17 20:46:16 -04:00
|
|
|
def scan
|
|
|
|
reset
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
catch :eof do
|
2010-12-19 22:22:49 -05:00
|
|
|
begin
|
|
|
|
parse_top_level_statements @top_level
|
2012-11-26 23:28:14 -05:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
rescue StandardError => e
|
2017-12-23 18:33:09 -05:00
|
|
|
if @content.include?('<%') and @content.include?('%>') then
|
|
|
|
# Maybe, this is ERB.
|
|
|
|
$stderr.puts "\033[2KRDoc detects ERB file. Skips it for compatibility:"
|
|
|
|
$stderr.puts @file_name
|
|
|
|
return
|
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
if @scanner_point >= @scanner.size
|
|
|
|
now_line_no = @scanner[@scanner.size - 1][:line_no]
|
|
|
|
else
|
|
|
|
now_line_no = peek_tk[:line_no]
|
2010-12-19 22:22:49 -05:00
|
|
|
end
|
2018-03-26 01:56:26 -04:00
|
|
|
first_tk_index = @scanner.find_index { |tk| tk[:line_no] == now_line_no }
|
|
|
|
last_tk_index = @scanner.find_index { |tk| tk[:line_no] == now_line_no + 1 }
|
|
|
|
last_tk_index = last_tk_index ? last_tk_index - 1 : @scanner.size - 1
|
|
|
|
code = @scanner[first_tk_index..last_tk_index].map{ |t| t[:text] }.join
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
$stderr.puts <<-EOF
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
#{self.class} failure around line #{now_line_no} of
|
2010-04-01 03:45:16 -04:00
|
|
|
#{@file_name}
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
EOF
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2018-03-26 01:56:26 -04:00
|
|
|
unless code.empty? then
|
|
|
|
$stderr.puts code
|
2010-12-19 22:22:49 -05:00
|
|
|
$stderr.puts
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
2010-12-19 22:22:49 -05:00
|
|
|
|
|
|
|
raise e
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@top_level
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# while, until, and for have an optional do
|
|
|
|
|
|
|
|
def skip_optional_do_after_expression
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
|
|
|
|
2010-04-19 01:08:28 -04:00
|
|
|
b_nest = 0
|
2008-07-17 20:46:16 -04:00
|
|
|
nest = 0
|
|
|
|
|
|
|
|
loop do
|
2017-09-11 23:42:54 -04:00
|
|
|
break unless tk
|
|
|
|
case tk[:kind]
|
|
|
|
when :on_semicolon, :on_nl, :on_ignored_nl then
|
2010-04-19 01:08:28 -04:00
|
|
|
break if b_nest.zero?
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_lparen then
|
2008-07-17 20:46:16 -04:00
|
|
|
nest += 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_rparen then
|
2017-08-29 07:52:50 -04:00
|
|
|
nest -= 1
|
2017-09-11 23:42:54 -04:00
|
|
|
when :on_kw then
|
|
|
|
case tk[:text]
|
|
|
|
when 'begin'
|
|
|
|
b_nest += 1
|
|
|
|
when 'end'
|
|
|
|
b_nest -= 1
|
|
|
|
when 'do'
|
|
|
|
break if nest.zero?
|
|
|
|
end
|
|
|
|
when :on_comment, :on_embdoc then
|
|
|
|
if b_nest.zero? and "\n" == tk[:text][-1] then
|
|
|
|
break
|
|
|
|
end
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
tk = get_tk
|
|
|
|
end
|
2010-04-19 01:08:28 -04:00
|
|
|
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
|
2017-09-11 23:42:54 -04:00
|
|
|
get_tk if peek_tk && :on_kw == peek_tk[:kind] && 'do' == peek_tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# skip the var [in] part of a 'for' statement
|
|
|
|
|
|
|
|
def skip_for_variable
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2014-09-04 21:41:25 -04:00
|
|
|
get_tk
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_tkspace_without_nl
|
2008-07-17 20:46:16 -04:00
|
|
|
tk = get_tk
|
2017-09-11 23:42:54 -04:00
|
|
|
unget_tk(tk) unless :on_kw == tk[:kind] and 'in' == tk[:text]
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
|
|
|
# Skips the next method in +container+
|
|
|
|
|
2010-04-01 03:45:16 -04:00
|
|
|
def skip_method container
|
2008-07-17 20:46:16 -04:00
|
|
|
meth = RDoc::AnyMethod.new "", "anon"
|
2010-04-01 03:45:16 -04:00
|
|
|
parse_method_parameters meth
|
|
|
|
parse_statements container, false, meth
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Skip spaces until a comment is found
|
|
|
|
|
|
|
|
def skip_tkspace_comment(skip_nl = true)
|
|
|
|
loop do
|
2018-10-17 02:28:20 -04:00
|
|
|
skip_nl ? skip_tkspace : skip_tkspace_without_nl
|
2017-09-11 23:42:54 -04:00
|
|
|
next_tk = peek_tk
|
|
|
|
return if next_tk.nil? || (:on_comment != next_tk[:kind] and :on_embdoc != next_tk[:kind])
|
2008-07-17 20:46:16 -04:00
|
|
|
get_tk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-18 19:33:36 -04:00
|
|
|
##
|
|
|
|
# Updates visibility in +container+ from +vis_type+ and +vis+.
|
|
|
|
|
|
|
|
def update_visibility container, vis_type, vis, singleton # :nodoc:
|
|
|
|
new_methods = []
|
|
|
|
|
|
|
|
case vis_type
|
|
|
|
when 'module_function' then
|
|
|
|
args = parse_symbol_arg
|
|
|
|
container.set_visibility_for args, :private, false
|
|
|
|
|
|
|
|
container.methods_matching args do |m|
|
|
|
|
s_m = m.dup
|
|
|
|
record_location s_m
|
|
|
|
s_m.singleton = true
|
|
|
|
new_methods << s_m
|
|
|
|
end
|
|
|
|
when 'public_class_method', 'private_class_method' then
|
|
|
|
args = parse_symbol_arg
|
|
|
|
|
|
|
|
container.methods_matching args, true do |m|
|
|
|
|
if m.parent != container then
|
|
|
|
m = m.dup
|
|
|
|
record_location m
|
|
|
|
new_methods << m
|
|
|
|
end
|
|
|
|
|
|
|
|
m.visibility = vis
|
|
|
|
end
|
|
|
|
else
|
|
|
|
args = parse_symbol_arg
|
|
|
|
container.set_visibility_for args, vis, singleton
|
|
|
|
end
|
|
|
|
|
|
|
|
new_methods.each do |method|
|
|
|
|
case method
|
|
|
|
when RDoc::AnyMethod then
|
|
|
|
container.add_method method
|
|
|
|
when RDoc::Attr then
|
|
|
|
container.add_attribute method
|
|
|
|
end
|
|
|
|
method.visibility = vis
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 22:22:49 -05:00
|
|
|
##
|
2012-11-26 23:28:14 -05:00
|
|
|
# Prints +message+ to +$stderr+ unless we're being quiet
|
2010-12-19 22:22:49 -05:00
|
|
|
|
2012-11-26 23:28:14 -05:00
|
|
|
def warn message
|
|
|
|
@options.warn make_message message
|
2008-07-17 20:46:16 -04:00
|
|
|
end
|
|
|
|
|
2003-12-01 02:12:49 -05:00
|
|
|
end
|