1class CocoaRef::FunctionDef < CocoaRef::MethodDef
2  def to_rdoc
3    str  = ''
4    str += "  # Description::  #{@description.rdocify}\n"  unless @description.empty?
5    unless @discussion.empty?
6      index = 0
7      @discussion.each do |paragraph|
8        if index.zero?
9          str += "  # Discussion::   #{paragraph.rdocify}\n"
10        else
11          str += "  #                #{paragraph.rdocify}\n"
12        end
13        str += "  #\n"
14        index = index.next
15      end
16    end
17    str += "  # Availability:: #{@availability.rdocify}\n" unless @availability.empty?
18    unless @see_also.empty?
19      str += "  # See also::     "
20      @see_also.each do |s|
21        str += "<tt>#{s.to_rb_def}</tt> " unless s.empty?
22      end
23      str += "\n"
24    end
25    str += "  def self.#{self.to_rb_def}\n"
26    str += "    # #{self.definition.gsub(/\n/, ' ').strip_tags.clean_special_chars}\n"
27    str += "    #\n"
28  
29    str += "  end\n\n"
30  
31    return str
32  end
33  
34  def to_rb_def
35    #puts @definition.clean_objc
36  
37    function_def_parts = self.parse
38    str = "#{@name}(#{function_def_parts.collect {|f| f[:arg] }.join(', ')})"
39  
40    if str =~ /^[_(]+/
41      error_str  = "[WARNING] A empty string was returned as the method definition for:\n"
42      error_str += "          #{@name}\n"
43      @log.add(error_str)
44      return 'an_error_occurred_while_parsing_method_def!'
45    else
46      return str
47    end
48  end
49  
50  def regexp_start
51    self.override_result('(\()(.+)(\))', :new_regexp_start)
52  end
53  def regexp_repeater
54    self.override_result('^(.+)\s+([^\s]+)\s*$', :new_regexp_repeater)
55  end
56  
57  def regexp_result_arg(res)
58    self.override_result(res.last.gsub(/\*/, ''), :new_regexp_result_arg, [res])
59  end
60  def regexp_result_type(res)
61    self.override_result(res.first.gsub(/\*/, ''), :new_regexp_result_type, [res])
62  end
63  
64  def parse
65    args_part = self.definition.clean_objc.scan(Regexp.new(self.regexp_start)).flatten[1]  
66    return [] if args_part.nil?
67    
68    args = args_part.split(', ')
69    function_def_parts = []
70    if args.length == 1 and args.first == 'void'
71      return function_def_parts
72    else
73      args.each do |a|
74        if a.split("\s").length > 1
75          parsed_arg = a.scan(Regexp.new(self.regexp_repeater)).flatten
76          #p parsed_arg
77
78          unless parsed_arg.empty?
79            function_def_part = {}
80            function_def_part[:arg]  = regexp_result_arg(parsed_arg)
81            function_def_part[:type] = regexp_result_type(parsed_arg)
82            function_def_parts.push function_def_part
83          else
84            puts "[WARNING] A empty string was returned as a argument to function:\n" if $COCOA_REF_DEBUG
85            puts "          #{@name}\n" if $COCOA_REF_DEBUG
86          end
87        else
88          function_def_part = {}
89          function_def_part[:arg]  = a
90          function_def_part[:type] = 'id'
91          function_def_parts.push function_def_part
92        end
93      end
94
95      #p function_def_parts
96      function_def_parts.each do |p|
97        if p[:arg] == '...'
98          p[:arg] = '*args'
99        else
100          p[:arg].sub!(/^[A-Z]/) { |s| s.downcase }
101          p[:arg].sub!(/\[\d*\]/, '')
102        end
103      end 
104      return function_def_parts
105    end
106  end
107end
108