PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/jruby-1.1.6RC1/lib/ruby/1.8/rdoc/parsers/parse_c.rb

https://bitbucket.org/nicksieger/advent-jruby
Ruby | 773 lines | 555 code | 66 blank | 152 comment | 36 complexity | 15cdc53945e113706f9c8b9f2cd7c2b7 MD5 | raw file
Possible License(s): CPL-1.0, AGPL-1.0, LGPL-2.1, JSON
  1. # Classes and modules built in to the interpreter. We need
  2. # these to define superclasses of user objects
  3. require "rdoc/code_objects"
  4. require "rdoc/parsers/parserfactory"
  5. require "rdoc/options"
  6. require "rdoc/rdoc"
  7. module RDoc
  8. ##
  9. # Ruby's built-in classes.
  10. KNOWN_CLASSES = {
  11. "rb_cObject" => "Object",
  12. "rb_cArray" => "Array",
  13. "rb_cBignum" => "Bignum",
  14. "rb_cClass" => "Class",
  15. "rb_cDir" => "Dir",
  16. "rb_cData" => "Data",
  17. "rb_cFalseClass" => "FalseClass",
  18. "rb_cFile" => "File",
  19. "rb_cFixnum" => "Fixnum",
  20. "rb_cFloat" => "Float",
  21. "rb_cHash" => "Hash",
  22. "rb_cInteger" => "Integer",
  23. "rb_cIO" => "IO",
  24. "rb_cModule" => "Module",
  25. "rb_cNilClass" => "NilClass",
  26. "rb_cNumeric" => "Numeric",
  27. "rb_cProc" => "Proc",
  28. "rb_cRange" => "Range",
  29. "rb_cRegexp" => "Regexp",
  30. "rb_cString" => "String",
  31. "rb_cSymbol" => "Symbol",
  32. "rb_cThread" => "Thread",
  33. "rb_cTime" => "Time",
  34. "rb_cTrueClass" => "TrueClass",
  35. "rb_cStruct" => "Struct",
  36. "rb_eException" => "Exception",
  37. "rb_eStandardError" => "StandardError",
  38. "rb_eSystemExit" => "SystemExit",
  39. "rb_eInterrupt" => "Interrupt",
  40. "rb_eSignal" => "Signal",
  41. "rb_eFatal" => "Fatal",
  42. "rb_eArgError" => "ArgError",
  43. "rb_eEOFError" => "EOFError",
  44. "rb_eIndexError" => "IndexError",
  45. "rb_eRangeError" => "RangeError",
  46. "rb_eIOError" => "IOError",
  47. "rb_eRuntimeError" => "RuntimeError",
  48. "rb_eSecurityError" => "SecurityError",
  49. "rb_eSystemCallError" => "SystemCallError",
  50. "rb_eTypeError" => "TypeError",
  51. "rb_eZeroDivError" => "ZeroDivError",
  52. "rb_eNotImpError" => "NotImpError",
  53. "rb_eNoMemError" => "NoMemError",
  54. "rb_eFloatDomainError" => "FloatDomainError",
  55. "rb_eScriptError" => "ScriptError",
  56. "rb_eNameError" => "NameError",
  57. "rb_eSyntaxError" => "SyntaxError",
  58. "rb_eLoadError" => "LoadError",
  59. "rb_mKernel" => "Kernel",
  60. "rb_mComparable" => "Comparable",
  61. "rb_mEnumerable" => "Enumerable",
  62. "rb_mPrecision" => "Precision",
  63. "rb_mErrno" => "Errno",
  64. "rb_mFileTest" => "FileTest",
  65. "rb_mGC" => "GC",
  66. "rb_mMath" => "Math",
  67. "rb_mProcess" => "Process"
  68. }
  69. ##
  70. # We attempt to parse C extension files. Basically we look for
  71. # the standard patterns that you find in extensions: <tt>rb_define_class,
  72. # rb_define_method</tt> and so on. We also try to find the corresponding
  73. # C source for the methods and extract comments, but if we fail
  74. # we don't worry too much.
  75. #
  76. # The comments associated with a Ruby method are extracted from the C
  77. # comment block associated with the routine that _implements_ that
  78. # method, that is to say the method whose name is given in the
  79. # <tt>rb_define_method</tt> call. For example, you might write:
  80. #
  81. # /*
  82. # * Returns a new array that is a one-dimensional flattening of this
  83. # * array (recursively). That is, for every element that is an array,
  84. # * extract its elements into the new array.
  85. # *
  86. # * s = [ 1, 2, 3 ] #=> [1, 2, 3]
  87. # * t = [ 4, 5, 6, [7, 8] ] #=> [4, 5, 6, [7, 8]]
  88. # * a = [ s, t, 9, 10 ] #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
  89. # * a.flatten #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  90. # */
  91. # static VALUE
  92. # rb_ary_flatten(ary)
  93. # VALUE ary;
  94. # {
  95. # ary = rb_obj_dup(ary);
  96. # rb_ary_flatten_bang(ary);
  97. # return ary;
  98. # }
  99. #
  100. # ...
  101. #
  102. # void
  103. # Init_Array()
  104. # {
  105. # ...
  106. # rb_define_method(rb_cArray, "flatten", rb_ary_flatten, 0);
  107. #
  108. # Here RDoc will determine from the rb_define_method line that there's a
  109. # method called "flatten" in class Array, and will look for the implementation
  110. # in the method rb_ary_flatten. It will then use the comment from that
  111. # method in the HTML output. This method must be in the same source file
  112. # as the rb_define_method.
  113. #
  114. # C classes can be diagramed (see /tc/dl/ruby/ruby/error.c), and RDoc
  115. # integrates C and Ruby source into one tree
  116. #
  117. # The comment blocks may include special direcives:
  118. #
  119. # [Document-class: <i>name</i>]
  120. # This comment block is documentation for the given class. Use this
  121. # when the <tt>Init_xxx</tt> method is not named after the class.
  122. #
  123. # [Document-method: <i>name</i>]
  124. # This comment documents the named method. Use when RDoc cannot
  125. # automatically find the method from it's declaration
  126. #
  127. # [call-seq: <i>text up to an empty line</i>]
  128. # Because C source doesn't give descripive names to Ruby-level parameters,
  129. # you need to document the calling sequence explicitly
  130. #
  131. # In additon, RDoc assumes by default that the C method implementing a
  132. # Ruby function is in the same source file as the rb_define_method call.
  133. # If this isn't the case, add the comment
  134. #
  135. # rb_define_method(....); // in: filename
  136. #
  137. # As an example, we might have an extension that defines multiple classes
  138. # in its Init_xxx method. We could document them using
  139. #
  140. #
  141. # /*
  142. # * Document-class: MyClass
  143. # *
  144. # * Encapsulate the writing and reading of the configuration
  145. # * file. ...
  146. # */
  147. #
  148. # /*
  149. # * Document-method: read_value
  150. # *
  151. # * call-seq:
  152. # * cfg.read_value(key) -> value
  153. # * cfg.read_value(key} { |key| } -> value
  154. # *
  155. # * Return the value corresponding to +key+ from the configuration.
  156. # * In the second form, if the key isn't found, invoke the
  157. # * block and return its value.
  158. # */
  159. #
  160. class C_Parser
  161. attr_accessor :progress
  162. extend ParserFactory
  163. parse_files_matching(/\.(?:([CcHh])\1?|c([+xp])\2|y)\z/)
  164. @@known_bodies = {}
  165. # prepare to parse a C file
  166. def initialize(top_level, file_name, body, options, stats)
  167. @known_classes = KNOWN_CLASSES.dup
  168. @body = handle_tab_width(handle_ifdefs_in(body))
  169. @options = options
  170. @stats = stats
  171. @top_level = top_level
  172. @classes = Hash.new
  173. @file_dir = File.dirname(file_name)
  174. @progress = $stderr unless options.quiet
  175. end
  176. # Extract the classes/modules and methods from a C file
  177. # and return the corresponding top-level object
  178. def scan
  179. remove_commented_out_lines
  180. do_classes
  181. do_constants
  182. do_methods
  183. do_includes
  184. do_aliases
  185. @top_level
  186. end
  187. #######
  188. private
  189. #######
  190. def progress(char)
  191. unless @options.quiet
  192. @progress.print(char)
  193. @progress.flush
  194. end
  195. end
  196. def warn(msg)
  197. $stderr.puts
  198. $stderr.puts msg
  199. $stderr.flush
  200. end
  201. def remove_private_comments(comment)
  202. comment.gsub!(/\/?\*--(.*?)\/?\*\+\+/m, '')
  203. comment.sub!(/\/?\*--.*/m, '')
  204. end
  205. ##
  206. # removes lines that are commented out that might otherwise get picked up
  207. # when scanning for classes and methods
  208. def remove_commented_out_lines
  209. @body.gsub!(%r{//.*rb_define_}, '//')
  210. end
  211. def handle_class_module(var_name, class_mod, class_name, parent, in_module)
  212. progress(class_mod[0, 1])
  213. parent_name = @known_classes[parent] || parent
  214. if in_module
  215. enclosure = @classes[in_module]
  216. unless enclosure
  217. if enclosure = @known_classes[in_module]
  218. handle_class_module(in_module, (/^rb_m/ =~ in_module ? "module" : "class"),
  219. enclosure, nil, nil)
  220. enclosure = @classes[in_module]
  221. end
  222. end
  223. unless enclosure
  224. warn("Enclosing class/module '#{in_module}' for " +
  225. "#{class_mod} #{class_name} not known")
  226. return
  227. end
  228. else
  229. enclosure = @top_level
  230. end
  231. if class_mod == "class"
  232. cm = enclosure.add_class(NormalClass, class_name, parent_name)
  233. @stats.num_classes += 1
  234. else
  235. cm = enclosure.add_module(NormalModule, class_name)
  236. @stats.num_modules += 1
  237. end
  238. cm.record_location(enclosure.toplevel)
  239. find_class_comment(cm.full_name, cm)
  240. @classes[var_name] = cm
  241. @known_classes[var_name] = cm.full_name
  242. end
  243. ##
  244. # Look for class or module documentation above Init_+class_name+(void),
  245. # in a Document-class +class_name+ (or module) comment or above an
  246. # rb_define_class (or module). If a comment is supplied above a matching
  247. # Init_ and a rb_define_class the Init_ comment is used.
  248. #
  249. # /*
  250. # * This is a comment for Foo
  251. # */
  252. # Init_Foo(void) {
  253. # VALUE cFoo = rb_define_class("Foo", rb_cObject);
  254. # }
  255. #
  256. # /*
  257. # * Document-class: Foo
  258. # * This is a comment for Foo
  259. # */
  260. # Init_foo(void) {
  261. # VALUE cFoo = rb_define_class("Foo", rb_cObject);
  262. # }
  263. #
  264. # /*
  265. # * This is a comment for Foo
  266. # */
  267. # VALUE cFoo = rb_define_class("Foo", rb_cObject);
  268. def find_class_comment(class_name, class_meth)
  269. comment = nil
  270. if @body =~ %r{((?>/\*.*?\*/\s+))
  271. (static\s+)?void\s+Init_#{class_name}\s*(?:_\(\s*)?\(\s*(?:void\s*)?\)}xmi
  272. comment = $1
  273. elsif @body =~ %r{Document-(class|module):\s#{class_name}\s*?\n((?>.*?\*/))}m
  274. comment = $2
  275. else
  276. if @body =~ /rb_define_(class|module)/m then
  277. class_name = class_name.split("::").last
  278. comments = []
  279. @body.split(/(\/\*.*?\*\/)\s*?\n/m).each_with_index do |chunk, index|
  280. comments[index] = chunk
  281. if chunk =~ /rb_define_(class|module).*?"(#{class_name})"/m then
  282. comment = comments[index-1]
  283. break
  284. end
  285. end
  286. end
  287. end
  288. class_meth.comment = mangle_comment(comment) if comment
  289. end
  290. ############################################################
  291. def do_classes
  292. @body.scan(/(\w+)\s* = \s*rb_define_module\s*\(\s*"(\w+)"\s*\)/mx) do
  293. |var_name, class_name|
  294. handle_class_module(var_name, "module", class_name, nil, nil)
  295. end
  296. # The '.' lets us handle SWIG-generated files
  297. @body.scan(/([\w\.]+)\s* = \s*rb_define_class\s*
  298. \(
  299. \s*"(\w+)",
  300. \s*(\w+)\s*
  301. \)/mx) do
  302. |var_name, class_name, parent|
  303. handle_class_module(var_name, "class", class_name, parent, nil)
  304. end
  305. @body.scan(/(\w+)\s*=\s*boot_defclass\s*\(\s*"(\w+?)",\s*(\w+?)\s*\)/) do
  306. |var_name, class_name, parent|
  307. parent = nil if parent == "0"
  308. handle_class_module(var_name, "class", class_name, parent, nil)
  309. end
  310. @body.scan(/(\w+)\s* = \s*rb_define_module_under\s*
  311. \(
  312. \s*(\w+),
  313. \s*"(\w+)"
  314. \s*\)/mx) do
  315. |var_name, in_module, class_name|
  316. handle_class_module(var_name, "module", class_name, nil, in_module)
  317. end
  318. @body.scan(/([\w\.]+)\s* = \s*rb_define_class_under\s*
  319. \(
  320. \s*(\w+),
  321. \s*"(\w+)",
  322. \s*(\w+)\s*
  323. \s*\)/mx) do
  324. |var_name, in_module, class_name, parent|
  325. handle_class_module(var_name, "class", class_name, parent, in_module)
  326. end
  327. end
  328. ###########################################################
  329. def do_constants
  330. @body.scan(%r{\Wrb_define_
  331. (
  332. variable |
  333. readonly_variable |
  334. const |
  335. global_const |
  336. )
  337. \s*\(
  338. (?:\s*(\w+),)?
  339. \s*"(\w+)",
  340. \s*(.*?)\s*\)\s*;
  341. }xm) do
  342. |type, var_name, const_name, definition|
  343. var_name = "rb_cObject" if !var_name or var_name == "rb_mKernel"
  344. handle_constants(type, var_name, const_name, definition)
  345. end
  346. end
  347. ############################################################
  348. def do_methods
  349. @body.scan(%r{rb_define_
  350. (
  351. singleton_method |
  352. method |
  353. module_function |
  354. private_method
  355. )
  356. \s*\(\s*([\w\.]+),
  357. \s*"([^"]+)",
  358. \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
  359. \s*(-?\w+)\s*\)
  360. (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
  361. }xm) do
  362. |type, var_name, meth_name, meth_body, param_count, source_file|
  363. #"
  364. # Ignore top-object and weird struct.c dynamic stuff
  365. next if var_name == "ruby_top_self"
  366. next if var_name == "nstr"
  367. next if var_name == "envtbl"
  368. next if var_name == "argf" # it'd be nice to handle this one
  369. var_name = "rb_cObject" if var_name == "rb_mKernel"
  370. handle_method(type, var_name, meth_name,
  371. meth_body, param_count, source_file)
  372. end
  373. @body.scan(%r{rb_define_attr\(
  374. \s*([\w\.]+),
  375. \s*"([^"]+)",
  376. \s*(\d+),
  377. \s*(\d+)\s*\);
  378. }xm) do #"
  379. |var_name, attr_name, attr_reader, attr_writer|
  380. #var_name = "rb_cObject" if var_name == "rb_mKernel"
  381. handle_attr(var_name, attr_name,
  382. attr_reader.to_i != 0,
  383. attr_writer.to_i != 0)
  384. end
  385. @body.scan(%r{rb_define_global_function\s*\(
  386. \s*"([^"]+)",
  387. \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
  388. \s*(-?\w+)\s*\)
  389. (?:;\s*/[*/]\s+in\s+(\w+?\.[cy]))?
  390. }xm) do #"
  391. |meth_name, meth_body, param_count, source_file|
  392. handle_method("method", "rb_mKernel", meth_name,
  393. meth_body, param_count, source_file)
  394. end
  395. @body.scan(/define_filetest_function\s*\(
  396. \s*"([^"]+)",
  397. \s*(?:RUBY_METHOD_FUNC\(|VALUEFUNC\()?(\w+)\)?,
  398. \s*(-?\w+)\s*\)/xm) do #"
  399. |meth_name, meth_body, param_count|
  400. handle_method("method", "rb_mFileTest", meth_name, meth_body, param_count)
  401. handle_method("singleton_method", "rb_cFile", meth_name, meth_body, param_count)
  402. end
  403. end
  404. ############################################################
  405. def do_aliases
  406. @body.scan(%r{rb_define_alias\s*\(\s*(\w+),\s*"([^"]+)",\s*"([^"]+)"\s*\)}m) do
  407. |var_name, new_name, old_name|
  408. @stats.num_methods += 1
  409. class_name = @known_classes[var_name] || var_name
  410. class_obj = find_class(var_name, class_name)
  411. class_obj.add_alias(Alias.new("", old_name, new_name, ""))
  412. end
  413. end
  414. ##
  415. # Adds constant comments. By providing some_value: at the start ofthe
  416. # comment you can override the C value of the comment to give a friendly
  417. # definition.
  418. #
  419. # /* 300: The perfect score in bowling */
  420. # rb_define_const(cFoo, "PERFECT", INT2FIX(300);
  421. #
  422. # Will override +INT2FIX(300)+ with the value +300+ in the output RDoc.
  423. # Values may include quotes and escaped colons (\:).
  424. def handle_constants(type, var_name, const_name, definition)
  425. #@stats.num_constants += 1
  426. class_name = @known_classes[var_name]
  427. return unless class_name
  428. class_obj = find_class(var_name, class_name)
  429. unless class_obj
  430. warn("Enclosing class/module '#{const_name}' for not known")
  431. return
  432. end
  433. comment = find_const_comment(type, const_name)
  434. # In the case of rb_define_const, the definition and comment are in
  435. # "/* definition: comment */" form. The literal ':' and '\' characters
  436. # can be escaped with a backslash.
  437. if type.downcase == 'const' then
  438. elements = mangle_comment(comment).split(':')
  439. if elements.nil? or elements.empty? then
  440. con = Constant.new(const_name, definition, mangle_comment(comment))
  441. else
  442. new_definition = elements[0..-2].join(':')
  443. if new_definition.empty? then # Default to literal C definition
  444. new_definition = definition
  445. else
  446. new_definition.gsub!("\:", ":")
  447. new_definition.gsub!("\\", '\\')
  448. end
  449. new_definition.sub!(/\A(\s+)/, '')
  450. new_comment = $1.nil? ? elements.last : "#{$1}#{elements.last.lstrip}"
  451. con = Constant.new(const_name, new_definition,
  452. mangle_comment(new_comment))
  453. end
  454. else
  455. con = Constant.new(const_name, definition, mangle_comment(comment))
  456. end
  457. class_obj.add_constant(con)
  458. end
  459. ##
  460. # Finds a comment matching +type+ and +const_name+ either above the
  461. # comment or in the matching Document- section.
  462. def find_const_comment(type, const_name)
  463. if @body =~ %r{((?>^\s*/\*.*?\*/\s+))
  464. rb_define_#{type}\((?:\s*(\w+),)?\s*"#{const_name}"\s*,.*?\)\s*;}xmi
  465. $1
  466. elsif @body =~ %r{Document-(?:const|global|variable):\s#{const_name}\s*?\n((?>.*?\*/))}m
  467. $1
  468. else
  469. ''
  470. end
  471. end
  472. ###########################################################
  473. def handle_attr(var_name, attr_name, reader, writer)
  474. rw = ''
  475. if reader
  476. #@stats.num_methods += 1
  477. rw << 'R'
  478. end
  479. if writer
  480. #@stats.num_methods += 1
  481. rw << 'W'
  482. end
  483. class_name = @known_classes[var_name]
  484. return unless class_name
  485. class_obj = find_class(var_name, class_name)
  486. if class_obj
  487. comment = find_attr_comment(attr_name)
  488. unless comment.empty?
  489. comment = mangle_comment(comment)
  490. end
  491. att = Attr.new('', attr_name, rw, comment)
  492. class_obj.add_attribute(att)
  493. end
  494. end
  495. ###########################################################
  496. def find_attr_comment(attr_name)
  497. if @body =~ %r{((?>/\*.*?\*/\s+))
  498. rb_define_attr\((?:\s*(\w+),)?\s*"#{attr_name}"\s*,.*?\)\s*;}xmi
  499. $1
  500. elsif @body =~ %r{Document-attr:\s#{attr_name}\s*?\n((?>.*?\*/))}m
  501. $1
  502. else
  503. ''
  504. end
  505. end
  506. ###########################################################
  507. def handle_method(type, var_name, meth_name,
  508. meth_body, param_count, source_file = nil)
  509. progress(".")
  510. @stats.num_methods += 1
  511. class_name = @known_classes[var_name]
  512. return unless class_name
  513. class_obj = find_class(var_name, class_name)
  514. if class_obj
  515. if meth_name == "initialize"
  516. meth_name = "new"
  517. type = "singleton_method"
  518. end
  519. meth_obj = AnyMethod.new("", meth_name)
  520. meth_obj.singleton =
  521. %w{singleton_method module_function}.include?(type)
  522. p_count = (Integer(param_count) rescue -1)
  523. if p_count < 0
  524. meth_obj.params = "(...)"
  525. elsif p_count == 0
  526. meth_obj.params = "()"
  527. else
  528. meth_obj.params = "(" +
  529. (1..p_count).map{|i| "p#{i}"}.join(", ") +
  530. ")"
  531. end
  532. if source_file
  533. file_name = File.join(@file_dir, source_file)
  534. body = (@@known_bodies[source_file] ||= File.read(file_name))
  535. else
  536. body = @body
  537. end
  538. if find_body(meth_body, meth_obj, body) and meth_obj.document_self
  539. class_obj.add_method(meth_obj)
  540. end
  541. end
  542. end
  543. ############################################################
  544. # Find the C code corresponding to a Ruby method
  545. def find_body(meth_name, meth_obj, body, quiet = false)
  546. case body
  547. when %r{((?>/\*.*?\*/\s*))(?:static\s+)?VALUE\s+#{meth_name}
  548. \s*(\(.*?\)).*?^}xm
  549. comment, params = $1, $2
  550. body_text = $&
  551. remove_private_comments(comment) if comment
  552. # see if we can find the whole body
  553. re = Regexp.escape(body_text) + '[^(]*^\{.*?^\}'
  554. if Regexp.new(re, Regexp::MULTILINE).match(body)
  555. body_text = $&
  556. end
  557. # The comment block may have been overridden with a
  558. # 'Document-method' block. This happens in the interpreter
  559. # when multiple methods are vectored through to the same
  560. # C method but those methods are logically distinct (for
  561. # example Kernel.hash and Kernel.object_id share the same
  562. # implementation
  563. override_comment = find_override_comment(meth_obj.name)
  564. comment = override_comment if override_comment
  565. find_modifiers(comment, meth_obj) if comment
  566. # meth_obj.params = params
  567. meth_obj.start_collecting_tokens
  568. meth_obj.add_token(RubyToken::Token.new(1,1).set_text(body_text))
  569. meth_obj.comment = mangle_comment(comment)
  570. when %r{((?>/\*.*?\*/\s*))^\s*\#\s*define\s+#{meth_name}\s+(\w+)}m
  571. comment = $1
  572. find_body($2, meth_obj, body, true)
  573. find_modifiers(comment, meth_obj)
  574. meth_obj.comment = mangle_comment(comment) + meth_obj.comment
  575. when %r{^\s*\#\s*define\s+#{meth_name}\s+(\w+)}m
  576. unless find_body($1, meth_obj, body, true)
  577. warn "No definition for #{meth_name}" unless quiet
  578. return false
  579. end
  580. else
  581. # No body, but might still have an override comment
  582. comment = find_override_comment(meth_obj.name)
  583. if comment
  584. find_modifiers(comment, meth_obj)
  585. meth_obj.comment = mangle_comment(comment)
  586. else
  587. warn "No definition for #{meth_name}" unless quiet
  588. return false
  589. end
  590. end
  591. true
  592. end
  593. ##
  594. # If the comment block contains a section that looks like:
  595. #
  596. # call-seq:
  597. # Array.new
  598. # Array.new(10)
  599. #
  600. # use it for the parameters.
  601. def find_modifiers(comment, meth_obj)
  602. if comment.sub!(/:nodoc:\s*^\s*\*?\s*$/m, '') or
  603. comment.sub!(/\A\/\*\s*:nodoc:\s*\*\/\Z/, '')
  604. meth_obj.document_self = false
  605. end
  606. if comment.sub!(/call-seq:(.*?)^\s*\*?\s*$/m, '') or
  607. comment.sub!(/\A\/\*\s*call-seq:(.*?)\*\/\Z/, '')
  608. seq = $1
  609. seq.gsub!(/^\s*\*\s*/, '')
  610. meth_obj.call_seq = seq
  611. end
  612. end
  613. ############################################################
  614. def find_override_comment(meth_name)
  615. name = Regexp.escape(meth_name)
  616. if @body =~ %r{Document-method:\s#{name}\s*?\n((?>.*?\*/))}m
  617. $1
  618. end
  619. end
  620. ##
  621. # Look for includes of the form:
  622. #
  623. # rb_include_module(rb_cArray, rb_mEnumerable);
  624. def do_includes
  625. @body.scan(/rb_include_module\s*\(\s*(\w+?),\s*(\w+?)\s*\)/) do |c,m|
  626. if cls = @classes[c]
  627. m = @known_classes[m] || m
  628. cls.add_include(Include.new(m, ""))
  629. end
  630. end
  631. end
  632. ##
  633. # Remove the /*'s and leading asterisks from C comments
  634. def mangle_comment(comment)
  635. comment.sub!(%r{/\*+}) { " " * $&.length }
  636. comment.sub!(%r{\*+/}) { " " * $&.length }
  637. comment.gsub!(/^[ \t]*\*/m) { " " * $&.length }
  638. comment
  639. end
  640. def find_class(raw_name, name)
  641. unless @classes[raw_name]
  642. if raw_name =~ /^rb_m/
  643. @classes[raw_name] = @top_level.add_module(NormalModule, name)
  644. else
  645. @classes[raw_name] = @top_level.add_class(NormalClass, name, nil)
  646. end
  647. end
  648. @classes[raw_name]
  649. end
  650. def handle_tab_width(body)
  651. if /\t/ =~ body
  652. tab_width = Options.instance.tab_width
  653. body.split(/\n/).map do |line|
  654. 1 while line.gsub!(/\t+/) { ' ' * (tab_width*$&.length - $`.length % tab_width)} && $~ #`
  655. line
  656. end .join("\n")
  657. else
  658. body
  659. end
  660. end
  661. ##
  662. # Removes #ifdefs that would otherwise confuse us
  663. def handle_ifdefs_in(body)
  664. body.gsub(/^#ifdef HAVE_PROTOTYPES.*?#else.*?\n(.*?)#endif.*?\n/m) { $1 }
  665. end
  666. end
  667. end