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;
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());
31 this.defaultValue = "";
39 this.comment = { isUserComment: false };
40 //this.defaultValue = null;
47 this.inheritsFrom = [];
48 this.isa = "OBJECT"; // OBJECT//FUNCTION
50 this.isConstant = false;
51 this.isIgnored = false;
53 this.isNamespace = false;
54 this.isPrivate = false;
55 this.isStatic = false;
68 this.childClasses = [];
73 serialize : function() {
82 if (typeof this[keys[i]] == "function") continue;
83 out += " " +keys[i]+" => "+
85 (typeof(this[keys[i]]) != "object") ?
87 "[" +typeof(this[keys[i]])+"]"
91 return "\n{\n" + out + "}\n";
95 var clone = new Symbol();
96 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
97 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
104 //__defineSetter__("name",
105 setName : function(n) {
106 n = n.replace(/^_global_[.#-]/, "");
107 n = n.replace(/\.prototype\.?/g, '#');
108 n = n.replace(/#$/g, '');
110 this.name = n; // real!
113 //__defineGetter__("name",
114 getName : function() { return this._name; },
116 //__defineSetter__("params",
117 setParams :function(v) {
118 for (var i = 0, l = v.length; i < l; i++) {
119 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
120 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
121 this._params[i] = new DocTag(
122 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
125 this._params[i] = v[i];
128 this.params = this._params;
133 //__defineGetter__("params",
134 getParams : function() { return this._params; },
139 /** Object[] */ params,
141 /** DocComment */ comment
143 this.$args = arguments;
144 //println("Symbol created: " + isa + ":" + name);
146 this.alias = this.getName();
147 this.setParams(params);
148 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
149 this.comment = comment || new DocComment("");
150 this.srcFile = Symbol.srcFile;
154 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
158 //if (typeof PluginManager != "undefined") {
159 // PluginManager.run("onSymbol", this);
163 setTags : function() {
165 var authors = this.comment.getTag("author");
166 if (authors.length) {
167 this.author = authors.map(function($){return $.desc;}).join(", ");
171 assert("testing Symbol");
173 requires("../lib/JSDOC/DocComment.js");
174 requires("../frame/String.js");
175 requires("../lib/JSDOC/DocTag.js");
177 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
178 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
181 var mth = this.comment.getTag("method");
183 this.isa = "FUNCTION";
186 var descs = this.comment.getTag("desc");
188 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
192 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
193 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
197 if (this.is("FILE")) {
198 if (!this.alias) this.alias = this.srcFile;
200 var overviews = this.comment.getTag("overview");
201 if (overviews.length) {
202 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
207 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
208 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
212 var sinces = this.comment.getTag("since");
214 this.since = sinces.map(function($){return $.desc;}).join(", ");
218 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
219 assertEqual(sym.since, "1.01", "@since tag, description is found.");
223 if (this.comment.getTag("constant").length) {
224 this.isConstant = true;
229 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
230 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
234 var versions = this.comment.getTag("version");
235 if (versions.length) {
236 this.version = versions.map(function($){return $.desc;}).join(", ");
240 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
241 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
245 var deprecateds = this.comment.getTag("deprecated");
246 if (deprecateds.length) {
247 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
251 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
252 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
256 var examples = this.comment.getTag("example");
257 if (examples.length) {
258 this.example = examples[0];
262 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
263 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
267 var sees = this.comment.getTag("see");
269 var thisSee = this.see;
270 sees.map(function($){thisSee.push($.desc);});
274 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
275 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
279 var classes = this.comment.getTag("class");
280 if (classes.length) {
281 this.isa = "CONSTRUCTOR";
282 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
283 if (!this.classDesc) {
284 this.classDesc = this.desc;
291 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
292 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
293 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
297 var namespaces = this.comment.getTag("namespace");
298 if (namespaces.length) {
299 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
300 this.isNamespace = true;
304 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
305 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
309 var params = this.comment.getTag("param");
311 // user-defined params overwrite those with same name defined by the parser
312 var thisParams = this.getParams();
314 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
315 this.setParams(params);
317 else { // need to overlay these user-defined params on to existing parser-defined params
318 for (var i = 0, l = params.length; i < l; i++) {
320 if (params[i].type) thisParams[i].type = params[i].type;
321 thisParams[i].name = params[i].name;
322 thisParams[i].desc = params[i].desc;
323 thisParams[i].isOptional = params[i].isOptional;
324 thisParams[i].defaultValue = params[i].defaultValue;
326 else thisParams[i] = params[i];
332 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
333 assertEqual(sym.params.length, 1, "parser defined param is found.");
335 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
336 assertEqual(sym.params.length, 1, "user defined param is found.");
337 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
338 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
340 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
341 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
342 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
343 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
345 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
346 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
347 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
348 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
350 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
351 assertEqual(sym.params.length, 1, "user defined param with description is found.");
352 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
356 if (this.comment.getTag("constructor").length) {
357 this.isa = "CONSTRUCTOR";
361 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
362 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
366 if (this.comment.getTag("static").length) {
367 this.isStatic = true;
368 if (this.isa == "CONSTRUCTOR") {
369 this.isNamespace = true;
374 if (this.comment.getTag("singleton").length) {
375 this.isStatic = true;
376 //if (this.isa == "CONSTRUCTOR") {
377 // this.isNamespace = true;
384 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
385 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
386 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
390 if (this.comment.getTag("inner").length) {
392 this.isStatic = false;
396 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
397 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
398 assertEqual(sym.isInner, true, "@inner makes isInner true.");
402 if (this.comment.getTag("field").length) {
407 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
408 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
412 if (this.comment.getTag("function").length) {
413 this.isa = "FUNCTION";
417 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
418 // change a property to a function..
419 this.isa = "FUNCTION";
424 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
425 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
429 var events = this.comment.getTag("event");
431 this.isa = "FUNCTION";
436 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
437 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
438 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
442 var names = this.comment.getTag("name");
444 this.setName(names[0].desc);
452 var properties = this.comment.getTag("property");
453 if (properties.length) {
454 thisProperties = this.properties;
455 for (var i = 0; i < properties.length; i++) {
456 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
457 // TODO: shouldn't the following happen in the addProperty method of Symbol?
458 property.name = properties[i].name;
459 property.memberOf = this.alias;
460 if (properties[i].type) property.type = properties[i].type;
461 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
462 this.addProperty(property);
463 Parser.addSymbol(property);
468 var conf = this.comment.getTag("cfg");
470 for (var i = 0; i < conf.length; i++) {
471 this.addConfig(conf[i]);
480 var returns = this.comment.getTag("return");
481 if (returns.length) { // there can be many return tags in a single doclet
482 this.returns = returns;
483 this.type = returns.map(function($){return $.type}).join(", ");
491 this.exceptions = this.comment.getTag("throws");
498 var requires = this.comment.getTag("requires");
499 if (requires.length) {
500 this.requires = requires.map(function($){return $.desc});
508 var types = this.comment.getTag("type");
510 this.type = types[0].desc; //multiple type tags are ignored
518 if (this.comment.getTag("private").length || this.isInner) {
519 this.isPrivate = true;
523 if (this.comment.getTag("ignore").length) {
524 this.isIgnored = true;
531 // @inherits ... as ...
532 var inherits = this.comment.getTag("inherits");
533 if (inherits.length) {
534 for (var i = 0; i < inherits.length; i++) {
535 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
536 var inAlias = RegExp.$1;
537 var inAs = RegExp.$2 || inAlias;
539 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
542 inAs = inAs.replace(/\.prototype\.?/g, "#");
543 inAs = inAs.replace(/^this\.?/, "#");
546 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
548 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
551 inAs = this.alias + joiner + inAs;
554 this.inherits.push({alias: inAlias, as: inAs});
563 this.augments = this.comment.getTag("augments");
566 if (this.comment.getTag("extends")) {
567 this.augments = this.comment.getTag("extends");
572 var defaults = this.comment.getTag("default");
573 if (defaults.length) {
574 if (this.is("OBJECT")) {
575 this.defaultValue = defaults[0].desc;
584 var memberOfs = this.comment.getTag("memberOf");
585 if (memberOfs.length) {
586 this.memberOf = memberOfs[0].desc;
587 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
595 if (this.comment.getTag("public").length) {
596 this.isPrivate = false;
604 is : function(what) {
605 return this.isa === what;
608 isBuiltin : function() {
609 return SymbolSet.isBuiltin(this.alias);
612 setType : function(/**String*/comment, /**Boolean*/overwrite) {
613 if (!overwrite && this.type) return;
614 var typeComment = DocComment.unwrapComment(comment);
615 this.type = typeComment;
618 inherit : function(symbol) {
619 if (!this.hasMember(symbol.name) && !symbol.isInner) {
620 if (symbol.is("FUNCTION"))
621 this.methods.push(symbol);
622 else if (symbol.is("OBJECT"))
623 this.properties.push(symbol);
627 hasMember : function(name) {
628 return (this.hasMethod(name) || this.hasProperty(name));
631 addMember : function(symbol) {
632 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
634 if (symbol.comment.getTag("cfg").length == 1) {
635 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
636 this.addConfig(symbol.comment.getTag("cfg")[0]);
640 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
641 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
644 hasMethod : function(name) {
645 var thisMethods = this.methods;
646 for (var i = 0, l = thisMethods.length; i < l; i++) {
647 if (thisMethods[i].name == name) return true;
648 if (thisMethods[i].alias == name) return true;
653 addMethod : function(symbol) {
654 var methodAlias = symbol.alias;
655 var thisMethods = this.methods;
656 for (var i = 0, l = thisMethods.length; i < l; i++) {
657 if (thisMethods[i].alias == methodAlias) {
658 thisMethods[i] = symbol; // overwriting previous method
662 thisMethods.push(symbol); // new method with this alias
665 hasProperty : function(name) {
666 var thisProperties = this.properties;
667 for (var i = 0, l = thisProperties.length; i < l; i++) {
668 if (thisProperties[i].name == name) return true;
669 if (thisProperties[i].alias == name) return true;
674 addProperty : function(symbol) {
675 var propertyAlias = symbol.alias;
676 var thisProperties = this.properties;
677 for (var i = 0, l = thisProperties.length; i < l; i++) {
678 if (thisProperties[i].alias == propertyAlias) {
679 thisProperties[i] = symbol; // overwriting previous property
684 thisProperties.push(symbol); // new property with this alias
687 addDocTag : function(docTag)
689 this.comment.tags.push(docTag);
690 if (docTag.title == 'cfg') {
691 this.addConfig(docTag);
696 addConfig : function(docTag)
698 if (typeof(docTag['memberOf']) == 'undefined') {
699 // remove prototype data...
700 //var a = this.alias.split('#')[0];
701 //docTag.memberOf = a;
702 docTag.memberOf = this.alias;
704 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
705 this.cfgs[docTag.name] = docTag;
709 configToArray: function()
712 for(var ci in this.cfgs) {
713 // dont show hidden!!
714 if (this.cfgs[ci].desc.match(/@hide/)) {
717 r.push(this.cfgs[ci]);
728 Symbol.srcFile = ""; //running reference to the current file being parsed
731 Symbol.fromDump = function(t)
733 var ns = new Symbol();
735 if (typeof(ns[i]) == "undefined") {
736 println("ERR:no default for Symbol:"+ i);