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);
104 case 'cfgs': // key val of doctags..
107 ret[i][k] = this[i][k].toObject()
113 print(JSON.stringify(this[i]));
114 ret[i] = this[i].toObject();
123 print("object? :" + i);
130 ret[i] = this[i]; continue;
132 print("unknown type:" + typeof(this[i]));
142 // only initialize arrays / objects..
147 this.exceptions = [];
150 this.isa = "OBJECT"; // OBJECT//FUNCTION
153 this.properties = [];
162 this.inheritsFrom = [];
163 this.childClasses = [];
165 this.comment = new DocComment();
166 this.comment.isUserComment = false;
171 serialize : function() {
173 for (var p in this) {
179 for (var i in keys) {
180 if (typeof this[keys[i]] == "function") continue;
181 out += " " +keys[i]+" => "+
183 (typeof(this[keys[i]]) != "object") ?
185 "[" +typeof(this[keys[i]])+"]"
189 return "\n{\n" + out + "}\n";
193 var clone = new Symbol();
194 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
195 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
202 //__defineSetter__("name",
203 setName : function(n) {
204 n = n.replace(/^_global_[.#-]/, "");
205 n = n.replace(/\.prototype\.?/g, '#');
206 n = n.replace(/#$/g, '');
208 this.name = n; // real!
211 //__defineGetter__("name",
212 getName : function() { return this._name; },
214 //__defineSetter__("params",
215 setParams :function(v) {
216 for (var i = 0, l = v.length; i < l; i++) {
217 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
218 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
219 this._params[i] = new DocTag(
220 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
223 this._params[i] = v[i];
226 this.params = this._params;
231 //__defineGetter__("params",
232 getParams : function() { return this._params; },
237 /** Object[] */ params,
239 /** DocComment */ comment
241 this.$args = arguments;
242 //println("Symbol created: " + isa + ":" + name);
244 this.alias = this.getName();
245 this.setParams(params);
246 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
247 this.comment = comment || new DocComment("");
248 this.srcFile = Symbol.srcFile;
252 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
256 //if (typeof PluginManager != "undefined") {
257 // PluginManager.run("onSymbol", this);
261 setTags : function() {
263 var authors = this.comment.getTag("author");
264 if (authors.length) {
265 this.author = authors.map(function($){return $.desc;}).join(", ");
269 assert("testing Symbol");
271 requires("../lib/JSDOC/DocComment.js");
272 requires("../frame/String.js");
273 requires("../lib/JSDOC/DocTag.js");
275 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
276 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
279 var mth = this.comment.getTag("method");
281 this.isa = "FUNCTION";
284 var descs = this.comment.getTag("desc");
286 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
290 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
291 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
295 if (this.is("FILE")) {
296 if (!this.alias) this.alias = this.srcFile;
298 var overviews = this.comment.getTag("overview");
299 if (overviews.length) {
300 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
305 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
306 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
310 var sinces = this.comment.getTag("since");
312 this.since = sinces.map(function($){return $.desc;}).join(", ");
316 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
317 assertEqual(sym.since, "1.01", "@since tag, description is found.");
321 if (this.comment.getTag("constant").length) {
322 this.isConstant = true;
327 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
328 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
332 var versions = this.comment.getTag("version");
333 if (versions.length) {
334 this.version = versions.map(function($){return $.desc;}).join(", ");
338 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
339 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
343 var deprecateds = this.comment.getTag("deprecated");
344 if (deprecateds.length) {
345 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
349 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
350 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
354 var examples = this.comment.getTag("example");
355 if (examples.length) {
356 this.example = examples[0];
360 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
361 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
365 var sees = this.comment.getTag("see");
367 var thisSee = this.see;
368 sees.map(function($){thisSee.push($.desc);});
372 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
373 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
377 var classes = this.comment.getTag("class");
378 if (classes.length) {
379 this.isa = "CONSTRUCTOR";
380 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
381 if (!this.classDesc) {
382 this.classDesc = this.desc;
389 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
390 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
391 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
395 var namespaces = this.comment.getTag("namespace");
396 if (namespaces.length) {
397 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
398 this.isNamespace = true;
402 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
403 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
407 var params = this.comment.getTag("param");
409 // user-defined params overwrite those with same name defined by the parser
410 var thisParams = this.getParams();
412 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
413 this.setParams(params);
415 else { // need to overlay these user-defined params on to existing parser-defined params
416 for (var i = 0, l = params.length; i < l; i++) {
418 if (params[i].type) thisParams[i].type = params[i].type;
419 thisParams[i].name = params[i].name;
420 thisParams[i].desc = params[i].desc;
421 thisParams[i].isOptional = params[i].isOptional;
422 thisParams[i].defaultValue = params[i].defaultValue;
424 else thisParams[i] = params[i];
430 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
431 assertEqual(sym.params.length, 1, "parser defined param is found.");
433 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
434 assertEqual(sym.params.length, 1, "user defined param is found.");
435 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
436 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
438 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
439 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
440 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
441 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
443 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
444 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
445 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
446 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
448 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
449 assertEqual(sym.params.length, 1, "user defined param with description is found.");
450 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
454 if (this.comment.getTag("constructor").length) {
455 this.isa = "CONSTRUCTOR";
459 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
460 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
464 if (this.comment.getTag("static").length) {
465 this.isStatic = true;
466 if (this.isa == "CONSTRUCTOR") {
467 this.isNamespace = true;
472 if (this.comment.getTag("singleton").length) {
473 this.isStatic = true;
474 //if (this.isa == "CONSTRUCTOR") {
475 // this.isNamespace = true;
482 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
483 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
484 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
488 if (this.comment.getTag("inner").length) {
490 this.isStatic = false;
494 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
495 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
496 assertEqual(sym.isInner, true, "@inner makes isInner true.");
500 if (this.comment.getTag("field").length) {
505 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
506 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
510 if (this.comment.getTag("function").length) {
511 this.isa = "FUNCTION";
515 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
516 // change a property to a function..
517 this.isa = "FUNCTION";
522 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
523 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
527 var events = this.comment.getTag("event");
529 this.isa = "FUNCTION";
534 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
535 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
536 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
540 var names = this.comment.getTag("name");
542 this.setName(names[0].desc);
550 var properties = this.comment.getTag("property");
551 if (properties.length) {
552 thisProperties = this.properties;
553 for (var i = 0; i < properties.length; i++) {
554 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
555 // TODO: shouldn't the following happen in the addProperty method of Symbol?
556 property.name = properties[i].name;
557 property.memberOf = this.alias;
558 if (properties[i].type) property.type = properties[i].type;
559 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
560 this.addProperty(property);
561 imports.Parser.Parser.addSymbol(property);
566 var conf = this.comment.getTag("cfg");
568 for (var i = 0; i < conf.length; i++) {
569 this.addConfig(conf[i]);
578 var returns = this.comment.getTag("return");
579 if (returns.length) { // there can be many return tags in a single doclet
580 this.returns = returns;
581 this.type = returns.map(function($){return $.type}).join(", ");
589 this.exceptions = this.comment.getTag("throws");
596 var requires = this.comment.getTag("requires");
597 if (requires.length) {
598 this.requires = requires.map(function($){return $.desc});
606 var types = this.comment.getTag("type");
608 this.type = types[0].desc; //multiple type tags are ignored
616 if (this.comment.getTag("private").length || this.isInner) {
617 this.isPrivate = true;
621 if (this.comment.getTag("ignore").length) {
622 this.isIgnored = true;
629 // @inherits ... as ...
630 var inherits = this.comment.getTag("inherits");
631 if (inherits.length) {
632 for (var i = 0; i < inherits.length; i++) {
633 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
634 var inAlias = RegExp.$1;
635 var inAs = RegExp.$2 || inAlias;
637 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
640 inAs = inAs.replace(/\.prototype\.?/g, "#");
641 inAs = inAs.replace(/^this\.?/, "#");
644 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
646 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
649 inAs = this.alias + joiner + inAs;
652 this.inherits.push({alias: inAlias, as: inAs});
661 this.augments = this.comment.getTag("augments");
664 if (this.comment.getTag("extends")) {
665 this.augments = this.comment.getTag("extends");
670 var defaults = this.comment.getTag("default");
671 if (defaults.length) {
672 if (this.is("OBJECT")) {
673 this.defaultValue = defaults[0].desc;
682 var memberOfs = this.comment.getTag("memberOf");
683 if (memberOfs.length) {
684 this.memberOf = memberOfs[0].desc;
685 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
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
833 Symbol.fromDump = function(t)
835 var ns = new Symbol();
837 if (typeof(ns[i]) == "undefined") {
838 println("ERR:no default for Symbol:"+ i);