Skip to content

Commit f3ca1e7

Browse files
committed
Support all struct definition functions
Currentry only `rb_struct_define_without_accessor` is supported by #73. We should support other three functions too.
1 parent fd9c58b commit f3ca1e7

File tree

2 files changed

+105
-71
lines changed

2 files changed

+105
-71
lines changed

lib/rdoc/parser/c.rb

Lines changed: 61 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -295,93 +295,83 @@ def do_classes_and_modules
295295

296296
@content.scan(
297297
%r(
298+
(?<open>\s*\(\s*) {0}
299+
(?<close>\s*\)\s*) {0}
300+
(?<name>\s*"(?<class_name>\w+)") {0}
301+
(?<parent>\s*(?:
302+
(?<parent_name>[\w\*\s\(\)\.\->]+) |
303+
rb_path2class\s*\(\s*"(?<path>[\w:]+)"\s*\)
304+
)) {0}
305+
(?<under>\w+) {0}
306+
298307
(?<var_name>[\w\.]+)\s* =
299308
\s*rb_(?:
300309
define_(?:
301-
class(?: # rb_define_class(class_name_1, parent_name_1)
302-
\s*\(
303-
\s*"(?<class_name_1>\w+)",
304-
\s*(?<parent_name_1>\w+)\s*
305-
\)
306-
|
307-
_under\s*\( # rb_define_class_under(class_under, class_name2, parent_name2...)
308-
\s* (?<class_under>\w+),
309-
\s* "(?<class_name_2>\w+)",
310-
\s*
311-
(?:
312-
(?<parent_name_2>[\w\*\s\(\)\.\->]+) |
313-
rb_path2class\("(?<path>[\w:]+)"\)
314-
)
310+
class(?: # rb_define_class(name, parent_name)
311+
\(\s*
312+
\g<name>,
313+
\g<parent>
315314
\s*\)
315+
|
316+
_under\g<open> # rb_define_class_under(under, name, parent_name...)
317+
\g<under>,
318+
\g<name>,
319+
\g<parent>
320+
\g<close>
316321
)
317322
|
318-
module(?: # rb_define_module(module_name_1)
319-
\s*\(
320-
\s*"(?<module_name_1>\w+)"\s*
321-
\)
323+
(?<module>)
324+
module(?: # rb_define_module(name)
325+
\g<open>
326+
\g<name>
327+
\g<close>
322328
|
323-
_under\s*\( # rb_define_module_under(module_under, module_name_2)
324-
\s*(?<module_under>\w+),
325-
\s*"(?<module_name_2>\w+)"
326-
\s*\)
329+
_under\g<open> # rb_define_module_under(under, name)
330+
\g<under>,
331+
\g<name>
332+
\g<close>
327333
)
328334
)
329335
|
330-
struct_define_without_accessor\s*\( # rb_struct_define_without_accessor(class_name_3, parent_name_3, ...)
331-
\s*"(?<class_name_3>\w+)",
332-
\s*(?<parent_name_3>\w+),
333-
\s*\w+, # Allocation function
334-
(?:\s*"\w+",)* # Attributes
335-
\s*NULL
336-
\)
336+
(?<attributes>(?:\s*"\w+",)*\s*NULL\s*) {0}
337+
struct_define(?:
338+
\g<open> # rb_struct_define(name, ...)
339+
\g<name>,
340+
|
341+
_under\g<open> # rb_struct_define_under(under, name, ...)
342+
\g<under>,
343+
\g<name>,
344+
|
345+
_without_accessor(?:
346+
\g<open> # rb_struct_define_without_accessor(name, parent_name, ...)
347+
|
348+
_under\g<open> # rb_struct_define_without_accessor_under(under, name, parent_name, ...)
349+
\g<under>,
350+
)
351+
\g<name>,
352+
\g<parent>,
353+
\s*\w+, # Allocation function
354+
)
355+
\g<attributes>
356+
\g<close>
337357
|
338-
singleton_class\s*\( # rb_singleton_class(target_class_name)
339-
\s*(?<target_class_name>\w+)
340-
\)
358+
singleton_class\g<open> # rb_singleton_class(target_class_name)
359+
(?<target_class_name>\w+)
360+
\g<close>
341361
)
342362
)mx
343363
) do
344-
class_name = $~[:class_name_1]
345-
type = :class
346-
if class_name
347-
# rb_define_class(class_name_1, parent_name_1)
348-
parent_name = $~[:parent_name_1]
349-
#under = nil
350-
else
351-
class_name = $~[:class_name_2]
352-
if class_name
353-
# rb_define_class_under(class_under, class_name2, parent_name2...)
354-
parent_name = $~[:parent_name_2] || $~[:path]
355-
under = $~[:class_under]
356-
else
357-
class_name = $~[:class_name_3]
358-
if class_name
359-
# rb_struct_define_without_accessor(class_name_3, parent_name_3, ...)
360-
parent_name = $~[:parent_name_3]
361-
#under = nil
362-
else
363-
type = :module
364-
class_name = $~[:module_name_1]
365-
#parent_name = nil
366-
if class_name
367-
# rb_define_module(module_name_1)
368-
#under = nil
369-
else
370-
class_name = $~[:module_name_2]
371-
if class_name
372-
# rb_define_module_under(module_under, module_name_1)
373-
under = $~[:module_under]
374-
else
375-
# rb_singleton_class(target_class_name)
376-
target_class_name = $~[:target_class_name]
377-
handle_singleton $~[:var_name], target_class_name
378-
next
379-
end
380-
end
381-
end
382-
end
364+
if target_class_name = $~[:target_class_name]
365+
# rb_singleton_class(target_class_name)
366+
handle_singleton $~[:var_name], target_class_name
367+
next
383368
end
384369

370+
type = $~[:module] ? :module : :class
371+
class_name = $~[:class_name]
372+
parent_name = $~[:parent_name] || $~[:path]
373+
under = $~[:under]
374+
385375
handle_class_module($~[:var_name], type, class_name, parent_name, under)
386376
end
387377
end

test/rdoc/test_rdoc_parser_c.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,35 @@ def test_do_classes_struct
355355
/* Document-class: Foo
356356
* this is the Foo class
357357
*/
358+
VALUE cFoo = rb_struct_define(
359+
"Foo",
360+
"some", "various", "fields", NULL);
361+
EOF
362+
363+
klass = util_get_class content, 'cFoo'
364+
assert_equal "this is the Foo class", klass.comment.text
365+
end
366+
367+
def test_do_classes_struct_under
368+
content = <<-EOF
369+
/* Document-class: Kernel::Foo
370+
* this is the Foo class under Kernel
371+
*/
372+
VALUE cFoo = rb_struct_define_under(
373+
rb_mKernel, "Foo",
374+
"some", "various", "fields", NULL);
375+
EOF
376+
377+
klass = util_get_class content, 'cFoo'
378+
assert_equal 'Kernel::Foo', klass.full_name
379+
assert_equal "this is the Foo class under Kernel", klass.comment.text
380+
end
381+
382+
def test_do_classes_struct_without_accessor
383+
content = <<-EOF
384+
/* Document-class: Foo
385+
* this is the Foo class
386+
*/
358387
VALUE cFoo = rb_struct_define_without_accessor(
359388
"Foo", rb_cObject, foo_alloc,
360389
"some", "various", "fields", NULL);
@@ -364,6 +393,21 @@ def test_do_classes_struct
364393
assert_equal "this is the Foo class", klass.comment.text
365394
end
366395

396+
def test_do_classes_struct_without_accessor_under
397+
content = <<-EOF
398+
/* Document-class: Kernel::Foo
399+
* this is the Foo class under Kernel
400+
*/
401+
VALUE cFoo = rb_struct_define_without_accessor_under(
402+
rb_mKernel, "Foo", rb_cObject, foo_alloc,
403+
"some", "various", "fields", NULL);
404+
EOF
405+
406+
klass = util_get_class content, 'cFoo'
407+
assert_equal 'Kernel::Foo', klass.full_name
408+
assert_equal "this is the Foo class under Kernel", klass.comment.text
409+
end
410+
367411
def test_do_classes_class_under
368412
content = <<-EOF
369413
/* Document-class: Kernel::Foo

0 commit comments

Comments
 (0)