@@ -606,18 +606,46 @@ def find_attr_comment var_name, attr_name, read = nil, write = nil
606606 RDoc ::Comment . new comment , @top_level
607607 end
608608
609+ ##
610+ # Generate a Ruby-method table
611+
612+ def gen_body_table file_content
613+ table = { }
614+ file_content . scan ( %r{
615+ ((?>/\* .*?\* /\s *)?)
616+ ((?:(?:\w +)\s +)?
617+ (?:intern\s +)?VALUE\s +(\w +)
618+ \s *(?:\( [^)]*\) )(?:[^;]|$))
619+ | ((?>/\* .*?\* /\s *))^\s *(\# \s *define\s +(\w +)\s +(\w +))
620+ | ^\s *\# \s *define\s +(\w +)\s +(\w +)
621+ }xm ) do
622+ case
623+ when $1
624+ table [ $3] = [ :func_def , $1, $2, $~. offset ( 2 ) ] if !table [ $3] || table [ $3] [ 0 ] != :func_def
625+ when $4
626+ table [ $6] = [ :macro_def , $4, $5, $~. offset ( 5 ) , $7] if !table [ $6] || table [ $6] [ 0 ] == :macro_alias
627+ when $8
628+ table [ $8] ||= [ :macro_alias , $9]
629+ end
630+ end
631+ table
632+ end
633+
609634 ##
610635 # Find the C code corresponding to a Ruby method
611636
612637 def find_body class_name , meth_name , meth_obj , file_content , quiet = false
613- case file_content
614- when %r%((?>/\* .*?\* /\s *)?)
615- ((?:(?:\w +)\s +)?
616- (?:intern\s +)?VALUE\s +#{ meth_name }
617- \s *(\( [^)]*\) )([^;]|$))%xm then
618- comment = RDoc ::Comment . new $1, @top_level
619- body = $2
620- offset , = $~. offset ( 2 )
638+ if file_content
639+ @body_table ||= { }
640+ @body_table [ file_content ] ||= gen_body_table file_content
641+ type , *args = @body_table [ file_content ] [ meth_name ]
642+ end
643+
644+ case type
645+ when :func_def
646+ comment = RDoc ::Comment . new args [ 0 ] , @top_level
647+ body = args [ 1 ]
648+ offset , = args [ 2 ]
621649
622650 comment . remove_private if comment
623651
@@ -646,12 +674,12 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
646674 meth_obj . line = file_content [ 0 , offset ] . count ( "\n " ) + 1
647675
648676 body
649- when %r%((?>/ \* .*? \* / \s *))^ \s *( \# \s *define \s + #{ meth_name } \s +( \w +))%m then
650- comment = RDoc ::Comment . new $1 , @top_level
651- body = $2
652- offset = $~ . offset ( 2 ) . first
677+ when :macro_def
678+ comment = RDoc ::Comment . new args [ 0 ] , @top_level
679+ body = args [ 1 ]
680+ offset , = args [ 2 ]
653681
654- find_body class_name , $3 , meth_obj , file_content , true
682+ find_body class_name , args [ 3 ] , meth_obj , file_content , true
655683
656684 comment . normalize
657685 find_modifiers comment , meth_obj
@@ -665,11 +693,11 @@ def find_body class_name, meth_name, meth_obj, file_content, quiet = false
665693 meth_obj . line = file_content [ 0 , offset ] . count ( "\n " ) + 1
666694
667695 body
668- when %r%^ \s * \# \s *define \s + #{ meth_name } \s +( \w +)%m then
696+ when :macro_alias
669697 # with no comment we hope the aliased definition has it and use it's
670698 # definition
671699
672- body = find_body ( class_name , $1 , meth_obj , file_content , true )
700+ body = find_body ( class_name , args [ 0 ] , meth_obj , file_content , true )
673701
674702 return body if body
675703
@@ -764,28 +792,42 @@ def find_class_comment class_name, class_mod
764792 class_mod . add_comment comment , @top_level
765793 end
766794
795+ ##
796+ # Generate a const table
797+
798+ def gen_const_table file_content
799+ table = { }
800+ @content . scan ( %r{
801+ ((?>^\s */\* .*?\* /\s +))
802+ rb_define_(\w +)\( (?:\s *(?:\w +),)?\s *
803+ "(\w +)"\s *,
804+ .*?\) \s *;
805+ | Document-(?:const|global|variable):\s
806+ ((?:\w +::)*\w +)
807+ \s *?\n ((?>.*?\* /))
808+ }mxi ) do
809+ case
810+ when $1 then table [ [ $2, $3] ] = $1
811+ when $4 then table [ $4] = "/*\n " + $5
812+ end
813+ end
814+ table
815+ end
816+
767817 ##
768818 # Finds a comment matching +type+ and +const_name+ either above the
769819 # comment or in the matching Document- section.
770820
771821 def find_const_comment ( type , const_name , class_name = nil )
772- comment = if @content =~ %r%((?>^\s */\* .*?\* /\s +))
773- rb_define_#{ type } \( (?:\s *(\w +),)?\s *
774- "#{ const_name } "\s *,
775- .*?\) \s *;%xmi then
776- $1
777- elsif class_name and
778- @content =~ %r%Document-(?:const|global|variable):\s
779- #{ class_name } ::#{ const_name }
780- \s *?\n ((?>.*?\* /))%xm then
781- "/*\n #{ $1} "
782- elsif @content =~ %r%Document-(?:const|global|variable):
783- \s #{ const_name }
784- \s *?\n ((?>.*?\* /))%xm then
785- "/*\n #{ $1} "
786- else
787- ''
788- end
822+ @const_table ||= { }
823+ @const_table [ @content ] ||= gen_const_table @content
824+ table = @const_table [ @content ]
825+
826+ comment =
827+ table [ [ type , const_name ] ] ||
828+ ( class_name && table [ class_name + "::" + const_name ] ) ||
829+ table [ const_name ] ||
830+ ''
789831
790832 RDoc ::Comment . new comment , @top_level
791833 end
0 commit comments