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 : false, // Doctag
32 comment : { isUserComment: false },
40 isa : "OBJECT", // OBJECT//FUNCTION
69 function array2object(a , m)
73 a.forEach(function(e) {
74 r.push(m ? e : e.toObject());
80 if (Symbol.hide.indexOf(i) > -1) {
83 switch (typeof(this[i])) {
90 ret[i] = array2object(this[i],true);
95 print(JSON.stringify(this[i]));
96 ret[i] = this[i].toObject();
100 print("object? :" + i);
107 ret[i] = this[i]; continue;
109 print("unknown type:" + typeof(this[i]));
119 // only initialize arrays / objects..
122 this.comment = { isUserComment: false };
124 this.exceptions = [];
126 this.inheritsFrom = [];
127 this.isa = "OBJECT"; // OBJECT//FUNCTION
130 this.properties = [];
135 this.childClasses = [];
140 serialize : function() {
142 for (var p in this) {
148 for (var i in keys) {
149 if (typeof this[keys[i]] == "function") continue;
150 out += " " +keys[i]+" => "+
152 (typeof(this[keys[i]]) != "object") ?
154 "[" +typeof(this[keys[i]])+"]"
158 return "\n{\n" + out + "}\n";
162 var clone = new Symbol();
163 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
164 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
171 //__defineSetter__("name",
172 setName : function(n) {
173 n = n.replace(/^_global_[.#-]/, "");
174 n = n.replace(/\.prototype\.?/g, '#');
175 n = n.replace(/#$/g, '');
177 this.name = n; // real!
180 //__defineGetter__("name",
181 getName : function() { return this._name; },
183 //__defineSetter__("params",
184 setParams :function(v) {
185 for (var i = 0, l = v.length; i < l; i++) {
186 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
187 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
188 this._params[i] = new DocTag(
189 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
192 this._params[i] = v[i];
195 this.params = this._params;
200 //__defineGetter__("params",
201 getParams : function() { return this._params; },
206 /** Object[] */ params,
208 /** DocComment */ comment
210 this.$args = arguments;
211 //println("Symbol created: " + isa + ":" + name);
213 this.alias = this.getName();
214 this.setParams(params);
215 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
216 this.comment = comment || new DocComment("");
217 this.srcFile = Symbol.srcFile;
221 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
225 //if (typeof PluginManager != "undefined") {
226 // PluginManager.run("onSymbol", this);
230 setTags : function() {
232 var authors = this.comment.getTag("author");
233 if (authors.length) {
234 this.author = authors.map(function($){return $.desc;}).join(", ");
238 assert("testing Symbol");
240 requires("../lib/JSDOC/DocComment.js");
241 requires("../frame/String.js");
242 requires("../lib/JSDOC/DocTag.js");
244 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
245 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
248 var mth = this.comment.getTag("method");
250 this.isa = "FUNCTION";
253 var descs = this.comment.getTag("desc");
255 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
259 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
260 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
264 if (this.is("FILE")) {
265 if (!this.alias) this.alias = this.srcFile;
267 var overviews = this.comment.getTag("overview");
268 if (overviews.length) {
269 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
274 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
275 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
279 var sinces = this.comment.getTag("since");
281 this.since = sinces.map(function($){return $.desc;}).join(", ");
285 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
286 assertEqual(sym.since, "1.01", "@since tag, description is found.");
290 if (this.comment.getTag("constant").length) {
291 this.isConstant = true;
296 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
297 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
301 var versions = this.comment.getTag("version");
302 if (versions.length) {
303 this.version = versions.map(function($){return $.desc;}).join(", ");
307 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
308 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
312 var deprecateds = this.comment.getTag("deprecated");
313 if (deprecateds.length) {
314 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
318 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
319 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
323 var examples = this.comment.getTag("example");
324 if (examples.length) {
325 this.example = examples[0];
329 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
330 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
334 var sees = this.comment.getTag("see");
336 var thisSee = this.see;
337 sees.map(function($){thisSee.push($.desc);});
341 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
342 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
346 var classes = this.comment.getTag("class");
347 if (classes.length) {
348 this.isa = "CONSTRUCTOR";
349 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
350 if (!this.classDesc) {
351 this.classDesc = this.desc;
358 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
359 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
360 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
364 var namespaces = this.comment.getTag("namespace");
365 if (namespaces.length) {
366 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
367 this.isNamespace = true;
371 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
372 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
376 var params = this.comment.getTag("param");
378 // user-defined params overwrite those with same name defined by the parser
379 var thisParams = this.getParams();
381 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
382 this.setParams(params);
384 else { // need to overlay these user-defined params on to existing parser-defined params
385 for (var i = 0, l = params.length; i < l; i++) {
387 if (params[i].type) thisParams[i].type = params[i].type;
388 thisParams[i].name = params[i].name;
389 thisParams[i].desc = params[i].desc;
390 thisParams[i].isOptional = params[i].isOptional;
391 thisParams[i].defaultValue = params[i].defaultValue;
393 else thisParams[i] = params[i];
399 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
400 assertEqual(sym.params.length, 1, "parser defined param is found.");
402 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
403 assertEqual(sym.params.length, 1, "user defined param is found.");
404 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
405 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
407 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
408 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
409 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
410 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
412 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
413 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
414 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
415 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
417 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
418 assertEqual(sym.params.length, 1, "user defined param with description is found.");
419 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
423 if (this.comment.getTag("constructor").length) {
424 this.isa = "CONSTRUCTOR";
428 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
429 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
433 if (this.comment.getTag("static").length) {
434 this.isStatic = true;
435 if (this.isa == "CONSTRUCTOR") {
436 this.isNamespace = true;
441 if (this.comment.getTag("singleton").length) {
442 this.isStatic = true;
443 //if (this.isa == "CONSTRUCTOR") {
444 // this.isNamespace = true;
451 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
452 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
453 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
457 if (this.comment.getTag("inner").length) {
459 this.isStatic = false;
463 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
464 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
465 assertEqual(sym.isInner, true, "@inner makes isInner true.");
469 if (this.comment.getTag("field").length) {
474 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
475 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
479 if (this.comment.getTag("function").length) {
480 this.isa = "FUNCTION";
484 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
485 // change a property to a function..
486 this.isa = "FUNCTION";
491 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
492 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
496 var events = this.comment.getTag("event");
498 this.isa = "FUNCTION";
503 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
504 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
505 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
509 var names = this.comment.getTag("name");
511 this.setName(names[0].desc);
519 var properties = this.comment.getTag("property");
520 if (properties.length) {
521 thisProperties = this.properties;
522 for (var i = 0; i < properties.length; i++) {
523 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
524 // TODO: shouldn't the following happen in the addProperty method of Symbol?
525 property.name = properties[i].name;
526 property.memberOf = this.alias;
527 if (properties[i].type) property.type = properties[i].type;
528 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
529 this.addProperty(property);
530 imports.Parser.Parser.addSymbol(property);
535 var conf = this.comment.getTag("cfg");
537 for (var i = 0; i < conf.length; i++) {
538 this.addConfig(conf[i]);
547 var returns = this.comment.getTag("return");
548 if (returns.length) { // there can be many return tags in a single doclet
549 this.returns = returns;
550 this.type = returns.map(function($){return $.type}).join(", ");
558 this.exceptions = this.comment.getTag("throws");
565 var requires = this.comment.getTag("requires");
566 if (requires.length) {
567 this.requires = requires.map(function($){return $.desc});
575 var types = this.comment.getTag("type");
577 this.type = types[0].desc; //multiple type tags are ignored
585 if (this.comment.getTag("private").length || this.isInner) {
586 this.isPrivate = true;
590 if (this.comment.getTag("ignore").length) {
591 this.isIgnored = true;
598 // @inherits ... as ...
599 var inherits = this.comment.getTag("inherits");
600 if (inherits.length) {
601 for (var i = 0; i < inherits.length; i++) {
602 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
603 var inAlias = RegExp.$1;
604 var inAs = RegExp.$2 || inAlias;
606 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
609 inAs = inAs.replace(/\.prototype\.?/g, "#");
610 inAs = inAs.replace(/^this\.?/, "#");
613 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
615 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
618 inAs = this.alias + joiner + inAs;
621 this.inherits.push({alias: inAlias, as: inAs});
630 this.augments = this.comment.getTag("augments");
633 if (this.comment.getTag("extends")) {
634 this.augments = this.comment.getTag("extends");
639 var defaults = this.comment.getTag("default");
640 if (defaults.length) {
641 if (this.is("OBJECT")) {
642 this.defaultValue = defaults[0].desc;
651 var memberOfs = this.comment.getTag("memberOf");
652 if (memberOfs.length) {
653 this.memberOf = memberOfs[0].desc;
654 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
662 if (this.comment.getTag("public").length) {
663 this.isPrivate = false;
671 is : function(what) {
672 return this.isa === what;
675 isBuiltin : function() {
676 return SymbolSet.isBuiltin(this.alias);
679 setType : function(/**String*/comment, /**Boolean*/overwrite) {
680 if (!overwrite && this.type) return;
681 var typeComment = DocComment.unwrapComment(comment);
682 this.type = typeComment;
685 inherit : function(symbol) {
686 if (!this.hasMember(symbol.name) && !symbol.isInner) {
687 if (symbol.is("FUNCTION"))
688 this.methods.push(symbol);
689 else if (symbol.is("OBJECT"))
690 this.properties.push(symbol);
694 hasMember : function(name) {
695 return (this.hasMethod(name) || this.hasProperty(name));
698 addMember : function(symbol) {
699 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
701 if (symbol.comment.getTag("cfg").length == 1) {
702 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
703 this.addConfig(symbol.comment.getTag("cfg")[0]);
707 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
708 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
711 hasMethod : function(name) {
712 var thisMethods = this.methods;
713 for (var i = 0, l = thisMethods.length; i < l; i++) {
714 if (thisMethods[i].name == name) return true;
715 if (thisMethods[i].alias == name) return true;
720 addMethod : function(symbol) {
721 var methodAlias = symbol.alias;
722 var thisMethods = this.methods;
723 for (var i = 0, l = thisMethods.length; i < l; i++) {
724 if (thisMethods[i].alias == methodAlias) {
725 thisMethods[i] = symbol; // overwriting previous method
729 thisMethods.push(symbol); // new method with this alias
732 hasProperty : function(name) {
733 var thisProperties = this.properties;
734 for (var i = 0, l = thisProperties.length; i < l; i++) {
735 if (thisProperties[i].name == name) return true;
736 if (thisProperties[i].alias == name) return true;
741 addProperty : function(symbol) {
742 var propertyAlias = symbol.alias;
743 var thisProperties = this.properties;
744 for (var i = 0, l = thisProperties.length; i < l; i++) {
745 if (thisProperties[i].alias == propertyAlias) {
746 thisProperties[i] = symbol; // overwriting previous property
751 thisProperties.push(symbol); // new property with this alias
754 addDocTag : function(docTag)
756 this.comment.tags.push(docTag);
757 if (docTag.title == 'cfg') {
758 this.addConfig(docTag);
763 addConfig : function(docTag)
765 if (typeof(docTag['memberOf']) == 'undefined') {
766 // remove prototype data...
767 //var a = this.alias.split('#')[0];
768 //docTag.memberOf = a;
769 docTag.memberOf = this.alias;
771 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
772 this.cfgs[docTag.name] = docTag;
776 configToArray: function()
779 for(var ci in this.cfgs) {
780 // dont show hidden!!
781 if (this.cfgs[ci].desc.match(/@hide/)) {
784 r.push(this.cfgs[ci]);
792 * Elements that are not serialized
796 '$args' // not needed AFAIK
799 Symbol.srcFile = ""; //running reference to the current file being parsed
802 Symbol.fromDump = function(t)
804 var ns = new Symbol();
806 if (typeof(ns[i]) == "undefined") {
807 println("ERR:no default for Symbol:"+ i);