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;
113 print("unknown type:" + typeof(this[i]));
123 // only initialize arrays / objects..
128 this.exceptions = [];
131 this.isa = "OBJECT"; // OBJECT//FUNCTION
134 this.properties = [];
143 this.inheritsFrom = [];
144 this.childClasses = [];
146 this.comment = new DocComment();
147 this.comment.isUserComment = false;
152 serialize : function() {
154 for (var p in this) {
160 for (var i in keys) {
161 if (typeof this[keys[i]] == "function") continue;
162 out += " " +keys[i]+" => "+
164 (typeof(this[keys[i]]) != "object") ?
166 "[" +typeof(this[keys[i]])+"]"
170 return "\n{\n" + out + "}\n";
174 var clone = new Symbol();
175 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
176 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
183 //__defineSetter__("name",
184 setName : function(n) {
185 n = n.replace(/^_global_[.#-]/, "");
186 n = n.replace(/\.prototype\.?/g, '#');
187 n = n.replace(/#$/g, '');
189 this.name = n; // real!
192 //__defineGetter__("name",
193 getName : function() { return this._name; },
195 //__defineSetter__("params",
196 setParams :function(v) {
197 for (var i = 0, l = v.length; i < l; i++) {
198 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
199 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
200 this._params[i] = new DocTag(
201 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
204 this._params[i] = v[i];
207 this.params = this._params;
212 //__defineGetter__("params",
213 getParams : function() { return this._params; },
218 /** Object[] */ params,
220 /** DocComment */ comment
222 this.$args = arguments;
223 //println("Symbol created: " + isa + ":" + name);
225 this.alias = this.getName();
226 this.setParams(params);
227 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
228 this.comment = comment || new DocComment("");
229 this.srcFile = Symbol.srcFile;
233 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
237 //if (typeof PluginManager != "undefined") {
238 // PluginManager.run("onSymbol", this);
242 setTags : function() {
244 var authors = this.comment.getTag("author");
245 if (authors.length) {
246 this.author = authors.map(function($){return $.desc;}).join(", ");
250 assert("testing Symbol");
252 requires("../lib/JSDOC/DocComment.js");
253 requires("../frame/String.js");
254 requires("../lib/JSDOC/DocTag.js");
256 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
257 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
260 var mth = this.comment.getTag("method");
262 this.isa = "FUNCTION";
265 var descs = this.comment.getTag("desc");
267 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
271 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
272 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
276 if (this.is("FILE")) {
277 if (!this.alias) this.alias = this.srcFile;
279 var overviews = this.comment.getTag("overview");
280 if (overviews.length) {
281 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
286 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
287 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
291 var sinces = this.comment.getTag("since");
293 this.since = sinces.map(function($){return $.desc;}).join(", ");
297 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
298 assertEqual(sym.since, "1.01", "@since tag, description is found.");
302 if (this.comment.getTag("constant").length) {
303 this.isConstant = true;
308 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
309 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
313 var versions = this.comment.getTag("version");
314 if (versions.length) {
315 this.version = versions.map(function($){return $.desc;}).join(", ");
319 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
320 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
324 var deprecateds = this.comment.getTag("deprecated");
325 if (deprecateds.length) {
326 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
330 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
331 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
335 var examples = this.comment.getTag("example");
336 if (examples.length) {
337 this.example = examples[0];
341 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
342 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
346 var sees = this.comment.getTag("see");
348 var thisSee = this.see;
349 sees.map(function($){thisSee.push($.desc);});
353 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
354 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
358 var classes = this.comment.getTag("class");
359 if (classes.length) {
360 //print(JSON.stringify(this,null,4));
361 this.isa = "CONSTRUCTOR";
362 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
363 if (!this.classDesc) {
364 this.classDesc = this.desc;
371 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
372 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
373 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
377 var namespaces = this.comment.getTag("namespace");
378 if (namespaces.length) {
379 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
380 this.isNamespace = true;
384 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
385 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
389 var params = this.comment.getTag("param");
391 // user-defined params overwrite those with same name defined by the parser
392 var thisParams = this.getParams();
394 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
395 this.setParams(params);
397 else { // need to overlay these user-defined params on to existing parser-defined params
398 for (var i = 0, l = params.length; i < l; i++) {
400 if (params[i].type) thisParams[i].type = params[i].type;
401 thisParams[i].name = params[i].name;
402 thisParams[i].desc = params[i].desc;
403 thisParams[i].isOptional = params[i].isOptional;
404 thisParams[i].defaultValue = params[i].defaultValue;
406 else thisParams[i] = params[i];
412 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
413 assertEqual(sym.params.length, 1, "parser defined param is found.");
415 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
416 assertEqual(sym.params.length, 1, "user defined param is found.");
417 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
418 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
420 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
421 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
422 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
423 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
425 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
426 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
427 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
428 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
430 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
431 assertEqual(sym.params.length, 1, "user defined param with description is found.");
432 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
436 if (this.comment.getTag("constructor").length) {
437 this.isa = "CONSTRUCTOR";
441 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
442 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
446 if (this.comment.getTag("static").length) {
447 this.isStatic = true;
448 if (this.isa == "CONSTRUCTOR") {
449 this.isNamespace = true;
454 if (this.comment.getTag("singleton").length) {
455 this.isStatic = true;
456 //print('------------- got singleton ---------------' + this.isa);
457 //if (this.isa == "CONSTRUCTOR") {
458 // this.isNamespace = true;
465 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
466 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
467 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
471 if (this.comment.getTag("inner").length) {
473 this.isStatic = false;
477 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
478 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
479 assertEqual(sym.isInner, true, "@inner makes isInner true.");
483 if (this.comment.getTag("field").length) {
488 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
489 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
493 if (this.comment.getTag("function").length) {
494 this.isa = "FUNCTION";
498 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
499 // change a property to a function..
500 this.isa = "FUNCTION";
505 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
506 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
510 var events = this.comment.getTag("event");
512 this.isa = "FUNCTION";
517 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
518 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
519 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
523 var names = this.comment.getTag("name");
525 this.setName(names[0].desc);
533 var properties = this.comment.getTag("property");
534 if (properties.length) {
535 thisProperties = this.properties;
536 for (var i = 0; i < properties.length; i++) {
537 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
538 // TODO: shouldn't the following happen in the addProperty method of Symbol?
539 property.name = properties[i].name;
540 property.memberOf = this.alias;
541 if (properties[i].type) property.type = properties[i].type;
542 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
543 this.addProperty(property);
544 imports.Parser.Parser.addSymbol(property);
549 var conf = this.comment.getTag("cfg");
551 for (var i = 0; i < conf.length; i++) {
552 this.addConfig(conf[i]);
561 var returns = this.comment.getTag("return");
562 if (returns.length) { // there can be many return tags in a single doclet
563 this.returns = returns;
564 this.type = returns.map(function($){return $.type}).join(", ");
572 this.exceptions = this.comment.getTag("throws");
579 var requires = this.comment.getTag("requires");
580 if (requires.length) {
581 this.requires = requires.map(function($){return $.desc});
589 var types = this.comment.getTag("type");
591 this.type = types[0].desc; //multiple type tags are ignored
599 if (this.comment.getTag("private").length || this.isInner) {
600 this.isPrivate = true;
604 if (this.comment.getTag("ignore").length) {
605 this.isIgnored = true;
612 // @inherits ... as ...
613 var inherits = this.comment.getTag("inherits");
614 if (inherits.length) {
615 for (var i = 0; i < inherits.length; i++) {
616 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
617 var inAlias = RegExp.$1;
618 var inAs = RegExp.$2 || inAlias;
620 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
623 inAs = inAs.replace(/\.prototype\.?/g, "#");
624 inAs = inAs.replace(/^this\.?/, "#");
627 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
629 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
632 inAs = this.alias + joiner + inAs;
635 this.inherits.push({alias: inAlias, as: inAs});
644 this.augments = this.comment.getTag("augments");
647 if (this.comment.getTag("extends")) {
648 this.augments = this.comment.getTag("extends");
653 var defaults = this.comment.getTag("default");
654 if (defaults.length) {
655 if (this.is("OBJECT")) {
656 this.defaultValue = defaults[0].desc;
665 var memberOfs = this.comment.getTag("memberOf");
666 if (memberOfs.length) {
667 this.memberOf = memberOfs[0].desc;
668 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
669 this.name = this.name.split('.').pop();
670 this.name = this.name.split('#').pop();
671 this.name = this.memberOf + this.name;
672 this._name = this.name
673 this.alias = this.name;
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);