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
69 function array2object(a)
73 a.forEach(function(e) {
81 if (Symbol.hide.indexOf(i) > -1) {
84 switch (typeof(this[i])) {
94 ret[i] = array2object(this[i],true);
100 print(JSON.stringify(this[i]));
101 ret[i] = this[i].toObject();
105 print("object? :" + i);
112 ret[i] = this[i]; continue;
114 print("unknown type:" + typeof(this[i]));
124 // only initialize arrays / objects..
129 this.exceptions = [];
131 this.inheritsFrom = [];
132 this.isa = "OBJECT"; // OBJECT//FUNCTION
135 this.properties = [];
140 this.childClasses = [];
143 this.comment = new DocComment();
144 this.comment.isUserComment = false;
149 serialize : function() {
151 for (var p in this) {
157 for (var i in keys) {
158 if (typeof this[keys[i]] == "function") continue;
159 out += " " +keys[i]+" => "+
161 (typeof(this[keys[i]]) != "object") ?
163 "[" +typeof(this[keys[i]])+"]"
167 return "\n{\n" + out + "}\n";
171 var clone = new Symbol();
172 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
173 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
180 //__defineSetter__("name",
181 setName : function(n) {
182 n = n.replace(/^_global_[.#-]/, "");
183 n = n.replace(/\.prototype\.?/g, '#');
184 n = n.replace(/#$/g, '');
186 this.name = n; // real!
189 //__defineGetter__("name",
190 getName : function() { return this._name; },
192 //__defineSetter__("params",
193 setParams :function(v) {
194 for (var i = 0, l = v.length; i < l; i++) {
195 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
196 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
197 this._params[i] = new DocTag(
198 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
201 this._params[i] = v[i];
204 this.params = this._params;
209 //__defineGetter__("params",
210 getParams : function() { return this._params; },
215 /** Object[] */ params,
217 /** DocComment */ comment
219 this.$args = arguments;
220 //println("Symbol created: " + isa + ":" + name);
222 this.alias = this.getName();
223 this.setParams(params);
224 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
225 this.comment = comment || new DocComment("");
226 this.srcFile = Symbol.srcFile;
230 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
234 //if (typeof PluginManager != "undefined") {
235 // PluginManager.run("onSymbol", this);
239 setTags : function() {
241 var authors = this.comment.getTag("author");
242 if (authors.length) {
243 this.author = authors.map(function($){return $.desc;}).join(", ");
247 assert("testing Symbol");
249 requires("../lib/JSDOC/DocComment.js");
250 requires("../frame/String.js");
251 requires("../lib/JSDOC/DocTag.js");
253 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
254 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
257 var mth = this.comment.getTag("method");
259 this.isa = "FUNCTION";
262 var descs = this.comment.getTag("desc");
264 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
268 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
269 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
273 if (this.is("FILE")) {
274 if (!this.alias) this.alias = this.srcFile;
276 var overviews = this.comment.getTag("overview");
277 if (overviews.length) {
278 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
283 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
284 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
288 var sinces = this.comment.getTag("since");
290 this.since = sinces.map(function($){return $.desc;}).join(", ");
294 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
295 assertEqual(sym.since, "1.01", "@since tag, description is found.");
299 if (this.comment.getTag("constant").length) {
300 this.isConstant = true;
305 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
306 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
310 var versions = this.comment.getTag("version");
311 if (versions.length) {
312 this.version = versions.map(function($){return $.desc;}).join(", ");
316 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
317 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
321 var deprecateds = this.comment.getTag("deprecated");
322 if (deprecateds.length) {
323 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
327 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
328 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
332 var examples = this.comment.getTag("example");
333 if (examples.length) {
334 this.example = examples[0];
338 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
339 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
343 var sees = this.comment.getTag("see");
345 var thisSee = this.see;
346 sees.map(function($){thisSee.push($.desc);});
350 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
351 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
355 var classes = this.comment.getTag("class");
356 if (classes.length) {
357 this.isa = "CONSTRUCTOR";
358 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
359 if (!this.classDesc) {
360 this.classDesc = this.desc;
367 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
368 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
369 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
373 var namespaces = this.comment.getTag("namespace");
374 if (namespaces.length) {
375 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
376 this.isNamespace = true;
380 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
381 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
385 var params = this.comment.getTag("param");
387 // user-defined params overwrite those with same name defined by the parser
388 var thisParams = this.getParams();
390 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
391 this.setParams(params);
393 else { // need to overlay these user-defined params on to existing parser-defined params
394 for (var i = 0, l = params.length; i < l; i++) {
396 if (params[i].type) thisParams[i].type = params[i].type;
397 thisParams[i].name = params[i].name;
398 thisParams[i].desc = params[i].desc;
399 thisParams[i].isOptional = params[i].isOptional;
400 thisParams[i].defaultValue = params[i].defaultValue;
402 else thisParams[i] = params[i];
408 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
409 assertEqual(sym.params.length, 1, "parser defined param is found.");
411 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
412 assertEqual(sym.params.length, 1, "user defined param is found.");
413 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
414 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
416 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
417 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
418 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
419 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
421 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
422 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
423 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
424 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
426 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
427 assertEqual(sym.params.length, 1, "user defined param with description is found.");
428 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
432 if (this.comment.getTag("constructor").length) {
433 this.isa = "CONSTRUCTOR";
437 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
438 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
442 if (this.comment.getTag("static").length) {
443 this.isStatic = true;
444 if (this.isa == "CONSTRUCTOR") {
445 this.isNamespace = true;
450 if (this.comment.getTag("singleton").length) {
451 this.isStatic = true;
452 //if (this.isa == "CONSTRUCTOR") {
453 // this.isNamespace = true;
460 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
461 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
462 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
466 if (this.comment.getTag("inner").length) {
468 this.isStatic = false;
472 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
473 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
474 assertEqual(sym.isInner, true, "@inner makes isInner true.");
478 if (this.comment.getTag("field").length) {
483 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
484 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
488 if (this.comment.getTag("function").length) {
489 this.isa = "FUNCTION";
493 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
494 // change a property to a function..
495 this.isa = "FUNCTION";
500 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
501 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
505 var events = this.comment.getTag("event");
507 this.isa = "FUNCTION";
512 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
513 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
514 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
518 var names = this.comment.getTag("name");
520 this.setName(names[0].desc);
528 var properties = this.comment.getTag("property");
529 if (properties.length) {
530 thisProperties = this.properties;
531 for (var i = 0; i < properties.length; i++) {
532 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
533 // TODO: shouldn't the following happen in the addProperty method of Symbol?
534 property.name = properties[i].name;
535 property.memberOf = this.alias;
536 if (properties[i].type) property.type = properties[i].type;
537 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
538 this.addProperty(property);
539 imports.Parser.Parser.addSymbol(property);
544 var conf = this.comment.getTag("cfg");
546 for (var i = 0; i < conf.length; i++) {
547 this.addConfig(conf[i]);
556 var returns = this.comment.getTag("return");
557 if (returns.length) { // there can be many return tags in a single doclet
558 this.returns = returns;
559 this.type = returns.map(function($){return $.type}).join(", ");
567 this.exceptions = this.comment.getTag("throws");
574 var requires = this.comment.getTag("requires");
575 if (requires.length) {
576 this.requires = requires.map(function($){return $.desc});
584 var types = this.comment.getTag("type");
586 this.type = types[0].desc; //multiple type tags are ignored
594 if (this.comment.getTag("private").length || this.isInner) {
595 this.isPrivate = true;
599 if (this.comment.getTag("ignore").length) {
600 this.isIgnored = true;
607 // @inherits ... as ...
608 var inherits = this.comment.getTag("inherits");
609 if (inherits.length) {
610 for (var i = 0; i < inherits.length; i++) {
611 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
612 var inAlias = RegExp.$1;
613 var inAs = RegExp.$2 || inAlias;
615 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
618 inAs = inAs.replace(/\.prototype\.?/g, "#");
619 inAs = inAs.replace(/^this\.?/, "#");
622 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
624 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
627 inAs = this.alias + joiner + inAs;
630 this.inherits.push({alias: inAlias, as: inAs});
639 this.augments = this.comment.getTag("augments");
642 if (this.comment.getTag("extends")) {
643 this.augments = this.comment.getTag("extends");
648 var defaults = this.comment.getTag("default");
649 if (defaults.length) {
650 if (this.is("OBJECT")) {
651 this.defaultValue = defaults[0].desc;
660 var memberOfs = this.comment.getTag("memberOf");
661 if (memberOfs.length) {
662 this.memberOf = memberOfs[0].desc;
663 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
671 if (this.comment.getTag("public").length) {
672 this.isPrivate = false;
680 is : function(what) {
681 return this.isa === what;
684 isBuiltin : function() {
685 return SymbolSet.isBuiltin(this.alias);
688 setType : function(/**String*/comment, /**Boolean*/overwrite) {
689 if (!overwrite && this.type) return;
690 var typeComment = DocComment.unwrapComment(comment);
691 this.type = typeComment;
694 inherit : function(symbol) {
695 if (!this.hasMember(symbol.name) && !symbol.isInner) {
696 if (symbol.is("FUNCTION"))
697 this.methods.push(symbol);
698 else if (symbol.is("OBJECT"))
699 this.properties.push(symbol);
703 hasMember : function(name) {
704 return (this.hasMethod(name) || this.hasProperty(name));
707 addMember : function(symbol) {
708 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
710 if (symbol.comment.getTag("cfg").length == 1) {
711 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
712 this.addConfig(symbol.comment.getTag("cfg")[0]);
716 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
717 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
720 hasMethod : function(name) {
721 var thisMethods = this.methods;
722 for (var i = 0, l = thisMethods.length; i < l; i++) {
723 if (thisMethods[i].name == name) return true;
724 if (thisMethods[i].alias == name) return true;
729 addMethod : function(symbol) {
730 var methodAlias = symbol.alias;
731 var thisMethods = this.methods;
732 for (var i = 0, l = thisMethods.length; i < l; i++) {
733 if (thisMethods[i].alias == methodAlias) {
734 thisMethods[i] = symbol; // overwriting previous method
738 thisMethods.push(symbol); // new method with this alias
741 hasProperty : function(name) {
742 var thisProperties = this.properties;
743 for (var i = 0, l = thisProperties.length; i < l; i++) {
744 if (thisProperties[i].name == name) return true;
745 if (thisProperties[i].alias == name) return true;
750 addProperty : function(symbol) {
751 var propertyAlias = symbol.alias;
752 var thisProperties = this.properties;
753 for (var i = 0, l = thisProperties.length; i < l; i++) {
754 if (thisProperties[i].alias == propertyAlias) {
755 thisProperties[i] = symbol; // overwriting previous property
760 thisProperties.push(symbol); // new property with this alias
763 addDocTag : function(docTag)
765 this.comment.tags.push(docTag);
766 if (docTag.title == 'cfg') {
767 this.addConfig(docTag);
772 addConfig : function(docTag)
774 if (typeof(docTag['memberOf']) == 'undefined') {
775 // remove prototype data...
776 //var a = this.alias.split('#')[0];
777 //docTag.memberOf = a;
778 docTag.memberOf = this.alias;
780 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
781 this.cfgs[docTag.name] = docTag;
785 configToArray: function()
788 for(var ci in this.cfgs) {
789 // dont show hidden!!
790 if (this.cfgs[ci].desc.match(/@hide/)) {
793 r.push(this.cfgs[ci]);
801 * Elements that are not serialized
805 '$args' // not needed AFAIK
808 Symbol.srcFile = ""; //running reference to the current file being parsed
811 Symbol.fromDump = function(t)
813 var ns = new Symbol();
815 if (typeof(ns[i]) == "undefined") {
816 println("ERR:no default for Symbol:"+ i);