1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
require 'yaml'
require 'rdoc/markup/simple_markup/fragments'
# BEGIN NETBEANS MODIFICATIONS
require 'set'
# END NETBEANS MODIFICATIONS
# Descriptions are created by RDoc (in nb_generator) and
# written out in serialized form into the documentation
# tree. ri then reads these to generate the documentation
module NB
class NamedThing
attr_reader :name
def initialize(name)
@name = name
end
def <=>(other)
@name <=> other.name
end
def hash
@name.hash
end
def eql?(other)
@name.eql?(other)
end
end
# Alias = Struct.new(:old_name, :new_name)
class AliasName < NamedThing
end
class Attribute < NamedThing
attr_reader :rw, :comment
# BEGIN NETBEANS MODIFICATIONS
attr_reader :rdoccomment
# END NETBEANS MODIFICATIONS
# BEGIN NETBEANS MODIFICATIONS
#def initialize(name, rw, comment)
def initialize(name, rw, comment, rdoccomment)
# END NETBEANS MODIFICATIONS
super(name)
@rw = rw
@comment = comment
# BEGIN NETBEANS MODIFICATIONS
@rdoccomment = rdoccomment
# END NETBEANS MODIFICATIONS
end
end
class Constant < NamedThing
attr_reader :value, :comment
# BEGIN NETBEANS MODIFICATIONS
attr_reader :rdoccomment
# END NETBEANS MODIFICATIONS
# BEGIN NETBEANS MODIFICATIONS
#def initialize(name, value, comment)
def initialize(name, value, comment, rdoccomment)
# END NETBEANS MODIFICATIONS
super(name)
@value = value
@comment = comment
# BEGIN NETBEANS MODIFICATIONS
@rdoccomment = rdoccomment
# END NETBEANS MODIFICATIONS
end
end
class IncludedModule < NamedThing
end
# BEGIN NETBEANS MODIFICATIONS
class Requires < NamedThing
end
class InFile < NamedThing
end
# END NETBEANS MODIFICATIONS
class MethodSummary < NamedThing
def initialize(name="")
super
end
end
class Description
attr_accessor :name
attr_accessor :full_name
attr_accessor :comment
def serialize
self.to_yaml
end
def Description.deserialize(from)
YAML.load(from)
end
def <=>(other)
@name <=> other.name
end
def append_comment(rdoccomment, s)
if rdoccomment != nil
rdoccomment.each_line { |line|
if !(/^#/ =~ line)
# C comments seem to have 3 or 4 spaces too many
if /^ / =~ line
#line = line.gsub(/^ /, "#")
# TODO: How do I strip off 4?
line = "#" + line[3, line.length-3]
#line = "# " + line
elsif /^ / =~ line
#line = line.gsub(/^ /, "#")
# TODO: How do I strip off 4?
line = "#" + line[2, line.length-2]
#line = "# " + line
else
line = "# " + line
end
end
s << line
}
# Ensure the comment doesn't disable the next output
if (!(/\n$/ =~ s))
s << "\n"
end
end
end
end
class ModuleDescription < Description
attr_accessor :class_methods
attr_accessor :instance_methods
attr_accessor :attributes
attr_accessor :constants
attr_accessor :includes
# BEGIN NETBEANS MODIFICATIONS
attr_accessor :in_files
attr_accessor :requires
attr_accessor :rdoccomment
def codename
"module"
end
def extend
nil
end
# END NETBEANS MODIFICATIONS
# merge in another class desscription into this one
def merge_in(old)
merge(@class_methods, old.class_methods)
merge(@instance_methods, old.instance_methods)
merge(@attributes, old.attributes)
merge(@constants, old.constants)
merge(@includes, old.includes)
if @comment.nil? || @comment.empty?
@comment = old.comment
else
unless old.comment.nil? or old.comment.empty? then
@comment << SM::Flow::RULE.new
@comment.concat old.comment
end
end
end
def display_name
"Module"
end
# the 'ClassDescription' subclass overrides this
# to format up the name of a parent
def superclass_string
nil
end
# BEGIN NETBEANS MODIFICATIONS
# Preferred filename
def filename
only_ext = true
relative = nil
if (@in_files != nil)
@in_files.each { |file|
re = /ext\/(.*)+\/([^)]+)\.c/
md = re.match(file.name)
if md == nil
only_ext = false
else
relative = md[1]
end
}
if only_ext
return relative
end
end
nil
end
def stubify
s = ""
# if (@in_files != nil)
# s << "# Defined in: \n"
# @in_files.each { |file|
# s << "# "
# s << file.name
# s << "\n"
# }
# end
if (@requires != nil)
@requires.each { |require|
s << "require '"
s << require.name
s << "'\n"
}
end
append_comment(@rdoccomment, s)
s << codename
s << " "
s << @name
extendCode = extend()
if (extendCode != nil)
s << extendCode
end
s << "\n"
if (@includes != nil)
@includes.each { |include|
s << " include "
s << include.name
s << "\n"
}
end
if (@attributes != nil)
@attributes.each { |attribute|
accessor = case attribute.rw
when "RW" then "attr_accessor"
when "R" then "attr_reader"
when "W" then "attr_writer"
else "#unknown attribute "
end
s << " "
s << accessor
s << " :"
s << attribute.name
s << "\n"
}
end
if (@constants != nil)
@constants.each { |constant|
s << " "
key = constant.name
# Some keys aren't compilable - rename these
if (key == "END")
key = "END_"
end
s << key
s << " = "
if (constant.value != nil)
value = constant.value
# Some values aren't compilable - turn those into strings
if /\(/ =~ value
value = "'" + value + "'"
end
s << value
else
s << "unknown"
end
s << "\n"
}
end
# TODO - class methods and instance methods?
# These seem to be empty even for classes that have them
# if (@class_methods != nil)
# @class_methods.each { |method|
# s << "\n"
# s << method.stuify
# s << "\n"
# }
# end
#
# if (@instance_methods != nil)
# @instance_methods.each { |method|
# s << "\n"
# s << method.stuify
# s << "\n"
# }
# end
return s
end
# END NETBEANS MODIFICATIONS
private
def merge(into, from)
names = {}
into.each {|i| names[i.name] = i }
from.each {|i| names[i.name] = i }
into.replace(names.keys.sort.map {|n| names[n]})
end
end
class ClassDescription < ModuleDescription
attr_accessor :superclass
# BEGIN NETBEANS MODIFICATIONS
# TODO - emit stuff from parent too
attr_accessor :rdoccomment
def extend
s = superclass_string()
if (s != nil)
" < " + s
else
nil
end
end
def codename
"class"
end
# END NETBEANS MODIFICATIONS
def display_name
"Class"
end
def superclass_string
if @superclass && @superclass != "Object"
@superclass
else
nil
end
end
end
class MethodDescription < Description
attr_accessor :is_class_method
attr_accessor :visibility
attr_accessor :block_params
attr_accessor :is_singleton
attr_accessor :aliases
attr_accessor :is_alias_for
attr_accessor :params
# BEGIN NETBEANS MODIFICATIONS
attr_accessor :call_seq
attr_accessor :rdoccomment
# END NETBEANS MODIFICATIONS
# BEGIN NETBEANS MODIFICATIONS
def stubify
#TODO @aliases, @is_alias_for
#TODO @is_class_method
#TODO @block_params
# @aliases is always empty - rdoc seems to duplicate the method instead
# and set its documentation to "# Alias for _name_"
methods = Set.new
if (@call_seq != nil)
# File.fnmatch(arg1, arg2) => foo -> fnmatch, (arg1, arg2)
# but also don't freak out on "def shift -> (key, value)"
re = /([^(.:=-]+)(\([^)]+\))(\s*(=>|->).*)?/
args = nil
@call_seq.each_line { |line|
# Handle special case
if (line =~ /gdbm.shift/)
methodname = "shift"
args = ""
signature = "shift"
else
md = re.match(line)
if md != nil
methodname = md[1]
args = argify(md[2])
signature = methodname + args
if !signature.eql?('set_trace_func(nil)') # Skip this method, it's just doc on nil behavior
methods.add(signature)
end
end
end
}
if methods.empty?
# Perhaps the signature doesn't include parens, for example
# mtch.postmtch => result
# Just use @name for this - works best for operators (===) and such
methods.add(@name)
end
else
signature = @name
if @params == nil
signature = @name
elsif (@params.eql?('(...)'))
# It's a method defined in native code that didn't include
# a call-seq. That makes it an unknown - we'll just use *args
signature = @name+"(*args)"
elsif !@params.eql?("()")
signature = signature + @params
end
methods.add(signature)
end
s = ""
methods.each { |signature|
if @call_seq != nil
@call_seq.each_line { |line|
if !(/^#/ =~ line)
line = "# " + line
end
s << line
}
end
append_comment(@rdoccomment, s)
if (@access != nil &&
!@access.eql?("public"))
s << @access + "\n"
end
s << "def "
if @is_singleton
s << "self."
end
s << signature
s << "\n"
s << <<FOO
# This is just a stub for a builtin Ruby method.
# See the top of this file for more info.
FOO
s << "end\n\n"
}
return s
end
#:arg:argList=>String
def argify(argList)
# Clean up meta-syntax for arguments
# e.g. initialize(string [, options [, lang]])
# should be initialize(string, options, lang)
# e.g. union([pattern]*)
# should be union(pattern), etc.]
# e.g. Struct.new( [aString] [, aSym]+> )
# shoudl be Struct.new( aString, aSym )
# def chomp!(separator=$/)
# should be
# def split(pattern=$;,limit) ?
# delete!(other_str>)
# should be
# TODO: Handle " def []=( i1, i2, ... iN )"
#
# Note that this "cleanup" will create signatures
# that do not reflect the original intent of the methods!
# I need to find better ways of writing these things,
# perhaps into a better code completion model aware of
# such conventions
#
# Known special case in string.c:
if argList.eql?("(pattern=$;, [limit])")
# Can't just do the normal substitution here
# because I end up with
# (pattern="$;", limit)
# and that is illegal - you cannot have a default
# (for pattern) followed by a nondefault argument
argList = "(pattern)";
end
if argList.eql?('(string=""[, mode])')
return '(string="", mode=3)' # FMODE_READWRITE=3, the default
end
if argList.eql?('(obj, aProc=proc()')
# The regexp doesn't include the last )
return argList+")"
end
# Missing quotes
argList = argList.gsub("=$/", "=\"$/\"");
argList = argList.gsub("=$;", "=\"$;\"");
argList = argList.gsub("=$,", "=\"$,\"");
argList = argList.gsub("=$>", "=\"$>\"");
# Prevent keywords in argument list
#if (argList.eql?("(module, ...)")) {
# argList = "(modules)";
#} else
if (/\(module, / =~ argList)
argList = argList.gsub("(module, ", "(module_, ");
end
argList = argList.gsub(", end,", ", endd,"); # yuck
argList = argList.gsub("]+>", "");
argList = argList.gsub("]*", "");
argList = argList.gsub("]+", "");
argList = argList.gsub("]", "");
argList = argList.gsub(" [", "");
argList = argList.gsub("[", "");
# TODO: ... probably means further args - put in an *args here?
argList = argList.gsub("...", "");
argList = argList.gsub("..", ""); #probably typo in original source
# Compress spaces
argList = argList.gsub(" ", " ");
# Avoid keyword conflicts
if (argList.eql?("(module)"))
argList = "(module_name)";
elsif (argList.eql?("(class)"))
argList = "(class_name)";
end
argList = argList.gsub(", )", ")");
argList = argList.gsub(",)", ")");
argList = argList.gsub(">)", ")");
argList = argList.gsub("//)", ")");
argList = argList.gsub("()", "");
# Some other known problems: Duplicate parameter names
argList = argList.gsub("fixnum, fixnum", "fixnum1, fixnum2");
return argList;
end
# END NETBEANS MODIFICATIONS
end
end
|