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[]
70 var ret = { '*object' : 'Symbol' };
72 if (Symbol.hide.indexOf(i) > -1) {
75 switch (typeof(this[i])) {
91 case 'cfgs': // key val of doctags..
102 print("object? :" + i);
110 ret[i] = this[i]; continue;
112 print("unknown type:" + typeof(this[i]));
122 // only initialize arrays / objects..
127 this.exceptions = [];
130 this.isa = "OBJECT"; // OBJECT//FUNCTION
133 this.properties = [];
142 this.inheritsFrom = [];
143 this.childClasses = [];
145 this.comment = new DocComment();
146 this.comment.isUserComment = false;
151 serialize : function() {
153 for (var p in this) {
159 for (var i in keys) {
160 if (typeof this[keys[i]] == "function") continue;
161 out += " " +keys[i]+" => "+
163 (typeof(this[keys[i]]) != "object") ?
165 "[" +typeof(this[keys[i]])+"]"
169 return "\n{\n" + out + "}\n";
173 var clone = new Symbol();
174 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
175 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
182 //__defineSetter__("name",
183 setName : function(n) {
184 n = n.replace(/^_global_[.#-]/, "");
185 n = n.replace(/\.prototype\.?/g, '#');
186 n = n.replace(/#$/g, '');
188 this.name = n; // real!
191 //__defineGetter__("name",
192 getName : function() { return this._name; },
194 //__defineSetter__("params",
195 setParams :function(v) {
196 for (var i = 0, l = v.length; i < l; i++) {
197 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
198 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
199 this._params[i] = new DocTag(
200 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
203 this._params[i] = v[i];
206 this.params = this._params;
211 //__defineGetter__("params",
212 getParams : function() { return this._params; },
217 /** Object[] */ params,
219 /** DocComment */ comment
221 this.$args = arguments;
222 //println("Symbol created: " + isa + ":" + name);
224 this.alias = this.getName();
225 this.setParams(params);
226 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
227 this.comment = comment || new DocComment("");
228 this.srcFile = Symbol.srcFile;
232 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
236 //if (typeof PluginManager != "undefined") {
237 // PluginManager.run("onSymbol", this);
241 setTags : function() {
243 var authors = this.comment.getTag("author");
244 if (authors.length) {
245 this.author = authors.map(function($){return $.desc;}).join(", ");
249 assert("testing Symbol");
251 requires("../lib/JSDOC/DocComment.js");
252 requires("../frame/String.js");
253 requires("../lib/JSDOC/DocTag.js");
255 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
256 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
259 var mth = this.comment.getTag("method");
261 this.isa = "FUNCTION";
264 var descs = this.comment.getTag("desc");
266 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
270 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
271 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
275 if (this.is("FILE")) {
276 if (!this.alias) this.alias = this.srcFile;
278 var overviews = this.comment.getTag("overview");
279 if (overviews.length) {
280 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
285 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
286 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
290 var sinces = this.comment.getTag("since");
292 this.since = sinces.map(function($){return $.desc;}).join(", ");
296 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
297 assertEqual(sym.since, "1.01", "@since tag, description is found.");
301 if (this.comment.getTag("constant").length) {
302 this.isConstant = true;
307 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
308 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
312 var versions = this.comment.getTag("version");
313 if (versions.length) {
314 this.version = versions.map(function($){return $.desc;}).join(", ");
318 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
319 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
323 var deprecateds = this.comment.getTag("deprecated");
324 if (deprecateds.length) {
325 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
329 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
330 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
334 var examples = this.comment.getTag("example");
335 if (examples.length) {
336 this.example = examples[0];
340 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
341 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
345 var sees = this.comment.getTag("see");
347 var thisSee = this.see;
348 sees.map(function($){thisSee.push($.desc);});
352 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
353 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
357 var classes = this.comment.getTag("class");
358 if (classes.length) {
359 this.isa = "CONSTRUCTOR";
360 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
361 if (!this.classDesc) {
362 this.classDesc = this.desc;
369 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
370 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
371 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
375 var namespaces = this.comment.getTag("namespace");
376 if (namespaces.length) {
377 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
378 this.isNamespace = true;
382 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
383 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
387 var params = this.comment.getTag("param");
389 // user-defined params overwrite those with same name defined by the parser
390 var thisParams = this.getParams();
392 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
393 this.setParams(params);
395 else { // need to overlay these user-defined params on to existing parser-defined params
396 for (var i = 0, l = params.length; i < l; i++) {
398 if (params[i].type) thisParams[i].type = params[i].type;
399 thisParams[i].name = params[i].name;
400 thisParams[i].desc = params[i].desc;
401 thisParams[i].isOptional = params[i].isOptional;
402 thisParams[i].defaultValue = params[i].defaultValue;
404 else thisParams[i] = params[i];
410 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
411 assertEqual(sym.params.length, 1, "parser defined param is found.");
413 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
414 assertEqual(sym.params.length, 1, "user defined param is found.");
415 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
416 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
418 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
419 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
420 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
421 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
423 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
424 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
425 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
426 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
428 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
429 assertEqual(sym.params.length, 1, "user defined param with description is found.");
430 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
434 if (this.comment.getTag("constructor").length) {
435 this.isa = "CONSTRUCTOR";
439 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
440 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
444 if (this.comment.getTag("static").length) {
445 this.isStatic = true;
446 if (this.isa == "CONSTRUCTOR") {
447 this.isNamespace = true;
452 if (this.comment.getTag("singleton").length) {
453 this.isStatic = true;
454 //if (this.isa == "CONSTRUCTOR") {
455 // this.isNamespace = true;
462 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
463 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
464 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
468 if (this.comment.getTag("inner").length) {
470 this.isStatic = false;
474 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
475 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
476 assertEqual(sym.isInner, true, "@inner makes isInner true.");
480 if (this.comment.getTag("field").length) {
485 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
486 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
490 if (this.comment.getTag("function").length) {
491 this.isa = "FUNCTION";
495 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
496 // change a property to a function..
497 this.isa = "FUNCTION";
502 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
503 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
507 var events = this.comment.getTag("event");
509 this.isa = "FUNCTION";
514 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
515 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
516 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
520 var names = this.comment.getTag("name");
522 this.setName(names[0].desc);
530 var properties = this.comment.getTag("property");
531 if (properties.length) {
532 thisProperties = this.properties;
533 for (var i = 0; i < properties.length; i++) {
534 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
535 // TODO: shouldn't the following happen in the addProperty method of Symbol?
536 property.name = properties[i].name;
537 property.memberOf = this.alias;
538 if (properties[i].type) property.type = properties[i].type;
539 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
540 this.addProperty(property);
541 imports.Parser.Parser.addSymbol(property);
546 var conf = this.comment.getTag("cfg");
548 for (var i = 0; i < conf.length; i++) {
549 this.addConfig(conf[i]);
558 var returns = this.comment.getTag("return");
559 if (returns.length) { // there can be many return tags in a single doclet
560 this.returns = returns;
561 this.type = returns.map(function($){return $.type}).join(", ");
569 this.exceptions = this.comment.getTag("throws");
576 var requires = this.comment.getTag("requires");
577 if (requires.length) {
578 this.requires = requires.map(function($){return $.desc});
586 var types = this.comment.getTag("type");
588 this.type = types[0].desc; //multiple type tags are ignored
596 if (this.comment.getTag("private").length || this.isInner) {
597 this.isPrivate = true;
601 if (this.comment.getTag("ignore").length) {
602 this.isIgnored = true;
609 // @inherits ... as ...
610 var inherits = this.comment.getTag("inherits");
611 if (inherits.length) {
612 for (var i = 0; i < inherits.length; i++) {
613 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
614 var inAlias = RegExp.$1;
615 var inAs = RegExp.$2 || inAlias;
617 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
620 inAs = inAs.replace(/\.prototype\.?/g, "#");
621 inAs = inAs.replace(/^this\.?/, "#");
624 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
626 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
629 inAs = this.alias + joiner + inAs;
632 this.inherits.push({alias: inAlias, as: inAs});
641 this.augments = this.comment.getTag("augments");
644 if (this.comment.getTag("extends")) {
645 this.augments = this.comment.getTag("extends");
650 var defaults = this.comment.getTag("default");
651 if (defaults.length) {
652 if (this.is("OBJECT")) {
653 this.defaultValue = defaults[0].desc;
662 var memberOfs = this.comment.getTag("memberOf");
663 if (memberOfs.length) {
664 this.memberOf = memberOfs[0].desc;
665 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
666 this.name = this.name.split('.').pop();
667 this.name = this.name.split('#').pop();
668 this.name = this.memberOf + this.name;
676 if (this.comment.getTag("public").length) {
677 this.isPrivate = false;
685 is : function(what) {
686 return this.isa === what;
689 isBuiltin : function() {
690 return SymbolSet.isBuiltin(this.alias);
693 setType : function(/**String*/comment, /**Boolean*/overwrite) {
694 if (!overwrite && this.type) return;
695 var typeComment = DocComment.unwrapComment(comment);
696 this.type = typeComment;
699 inherit : function(symbol) {
700 if (!this.hasMember(symbol.name) && !symbol.isInner) {
701 if (symbol.is("FUNCTION"))
702 this.methods.push(symbol);
703 else if (symbol.is("OBJECT"))
704 this.properties.push(symbol);
708 hasMember : function(name) {
709 return (this.hasMethod(name) || this.hasProperty(name));
712 addMember : function(symbol) {
713 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
715 if (symbol.comment.getTag("cfg").length == 1) {
716 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
717 this.addConfig(symbol.comment.getTag("cfg")[0]);
721 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
722 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
725 hasMethod : function(name) {
726 var thisMethods = this.methods;
727 for (var i = 0, l = thisMethods.length; i < l; i++) {
728 if (thisMethods[i].name == name) return true;
729 if (thisMethods[i].alias == name) return true;
734 addMethod : function(symbol) {
735 var methodAlias = symbol.alias;
736 var thisMethods = this.methods;
737 for (var i = 0, l = thisMethods.length; i < l; i++) {
738 if (thisMethods[i].alias == methodAlias) {
739 thisMethods[i] = symbol; // overwriting previous method
743 thisMethods.push(symbol); // new method with this alias
746 hasProperty : function(name) {
747 var thisProperties = this.properties;
748 for (var i = 0, l = thisProperties.length; i < l; i++) {
749 if (thisProperties[i].name == name) return true;
750 if (thisProperties[i].alias == name) return true;
755 addProperty : function(symbol) {
756 var propertyAlias = symbol.alias;
757 var thisProperties = this.properties;
758 for (var i = 0, l = thisProperties.length; i < l; i++) {
759 if (thisProperties[i].alias == propertyAlias) {
760 thisProperties[i] = symbol; // overwriting previous property
765 thisProperties.push(symbol); // new property with this alias
768 addDocTag : function(docTag)
770 this.comment.tags.push(docTag);
771 if (docTag.title == 'cfg') {
772 this.addConfig(docTag);
777 addConfig : function(docTag)
779 if (typeof(docTag['memberOf']) == 'undefined') {
780 // remove prototype data...
781 //var a = this.alias.split('#')[0];
782 //docTag.memberOf = a;
783 docTag.memberOf = this.alias;
785 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
786 this.cfgs[docTag.name] = docTag;
790 configToArray: function()
793 for(var ci in this.cfgs) {
794 // dont show hidden!!
795 if (this.cfgs[ci].desc.match(/@hide/)) {
798 r.push(this.cfgs[ci]);
806 * Elements that are not serialized
810 '$args' // not needed AFAIK
813 Symbol.srcFile = ""; //running reference to the current file being parsed
816 Symbol.fromDump = function(t)
818 var ns = new Symbol();
820 if (typeof(ns[i]) == "undefined") {
821 println("ERR:no default for Symbol:"+ i);