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.
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);
94 ret[i] = this[i].toArray();
97 print("object? :" + i);
104 ret[i] = this[i]; continue;
106 print("unknown type:" + typeof(this[i]));
117 this.defaultValue = "";
125 this.comment = { isUserComment: false };
126 //this.defaultValue = null;
127 this.deprecated = "";
131 this.exceptions = [];
133 this.inheritsFrom = [];
134 this.isa = "OBJECT"; // OBJECT//FUNCTION
135 this.isEvent = false;
136 this.isConstant = false;
137 this.isIgnored = false;
138 this.isInner = false;
139 this.isNamespace = false;
140 this.isPrivate = false;
141 this.isStatic = false;
146 this.properties = [];
154 this.childClasses = [];
159 serialize : function() {
161 for (var p in this) {
167 for (var i in keys) {
168 if (typeof this[keys[i]] == "function") continue;
169 out += " " +keys[i]+" => "+
171 (typeof(this[keys[i]]) != "object") ?
173 "[" +typeof(this[keys[i]])+"]"
177 return "\n{\n" + out + "}\n";
181 var clone = new Symbol();
182 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
183 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
190 //__defineSetter__("name",
191 setName : function(n) {
192 n = n.replace(/^_global_[.#-]/, "");
193 n = n.replace(/\.prototype\.?/g, '#');
194 n = n.replace(/#$/g, '');
196 this.name = n; // real!
199 //__defineGetter__("name",
200 getName : function() { return this._name; },
202 //__defineSetter__("params",
203 setParams :function(v) {
204 for (var i = 0, l = v.length; i < l; i++) {
205 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
206 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
207 this._params[i] = new DocTag(
208 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
211 this._params[i] = v[i];
214 this.params = this._params;
219 //__defineGetter__("params",
220 getParams : function() { return this._params; },
225 /** Object[] */ params,
227 /** DocComment */ comment
229 this.$args = arguments;
230 //println("Symbol created: " + isa + ":" + name);
232 this.alias = this.getName();
233 this.setParams(params);
234 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
235 this.comment = comment || new DocComment("");
236 this.srcFile = Symbol.srcFile;
240 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
244 //if (typeof PluginManager != "undefined") {
245 // PluginManager.run("onSymbol", this);
249 setTags : function() {
251 var authors = this.comment.getTag("author");
252 if (authors.length) {
253 this.author = authors.map(function($){return $.desc;}).join(", ");
257 assert("testing Symbol");
259 requires("../lib/JSDOC/DocComment.js");
260 requires("../frame/String.js");
261 requires("../lib/JSDOC/DocTag.js");
263 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
264 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
267 var mth = this.comment.getTag("method");
269 this.isa = "FUNCTION";
272 var descs = this.comment.getTag("desc");
274 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
278 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
279 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
283 if (this.is("FILE")) {
284 if (!this.alias) this.alias = this.srcFile;
286 var overviews = this.comment.getTag("overview");
287 if (overviews.length) {
288 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
293 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
294 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
298 var sinces = this.comment.getTag("since");
300 this.since = sinces.map(function($){return $.desc;}).join(", ");
304 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
305 assertEqual(sym.since, "1.01", "@since tag, description is found.");
309 if (this.comment.getTag("constant").length) {
310 this.isConstant = true;
315 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
316 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
320 var versions = this.comment.getTag("version");
321 if (versions.length) {
322 this.version = versions.map(function($){return $.desc;}).join(", ");
326 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
327 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
331 var deprecateds = this.comment.getTag("deprecated");
332 if (deprecateds.length) {
333 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
337 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
338 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
342 var examples = this.comment.getTag("example");
343 if (examples.length) {
344 this.example = examples[0];
348 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
349 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
353 var sees = this.comment.getTag("see");
355 var thisSee = this.see;
356 sees.map(function($){thisSee.push($.desc);});
360 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
361 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
365 var classes = this.comment.getTag("class");
366 if (classes.length) {
367 this.isa = "CONSTRUCTOR";
368 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
369 if (!this.classDesc) {
370 this.classDesc = this.desc;
377 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
378 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
379 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
383 var namespaces = this.comment.getTag("namespace");
384 if (namespaces.length) {
385 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
386 this.isNamespace = true;
390 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
391 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
395 var params = this.comment.getTag("param");
397 // user-defined params overwrite those with same name defined by the parser
398 var thisParams = this.getParams();
400 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
401 this.setParams(params);
403 else { // need to overlay these user-defined params on to existing parser-defined params
404 for (var i = 0, l = params.length; i < l; i++) {
406 if (params[i].type) thisParams[i].type = params[i].type;
407 thisParams[i].name = params[i].name;
408 thisParams[i].desc = params[i].desc;
409 thisParams[i].isOptional = params[i].isOptional;
410 thisParams[i].defaultValue = params[i].defaultValue;
412 else thisParams[i] = params[i];
418 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
419 assertEqual(sym.params.length, 1, "parser defined param is found.");
421 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
422 assertEqual(sym.params.length, 1, "user defined param is found.");
423 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
424 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
426 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
427 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
428 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
429 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
431 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
432 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
433 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
434 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
436 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
437 assertEqual(sym.params.length, 1, "user defined param with description is found.");
438 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
442 if (this.comment.getTag("constructor").length) {
443 this.isa = "CONSTRUCTOR";
447 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
448 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
452 if (this.comment.getTag("static").length) {
453 this.isStatic = true;
454 if (this.isa == "CONSTRUCTOR") {
455 this.isNamespace = true;
460 if (this.comment.getTag("singleton").length) {
461 this.isStatic = true;
462 //if (this.isa == "CONSTRUCTOR") {
463 // this.isNamespace = true;
470 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
471 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
472 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
476 if (this.comment.getTag("inner").length) {
478 this.isStatic = false;
482 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
483 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
484 assertEqual(sym.isInner, true, "@inner makes isInner true.");
488 if (this.comment.getTag("field").length) {
493 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
494 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
498 if (this.comment.getTag("function").length) {
499 this.isa = "FUNCTION";
503 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
504 // change a property to a function..
505 this.isa = "FUNCTION";
510 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
511 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
515 var events = this.comment.getTag("event");
517 this.isa = "FUNCTION";
522 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
523 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
524 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
528 var names = this.comment.getTag("name");
530 this.setName(names[0].desc);
538 var properties = this.comment.getTag("property");
539 if (properties.length) {
540 thisProperties = this.properties;
541 for (var i = 0; i < properties.length; i++) {
542 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
543 // TODO: shouldn't the following happen in the addProperty method of Symbol?
544 property.name = properties[i].name;
545 property.memberOf = this.alias;
546 if (properties[i].type) property.type = properties[i].type;
547 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
548 this.addProperty(property);
549 imports.Parser.Parser.addSymbol(property);
554 var conf = this.comment.getTag("cfg");
556 for (var i = 0; i < conf.length; i++) {
557 this.addConfig(conf[i]);
566 var returns = this.comment.getTag("return");
567 if (returns.length) { // there can be many return tags in a single doclet
568 this.returns = returns;
569 this.type = returns.map(function($){return $.type}).join(", ");
577 this.exceptions = this.comment.getTag("throws");
584 var requires = this.comment.getTag("requires");
585 if (requires.length) {
586 this.requires = requires.map(function($){return $.desc});
594 var types = this.comment.getTag("type");
596 this.type = types[0].desc; //multiple type tags are ignored
604 if (this.comment.getTag("private").length || this.isInner) {
605 this.isPrivate = true;
609 if (this.comment.getTag("ignore").length) {
610 this.isIgnored = true;
617 // @inherits ... as ...
618 var inherits = this.comment.getTag("inherits");
619 if (inherits.length) {
620 for (var i = 0; i < inherits.length; i++) {
621 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
622 var inAlias = RegExp.$1;
623 var inAs = RegExp.$2 || inAlias;
625 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
628 inAs = inAs.replace(/\.prototype\.?/g, "#");
629 inAs = inAs.replace(/^this\.?/, "#");
632 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
634 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
637 inAs = this.alias + joiner + inAs;
640 this.inherits.push({alias: inAlias, as: inAs});
649 this.augments = this.comment.getTag("augments");
652 if (this.comment.getTag("extends")) {
653 this.augments = this.comment.getTag("extends");
658 var defaults = this.comment.getTag("default");
659 if (defaults.length) {
660 if (this.is("OBJECT")) {
661 this.defaultValue = defaults[0].desc;
670 var memberOfs = this.comment.getTag("memberOf");
671 if (memberOfs.length) {
672 this.memberOf = memberOfs[0].desc;
673 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
681 if (this.comment.getTag("public").length) {
682 this.isPrivate = false;
690 is : function(what) {
691 return this.isa === what;
694 isBuiltin : function() {
695 return SymbolSet.isBuiltin(this.alias);
698 setType : function(/**String*/comment, /**Boolean*/overwrite) {
699 if (!overwrite && this.type) return;
700 var typeComment = DocComment.unwrapComment(comment);
701 this.type = typeComment;
704 inherit : function(symbol) {
705 if (!this.hasMember(symbol.name) && !symbol.isInner) {
706 if (symbol.is("FUNCTION"))
707 this.methods.push(symbol);
708 else if (symbol.is("OBJECT"))
709 this.properties.push(symbol);
713 hasMember : function(name) {
714 return (this.hasMethod(name) || this.hasProperty(name));
717 addMember : function(symbol) {
718 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
720 if (symbol.comment.getTag("cfg").length == 1) {
721 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
722 this.addConfig(symbol.comment.getTag("cfg")[0]);
726 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
727 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
730 hasMethod : function(name) {
731 var thisMethods = this.methods;
732 for (var i = 0, l = thisMethods.length; i < l; i++) {
733 if (thisMethods[i].name == name) return true;
734 if (thisMethods[i].alias == name) return true;
739 addMethod : function(symbol) {
740 var methodAlias = symbol.alias;
741 var thisMethods = this.methods;
742 for (var i = 0, l = thisMethods.length; i < l; i++) {
743 if (thisMethods[i].alias == methodAlias) {
744 thisMethods[i] = symbol; // overwriting previous method
748 thisMethods.push(symbol); // new method with this alias
751 hasProperty : function(name) {
752 var thisProperties = this.properties;
753 for (var i = 0, l = thisProperties.length; i < l; i++) {
754 if (thisProperties[i].name == name) return true;
755 if (thisProperties[i].alias == name) return true;
760 addProperty : function(symbol) {
761 var propertyAlias = symbol.alias;
762 var thisProperties = this.properties;
763 for (var i = 0, l = thisProperties.length; i < l; i++) {
764 if (thisProperties[i].alias == propertyAlias) {
765 thisProperties[i] = symbol; // overwriting previous property
770 thisProperties.push(symbol); // new property with this alias
773 addDocTag : function(docTag)
775 this.comment.tags.push(docTag);
776 if (docTag.title == 'cfg') {
777 this.addConfig(docTag);
782 addConfig : function(docTag)
784 if (typeof(docTag['memberOf']) == 'undefined') {
785 // remove prototype data...
786 //var a = this.alias.split('#')[0];
787 //docTag.memberOf = a;
788 docTag.memberOf = this.alias;
790 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
791 this.cfgs[docTag.name] = docTag;
795 configToArray: function()
798 for(var ci in this.cfgs) {
799 // dont show hidden!!
800 if (this.cfgs[ci].desc.match(/@hide/)) {
803 r.push(this.cfgs[ci]);
811 * Elements that are not serialized
815 '$args' // not needed AFAIK
818 Symbol.srcFile = ""; //running reference to the current file being parsed
821 Symbol.fromDump = function(t)
823 var ns = new Symbol();
825 if (typeof(ns[i]) == "undefined") {
826 println("ERR:no default for Symbol:"+ i);