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 return toQDump(t, 'Symbol.fromDump({', '})', {});//new Symbol());
32 this.defaultValue = "";
40 this.comment = { isUserComment: false };
41 //this.defaultValue = null;
48 this.inheritsFrom = [];
49 this.isa = "OBJECT"; // OBJECT//FUNCTION
51 this.isConstant = false;
52 this.isIgnored = false;
54 this.isNamespace = false;
55 this.isPrivate = false;
56 this.isStatic = false;
69 this.childClasses = [];
74 serialize : function() {
83 if (typeof this[keys[i]] == "function") continue;
84 out += " " +keys[i]+" => "+
86 (typeof(this[keys[i]]) != "object") ?
88 "[" +typeof(this[keys[i]])+"]"
92 return "\n{\n" + out + "}\n";
96 var clone = new Symbol();
97 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
98 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
105 //__defineSetter__("name",
106 setName : function(n) {
107 n = n.replace(/^_global_[.#-]/, "");
108 n = n.replace(/\.prototype\.?/g, '#');
109 n = n.replace(/#$/g, '');
111 this.name = n; // real!
114 //__defineGetter__("name",
115 getName : function() { return this._name; },
117 //__defineSetter__("params",
118 setParams :function(v) {
119 for (var i = 0, l = v.length; i < l; i++) {
120 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
121 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
122 this._params[i] = new DocTag(
123 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
126 this._params[i] = v[i];
129 this.params = this._params;
134 //__defineGetter__("params",
135 getParams : function() { return this._params; },
140 /** Object[] */ params,
142 /** DocComment */ comment
144 this.$args = arguments;
145 //println("Symbol created: " + isa + ":" + name);
147 this.alias = this.getName();
148 this.setParams(params);
149 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
150 this.comment = comment || new DocComment("");
151 this.srcFile = Symbol.srcFile;
155 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
159 //if (typeof PluginManager != "undefined") {
160 // PluginManager.run("onSymbol", this);
164 setTags : function() {
166 var authors = this.comment.getTag("author");
167 if (authors.length) {
168 this.author = authors.map(function($){return $.desc;}).join(", ");
172 assert("testing Symbol");
174 requires("../lib/JSDOC/DocComment.js");
175 requires("../frame/String.js");
176 requires("../lib/JSDOC/DocTag.js");
178 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
179 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
182 var mth = this.comment.getTag("method");
184 this.isa = "FUNCTION";
187 var descs = this.comment.getTag("desc");
189 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
193 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
194 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
198 if (this.is("FILE")) {
199 if (!this.alias) this.alias = this.srcFile;
201 var overviews = this.comment.getTag("overview");
202 if (overviews.length) {
203 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
208 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
209 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
213 var sinces = this.comment.getTag("since");
215 this.since = sinces.map(function($){return $.desc;}).join(", ");
219 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
220 assertEqual(sym.since, "1.01", "@since tag, description is found.");
224 if (this.comment.getTag("constant").length) {
225 this.isConstant = true;
230 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
231 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
235 var versions = this.comment.getTag("version");
236 if (versions.length) {
237 this.version = versions.map(function($){return $.desc;}).join(", ");
241 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
242 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
246 var deprecateds = this.comment.getTag("deprecated");
247 if (deprecateds.length) {
248 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
252 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
253 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
257 var examples = this.comment.getTag("example");
258 if (examples.length) {
259 this.example = examples[0];
263 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
264 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
268 var sees = this.comment.getTag("see");
270 var thisSee = this.see;
271 sees.map(function($){thisSee.push($.desc);});
275 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
276 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
280 var classes = this.comment.getTag("class");
281 if (classes.length) {
282 this.isa = "CONSTRUCTOR";
283 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
284 if (!this.classDesc) {
285 this.classDesc = this.desc;
292 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
293 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
294 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
298 var namespaces = this.comment.getTag("namespace");
299 if (namespaces.length) {
300 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
301 this.isNamespace = true;
305 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
306 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
310 var params = this.comment.getTag("param");
312 // user-defined params overwrite those with same name defined by the parser
313 var thisParams = this.getParams();
315 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
316 this.setParams(params);
318 else { // need to overlay these user-defined params on to existing parser-defined params
319 for (var i = 0, l = params.length; i < l; i++) {
321 if (params[i].type) thisParams[i].type = params[i].type;
322 thisParams[i].name = params[i].name;
323 thisParams[i].desc = params[i].desc;
324 thisParams[i].isOptional = params[i].isOptional;
325 thisParams[i].defaultValue = params[i].defaultValue;
327 else thisParams[i] = params[i];
333 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
334 assertEqual(sym.params.length, 1, "parser defined param is found.");
336 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
337 assertEqual(sym.params.length, 1, "user defined param is found.");
338 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
339 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
341 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
342 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
343 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
344 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
346 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
347 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
348 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
349 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
351 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
352 assertEqual(sym.params.length, 1, "user defined param with description is found.");
353 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
357 if (this.comment.getTag("constructor").length) {
358 this.isa = "CONSTRUCTOR";
362 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
363 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
367 if (this.comment.getTag("static").length) {
368 this.isStatic = true;
369 if (this.isa == "CONSTRUCTOR") {
370 this.isNamespace = true;
375 if (this.comment.getTag("singleton").length) {
376 this.isStatic = true;
377 //if (this.isa == "CONSTRUCTOR") {
378 // this.isNamespace = true;
385 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
386 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
387 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
391 if (this.comment.getTag("inner").length) {
393 this.isStatic = false;
397 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
398 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
399 assertEqual(sym.isInner, true, "@inner makes isInner true.");
403 if (this.comment.getTag("field").length) {
408 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
409 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
413 if (this.comment.getTag("function").length) {
414 this.isa = "FUNCTION";
418 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
419 // change a property to a function..
420 this.isa = "FUNCTION";
425 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
426 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
430 var events = this.comment.getTag("event");
432 this.isa = "FUNCTION";
437 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
438 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
439 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
443 var names = this.comment.getTag("name");
445 this.setName(names[0].desc);
453 var properties = this.comment.getTag("property");
454 if (properties.length) {
455 thisProperties = this.properties;
456 for (var i = 0; i < properties.length; i++) {
457 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
458 // TODO: shouldn't the following happen in the addProperty method of Symbol?
459 property.name = properties[i].name;
460 property.memberOf = this.alias;
461 if (properties[i].type) property.type = properties[i].type;
462 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
463 this.addProperty(property);
464 Parser.addSymbol(property);
469 var conf = this.comment.getTag("cfg");
471 for (var i = 0; i < conf.length; i++) {
472 this.addConfig(conf[i]);
481 var returns = this.comment.getTag("return");
482 if (returns.length) { // there can be many return tags in a single doclet
483 this.returns = returns;
484 this.type = returns.map(function($){return $.type}).join(", ");
492 this.exceptions = this.comment.getTag("throws");
499 var requires = this.comment.getTag("requires");
500 if (requires.length) {
501 this.requires = requires.map(function($){return $.desc});
509 var types = this.comment.getTag("type");
511 this.type = types[0].desc; //multiple type tags are ignored
519 if (this.comment.getTag("private").length || this.isInner) {
520 this.isPrivate = true;
524 if (this.comment.getTag("ignore").length) {
525 this.isIgnored = true;
532 // @inherits ... as ...
533 var inherits = this.comment.getTag("inherits");
534 if (inherits.length) {
535 for (var i = 0; i < inherits.length; i++) {
536 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
537 var inAlias = RegExp.$1;
538 var inAs = RegExp.$2 || inAlias;
540 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
543 inAs = inAs.replace(/\.prototype\.?/g, "#");
544 inAs = inAs.replace(/^this\.?/, "#");
547 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
549 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
552 inAs = this.alias + joiner + inAs;
555 this.inherits.push({alias: inAlias, as: inAs});
564 this.augments = this.comment.getTag("augments");
567 if (this.comment.getTag("extends")) {
568 this.augments = this.comment.getTag("extends");
573 var defaults = this.comment.getTag("default");
574 if (defaults.length) {
575 if (this.is("OBJECT")) {
576 this.defaultValue = defaults[0].desc;
585 var memberOfs = this.comment.getTag("memberOf");
586 if (memberOfs.length) {
587 this.memberOf = memberOfs[0].desc;
588 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
596 if (this.comment.getTag("public").length) {
597 this.isPrivate = false;
605 is : function(what) {
606 return this.isa === what;
609 isBuiltin : function() {
610 return SymbolSet.isBuiltin(this.alias);
613 setType : function(/**String*/comment, /**Boolean*/overwrite) {
614 if (!overwrite && this.type) return;
615 var typeComment = DocComment.unwrapComment(comment);
616 this.type = typeComment;
619 inherit : function(symbol) {
620 if (!this.hasMember(symbol.name) && !symbol.isInner) {
621 if (symbol.is("FUNCTION"))
622 this.methods.push(symbol);
623 else if (symbol.is("OBJECT"))
624 this.properties.push(symbol);
628 hasMember : function(name) {
629 return (this.hasMethod(name) || this.hasProperty(name));
632 addMember : function(symbol) {
633 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
635 if (symbol.comment.getTag("cfg").length == 1) {
636 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
637 this.addConfig(symbol.comment.getTag("cfg")[0]);
641 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
642 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
645 hasMethod : function(name) {
646 var thisMethods = this.methods;
647 for (var i = 0, l = thisMethods.length; i < l; i++) {
648 if (thisMethods[i].name == name) return true;
649 if (thisMethods[i].alias == name) return true;
654 addMethod : function(symbol) {
655 var methodAlias = symbol.alias;
656 var thisMethods = this.methods;
657 for (var i = 0, l = thisMethods.length; i < l; i++) {
658 if (thisMethods[i].alias == methodAlias) {
659 thisMethods[i] = symbol; // overwriting previous method
663 thisMethods.push(symbol); // new method with this alias
666 hasProperty : function(name) {
667 var thisProperties = this.properties;
668 for (var i = 0, l = thisProperties.length; i < l; i++) {
669 if (thisProperties[i].name == name) return true;
670 if (thisProperties[i].alias == name) return true;
675 addProperty : function(symbol) {
676 var propertyAlias = symbol.alias;
677 var thisProperties = this.properties;
678 for (var i = 0, l = thisProperties.length; i < l; i++) {
679 if (thisProperties[i].alias == propertyAlias) {
680 thisProperties[i] = symbol; // overwriting previous property
685 thisProperties.push(symbol); // new property with this alias
688 addDocTag : function(docTag)
690 this.comment.tags.push(docTag);
691 if (docTag.title == 'cfg') {
692 this.addConfig(docTag);
697 addConfig : function(docTag)
699 if (typeof(docTag['memberOf']) == 'undefined') {
700 // remove prototype data...
701 //var a = this.alias.split('#')[0];
702 //docTag.memberOf = a;
703 docTag.memberOf = this.alias;
705 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
706 this.cfgs[docTag.name] = docTag;
710 configToArray: function()
713 for(var ci in this.cfgs) {
714 // dont show hidden!!
715 if (this.cfgs[ci].desc.match(/@hide/)) {
718 r.push(this.cfgs[ci]);
729 Symbol.srcFile = ""; //running reference to the current file being parsed
732 Symbol.fromDump = function(t)
734 var ns = new Symbol();
736 if (typeof(ns[i]) == "undefined") {
737 println("ERR:no default for Symbol:"+ i);