1 //<script type="text/javascript">
3 XObject = imports.XObject.XObject;
5 SymbolSet = imports.SymbolSet.SymbolSet;
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 : [], // Doctag[]
37 exceptions : [], // Doctag[]
38 inherits : [], // Doctag[]
40 isa : "OBJECT", // OBJECT//FUNCTION
49 methods : [], // Symbol[]
51 _params : [], //Doctag[]
52 properties : [], //Doctag[]
53 requires : [], //Doctag[]
54 returns : [], //Doctag[]
70 var ret = { '*object' : 'Symbol' };
72 if (Symbol.hide.indexOf(i) > -1) {
75 switch (typeof(this[i])) {
91 case 'cfgs': // key val of doctags..
102 print("object? :" + i);
110 ret[i] = this[i]; continue;
112 print("unknown type:" + typeof(this[i]));
122 // only initialize arrays / objects..
127 this.exceptions = [];
130 this.isa = "OBJECT"; // OBJECT//FUNCTION
133 this.properties = [];
142 this.inheritsFrom = [];
143 this.childClasses = [];
145 this.comment = new DocComment();
146 this.comment.isUserComment = false;
151 serialize : function() {
153 for (var p in this) {
159 for (var i in keys) {
160 if (typeof this[keys[i]] == "function") continue;
161 out += " " +keys[i]+" => "+
163 (typeof(this[keys[i]]) != "object") ?
165 "[" +typeof(this[keys[i]])+"]"
169 return "\n{\n" + out + "}\n";
173 var clone = new Symbol();
174 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
175 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
182 //__defineSetter__("name",
183 setName : function(n) {
184 n = n.replace(/^_global_[.#-]/, "");
185 n = n.replace(/\.prototype\.?/g, '#');
186 n = n.replace(/#$/g, '');
188 this.name = n; // real!
191 //__defineGetter__("name",
192 getName : function() { return this._name; },
194 //__defineSetter__("params",
195 setParams :function(v) {
196 for (var i = 0, l = v.length; i < l; i++) {
197 if (typeof(v[i]) == 'string') {
198 this._params[i] = new DocTag( "param "+ v[i].name );
201 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
202 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
203 this._params[i] = new DocTag(
204 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
207 // default is doctag..
208 this._params[i] = v[i];
211 this.params = this._params;
216 //__defineGetter__("params",
217 getParams : function() { return this._params; },
222 /** Object[] */ params,
224 /** DocComment */ comment
226 this.$args = arguments;
227 //println("Symbol created: " + isa + ":" + name);
229 this.alias = this.getName();
230 this.setParams(params);
231 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
232 this.comment = comment || new DocComment("");
233 this.srcFile = Symbol.srcFile;
237 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
241 //if (typeof PluginManager != "undefined") {
242 // PluginManager.run("onSymbol", this);
246 setTags : function() {
248 var authors = this.comment.getTag("author");
249 if (authors.length) {
250 this.author = authors.map(function($){return $.desc;}).join(", ");
254 assert("testing Symbol");
256 requires("../lib/JSDOC/DocComment.js");
257 requires("../frame/String.js");
258 requires("../lib/JSDOC/DocTag.js");
260 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
261 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
264 var mth = this.comment.getTag("method");
266 this.isa = "FUNCTION";
269 var descs = this.comment.getTag("desc");
271 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
275 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
276 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
280 if (this.is("FILE")) {
281 if (!this.alias) this.alias = this.srcFile;
283 var overviews = this.comment.getTag("overview");
284 if (overviews.length) {
285 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
291 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
295 var sinces = this.comment.getTag("since");
297 this.since = sinces.map(function($){return $.desc;}).join(", ");
301 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
302 assertEqual(sym.since, "1.01", "@since tag, description is found.");
306 if (this.comment.getTag("constant").length) {
307 this.isConstant = true;
312 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
313 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
317 var versions = this.comment.getTag("version");
318 if (versions.length) {
319 this.version = versions.map(function($){return $.desc;}).join(", ");
323 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
324 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
328 var deprecateds = this.comment.getTag("deprecated");
329 if (deprecateds.length) {
330 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
334 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
335 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
339 var examples = this.comment.getTag("example");
340 if (examples.length) {
341 this.example = examples[0];
345 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
346 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
350 var sees = this.comment.getTag("see");
352 var thisSee = this.see;
353 sees.map(function($){thisSee.push($.desc);});
357 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
358 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
362 var classes = this.comment.getTag("class");
363 if (classes.length) {
364 this.isa = "CONSTRUCTOR";
365 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
366 if (!this.classDesc) {
367 this.classDesc = this.desc;
374 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
375 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
376 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
380 var namespaces = this.comment.getTag("namespace");
381 if (namespaces.length) {
382 this.namespace = namespaces[0].desc;
383 Symbol.namespace = namespaces[0].desc + '.';
384 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
385 this.isNamespace = true;
389 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
390 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
394 var params = this.comment.getTag("param");
396 // user-defined params overwrite those with same name defined by the parser
397 var thisParams = this.getParams();
399 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
400 this.setParams(params);
402 else { // need to overlay these user-defined params on to existing parser-defined params
403 for (var i = 0, l = params.length; i < l; i++) {
405 if (params[i].type) thisParams[i].type = params[i].type;
406 thisParams[i].name = params[i].name;
407 thisParams[i].desc = params[i].desc;
408 thisParams[i].isOptional = params[i].isOptional;
409 thisParams[i].defaultValue = params[i].defaultValue;
411 else thisParams[i] = params[i];
417 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
418 assertEqual(sym.params.length, 1, "parser defined param is found.");
420 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
421 assertEqual(sym.params.length, 1, "user defined param is found.");
422 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
423 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
425 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
426 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
427 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
428 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
430 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
431 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
432 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
433 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
435 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
436 assertEqual(sym.params.length, 1, "user defined param with description is found.");
437 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
441 if (this.comment.getTag("constructor").length) {
442 this.isa = "CONSTRUCTOR";
446 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
447 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
451 if (this.comment.getTag("static").length) {
452 this.isStatic = true;
453 // replace '#' with '.'
455 this.name = this.name.split('#').join('.');
456 this._name = this.name
457 this.alias = this.name;
459 if (this.isa == "CONSTRUCTOR") {
460 this.isNamespace = true;
465 if (this.comment.getTag("singleton").length) {
466 this.isStatic = true;
467 //if (this.isa == "CONSTRUCTOR") {
468 // this.isNamespace = true;
475 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
476 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
477 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
481 if (this.comment.getTag("inner").length) {
483 this.isStatic = false;
487 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
488 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
489 assertEqual(sym.isInner, true, "@inner makes isInner true.");
493 if (this.comment.getTag("field").length) {
498 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
499 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
503 if (this.comment.getTag("function").length) {
504 this.isa = "FUNCTION";
508 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
509 // change a property to a function..
510 this.isa = "FUNCTION";
515 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
516 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
520 var events = this.comment.getTag("event");
522 this.isa = "FUNCTION";
527 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
528 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
529 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
533 var names = this.comment.getTag("name");
535 this.setName(names[0].desc);
543 var properties = this.comment.getTag("property");
544 if (properties.length) {
545 thisProperties = this.properties;
546 for (var i = 0; i < properties.length; i++) {
547 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
548 // TODO: shouldn't the following happen in the addProperty method of Symbol?
549 property.name = properties[i].name;
550 property.memberOf = this.alias;
551 if (properties[i].type) property.type = properties[i].type;
552 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
553 this.addProperty(property);
554 //Symbol.parser.addSymbol(property);
555 imports.ScopeNamer.ScopeNamer.addSymbol(property);
560 var conf = this.comment.getTag("cfg");
562 for (var i = 0; i < conf.length; i++) {
563 this.addConfig(conf[i]);
572 var returns = this.comment.getTag("return");
573 if (returns.length) { // there can be many return tags in a single doclet
574 this.returns = returns;
575 this.type = returns.map(function($){return $.type}).join(", ");
583 this.exceptions = this.comment.getTag("throws");
590 var requires = this.comment.getTag("requires");
591 if (requires.length) {
592 this.requires = requires.map(function($){return $.desc});
600 var types = this.comment.getTag("type");
602 this.type = types[0].desc; //multiple type tags are ignored
610 if (this.comment.getTag("private").length || this.isInner) {
611 this.isPrivate = true;
615 if (this.comment.getTag("ignore").length) {
616 this.isIgnored = true;
623 // @inherits ... as ...
624 var inherits = this.comment.getTag("inherits");
625 if (inherits.length) {
626 for (var i = 0; i < inherits.length; i++) {
627 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
628 var inAlias = RegExp.$1;
629 var inAs = RegExp.$2 || inAlias;
631 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
634 inAs = inAs.replace(/\.prototype\.?/g, "#");
635 inAs = inAs.replace(/^this\.?/, "#");
638 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
640 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
643 inAs = this.alias + joiner + inAs;
646 this.inherits.push({alias: inAlias, as: inAs});
655 this.augments = this.comment.getTag("augments");
658 if (this.comment.getTag("extends")) {
659 this.augments = this.comment.getTag("extends");
664 var defaults = this.comment.getTag("default");
665 if (defaults.length) {
666 if (this.is("OBJECT")) {
667 this.defaultValue = defaults[0].desc;
676 var memberOfs = this.comment.getTag("memberOf");
677 if (memberOfs.length) {
678 this.memberOf = memberOfs[0].desc;
679 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
680 this.name = this.name.split('.').pop();
681 this.name = this.name.split('#').pop();
682 this.name = this.memberOf + this.name;
683 this._name = this.name
684 this.alias = this.name;
692 if (this.comment.getTag("public").length) {
693 this.isPrivate = false;
701 is : function(what) {
702 return this.isa === what;
705 isBuiltin : function() {
706 return SymbolSet.isBuiltin(this.alias);
709 setType : function(/**String*/comment, /**Boolean*/overwrite) {
710 if (!overwrite && this.type) return;
711 var typeComment = DocComment.unwrapComment(comment);
712 this.type = typeComment;
715 inherit : function(symbol) {
716 if (!this.hasMember(symbol.name) && !symbol.isInner) {
717 if (symbol.is("FUNCTION"))
718 this.methods.push(symbol);
719 else if (symbol.is("OBJECT"))
720 this.properties.push(symbol);
724 hasMember : function(name) {
725 return (this.hasMethod(name) || this.hasProperty(name));
728 addMember : function(symbol) {
729 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
731 if (symbol.comment.getTag("cfg").length == 1) {
732 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
733 this.addConfig(symbol.comment.getTag("cfg")[0]);
737 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
738 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
741 hasMethod : function(name) {
742 var thisMethods = this.methods;
743 for (var i = 0, l = thisMethods.length; i < l; i++) {
744 if (thisMethods[i].name == name) return true;
745 if (thisMethods[i].alias == name) return true;
750 addMethod : function(symbol) {
751 var methodAlias = symbol.alias;
752 var thisMethods = this.methods;
753 for (var i = 0, l = thisMethods.length; i < l; i++) {
754 if (thisMethods[i].alias == methodAlias) {
755 thisMethods[i] = symbol; // overwriting previous method
759 thisMethods.push(symbol); // new method with this alias
762 hasProperty : function(name) {
763 var thisProperties = this.properties;
764 for (var i = 0, l = thisProperties.length; i < l; i++) {
765 if (thisProperties[i].name == name) return true;
766 if (thisProperties[i].alias == name) return true;
771 addProperty : function(symbol) {
772 var propertyAlias = symbol.alias;
773 var thisProperties = this.properties;
774 for (var i = 0, l = thisProperties.length; i < l; i++) {
775 if (thisProperties[i].alias == propertyAlias) {
776 thisProperties[i] = symbol; // overwriting previous property
781 thisProperties.push(symbol); // new property with this alias
784 addDocTag : function(docTag)
786 this.comment.tags.push(docTag);
787 if (docTag.title == 'cfg') {
788 this.addConfig(docTag);
793 addConfig : function(docTag)
795 if (typeof(docTag['memberOf']) == 'undefined') {
796 // remove prototype data...
797 //var a = this.alias.split('#')[0];
798 //docTag.memberOf = a;
799 docTag.memberOf = this.alias;
801 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
802 this.cfgs[docTag.name] = docTag;
806 configToArray: function()
809 for(var ci in this.cfgs) {
810 // dont show hidden!!
811 if (this.cfgs[ci].desc.match(/@hide/)) {
814 r.push(this.cfgs[ci]);
822 * Elements that are not serialized
826 '$args' // not needed AFAIK
829 Symbol.srcFile = ""; //running reference to the current file being parsed
830 Symbol.namespace = ""; //running reference to the current file being parsed
833 Symbol.fromDump = function(t)
835 var ns = new Symbol();
837 if (typeof(ns[i]) == "undefined") {
838 println("ERR:no default for Symbol:"+ i);
844 Symbol.parser = false; // set up by parser..