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 : [], // Doctag[]
37 exceptions : [], // 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])) {
93 ret[i] = array2object(this[i],true);
99 print(JSON.stringify(this[i]));
100 ret[i] = this[i].toObject();
104 print("object? :" + i);
111 ret[i] = this[i]; continue;
113 print("unknown type:" + typeof(this[i]));
123 // only initialize arrays / objects..
128 this.exceptions = [];
130 this.inheritsFrom = [];
131 this.isa = "OBJECT"; // OBJECT//FUNCTION
134 this.properties = [];
139 this.childClasses = [];
142 this.comment = new DocComment();
143 this.comment.isUserComment = false;
148 serialize : function() {
150 for (var p in this) {
156 for (var i in keys) {
157 if (typeof this[keys[i]] == "function") continue;
158 out += " " +keys[i]+" => "+
160 (typeof(this[keys[i]]) != "object") ?
162 "[" +typeof(this[keys[i]])+"]"
166 return "\n{\n" + out + "}\n";
170 var clone = new Symbol();
171 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
172 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
179 //__defineSetter__("name",
180 setName : function(n) {
181 n = n.replace(/^_global_[.#-]/, "");
182 n = n.replace(/\.prototype\.?/g, '#');
183 n = n.replace(/#$/g, '');
185 this.name = n; // real!
188 //__defineGetter__("name",
189 getName : function() { return this._name; },
191 //__defineSetter__("params",
192 setParams :function(v) {
193 for (var i = 0, l = v.length; i < l; i++) {
194 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
195 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
196 this._params[i] = new DocTag(
197 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
200 this._params[i] = v[i];
203 this.params = this._params;
208 //__defineGetter__("params",
209 getParams : function() { return this._params; },
214 /** Object[] */ params,
216 /** DocComment */ comment
218 this.$args = arguments;
219 //println("Symbol created: " + isa + ":" + name);
221 this.alias = this.getName();
222 this.setParams(params);
223 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
224 this.comment = comment || new DocComment("");
225 this.srcFile = Symbol.srcFile;
229 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
233 //if (typeof PluginManager != "undefined") {
234 // PluginManager.run("onSymbol", this);
238 setTags : function() {
240 var authors = this.comment.getTag("author");
241 if (authors.length) {
242 this.author = authors.map(function($){return $.desc;}).join(", ");
246 assert("testing Symbol");
248 requires("../lib/JSDOC/DocComment.js");
249 requires("../frame/String.js");
250 requires("../lib/JSDOC/DocTag.js");
252 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
253 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
256 var mth = this.comment.getTag("method");
258 this.isa = "FUNCTION";
261 var descs = this.comment.getTag("desc");
263 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
267 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
268 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
272 if (this.is("FILE")) {
273 if (!this.alias) this.alias = this.srcFile;
275 var overviews = this.comment.getTag("overview");
276 if (overviews.length) {
277 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
282 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
283 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
287 var sinces = this.comment.getTag("since");
289 this.since = sinces.map(function($){return $.desc;}).join(", ");
293 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
294 assertEqual(sym.since, "1.01", "@since tag, description is found.");
298 if (this.comment.getTag("constant").length) {
299 this.isConstant = true;
304 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
305 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
309 var versions = this.comment.getTag("version");
310 if (versions.length) {
311 this.version = versions.map(function($){return $.desc;}).join(", ");
315 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
316 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
320 var deprecateds = this.comment.getTag("deprecated");
321 if (deprecateds.length) {
322 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
326 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
327 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
331 var examples = this.comment.getTag("example");
332 if (examples.length) {
333 this.example = examples[0];
337 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
338 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
342 var sees = this.comment.getTag("see");
344 var thisSee = this.see;
345 sees.map(function($){thisSee.push($.desc);});
349 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
350 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
354 var classes = this.comment.getTag("class");
355 if (classes.length) {
356 this.isa = "CONSTRUCTOR";
357 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
358 if (!this.classDesc) {
359 this.classDesc = this.desc;
366 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
367 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
368 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
372 var namespaces = this.comment.getTag("namespace");
373 if (namespaces.length) {
374 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
375 this.isNamespace = true;
379 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
380 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
384 var params = this.comment.getTag("param");
386 // user-defined params overwrite those with same name defined by the parser
387 var thisParams = this.getParams();
389 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
390 this.setParams(params);
392 else { // need to overlay these user-defined params on to existing parser-defined params
393 for (var i = 0, l = params.length; i < l; i++) {
395 if (params[i].type) thisParams[i].type = params[i].type;
396 thisParams[i].name = params[i].name;
397 thisParams[i].desc = params[i].desc;
398 thisParams[i].isOptional = params[i].isOptional;
399 thisParams[i].defaultValue = params[i].defaultValue;
401 else thisParams[i] = params[i];
407 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
408 assertEqual(sym.params.length, 1, "parser defined param is found.");
410 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
411 assertEqual(sym.params.length, 1, "user defined param is found.");
412 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
413 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
415 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
416 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
417 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
418 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
420 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
421 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
422 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
423 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
425 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
426 assertEqual(sym.params.length, 1, "user defined param with description is found.");
427 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
431 if (this.comment.getTag("constructor").length) {
432 this.isa = "CONSTRUCTOR";
436 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
437 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
441 if (this.comment.getTag("static").length) {
442 this.isStatic = true;
443 if (this.isa == "CONSTRUCTOR") {
444 this.isNamespace = true;
449 if (this.comment.getTag("singleton").length) {
450 this.isStatic = true;
451 //if (this.isa == "CONSTRUCTOR") {
452 // this.isNamespace = true;
459 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
460 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
461 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
465 if (this.comment.getTag("inner").length) {
467 this.isStatic = false;
471 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
472 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
473 assertEqual(sym.isInner, true, "@inner makes isInner true.");
477 if (this.comment.getTag("field").length) {
482 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
483 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
487 if (this.comment.getTag("function").length) {
488 this.isa = "FUNCTION";
492 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
493 // change a property to a function..
494 this.isa = "FUNCTION";
499 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
500 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
504 var events = this.comment.getTag("event");
506 this.isa = "FUNCTION";
511 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
512 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
513 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
517 var names = this.comment.getTag("name");
519 this.setName(names[0].desc);
527 var properties = this.comment.getTag("property");
528 if (properties.length) {
529 thisProperties = this.properties;
530 for (var i = 0; i < properties.length; i++) {
531 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
532 // TODO: shouldn't the following happen in the addProperty method of Symbol?
533 property.name = properties[i].name;
534 property.memberOf = this.alias;
535 if (properties[i].type) property.type = properties[i].type;
536 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
537 this.addProperty(property);
538 imports.Parser.Parser.addSymbol(property);
543 var conf = this.comment.getTag("cfg");
545 for (var i = 0; i < conf.length; i++) {
546 this.addConfig(conf[i]);
555 var returns = this.comment.getTag("return");
556 if (returns.length) { // there can be many return tags in a single doclet
557 this.returns = returns;
558 this.type = returns.map(function($){return $.type}).join(", ");
566 this.exceptions = this.comment.getTag("throws");
573 var requires = this.comment.getTag("requires");
574 if (requires.length) {
575 this.requires = requires.map(function($){return $.desc});
583 var types = this.comment.getTag("type");
585 this.type = types[0].desc; //multiple type tags are ignored
593 if (this.comment.getTag("private").length || this.isInner) {
594 this.isPrivate = true;
598 if (this.comment.getTag("ignore").length) {
599 this.isIgnored = true;
606 // @inherits ... as ...
607 var inherits = this.comment.getTag("inherits");
608 if (inherits.length) {
609 for (var i = 0; i < inherits.length; i++) {
610 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
611 var inAlias = RegExp.$1;
612 var inAs = RegExp.$2 || inAlias;
614 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
617 inAs = inAs.replace(/\.prototype\.?/g, "#");
618 inAs = inAs.replace(/^this\.?/, "#");
621 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
623 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
626 inAs = this.alias + joiner + inAs;
629 this.inherits.push({alias: inAlias, as: inAs});
638 this.augments = this.comment.getTag("augments");
641 if (this.comment.getTag("extends")) {
642 this.augments = this.comment.getTag("extends");
647 var defaults = this.comment.getTag("default");
648 if (defaults.length) {
649 if (this.is("OBJECT")) {
650 this.defaultValue = defaults[0].desc;
659 var memberOfs = this.comment.getTag("memberOf");
660 if (memberOfs.length) {
661 this.memberOf = memberOfs[0].desc;
662 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
670 if (this.comment.getTag("public").length) {
671 this.isPrivate = false;
679 is : function(what) {
680 return this.isa === what;
683 isBuiltin : function() {
684 return SymbolSet.isBuiltin(this.alias);
687 setType : function(/**String*/comment, /**Boolean*/overwrite) {
688 if (!overwrite && this.type) return;
689 var typeComment = DocComment.unwrapComment(comment);
690 this.type = typeComment;
693 inherit : function(symbol) {
694 if (!this.hasMember(symbol.name) && !symbol.isInner) {
695 if (symbol.is("FUNCTION"))
696 this.methods.push(symbol);
697 else if (symbol.is("OBJECT"))
698 this.properties.push(symbol);
702 hasMember : function(name) {
703 return (this.hasMethod(name) || this.hasProperty(name));
706 addMember : function(symbol) {
707 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
709 if (symbol.comment.getTag("cfg").length == 1) {
710 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
711 this.addConfig(symbol.comment.getTag("cfg")[0]);
715 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
716 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
719 hasMethod : function(name) {
720 var thisMethods = this.methods;
721 for (var i = 0, l = thisMethods.length; i < l; i++) {
722 if (thisMethods[i].name == name) return true;
723 if (thisMethods[i].alias == name) return true;
728 addMethod : function(symbol) {
729 var methodAlias = symbol.alias;
730 var thisMethods = this.methods;
731 for (var i = 0, l = thisMethods.length; i < l; i++) {
732 if (thisMethods[i].alias == methodAlias) {
733 thisMethods[i] = symbol; // overwriting previous method
737 thisMethods.push(symbol); // new method with this alias
740 hasProperty : function(name) {
741 var thisProperties = this.properties;
742 for (var i = 0, l = thisProperties.length; i < l; i++) {
743 if (thisProperties[i].name == name) return true;
744 if (thisProperties[i].alias == name) return true;
749 addProperty : function(symbol) {
750 var propertyAlias = symbol.alias;
751 var thisProperties = this.properties;
752 for (var i = 0, l = thisProperties.length; i < l; i++) {
753 if (thisProperties[i].alias == propertyAlias) {
754 thisProperties[i] = symbol; // overwriting previous property
759 thisProperties.push(symbol); // new property with this alias
762 addDocTag : function(docTag)
764 this.comment.tags.push(docTag);
765 if (docTag.title == 'cfg') {
766 this.addConfig(docTag);
771 addConfig : function(docTag)
773 if (typeof(docTag['memberOf']) == 'undefined') {
774 // remove prototype data...
775 //var a = this.alias.split('#')[0];
776 //docTag.memberOf = a;
777 docTag.memberOf = this.alias;
779 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
780 this.cfgs[docTag.name] = docTag;
784 configToArray: function()
787 for(var ci in this.cfgs) {
788 // dont show hidden!!
789 if (this.cfgs[ci].desc.match(/@hide/)) {
792 r.push(this.cfgs[ci]);
800 * Elements that are not serialized
804 '$args' // not needed AFAIK
807 Symbol.srcFile = ""; //running reference to the current file being parsed
810 Symbol.fromDump = function(t)
812 var ns = new Symbol();
814 if (typeof(ns[i]) == "undefined") {
815 println("ERR:no default for Symbol:"+ i);