1 # We represent the various high-level code constructs that appear
2 # in Ruby programs: classes, modules, methods, and so on.
4 require 'rdoc/tokenstream'
9 # We contain the common stuff for contexts (which are containers) and other
10 # elements (methods, attributes and so on)
16 # We are the model of the code, but we know that at some point
17 # we will be worked on by viewers. By implementing the Viewable
18 # protocol, viewers can associated themselves with these objects.
22 # are we done documenting (ie, did we come across a :enddoc:)?
24 attr_accessor :done_documenting
26 # Which section are we in
28 attr_accessor :section
30 # do we document ourselves?
32 attr_reader :document_self
36 @document_children = true
37 @force_documentation = false
38 @done_documenting = false
41 def document_self=(val)
48 # set and cleared by :startdoc: and :enddoc:, this is used to toggle
49 # the capturing of documentation
52 @document_children = true
56 @document_self = false
57 @document_children = false
60 # do we document ourselves and our children
62 attr_reader :document_children
64 def document_children=(val)
65 @document_children = val
67 remove_classes_and_modules
71 # Do we _force_ documentation, even is we wouldn't normally show the entity
72 attr_accessor :force_documentation
75 @parent ? @parent.file_base_name : '(unknown)'
79 @parent ? @parent.name : '(unknown)'
82 # Default callbacks to nothing, but this is overridden for classes
84 def remove_classes_and_modules
87 def remove_methods_etc
90 # Access the code object's comment
93 # Update the comment, but don't overwrite a real comment with an empty one
95 @comment = comment unless comment.empty?
98 # There's a wee trick we pull. Comment blocks can have directives that
99 # override the stuff we extract during the parse. So, we have a special
100 # class method, attr_overridable, that lets code objects list
101 # those directives. Wehn a comment is assigned, we then extract
102 # out any matching directives and update our object
104 def self.attr_overridable(name, *aliases)
110 aliases.each do |directive_name|
111 @overridables[directive_name.to_s] = name
118 # A Context is something that can hold modules, classes, methods,
119 # attributes, aliases, requires, and includes. Classes, modules, and files
122 class Context < CodeObject
125 attr_reader :attributes
126 attr_reader :constants
127 attr_reader :current_section
128 attr_reader :in_files
129 attr_reader :includes
130 attr_reader :method_list
132 attr_reader :requires
133 attr_reader :sections
134 attr_reader :visibility
137 attr_reader :title, :comment, :sequence
139 @@sequence = "SEC00000"
141 def initialize(title, comment)
144 @sequence = @@sequence.dup
150 self.class === other and @sequence == other.sequence
154 "#<%s:0x%x %s %p>" % [
155 self.class, object_id,
161 # Set the comment for this section from the original comment block If
162 # the first line contains :section:, strip it and use the rest.
163 # Otherwise remove lines up to the line containing :section:, and look
164 # for those lines again at the end and remove them. This lets us write
166 # # ---------------------
167 # # :SECTION: The title
169 # # ---------------------
171 def set_comment(comment)
172 return unless comment
174 if comment =~ /^#[ \t]*:section:.*\n/
181 @comment = rest.sub(/#{start.chomp}\Z/, '')
186 @comment = nil if @comment.empty?
199 @visibility = :public
201 @current_section = Section.new(nil, nil)
202 @sections = [ @current_section ]
204 initialize_methods_etc
205 initialize_classes_and_modules
209 # map the class hash to an array externally
216 # map the module hash to an array externally
223 # Change the default visibility for new methods
225 def ongoing_visibility=(vis)
230 # Yields Method and Attr entries matching the list of names in +methods+.
231 # Attributes are only returned when +singleton+ is false.
233 def methods_matching(methods, singleton = false)
236 @method_list.each do |m|
237 if methods.include? m.name and m.singleton == singleton then
243 return if count == methods.size || singleton
245 # perhaps we need to look at attributes
247 @attributes.each do |a|
248 yield a if methods.include? a.name
253 # Given an array +methods+ of method names, set the visibility of the
254 # corresponding AnyMethod object
256 def set_visibility_for(methods, vis, singleton = false)
257 methods_matching methods, singleton do |m|
263 # Record the file that we happen to find it in
265 def record_location(toplevel)
266 @in_files << toplevel unless @in_files.include?(toplevel)
269 # Return true if at least part of this thing was defined in +file+
270 def defined_in?(file)
271 @in_files.include?(file)
274 def add_class(class_type, name, superclass)
275 add_class_or_module @classes, class_type, name, superclass
278 def add_module(class_type, name)
279 add_class_or_module(@modules, class_type, name, nil)
282 def add_method(a_method)
283 a_method.visibility = @visibility
284 add_to(@method_list, a_method)
287 def add_attribute(an_attribute)
288 add_to(@attributes, an_attribute)
291 def add_alias(an_alias)
292 meth = find_instance_method_named(an_alias.old_name)
295 new_meth = AnyMethod.new(an_alias.text, an_alias.new_name)
296 new_meth.is_alias_for = meth
297 new_meth.singleton = meth.singleton
298 new_meth.params = meth.params
299 new_meth.comment = "Alias for \##{meth.name}"
300 meth.add_alias(new_meth)
303 add_to(@aliases, an_alias)
309 def add_include(an_include)
310 add_to(@includes, an_include)
313 def add_constant(const)
314 add_to(@constants, const)
317 # Requires always get added to the top-level (file) context
318 def add_require(a_require)
319 if TopLevel === self then
320 add_to @requires, a_require
322 parent.add_require a_require
326 def add_class_or_module(collection, class_type, name, superclass=nil)
327 cls = collection[name]
330 cls.superclass = superclass unless cls.module?
331 puts "Reusing class/module #{name}" if $DEBUG_RDOC
333 cls = class_type.new(name, superclass)
334 # collection[name] = cls if @document_self && !@done_documenting
335 collection[name] = cls if !@done_documenting
337 cls.section = @current_section
342 def add_to(array, thing)
343 array << thing if @document_self and not @done_documenting
345 thing.section = @current_section
348 # If a class's documentation is turned off after we've started
349 # collecting methods etc., we need to remove the ones
352 def remove_methods_etc
353 initialize_methods_etc
356 def initialize_methods_etc
365 # and remove classes and modules when we see a :nodoc: all
366 def remove_classes_and_modules
367 initialize_classes_and_modules
370 def initialize_classes_and_modules
375 # Find a named module
376 def find_module_named(name)
377 return self if self.name == name
378 res = @modules[name] || @classes[name]
380 find_enclosing_module_named(name)
383 # find a module at a higher scope
384 def find_enclosing_module_named(name)
385 parent && parent.find_module_named(name)
388 # Iterate over all the classes and modules in
392 @modules.each_value {|m| yield m}
393 @classes.each_value {|c| yield c}
397 @method_list.each {|m| yield m}
401 @attributes.each {|a| yield a}
405 @constants.each {|c| yield c}
408 # Return the toplevel that owns us
411 return @toplevel if defined? @toplevel
413 @toplevel = @toplevel.parent until TopLevel === @toplevel
417 # allow us to sort modules by name
423 # Look up +symbol+. If +method+ is non-nil, then we assume the symbol
424 # references a module that contains that method.
426 def find_symbol(symbol, method = nil)
431 result = toplevel.find_symbol($1)
433 modules = symbol.split(/::/)
435 unless modules.empty? then
436 module_name = modules.shift
437 result = find_module_named(module_name)
439 modules.each do |name|
440 result = result.find_module_named(name)
447 # if a method is specified, then we're definitely looking for
448 # a module, otherwise it could be any symbol
450 result = find_module_named(symbol)
452 result = find_local_symbol(symbol)
454 if symbol =~ /^[A-Z]/
456 while result && result.name != symbol
457 result = result.parent
464 if result and method then
465 fail unless result.respond_to? :find_local_symbol
466 result = result.find_local_symbol(method)
472 def find_local_symbol(symbol)
473 res = find_method_named(symbol) ||
474 find_constant_named(symbol) ||
475 find_attribute_named(symbol) ||
476 find_module_named(symbol) ||
477 find_file_named(symbol)
482 def set_current_section(title, comment)
483 @current_section = Section.new(title, comment)
484 @sections << @current_section
489 # Find a named method, or return nil
490 def find_method_named(name)
491 @method_list.find {|meth| meth.name == name}
494 # Find a named instance method, or return nil
495 def find_instance_method_named(name)
496 @method_list.find {|meth| meth.name == name && !meth.singleton}
499 # Find a named constant, or return nil
500 def find_constant_named(name)
501 @constants.find {|m| m.name == name}
504 # Find a named attribute, or return nil
505 def find_attribute_named(name)
506 @attributes.find {|m| m.name == name}
510 # Find a named file, or return nil
512 def find_file_named(name)
513 toplevel.class.find_file_named(name)
519 # A TopLevel context is a source file
521 class TopLevel < Context
522 attr_accessor :file_stat
523 attr_accessor :file_relative_name
524 attr_accessor :file_absolute_name
525 attr_accessor :diagram
537 def initialize(file_name)
540 @file_relative_name = file_name
541 @file_absolute_name = file_name
542 @file_stat = File.stat(file_name)
544 @@all_files[file_name] = self
548 File.basename @file_absolute_name
556 # Adding a class or module to a TopLevel is special, as we only want one
557 # copy of a particular top-level class. For example, if both file A and
558 # file B implement class C, we only want one ClassModule object for C.
559 # This code arranges to share classes and modules between files.
561 def add_class_or_module(collection, class_type, name, superclass)
562 cls = collection[name]
565 cls.superclass = superclass unless cls.module?
566 puts "Reusing class/module #{cls.full_name}" if $DEBUG_RDOC
568 if class_type == NormalModule then
577 cls = class_type.new name, superclass
578 all[name] = cls unless @done_documenting
581 collection[name] = cls unless @done_documenting
589 def self.all_classes_and_modules
590 @@all_classes.values + @@all_modules.values
593 def self.find_class_named(name)
594 @@all_classes.each_value do |c|
595 res = c.find_class_named(name)
601 def self.find_file_named(name)
605 def find_local_symbol(symbol)
606 find_class_or_module_named(symbol) || super
609 def find_class_or_module_named(symbol)
610 @@all_classes.each_value {|c| return c if c.name == symbol}
611 @@all_modules.each_value {|m| return m if m.name == symbol}
616 # Find a named module
618 def find_module_named(name)
619 find_class_or_module_named(name) || find_enclosing_module_named(name)
623 "#<%s:0x%x %p modules: %p classes: %p>" % [
624 self.class, object_id,
626 @modules.map { |n,m| m },
627 @classes.map { |n,c| c }
634 # ClassModule is the base class for objects representing either a class or a
637 class ClassModule < Context
639 attr_accessor :diagram
641 def initialize(name, superclass = nil)
644 @superclass = superclass
649 def find_class_named(name)
650 return self if full_name == name
651 @classes.each_value {|c| return c if c.find_class_named(name) }
656 # Return the fully qualified name of this class or module
659 if @parent && @parent.full_name
660 @parent.full_name + "::" + @name
667 path = full_name.split("::")
668 File.join(prefix, *path) + ".html"
672 # Does this object represent a module?
679 # Get the superclass of this class. Attempts to retrieve the superclass'
680 # real name by following module nesting.
683 raise NoMethodError, "#{full_name} is a module" if module?
688 superclass = scope.classes.find { |c| c.name == @superclass }
690 return superclass.full_name if superclass
692 end until scope.nil? or TopLevel === scope
698 # Set the superclass of this class
700 def superclass=(superclass)
701 raise NoMethodError, "#{full_name} is a module" if module?
703 if @superclass.nil? or @superclass == 'Object' then
704 @superclass = superclass
709 "#{self.class}: #{@name} #{@comment} #{super}"
717 class AnonClass < ClassModule
723 class NormalClass < ClassModule
726 superclass = @superclass ? " < #{@superclass}" : nil
727 "<%s:0x%x class %s%s includes: %p attributes: %p methods: %p aliases: %p>" % [
728 self.class, object_id,
729 @name, superclass, @includes, @attributes, @method_list, @aliases
738 class SingleClass < ClassModule
744 class NormalModule < ClassModule
746 def comment=(comment)
747 return if comment.empty?
748 comment = @comment << "# ---\n" << comment unless @comment.empty?
754 "#<%s:0x%x module %s includes: %p attributes: %p methods: %p aliases: %p>" % [
755 self.class, object_id,
756 @name, @includes, @attributes, @method_list, @aliases
767 # AnyMethod is the base class for objects representing methods
769 class AnyMethod < CodeObject
772 attr_accessor :visibility
773 attr_accessor :block_params
774 attr_accessor :dont_rename_initialize
775 attr_accessor :singleton
778 # list of other names for this method
781 # method we're aliasing
782 attr_accessor :is_alias_for
784 attr_overridable :params, :param, :parameters, :parameter
786 attr_accessor :call_seq
790 def initialize(text, name)
795 @visibility = :public
796 @dont_rename_initialize = false
808 def add_alias(method)
813 alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
814 "#<%s:0x%x %s%s%s (%s)%s>" % [
815 self.class, object_id,
817 singleton ? '::' : '#',
825 params = params.gsub(/\s*\#.*/, '')
826 params = params.tr("\n", " ").squeeze(" ")
827 params = "(#{params})" unless p[0] == ?(
829 if block = block_params then # yes, =
830 # If this method has explicit block parameters, remove any explicit
832 params.sub!(/,?\s*&\w+/)
834 block.gsub!(/\s*\#.*/, '')
835 block = block.tr("\n", " ").squeeze(" ")
837 block.sub!(/^\(/, '').sub!(/\)/, '')
839 params << " { |#{block}| ... }"
846 res = self.class.name + ": " + @name + " (" + @text + ")\n"
854 # GhostMethod represents a method referenced only by a comment
856 class GhostMethod < AnyMethod
860 # MetaMethod represents a meta-programmed method
862 class MetaMethod < AnyMethod
866 # Represent an alias, which is an old_name/ new_name pair associated with a
869 class Alias < CodeObject
871 attr_accessor :text, :old_name, :new_name, :comment
873 def initialize(text, old_name, new_name, comment)
878 self.comment = comment
882 "#<%s:0x%x %s.alias_method %s, %s>" % [
883 self.class, object_id,
884 parent.name, @old_name, @new_name,
889 "alias: #{self.old_name} -> #{self.new_name}\n#{self.comment}"
895 # Represent a constant
897 class Constant < CodeObject
898 attr_accessor :name, :value
900 def initialize(name, value, comment)
904 self.comment = comment
909 # Represent attributes
911 class Attr < CodeObject
912 attr_accessor :text, :name, :rw, :visibility
914 def initialize(text, name, rw, comment)
919 @visibility = :public
920 self.comment = comment
924 self.name <=> other.name
929 when 'RW' then :attr_accessor
930 when 'R' then :attr_reader
931 when 'W' then :attr_writer
936 "#<%s:0x%x %s.%s :%s>" % [
937 self.class, object_id,
938 parent_name, attr, @name,
943 "attr: #{self.name} #{self.rw}\n#{self.comment}"
951 class Require < CodeObject
954 def initialize(name, comment)
956 @name = name.gsub(/'|"/, "") #'
957 self.comment = comment
961 "#<%s:0x%x require '%s' in %s>" % [
974 class Include < CodeObject
978 def initialize(name, comment)
981 self.comment = comment
986 "#<%s:0x%x %s.include %s>" % [