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.
30 augments : [], // Doctag[]
38 exceptions : [], // Doctag[]
39 inherits : [], // Doctag[]
41 isa : "OBJECT", // OBJECT//FUNCTION
46 isNamespace : false, // depreciated.
50 methods : [], // Symbol[]
52 _params : [], //Doctag[]
53 properties : [], //Doctag[]
54 requires : [], //Doctag[]
55 returns : [], //Doctag[]
71 var ret = { '*object' : 'Symbol' };
73 if (Symbol.hide.indexOf(i) > -1) {
76 switch (typeof(this[i])) {
92 case 'cfgs': // key val of doctags..
103 print("object? :" + i);
111 ret[i] = this[i]; continue;
113 print("unknown type:" + typeof(this[i]));
123 // only initialize arrays / objects..
128 this.exceptions = [];
131 this.isa = "OBJECT"; // OBJECT//FUNCTION
134 this.properties = [];
143 this.inheritsFrom = [];
144 this.childClasses = [];
146 this.comment = new DocComment();
147 this.comment.isUserComment = false;
152 serialize : function() {
154 for (var p in this) {
160 for (var i in keys) {
161 if (typeof this[keys[i]] == "function") continue;
162 out += " " +keys[i]+" => "+
164 (typeof(this[keys[i]]) != "object") ?
166 "[" +typeof(this[keys[i]])+"]"
170 return "\n{\n" + out + "}\n";
174 var clone = new Symbol();
175 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
176 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
183 //__defineSetter__("name",
184 setName : function(n) {
185 n = n.replace(/^_global_[.#-]/, "");
186 n = n.replace(/\.prototype\.?/g, '#');
187 n = n.replace(/#$/g, '');
189 this.name = n; // real!
192 //__defineGetter__("name",
193 getName : function() {
197 //__defineSetter__("params",
198 setParams :function(v) {
199 for (var i = 0, l = v.length; i < l; i++) {
200 if (typeof(v[i]) == 'string') {
201 this._params[i] = new DocTag( "param "+ v[i] );
204 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
205 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
206 this._params[i] = new DocTag(
207 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
210 // default is doctag..
211 this._params[i] = v[i];
214 this.params = this._params;
219 //__defineGetter__("params",
220 getParams : function() { return this._params; },
225 /** Object[] */ params,
227 /** DocComment */ comment
229 this.$args = arguments;
230 //println("Symbol created: " + isa + ":" + name);
232 this.alias = this.getName();
233 this.setParams(params);
234 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
235 this.comment = comment || new DocComment("");
236 this.srcFile = Symbol.srcFile;
240 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
244 //if (typeof PluginManager != "undefined") {
245 // PluginManager.run("onSymbol", this);
249 setTags : function() {
251 var authors = this.comment.getTag("author");
252 if (authors.length) {
253 this.author = authors.map(function($){return $.desc;}).join(", ");
257 assert("testing Symbol");
259 requires("../lib/JSDOC/DocComment.js");
260 requires("../frame/String.js");
261 requires("../lib/JSDOC/DocTag.js");
263 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
264 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
267 var mth = this.comment.getTag("method");
269 this.isa = "FUNCTION";
272 var descs = this.comment.getTag("desc");
274 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
278 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
279 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
283 if (this.is("FILE")) {
284 if (!this.alias) this.alias = this.srcFile;
286 var overviews = this.comment.getTag("overview");
287 if (overviews.length) {
288 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
293 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
294 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
298 var sinces = this.comment.getTag("since");
300 this.since = sinces.map(function($){return $.desc;}).join(", ");
304 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
305 assertEqual(sym.since, "1.01", "@since tag, description is found.");
309 if (this.comment.getTag("constant").length) {
310 this.isConstant = true;
315 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
316 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
320 var versions = this.comment.getTag("version");
321 if (versions.length) {
322 this.version = versions.map(function($){return $.desc;}).join(", ");
326 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
327 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
331 var deprecateds = this.comment.getTag("deprecated");
332 if (deprecateds.length) {
333 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
337 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
338 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
342 var examples = this.comment.getTag("example");
343 if (examples.length) {
344 this.example = examples[0];
348 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
349 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
353 var sees = this.comment.getTag("see");
355 var thisSee = this.see;
356 sees.map(function($){thisSee.push($.desc);});
360 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
361 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
365 var classes = this.comment.getTag("class");
366 if (classes.length) {
367 this.isa = "CONSTRUCTOR";
368 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
369 if (!this.classDesc) {
370 this.classDesc = this.desc;
377 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
378 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
379 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
383 var namespaces = this.comment.getTag("namespace");
384 if (namespaces.length) {
385 this.namespace = namespaces[0].name;
386 Symbol.namespace = namespaces[0].name + '.';
387 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
388 //this.isNamespace = true;
392 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
393 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
397 var params = this.comment.getTag("param");
399 // user-defined params overwrite those with same name defined by the parser
400 var thisParams = this.getParams();
402 if (thisParams.length == 0) {
403 // none exist yet, so just bung all these user-defined params
405 this.setParams(params);
407 // need to overlay these user-defined params on to
408 // existing parser-defined params
409 for (var i = 0, l = params.length; i < l; i++) {
411 if (params[i].type) {
412 thisParams[i].type = params[i].type;
414 thisParams[i].name = params[i].name;
415 thisParams[i].desc = params[i].desc;
416 thisParams[i].isOptional = params[i].isOptional;
417 thisParams[i].defaultValue = params[i].defaultValue;
421 thisParams[i] = params[i];
428 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
429 assertEqual(sym.params.length, 1, "parser defined param is found.");
431 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
432 assertEqual(sym.params.length, 1, "user defined param is found.");
433 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
434 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
436 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
437 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
438 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
439 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
441 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
442 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
443 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
444 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
446 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
447 assertEqual(sym.params.length, 1, "user defined param with description is found.");
448 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
452 if (this.comment.getTag("constructor").length) {
453 this.isa = "CONSTRUCTOR";
457 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
458 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
462 if (this.comment.getTag("static").length) {
463 this.isStatic = true;
464 // replace '#' with '.'
466 this.name = this.name.split('#').join('.');
467 this._name = this.name
468 this.alias = this.name;
470 //if (this.isa == "CONSTRUCTOR") {
471 // this.isNamespace = true;
476 if (this.comment.getTag("singleton").length) {
477 this.isStatic = true;
478 //if (this.isa == "CONSTRUCTOR") {
479 // this.isNamespace = true;
486 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
487 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
488 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
492 if (this.comment.getTag("inner").length) {
494 this.isStatic = false;
498 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
499 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
500 assertEqual(sym.isInner, true, "@inner makes isInner true.");
504 if (this.comment.getTag("field").length) {
509 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
510 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
514 if (this.comment.getTag("function").length) {
515 this.isa = "FUNCTION";
519 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
520 // change a property to a function..
521 this.isa = "FUNCTION";
526 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
527 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
531 var events = this.comment.getTag("event");
533 this.isa = "FUNCTION";
538 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
539 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
540 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
544 var names = this.comment.getTag("name");
546 this.setName(names[0].desc);
554 var properties = this.comment.getTag("property");
555 if (properties.length) {
556 thisProperties = this.properties;
557 for (var i = 0; i < properties.length; i++) {
558 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
559 // TODO: shouldn't the following happen in the addProperty method of Symbol?
560 property.name = properties[i].name;
561 property.memberOf = this.alias;
562 if (properties[i].type) property.type = properties[i].type;
563 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
564 this.addProperty(property);
565 //Symbol.parser.addSymbol(property);
566 imports.ScopeNamer.ScopeNamer.addSymbol(property);
571 var conf = this.comment.getTag("cfg");
573 for (var i = 0; i < conf.length; i++) {
574 this.addConfig(conf[i]);
583 var returns = this.comment.getTag("return");
584 if (returns.length) { // there can be many return tags in a single doclet
585 this.returns = returns;
586 this.type = returns.map(function($){return $.type}).join(", ");
594 this.exceptions = this.comment.getTag("throws");
601 var requires = this.comment.getTag("requires");
602 if (requires.length) {
603 this.requires = requires.map(function($){return $.desc});
611 var types = this.comment.getTag("type");
613 this.type = types[0].type; //multiple type tags are ignored
614 if (types[0].desc.length && !this.desc.length) {
615 this.desc = types[0].desc;
624 if (this.comment.getTag("private").length || this.isInner) {
625 this.isPrivate = true;
629 if (this.comment.getTag("ignore").length) {
630 this.isIgnored = true;
637 // @inherits ... as ...
638 var inherits = this.comment.getTag("inherits");
639 if (inherits.length) {
640 for (var i = 0; i < inherits.length; i++) {
641 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
642 var inAlias = RegExp.$1;
643 var inAs = RegExp.$2 || inAlias;
645 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
648 inAs = inAs.replace(/\.prototype\.?/g, "#");
649 inAs = inAs.replace(/^this\.?/, "#");
652 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
654 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
657 inAs = this.alias + joiner + inAs;
660 this.inherits.push({alias: inAlias, as: inAs});
669 this.augments = this.comment.getTag("augments");
672 if (this.comment.getTag("extends")) {
673 this.augments = this.comment.getTag("extends");
678 var defaults = this.comment.getTag("default");
679 if (defaults.length) {
680 if (this.is("OBJECT")) {
681 this.defaultValue = defaults[0].desc;
690 var memberOfs = this.comment.getTag("memberOf");
691 if (memberOfs.length) {
692 this.memberOf = memberOfs[0].desc;
693 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
694 this.name = this.name.split('.').pop();
695 this.name = this.name.split('#').pop();
696 this.name = this.memberOf + this.name;
697 this._name = this.name
698 this.alias = this.name;
706 if (this.comment.getTag("public").length) {
707 this.isPrivate = false;
715 is : function(what) {
716 return this.isa === what;
719 isBuiltin : function() {
720 return SymbolSet.isBuiltin(this.alias);
723 setType : function(/**String*/comment, /**Boolean*/overwrite) {
724 if (!overwrite && this.type) return;
725 var typeComment = DocComment.unwrapComment(comment);
726 this.type = typeComment;
729 inherit : function(symbol) {
730 if (!this.hasMember(symbol.name) && !symbol.isInner) {
731 if (symbol.is("FUNCTION"))
732 this.methods.push(symbol);
733 else if (symbol.is("OBJECT"))
734 this.properties.push(symbol);
738 hasMember : function(name) {
739 return (this.hasMethod(name) || this.hasProperty(name));
742 addMember : function(symbol) {
743 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
745 if (symbol.comment.getTag("cfg").length == 1) {
746 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
747 this.addConfig(symbol.comment.getTag("cfg")[0]);
751 if (symbol.is("FUNCTION")) {
752 this.addMethod(symbol);
755 if (symbol.is("OBJECT")) {
756 this.addProperty(symbol);
762 hasMethod : function(name) {
763 var thisMethods = this.methods;
764 for (var i = 0, l = thisMethods.length; i < l; i++) {
765 if (thisMethods[i].name == name) return true;
766 if (thisMethods[i].alias == name) return true;
771 addMethod : function(symbol) {
772 var methodAlias = symbol.alias;
773 var thisMethods = this.methods;
774 for (var i = 0, l = thisMethods.length; i < l; i++) {
775 if (thisMethods[i].alias == methodAlias) {
776 thisMethods[i] = symbol; // overwriting previous method
780 thisMethods.push(symbol); // new method with this alias
783 hasProperty : function(name) {
784 var thisProperties = this.properties;
785 for (var i = 0, l = thisProperties.length; i < l; i++) {
786 if (thisProperties[i].name == name) return true;
787 if (thisProperties[i].alias == name) return true;
792 addProperty : function(symbol) {
793 var propertyAlias = symbol.alias;
794 var thisProperties = this.properties;
795 for (var i = 0, l = thisProperties.length; i < l; i++) {
796 if (thisProperties[i].alias == propertyAlias) {
797 thisProperties[i] = symbol; // overwriting previous property
802 thisProperties.push(symbol); // new property with this alias
805 addDocTag : function(docTag)
807 this.comment.tags.push(docTag);
808 if (docTag.title == 'cfg') {
809 this.addConfig(docTag);
814 addConfig : function(docTag)
816 if (typeof(docTag['memberOf']) == 'undefined') {
817 // remove prototype data...
818 //var a = this.alias.split('#')[0];
819 //docTag.memberOf = a;
820 docTag.memberOf = this.alias;
822 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
823 this.cfgs[docTag.name] = docTag;
827 configToArray: function()
830 for(var ci in this.cfgs) {
831 // dont show hidden!!
832 if (this.cfgs[ci].desc.match(/@hide/)) {
835 r.push(this.cfgs[ci]);
843 * Elements that are not serialized
847 '$args' // not needed AFAIK
850 Symbol.srcFile = ""; //running reference to the current file being parsed
851 Symbol.namespace = ""; //running reference to the current file being parsed
854 Symbol.fromDump = function(t)
856 var ns = new Symbol();
858 if (typeof(ns[i]) == "undefined") {
859 println("ERR:no default for Symbol:"+ i);
865 Symbol.parser = false; // set up by parser..