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..
105 print("object? :" + i);
113 ret[i] = this[i]; continue;
115 print("unknown type:" + typeof(this[i]));
125 // only initialize arrays / objects..
130 this.exceptions = [];
133 this.isa = "OBJECT"; // OBJECT//FUNCTION
136 this.properties = [];
145 this.inheritsFrom = [];
146 this.childClasses = [];
148 this.comment = new DocComment();
149 this.comment.isUserComment = false;
154 serialize : function() {
156 for (var p in this) {
162 for (var i in keys) {
163 if (typeof this[keys[i]] == "function") continue;
164 out += " " +keys[i]+" => "+
166 (typeof(this[keys[i]]) != "object") ?
168 "[" +typeof(this[keys[i]])+"]"
172 return "\n{\n" + out + "}\n";
176 var clone = new Symbol();
177 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
178 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
185 //__defineSetter__("name",
186 setName : function(in_n) {
187 n = in_n.replace(/^_global_[.#-]/, "");
188 n = n.replace(/\.prototype\.?/g, '#');
189 n = n.replace(/\#prototype\.?/g, '#');
190 n = n.replace(/#$/g, '');
192 this.name = n; // real!
193 print("Symbol: setName(" + in_n + ') => ' + this.name );
196 //__defineGetter__("name",
197 getName : function() {
201 //__defineSetter__("params",
202 setParams :function(v) {
203 for (var i = 0, l = v.length; i < l; i++) {
204 if (typeof(v[i]) == 'string') {
205 this._params[i] = new DocTag( "param "+ v[i] );
208 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
209 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
210 this._params[i] = new DocTag(
211 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
214 // default is doctag..
215 this._params[i] = v[i];
218 this.params = this._params;
223 //__defineGetter__("params",
224 getParams : function() { return this._params; },
229 /** Object[] */ params,
231 /** DocComment */ comment
233 this.$args = arguments;
234 //println("Symbol created: " + isa + ":" + name);
236 this.namespace = Symbol.namespace;
237 this.alias = this.getName();
238 this.setParams(params);
239 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
240 this.comment = comment || new DocComment("");
241 this.srcFile = Symbol.srcFile;
245 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
249 //if (typeof PluginManager != "undefined") {
250 // PluginManager.run("onSymbol", this);
254 setTags : function() {
256 var authors = this.comment.getTag("author");
257 if (authors.length) {
258 this.author = authors.map(function($){return $.desc;}).join(", ");
262 assert("testing Symbol");
264 requires("../lib/JSDOC/DocComment.js");
265 requires("../frame/String.js");
266 requires("../lib/JSDOC/DocTag.js");
268 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
269 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
272 var mth = this.comment.getTag("method");
274 this.isa = "FUNCTION";
277 var descs = this.comment.getTag("desc");
279 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
283 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
284 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
288 if (this.is("FILE")) {
289 if (!this.alias) this.alias = this.srcFile;
291 var overviews = this.comment.getTag("overview");
292 if (overviews.length) {
293 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
298 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
299 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
303 var sinces = this.comment.getTag("since");
305 this.since = sinces.map(function($){return $.desc;}).join(", ");
309 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
310 assertEqual(sym.since, "1.01", "@since tag, description is found.");
314 if (this.comment.getTag("constant").length) {
315 this.isConstant = true;
320 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
321 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
325 var versions = this.comment.getTag("version");
326 if (versions.length) {
327 this.version = versions.map(function($){return $.desc;}).join(", ");
331 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
332 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
336 var deprecateds = this.comment.getTag("deprecated");
337 if (deprecateds.length) {
338 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
342 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
343 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
347 var examples = this.comment.getTag("example");
348 if (examples.length) {
349 this.example = examples[0];
353 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
354 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
358 var sees = this.comment.getTag("see");
360 var thisSee = this.see;
361 sees.map(function($){thisSee.push($.desc);});
365 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
366 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
370 var classes = this.comment.getTag("class");
371 if (classes.length) {
372 this.isa = "CONSTRUCTOR";
373 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
374 if (!this.classDesc) {
375 this.classDesc = this.desc;
382 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
383 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
384 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
388 var namespaces = this.comment.getTag("namespace");
389 if (namespaces.length) {
390 // scope for this file...
391 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
392 this.isNamespace = true;
396 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
397 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
401 var params = this.comment.getTag("param");
403 // user-defined params overwrite those with same name defined by the parser
404 var thisParams = this.getParams();
406 if (thisParams.length == 0) {
407 // none exist yet, so just bung all these user-defined params
409 this.setParams(params);
411 // need to overlay these user-defined params on to
412 // existing parser-defined params
413 for (var i = 0, l = params.length; i < l; i++) {
415 if (params[i].type) {
416 thisParams[i].type = params[i].type;
418 thisParams[i].name = params[i].name;
419 thisParams[i].desc = params[i].desc;
420 thisParams[i].isOptional = params[i].isOptional;
421 thisParams[i].defaultValue = params[i].defaultValue;
425 thisParams[i] = params[i];
432 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
433 assertEqual(sym.params.length, 1, "parser defined param is found.");
435 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
436 assertEqual(sym.params.length, 1, "user defined param is found.");
437 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
438 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
440 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
441 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
442 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
443 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
445 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
446 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
447 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
448 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
450 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
451 assertEqual(sym.params.length, 1, "user defined param with description is found.");
452 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
456 if (this.comment.getTag("constructor").length) {
457 this.isa = "CONSTRUCTOR";
461 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
462 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
466 if (this.comment.getTag("static").length) {
467 this.isStatic = true;
468 // replace '#' with '.'
470 this.name = this.name.split('#').join('.');
471 this._name = this.name
472 this.alias = this.name;
474 if (this.isa == "CONSTRUCTOR") {
475 this.isNamespace = true;
480 if (this.comment.getTag("singleton").length) {
481 this.isStatic = true;
482 if (this.isa == "CONSTRUCTOR") {
483 this.isNamespace = true;
490 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
491 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
492 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
496 if (this.comment.getTag("inner").length) {
498 this.isStatic = false;
502 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
503 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
504 assertEqual(sym.isInner, true, "@inner makes isInner true.");
508 if (this.comment.getTag("field").length) {
513 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
514 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
518 if (this.comment.getTag("function").length) {
519 this.isa = "FUNCTION";
523 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
524 // change a property to a function..
525 this.isa = "FUNCTION";
530 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
531 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
535 var events = this.comment.getTag("event");
537 this.isa = "FUNCTION";
542 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
543 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
544 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
548 var names = this.comment.getTag("name");
550 this.setName(names[0].desc);
558 var properties = this.comment.getTag("property");
559 if (properties.length) {
560 thisProperties = this.properties;
561 for (var i = 0; i < properties.length; i++) {
562 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
563 // TODO: shouldn't the following happen in the addProperty method of Symbol?
564 property.name = properties[i].name;
565 property.memberOf = this.alias;
566 if (properties[i].type) property.type = properties[i].type;
567 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
568 this.addProperty(property);
569 //Symbol.parser.addSymbol(property);
570 imports.ScopeNamer.ScopeNamer.addSymbol(property);
575 var conf = this.comment.getTag("cfg");
577 for (var i = 0; i < conf.length; i++) {
578 this.addConfig(conf[i]);
587 var returns = this.comment.getTag("return");
588 if (returns.length) { // there can be many return tags in a single doclet
589 this.returns = returns;
590 this.type = returns.map(function($){return $.type}).join(", ");
598 this.exceptions = this.comment.getTag("throws");
605 var requires = this.comment.getTag("requires");
606 if (requires.length) {
607 this.requires = requires.map(function($){return $.desc});
615 var types = this.comment.getTag("type");
617 this.type = types[0].type; //multiple type tags are ignored
618 if (types[0].desc.length && !this.desc.length) {
619 this.desc = types[0].desc;
628 if (this.comment.getTag("private").length || this.isInner) {
629 this.isPrivate = true;
633 if (this.comment.getTag("ignore").length) {
634 this.isIgnored = true;
641 // @inherits ... as ...
642 var inherits = this.comment.getTag("inherits");
643 if (inherits.length) {
644 for (var i = 0; i < inherits.length; i++) {
645 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
646 var inAlias = RegExp.$1;
647 var inAs = RegExp.$2 || inAlias;
649 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
652 inAs = inAs.replace(/\.prototype\.?/g, "#");
653 inAs = inAs.replace(/^this\.?/, "#");
656 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
658 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
661 inAs = this.alias + joiner + inAs;
664 this.inherits.push({alias: inAlias, as: inAs});
673 this.augments = this.comment.getTag("augments");
676 if (this.comment.getTag("extends")) {
677 this.augments = this.comment.getTag("extends");
682 var defaults = this.comment.getTag("default");
683 if (defaults.length) {
684 if (this.is("OBJECT")) {
685 this.defaultValue = defaults[0].desc;
694 var memberOfs = this.comment.getTag("memberOf");
695 if (memberOfs.length) {
696 this.memberOf = memberOfs[0].desc;
697 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
698 this.name = this.name.split('.').pop();
699 this.name = this.name.split('#').pop();
700 this.name = this.memberOf + this.name;
701 this._name = this.name
702 this.alias = this.name;
710 if (this.comment.getTag("public").length) {
711 this.isPrivate = false;
719 is : function(what) {
720 return this.isa === what;
723 isBuiltin : function() {
724 return SymbolSet.isBuiltin(this.alias);
727 setType : function(/**String*/comment, /**Boolean*/overwrite) {
728 if (!overwrite && this.type) return;
729 var typeComment = DocComment.unwrapComment(comment);
730 this.type = typeComment;
733 inherit : function(symbol) {
734 if (!this.hasMember(symbol.name) && !symbol.isInner) {
735 if (symbol.is("FUNCTION"))
736 this.methods.push(symbol);
737 else if (symbol.is("OBJECT"))
738 this.properties.push(symbol);
742 hasMember : function(name) {
743 return (this.hasMethod(name) || this.hasProperty(name));
746 addMember : function(symbol) {
747 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
748 if (this.is("FUNCTION")) {
751 if (symbol.comment.getTag("cfg").length == 1) {
752 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
753 this.addConfig(symbol.comment.getTag("cfg")[0]);
757 if (symbol.is("FUNCTION")) {
758 this.addMethod(symbol);
761 if (symbol.is("OBJECT")) {
762 this.addProperty(symbol);
768 hasMethod : function(name) {
769 var thisMethods = this.methods;
770 for (var i = 0, l = thisMethods.length; i < l; i++) {
771 if (thisMethods[i].name == name) return true;
772 if (thisMethods[i].alias == name) return true;
777 addMethod : function(symbol) {
778 var methodAlias = symbol.alias;
779 var thisMethods = this.methods;
780 for (var i = 0, l = thisMethods.length; i < l; i++) {
781 if (thisMethods[i].alias == methodAlias) {
782 thisMethods[i] = symbol; // overwriting previous method
786 thisMethods.push(symbol); // new method with this alias
789 hasProperty : function(name) {
790 var thisProperties = this.properties;
791 for (var i = 0, l = thisProperties.length; i < l; i++) {
792 if (thisProperties[i].name == name) return true;
793 if (thisProperties[i].alias == name) return true;
798 addProperty : function(symbol) {
799 var propertyAlias = symbol.alias;
800 var thisProperties = this.properties;
801 for (var i = 0, l = thisProperties.length; i < l; i++) {
802 if (thisProperties[i].alias == propertyAlias) {
803 thisProperties[i] = symbol; // overwriting previous property
808 thisProperties.push(symbol); // new property with this alias
811 addDocTag : function(docTag)
813 this.comment.tags.push(docTag);
814 if (docTag.title == 'cfg') {
815 this.addConfig(docTag);
820 addConfig : function(docTag)
822 if (typeof(docTag['memberOf']) == 'undefined') {
823 // remove prototype data...
824 //var a = this.alias.split('#')[0];
825 //docTag.memberOf = a;
826 docTag.memberOf = this.alias;
828 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
829 this.cfgs[docTag.name] = docTag;
833 configToArray: function()
836 for(var ci in this.cfgs) {
837 // dont show hidden!!
838 if (this.cfgs[ci].desc.match(/@hide/)) {
841 r.push(this.cfgs[ci]);
849 * Elements that are not serialized
853 '$args' // not needed AFAIK
856 Symbol.srcFile = ""; //running reference to the current file being parsed
857 Symbol.namespace = ""; //running reference to the current file being parsed
860 Symbol.fromDump = function(t)
862 var ns = new Symbol();
864 if (typeof(ns[i]) == "undefined") {
865 println("ERR:no default for Symbol:"+ i);
871 Symbol.parser = false; // set up by parser..