4 @class Represents a symbol in the source code.
11 public class Symbol : Object
19 // probably needs vairous CTORS..
23 string defaultValue : "",
28 augments : [], // Doctag[]
29 exceptions : [], // Doctag[]
30 inherits : [], // Doctag[]
31 methods : [], // Symbol[]
33 _params : [], //Doctag[]
34 properties : [], //Doctag[]
35 requires : [], //Doctag[]
36 returns : [], //Doctag[]
46 //$args : [], // original arguments used when constructing.
48 public string alias = "";
51 string classDesc = "";
53 string deprecated = "";
59 string isa = "OBJECT"; // OBJECT//FUNCTION
61 public bool isEvent = false;
62 public bool isConstant = false;
63 public bool isIgnored = false;
64 public bool isInner = false;
65 public bool isNamespace = false;
66 public bool isPrivate = false;
67 public bool isStatic = false;
85 // only initialize arrays / objects..
86 this.srcFile = DocParser.currentSourceFile;
95 this.isa = "OBJECT"; // OBJECT//FUNCTION
107 this.inheritsFrom = [];
108 this.childClasses = [];
110 this.comment = new DocComment();
111 this.comment.isUserComment = false;
116 serialize : function() {
118 for (var p in this) {
124 for (var i in keys) {
125 if (typeof this[keys[i]] == "function") continue;
126 out += " " +keys[i]+" => "+
128 (typeof(this[keys[i]]) != "object") ?
130 "[" +typeof(this[keys[i]])+"]"
134 return "\n{\n" + out + "}\n";
139 var clone = new Symbol();
140 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
141 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
148 //__defineSetter__("name",
149 setName : function(n) {
150 n = n.replace(/^_global_[.#-]/, "");
151 n = n.replace(/\.prototype\.?/g, '#');
152 n = n.replace(/#$/g, '');
154 this.name = n; // real!
157 //__defineGetter__("name",
158 getName : function() { return this._name; },
160 //__defineSetter__("params",
161 setParams :function(v) {
162 for (var i = 0, l = v.length; i < l; i++) {
163 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
164 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
165 this._params[i] = new DocTag(
166 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
169 this._params[i] = v[i];
172 this.params = this._params;
177 //__defineGetter__("params",
178 getParams : function() { return this._params; },
183 /** Object[] */ params,
185 /** DocComment */ comment
187 this.$args = arguments;
188 //println("Symbol created: " + isa + ":" + name);
190 this.alias = this.getName();
191 this.setParams(params);
192 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
193 this.comment = comment || new DocComment("");
194 this.srcFile = Symbol.srcFile;
198 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
202 //if (typeof PluginManager != "undefined") {
203 // PluginManager.run("onSymbol", this);
207 setTags : function() {
209 var authors = this.comment.getTag("author");
210 if (authors.length) {
211 this.author = authors.map(function($){return $.desc;}).join(", ");
215 assert("testing Symbol");
217 requires("../lib/JSDOC/DocComment.js");
218 requires("../frame/String.js");
219 requires("../lib/JSDOC/DocTag.js");
221 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
222 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
225 var mth = this.comment.getTag("method");
227 this.isa = "FUNCTION";
230 var descs = this.comment.getTag("desc");
232 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
236 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
237 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
241 if (this.is("FILE")) {
242 if (!this.alias) this.alias = this.srcFile;
244 var overviews = this.comment.getTag("overview");
245 if (overviews.length) {
246 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
251 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
252 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
256 var sinces = this.comment.getTag("since");
258 this.since = sinces.map(function($){return $.desc;}).join(", ");
262 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
263 assertEqual(sym.since, "1.01", "@since tag, description is found.");
267 if (this.comment.getTag("constant").length) {
268 this.isConstant = true;
273 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
274 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
278 var versions = this.comment.getTag("version");
279 if (versions.length) {
280 this.version = versions.map(function($){return $.desc;}).join(", ");
284 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
285 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
289 var deprecateds = this.comment.getTag("deprecated");
290 if (deprecateds.length) {
291 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
295 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
296 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
300 var examples = this.comment.getTag("example");
301 if (examples.length) {
302 this.example = examples[0];
306 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
307 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
311 var sees = this.comment.getTag("see");
313 var thisSee = this.see;
314 sees.map(function($){thisSee.push($.desc);});
318 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
319 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
323 var classes = this.comment.getTag("class");
324 if (classes.length) {
325 //print(JSON.stringify(this,null,4));
326 this.isa = "CONSTRUCTOR";
327 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
328 if (!this.classDesc) {
329 this.classDesc = this.desc;
336 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
337 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
338 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
342 var namespaces = this.comment.getTag("namespace");
343 if (namespaces.length) {
344 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
345 this.isNamespace = true;
349 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
350 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
354 var params = this.comment.getTag("param");
356 // user-defined params overwrite those with same name defined by the parser
357 var thisParams = this.getParams();
359 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
360 this.setParams(params);
362 else { // need to overlay these user-defined params on to existing parser-defined params
363 for (var i = 0, l = params.length; i < l; i++) {
365 if (params[i].type) thisParams[i].type = params[i].type;
366 thisParams[i].name = params[i].name;
367 thisParams[i].desc = params[i].desc;
368 thisParams[i].isOptional = params[i].isOptional;
369 thisParams[i].defaultValue = params[i].defaultValue;
371 else thisParams[i] = params[i];
377 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
378 assertEqual(sym.params.length, 1, "parser defined param is found.");
380 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
381 assertEqual(sym.params.length, 1, "user defined param is found.");
382 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
383 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
385 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
386 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
387 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
388 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
390 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
391 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
392 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
393 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
395 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
396 assertEqual(sym.params.length, 1, "user defined param with description is found.");
397 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
401 if (this.comment.getTag("constructor").length) {
402 this.isa = "CONSTRUCTOR";
406 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
407 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
411 if (this.comment.getTag("static").length) {
412 this.isStatic = true;
413 if (this.isa == "CONSTRUCTOR") {
414 this.isNamespace = true;
419 if (this.comment.getTag("singleton").length) {
420 this.isStatic = true;
421 //print('------------- got singleton ---------------' + this.isa);
422 //if (this.isa == "CONSTRUCTOR") {
423 // this.isNamespace = true;
430 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
431 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
432 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
436 if (this.comment.getTag("inner").length) {
438 this.isStatic = false;
442 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
443 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
444 assertEqual(sym.isInner, true, "@inner makes isInner true.");
448 if (this.comment.getTag("field").length) {
453 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
454 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
458 if (this.comment.getTag("function").length) {
459 this.isa = "FUNCTION";
463 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
464 // change a property to a function..
465 this.isa = "FUNCTION";
470 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
471 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
475 var events = this.comment.getTag("event");
477 this.isa = "FUNCTION";
482 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
483 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
484 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
488 var names = this.comment.getTag("name");
490 this.setName(names[0].desc);
498 var properties = this.comment.getTag("property");
499 if (properties.length) {
500 thisProperties = this.properties;
501 for (var i = 0; i < properties.length; i++) {
502 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
503 // TODO: shouldn't the following happen in the addProperty method of Symbol?
504 property.name = properties[i].name;
505 property.memberOf = this.alias;
506 if (properties[i].type) property.type = properties[i].type;
507 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
508 this.addProperty(property);
509 imports.Parser.Parser.addSymbol(property);
514 var conf = this.comment.getTag("cfg");
516 for (var i = 0; i < conf.length; i++) {
517 this.addConfig(conf[i]);
526 var returns = this.comment.getTag("return");
527 if (returns.length) { // there can be many return tags in a single doclet
528 this.returns = returns;
529 this.type = returns.map(function($){return $.type}).join(", ");
537 this.exceptions = this.comment.getTag("throws");
544 var requires = this.comment.getTag("requires");
545 if (requires.length) {
546 this.requires = requires.map(function($){return $.desc});
554 var types = this.comment.getTag("type");
556 this.type = types[0].desc; //multiple type tags are ignored
564 if (this.comment.getTag("private").length || this.isInner) {
565 this.isPrivate = true;
569 if (this.comment.getTag("ignore").length) {
570 this.isIgnored = true;
577 // @inherits ... as ...
578 var inherits = this.comment.getTag("inherits");
579 if (inherits.length) {
580 for (var i = 0; i < inherits.length; i++) {
581 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
582 var inAlias = RegExp.$1;
583 var inAs = RegExp.$2 || inAlias;
585 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
588 inAs = inAs.replace(/\.prototype\.?/g, "#");
589 inAs = inAs.replace(/^this\.?/, "#");
592 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
594 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
597 inAs = this.alias + joiner + inAs;
600 this.inherits.push({alias: inAlias, as: inAs});
609 this.augments = this.comment.getTag("augments");
612 if (this.comment.getTag("extends")) {
613 this.augments = this.comment.getTag("extends");
618 var defaults = this.comment.getTag("default");
619 if (defaults.length) {
620 if (this.is("OBJECT")) {
621 this.defaultValue = defaults[0].desc;
630 var memberOfs = this.comment.getTag("memberOf");
631 if (memberOfs.length) {
632 this.memberOf = memberOfs[0].desc;
633 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
634 this.name = this.name.split('.').pop();
635 this.name = this.name.split('#').pop();
636 this.name = this.memberOf + this.name;
637 this._name = this.name
638 this.alias = this.name;
646 if (this.comment.getTag("public").length) {
647 this.isPrivate = false;
655 is : function(what) {
656 return this.isa === what;
659 isBuiltin : function() {
660 return SymbolSet.isBuiltin(this.alias);
663 setType : function(/**String*/comment, /**Boolean*/overwrite) {
664 if (!overwrite && this.type) return;
665 var typeComment = DocComment.unwrapComment(comment);
666 this.type = typeComment;
669 inherit : function(symbol) {
670 if (!this.hasMember(symbol.name) && !symbol.isInner) {
671 if (symbol.is("FUNCTION"))
672 this.methods.push(symbol);
673 else if (symbol.is("OBJECT"))
674 this.properties.push(symbol);
678 hasMember : function(name) {
679 return (this.hasMethod(name) || this.hasProperty(name));
682 addMember : function(symbol) {
683 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
685 if (symbol.comment.getTag("cfg").length == 1) {
686 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
687 this.addConfig(symbol.comment.getTag("cfg")[0]);
691 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
692 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
695 hasMethod : function(name) {
696 var thisMethods = this.methods;
697 for (var i = 0, l = thisMethods.length; i < l; i++) {
698 if (thisMethods[i].name == name) return true;
699 if (thisMethods[i].alias == name) return true;
704 addMethod : function(symbol) {
705 var methodAlias = symbol.alias;
706 var thisMethods = this.methods;
707 for (var i = 0, l = thisMethods.length; i < l; i++) {
708 if (thisMethods[i].alias == methodAlias) {
709 thisMethods[i] = symbol; // overwriting previous method
713 thisMethods.push(symbol); // new method with this alias
716 hasProperty : function(name) {
717 var thisProperties = this.properties;
718 for (var i = 0, l = thisProperties.length; i < l; i++) {
719 if (thisProperties[i].name == name) return true;
720 if (thisProperties[i].alias == name) return true;
725 addProperty : function(symbol) {
726 var propertyAlias = symbol.alias;
727 var thisProperties = this.properties;
728 for (var i = 0, l = thisProperties.length; i < l; i++) {
729 if (thisProperties[i].alias == propertyAlias) {
730 thisProperties[i] = symbol; // overwriting previous property
735 thisProperties.push(symbol); // new property with this alias
738 addDocTag : function(docTag)
740 this.comment.tags.push(docTag);
741 if (docTag.title == 'cfg') {
742 this.addConfig(docTag);
747 addConfig : function(docTag)
749 if (typeof(docTag['memberOf']) == 'undefined') {
750 // remove prototype data...
751 //var a = this.alias.split('#')[0];
752 //docTag.memberOf = a;
753 docTag.memberOf = this.alias;
755 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
756 this.cfgs[docTag.name] = docTag;
760 configToArray: function()
763 for(var ci in this.cfgs) {
764 // dont show hidden!!
765 if (this.cfgs[ci].desc.match(/@hide/)) {
768 r.push(this.cfgs[ci]);
776 * Elements that are not serialized
780 '$args' // not needed AFAIK
783 Symbol.srcFile = ""; //running reference to the current file being parsed
786 Symbol.fromDump = function(t)
788 var ns = new Symbol();
790 if (typeof(ns[i]) == "undefined") {
791 println("ERR:no default for Symbol:"+ i);