1 //<script type="text/javascript">
3 XObject = imports.XObject.XObject;
5 SymbolSet = imports.SymbolSet.SymbolSet;
6 //Parser = imports.Parser.Parser;
7 DocComment = imports.DocComment.DocComment;
8 DocTag = imports.DocTag.DocTag;
11 @class Represents a symbol in the source code.
13 Symbol = XObject.define(
16 if (arguments.length) this.populate.apply(this, arguments);
28 switch (typeof(this[i])) {
46 this.defaultValue = "";
54 this.comment = { isUserComment: false };
55 //this.defaultValue = null;
62 this.inheritsFrom = [];
63 this.isa = "OBJECT"; // OBJECT//FUNCTION
65 this.isConstant = false;
66 this.isIgnored = false;
68 this.isNamespace = false;
69 this.isPrivate = false;
70 this.isStatic = false;
83 this.childClasses = [];
88 serialize : function() {
97 if (typeof this[keys[i]] == "function") continue;
98 out += " " +keys[i]+" => "+
100 (typeof(this[keys[i]]) != "object") ?
102 "[" +typeof(this[keys[i]])+"]"
106 return "\n{\n" + out + "}\n";
110 var clone = new Symbol();
111 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
112 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
119 //__defineSetter__("name",
120 setName : function(n) {
121 n = n.replace(/^_global_[.#-]/, "");
122 n = n.replace(/\.prototype\.?/g, '#');
123 n = n.replace(/#$/g, '');
125 this.name = n; // real!
128 //__defineGetter__("name",
129 getName : function() { return this._name; },
131 //__defineSetter__("params",
132 setParams :function(v) {
133 for (var i = 0, l = v.length; i < l; i++) {
134 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
135 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
136 this._params[i] = new DocTag(
137 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
140 this._params[i] = v[i];
143 this.params = this._params;
148 //__defineGetter__("params",
149 getParams : function() { return this._params; },
154 /** Object[] */ params,
156 /** DocComment */ comment
158 this.$args = arguments;
159 //println("Symbol created: " + isa + ":" + name);
161 this.alias = this.getName();
162 this.setParams(params);
163 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
164 this.comment = comment || new DocComment("");
165 this.srcFile = Symbol.srcFile;
169 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
173 //if (typeof PluginManager != "undefined") {
174 // PluginManager.run("onSymbol", this);
178 setTags : function() {
180 var authors = this.comment.getTag("author");
181 if (authors.length) {
182 this.author = authors.map(function($){return $.desc;}).join(", ");
186 assert("testing Symbol");
188 requires("../lib/JSDOC/DocComment.js");
189 requires("../frame/String.js");
190 requires("../lib/JSDOC/DocTag.js");
192 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
193 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
196 var mth = this.comment.getTag("method");
198 this.isa = "FUNCTION";
201 var descs = this.comment.getTag("desc");
203 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
207 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
208 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
212 if (this.is("FILE")) {
213 if (!this.alias) this.alias = this.srcFile;
215 var overviews = this.comment.getTag("overview");
216 if (overviews.length) {
217 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
222 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
223 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
227 var sinces = this.comment.getTag("since");
229 this.since = sinces.map(function($){return $.desc;}).join(", ");
233 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
234 assertEqual(sym.since, "1.01", "@since tag, description is found.");
238 if (this.comment.getTag("constant").length) {
239 this.isConstant = true;
244 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
245 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
249 var versions = this.comment.getTag("version");
250 if (versions.length) {
251 this.version = versions.map(function($){return $.desc;}).join(", ");
255 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
256 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
260 var deprecateds = this.comment.getTag("deprecated");
261 if (deprecateds.length) {
262 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
266 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
267 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
271 var examples = this.comment.getTag("example");
272 if (examples.length) {
273 this.example = examples[0];
277 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
278 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
282 var sees = this.comment.getTag("see");
284 var thisSee = this.see;
285 sees.map(function($){thisSee.push($.desc);});
289 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
290 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
294 var classes = this.comment.getTag("class");
295 if (classes.length) {
296 this.isa = "CONSTRUCTOR";
297 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
298 if (!this.classDesc) {
299 this.classDesc = this.desc;
306 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
307 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
308 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
312 var namespaces = this.comment.getTag("namespace");
313 if (namespaces.length) {
314 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
315 this.isNamespace = true;
319 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
320 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
324 var params = this.comment.getTag("param");
326 // user-defined params overwrite those with same name defined by the parser
327 var thisParams = this.getParams();
329 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
330 this.setParams(params);
332 else { // need to overlay these user-defined params on to existing parser-defined params
333 for (var i = 0, l = params.length; i < l; i++) {
335 if (params[i].type) thisParams[i].type = params[i].type;
336 thisParams[i].name = params[i].name;
337 thisParams[i].desc = params[i].desc;
338 thisParams[i].isOptional = params[i].isOptional;
339 thisParams[i].defaultValue = params[i].defaultValue;
341 else thisParams[i] = params[i];
347 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
348 assertEqual(sym.params.length, 1, "parser defined param is found.");
350 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
351 assertEqual(sym.params.length, 1, "user defined param is found.");
352 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
353 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
355 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
356 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
357 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
358 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
360 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
361 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
362 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
363 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
365 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
366 assertEqual(sym.params.length, 1, "user defined param with description is found.");
367 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
371 if (this.comment.getTag("constructor").length) {
372 this.isa = "CONSTRUCTOR";
376 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
377 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
381 if (this.comment.getTag("static").length) {
382 this.isStatic = true;
383 if (this.isa == "CONSTRUCTOR") {
384 this.isNamespace = true;
389 if (this.comment.getTag("singleton").length) {
390 this.isStatic = true;
391 //if (this.isa == "CONSTRUCTOR") {
392 // this.isNamespace = true;
399 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
400 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
401 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
405 if (this.comment.getTag("inner").length) {
407 this.isStatic = false;
411 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
412 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
413 assertEqual(sym.isInner, true, "@inner makes isInner true.");
417 if (this.comment.getTag("field").length) {
422 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
423 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
427 if (this.comment.getTag("function").length) {
428 this.isa = "FUNCTION";
432 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
433 // change a property to a function..
434 this.isa = "FUNCTION";
439 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
440 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
444 var events = this.comment.getTag("event");
446 this.isa = "FUNCTION";
451 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
452 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
453 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
457 var names = this.comment.getTag("name");
459 this.setName(names[0].desc);
467 var properties = this.comment.getTag("property");
468 if (properties.length) {
469 thisProperties = this.properties;
470 for (var i = 0; i < properties.length; i++) {
471 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
472 // TODO: shouldn't the following happen in the addProperty method of Symbol?
473 property.name = properties[i].name;
474 property.memberOf = this.alias;
475 if (properties[i].type) property.type = properties[i].type;
476 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
477 this.addProperty(property);
478 imports.Parser.Parser.addSymbol(property);
483 var conf = this.comment.getTag("cfg");
485 for (var i = 0; i < conf.length; i++) {
486 this.addConfig(conf[i]);
495 var returns = this.comment.getTag("return");
496 if (returns.length) { // there can be many return tags in a single doclet
497 this.returns = returns;
498 this.type = returns.map(function($){return $.type}).join(", ");
506 this.exceptions = this.comment.getTag("throws");
513 var requires = this.comment.getTag("requires");
514 if (requires.length) {
515 this.requires = requires.map(function($){return $.desc});
523 var types = this.comment.getTag("type");
525 this.type = types[0].desc; //multiple type tags are ignored
533 if (this.comment.getTag("private").length || this.isInner) {
534 this.isPrivate = true;
538 if (this.comment.getTag("ignore").length) {
539 this.isIgnored = true;
546 // @inherits ... as ...
547 var inherits = this.comment.getTag("inherits");
548 if (inherits.length) {
549 for (var i = 0; i < inherits.length; i++) {
550 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
551 var inAlias = RegExp.$1;
552 var inAs = RegExp.$2 || inAlias;
554 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
557 inAs = inAs.replace(/\.prototype\.?/g, "#");
558 inAs = inAs.replace(/^this\.?/, "#");
561 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
563 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
566 inAs = this.alias + joiner + inAs;
569 this.inherits.push({alias: inAlias, as: inAs});
578 this.augments = this.comment.getTag("augments");
581 if (this.comment.getTag("extends")) {
582 this.augments = this.comment.getTag("extends");
587 var defaults = this.comment.getTag("default");
588 if (defaults.length) {
589 if (this.is("OBJECT")) {
590 this.defaultValue = defaults[0].desc;
599 var memberOfs = this.comment.getTag("memberOf");
600 if (memberOfs.length) {
601 this.memberOf = memberOfs[0].desc;
602 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
610 if (this.comment.getTag("public").length) {
611 this.isPrivate = false;
619 is : function(what) {
620 return this.isa === what;
623 isBuiltin : function() {
624 return SymbolSet.isBuiltin(this.alias);
627 setType : function(/**String*/comment, /**Boolean*/overwrite) {
628 if (!overwrite && this.type) return;
629 var typeComment = DocComment.unwrapComment(comment);
630 this.type = typeComment;
633 inherit : function(symbol) {
634 if (!this.hasMember(symbol.name) && !symbol.isInner) {
635 if (symbol.is("FUNCTION"))
636 this.methods.push(symbol);
637 else if (symbol.is("OBJECT"))
638 this.properties.push(symbol);
642 hasMember : function(name) {
643 return (this.hasMethod(name) || this.hasProperty(name));
646 addMember : function(symbol) {
647 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
649 if (symbol.comment.getTag("cfg").length == 1) {
650 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
651 this.addConfig(symbol.comment.getTag("cfg")[0]);
655 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
656 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
659 hasMethod : function(name) {
660 var thisMethods = this.methods;
661 for (var i = 0, l = thisMethods.length; i < l; i++) {
662 if (thisMethods[i].name == name) return true;
663 if (thisMethods[i].alias == name) return true;
668 addMethod : function(symbol) {
669 var methodAlias = symbol.alias;
670 var thisMethods = this.methods;
671 for (var i = 0, l = thisMethods.length; i < l; i++) {
672 if (thisMethods[i].alias == methodAlias) {
673 thisMethods[i] = symbol; // overwriting previous method
677 thisMethods.push(symbol); // new method with this alias
680 hasProperty : function(name) {
681 var thisProperties = this.properties;
682 for (var i = 0, l = thisProperties.length; i < l; i++) {
683 if (thisProperties[i].name == name) return true;
684 if (thisProperties[i].alias == name) return true;
689 addProperty : function(symbol) {
690 var propertyAlias = symbol.alias;
691 var thisProperties = this.properties;
692 for (var i = 0, l = thisProperties.length; i < l; i++) {
693 if (thisProperties[i].alias == propertyAlias) {
694 thisProperties[i] = symbol; // overwriting previous property
699 thisProperties.push(symbol); // new property with this alias
702 addDocTag : function(docTag)
704 this.comment.tags.push(docTag);
705 if (docTag.title == 'cfg') {
706 this.addConfig(docTag);
711 addConfig : function(docTag)
713 if (typeof(docTag['memberOf']) == 'undefined') {
714 // remove prototype data...
715 //var a = this.alias.split('#')[0];
716 //docTag.memberOf = a;
717 docTag.memberOf = this.alias;
719 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
720 this.cfgs[docTag.name] = docTag;
724 configToArray: function()
727 for(var ci in this.cfgs) {
728 // dont show hidden!!
729 if (this.cfgs[ci].desc.match(/@hide/)) {
732 r.push(this.cfgs[ci]);
743 Symbol.srcFile = ""; //running reference to the current file being parsed
746 Symbol.fromDump = function(t)
748 var ns = new Symbol();
750 if (typeof(ns[i]) == "undefined") {
751 println("ERR:no default for Symbol:"+ i);