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])) {
33 print("object? :" + i);
37 ret[i] = this[i]; continue;
39 print("unknown type:" + typeof(this[i]));
50 this.defaultValue = "";
58 this.comment = { isUserComment: false };
59 //this.defaultValue = null;
66 this.inheritsFrom = [];
67 this.isa = "OBJECT"; // OBJECT//FUNCTION
69 this.isConstant = false;
70 this.isIgnored = false;
72 this.isNamespace = false;
73 this.isPrivate = false;
74 this.isStatic = false;
87 this.childClasses = [];
92 serialize : function() {
100 for (var i in keys) {
101 if (typeof this[keys[i]] == "function") continue;
102 out += " " +keys[i]+" => "+
104 (typeof(this[keys[i]]) != "object") ?
106 "[" +typeof(this[keys[i]])+"]"
110 return "\n{\n" + out + "}\n";
114 var clone = new Symbol();
115 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
116 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
123 //__defineSetter__("name",
124 setName : function(n) {
125 n = n.replace(/^_global_[.#-]/, "");
126 n = n.replace(/\.prototype\.?/g, '#');
127 n = n.replace(/#$/g, '');
129 this.name = n; // real!
132 //__defineGetter__("name",
133 getName : function() { return this._name; },
135 //__defineSetter__("params",
136 setParams :function(v) {
137 for (var i = 0, l = v.length; i < l; i++) {
138 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
139 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
140 this._params[i] = new DocTag(
141 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
144 this._params[i] = v[i];
147 this.params = this._params;
152 //__defineGetter__("params",
153 getParams : function() { return this._params; },
158 /** Object[] */ params,
160 /** DocComment */ comment
162 this.$args = arguments;
163 //println("Symbol created: " + isa + ":" + name);
165 this.alias = this.getName();
166 this.setParams(params);
167 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
168 this.comment = comment || new DocComment("");
169 this.srcFile = Symbol.srcFile;
173 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
177 //if (typeof PluginManager != "undefined") {
178 // PluginManager.run("onSymbol", this);
182 setTags : function() {
184 var authors = this.comment.getTag("author");
185 if (authors.length) {
186 this.author = authors.map(function($){return $.desc;}).join(", ");
190 assert("testing Symbol");
192 requires("../lib/JSDOC/DocComment.js");
193 requires("../frame/String.js");
194 requires("../lib/JSDOC/DocTag.js");
196 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
197 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
200 var mth = this.comment.getTag("method");
202 this.isa = "FUNCTION";
205 var descs = this.comment.getTag("desc");
207 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
211 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
212 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
216 if (this.is("FILE")) {
217 if (!this.alias) this.alias = this.srcFile;
219 var overviews = this.comment.getTag("overview");
220 if (overviews.length) {
221 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
226 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
227 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
231 var sinces = this.comment.getTag("since");
233 this.since = sinces.map(function($){return $.desc;}).join(", ");
237 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
238 assertEqual(sym.since, "1.01", "@since tag, description is found.");
242 if (this.comment.getTag("constant").length) {
243 this.isConstant = true;
248 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
249 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
253 var versions = this.comment.getTag("version");
254 if (versions.length) {
255 this.version = versions.map(function($){return $.desc;}).join(", ");
259 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
260 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
264 var deprecateds = this.comment.getTag("deprecated");
265 if (deprecateds.length) {
266 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
270 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
271 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
275 var examples = this.comment.getTag("example");
276 if (examples.length) {
277 this.example = examples[0];
281 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
282 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
286 var sees = this.comment.getTag("see");
288 var thisSee = this.see;
289 sees.map(function($){thisSee.push($.desc);});
293 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
294 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
298 var classes = this.comment.getTag("class");
299 if (classes.length) {
300 this.isa = "CONSTRUCTOR";
301 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
302 if (!this.classDesc) {
303 this.classDesc = this.desc;
310 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
311 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
312 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
316 var namespaces = this.comment.getTag("namespace");
317 if (namespaces.length) {
318 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
319 this.isNamespace = true;
323 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
324 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
328 var params = this.comment.getTag("param");
330 // user-defined params overwrite those with same name defined by the parser
331 var thisParams = this.getParams();
333 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
334 this.setParams(params);
336 else { // need to overlay these user-defined params on to existing parser-defined params
337 for (var i = 0, l = params.length; i < l; i++) {
339 if (params[i].type) thisParams[i].type = params[i].type;
340 thisParams[i].name = params[i].name;
341 thisParams[i].desc = params[i].desc;
342 thisParams[i].isOptional = params[i].isOptional;
343 thisParams[i].defaultValue = params[i].defaultValue;
345 else thisParams[i] = params[i];
351 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
352 assertEqual(sym.params.length, 1, "parser defined param is found.");
354 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
355 assertEqual(sym.params.length, 1, "user defined param is found.");
356 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
357 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
359 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
360 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
361 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
362 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
364 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
365 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
366 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
367 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
369 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
370 assertEqual(sym.params.length, 1, "user defined param with description is found.");
371 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
375 if (this.comment.getTag("constructor").length) {
376 this.isa = "CONSTRUCTOR";
380 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
381 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
385 if (this.comment.getTag("static").length) {
386 this.isStatic = true;
387 if (this.isa == "CONSTRUCTOR") {
388 this.isNamespace = true;
393 if (this.comment.getTag("singleton").length) {
394 this.isStatic = true;
395 //if (this.isa == "CONSTRUCTOR") {
396 // this.isNamespace = true;
403 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
404 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
405 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
409 if (this.comment.getTag("inner").length) {
411 this.isStatic = false;
415 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
416 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
417 assertEqual(sym.isInner, true, "@inner makes isInner true.");
421 if (this.comment.getTag("field").length) {
426 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
427 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
431 if (this.comment.getTag("function").length) {
432 this.isa = "FUNCTION";
436 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
437 // change a property to a function..
438 this.isa = "FUNCTION";
443 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
444 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
448 var events = this.comment.getTag("event");
450 this.isa = "FUNCTION";
455 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
456 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
457 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
461 var names = this.comment.getTag("name");
463 this.setName(names[0].desc);
471 var properties = this.comment.getTag("property");
472 if (properties.length) {
473 thisProperties = this.properties;
474 for (var i = 0; i < properties.length; i++) {
475 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
476 // TODO: shouldn't the following happen in the addProperty method of Symbol?
477 property.name = properties[i].name;
478 property.memberOf = this.alias;
479 if (properties[i].type) property.type = properties[i].type;
480 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
481 this.addProperty(property);
482 imports.Parser.Parser.addSymbol(property);
487 var conf = this.comment.getTag("cfg");
489 for (var i = 0; i < conf.length; i++) {
490 this.addConfig(conf[i]);
499 var returns = this.comment.getTag("return");
500 if (returns.length) { // there can be many return tags in a single doclet
501 this.returns = returns;
502 this.type = returns.map(function($){return $.type}).join(", ");
510 this.exceptions = this.comment.getTag("throws");
517 var requires = this.comment.getTag("requires");
518 if (requires.length) {
519 this.requires = requires.map(function($){return $.desc});
527 var types = this.comment.getTag("type");
529 this.type = types[0].desc; //multiple type tags are ignored
537 if (this.comment.getTag("private").length || this.isInner) {
538 this.isPrivate = true;
542 if (this.comment.getTag("ignore").length) {
543 this.isIgnored = true;
550 // @inherits ... as ...
551 var inherits = this.comment.getTag("inherits");
552 if (inherits.length) {
553 for (var i = 0; i < inherits.length; i++) {
554 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
555 var inAlias = RegExp.$1;
556 var inAs = RegExp.$2 || inAlias;
558 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
561 inAs = inAs.replace(/\.prototype\.?/g, "#");
562 inAs = inAs.replace(/^this\.?/, "#");
565 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
567 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
570 inAs = this.alias + joiner + inAs;
573 this.inherits.push({alias: inAlias, as: inAs});
582 this.augments = this.comment.getTag("augments");
585 if (this.comment.getTag("extends")) {
586 this.augments = this.comment.getTag("extends");
591 var defaults = this.comment.getTag("default");
592 if (defaults.length) {
593 if (this.is("OBJECT")) {
594 this.defaultValue = defaults[0].desc;
603 var memberOfs = this.comment.getTag("memberOf");
604 if (memberOfs.length) {
605 this.memberOf = memberOfs[0].desc;
606 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
614 if (this.comment.getTag("public").length) {
615 this.isPrivate = false;
623 is : function(what) {
624 return this.isa === what;
627 isBuiltin : function() {
628 return SymbolSet.isBuiltin(this.alias);
631 setType : function(/**String*/comment, /**Boolean*/overwrite) {
632 if (!overwrite && this.type) return;
633 var typeComment = DocComment.unwrapComment(comment);
634 this.type = typeComment;
637 inherit : function(symbol) {
638 if (!this.hasMember(symbol.name) && !symbol.isInner) {
639 if (symbol.is("FUNCTION"))
640 this.methods.push(symbol);
641 else if (symbol.is("OBJECT"))
642 this.properties.push(symbol);
646 hasMember : function(name) {
647 return (this.hasMethod(name) || this.hasProperty(name));
650 addMember : function(symbol) {
651 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
653 if (symbol.comment.getTag("cfg").length == 1) {
654 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
655 this.addConfig(symbol.comment.getTag("cfg")[0]);
659 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
660 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
663 hasMethod : function(name) {
664 var thisMethods = this.methods;
665 for (var i = 0, l = thisMethods.length; i < l; i++) {
666 if (thisMethods[i].name == name) return true;
667 if (thisMethods[i].alias == name) return true;
672 addMethod : function(symbol) {
673 var methodAlias = symbol.alias;
674 var thisMethods = this.methods;
675 for (var i = 0, l = thisMethods.length; i < l; i++) {
676 if (thisMethods[i].alias == methodAlias) {
677 thisMethods[i] = symbol; // overwriting previous method
681 thisMethods.push(symbol); // new method with this alias
684 hasProperty : function(name) {
685 var thisProperties = this.properties;
686 for (var i = 0, l = thisProperties.length; i < l; i++) {
687 if (thisProperties[i].name == name) return true;
688 if (thisProperties[i].alias == name) return true;
693 addProperty : function(symbol) {
694 var propertyAlias = symbol.alias;
695 var thisProperties = this.properties;
696 for (var i = 0, l = thisProperties.length; i < l; i++) {
697 if (thisProperties[i].alias == propertyAlias) {
698 thisProperties[i] = symbol; // overwriting previous property
703 thisProperties.push(symbol); // new property with this alias
706 addDocTag : function(docTag)
708 this.comment.tags.push(docTag);
709 if (docTag.title == 'cfg') {
710 this.addConfig(docTag);
715 addConfig : function(docTag)
717 if (typeof(docTag['memberOf']) == 'undefined') {
718 // remove prototype data...
719 //var a = this.alias.split('#')[0];
720 //docTag.memberOf = a;
721 docTag.memberOf = this.alias;
723 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
724 this.cfgs[docTag.name] = docTag;
728 configToArray: function()
731 for(var ci in this.cfgs) {
732 // dont show hidden!!
733 if (this.cfgs[ci].desc.match(/@hide/)) {
736 r.push(this.cfgs[ci]);
747 Symbol.srcFile = ""; //running reference to the current file being parsed
750 Symbol.fromDump = function(t)
752 var ns = new Symbol();
754 if (typeof(ns[i]) == "undefined") {
755 println("ERR:no default for Symbol:"+ i);