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
74 # Default callbacks to nothing, but this is overridden for classes
76 def remove_classes_and_modules
79 def remove_methods_etc
82 # Access the code object's comment
85 # Update the comment, but don't overwrite a real comment with an empty one
87 @comment = comment unless comment.empty?
90 # There's a wee trick we pull. Comment blocks can have directives that
91 # override the stuff we extract during the parse. So, we have a special
92 # class method, attr_overridable, that lets code objects list
93 # those directives. Wehn a comment is assigned, we then extract
94 # out any matching directives and update our object
96 def self.attr_overridable(name, *aliases)
102 aliases.each do |directive_name|
103 @overridables[directive_name.to_s] = name
110 # A Context is something that can hold modules, classes, methods,
111 # attributes, aliases, requires, and includes. Classes, modules, and files
114 class Context < CodeObject
117 attr_reader :attributes
118 attr_reader :constants
119 attr_reader :current_section
120 attr_reader :in_files
121 attr_reader :includes
122 attr_reader :method_list
124 attr_reader :requires
125 attr_reader :sections
126 attr_reader :visibility
129 attr_reader :title, :comment, :sequence
131 @@sequence = "SEC00000"
133 def initialize(title, comment)
136 @sequence = @@sequence.dup
142 self.class === other and @sequence == other.sequence
146 "#<%s:0x%x %s %p>" % [
147 self.class, object_id,
153 # Set the comment for this section from the original comment block If
154 # the first line contains :section:, strip it and use the rest.
155 # Otherwise remove lines up to the line containing :section:, and look
156 # for those lines again at the end and remove them. This lets us write
158 # # ---------------------
159 # # :SECTION: The title
161 # # ---------------------
163 def set_comment(comment)
164 return unless comment
166 if comment =~ /^#[ \t]*:section:.*\n/
173 @comment = rest.sub(/#{start.chomp}\Z/, '')
178 @comment = nil if @comment.empty?
191 @visibility = :public
193 @current_section = Section.new(nil, nil)
194 @sections = [ @current_section ]
196 initialize_methods_etc
197 initialize_classes_and_modules
201 # map the class hash to an array externally
208 # map the module hash to an array externally
215 # Change the default visibility for new methods
217 def ongoing_visibility=(vis)
222 # Yields Method and Attr entries matching the list of names in +methods+.
223 # Attributes are only returned when +singleton+ is false.
225 def methods_matching(methods, singleton = false)
228 @method_list.each do |m|
229 if methods.include? m.name and m.singleton == singleton then
235 return if count == methods.size || singleton
237 # perhaps we need to look at attributes
239 @attributes.each do |a|
240 yield a if methods.include? a.name
245 # Given an array +methods+ of method names, set the visibility of the
246 # corresponding AnyMethod object
248 def set_visibility_for(methods, vis, singleton = false)
249 methods_matching methods, singleton do |m|
255 # Record the file that we happen to find it in
257 def record_location(toplevel)
258 @in_files << toplevel unless @in_files.include?(toplevel)
261 # Return true if at least part of this thing was defined in +file+
262 def defined_in?(file)
263 @in_files.include?(file)
266 def add_class(class_type, name, superclass)
267 add_class_or_module(@classes, class_type, name, superclass)
270 def add_module(class_type, name)
271 add_class_or_module(@modules, class_type, name, nil)
274 def add_method(a_method)
275 puts "Adding #@visibility method #{a_method.name} to #@name" if $DEBUG_RDOC
276 a_method.visibility = @visibility
277 add_to(@method_list, a_method)
280 def add_attribute(an_attribute)
281 add_to(@attributes, an_attribute)
284 def add_alias(an_alias)
285 meth = find_instance_method_named(an_alias.old_name)
287 new_meth = AnyMethod.new(an_alias.text, an_alias.new_name)
288 new_meth.is_alias_for = meth
289 new_meth.singleton = meth.singleton
290 new_meth.params = meth.params
291 new_meth.comment = "Alias for \##{meth.name}"
292 meth.add_alias(new_meth)
295 add_to(@aliases, an_alias)
299 def add_include(an_include)
300 add_to(@includes, an_include)
303 def add_constant(const)
304 add_to(@constants, const)
307 # Requires always get added to the top-level (file) context
308 def add_require(a_require)
309 if TopLevel === self then
310 add_to @requires, a_require
312 parent.add_require a_require
316 def add_class_or_module(collection, class_type, name, superclass=nil)
317 cls = collection[name]
319 puts "Reusing class/module #{name}" if $DEBUG_RDOC
321 cls = class_type.new(name, superclass)
322 puts "Adding class/module #{name} to #@name" if $DEBUG_RDOC
323 # collection[name] = cls if @document_self && !@done_documenting
324 collection[name] = cls if !@done_documenting
326 cls.section = @current_section
331 def add_to(array, thing)
332 array << thing if @document_self and not @done_documenting
334 thing.section = @current_section
337 # If a class's documentation is turned off after we've started
338 # collecting methods etc., we need to remove the ones
341 def remove_methods_etc
342 initialize_methods_etc
345 def initialize_methods_etc
354 # and remove classes and modules when we see a :nodoc: all
355 def remove_classes_and_modules
356 initialize_classes_and_modules
359 def initialize_classes_and_modules
364 # Find a named module
365 def find_module_named(name)
366 return self if self.name == name
367 res = @modules[name] || @classes[name]
369 find_enclosing_module_named(name)
372 # find a module at a higher scope
373 def find_enclosing_module_named(name)
374 parent && parent.find_module_named(name)
377 # Iterate over all the classes and modules in
381 @modules.each_value {|m| yield m}
382 @classes.each_value {|c| yield c}
386 @method_list.each {|m| yield m}
390 @attributes.each {|a| yield a}
394 @constants.each {|c| yield c}
397 # Return the toplevel that owns us
400 return @toplevel if defined? @toplevel
402 @toplevel = @toplevel.parent until TopLevel === @toplevel
406 # allow us to sort modules by name
412 # Look up +symbol+. If +method+ is non-nil, then we assume the symbol
413 # references a module that contains that method.
415 def find_symbol(symbol, method = nil)
420 result = toplevel.find_symbol($1)
422 modules = symbol.split(/::/)
424 unless modules.empty? then
425 module_name = modules.shift
426 result = find_module_named(module_name)
428 modules.each do |name|
429 result = result.find_module_named(name)
436 # if a method is specified, then we're definitely looking for
437 # a module, otherwise it could be any symbol
439 result = find_module_named(symbol)
441 result = find_local_symbol(symbol)
443 if symbol =~ /^[A-Z]/
445 while result && result.name != symbol
446 result = result.parent
453 if result and method then
454 fail unless result.respond_to? :find_local_symbol
455 result = result.find_local_symbol(method)
461 def find_local_symbol(symbol)
462 res = find_method_named(symbol) ||
463 find_constant_named(symbol) ||
464 find_attribute_named(symbol) ||
465 find_module_named(symbol) ||
466 find_file_named(symbol)
471 def set_current_section(title, comment)
472 @current_section = Section.new(title, comment)
473 @sections << @current_section
478 # Find a named method, or return nil
479 def find_method_named(name)
480 @method_list.find {|meth| meth.name == name}
483 # Find a named instance method, or return nil
484 def find_instance_method_named(name)
485 @method_list.find {|meth| meth.name == name && !meth.singleton}
488 # Find a named constant, or return nil
489 def find_constant_named(name)
490 @constants.find {|m| m.name == name}
493 # Find a named attribute, or return nil
494 def find_attribute_named(name)
495 @attributes.find {|m| m.name == name}
499 # Find a named file, or return nil
501 def find_file_named(name)
502 toplevel.class.find_file_named(name)
508 # A TopLevel context is a source file
510 class TopLevel < Context
511 attr_accessor :file_stat
512 attr_accessor :file_relative_name
513 attr_accessor :file_absolute_name
514 attr_accessor :diagram
526 def initialize(file_name)
529 @file_relative_name = file_name
530 @file_absolute_name = file_name
531 @file_stat = File.stat(file_name)
533 @@all_files[file_name] = self
537 File.basename @file_absolute_name
545 # Adding a class or module to a TopLevel is special, as we only want one
546 # copy of a particular top-level class. For example, if both file A and
547 # file B implement class C, we only want one ClassModule object for C.
548 # This code arranges to share classes and modules between files.
550 def add_class_or_module(collection, class_type, name, superclass)
551 cls = collection[name]
554 puts "Reusing class/module #{name}" #if $DEBUG_RDOC
556 if class_type == NormalModule
565 cls = class_type.new(name, superclass)
566 all[name] = cls unless @done_documenting
569 puts "Adding class/module #{name} to #{@name}" if $DEBUG_RDOC
571 collection[name] = cls unless @done_documenting
579 def self.all_classes_and_modules
580 @@all_classes.values + @@all_modules.values
583 def self.find_class_named(name)
584 @@all_classes.each_value do |c|
585 res = c.find_class_named(name)
591 def self.find_file_named(name)
595 def find_local_symbol(symbol)
596 find_class_or_module_named(symbol) || super
599 def find_class_or_module_named(symbol)
600 @@all_classes.each_value {|c| return c if c.name == symbol}
601 @@all_modules.each_value {|m| return m if m.name == symbol}
606 # Find a named module
608 def find_module_named(name)
609 find_class_or_module_named(name) || find_enclosing_module_named(name)
615 # ClassModule is the base class for objects representing either a class or a
618 class ClassModule < Context
620 attr_reader :superclass
621 attr_accessor :diagram
623 def initialize(name, superclass = nil)
626 @superclass = superclass
631 # Return the fully qualified name of this class or module
633 if @parent && @parent.full_name
634 @parent.full_name + "::" + @name
641 path = full_name.split("::")
642 File.join(prefix, *path) + ".html"
645 # Return +true+ if this object represents a module
650 # to_s is simply for debugging
652 res = self.class.name + ": " + @name
658 def find_class_named(name)
659 return self if full_name == name
660 @classes.each_value {|c| return c if c.find_class_named(name) }
668 class AnonClass < ClassModule
674 class NormalClass < ClassModule
677 superclass = @superclass ? " < #{@superclass}" : nil
678 "<%s:0x%x class %s%s includes: %p attributes: %p methods: %p aliases: %p>" % [
679 self.class, object_id,
680 @name, superclass, @includes, @attributes, @method_list, @aliases
689 class SingleClass < ClassModule
695 class NormalModule < ClassModule
697 def comment=(comment)
698 return if comment.empty?
699 comment = @comment << "# ---\n" << comment unless @comment.empty?
705 "#<%s:0x%x module %s includes: %p attributes: %p methods: %p aliases: %p>" % [
706 self.class, object_id,
707 @name, @includes, @attributes, @method_list, @aliases
718 # AnyMethod is the base class for objects representing methods
720 class AnyMethod < CodeObject
723 attr_accessor :visibility
724 attr_accessor :block_params
725 attr_accessor :dont_rename_initialize
726 attr_accessor :singleton
729 # list of other names for this method
732 # method we're aliasing
733 attr_accessor :is_alias_for
735 attr_overridable :params, :param, :parameters, :parameter
737 attr_accessor :call_seq
741 def initialize(text, name)
746 @visibility = :public
747 @dont_rename_initialize = false
759 def add_alias(method)
764 alias_for = @is_alias_for ? " (alias for #{@is_alias_for.name})" : nil
765 "#<%s:0x%x %s%s%s (%s)%s>" % [
766 self.class, object_id,
768 singleton ? '::' : '#',
776 p = params.gsub(/\s*\#.*/, '')
777 p = p.tr("\n", " ").squeeze(" ")
778 p = "(" + p + ")" unless p[0] == ?(
780 if (block = block_params)
781 # If this method has explicit block parameters, remove any
787 block.gsub!(/\s*\#.*/, '')
788 block = block.tr("\n", " ").squeeze(" ")
790 block.sub!(/^\(/, '').sub!(/\)/, '')
792 p << " {|#{block}| ...}"
798 res = self.class.name + ": " + @name + " (" + @text + ")\n"
806 # GhostMethod represents a method referenced only by a comment
808 class GhostMethod < AnyMethod
812 # MetaMethod represents a meta-programmed method
814 class MetaMethod < AnyMethod
818 # Represent an alias, which is an old_name/ new_name pair associated with a
821 class Alias < CodeObject
823 attr_accessor :text, :old_name, :new_name, :comment
825 def initialize(text, old_name, new_name, comment)
830 self.comment = comment
834 "#<%s:0x%x %s.alias_method %s, %s>" % [
835 self.class, object_id,
836 parent.name, @old_name, @new_name,
841 "alias: #{self.old_name} -> #{self.new_name}\n#{self.comment}"
847 # Represent a constant
849 class Constant < CodeObject
850 attr_accessor :name, :value
852 def initialize(name, value, comment)
856 self.comment = comment
861 # Represent attributes
863 class Attr < CodeObject
864 attr_accessor :text, :name, :rw, :visibility
866 def initialize(text, name, rw, comment)
871 @visibility = :public
872 self.comment = comment
876 self.name <=> other.name
881 when 'RW' then :attr_accessor
882 when 'R' then :attr_reader
883 when 'W' then :attr_writer
888 "#<%s:0x%x %s.%s :%s>" % [
889 self.class, object_id,
890 @parent.name, attr, @name,
895 "attr: #{self.name} #{self.rw}\n#{self.comment}"
903 class Require < CodeObject
906 def initialize(name, comment)
908 @name = name.gsub(/'|"/, "") #'
909 self.comment = comment
913 "#<%s:0x%x require '%s' in %s>" % [
917 @parent.file_base_name,
926 class Include < CodeObject
930 def initialize(name, comment)
933 self.comment = comment
938 "#<%s:0x%x %s.include %s>" % [