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);
26 $args : [], // original arguments used when constructing.
32 comment : { isUserComment: false },
40 isa : "OBJECT", // OBJECT//FUNCTION
69 function array2object(a , m)
73 a.forEach(function(e) {
74 r.push(m ? e : e.toObject());
80 if (Symbol.hide.indexOf[i] > -1) {
83 switch (typeof(this[i])) {
88 print("object? :" + i);
92 ret[i] = this[i]; continue;
94 print("unknown type:" + typeof(this[i]));
105 this.defaultValue = "";
113 this.comment = { isUserComment: false };
114 //this.defaultValue = null;
115 this.deprecated = "";
119 this.exceptions = [];
121 this.inheritsFrom = [];
122 this.isa = "OBJECT"; // OBJECT//FUNCTION
123 this.isEvent = false;
124 this.isConstant = false;
125 this.isIgnored = false;
126 this.isInner = false;
127 this.isNamespace = false;
128 this.isPrivate = false;
129 this.isStatic = false;
134 this.properties = [];
142 this.childClasses = [];
147 serialize : function() {
149 for (var p in this) {
155 for (var i in keys) {
156 if (typeof this[keys[i]] == "function") continue;
157 out += " " +keys[i]+" => "+
159 (typeof(this[keys[i]]) != "object") ?
161 "[" +typeof(this[keys[i]])+"]"
165 return "\n{\n" + out + "}\n";
169 var clone = new Symbol();
170 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
171 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
178 //__defineSetter__("name",
179 setName : function(n) {
180 n = n.replace(/^_global_[.#-]/, "");
181 n = n.replace(/\.prototype\.?/g, '#');
182 n = n.replace(/#$/g, '');
184 this.name = n; // real!
187 //__defineGetter__("name",
188 getName : function() { return this._name; },
190 //__defineSetter__("params",
191 setParams :function(v) {
192 for (var i = 0, l = v.length; i < l; i++) {
193 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
194 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
195 this._params[i] = new DocTag(
196 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
199 this._params[i] = v[i];
202 this.params = this._params;
207 //__defineGetter__("params",
208 getParams : function() { return this._params; },
213 /** Object[] */ params,
215 /** DocComment */ comment
217 this.$args = arguments;
218 //println("Symbol created: " + isa + ":" + name);
220 this.alias = this.getName();
221 this.setParams(params);
222 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
223 this.comment = comment || new DocComment("");
224 this.srcFile = Symbol.srcFile;
228 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
232 //if (typeof PluginManager != "undefined") {
233 // PluginManager.run("onSymbol", this);
237 setTags : function() {
239 var authors = this.comment.getTag("author");
240 if (authors.length) {
241 this.author = authors.map(function($){return $.desc;}).join(", ");
245 assert("testing Symbol");
247 requires("../lib/JSDOC/DocComment.js");
248 requires("../frame/String.js");
249 requires("../lib/JSDOC/DocTag.js");
251 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
252 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
255 var mth = this.comment.getTag("method");
257 this.isa = "FUNCTION";
260 var descs = this.comment.getTag("desc");
262 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
266 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
267 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
271 if (this.is("FILE")) {
272 if (!this.alias) this.alias = this.srcFile;
274 var overviews = this.comment.getTag("overview");
275 if (overviews.length) {
276 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
281 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
282 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
286 var sinces = this.comment.getTag("since");
288 this.since = sinces.map(function($){return $.desc;}).join(", ");
292 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
293 assertEqual(sym.since, "1.01", "@since tag, description is found.");
297 if (this.comment.getTag("constant").length) {
298 this.isConstant = true;
303 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
304 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
308 var versions = this.comment.getTag("version");
309 if (versions.length) {
310 this.version = versions.map(function($){return $.desc;}).join(", ");
314 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
315 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
319 var deprecateds = this.comment.getTag("deprecated");
320 if (deprecateds.length) {
321 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
325 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
326 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
330 var examples = this.comment.getTag("example");
331 if (examples.length) {
332 this.example = examples[0];
336 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
337 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
341 var sees = this.comment.getTag("see");
343 var thisSee = this.see;
344 sees.map(function($){thisSee.push($.desc);});
348 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
349 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
353 var classes = this.comment.getTag("class");
354 if (classes.length) {
355 this.isa = "CONSTRUCTOR";
356 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
357 if (!this.classDesc) {
358 this.classDesc = this.desc;
365 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
366 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
367 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
371 var namespaces = this.comment.getTag("namespace");
372 if (namespaces.length) {
373 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
374 this.isNamespace = true;
378 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
379 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
383 var params = this.comment.getTag("param");
385 // user-defined params overwrite those with same name defined by the parser
386 var thisParams = this.getParams();
388 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
389 this.setParams(params);
391 else { // need to overlay these user-defined params on to existing parser-defined params
392 for (var i = 0, l = params.length; i < l; i++) {
394 if (params[i].type) thisParams[i].type = params[i].type;
395 thisParams[i].name = params[i].name;
396 thisParams[i].desc = params[i].desc;
397 thisParams[i].isOptional = params[i].isOptional;
398 thisParams[i].defaultValue = params[i].defaultValue;
400 else thisParams[i] = params[i];
406 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
407 assertEqual(sym.params.length, 1, "parser defined param is found.");
409 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
410 assertEqual(sym.params.length, 1, "user defined param is found.");
411 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
412 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
414 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
415 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
416 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
417 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
419 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
420 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
421 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
422 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
424 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
425 assertEqual(sym.params.length, 1, "user defined param with description is found.");
426 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
430 if (this.comment.getTag("constructor").length) {
431 this.isa = "CONSTRUCTOR";
435 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
436 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
440 if (this.comment.getTag("static").length) {
441 this.isStatic = true;
442 if (this.isa == "CONSTRUCTOR") {
443 this.isNamespace = true;
448 if (this.comment.getTag("singleton").length) {
449 this.isStatic = true;
450 //if (this.isa == "CONSTRUCTOR") {
451 // this.isNamespace = true;
458 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
459 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
460 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
464 if (this.comment.getTag("inner").length) {
466 this.isStatic = false;
470 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
471 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
472 assertEqual(sym.isInner, true, "@inner makes isInner true.");
476 if (this.comment.getTag("field").length) {
481 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
482 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
486 if (this.comment.getTag("function").length) {
487 this.isa = "FUNCTION";
491 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
492 // change a property to a function..
493 this.isa = "FUNCTION";
498 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
499 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
503 var events = this.comment.getTag("event");
505 this.isa = "FUNCTION";
510 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
511 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
512 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
516 var names = this.comment.getTag("name");
518 this.setName(names[0].desc);
526 var properties = this.comment.getTag("property");
527 if (properties.length) {
528 thisProperties = this.properties;
529 for (var i = 0; i < properties.length; i++) {
530 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
531 // TODO: shouldn't the following happen in the addProperty method of Symbol?
532 property.name = properties[i].name;
533 property.memberOf = this.alias;
534 if (properties[i].type) property.type = properties[i].type;
535 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
536 this.addProperty(property);
537 imports.Parser.Parser.addSymbol(property);
542 var conf = this.comment.getTag("cfg");
544 for (var i = 0; i < conf.length; i++) {
545 this.addConfig(conf[i]);
554 var returns = this.comment.getTag("return");
555 if (returns.length) { // there can be many return tags in a single doclet
556 this.returns = returns;
557 this.type = returns.map(function($){return $.type}).join(", ");
565 this.exceptions = this.comment.getTag("throws");
572 var requires = this.comment.getTag("requires");
573 if (requires.length) {
574 this.requires = requires.map(function($){return $.desc});
582 var types = this.comment.getTag("type");
584 this.type = types[0].desc; //multiple type tags are ignored
592 if (this.comment.getTag("private").length || this.isInner) {
593 this.isPrivate = true;
597 if (this.comment.getTag("ignore").length) {
598 this.isIgnored = true;
605 // @inherits ... as ...
606 var inherits = this.comment.getTag("inherits");
607 if (inherits.length) {
608 for (var i = 0; i < inherits.length; i++) {
609 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
610 var inAlias = RegExp.$1;
611 var inAs = RegExp.$2 || inAlias;
613 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
616 inAs = inAs.replace(/\.prototype\.?/g, "#");
617 inAs = inAs.replace(/^this\.?/, "#");
620 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
622 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
625 inAs = this.alias + joiner + inAs;
628 this.inherits.push({alias: inAlias, as: inAs});
637 this.augments = this.comment.getTag("augments");
640 if (this.comment.getTag("extends")) {
641 this.augments = this.comment.getTag("extends");
646 var defaults = this.comment.getTag("default");
647 if (defaults.length) {
648 if (this.is("OBJECT")) {
649 this.defaultValue = defaults[0].desc;
658 var memberOfs = this.comment.getTag("memberOf");
659 if (memberOfs.length) {
660 this.memberOf = memberOfs[0].desc;
661 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
669 if (this.comment.getTag("public").length) {
670 this.isPrivate = false;
678 is : function(what) {
679 return this.isa === what;
682 isBuiltin : function() {
683 return SymbolSet.isBuiltin(this.alias);
686 setType : function(/**String*/comment, /**Boolean*/overwrite) {
687 if (!overwrite && this.type) return;
688 var typeComment = DocComment.unwrapComment(comment);
689 this.type = typeComment;
692 inherit : function(symbol) {
693 if (!this.hasMember(symbol.name) && !symbol.isInner) {
694 if (symbol.is("FUNCTION"))
695 this.methods.push(symbol);
696 else if (symbol.is("OBJECT"))
697 this.properties.push(symbol);
701 hasMember : function(name) {
702 return (this.hasMethod(name) || this.hasProperty(name));
705 addMember : function(symbol) {
706 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
708 if (symbol.comment.getTag("cfg").length == 1) {
709 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
710 this.addConfig(symbol.comment.getTag("cfg")[0]);
714 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
715 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
718 hasMethod : function(name) {
719 var thisMethods = this.methods;
720 for (var i = 0, l = thisMethods.length; i < l; i++) {
721 if (thisMethods[i].name == name) return true;
722 if (thisMethods[i].alias == name) return true;
727 addMethod : function(symbol) {
728 var methodAlias = symbol.alias;
729 var thisMethods = this.methods;
730 for (var i = 0, l = thisMethods.length; i < l; i++) {
731 if (thisMethods[i].alias == methodAlias) {
732 thisMethods[i] = symbol; // overwriting previous method
736 thisMethods.push(symbol); // new method with this alias
739 hasProperty : function(name) {
740 var thisProperties = this.properties;
741 for (var i = 0, l = thisProperties.length; i < l; i++) {
742 if (thisProperties[i].name == name) return true;
743 if (thisProperties[i].alias == name) return true;
748 addProperty : function(symbol) {
749 var propertyAlias = symbol.alias;
750 var thisProperties = this.properties;
751 for (var i = 0, l = thisProperties.length; i < l; i++) {
752 if (thisProperties[i].alias == propertyAlias) {
753 thisProperties[i] = symbol; // overwriting previous property
758 thisProperties.push(symbol); // new property with this alias
761 addDocTag : function(docTag)
763 this.comment.tags.push(docTag);
764 if (docTag.title == 'cfg') {
765 this.addConfig(docTag);
770 addConfig : function(docTag)
772 if (typeof(docTag['memberOf']) == 'undefined') {
773 // remove prototype data...
774 //var a = this.alias.split('#')[0];
775 //docTag.memberOf = a;
776 docTag.memberOf = this.alias;
778 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
779 this.cfgs[docTag.name] = docTag;
783 configToArray: function()
786 for(var ci in this.cfgs) {
787 // dont show hidden!!
788 if (this.cfgs[ci].desc.match(/@hide/)) {
791 r.push(this.cfgs[ci]);
799 * Elements that are not serialized
803 '$args' // not needed AFAIK
806 Symbol.srcFile = ""; //running reference to the current file being parsed
809 Symbol.fromDump = function(t)
811 var ns = new Symbol();
813 if (typeof(ns[i]) == "undefined") {
814 println("ERR:no default for Symbol:"+ i);