1 //<script type="text/javascript">
3 XObject = imports.XObject.XObject;
5 SymbolSet = imports.SymbolSet.SymbolSet;
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.
30 augments : [], // Doctag[]
38 exceptions : [], // Doctag[]
39 inherits : [], // Doctag[]
41 isa : "OBJECT", // OBJECT//FUNCTION
50 methods : [], // Symbol[]
52 _params : [], //Doctag[]
53 properties : [], //Doctag[]
54 requires : [], //Doctag[]
55 returns : [], //Doctag[]
71 var ret = { '*object' : 'Symbol' };
73 if (Symbol.hide.indexOf(i) > -1) {
76 switch (typeof(this[i])) {
92 case 'cfgs': // key val of doctags..
103 print("object? :" + i);
111 ret[i] = this[i]; continue;
113 print("unknown type:" + typeof(this[i]));
123 // only initialize arrays / objects..
128 this.exceptions = [];
131 this.isa = "OBJECT"; // OBJECT//FUNCTION
134 this.properties = [];
143 this.inheritsFrom = [];
144 this.childClasses = [];
146 this.comment = new DocComment();
147 this.comment.isUserComment = false;
152 serialize : function() {
154 for (var p in this) {
160 for (var i in keys) {
161 if (typeof this[keys[i]] == "function") continue;
162 out += " " +keys[i]+" => "+
164 (typeof(this[keys[i]]) != "object") ?
166 "[" +typeof(this[keys[i]])+"]"
170 return "\n{\n" + out + "}\n";
174 var clone = new Symbol();
175 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
176 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
183 //__defineSetter__("name",
184 setName : function(n) {
185 n = n.replace(/^_global_[.#-]/, "");
186 n = n.replace(/\.prototype\.?/g, '#');
187 n = n.replace(/#$/g, '');
189 this.name = n; // real!
192 //__defineGetter__("name",
193 getName : function() { return this._name; },
195 //__defineSetter__("params",
196 setParams :function(v) {
197 for (var i = 0, l = v.length; i < l; i++) {
198 if (typeof(v[i]) == 'string') {
199 this._params[i] = new DocTag( "param "+ v[i].name );
202 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
203 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
204 this._params[i] = new DocTag(
205 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
208 // default is doctag..
209 this._params[i] = v[i];
212 this.params = this._params;
217 //__defineGetter__("params",
218 getParams : function() { return this._params; },
223 /** Object[] */ params,
225 /** DocComment */ comment
227 this.$args = arguments;
228 //println("Symbol created: " + isa + ":" + name);
230 this.alias = this.getName();
231 this.setParams(params);
232 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
233 this.comment = comment || new DocComment("");
234 this.srcFile = Symbol.srcFile;
238 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
242 //if (typeof PluginManager != "undefined") {
243 // PluginManager.run("onSymbol", this);
247 setTags : function() {
249 var authors = this.comment.getTag("author");
250 if (authors.length) {
251 this.author = authors.map(function($){return $.desc;}).join(", ");
255 assert("testing Symbol");
257 requires("../lib/JSDOC/DocComment.js");
258 requires("../frame/String.js");
259 requires("../lib/JSDOC/DocTag.js");
261 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
262 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
265 var mth = this.comment.getTag("method");
267 this.isa = "FUNCTION";
270 var descs = this.comment.getTag("desc");
272 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
276 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
277 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
281 if (this.is("FILE")) {
282 if (!this.alias) this.alias = this.srcFile;
284 var overviews = this.comment.getTag("overview");
285 if (overviews.length) {
286 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
292 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
296 var sinces = this.comment.getTag("since");
298 this.since = sinces.map(function($){return $.desc;}).join(", ");
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
303 assertEqual(sym.since, "1.01", "@since tag, description is found.");
307 if (this.comment.getTag("constant").length) {
308 this.isConstant = true;
313 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
314 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
318 var versions = this.comment.getTag("version");
319 if (versions.length) {
320 this.version = versions.map(function($){return $.desc;}).join(", ");
324 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
325 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
329 var deprecateds = this.comment.getTag("deprecated");
330 if (deprecateds.length) {
331 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
335 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
336 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
340 var examples = this.comment.getTag("example");
341 if (examples.length) {
342 this.example = examples[0];
346 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
347 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
351 var sees = this.comment.getTag("see");
353 var thisSee = this.see;
354 sees.map(function($){thisSee.push($.desc);});
358 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
359 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
363 var classes = this.comment.getTag("class");
364 if (classes.length) {
365 this.isa = "CONSTRUCTOR";
366 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
367 if (!this.classDesc) {
368 this.classDesc = this.desc;
375 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
376 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
377 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
381 var namespaces = this.comment.getTag("namespace");
382 if (namespaces.length) {
383 this.namespace = namespaces[0].desc;
384 Symbol.namespace = namespaces[0].desc + '.';
385 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
386 this.isNamespace = true;
390 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
391 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
395 var params = this.comment.getTag("param");
397 // user-defined params overwrite those with same name defined by the parser
398 var thisParams = this.getParams();
400 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
401 this.setParams(params);
403 else { // need to overlay these user-defined params on to existing parser-defined params
404 for (var i = 0, l = params.length; i < l; i++) {
406 if (params[i].type) thisParams[i].type = params[i].type;
407 thisParams[i].name = params[i].name;
408 thisParams[i].desc = params[i].desc;
409 thisParams[i].isOptional = params[i].isOptional;
410 thisParams[i].defaultValue = params[i].defaultValue;
412 else thisParams[i] = params[i];
418 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
419 assertEqual(sym.params.length, 1, "parser defined param is found.");
421 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
422 assertEqual(sym.params.length, 1, "user defined param is found.");
423 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
424 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
426 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
427 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
428 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
429 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
431 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
432 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
433 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
434 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
436 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
437 assertEqual(sym.params.length, 1, "user defined param with description is found.");
438 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
442 if (this.comment.getTag("constructor").length) {
443 this.isa = "CONSTRUCTOR";
447 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
448 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
452 if (this.comment.getTag("static").length) {
453 this.isStatic = true;
454 // replace '#' with '.'
456 this.name = this.name.split('#').join('.');
457 this._name = this.name
458 this.alias = this.name;
460 if (this.isa == "CONSTRUCTOR") {
461 this.isNamespace = true;
466 if (this.comment.getTag("singleton").length) {
467 this.isStatic = true;
468 //if (this.isa == "CONSTRUCTOR") {
469 // this.isNamespace = true;
476 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
477 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
478 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
482 if (this.comment.getTag("inner").length) {
484 this.isStatic = false;
488 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
489 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
490 assertEqual(sym.isInner, true, "@inner makes isInner true.");
494 if (this.comment.getTag("field").length) {
499 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
500 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
504 if (this.comment.getTag("function").length) {
505 this.isa = "FUNCTION";
509 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
510 // change a property to a function..
511 this.isa = "FUNCTION";
516 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
517 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
521 var events = this.comment.getTag("event");
523 this.isa = "FUNCTION";
528 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
529 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
530 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
534 var names = this.comment.getTag("name");
536 this.setName(names[0].desc);
544 var properties = this.comment.getTag("property");
545 if (properties.length) {
546 thisProperties = this.properties;
547 for (var i = 0; i < properties.length; i++) {
548 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
549 // TODO: shouldn't the following happen in the addProperty method of Symbol?
550 property.name = properties[i].name;
551 property.memberOf = this.alias;
552 if (properties[i].type) property.type = properties[i].type;
553 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
554 this.addProperty(property);
555 //Symbol.parser.addSymbol(property);
556 imports.ScopeNamer.ScopeNamer.addSymbol(property);
561 var conf = this.comment.getTag("cfg");
563 for (var i = 0; i < conf.length; i++) {
564 this.addConfig(conf[i]);
573 var returns = this.comment.getTag("return");
574 if (returns.length) { // there can be many return tags in a single doclet
575 this.returns = returns;
576 this.type = returns.map(function($){return $.type}).join(", ");
584 this.exceptions = this.comment.getTag("throws");
591 var requires = this.comment.getTag("requires");
592 if (requires.length) {
593 this.requires = requires.map(function($){return $.desc});
601 var types = this.comment.getTag("type");
603 this.type = types[0].desc; //multiple type tags are ignored
611 if (this.comment.getTag("private").length || this.isInner) {
612 this.isPrivate = true;
616 if (this.comment.getTag("ignore").length) {
617 this.isIgnored = true;
624 // @inherits ... as ...
625 var inherits = this.comment.getTag("inherits");
626 if (inherits.length) {
627 for (var i = 0; i < inherits.length; i++) {
628 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
629 var inAlias = RegExp.$1;
630 var inAs = RegExp.$2 || inAlias;
632 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
635 inAs = inAs.replace(/\.prototype\.?/g, "#");
636 inAs = inAs.replace(/^this\.?/, "#");
639 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
641 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
644 inAs = this.alias + joiner + inAs;
647 this.inherits.push({alias: inAlias, as: inAs});
656 this.augments = this.comment.getTag("augments");
659 if (this.comment.getTag("extends")) {
660 this.augments = this.comment.getTag("extends");
665 var defaults = this.comment.getTag("default");
666 if (defaults.length) {
667 if (this.is("OBJECT")) {
668 this.defaultValue = defaults[0].desc;
677 var memberOfs = this.comment.getTag("memberOf");
678 if (memberOfs.length) {
679 this.memberOf = memberOfs[0].desc;
680 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
681 this.name = this.name.split('.').pop();
682 this.name = this.name.split('#').pop();
683 this.name = this.memberOf + this.name;
684 this._name = this.name
685 this.alias = this.name;
693 if (this.comment.getTag("public").length) {
694 this.isPrivate = false;
702 is : function(what) {
703 return this.isa === what;
706 isBuiltin : function() {
707 return SymbolSet.isBuiltin(this.alias);
710 setType : function(/**String*/comment, /**Boolean*/overwrite) {
711 if (!overwrite && this.type) return;
712 var typeComment = DocComment.unwrapComment(comment);
713 this.type = typeComment;
716 inherit : function(symbol) {
717 if (!this.hasMember(symbol.name) && !symbol.isInner) {
718 if (symbol.is("FUNCTION"))
719 this.methods.push(symbol);
720 else if (symbol.is("OBJECT"))
721 this.properties.push(symbol);
725 hasMember : function(name) {
726 return (this.hasMethod(name) || this.hasProperty(name));
729 addMember : function(symbol) {
730 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
732 if (symbol.comment.getTag("cfg").length == 1) {
733 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
734 this.addConfig(symbol.comment.getTag("cfg")[0]);
738 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
739 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
742 hasMethod : function(name) {
743 var thisMethods = this.methods;
744 for (var i = 0, l = thisMethods.length; i < l; i++) {
745 if (thisMethods[i].name == name) return true;
746 if (thisMethods[i].alias == name) return true;
751 addMethod : function(symbol) {
752 var methodAlias = symbol.alias;
753 var thisMethods = this.methods;
754 for (var i = 0, l = thisMethods.length; i < l; i++) {
755 if (thisMethods[i].alias == methodAlias) {
756 thisMethods[i] = symbol; // overwriting previous method
760 thisMethods.push(symbol); // new method with this alias
763 hasProperty : function(name) {
764 var thisProperties = this.properties;
765 for (var i = 0, l = thisProperties.length; i < l; i++) {
766 if (thisProperties[i].name == name) return true;
767 if (thisProperties[i].alias == name) return true;
772 addProperty : function(symbol) {
773 var propertyAlias = symbol.alias;
774 var thisProperties = this.properties;
775 for (var i = 0, l = thisProperties.length; i < l; i++) {
776 if (thisProperties[i].alias == propertyAlias) {
777 thisProperties[i] = symbol; // overwriting previous property
782 thisProperties.push(symbol); // new property with this alias
785 addDocTag : function(docTag)
787 this.comment.tags.push(docTag);
788 if (docTag.title == 'cfg') {
789 this.addConfig(docTag);
794 addConfig : function(docTag)
796 if (typeof(docTag['memberOf']) == 'undefined') {
797 // remove prototype data...
798 //var a = this.alias.split('#')[0];
799 //docTag.memberOf = a;
800 docTag.memberOf = this.alias;
802 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
803 this.cfgs[docTag.name] = docTag;
807 configToArray: function()
810 for(var ci in this.cfgs) {
811 // dont show hidden!!
812 if (this.cfgs[ci].desc.match(/@hide/)) {
815 r.push(this.cfgs[ci]);
823 * Elements that are not serialized
827 '$args' // not needed AFAIK
830 Symbol.srcFile = ""; //running reference to the current file being parsed
831 Symbol.namespace = ""; //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);
845 Symbol.parser = false; // set up by parser..