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[]
38 inherits : [], // Doctag[]
40 isa : "OBJECT", // OBJECT//FUNCTION
49 methods : [], // Symbol[]
51 _params : [], //Doctag[]
52 properties : [], //Doctag[]
53 requires : [], //Doctag[]
54 returns : [], //Doctag[]
69 function array2object(a)
73 a.forEach(function(e) {
81 if (Symbol.hide.indexOf(i) > -1) {
84 switch (typeof(this[i])) {
100 ret[i] = array2object(this[i],true);
104 case 'cfgs': // key val of doctags..
107 ret[i][k] = this[i][k].toObject()
113 // print(JSON.stringify(this[i]));
114 ret[i] = this[i].toObject();
123 print("object? :" + i);
131 ret[i] = this[i]; continue;
133 print("unknown type:" + typeof(this[i]));
143 // only initialize arrays / objects..
148 this.exceptions = [];
151 this.isa = "OBJECT"; // OBJECT//FUNCTION
154 this.properties = [];
163 this.inheritsFrom = [];
164 this.childClasses = [];
166 this.comment = new DocComment();
167 this.comment.isUserComment = false;
172 serialize : function() {
174 for (var p in this) {
180 for (var i in keys) {
181 if (typeof this[keys[i]] == "function") continue;
182 out += " " +keys[i]+" => "+
184 (typeof(this[keys[i]]) != "object") ?
186 "[" +typeof(this[keys[i]])+"]"
190 return "\n{\n" + out + "}\n";
194 var clone = new Symbol();
195 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
196 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
203 //__defineSetter__("name",
204 setName : function(n) {
205 n = n.replace(/^_global_[.#-]/, "");
206 n = n.replace(/\.prototype\.?/g, '#');
207 n = n.replace(/#$/g, '');
209 this.name = n; // real!
212 //__defineGetter__("name",
213 getName : function() { return this._name; },
215 //__defineSetter__("params",
216 setParams :function(v) {
217 for (var i = 0, l = v.length; i < l; i++) {
218 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
219 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
220 this._params[i] = new DocTag(
221 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
224 this._params[i] = v[i];
227 this.params = this._params;
232 //__defineGetter__("params",
233 getParams : function() { return this._params; },
238 /** Object[] */ params,
240 /** DocComment */ comment
242 this.$args = arguments;
243 //println("Symbol created: " + isa + ":" + name);
245 this.alias = this.getName();
246 this.setParams(params);
247 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
248 this.comment = comment || new DocComment("");
249 this.srcFile = Symbol.srcFile;
253 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
257 //if (typeof PluginManager != "undefined") {
258 // PluginManager.run("onSymbol", this);
262 setTags : function() {
264 var authors = this.comment.getTag("author");
265 if (authors.length) {
266 this.author = authors.map(function($){return $.desc;}).join(", ");
270 assert("testing Symbol");
272 requires("../lib/JSDOC/DocComment.js");
273 requires("../frame/String.js");
274 requires("../lib/JSDOC/DocTag.js");
276 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
277 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
280 var mth = this.comment.getTag("method");
282 this.isa = "FUNCTION";
285 var descs = this.comment.getTag("desc");
287 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
291 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
292 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
296 if (this.is("FILE")) {
297 if (!this.alias) this.alias = this.srcFile;
299 var overviews = this.comment.getTag("overview");
300 if (overviews.length) {
301 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
306 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
307 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
311 var sinces = this.comment.getTag("since");
313 this.since = sinces.map(function($){return $.desc;}).join(", ");
317 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
318 assertEqual(sym.since, "1.01", "@since tag, description is found.");
322 if (this.comment.getTag("constant").length) {
323 this.isConstant = true;
328 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
329 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
333 var versions = this.comment.getTag("version");
334 if (versions.length) {
335 this.version = versions.map(function($){return $.desc;}).join(", ");
339 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
340 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
344 var deprecateds = this.comment.getTag("deprecated");
345 if (deprecateds.length) {
346 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
350 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
351 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
355 var examples = this.comment.getTag("example");
356 if (examples.length) {
357 this.example = examples[0];
361 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
362 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
366 var sees = this.comment.getTag("see");
368 var thisSee = this.see;
369 sees.map(function($){thisSee.push($.desc);});
373 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
374 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
378 var classes = this.comment.getTag("class");
379 if (classes.length) {
380 this.isa = "CONSTRUCTOR";
381 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
382 if (!this.classDesc) {
383 this.classDesc = this.desc;
390 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
391 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
392 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
396 var namespaces = this.comment.getTag("namespace");
397 if (namespaces.length) {
398 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
399 this.isNamespace = true;
403 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
404 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
408 var params = this.comment.getTag("param");
410 // user-defined params overwrite those with same name defined by the parser
411 var thisParams = this.getParams();
413 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
414 this.setParams(params);
416 else { // need to overlay these user-defined params on to existing parser-defined params
417 for (var i = 0, l = params.length; i < l; i++) {
419 if (params[i].type) thisParams[i].type = params[i].type;
420 thisParams[i].name = params[i].name;
421 thisParams[i].desc = params[i].desc;
422 thisParams[i].isOptional = params[i].isOptional;
423 thisParams[i].defaultValue = params[i].defaultValue;
425 else thisParams[i] = params[i];
431 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
432 assertEqual(sym.params.length, 1, "parser defined param is found.");
434 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
435 assertEqual(sym.params.length, 1, "user defined param is found.");
436 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
437 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
439 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
440 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
441 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
442 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
444 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
445 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
446 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
447 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
449 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
450 assertEqual(sym.params.length, 1, "user defined param with description is found.");
451 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
455 if (this.comment.getTag("constructor").length) {
456 this.isa = "CONSTRUCTOR";
460 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
461 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
465 if (this.comment.getTag("static").length) {
466 this.isStatic = true;
467 if (this.isa == "CONSTRUCTOR") {
468 this.isNamespace = true;
473 if (this.comment.getTag("singleton").length) {
474 this.isStatic = true;
475 //if (this.isa == "CONSTRUCTOR") {
476 // this.isNamespace = true;
483 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
484 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
485 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
489 if (this.comment.getTag("inner").length) {
491 this.isStatic = false;
495 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
496 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
497 assertEqual(sym.isInner, true, "@inner makes isInner true.");
501 if (this.comment.getTag("field").length) {
506 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
507 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
511 if (this.comment.getTag("function").length) {
512 this.isa = "FUNCTION";
516 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
517 // change a property to a function..
518 this.isa = "FUNCTION";
523 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
524 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
528 var events = this.comment.getTag("event");
530 this.isa = "FUNCTION";
535 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
536 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
537 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
541 var names = this.comment.getTag("name");
543 this.setName(names[0].desc);
551 var properties = this.comment.getTag("property");
552 if (properties.length) {
553 thisProperties = this.properties;
554 for (var i = 0; i < properties.length; i++) {
555 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
556 // TODO: shouldn't the following happen in the addProperty method of Symbol?
557 property.name = properties[i].name;
558 property.memberOf = this.alias;
559 if (properties[i].type) property.type = properties[i].type;
560 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
561 this.addProperty(property);
562 imports.Parser.Parser.addSymbol(property);
567 var conf = this.comment.getTag("cfg");
569 for (var i = 0; i < conf.length; i++) {
570 this.addConfig(conf[i]);
579 var returns = this.comment.getTag("return");
580 if (returns.length) { // there can be many return tags in a single doclet
581 this.returns = returns;
582 this.type = returns.map(function($){return $.type}).join(", ");
590 this.exceptions = this.comment.getTag("throws");
597 var requires = this.comment.getTag("requires");
598 if (requires.length) {
599 this.requires = requires.map(function($){return $.desc});
607 var types = this.comment.getTag("type");
609 this.type = types[0].desc; //multiple type tags are ignored
617 if (this.comment.getTag("private").length || this.isInner) {
618 this.isPrivate = true;
622 if (this.comment.getTag("ignore").length) {
623 this.isIgnored = true;
630 // @inherits ... as ...
631 var inherits = this.comment.getTag("inherits");
632 if (inherits.length) {
633 for (var i = 0; i < inherits.length; i++) {
634 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
635 var inAlias = RegExp.$1;
636 var inAs = RegExp.$2 || inAlias;
638 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
641 inAs = inAs.replace(/\.prototype\.?/g, "#");
642 inAs = inAs.replace(/^this\.?/, "#");
645 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
647 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
650 inAs = this.alias + joiner + inAs;
653 this.inherits.push({alias: inAlias, as: inAs});
662 this.augments = this.comment.getTag("augments");
665 if (this.comment.getTag("extends")) {
666 this.augments = this.comment.getTag("extends");
671 var defaults = this.comment.getTag("default");
672 if (defaults.length) {
673 if (this.is("OBJECT")) {
674 this.defaultValue = defaults[0].desc;
683 var memberOfs = this.comment.getTag("memberOf");
684 if (memberOfs.length) {
685 this.memberOf = memberOfs[0].desc;
686 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
694 if (this.comment.getTag("public").length) {
695 this.isPrivate = false;
703 is : function(what) {
704 return this.isa === what;
707 isBuiltin : function() {
708 return SymbolSet.isBuiltin(this.alias);
711 setType : function(/**String*/comment, /**Boolean*/overwrite) {
712 if (!overwrite && this.type) return;
713 var typeComment = DocComment.unwrapComment(comment);
714 this.type = typeComment;
717 inherit : function(symbol) {
718 if (!this.hasMember(symbol.name) && !symbol.isInner) {
719 if (symbol.is("FUNCTION"))
720 this.methods.push(symbol);
721 else if (symbol.is("OBJECT"))
722 this.properties.push(symbol);
726 hasMember : function(name) {
727 return (this.hasMethod(name) || this.hasProperty(name));
730 addMember : function(symbol) {
731 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
733 if (symbol.comment.getTag("cfg").length == 1) {
734 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
735 this.addConfig(symbol.comment.getTag("cfg")[0]);
739 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
740 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
743 hasMethod : function(name) {
744 var thisMethods = this.methods;
745 for (var i = 0, l = thisMethods.length; i < l; i++) {
746 if (thisMethods[i].name == name) return true;
747 if (thisMethods[i].alias == name) return true;
752 addMethod : function(symbol) {
753 var methodAlias = symbol.alias;
754 var thisMethods = this.methods;
755 for (var i = 0, l = thisMethods.length; i < l; i++) {
756 if (thisMethods[i].alias == methodAlias) {
757 thisMethods[i] = symbol; // overwriting previous method
761 thisMethods.push(symbol); // new method with this alias
764 hasProperty : function(name) {
765 var thisProperties = this.properties;
766 for (var i = 0, l = thisProperties.length; i < l; i++) {
767 if (thisProperties[i].name == name) return true;
768 if (thisProperties[i].alias == name) return true;
773 addProperty : function(symbol) {
774 var propertyAlias = symbol.alias;
775 var thisProperties = this.properties;
776 for (var i = 0, l = thisProperties.length; i < l; i++) {
777 if (thisProperties[i].alias == propertyAlias) {
778 thisProperties[i] = symbol; // overwriting previous property
783 thisProperties.push(symbol); // new property with this alias
786 addDocTag : function(docTag)
788 this.comment.tags.push(docTag);
789 if (docTag.title == 'cfg') {
790 this.addConfig(docTag);
795 addConfig : function(docTag)
797 if (typeof(docTag['memberOf']) == 'undefined') {
798 // remove prototype data...
799 //var a = this.alias.split('#')[0];
800 //docTag.memberOf = a;
801 docTag.memberOf = this.alias;
803 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
804 this.cfgs[docTag.name] = docTag;
808 configToArray: function()
811 for(var ci in this.cfgs) {
812 // dont show hidden!!
813 if (this.cfgs[ci].desc.match(/@hide/)) {
816 r.push(this.cfgs[ci]);
824 * Elements that are not serialized
828 '$args' // not needed AFAIK
831 Symbol.srcFile = ""; //running reference to the current file being parsed
834 Symbol.fromDump = function(t)
836 var ns = new Symbol();
838 if (typeof(ns[i]) == "undefined") {
839 println("ERR:no default for Symbol:"+ i);