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);
28 switch (typeof(this[i])) {
34 ret[i] = this[i]; continue;
36 print("unknown type:" + typeof(this[i]));
47 this.defaultValue = "";
55 this.comment = { isUserComment: false };
56 //this.defaultValue = null;
63 this.inheritsFrom = [];
64 this.isa = "OBJECT"; // OBJECT//FUNCTION
66 this.isConstant = false;
67 this.isIgnored = false;
69 this.isNamespace = false;
70 this.isPrivate = false;
71 this.isStatic = false;
84 this.childClasses = [];
89 serialize : function() {
98 if (typeof this[keys[i]] == "function") continue;
99 out += " " +keys[i]+" => "+
101 (typeof(this[keys[i]]) != "object") ?
103 "[" +typeof(this[keys[i]])+"]"
107 return "\n{\n" + out + "}\n";
111 var clone = new Symbol();
112 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
113 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
120 //__defineSetter__("name",
121 setName : function(n) {
122 n = n.replace(/^_global_[.#-]/, "");
123 n = n.replace(/\.prototype\.?/g, '#');
124 n = n.replace(/#$/g, '');
126 this.name = n; // real!
129 //__defineGetter__("name",
130 getName : function() { return this._name; },
132 //__defineSetter__("params",
133 setParams :function(v) {
134 for (var i = 0, l = v.length; i < l; i++) {
135 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
136 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
137 this._params[i] = new DocTag(
138 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
141 this._params[i] = v[i];
144 this.params = this._params;
149 //__defineGetter__("params",
150 getParams : function() { return this._params; },
155 /** Object[] */ params,
157 /** DocComment */ comment
159 this.$args = arguments;
160 //println("Symbol created: " + isa + ":" + name);
162 this.alias = this.getName();
163 this.setParams(params);
164 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
165 this.comment = comment || new DocComment("");
166 this.srcFile = Symbol.srcFile;
170 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
174 //if (typeof PluginManager != "undefined") {
175 // PluginManager.run("onSymbol", this);
179 setTags : function() {
181 var authors = this.comment.getTag("author");
182 if (authors.length) {
183 this.author = authors.map(function($){return $.desc;}).join(", ");
187 assert("testing Symbol");
189 requires("../lib/JSDOC/DocComment.js");
190 requires("../frame/String.js");
191 requires("../lib/JSDOC/DocTag.js");
193 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
194 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
197 var mth = this.comment.getTag("method");
199 this.isa = "FUNCTION";
202 var descs = this.comment.getTag("desc");
204 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
208 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
209 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
213 if (this.is("FILE")) {
214 if (!this.alias) this.alias = this.srcFile;
216 var overviews = this.comment.getTag("overview");
217 if (overviews.length) {
218 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
223 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
224 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
228 var sinces = this.comment.getTag("since");
230 this.since = sinces.map(function($){return $.desc;}).join(", ");
234 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
235 assertEqual(sym.since, "1.01", "@since tag, description is found.");
239 if (this.comment.getTag("constant").length) {
240 this.isConstant = true;
245 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
246 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
250 var versions = this.comment.getTag("version");
251 if (versions.length) {
252 this.version = versions.map(function($){return $.desc;}).join(", ");
256 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
257 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
261 var deprecateds = this.comment.getTag("deprecated");
262 if (deprecateds.length) {
263 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
267 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
268 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
272 var examples = this.comment.getTag("example");
273 if (examples.length) {
274 this.example = examples[0];
278 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
279 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
283 var sees = this.comment.getTag("see");
285 var thisSee = this.see;
286 sees.map(function($){thisSee.push($.desc);});
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
291 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
295 var classes = this.comment.getTag("class");
296 if (classes.length) {
297 this.isa = "CONSTRUCTOR";
298 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
299 if (!this.classDesc) {
300 this.classDesc = this.desc;
307 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
308 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
309 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
313 var namespaces = this.comment.getTag("namespace");
314 if (namespaces.length) {
315 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
316 this.isNamespace = true;
320 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
321 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
325 var params = this.comment.getTag("param");
327 // user-defined params overwrite those with same name defined by the parser
328 var thisParams = this.getParams();
330 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
331 this.setParams(params);
333 else { // need to overlay these user-defined params on to existing parser-defined params
334 for (var i = 0, l = params.length; i < l; i++) {
336 if (params[i].type) thisParams[i].type = params[i].type;
337 thisParams[i].name = params[i].name;
338 thisParams[i].desc = params[i].desc;
339 thisParams[i].isOptional = params[i].isOptional;
340 thisParams[i].defaultValue = params[i].defaultValue;
342 else thisParams[i] = params[i];
348 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
349 assertEqual(sym.params.length, 1, "parser defined param is found.");
351 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
352 assertEqual(sym.params.length, 1, "user defined param is found.");
353 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
354 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
356 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
357 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
358 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
359 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
361 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
362 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
363 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
364 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
366 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
367 assertEqual(sym.params.length, 1, "user defined param with description is found.");
368 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
372 if (this.comment.getTag("constructor").length) {
373 this.isa = "CONSTRUCTOR";
377 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
378 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
382 if (this.comment.getTag("static").length) {
383 this.isStatic = true;
384 if (this.isa == "CONSTRUCTOR") {
385 this.isNamespace = true;
390 if (this.comment.getTag("singleton").length) {
391 this.isStatic = true;
392 //if (this.isa == "CONSTRUCTOR") {
393 // this.isNamespace = true;
400 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
401 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
402 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
406 if (this.comment.getTag("inner").length) {
408 this.isStatic = false;
412 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
413 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
414 assertEqual(sym.isInner, true, "@inner makes isInner true.");
418 if (this.comment.getTag("field").length) {
423 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
424 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
428 if (this.comment.getTag("function").length) {
429 this.isa = "FUNCTION";
433 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
434 // change a property to a function..
435 this.isa = "FUNCTION";
440 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
441 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
445 var events = this.comment.getTag("event");
447 this.isa = "FUNCTION";
452 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
453 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
454 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
458 var names = this.comment.getTag("name");
460 this.setName(names[0].desc);
468 var properties = this.comment.getTag("property");
469 if (properties.length) {
470 thisProperties = this.properties;
471 for (var i = 0; i < properties.length; i++) {
472 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
473 // TODO: shouldn't the following happen in the addProperty method of Symbol?
474 property.name = properties[i].name;
475 property.memberOf = this.alias;
476 if (properties[i].type) property.type = properties[i].type;
477 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
478 this.addProperty(property);
479 imports.Parser.Parser.addSymbol(property);
484 var conf = this.comment.getTag("cfg");
486 for (var i = 0; i < conf.length; i++) {
487 this.addConfig(conf[i]);
496 var returns = this.comment.getTag("return");
497 if (returns.length) { // there can be many return tags in a single doclet
498 this.returns = returns;
499 this.type = returns.map(function($){return $.type}).join(", ");
507 this.exceptions = this.comment.getTag("throws");
514 var requires = this.comment.getTag("requires");
515 if (requires.length) {
516 this.requires = requires.map(function($){return $.desc});
524 var types = this.comment.getTag("type");
526 this.type = types[0].desc; //multiple type tags are ignored
534 if (this.comment.getTag("private").length || this.isInner) {
535 this.isPrivate = true;
539 if (this.comment.getTag("ignore").length) {
540 this.isIgnored = true;
547 // @inherits ... as ...
548 var inherits = this.comment.getTag("inherits");
549 if (inherits.length) {
550 for (var i = 0; i < inherits.length; i++) {
551 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
552 var inAlias = RegExp.$1;
553 var inAs = RegExp.$2 || inAlias;
555 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
558 inAs = inAs.replace(/\.prototype\.?/g, "#");
559 inAs = inAs.replace(/^this\.?/, "#");
562 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
564 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
567 inAs = this.alias + joiner + inAs;
570 this.inherits.push({alias: inAlias, as: inAs});
579 this.augments = this.comment.getTag("augments");
582 if (this.comment.getTag("extends")) {
583 this.augments = this.comment.getTag("extends");
588 var defaults = this.comment.getTag("default");
589 if (defaults.length) {
590 if (this.is("OBJECT")) {
591 this.defaultValue = defaults[0].desc;
600 var memberOfs = this.comment.getTag("memberOf");
601 if (memberOfs.length) {
602 this.memberOf = memberOfs[0].desc;
603 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
611 if (this.comment.getTag("public").length) {
612 this.isPrivate = false;
620 is : function(what) {
621 return this.isa === what;
624 isBuiltin : function() {
625 return SymbolSet.isBuiltin(this.alias);
628 setType : function(/**String*/comment, /**Boolean*/overwrite) {
629 if (!overwrite && this.type) return;
630 var typeComment = DocComment.unwrapComment(comment);
631 this.type = typeComment;
634 inherit : function(symbol) {
635 if (!this.hasMember(symbol.name) && !symbol.isInner) {
636 if (symbol.is("FUNCTION"))
637 this.methods.push(symbol);
638 else if (symbol.is("OBJECT"))
639 this.properties.push(symbol);
643 hasMember : function(name) {
644 return (this.hasMethod(name) || this.hasProperty(name));
647 addMember : function(symbol) {
648 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
650 if (symbol.comment.getTag("cfg").length == 1) {
651 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
652 this.addConfig(symbol.comment.getTag("cfg")[0]);
656 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
657 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
660 hasMethod : function(name) {
661 var thisMethods = this.methods;
662 for (var i = 0, l = thisMethods.length; i < l; i++) {
663 if (thisMethods[i].name == name) return true;
664 if (thisMethods[i].alias == name) return true;
669 addMethod : function(symbol) {
670 var methodAlias = symbol.alias;
671 var thisMethods = this.methods;
672 for (var i = 0, l = thisMethods.length; i < l; i++) {
673 if (thisMethods[i].alias == methodAlias) {
674 thisMethods[i] = symbol; // overwriting previous method
678 thisMethods.push(symbol); // new method with this alias
681 hasProperty : function(name) {
682 var thisProperties = this.properties;
683 for (var i = 0, l = thisProperties.length; i < l; i++) {
684 if (thisProperties[i].name == name) return true;
685 if (thisProperties[i].alias == name) return true;
690 addProperty : function(symbol) {
691 var propertyAlias = symbol.alias;
692 var thisProperties = this.properties;
693 for (var i = 0, l = thisProperties.length; i < l; i++) {
694 if (thisProperties[i].alias == propertyAlias) {
695 thisProperties[i] = symbol; // overwriting previous property
700 thisProperties.push(symbol); // new property with this alias
703 addDocTag : function(docTag)
705 this.comment.tags.push(docTag);
706 if (docTag.title == 'cfg') {
707 this.addConfig(docTag);
712 addConfig : function(docTag)
714 if (typeof(docTag['memberOf']) == 'undefined') {
715 // remove prototype data...
716 //var a = this.alias.split('#')[0];
717 //docTag.memberOf = a;
718 docTag.memberOf = this.alias;
720 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
721 this.cfgs[docTag.name] = docTag;
725 configToArray: function()
728 for(var ci in this.cfgs) {
729 // dont show hidden!!
730 if (this.cfgs[ci].desc.match(/@hide/)) {
733 r.push(this.cfgs[ci]);
744 Symbol.srcFile = ""; //running reference to the current file being parsed
747 Symbol.fromDump = function(t)
749 var ns = new Symbol();
751 if (typeof(ns[i]) == "undefined") {
752 println("ERR:no default for Symbol:"+ i);