We attempt to parse C extension files. Basically we look for the standard patterns that you find in extensions: rb_define_class, rb_define_method and so on. We also try to find the corresponding C source for the methods and extract comments, but if we fail we don’t worry too much.
The comments associated with a Ruby method are extracted from the C comment block associated with the routine that implements that method, that is to say the method whose name is given in the rb_define_method call. For example, you might write:
/*
* Returns a new array that is a one-dimensional flattening of this
* array (recursively). That is, for every element that is an array,
* extract its elements into the new array.
* * s = [ 1, 2, 3 ] #=> [1, 2, 3]
* t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
* a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
* a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
*/
static VALUE
rb_ary_flatten(ary)
VALUE ary;
{
ary = rb_obj_dup(ary);
rb_ary_flatten_bang(ary);
return ary;
}
...
void
Init_Array()
{
...
rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);
Here RDoc will determine from the rb_define_method line that there’s a method called “flatten” in class Array, and will look for the implementation in the method rb_ary_flatten. It will then use the comment from that method in the HTML output. This method must be in the same source file as the rb_define_method.
The comment blocks may include special directives:
This comment block is documentation for the given class. Use this when the Init_xxx method is not named after the class.
This comment documents the named method. Use when RDoc cannot automatically find the method from it’s declaration
Because C source doesn’t give descripive names to Ruby-level parameters, you need to document the calling sequence explicitly
In addition, RDoc assumes by default that the C method implementing a Ruby function is in the same source file as the rb_define_method call. If this isn’t the case, add the comment:
rb_define_method(....); // in filename
As an example, we might have an extension that defines multiple classes in its Init_xxx method. We could document them using
/*
* Document-class: MyClass
* * Encapsulate the writing and reading of the configuration
* file. ...
*/
/*
* Document-method: read_value
* * call-seq:
* cfg.read_value(key) -> value
* cfg.read_value(key} { |key| } -> value
* * Return the value corresponding to +key+ from the configuration.
* In the second form, if the key isn't found, invoke the
* block and return its value.
*/
Prepare to parse a C file
# File lib/rdoc/parser/c.rb, line 117
117: def initialize(top_level, file_name, content, options, stats)
118: super
119:
120: @known_classes = RDoc::KNOWN_CLASSES.dup
121: @content = handle_tab_width handle_ifdefs_in(@content)
122: @classes = Hash.new
123: @singleton_classes = Hash.new
124: @file_dir = File.dirname(@file_name)
125: end
# File lib/rdoc/parser/c.rb, line 127
127: def do_aliases
128: @content.scan(/rb_define_alias\s*\(
129: \s*(\w+),
130: \s*"(.+?)",
131: \s*"(.+?)"
132: \s*\)/m) do |var_name, new_name, old_name|
133: class_name = @known_classes[var_name] || var_name
134: class_obj = find_class var_name, class_name
135:
136: al = RDoc::Alias.new '', old_name, new_name, ''
137: al.singleton = @singleton_classes.key?(var_name)
138:
139: comment = find_alias_comment var_name, new_name, old_name
140: comment = strip_stars comment
141: al.comment = comment
142:
143: class_obj.add_alias al
144: @stats.add_alias al
145: end
146: end
# File lib/rdoc/parser/c.rb, line 148
148: def do_classes
149: @content.scan(/(\w+)\s* = \s*rb_define_module\s*\(\s*"(\w+)"\s*\)/x) do
150: |var_name, class_name|
151: handle_class_module(var_name, "module", class_name, nil, nil)
152: end
153:
154: # The '.' lets us handle SWIG-generated files
155: @content.scan(/([\w\.]+)\s* = \s*rb_define_class\s*
156: \(
157: \s*"(\w+)",
158: \s*(\w+)\s*
159: \)/x) do |var_name, class_name, parent|
160: handle_class_module(var_name, "class", class_name, parent, nil)
161: end
162:
163: @content.scan(/(\w+)\s*=\s*boot_defclass\s*\(\s*"(\w+?)",\s*(\w+?)\s*\)/) do
164: |var_name, class_name, parent|
165: parent = nil if parent == "0"
166: handle_class_module(var_name, "class", class_name, parent, nil)
167: end
168:
169: @content.scan(/(\w+)\s* = \s*rb_define_module_under\s*
170: \(
171: \s*(\w+),
172: \s*"(\w+)"
173: \s*\)/x) do |var_name, in_module, class_name|
174: handle_class_module(var_name, "module", class_name, nil, in_module)
175: end
176:
177: @content.scan(/([\w\.]+)\s* = \s*rb_define_class_under\s*
178: \(
179: \s*(\w+),
180: \s*"(\w+)",
181: \s*([\w\*\s\(\)\.\->]+)\s* # for SWIG
182: \s*\)/x) do |var_name, in_module, class_name, parent|
183: handle_class_module(var_name, "class", class_name, parent, in_module)
184: end
185:
186: @content.scan(/([\w\.]+)\s* = \s*rb_singleton_class\s*
187: \(
188: \s*(\w+)
189: \s*\)/x) do |sclass_var, class_var|
190: handle_singleton sclass_var, class_var
191: end
192: end
# File lib/rdoc/parser/c.rb, line 194
194: def do_constants
195: @content.scan(%\Wrb_define_
196: ( variable |
197: readonly_variable |
198: const |
199: global_const | )
200: \s*\(
201: (?:\s*(\w+),)?
202: \s*"(\w+)",
203: \s*(.*?)\s*\)\s*;
204: %m) do |type, var_name, const_name, definition|
205: var_name = "rb_cObject" if !var_name or var_name == "rb_mKernel"
206: handle_constants type, var_name, const_name, definition
207: end
208: end
Look for includes of the form:
rb_include_module(rb_cArray, rb_mEnumerable);
# File lib/rdoc/parser/c.rb, line 215
215: def do_includes
216: @content.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c,m|
217: if cls = @classes[c]
218: m = @known_classes[m] || m
219: cls.add_include RDoc::Include.new(m, "")
220: end
221: end
222: end
# File lib/rdoc/parser/c.rb, line 224
224: def do_methods
225: @content.scan(%rb_define_
226: (
227: singleton_method |
228: method |
229: module_function |
230: private_method
231: )
232: \s*\(\s*([\w\.]+),
233: \s*"([^"]+)",
234: \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
235: \s*(-?\w+)\s*\)
236: (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
237: %m) do
238: |type, var_name, meth_name, meth_body, param_count, source_file|
239:
240: # Ignore top-object and weird struct.c dynamic stuff
241: next if var_name == "ruby_top_self"
242: next if var_name == "nstr"
243: next if var_name == "envtbl"
244: next if var_name == "argf" # it'd be nice to handle this one
245:
246: var_name = "rb_cObject" if var_name == "rb_mKernel"
247: handle_method(type, var_name, meth_name, meth_body, param_count,
248: source_file)
249: end
250:
251: @content.scan(%rb_define_attr\(
252: \s*([\w\.]+),
253: \s*"([^"]+)",
254: \s*(\d+),
255: \s*(\d+)\s*\);
256: %m) do |var_name, attr_name, attr_reader, attr_writer|
257: #var_name = "rb_cObject" if var_name == "rb_mKernel"
258: handle_attr(var_name, attr_name,
259: attr_reader.to_i != 0,
260: attr_writer.to_i != 0)
261: end
262:
263: @content.scan(%rb_define_global_function\s*\(
264: \s*"([^"]+)",
265: \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
266: \s*(-?\w+)\s*\)
267: (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
268: %m) do |meth_name, meth_body, param_count, source_file|
269: handle_method("method", "rb_mKernel", meth_name,
270: meth_body, param_count, source_file)
271: end
272:
273: @content.scan(/define_filetest_function\s*\(
274: \s*"([^"]+)",
275: \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
276: \s*(-?\w+)\s*\)/m) do |meth_name, meth_body, param_count|
277:
278: handle_method("method", "rb_mFileTest", meth_name, meth_body, param_count)
279: handle_method("singleton_method", "rb_cFile", meth_name, meth_body, param_count)
280: end
281: end
# File lib/rdoc/parser/c.rb, line 283
283: def find_alias_comment(class_name, new_name, old_name)
284: if content =~ %((?>/\*.*?\*/\s+))
285: rb_define_alias\(\s*#{class_name}\s*,
286: \s*"#{new_name}"\s*,
287: \s*"#{old_name}"\s*\);%m then
288: $1
289: else
290: ''
291: end
292: end
# File lib/rdoc/parser/c.rb, line 294
294: def find_attr_comment(attr_name)
295: if @content =~ %((?>/\*.*?\*/\s+))
296: rb_define_attr\((?:\s*(\w+),)?\s*
297: "#{attr_name}"\s*,.*?\)\s*;%mi
298: $1
299: elsif @content =~ %Document-attr:\s#{attr_name}\s*?\n((?>.*?\*/))%
300: $1
301: else
302: ''
303: end
304: end
Find the C code corresponding to a Ruby method
# File lib/rdoc/parser/c.rb, line 309
309: def find_body(class_name, meth_name, meth_obj, body, quiet = false)
310: case body
311: when %((?>/\*.*?\*/\s*))
312: ((?:(?:static|SWIGINTERN)\s+)?
313: (?:intern\s+)?VALUE\s+#{meth_name}
314: \s*(\([^)]*\))([^;]|$))%m then
315: comment = $1
316: body_text = $2
317: params = $3
318:
319: remove_private_comments comment if comment
320:
321: # see if we can find the whole body
322:
323: re = Regexp.escape(body_text) + '[^(]*^\{.*?^\}'
324: body_text = $& if /#{re}/ =~ body
325:
326: # The comment block may have been overridden with a 'Document-method'
327: # block. This happens in the interpreter when multiple methods are
328: # vectored through to the same C method but those methods are logically
329: # distinct (for example Kernel.hash and Kernel.object_id share the same
330: # implementation
331:
332: override_comment = find_override_comment class_name, meth_obj.name
333: comment = override_comment if override_comment
334:
335: find_modifiers comment, meth_obj if comment
336:
337: #meth_obj.params = params
338: meth_obj.start_collecting_tokens
339: tk = RDoc::RubyToken::Token.new nil, 1, 1
340: tk.set_text body_text
341: meth_obj.add_token tk
342: meth_obj.comment = strip_stars comment
343: when %((?>/\*.*?\*/\s*))^\s*(\#\s*define\s+#{meth_name}\s+(\w+))%
344: comment = $1
345: body_text = $2
346: find_body class_name, $3, meth_obj, body, true
347: find_modifiers comment, meth_obj
348:
349: meth_obj.start_collecting_tokens
350: tk = RDoc::RubyToken::Token.new nil, 1, 1
351: tk.set_text body_text
352: meth_obj.add_token tk
353: meth_obj.comment = strip_stars(comment) + meth_obj.comment.to_s
354: when %^\s*\#\s*define\s+#{meth_name}\s+(\w+)%
355: unless find_body(class_name, $1, meth_obj, body, true)
356: warn "No definition for #{meth_name}" unless @options.quiet
357: return false
358: end
359: else
360: # No body, but might still have an override comment
361: comment = find_override_comment(class_name, meth_obj.name)
362:
363: if comment
364: find_modifiers(comment, meth_obj)
365: meth_obj.comment = strip_stars comment
366: else
367: warn "No definition for #{meth_name}" unless @options.quiet
368: return false
369: end
370: end
371: true
372: end
# File lib/rdoc/parser/c.rb, line 374
374: def find_class(raw_name, name)
375: unless @classes[raw_name]
376: if raw_name =~ /^rb_m/
377: container = @top_level.add_module RDoc::NormalModule, name
378: else
379: container = @top_level.add_class RDoc::NormalClass, name
380: end
381:
382: container.record_location @top_level
383: @classes[raw_name] = container
384: end
385: @classes[raw_name]
386: end
Look for class or module documentation above Init_+class_name+(void), in a Document-class class_name (or module) comment or above an rb_define_class (or module). If a comment is supplied above a matching Init_ and a rb_define_class the Init_ comment is used.
/*
* This is a comment for Foo
*/
Init_Foo(void) {
VALUE cFoo = rb_define_class("Foo", rb_cObject);
}
/*
* Document-class: Foo
* This is a comment for Foo
*/
Init_foo(void) {
VALUE cFoo = rb_define_class("Foo", rb_cObject);
}
/*
* This is a comment for Foo
*/
VALUE cFoo = rb_define_class("Foo", rb_cObject);
# File lib/rdoc/parser/c.rb, line 414
414: def find_class_comment(class_name, class_mod)
415: comment = nil
416:
417: if @content =~ %
418: ((?>/\*.*?\*/\s+))
419: (static\s+)?
420: void\s+
421: Init_#{class_name}\s*(?:_\(\s*)?\(\s*(?:void\s*)?\)%mi then
422: comment = $1
423: elsif @content =~ %Document-(?:class|module):\s+#{class_name}\s*?
424: (?:<\s+[:,\w]+)?\n((?>.*?\*/))%m then
425: comment = $1
426: elsif @content =~ %((?>/\*.*?\*/\s+))
427: ([\w\.\s]+\s* = \s+)?rb_define_(class|module).*?"(#{class_name})"%m then
428: comment = $1
429: end
430:
431: return unless comment
432:
433: comment = strip_stars comment
434:
435: comment = look_for_directives_in class_mod, comment
436:
437: class_mod.comment = comment
438: end
Finds a comment matching type and const_name either above the comment or in the matching Document- section.
# File lib/rdoc/parser/c.rb, line 444
444: def find_const_comment(type, const_name)
445: if @content =~ %((?>^\s*/\*.*?\*/\s+))
446: rb_define_#{type}\((?:\s*(\w+),)?\s*
447: "#{const_name}"\s*,
448: .*?\)\s*;%mi then
449: $1
450: elsif @content =~ %Document-(?:const|global|variable):\s#{const_name}
451: \s*?\n((?>.*?\*/))%m
452: $1
453: else
454: ''
455: end
456: end
If the comment block contains a section that looks like:
use it for the parameters.
# File lib/rdoc/parser/c.rb, line 467
467: def find_modifiers(comment, meth_obj)
468: if comment.sub!(/:nodoc:\s*^\s*\*?\s*$/, '') or
469: comment.sub!(/\A\/\*\s*:nodoc:\s*\*\/\Z/, '') then
470: meth_obj.document_self = false
471: end
472:
473: if comment.sub!(/call-seq:(.*?)^\s*\*?\s*$/, '') or
474: comment.sub!(/\A\/\*\s*call-seq:(.*?)\*\/\Z/, '') then
475: seq = $1
476: seq.gsub!(/^\s*\*\s*/, '')
477: meth_obj.call_seq = seq
478: end
479: end
# File lib/rdoc/parser/c.rb, line 481
481: def find_override_comment(class_name, meth_name)
482: name = Regexp.escape(meth_name)
483: if @content =~ %Document-method:\s+#{class_name}(?:\.|::|#)#{name}\s*?\n((?>.*?\*/))% then
484: $1
485: elsif @content =~ %Document-method:\s#{name}\s*?\n((?>.*?\*/))% then
486: $1
487: end
488: end
# File lib/rdoc/parser/c.rb, line 490
490: def handle_attr(var_name, attr_name, reader, writer)
491: rw = ''
492: rw << 'R' if reader
493: rw << 'W' if writer
494:
495: class_name = @known_classes[var_name]
496:
497: return unless class_name
498:
499: class_obj = find_class(var_name, class_name)
500:
501: if class_obj
502: comment = find_attr_comment(attr_name)
503: comment = strip_stars comment
504: att = RDoc::Attr.new '', attr_name, rw, comment
505: @stats.add_method att
506: class_obj.add_attribute(att)
507: end
508: end
# File lib/rdoc/parser/c.rb, line 510
510: def handle_class_module(var_name, type, class_name, parent, in_module)
511: parent_name = @known_classes[parent] || parent
512:
513: if in_module then
514: enclosure = @classes[in_module] || @@enclosure_classes[in_module]
515:
516: if enclosure.nil? and enclosure = @known_classes[in_module] then
517: type = /^rb_m/ =~ in_module ? "module" : "class"
518: handle_class_module in_module, type, enclosure, nil, nil
519: enclosure = @classes[in_module]
520: end
521:
522: unless enclosure then
523: warn "Enclosing class/module '#{in_module}' for #{type} #{class_name} not known"
524: return
525: end
526: else
527: enclosure = @top_level
528: end
529:
530: if type == "class" then
531: full_name = if RDoc::ClassModule === enclosure then
532: enclosure.full_name + "::#{class_name}"
533: else
534: class_name
535: end
536:
537: if @content =~ %Document-class:\s+#{full_name}\s*<\s+([:,\w]+)% then
538: parent_name = $1
539: end
540:
541: cm = enclosure.add_class RDoc::NormalClass, class_name, parent_name
542:
543: @stats.add_class cm
544: else
545: cm = enclosure.add_module RDoc::NormalModule, class_name
546: @stats.add_module cm
547: end
548:
549: cm.record_location enclosure.top_level
550:
551: find_class_comment cm.full_name, cm
552:
553: @classes[var_name] = cm
554: @@enclosure_classes[var_name] = cm
555: @known_classes[var_name] = cm.full_name
556: end
Adds constant comments. By providing some_value: at the start ofthe comment you can override the C value of the comment to give a friendly definition.
/* 300: The perfect score in bowling */ rb_define_const(cFoo, "PERFECT", INT2FIX(300);
Will override +INT2FIX(300)+ with the value 300 in the output RDoc. Values may include quotes and escaped colons (:).
# File lib/rdoc/parser/c.rb, line 569
569: def handle_constants(type, var_name, const_name, definition)
570: class_name = @known_classes[var_name]
571:
572: return unless class_name
573:
574: class_obj = find_class var_name, class_name
575:
576: unless class_obj then
577: warn "Enclosing class/module #{const_name.inspect} not known"
578: return
579: end
580:
581: comment = find_const_comment type, const_name
582: comment = strip_stars comment
583: comment = normalize_comment comment
584:
585: # In the case of rb_define_const, the definition and comment are in
586: # "/* definition: comment */" form. The literal ':' and '\' characters
587: # can be escaped with a backslash.
588: if type.downcase == 'const' then
589: elements = comment.split ':'
590:
591: if elements.nil? or elements.empty? then
592: con = RDoc::Constant.new const_name, definition, comment
593: else
594: new_definition = elements[0..2].join(':')
595:
596: if new_definition.empty? then # Default to literal C definition
597: new_definition = definition
598: else
599: new_definition.gsub!("\:", ":")
600: new_definition.gsub!("\\", '\')
601: end
602:
603: new_definition.sub!(/\A(\s+)/, '')
604:
605: new_comment = if $1.nil? then
606: elements.last.lstrip
607: else
608: "#{$1}#{elements.last.lstrip}"
609: end
610:
611: con = RDoc::Constant.new const_name, new_definition, new_comment
612: end
613: else
614: con = RDoc::Constant.new const_name, definition, comment
615: end
616:
617: @stats.add_constant con
618: class_obj.add_constant con
619: end
Removes ifdefs that would otherwise confuse us
# File lib/rdoc/parser/c.rb, line 624
624: def handle_ifdefs_in(body)
625: body.gsub(/^#ifdef HAVE_PROTOTYPES.*?#else.*?\n(.*?)#endif.*?\n/, '\1')
626: end
# File lib/rdoc/parser/c.rb, line 628
628: def handle_method(type, var_name, meth_name, meth_body, param_count,
629: source_file = nil)
630: singleton = false
631: class_name = @known_classes[var_name]
632:
633: unless class_name then
634: class_name = @singleton_classes[var_name]
635: singleton = true if class_name
636: end
637:
638: return unless class_name
639:
640: class_obj = find_class var_name, class_name
641:
642: if class_obj then
643: if meth_name == "initialize" then
644: meth_name = "new"
645: singleton = true
646: end
647:
648: meth_obj = RDoc::AnyMethod.new '', meth_name
649: meth_obj.singleton =
650: singleton || ]singleton_method module_function].include?(type)
651:
652: p_count = Integer(param_count) rescue 1
653:
654: if p_count < 0 then
655: meth_obj.params = "(...)"
656: elsif p_count == 0
657: meth_obj.params = "()"
658: else
659: meth_obj.params = "(" + (1..p_count).map{|i| "p#{i}"}.join(", ") + ")"
660: end
661:
662: if source_file then
663: file_name = File.join @file_dir, source_file
664:
665: if File.exist? file_name then
666: body = (@@known_bodies[file_name] ||= File.read(file_name))
667: else
668: warn "unknown source #{source_file} for #{meth_name} in #{@file_name}"
669: end
670: else
671: body = @content
672: end
673:
674: if find_body(class_name, meth_body, meth_obj, body) and meth_obj.document_self then
675: class_obj.add_method meth_obj
676: @stats.add_method meth_obj
677: meth_obj.visibility = :private if 'private_method' == type
678: end
679: end
680: end
# File lib/rdoc/parser/c.rb, line 682
682: def handle_singleton sclass_var, class_var
683: class_name = @known_classes[class_var]
684:
685: @singleton_classes[sclass_var] = class_name
686: end
# File lib/rdoc/parser/c.rb, line 688
688: def handle_tab_width(body)
689: if /\t/ =~ body
690: tab_width = @options.tab_width
691: body.split(/\n/).map do |line|
692: 1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)} && $~ #`
693: line
694: end .join("\n")
695: else
696: body
697: end
698: end
Look for directives in a normal comment block:
/* * :title: My Awesome Project */
This routine modifies it’s parameter
# File lib/rdoc/parser/c.rb, line 709
709: def look_for_directives_in(context, comment)
710: preprocess = RDoc::Markup::PreProcess.new @file_name, @options.rdoc_include
711:
712: preprocess.handle comment, context do |directive, param|
713: case directive
714: when 'main' then
715: @options.main_page = param
716: ''
717: when 'title' then
718: @options.title = param
719: ''
720: end
721: end
722:
723: comment
724: end
Removes lines that are commented out that might otherwise get picked up when scanning for classes and methods
# File lib/rdoc/parser/c.rb, line 729
729: def remove_commented_out_lines
730: @content.gsub!(%//.*rb_define_%, '//')
731: end
# File lib/rdoc/parser/c.rb, line 733
733: def remove_private_comments(comment)
734: comment.gsub!(/\/?\*--\n(.*?)\/?\*\+\+/, '')
735: comment.sub!(/\/?\*--\n.*/, '')
736: end
Extract the classes/modules and methods from a C file and return the corresponding top-level object
# File lib/rdoc/parser/c.rb, line 742
742: def scan
743: remove_commented_out_lines
744: do_classes
745: do_constants
746: do_methods
747: do_includes
748: do_aliases
749: @top_level
750: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.