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])) {
90 ret[i] = array2object(this[i],true);
95 ret[i] = this[i].toArray();
99 print("object? :" + i);
106 ret[i] = this[i]; continue;
108 print("unknown type:" + typeof(this[i]));
119 this.defaultValue = "";
127 this.comment = { isUserComment: false };
128 //this.defaultValue = null;
129 this.deprecated = "";
133 this.exceptions = [];
135 this.inheritsFrom = [];
136 this.isa = "OBJECT"; // OBJECT//FUNCTION
137 this.isEvent = false;
138 this.isConstant = false;
139 this.isIgnored = false;
140 this.isInner = false;
141 this.isNamespace = false;
142 this.isPrivate = false;
143 this.isStatic = false;
148 this.properties = [];
156 this.childClasses = [];
161 serialize : function() {
163 for (var p in this) {
169 for (var i in keys) {
170 if (typeof this[keys[i]] == "function") continue;
171 out += " " +keys[i]+" => "+
173 (typeof(this[keys[i]]) != "object") ?
175 "[" +typeof(this[keys[i]])+"]"
179 return "\n{\n" + out + "}\n";
183 var clone = new Symbol();
184 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
185 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
192 //__defineSetter__("name",
193 setName : function(n) {
194 n = n.replace(/^_global_[.#-]/, "");
195 n = n.replace(/\.prototype\.?/g, '#');
196 n = n.replace(/#$/g, '');
198 this.name = n; // real!
201 //__defineGetter__("name",
202 getName : function() { return this._name; },
204 //__defineSetter__("params",
205 setParams :function(v) {
206 for (var i = 0, l = v.length; i < l; i++) {
207 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
208 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
209 this._params[i] = new DocTag(
210 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
213 this._params[i] = v[i];
216 this.params = this._params;
221 //__defineGetter__("params",
222 getParams : function() { return this._params; },
227 /** Object[] */ params,
229 /** DocComment */ comment
231 this.$args = arguments;
232 //println("Symbol created: " + isa + ":" + name);
234 this.alias = this.getName();
235 this.setParams(params);
236 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
237 this.comment = comment || new DocComment("");
238 this.srcFile = Symbol.srcFile;
242 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
246 //if (typeof PluginManager != "undefined") {
247 // PluginManager.run("onSymbol", this);
251 setTags : function() {
253 var authors = this.comment.getTag("author");
254 if (authors.length) {
255 this.author = authors.map(function($){return $.desc;}).join(", ");
259 assert("testing Symbol");
261 requires("../lib/JSDOC/DocComment.js");
262 requires("../frame/String.js");
263 requires("../lib/JSDOC/DocTag.js");
265 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
266 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
269 var mth = this.comment.getTag("method");
271 this.isa = "FUNCTION";
274 var descs = this.comment.getTag("desc");
276 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
280 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
281 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
285 if (this.is("FILE")) {
286 if (!this.alias) this.alias = this.srcFile;
288 var overviews = this.comment.getTag("overview");
289 if (overviews.length) {
290 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
295 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
296 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
300 var sinces = this.comment.getTag("since");
302 this.since = sinces.map(function($){return $.desc;}).join(", ");
306 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
307 assertEqual(sym.since, "1.01", "@since tag, description is found.");
311 if (this.comment.getTag("constant").length) {
312 this.isConstant = true;
317 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
318 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
322 var versions = this.comment.getTag("version");
323 if (versions.length) {
324 this.version = versions.map(function($){return $.desc;}).join(", ");
328 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
329 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
333 var deprecateds = this.comment.getTag("deprecated");
334 if (deprecateds.length) {
335 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
339 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
340 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
344 var examples = this.comment.getTag("example");
345 if (examples.length) {
346 this.example = examples[0];
350 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
351 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
355 var sees = this.comment.getTag("see");
357 var thisSee = this.see;
358 sees.map(function($){thisSee.push($.desc);});
362 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
363 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
367 var classes = this.comment.getTag("class");
368 if (classes.length) {
369 this.isa = "CONSTRUCTOR";
370 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
371 if (!this.classDesc) {
372 this.classDesc = this.desc;
379 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
380 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
381 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
385 var namespaces = this.comment.getTag("namespace");
386 if (namespaces.length) {
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) { // none exist yet, so just bung all these user-defined params straight in
403 this.setParams(params);
405 else { // need to overlay these user-defined params on to existing parser-defined params
406 for (var i = 0, l = params.length; i < l; i++) {
408 if (params[i].type) thisParams[i].type = params[i].type;
409 thisParams[i].name = params[i].name;
410 thisParams[i].desc = params[i].desc;
411 thisParams[i].isOptional = params[i].isOptional;
412 thisParams[i].defaultValue = params[i].defaultValue;
414 else thisParams[i] = params[i];
420 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
421 assertEqual(sym.params.length, 1, "parser defined param is found.");
423 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
424 assertEqual(sym.params.length, 1, "user defined param is found.");
425 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
426 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
428 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
429 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
430 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
431 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
433 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
434 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
435 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
436 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
438 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
439 assertEqual(sym.params.length, 1, "user defined param with description is found.");
440 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
444 if (this.comment.getTag("constructor").length) {
445 this.isa = "CONSTRUCTOR";
449 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
450 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
454 if (this.comment.getTag("static").length) {
455 this.isStatic = true;
456 if (this.isa == "CONSTRUCTOR") {
457 this.isNamespace = true;
462 if (this.comment.getTag("singleton").length) {
463 this.isStatic = true;
464 //if (this.isa == "CONSTRUCTOR") {
465 // this.isNamespace = true;
472 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
473 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
474 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
478 if (this.comment.getTag("inner").length) {
480 this.isStatic = false;
484 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
485 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
486 assertEqual(sym.isInner, true, "@inner makes isInner true.");
490 if (this.comment.getTag("field").length) {
495 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
496 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
500 if (this.comment.getTag("function").length) {
501 this.isa = "FUNCTION";
505 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
506 // change a property to a function..
507 this.isa = "FUNCTION";
512 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
513 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
517 var events = this.comment.getTag("event");
519 this.isa = "FUNCTION";
524 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
525 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
526 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
530 var names = this.comment.getTag("name");
532 this.setName(names[0].desc);
540 var properties = this.comment.getTag("property");
541 if (properties.length) {
542 thisProperties = this.properties;
543 for (var i = 0; i < properties.length; i++) {
544 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
545 // TODO: shouldn't the following happen in the addProperty method of Symbol?
546 property.name = properties[i].name;
547 property.memberOf = this.alias;
548 if (properties[i].type) property.type = properties[i].type;
549 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
550 this.addProperty(property);
551 imports.Parser.Parser.addSymbol(property);
556 var conf = this.comment.getTag("cfg");
558 for (var i = 0; i < conf.length; i++) {
559 this.addConfig(conf[i]);
568 var returns = this.comment.getTag("return");
569 if (returns.length) { // there can be many return tags in a single doclet
570 this.returns = returns;
571 this.type = returns.map(function($){return $.type}).join(", ");
579 this.exceptions = this.comment.getTag("throws");
586 var requires = this.comment.getTag("requires");
587 if (requires.length) {
588 this.requires = requires.map(function($){return $.desc});
596 var types = this.comment.getTag("type");
598 this.type = types[0].desc; //multiple type tags are ignored
606 if (this.comment.getTag("private").length || this.isInner) {
607 this.isPrivate = true;
611 if (this.comment.getTag("ignore").length) {
612 this.isIgnored = true;
619 // @inherits ... as ...
620 var inherits = this.comment.getTag("inherits");
621 if (inherits.length) {
622 for (var i = 0; i < inherits.length; i++) {
623 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
624 var inAlias = RegExp.$1;
625 var inAs = RegExp.$2 || inAlias;
627 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
630 inAs = inAs.replace(/\.prototype\.?/g, "#");
631 inAs = inAs.replace(/^this\.?/, "#");
634 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
636 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
639 inAs = this.alias + joiner + inAs;
642 this.inherits.push({alias: inAlias, as: inAs});
651 this.augments = this.comment.getTag("augments");
654 if (this.comment.getTag("extends")) {
655 this.augments = this.comment.getTag("extends");
660 var defaults = this.comment.getTag("default");
661 if (defaults.length) {
662 if (this.is("OBJECT")) {
663 this.defaultValue = defaults[0].desc;
672 var memberOfs = this.comment.getTag("memberOf");
673 if (memberOfs.length) {
674 this.memberOf = memberOfs[0].desc;
675 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
683 if (this.comment.getTag("public").length) {
684 this.isPrivate = false;
692 is : function(what) {
693 return this.isa === what;
696 isBuiltin : function() {
697 return SymbolSet.isBuiltin(this.alias);
700 setType : function(/**String*/comment, /**Boolean*/overwrite) {
701 if (!overwrite && this.type) return;
702 var typeComment = DocComment.unwrapComment(comment);
703 this.type = typeComment;
706 inherit : function(symbol) {
707 if (!this.hasMember(symbol.name) && !symbol.isInner) {
708 if (symbol.is("FUNCTION"))
709 this.methods.push(symbol);
710 else if (symbol.is("OBJECT"))
711 this.properties.push(symbol);
715 hasMember : function(name) {
716 return (this.hasMethod(name) || this.hasProperty(name));
719 addMember : function(symbol) {
720 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
722 if (symbol.comment.getTag("cfg").length == 1) {
723 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
724 this.addConfig(symbol.comment.getTag("cfg")[0]);
728 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
729 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
732 hasMethod : function(name) {
733 var thisMethods = this.methods;
734 for (var i = 0, l = thisMethods.length; i < l; i++) {
735 if (thisMethods[i].name == name) return true;
736 if (thisMethods[i].alias == name) return true;
741 addMethod : function(symbol) {
742 var methodAlias = symbol.alias;
743 var thisMethods = this.methods;
744 for (var i = 0, l = thisMethods.length; i < l; i++) {
745 if (thisMethods[i].alias == methodAlias) {
746 thisMethods[i] = symbol; // overwriting previous method
750 thisMethods.push(symbol); // new method with this alias
753 hasProperty : function(name) {
754 var thisProperties = this.properties;
755 for (var i = 0, l = thisProperties.length; i < l; i++) {
756 if (thisProperties[i].name == name) return true;
757 if (thisProperties[i].alias == name) return true;
762 addProperty : function(symbol) {
763 var propertyAlias = symbol.alias;
764 var thisProperties = this.properties;
765 for (var i = 0, l = thisProperties.length; i < l; i++) {
766 if (thisProperties[i].alias == propertyAlias) {
767 thisProperties[i] = symbol; // overwriting previous property
772 thisProperties.push(symbol); // new property with this alias
775 addDocTag : function(docTag)
777 this.comment.tags.push(docTag);
778 if (docTag.title == 'cfg') {
779 this.addConfig(docTag);
784 addConfig : function(docTag)
786 if (typeof(docTag['memberOf']) == 'undefined') {
787 // remove prototype data...
788 //var a = this.alias.split('#')[0];
789 //docTag.memberOf = a;
790 docTag.memberOf = this.alias;
792 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
793 this.cfgs[docTag.name] = docTag;
797 configToArray: function()
800 for(var ci in this.cfgs) {
801 // dont show hidden!!
802 if (this.cfgs[ci].desc.match(/@hide/)) {
805 r.push(this.cfgs[ci]);
813 * Elements that are not serialized
817 '$args' // not needed AFAIK
820 Symbol.srcFile = ""; //running reference to the current file being parsed
823 Symbol.fromDump = function(t)
825 var ns = new Symbol();
827 if (typeof(ns[i]) == "undefined") {
828 println("ERR:no default for Symbol:"+ i);