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.
29 augments : false, // Doctag
40 isa : "OBJECT", // OBJECT//FUNCTION
69 function array2object(a)
73 a.forEach(function(e) {
81 if (Symbol.hide.indexOf(i) > -1) {
84 switch (typeof(this[i])) {
91 ret[i] = array2object(this[i],true);
97 print(JSON.stringify(this[i]));
98 ret[i] = this[i].toObject();
102 print("object? :" + i);
109 ret[i] = this[i]; continue;
111 print("unknown type:" + typeof(this[i]));
121 // only initialize arrays / objects..
126 this.exceptions = [];
128 this.inheritsFrom = [];
129 this.isa = "OBJECT"; // OBJECT//FUNCTION
132 this.properties = [];
137 this.childClasses = [];
140 this.comment = new DocComment();
141 this.comment.isUserComment = false;
146 serialize : function() {
148 for (var p in this) {
154 for (var i in keys) {
155 if (typeof this[keys[i]] == "function") continue;
156 out += " " +keys[i]+" => "+
158 (typeof(this[keys[i]]) != "object") ?
160 "[" +typeof(this[keys[i]])+"]"
164 return "\n{\n" + out + "}\n";
168 var clone = new Symbol();
169 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
170 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
177 //__defineSetter__("name",
178 setName : function(n) {
179 n = n.replace(/^_global_[.#-]/, "");
180 n = n.replace(/\.prototype\.?/g, '#');
181 n = n.replace(/#$/g, '');
183 this.name = n; // real!
186 //__defineGetter__("name",
187 getName : function() { return this._name; },
189 //__defineSetter__("params",
190 setParams :function(v) {
191 for (var i = 0, l = v.length; i < l; i++) {
192 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
193 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
194 this._params[i] = new DocTag(
195 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
198 this._params[i] = v[i];
201 this.params = this._params;
206 //__defineGetter__("params",
207 getParams : function() { return this._params; },
212 /** Object[] */ params,
214 /** DocComment */ comment
216 this.$args = arguments;
217 //println("Symbol created: " + isa + ":" + name);
219 this.alias = this.getName();
220 this.setParams(params);
221 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
222 this.comment = comment || new DocComment("");
223 this.srcFile = Symbol.srcFile;
227 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
231 //if (typeof PluginManager != "undefined") {
232 // PluginManager.run("onSymbol", this);
236 setTags : function() {
238 var authors = this.comment.getTag("author");
239 if (authors.length) {
240 this.author = authors.map(function($){return $.desc;}).join(", ");
244 assert("testing Symbol");
246 requires("../lib/JSDOC/DocComment.js");
247 requires("../frame/String.js");
248 requires("../lib/JSDOC/DocTag.js");
250 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
251 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
254 var mth = this.comment.getTag("method");
256 this.isa = "FUNCTION";
259 var descs = this.comment.getTag("desc");
261 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
265 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
266 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
270 if (this.is("FILE")) {
271 if (!this.alias) this.alias = this.srcFile;
273 var overviews = this.comment.getTag("overview");
274 if (overviews.length) {
275 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
280 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
281 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
285 var sinces = this.comment.getTag("since");
287 this.since = sinces.map(function($){return $.desc;}).join(", ");
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
292 assertEqual(sym.since, "1.01", "@since tag, description is found.");
296 if (this.comment.getTag("constant").length) {
297 this.isConstant = true;
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
303 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
307 var versions = this.comment.getTag("version");
308 if (versions.length) {
309 this.version = versions.map(function($){return $.desc;}).join(", ");
313 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
314 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
318 var deprecateds = this.comment.getTag("deprecated");
319 if (deprecateds.length) {
320 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
324 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
325 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
329 var examples = this.comment.getTag("example");
330 if (examples.length) {
331 this.example = examples[0];
335 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
336 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
340 var sees = this.comment.getTag("see");
342 var thisSee = this.see;
343 sees.map(function($){thisSee.push($.desc);});
347 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
348 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
352 var classes = this.comment.getTag("class");
353 if (classes.length) {
354 this.isa = "CONSTRUCTOR";
355 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
356 if (!this.classDesc) {
357 this.classDesc = this.desc;
364 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
365 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
366 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
370 var namespaces = this.comment.getTag("namespace");
371 if (namespaces.length) {
372 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
373 this.isNamespace = true;
377 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
378 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
382 var params = this.comment.getTag("param");
384 // user-defined params overwrite those with same name defined by the parser
385 var thisParams = this.getParams();
387 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
388 this.setParams(params);
390 else { // need to overlay these user-defined params on to existing parser-defined params
391 for (var i = 0, l = params.length; i < l; i++) {
393 if (params[i].type) thisParams[i].type = params[i].type;
394 thisParams[i].name = params[i].name;
395 thisParams[i].desc = params[i].desc;
396 thisParams[i].isOptional = params[i].isOptional;
397 thisParams[i].defaultValue = params[i].defaultValue;
399 else thisParams[i] = params[i];
405 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
406 assertEqual(sym.params.length, 1, "parser defined param is found.");
408 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
409 assertEqual(sym.params.length, 1, "user defined param is found.");
410 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
411 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
413 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
414 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
415 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
416 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
418 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
419 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
420 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
421 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
423 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
424 assertEqual(sym.params.length, 1, "user defined param with description is found.");
425 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
429 if (this.comment.getTag("constructor").length) {
430 this.isa = "CONSTRUCTOR";
434 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
435 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
439 if (this.comment.getTag("static").length) {
440 this.isStatic = true;
441 if (this.isa == "CONSTRUCTOR") {
442 this.isNamespace = true;
447 if (this.comment.getTag("singleton").length) {
448 this.isStatic = true;
449 //if (this.isa == "CONSTRUCTOR") {
450 // this.isNamespace = true;
457 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
458 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
459 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
463 if (this.comment.getTag("inner").length) {
465 this.isStatic = false;
469 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
470 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
471 assertEqual(sym.isInner, true, "@inner makes isInner true.");
475 if (this.comment.getTag("field").length) {
480 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
481 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
485 if (this.comment.getTag("function").length) {
486 this.isa = "FUNCTION";
490 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
491 // change a property to a function..
492 this.isa = "FUNCTION";
497 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
498 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
502 var events = this.comment.getTag("event");
504 this.isa = "FUNCTION";
509 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
510 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
511 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
515 var names = this.comment.getTag("name");
517 this.setName(names[0].desc);
525 var properties = this.comment.getTag("property");
526 if (properties.length) {
527 thisProperties = this.properties;
528 for (var i = 0; i < properties.length; i++) {
529 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
530 // TODO: shouldn't the following happen in the addProperty method of Symbol?
531 property.name = properties[i].name;
532 property.memberOf = this.alias;
533 if (properties[i].type) property.type = properties[i].type;
534 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
535 this.addProperty(property);
536 imports.Parser.Parser.addSymbol(property);
541 var conf = this.comment.getTag("cfg");
543 for (var i = 0; i < conf.length; i++) {
544 this.addConfig(conf[i]);
553 var returns = this.comment.getTag("return");
554 if (returns.length) { // there can be many return tags in a single doclet
555 this.returns = returns;
556 this.type = returns.map(function($){return $.type}).join(", ");
564 this.exceptions = this.comment.getTag("throws");
571 var requires = this.comment.getTag("requires");
572 if (requires.length) {
573 this.requires = requires.map(function($){return $.desc});
581 var types = this.comment.getTag("type");
583 this.type = types[0].desc; //multiple type tags are ignored
591 if (this.comment.getTag("private").length || this.isInner) {
592 this.isPrivate = true;
596 if (this.comment.getTag("ignore").length) {
597 this.isIgnored = true;
604 // @inherits ... as ...
605 var inherits = this.comment.getTag("inherits");
606 if (inherits.length) {
607 for (var i = 0; i < inherits.length; i++) {
608 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
609 var inAlias = RegExp.$1;
610 var inAs = RegExp.$2 || inAlias;
612 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
615 inAs = inAs.replace(/\.prototype\.?/g, "#");
616 inAs = inAs.replace(/^this\.?/, "#");
619 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
621 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
624 inAs = this.alias + joiner + inAs;
627 this.inherits.push({alias: inAlias, as: inAs});
636 this.augments = this.comment.getTag("augments");
639 if (this.comment.getTag("extends")) {
640 this.augments = this.comment.getTag("extends");
645 var defaults = this.comment.getTag("default");
646 if (defaults.length) {
647 if (this.is("OBJECT")) {
648 this.defaultValue = defaults[0].desc;
657 var memberOfs = this.comment.getTag("memberOf");
658 if (memberOfs.length) {
659 this.memberOf = memberOfs[0].desc;
660 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
668 if (this.comment.getTag("public").length) {
669 this.isPrivate = false;
677 is : function(what) {
678 return this.isa === what;
681 isBuiltin : function() {
682 return SymbolSet.isBuiltin(this.alias);
685 setType : function(/**String*/comment, /**Boolean*/overwrite) {
686 if (!overwrite && this.type) return;
687 var typeComment = DocComment.unwrapComment(comment);
688 this.type = typeComment;
691 inherit : function(symbol) {
692 if (!this.hasMember(symbol.name) && !symbol.isInner) {
693 if (symbol.is("FUNCTION"))
694 this.methods.push(symbol);
695 else if (symbol.is("OBJECT"))
696 this.properties.push(symbol);
700 hasMember : function(name) {
701 return (this.hasMethod(name) || this.hasProperty(name));
704 addMember : function(symbol) {
705 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
707 if (symbol.comment.getTag("cfg").length == 1) {
708 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
709 this.addConfig(symbol.comment.getTag("cfg")[0]);
713 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
714 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
717 hasMethod : function(name) {
718 var thisMethods = this.methods;
719 for (var i = 0, l = thisMethods.length; i < l; i++) {
720 if (thisMethods[i].name == name) return true;
721 if (thisMethods[i].alias == name) return true;
726 addMethod : function(symbol) {
727 var methodAlias = symbol.alias;
728 var thisMethods = this.methods;
729 for (var i = 0, l = thisMethods.length; i < l; i++) {
730 if (thisMethods[i].alias == methodAlias) {
731 thisMethods[i] = symbol; // overwriting previous method
735 thisMethods.push(symbol); // new method with this alias
738 hasProperty : function(name) {
739 var thisProperties = this.properties;
740 for (var i = 0, l = thisProperties.length; i < l; i++) {
741 if (thisProperties[i].name == name) return true;
742 if (thisProperties[i].alias == name) return true;
747 addProperty : function(symbol) {
748 var propertyAlias = symbol.alias;
749 var thisProperties = this.properties;
750 for (var i = 0, l = thisProperties.length; i < l; i++) {
751 if (thisProperties[i].alias == propertyAlias) {
752 thisProperties[i] = symbol; // overwriting previous property
757 thisProperties.push(symbol); // new property with this alias
760 addDocTag : function(docTag)
762 this.comment.tags.push(docTag);
763 if (docTag.title == 'cfg') {
764 this.addConfig(docTag);
769 addConfig : function(docTag)
771 if (typeof(docTag['memberOf']) == 'undefined') {
772 // remove prototype data...
773 //var a = this.alias.split('#')[0];
774 //docTag.memberOf = a;
775 docTag.memberOf = this.alias;
777 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
778 this.cfgs[docTag.name] = docTag;
782 configToArray: function()
785 for(var ci in this.cfgs) {
786 // dont show hidden!!
787 if (this.cfgs[ci].desc.match(/@hide/)) {
790 r.push(this.cfgs[ci]);
798 * Elements that are not serialized
802 '$args' // not needed AFAIK
805 Symbol.srcFile = ""; //running reference to the current file being parsed
808 Symbol.fromDump = function(t)
810 var ns = new Symbol();
812 if (typeof(ns[i]) == "undefined") {
813 println("ERR:no default for Symbol:"+ i);