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);
26 $args : [], // original arguments used when constructing.
29 augments : [], // Doctag[]
37 exceptions : [], // Doctag[]
38 inherits : [], // Doctag[]
40 isa : "OBJECT", // OBJECT//FUNCTION
49 methods : [], // Symbol[]
51 _params : [], //Doctag[]
52 properties : [], //Doctag[]
53 requires : [], //Doctag[]
54 returns : [], //Doctag[]
69 function array2object(a)
73 a.forEach(function(e) {
81 if (Symbol.hide.indexOf(i) > -1) {
84 switch (typeof(this[i])) {
100 ret[i] = array2object(this[i],true);
106 print(JSON.stringify(this[i]));
107 ret[i] = this[i].toObject();
115 print("object? :" + i);
122 ret[i] = this[i]; continue;
124 print("unknown type:" + typeof(this[i]));
134 // only initialize arrays / objects..
139 this.exceptions = [];
142 this.isa = "OBJECT"; // OBJECT//FUNCTION
145 this.properties = [];
154 this.inheritsFrom = [];
155 this.childClasses = [];
157 this.comment = new DocComment();
158 this.comment.isUserComment = false;
163 serialize : function() {
165 for (var p in this) {
171 for (var i in keys) {
172 if (typeof this[keys[i]] == "function") continue;
173 out += " " +keys[i]+" => "+
175 (typeof(this[keys[i]]) != "object") ?
177 "[" +typeof(this[keys[i]])+"]"
181 return "\n{\n" + out + "}\n";
185 var clone = new Symbol();
186 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
187 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
194 //__defineSetter__("name",
195 setName : function(n) {
196 n = n.replace(/^_global_[.#-]/, "");
197 n = n.replace(/\.prototype\.?/g, '#');
198 n = n.replace(/#$/g, '');
200 this.name = n; // real!
203 //__defineGetter__("name",
204 getName : function() { return this._name; },
206 //__defineSetter__("params",
207 setParams :function(v) {
208 for (var i = 0, l = v.length; i < l; 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 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.alias = this.getName();
237 this.setParams(params);
238 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
239 this.comment = comment || new DocComment("");
240 this.srcFile = Symbol.srcFile;
244 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
248 //if (typeof PluginManager != "undefined") {
249 // PluginManager.run("onSymbol", this);
253 setTags : function() {
255 var authors = this.comment.getTag("author");
256 if (authors.length) {
257 this.author = authors.map(function($){return $.desc;}).join(", ");
261 assert("testing Symbol");
263 requires("../lib/JSDOC/DocComment.js");
264 requires("../frame/String.js");
265 requires("../lib/JSDOC/DocTag.js");
267 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
268 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
271 var mth = this.comment.getTag("method");
273 this.isa = "FUNCTION";
276 var descs = this.comment.getTag("desc");
278 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
282 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
283 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
287 if (this.is("FILE")) {
288 if (!this.alias) this.alias = this.srcFile;
290 var overviews = this.comment.getTag("overview");
291 if (overviews.length) {
292 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
297 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
298 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
302 var sinces = this.comment.getTag("since");
304 this.since = sinces.map(function($){return $.desc;}).join(", ");
308 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
309 assertEqual(sym.since, "1.01", "@since tag, description is found.");
313 if (this.comment.getTag("constant").length) {
314 this.isConstant = true;
319 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
320 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
324 var versions = this.comment.getTag("version");
325 if (versions.length) {
326 this.version = versions.map(function($){return $.desc;}).join(", ");
330 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
331 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
335 var deprecateds = this.comment.getTag("deprecated");
336 if (deprecateds.length) {
337 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
341 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
342 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
346 var examples = this.comment.getTag("example");
347 if (examples.length) {
348 this.example = examples[0];
352 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
353 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
357 var sees = this.comment.getTag("see");
359 var thisSee = this.see;
360 sees.map(function($){thisSee.push($.desc);});
364 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
365 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
369 var classes = this.comment.getTag("class");
370 if (classes.length) {
371 this.isa = "CONSTRUCTOR";
372 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
373 if (!this.classDesc) {
374 this.classDesc = this.desc;
381 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
382 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
383 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
387 var namespaces = this.comment.getTag("namespace");
388 if (namespaces.length) {
389 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
390 this.isNamespace = true;
394 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
395 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
399 var params = this.comment.getTag("param");
401 // user-defined params overwrite those with same name defined by the parser
402 var thisParams = this.getParams();
404 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
405 this.setParams(params);
407 else { // need to overlay these user-defined params on to existing parser-defined params
408 for (var i = 0, l = params.length; i < l; i++) {
410 if (params[i].type) thisParams[i].type = params[i].type;
411 thisParams[i].name = params[i].name;
412 thisParams[i].desc = params[i].desc;
413 thisParams[i].isOptional = params[i].isOptional;
414 thisParams[i].defaultValue = params[i].defaultValue;
416 else thisParams[i] = params[i];
422 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
423 assertEqual(sym.params.length, 1, "parser defined param is found.");
425 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
426 assertEqual(sym.params.length, 1, "user defined param is found.");
427 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
428 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
430 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
431 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
432 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
433 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
435 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
436 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
437 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
438 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
440 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
441 assertEqual(sym.params.length, 1, "user defined param with description is found.");
442 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
446 if (this.comment.getTag("constructor").length) {
447 this.isa = "CONSTRUCTOR";
451 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
452 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
456 if (this.comment.getTag("static").length) {
457 this.isStatic = true;
458 if (this.isa == "CONSTRUCTOR") {
459 this.isNamespace = true;
464 if (this.comment.getTag("singleton").length) {
465 this.isStatic = true;
466 //if (this.isa == "CONSTRUCTOR") {
467 // this.isNamespace = true;
474 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
475 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
476 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
480 if (this.comment.getTag("inner").length) {
482 this.isStatic = false;
486 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
487 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
488 assertEqual(sym.isInner, true, "@inner makes isInner true.");
492 if (this.comment.getTag("field").length) {
497 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
498 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
502 if (this.comment.getTag("function").length) {
503 this.isa = "FUNCTION";
507 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
508 // change a property to a function..
509 this.isa = "FUNCTION";
514 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
515 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
519 var events = this.comment.getTag("event");
521 this.isa = "FUNCTION";
526 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
527 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
528 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
532 var names = this.comment.getTag("name");
534 this.setName(names[0].desc);
542 var properties = this.comment.getTag("property");
543 if (properties.length) {
544 thisProperties = this.properties;
545 for (var i = 0; i < properties.length; i++) {
546 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
547 // TODO: shouldn't the following happen in the addProperty method of Symbol?
548 property.name = properties[i].name;
549 property.memberOf = this.alias;
550 if (properties[i].type) property.type = properties[i].type;
551 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
552 this.addProperty(property);
553 imports.Parser.Parser.addSymbol(property);
558 var conf = this.comment.getTag("cfg");
560 for (var i = 0; i < conf.length; i++) {
561 this.addConfig(conf[i]);
570 var returns = this.comment.getTag("return");
571 if (returns.length) { // there can be many return tags in a single doclet
572 this.returns = returns;
573 this.type = returns.map(function($){return $.type}).join(", ");
581 this.exceptions = this.comment.getTag("throws");
588 var requires = this.comment.getTag("requires");
589 if (requires.length) {
590 this.requires = requires.map(function($){return $.desc});
598 var types = this.comment.getTag("type");
600 this.type = types[0].desc; //multiple type tags are ignored
608 if (this.comment.getTag("private").length || this.isInner) {
609 this.isPrivate = true;
613 if (this.comment.getTag("ignore").length) {
614 this.isIgnored = true;
621 // @inherits ... as ...
622 var inherits = this.comment.getTag("inherits");
623 if (inherits.length) {
624 for (var i = 0; i < inherits.length; i++) {
625 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
626 var inAlias = RegExp.$1;
627 var inAs = RegExp.$2 || inAlias;
629 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
632 inAs = inAs.replace(/\.prototype\.?/g, "#");
633 inAs = inAs.replace(/^this\.?/, "#");
636 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
638 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
641 inAs = this.alias + joiner + inAs;
644 this.inherits.push({alias: inAlias, as: inAs});
653 this.augments = this.comment.getTag("augments");
656 if (this.comment.getTag("extends")) {
657 this.augments = this.comment.getTag("extends");
662 var defaults = this.comment.getTag("default");
663 if (defaults.length) {
664 if (this.is("OBJECT")) {
665 this.defaultValue = defaults[0].desc;
674 var memberOfs = this.comment.getTag("memberOf");
675 if (memberOfs.length) {
676 this.memberOf = memberOfs[0].desc;
677 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
685 if (this.comment.getTag("public").length) {
686 this.isPrivate = false;
694 is : function(what) {
695 return this.isa === what;
698 isBuiltin : function() {
699 return SymbolSet.isBuiltin(this.alias);
702 setType : function(/**String*/comment, /**Boolean*/overwrite) {
703 if (!overwrite && this.type) return;
704 var typeComment = DocComment.unwrapComment(comment);
705 this.type = typeComment;
708 inherit : function(symbol) {
709 if (!this.hasMember(symbol.name) && !symbol.isInner) {
710 if (symbol.is("FUNCTION"))
711 this.methods.push(symbol);
712 else if (symbol.is("OBJECT"))
713 this.properties.push(symbol);
717 hasMember : function(name) {
718 return (this.hasMethod(name) || this.hasProperty(name));
721 addMember : function(symbol) {
722 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
724 if (symbol.comment.getTag("cfg").length == 1) {
725 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
726 this.addConfig(symbol.comment.getTag("cfg")[0]);
730 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
731 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
734 hasMethod : function(name) {
735 var thisMethods = this.methods;
736 for (var i = 0, l = thisMethods.length; i < l; i++) {
737 if (thisMethods[i].name == name) return true;
738 if (thisMethods[i].alias == name) return true;
743 addMethod : function(symbol) {
744 var methodAlias = symbol.alias;
745 var thisMethods = this.methods;
746 for (var i = 0, l = thisMethods.length; i < l; i++) {
747 if (thisMethods[i].alias == methodAlias) {
748 thisMethods[i] = symbol; // overwriting previous method
752 thisMethods.push(symbol); // new method with this alias
755 hasProperty : function(name) {
756 var thisProperties = this.properties;
757 for (var i = 0, l = thisProperties.length; i < l; i++) {
758 if (thisProperties[i].name == name) return true;
759 if (thisProperties[i].alias == name) return true;
764 addProperty : function(symbol) {
765 var propertyAlias = symbol.alias;
766 var thisProperties = this.properties;
767 for (var i = 0, l = thisProperties.length; i < l; i++) {
768 if (thisProperties[i].alias == propertyAlias) {
769 thisProperties[i] = symbol; // overwriting previous property
774 thisProperties.push(symbol); // new property with this alias
777 addDocTag : function(docTag)
779 this.comment.tags.push(docTag);
780 if (docTag.title == 'cfg') {
781 this.addConfig(docTag);
786 addConfig : function(docTag)
788 if (typeof(docTag['memberOf']) == 'undefined') {
789 // remove prototype data...
790 //var a = this.alias.split('#')[0];
791 //docTag.memberOf = a;
792 docTag.memberOf = this.alias;
794 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
795 this.cfgs[docTag.name] = docTag;
799 configToArray: function()
802 for(var ci in this.cfgs) {
803 // dont show hidden!!
804 if (this.cfgs[ci].desc.match(/@hide/)) {
807 r.push(this.cfgs[ci]);
815 * Elements that are not serialized
819 '$args' // not needed AFAIK
822 Symbol.srcFile = ""; //running reference to the current file being parsed
825 Symbol.fromDump = function(t)
827 var ns = new Symbol();
829 if (typeof(ns[i]) == "undefined") {
830 println("ERR:no default for Symbol:"+ i);