lib/rdoc/parser/c.rb in rdoc-6.4.1.1 vs lib/rdoc/parser/c.rb in rdoc-6.5.0

- old
+ new

@@ -120,10 +120,15 @@ parse_files_matching(/\.(?:([CcHh])\1?|c([+xp])\2|y)\z/) include RDoc::Text + # :stopdoc: + BOOL_ARG_PATTERN = /\s*+\b([01]|Q?(?:true|false)|TRUE|FALSE)\b\s*/ + TRUE_VALUES = ['1', 'TRUE', 'true', 'Qtrue'].freeze + # :startdoc: + ## # Maps C variable names to names of Ruby classes or modules attr_reader :classes @@ -257,22 +262,22 @@ def do_attrs @content.scan(/rb_attr\s*\( \s*(\w+), \s*([\w"()]+), - \s*([01]), - \s*([01]), - \s*\w+\);/xm) do |var_name, attr_name, read, write| + #{BOOL_ARG_PATTERN}, + #{BOOL_ARG_PATTERN}, + \s*\w+\);/xmo) do |var_name, attr_name, read, write| handle_attr var_name, attr_name, read, write end @content.scan(%r%rb_define_attr\( \s*([\w\.]+), \s*"([^"]+)", - \s*(\d+), - \s*(\d+)\s*\); - %xm) do |var_name, attr_name, read, write| + #{BOOL_ARG_PATTERN}, + #{BOOL_ARG_PATTERN}\); + %xmo) do |var_name, attr_name, read, write| handle_attr var_name, attr_name, read, write end end ## @@ -293,98 +298,96 @@ def do_classes_and_modules do_boot_defclass if @file_name == "class.c" @content.scan( %r( + (?<open>\s*\(\s*) {0} + (?<close>\s*\)\s*) {0} + (?<name>\s*"(?<class_name>\w+)") {0} + (?<parent>\s*(?: + (?<parent_name>[\w\*\s\(\)\.\->]+) | + rb_path2class\s*\(\s*"(?<path>[\w:]+)"\s*\) + )) {0} + (?<under>\w+) {0} + (?<var_name>[\w\.]+)\s* = \s*rb_(?: define_(?: - class(?: # rb_define_class(class_name_1, parent_name_1) - \s*\( - \s*"(?<class_name_1>\w+)", - \s*(?<parent_name_1>\w+)\s* - \) - | - _under\s*\( # rb_define_class_under(class_under, class_name2, parent_name2...) - \s* (?<class_under>\w+), - \s* "(?<class_name_2>\w+)", - \s* - (?: - (?<parent_name_2>[\w\*\s\(\)\.\->]+) | - rb_path2class\("(?<path>[\w:]+)"\) - ) + class(?: # rb_define_class(name, parent_name) + \(\s* + \g<name>, + \g<parent> \s*\) + | + _under\g<open> # rb_define_class_under(under, name, parent_name...) + \g<under>, + \g<name>, + \g<parent> + \g<close> ) | - module(?: # rb_define_module(module_name_1) - \s*\( - \s*"(?<module_name_1>\w+)"\s* - \) + (?<module>) + module(?: # rb_define_module(name) + \g<open> + \g<name> + \g<close> | - _under\s*\( # rb_define_module_under(module_under, module_name_2) - \s*(?<module_under>\w+), - \s*"(?<module_name_2>\w+)" - \s*\) + _under\g<open> # rb_define_module_under(under, name) + \g<under>, + \g<name> + \g<close> ) ) | - struct_define_without_accessor\s*\( # rb_struct_define_without_accessor(class_name_3, parent_name_3, ...) - \s*"(?<class_name_3>\w+)", - \s*(?<parent_name_3>\w+), - \s*\w+, # Allocation function - (?:\s*"\w+",)* # Attributes - \s*NULL - \) + (?<attributes>(?:\s*"\w+",)*\s*NULL\s*) {0} + struct_define(?: + \g<open> # rb_struct_define(name, ...) + \g<name>, + | + _under\g<open> # rb_struct_define_under(under, name, ...) + \g<under>, + \g<name>, + | + _without_accessor(?: + \g<open> # rb_struct_define_without_accessor(name, parent_name, ...) + | + _under\g<open> # rb_struct_define_without_accessor_under(under, name, parent_name, ...) + \g<under>, + ) + \g<name>, + \g<parent>, + \s*\w+, # Allocation function + ) + \g<attributes> + \g<close> | - singleton_class\s*\( # rb_singleton_class(target_class_name) - \s*(?<target_class_name>\w+) - \) + singleton_class\g<open> # rb_singleton_class(target_class_name) + (?<target_class_name>\w+) + \g<close> ) )mx ) do - class_name = $~[:class_name_1] - type = :class - if class_name - # rb_define_class(class_name_1, parent_name_1) - parent_name = $~[:parent_name_1] - #under = nil - else - class_name = $~[:class_name_2] - if class_name - # rb_define_class_under(class_under, class_name2, parent_name2...) - parent_name = $~[:parent_name_2] || $~[:path] - under = $~[:class_under] - else - class_name = $~[:class_name_3] - if class_name - # rb_struct_define_without_accessor(class_name_3, parent_name_3, ...) - parent_name = $~[:parent_name_3] - #under = nil - else - type = :module - class_name = $~[:module_name_1] - #parent_name = nil - if class_name - # rb_define_module(module_name_1) - #under = nil - else - class_name = $~[:module_name_2] - if class_name - # rb_define_module_under(module_under, module_name_1) - under = $~[:module_under] - else - # rb_singleton_class(target_class_name) - target_class_name = $~[:target_class_name] - handle_singleton $~[:var_name], target_class_name - next - end - end - end - end + if target_class_name = $~[:target_class_name] + # rb_singleton_class(target_class_name) + handle_singleton $~[:var_name], target_class_name + next end - handle_class_module($~[:var_name], type, class_name, parent_name, under) + var_name = $~[:var_name] + type = $~[:module] ? :module : :class + class_name = $~[:class_name] + parent_name = $~[:parent_name] || $~[:path] + under = $~[:under] + attributes = $~[:attributes] + + handle_class_module(var_name, type, class_name, parent_name, under) + if attributes and !parent_name # rb_struct_define *not* without_accessor + true_flag = 'Qtrue' + attributes.scan(/"\K\w+(?=")/) do |attr_name| + handle_attr var_name, attr_name, true_flag, true_flag + end + end end end ## # Scans #content for rb_define_variable, rb_define_readonly_variable, @@ -675,17 +678,18 @@ end ## # Finds a RDoc::NormalClass or RDoc::NormalModule for +raw_name+ - def find_class(raw_name, name) + def find_class(raw_name, name, base_name = nil) unless @classes[raw_name] if raw_name =~ /^rb_m/ container = @top_level.add_module RDoc::NormalModule, name else container = @top_level.add_class RDoc::NormalClass, name end + container.name = base_name if base_name container.record_location @top_level @classes[raw_name] = container end @classes[raw_name] @@ -722,11 +726,11 @@ if @content =~ %r% ((?>/\*.*?\*/\s+)) (static\s+)? void\s+ - Init_#{class_name}\s*(?:_\(\s*)?\(\s*(?:void\s*)?\)%xmi then + Init(?:VM)?_(?i:#{class_name})\s*(?:_\(\s*)?\(\s*(?:void\s*)?\)%xm then comment = $1.sub(%r%Document-(?:class|module):\s+#{class_name}%, '') elsif @content =~ %r%Document-(?:class|module):\s+#{class_name}\s*? (?:<\s+[:,\w]+)?\n((?>.*?\*/))%xm then comment = "/*\n#{$1}" elsif @content =~ %r%((?>/\*.*?\*/\s+)) @@ -822,12 +826,12 @@ # Creates a new RDoc::Attr +attr_name+ on class +var_name+ that is either # +read+, +write+ or both def handle_attr(var_name, attr_name, read, write) rw = '' - rw += 'R' if '1' == read - rw += 'W' if '1' == write + rw += 'R' if TRUE_VALUES.include?(read) + rw += 'W' if TRUE_VALUES.include?(write) class_name = @known_classes[var_name] return unless class_name @@ -919,11 +923,11 @@ def handle_constants(type, var_name, const_name, definition) class_name = @known_classes[var_name] return unless class_name - class_obj = find_class var_name, class_name + class_obj = find_class var_name, class_name, class_name[/::\K[^:]+\z/] unless class_obj then @options.warn 'Enclosing class or module %p is not known' % [const_name] return end @@ -1023,11 +1027,12 @@ meth_obj.params = if p_count < -1 then # -2 is Array '(*args)' elsif p_count == -1 then # argc, argv rb_scan_args body else - "(#{(1..p_count).map { |i| "p#{i}" }.join ', '})" + args = (1..p_count).map { |i| "p#{i}" } + "(#{args.join ', '})" end meth_obj.record_location @top_level @@ -1048,26 +1053,9 @@ def handle_singleton sclass_var, class_var class_name = @known_classes[class_var] @known_classes[sclass_var] = class_name @singleton_classes[sclass_var] = class_name - end - - ## - # Normalizes tabs in +body+ - - def handle_tab_width(body) - if /\t/ =~ body - tab_width = @options.tab_width - body.split(/\n/).map do |line| - 1 while line.gsub!(/\t+/) do - ' ' * (tab_width * $&.length - $`.length % tab_width) - end && $~ - line - end.join "\n" - else - body - end end ## # Loads the variable map with the given +name+ from the RDoc::Store, if # present.