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;
10 * Create a new Symbol.
11 * @class Represents a symbol in the source code.
14 Symbol = XObject.define(
17 if (arguments.length) this.populate.apply(this, arguments);
27 $args : [], // original arguments used when constructing.
31 augments : [], // Doctag[]
39 exceptions : [], // Doctag[]
40 inherits : [], // Doctag[]
42 isa : "OBJECT", // OBJECT//FUNCTION
51 methods : [], // Symbol[]
53 _params : [], //Doctag[]
54 properties : [], //Doctag[]
55 requires : [], //Doctag[]
56 returns : [], //Doctag[]
72 var ret = { '*object' : 'Symbol' };
74 if (Symbol.hide.indexOf(i) > -1) {
77 switch (typeof(this[i])) {
93 case 'cfgs': // key val of doctags..
106 print("object? :" + i);
114 ret[i] = this[i]; continue;
116 print("unknown type:" + typeof(this[i]));
126 // only initialize arrays / objects..
131 this.exceptions = [];
134 this.isa = "OBJECT"; // OBJECT//FUNCTION
137 this.properties = [];
146 this.inheritsFrom = [];
147 this.childClasses = [];
149 this.comment = new DocComment();
150 this.comment.isUserComment = false;
155 serialize : function() {
157 for (var p in this) {
163 for (var i in keys) {
164 if (typeof this[keys[i]] == "function") continue;
165 out += " " +keys[i]+" => "+
167 (typeof(this[keys[i]]) != "object") ?
169 "[" +typeof(this[keys[i]])+"]"
173 return "\n{\n" + out + "}\n";
177 var clone = new Symbol();
178 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
179 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
186 //__defineSetter__("name",
187 setName : function(in_n) {
188 n = in_n.replace(/^_global_[.#-]/, "");
189 n = n.replace(/\.prototype\.?/g, '#');
190 n = n.replace(/\#prototype\.?/g, '#');
191 n = n.replace(/#$/g, '');
193 this.name = n; // real!
194 print("Symbol: setName(" + in_n + ') => ' + this.name );
197 //__defineGetter__("name",
198 getName : function() {
202 //__defineSetter__("params",
203 setParams :function(v) {
204 for (var i = 0, l = v.length; i < l; i++) {
205 if (typeof(v[i]) == 'string') {
206 this._params[i] = new DocTag( "param "+ v[i] );
209 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
210 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
211 this._params[i] = new DocTag(
212 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
215 // default is doctag..
216 this._params[i] = v[i];
219 this.params = this._params;
224 //__defineGetter__("params",
225 getParams : function() { return this._params; },
230 /** Object[] */ params,
232 /** DocComment */ comment
234 this.$args = arguments;
235 //println("Symbol created: " + isa + ":" + name);
237 this.namespace = Symbol.namespace;
238 this.alias = this.getName();
239 this.setParams(params);
240 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
241 this.comment = comment || new DocComment("");
242 this.srcFile = Symbol.srcFile;
246 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
250 //if (typeof PluginManager != "undefined") {
251 // PluginManager.run("onSymbol", this);
255 setTags : function() {
257 var authors = this.comment.getTag("author");
258 if (authors.length) {
259 this.author = authors.map(function($){return $.desc;}).join(", ");
263 assert("testing Symbol");
265 requires("../lib/JSDOC/DocComment.js");
266 requires("../frame/String.js");
267 requires("../lib/JSDOC/DocTag.js");
269 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
270 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
273 var mth = this.comment.getTag("method");
275 this.isa = "FUNCTION";
278 var descs = this.comment.getTag("desc");
280 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
284 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
285 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
289 if (this.is("FILE")) {
290 if (!this.alias) this.alias = this.srcFile;
292 var overviews = this.comment.getTag("overview");
293 if (overviews.length) {
294 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
299 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
300 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
304 var sinces = this.comment.getTag("since");
306 this.since = sinces.map(function($){return $.desc;}).join(", ");
310 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
311 assertEqual(sym.since, "1.01", "@since tag, description is found.");
315 if (this.comment.getTag("constant").length) {
316 this.isConstant = true;
321 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
322 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
326 var versions = this.comment.getTag("version");
327 if (versions.length) {
328 this.version = versions.map(function($){return $.desc;}).join(", ");
332 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
333 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
337 var deprecateds = this.comment.getTag("deprecated");
338 if (deprecateds.length) {
339 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
343 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
344 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
348 var examples = this.comment.getTag("example");
349 if (examples.length) {
350 this.example = examples[0];
354 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
355 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
359 var sees = this.comment.getTag("see");
361 var thisSee = this.see;
362 sees.map(function($){thisSee.push($.desc);});
366 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
367 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
371 var classes = this.comment.getTag("class");
372 if (classes.length) {
373 this.isa = "CONSTRUCTOR";
374 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
375 if (!this.classDesc) {
376 this.classDesc = this.desc;
383 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
384 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
385 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
389 var namespaces = this.comment.getTag("namespace");
390 if (namespaces.length) {
391 // scope for this file...
392 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
393 if (!this.classDesc) {
394 this.classDesc = this.desc;
396 this.isNamespace = true;
400 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
401 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
405 var params = this.comment.getTag("param");
407 // user-defined params overwrite those with same name defined by the parser
408 var thisParams = this.getParams();
410 if (thisParams.length == 0) {
411 // none exist yet, so just bung all these user-defined params
413 this.setParams(params);
415 // need to overlay these user-defined params on to
416 // existing parser-defined params
417 for (var i = 0, l = params.length; i < l; i++) {
419 if (params[i].type) {
420 thisParams[i].type = params[i].type;
422 thisParams[i].name = params[i].name;
423 thisParams[i].desc = params[i].desc;
424 thisParams[i].isOptional = params[i].isOptional;
425 thisParams[i].defaultValue = params[i].defaultValue;
429 thisParams[i] = params[i];
436 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
437 assertEqual(sym.params.length, 1, "parser defined param is found.");
439 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
440 assertEqual(sym.params.length, 1, "user defined param is found.");
441 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
442 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
444 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
445 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
446 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
447 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
449 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
450 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
451 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
452 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
454 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
455 assertEqual(sym.params.length, 1, "user defined param with description is found.");
456 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
460 if (this.comment.getTag("constructor").length) {
461 this.isa = "CONSTRUCTOR";
465 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
466 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
470 if (this.comment.getTag("static").length) {
471 this.isStatic = true;
472 // replace '#' with '.'
474 this.name = this.name.split('#').join('.');
475 this._name = this.name
476 this.alias = this.name;
478 if (this.isa == "CONSTRUCTOR") {
479 this.isNamespace = true;
484 if (this.comment.getTag("singleton").length) {
485 this.isStatic = true;
486 if (this.isa == "CONSTRUCTOR") {
487 this.isNamespace = true;
494 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
495 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
496 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
500 if (this.comment.getTag("inner").length) {
502 this.isStatic = false;
506 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
507 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
508 assertEqual(sym.isInner, true, "@inner makes isInner true.");
512 if (this.comment.getTag("field").length) {
517 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
518 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
522 if (this.comment.getTag("function").length) {
523 this.isa = "FUNCTION";
527 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
528 // change a property to a function..
529 this.isa = "FUNCTION";
534 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
535 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
539 var events = this.comment.getTag("event");
541 this.isa = "FUNCTION";
546 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
547 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
548 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
552 var names = this.comment.getTag("name");
554 this.setName(names[0].desc);
562 var properties = this.comment.getTag("property");
563 if (properties.length) {
564 thisProperties = this.properties;
565 for (var i = 0; i < properties.length; i++) {
566 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
567 // TODO: shouldn't the following happen in the addProperty method of Symbol?
568 property.name = properties[i].name;
569 property.memberOf = this.alias;
570 if (properties[i].type) property.type = properties[i].type;
571 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
572 this.addProperty(property);
573 //Symbol.parser.addSymbol(property);
574 imports.ScopeNamer.ScopeNamer.addSymbol(property);
579 var conf = this.comment.getTag("cfg");
581 for (var i = 0; i < conf.length; i++) {
582 this.addConfig(conf[i]);
591 var returns = this.comment.getTag("return");
592 if (returns.length) { // there can be many return tags in a single doclet
593 this.returns = returns;
594 this.type = returns.map(function($){return $.type}).join(", ");
602 this.exceptions = this.comment.getTag("throws");
609 var requires = this.comment.getTag("requires");
610 if (requires.length) {
611 this.requires = requires.map(function($){return $.desc});
619 var types = this.comment.getTag("type");
621 this.type = types[0].type; //multiple type tags are ignored
622 if (types[0].desc.length && !this.desc.length) {
623 this.desc = types[0].desc;
632 if (this.comment.getTag("private").length || this.isInner) {
633 this.isPrivate = true;
637 if (this.comment.getTag("ignore").length) {
638 this.isIgnored = true;
645 // @inherits ... as ...
646 var inherits = this.comment.getTag("inherits");
647 if (inherits.length) {
648 for (var i = 0; i < inherits.length; i++) {
649 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
650 var inAlias = RegExp.$1;
651 var inAs = RegExp.$2 || inAlias;
653 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
656 inAs = inAs.replace(/\.prototype\.?/g, "#");
657 inAs = inAs.replace(/^this\.?/, "#");
660 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
662 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
665 inAs = this.alias + joiner + inAs;
668 this.inherits.push({alias: inAlias, as: inAs});
677 this.augments = this.comment.getTag("augments");
680 if (this.comment.getTag("extends")) {
681 this.augments = this.comment.getTag("extends");
686 var defaults = this.comment.getTag("default");
687 if (defaults.length) {
688 if (this.is("OBJECT")) {
689 this.defaultValue = defaults[0].desc;
698 var memberOfs = this.comment.getTag("memberOf");
699 if (memberOfs.length) {
700 this.memberOf = memberOfs[0].desc;
701 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
702 this.name = this.name.split('.').pop();
703 this.name = this.name.split('#').pop();
704 this.name = this.memberOf + this.name;
705 this._name = this.name
706 this.alias = this.name;
714 if (this.comment.getTag("public").length) {
715 this.isPrivate = false;
723 is : function(what) {
724 return this.isa === what;
727 isBuiltin : function() {
728 return SymbolSet.isBuiltin(this.alias);
731 setType : function(/**String*/comment, /**Boolean*/overwrite) {
732 if (!overwrite && this.type) return;
733 var typeComment = DocComment.unwrapComment(comment);
734 this.type = typeComment;
737 inherit : function(symbol) {
738 if (!this.hasMember(symbol.name) && !symbol.isInner) {
739 if (symbol.is("FUNCTION")) {
740 this.methods.push(symbol);
741 } else if (symbol.is("OBJECT") || symbol.is("PROPERTY") ) {
742 this.properties.push(symbol);
747 hasMember : function(name) {
748 return (this.hasMethod(name) || this.hasProperty(name));
751 addMember : function(symbol) {
752 //print("ADDMEMBER: " + this.name + " + " + symbol.name);
753 if (this.is("FUNCTION")) {
756 if (symbol.comment.getTag("cfg").length == 1) {
757 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
758 this.addConfig(symbol.comment.getTag("cfg")[0]);
762 if (symbol.is("FUNCTION")) {
763 this.addMethod(symbol);
766 if (symbol.is("OBJECT") || symbol.is("PROPERTY") ) {
767 this.addProperty(symbol);
770 //print("addSomething?: " + JSON.stringify(symbol, null,4)); Seed.exit();
775 hasMethod : function(name) {
776 var thisMethods = this.methods;
777 for (var i = 0, l = thisMethods.length; i < l; i++) {
778 if (thisMethods[i].name == name) return true;
779 if (thisMethods[i].alias == name) return true;
784 addMethod : function(symbol) {
785 var methodAlias = symbol.alias;
786 var thisMethods = this.methods;
787 for (var i = 0, l = thisMethods.length; i < l; i++) {
788 if (thisMethods[i].alias == methodAlias) {
789 thisMethods[i] = symbol; // overwriting previous method
793 thisMethods.push(symbol); // new method with this alias
796 hasProperty : function(name) {
797 var thisProperties = this.properties;
798 for (var i = 0, l = thisProperties.length; i < l; i++) {
799 if (thisProperties[i].name == name) return true;
800 if (thisProperties[i].alias == name) return true;
805 addProperty : function(symbol) {
806 //print("addProperty: " + this.name + " + " + symbol.name);
807 var propertyAlias = symbol.alias;
808 var thisProperties = this.properties;
809 for (var i = 0, l = thisProperties.length; i < l; i++) {
810 if (thisProperties[i].alias == propertyAlias) {
811 thisProperties[i] = symbol; // overwriting previous property
816 thisProperties.push(symbol); // new property with this alias
819 addDocTag : function(docTag)
821 this.comment.tags.push(docTag);
822 if (docTag.title == 'cfg') {
823 this.addConfig(docTag);
828 addConfig : function(docTag)
830 if (typeof(docTag['memberOf']) == 'undefined') {
831 // remove prototype data...
832 //var a = this.alias.split('#')[0];
833 //docTag.memberOf = a;
834 docTag.memberOf = this.alias;
836 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
837 this.cfgs[docTag.name] = docTag;
841 configToArray: function()
844 for(var ci in this.cfgs) {
845 // dont show hidden!!
846 if (this.cfgs[ci].desc.match(/@hide/)) {
849 r.push(this.cfgs[ci]);
857 * Elements that are not serialized
861 '$args' // not needed AFAIK
864 Symbol.srcFile = ""; //running reference to the current file being parsed
865 Symbol.namespace = ""; //running reference to the current file being parsed
868 Symbol.fromDump = function(t)
870 var ns = new Symbol();
872 if (typeof(ns[i]) == "undefined") {
873 println("ERR:no default for Symbol:"+ i);
879 Symbol.parser = false; // set up by parser..