Re-enable spec/library for full CI runs.
[rbx.git] / lib / rdoc / code_objects.rb
blobfbdb612b92ed6b2898a9a5cd1a259d67dce6eddd
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'
6 module RDoc
8   ##
9   # We contain the common stuff for contexts (which are containers) and other
10   # elements (methods, attributes and so on)
12   class CodeObject
14     attr_accessor :parent
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.
20     attr_accessor :viewer
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
34     def initialize
35       @document_self = true
36       @document_children = true
37       @force_documentation = false
38       @done_documenting = false
39     end
41     def document_self=(val)
42       @document_self = val
43       if !val
44         remove_methods_etc
45       end
46     end
48     # set and cleared by :startdoc: and :enddoc:, this is used to toggle
49     # the capturing of documentation
50     def start_doc
51       @document_self = true
52       @document_children = true
53     end
55     def stop_doc
56       @document_self = false
57       @document_children = false
58     end
60     # do we document ourselves and our children
62     attr_reader :document_children
64     def document_children=(val)
65       @document_children = val
66       if !val
67         remove_classes_and_modules
68       end
69     end
71     # Do we _force_ documentation, even is we wouldn't normally show the entity
72     attr_accessor :force_documentation
74     def parent_file_name
75       @parent ? @parent.file_base_name : '(unknown)'
76     end
78     def parent_name
79       @parent ? @parent.name : '(unknown)'
80     end
82     # Default callbacks to nothing, but this is overridden for classes
83     # and modules
84     def remove_classes_and_modules
85     end
87     def remove_methods_etc
88     end
90     # Access the code object's comment
91     attr_reader :comment
93     # Update the comment, but don't overwrite a real comment with an empty one
94     def comment=(comment)
95       @comment = comment unless comment.empty?
96     end
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)
105       @overridables ||= {}
107       attr_accessor name
109       aliases.unshift name
110       aliases.each do |directive_name|
111         @overridables[directive_name.to_s] = name
112       end
113     end
115   end
117   ##
118   # A Context is something that can hold modules, classes, methods,
119   # attributes, aliases, requires, and includes. Classes, modules, and files
120   # are all Contexts.
122   class Context < CodeObject
124     attr_reader :aliases
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
131     attr_reader :name
132     attr_reader :requires
133     attr_reader :sections
134     attr_reader :visibility
136     class Section
137       attr_reader :title, :comment, :sequence
139       @@sequence = "SEC00000"
141       def initialize(title, comment)
142         @title = title
143         @@sequence.succ!
144         @sequence = @@sequence.dup
145         @comment = nil
146         set_comment(comment)
147       end
149       def ==(other)
150         self.class === other and @sequence == other.sequence
151       end
153       def inspect
154         "#<%s:0x%x %s %p>" % [
155           self.class, object_id,
156           @sequence, title
157         ]
158       end
160       ##
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
165       #
166       #   # ---------------------
167       #   # :SECTION: The title
168       #   # The body
169       #   # ---------------------
171       def set_comment(comment)
172         return unless comment
174         if comment =~ /^#[ \t]*:section:.*\n/
175           start = $`
176           rest = $'
178           if start.empty?
179             @comment = rest
180           else
181             @comment = rest.sub(/#{start.chomp}\Z/, '')
182           end
183         else
184           @comment = comment
185         end
186         @comment = nil if @comment.empty?
187       end
189     end
191     def initialize
192       super
194       @in_files = []
196       @name    ||= "unknown"
197       @comment ||= ""
198       @parent  = nil
199       @visibility = :public
201       @current_section = Section.new(nil, nil)
202       @sections = [ @current_section ]
204       initialize_methods_etc
205       initialize_classes_and_modules
206     end
208     ##
209     # map the class hash to an array externally
211     def classes
212       @classes.values
213     end
215     ##
216     # map the module hash to an array externally
218     def modules
219       @modules.values
220     end
222     ##
223     # Change the default visibility for new methods
225     def ongoing_visibility=(vis)
226       @visibility = vis
227     end
229     ##
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)
234       count = 0
236       @method_list.each do |m|
237         if methods.include? m.name and m.singleton == singleton then
238           yield m
239           count += 1
240         end
241       end
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
249       end
250     end
252     ##
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|
258         m.visibility = vis
259       end
260     end
262     ##
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)
267     end
269     # Return true if at least part of this thing was defined in +file+
270     def defined_in?(file)
271       @in_files.include?(file)
272     end
274     def add_class(class_type, name, superclass)
275       add_class_or_module @classes, class_type, name, superclass
276     end
278     def add_module(class_type, name)
279       add_class_or_module(@modules, class_type, name, nil)
280     end
282     def add_method(a_method)
283       a_method.visibility = @visibility
284       add_to(@method_list, a_method)
285     end
287     def add_attribute(an_attribute)
288       add_to(@attributes, an_attribute)
289     end
291     def add_alias(an_alias)
292       meth = find_instance_method_named(an_alias.old_name)
294       if meth then
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)
301         add_method(new_meth)
302       else
303         add_to(@aliases, an_alias)
304       end
306       an_alias
307     end
309     def add_include(an_include)
310       add_to(@includes, an_include)
311     end
313     def add_constant(const)
314       add_to(@constants, const)
315     end
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
321       else
322         parent.add_require a_require
323       end
324     end
326     def add_class_or_module(collection, class_type, name, superclass=nil)
327       cls = collection[name]
329       if cls then
330         cls.superclass = superclass unless cls.module?
331         puts "Reusing class/module #{name}" if $DEBUG_RDOC
332       else
333         cls = class_type.new(name, superclass)
334 #        collection[name] = cls if @document_self  && !@done_documenting
335         collection[name] = cls if !@done_documenting
336         cls.parent = self
337         cls.section = @current_section
338       end
339       cls
340     end
342     def add_to(array, thing)
343       array << thing if @document_self and not @done_documenting
344       thing.parent = self
345       thing.section = @current_section
346     end
348     # If a class's documentation is turned off after we've started
349     # collecting methods etc., we need to remove the ones
350     # we have
352     def remove_methods_etc
353       initialize_methods_etc
354     end
356     def initialize_methods_etc
357       @method_list = []
358       @attributes  = []
359       @aliases     = []
360       @requires    = []
361       @includes    = []
362       @constants   = []
363     end
365     # and remove classes and modules when we see a :nodoc: all
366     def remove_classes_and_modules
367       initialize_classes_and_modules
368     end
370     def initialize_classes_and_modules
371       @classes     = {}
372       @modules     = {}
373     end
375     # Find a named module
376     def find_module_named(name)
377       return self if self.name == name
378       res = @modules[name] || @classes[name]
379       return res if res
380       find_enclosing_module_named(name)
381     end
383     # find a module at a higher scope
384     def find_enclosing_module_named(name)
385       parent && parent.find_module_named(name)
386     end
388     # Iterate over all the classes and modules in
389     # this object
391     def each_classmodule
392       @modules.each_value {|m| yield m}
393       @classes.each_value {|c| yield c}
394     end
396     def each_method
397       @method_list.each {|m| yield m}
398     end
400     def each_attribute 
401       @attributes.each {|a| yield a}
402     end
404     def each_constant
405       @constants.each {|c| yield c}
406     end
408     # Return the toplevel that owns us
410     def toplevel
411       return @toplevel if defined? @toplevel
412       @toplevel = self
413       @toplevel = @toplevel.parent until TopLevel === @toplevel
414       @toplevel
415     end
417     # allow us to sort modules by name
418     def <=>(other)
419       name <=> other.name
420     end
422     ##
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)
427       result = nil
429       case symbol
430       when /^::(.*)/ then
431         result = toplevel.find_symbol($1)
432       when /::/ then
433         modules = symbol.split(/::/)
435         unless modules.empty? then
436           module_name = modules.shift
437           result = find_module_named(module_name)
438           if result then
439             modules.each do |name|
440               result = result.find_module_named(name)
441               break unless result
442             end
443           end
444         end
446       else
447         # if a method is specified, then we're definitely looking for
448         # a module, otherwise it could be any symbol
449         if method
450           result = find_module_named(symbol)
451         else
452           result = find_local_symbol(symbol)
453           if result.nil?
454             if symbol =~ /^[A-Z]/
455               result = parent
456               while result && result.name != symbol
457                 result = result.parent
458               end
459             end
460           end
461         end
462       end
464       if result and method then
465         fail unless result.respond_to? :find_local_symbol
466         result = result.find_local_symbol(method)
467       end
469       result
470     end
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)
478     end
480     # Handle sections
482     def set_current_section(title, comment)
483       @current_section = Section.new(title, comment)
484       @sections << @current_section
485     end
487     private
489     # Find a named method, or return nil
490     def find_method_named(name)
491       @method_list.find {|meth| meth.name == name}
492     end
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}
497     end
499     # Find a named constant, or return nil
500     def find_constant_named(name)
501       @constants.find {|m| m.name == name}
502     end
504     # Find a named attribute, or return nil
505     def find_attribute_named(name)
506       @attributes.find {|m| m.name == name}
507     end
509     ##
510     # Find a named file, or return nil
512     def find_file_named(name)
513       toplevel.class.find_file_named(name)
514     end
516   end
518   ##
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
527     @@all_classes = {}
528     @@all_modules = {}
529     @@all_files   = {}
531     def self.reset
532       @@all_classes = {}
533       @@all_modules = {}
534       @@all_files   = {}
535     end
537     def initialize(file_name)
538       super()
539       @name = "TopLevel"
540       @file_relative_name    = file_name
541       @file_absolute_name    = file_name
542       @file_stat             = File.stat(file_name)
543       @diagram               = nil
544       @@all_files[file_name] = self
545     end
547     def file_base_name
548       File.basename @file_absolute_name
549     end
551     def full_name
552       nil
553     end
555     ##
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]
564       if cls then
565         cls.superclass = superclass unless cls.module?
566         puts "Reusing class/module #{cls.full_name}" if $DEBUG_RDOC
567       else
568         if class_type == NormalModule then
569           all = @@all_modules
570         else
571           all = @@all_classes
572         end
574         cls = all[name]
576         unless cls then
577           cls = class_type.new name, superclass
578           all[name] = cls unless @done_documenting
579         end
581         collection[name] = cls unless @done_documenting
583         cls.parent = self
584       end
586       cls
587     end
589     def self.all_classes_and_modules
590       @@all_classes.values + @@all_modules.values
591     end
593     def self.find_class_named(name)
594      @@all_classes.each_value do |c|
595         res = c.find_class_named(name) 
596         return res if res
597       end
598       nil
599     end
601     def self.find_file_named(name)
602       @@all_files[name]
603     end
605     def find_local_symbol(symbol)
606       find_class_or_module_named(symbol) || super
607     end
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}
612       nil
613     end
615     ##
616     # Find a named module
618     def find_module_named(name)
619       find_class_or_module_named(name) || find_enclosing_module_named(name)
620     end
622     def inspect
623       "#<%s:0x%x %p modules: %p classes: %p>" % [
624         self.class, object_id,
625         file_base_name,
626         @modules.map { |n,m| m },
627         @classes.map { |n,c| c }
628       ]
629     end
631   end
633   ##
634   # ClassModule is the base class for objects representing either a class or a
635   # module.
637   class ClassModule < Context
639     attr_accessor :diagram
641     def initialize(name, superclass = nil)
642       @name       = name
643       @diagram    = nil
644       @superclass = superclass
645       @comment    = ""
646       super()
647     end
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) }
652       nil
653     end
655     ##
656     # Return the fully qualified name of this class or module
658     def full_name
659       if @parent && @parent.full_name
660         @parent.full_name + "::" + @name
661       else
662         @name
663       end
664     end
666     def http_url(prefix)
667       path = full_name.split("::")
668       File.join(prefix, *path) + ".html"
669     end
671     ##
672     # Does this object represent a module?
674     def module?
675       false
676     end
678     ##
679     # Get the superclass of this class.  Attempts to retrieve the superclass'
680     # real name by following module nesting.
682     def superclass
683       raise NoMethodError, "#{full_name} is a module" if module?
685       scope = self
687       begin
688         superclass = scope.classes.find { |c| c.name == @superclass }
690         return superclass.full_name if superclass
691         scope = scope.parent
692       end until scope.nil? or TopLevel === scope
694       @superclass
695     end
697     ##
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 
705       end
706     end
708     def to_s
709       "#{self.class}: #{@name} #{@comment} #{super}"
710     end
712   end
714   ##
715   # Anonymous classes
717   class AnonClass < ClassModule
718   end
720   ##
721   # Normal classes
723   class NormalClass < ClassModule
725     def inspect
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
730       ]
731     end
733   end
735   ##
736   # Singleton classes
738   class SingleClass < ClassModule
739   end
741   ##
742   # Module
744   class NormalModule < ClassModule
746     def comment=(comment)
747       return if comment.empty?
748       comment = @comment << "# ---\n" << comment unless @comment.empty?
750       super
751     end
753     def inspect
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
757       ]
758     end
760     def module?
761       true
762     end
764   end
766   ##
767   # AnyMethod is the base class for objects representing methods
769   class AnyMethod < CodeObject
771     attr_accessor :name
772     attr_accessor :visibility
773     attr_accessor :block_params
774     attr_accessor :dont_rename_initialize
775     attr_accessor :singleton
776     attr_reader :text
778     # list of other names for this method
779     attr_reader   :aliases
781     # method we're aliasing
782     attr_accessor :is_alias_for
784     attr_overridable :params, :param, :parameters, :parameter
786     attr_accessor :call_seq
788     include TokenStream
790     def initialize(text, name)
791       super()
792       @text = text
793       @name = name
794       @token_stream  = nil
795       @visibility    = :public
796       @dont_rename_initialize = false
797       @block_params  = nil
798       @aliases       = []
799       @is_alias_for  = nil
800       @comment = ""
801       @call_seq = nil
802     end
804     def <=>(other)
805       @name <=> other.name
806     end
808     def add_alias(method)
809       @aliases << method
810     end
812     def inspect
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,
816         parent_name,
817         singleton ? '::' : '#',
818         name,
819         visibility,
820         alias_for,
821       ]
822     end
824     def param_seq
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
831         # &block
832         params.sub!(/,?\s*&\w+/)
834         block.gsub!(/\s*\#.*/, '')
835         block = block.tr("\n", " ").squeeze(" ")
836         if block[0] == ?(
837           block.sub!(/^\(/, '').sub!(/\)/, '')
838         end
839         params << " { |#{block}| ... }"
840       end
842       params
843     end
845     def to_s
846       res = self.class.name + ": " + @name + " (" + @text + ")\n"
847       res << @comment.to_s
848       res
849     end
851   end
853   ##
854   # GhostMethod represents a method referenced only by a comment
856   class GhostMethod < AnyMethod
857   end
859   ##
860   # MetaMethod represents a meta-programmed method
862   class MetaMethod < AnyMethod
863   end
865   ##
866   # Represent an alias, which is an old_name/ new_name pair associated with a
867   # particular context
869   class Alias < CodeObject
871     attr_accessor :text, :old_name, :new_name, :comment
873     def initialize(text, old_name, new_name, comment)
874       super()
875       @text = text
876       @old_name = old_name
877       @new_name = new_name
878       self.comment = comment
879     end
881     def inspect
882       "#<%s:0x%x %s.alias_method %s, %s>" % [
883         self.class, object_id,
884         parent.name, @old_name, @new_name,
885       ]
886     end
888     def to_s
889       "alias: #{self.old_name} ->  #{self.new_name}\n#{self.comment}"
890     end
892   end
894   ##
895   # Represent a constant
897   class Constant < CodeObject
898     attr_accessor :name, :value
900     def initialize(name, value, comment)
901       super()
902       @name = name
903       @value = value
904       self.comment = comment
905     end
906   end
908   ##
909   # Represent attributes
911   class Attr < CodeObject
912     attr_accessor :text, :name, :rw, :visibility
914     def initialize(text, name, rw, comment)
915       super()
916       @text = text
917       @name = name
918       @rw = rw
919       @visibility = :public
920       self.comment = comment
921     end
923     def <=>(other)
924       self.name <=> other.name
925     end
927     def inspect
928       attr = case rw
929              when 'RW' then :attr_accessor
930              when 'R'  then :attr_reader
931              when 'W'  then :attr_writer
932              else
933                " (#{rw})"
934              end
936       "#<%s:0x%x %s.%s :%s>" % [
937         self.class, object_id,
938         parent_name, attr, @name,
939       ]
940     end
942     def to_s
943       "attr: #{self.name} #{self.rw}\n#{self.comment}"
944     end
946   end
948   ##
949   # A required file
951   class Require < CodeObject
952     attr_accessor :name
954     def initialize(name, comment)
955       super()
956       @name = name.gsub(/'|"/, "") #'
957       self.comment = comment
958     end
960     def inspect
961       "#<%s:0x%x require '%s' in %s>" % [
962         self.class,
963         object_id,
964         @name,
965         parent_file_name,
966       ]
967     end
969   end
971   ##
972   # An included module
974   class Include < CodeObject
976     attr_accessor :name
978     def initialize(name, comment)
979       super()
980       @name = name
981       self.comment = comment
983     end
985     def inspect
986       "#<%s:0x%x %s.include %s>" % [
987         self.class,
988         object_id,
989         parent_name, @name,
990       ]
991     end
993   end