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;
83 public void initArrays()
85 // only initialize arrays / objects..
95 this.isa = "OBJECT"; // OBJECT//FUNCTION
107 this.inheritsFrom = [];
108 this.childClasses = [];
110 this.comment = new DocComment();
111 this.comment.isUserComment = false;
116 Public Symbol.new_builtin(string name)
118 this.srcFile = DocParser.currentSourceFile;
119 this.setName( name );
120 this.alias = this.getName();
121 this.isa = "CONSTRUCTOR";
122 this.comment = new DocComment("");
123 this.isNamespace = false;
125 this.isPrivate = false;
133 //__defineSetter__("name",
134 setName : function(n) {
135 n = n.replace(/^_global_[.#-]/, "");
136 n = n.replace(/\.prototype\.?/g, '#');
137 n = n.replace(/#$/g, '');
139 this.name = n; // real!
142 //__defineGetter__("name",
143 getName : function() { return this._name; },
145 //__defineSetter__("params",
146 setParams :function(v) {
147 for (var i = 0, l = v.length; i < l; i++) {
148 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
149 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
150 this._params[i] = new DocTag(
151 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
154 this._params[i] = v[i];
157 this.params = this._params;
162 //__defineGetter__("params",
163 getParams : function() { return this._params; },
168 /** Object[] */ params,
170 /** DocComment */ comment
172 this.$args = arguments;
173 //println("Symbol created: " + isa + ":" + name);
175 this.alias = this.getName();
176 this.setParams(params);
177 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
178 this.comment = comment || new DocComment("");
179 this.srcFile = Symbol.srcFile;
183 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
187 //if (typeof PluginManager != "undefined") {
188 // PluginManager.run("onSymbol", this);
192 setTags : function() {
194 var authors = this.comment.getTag("author");
195 if (authors.length) {
196 this.author = authors.map(function($){return $.desc;}).join(", ");
200 assert("testing Symbol");
202 requires("../lib/JSDOC/DocComment.js");
203 requires("../frame/String.js");
204 requires("../lib/JSDOC/DocTag.js");
206 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
207 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
210 var mth = this.comment.getTag("method");
212 this.isa = "FUNCTION";
215 var descs = this.comment.getTag("desc");
217 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
221 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
222 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
226 if (this.is("FILE")) {
227 if (!this.alias) this.alias = this.srcFile;
229 var overviews = this.comment.getTag("overview");
230 if (overviews.length) {
231 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
236 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
237 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
241 var sinces = this.comment.getTag("since");
243 this.since = sinces.map(function($){return $.desc;}).join(", ");
247 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
248 assertEqual(sym.since, "1.01", "@since tag, description is found.");
252 if (this.comment.getTag("constant").length) {
253 this.isConstant = true;
258 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
259 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
263 var versions = this.comment.getTag("version");
264 if (versions.length) {
265 this.version = versions.map(function($){return $.desc;}).join(", ");
269 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
270 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
274 var deprecateds = this.comment.getTag("deprecated");
275 if (deprecateds.length) {
276 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
280 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
281 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
285 var examples = this.comment.getTag("example");
286 if (examples.length) {
287 this.example = examples[0];
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
292 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
296 var sees = this.comment.getTag("see");
298 var thisSee = this.see;
299 sees.map(function($){thisSee.push($.desc);});
303 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
304 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
308 var classes = this.comment.getTag("class");
309 if (classes.length) {
310 //print(JSON.stringify(this,null,4));
311 this.isa = "CONSTRUCTOR";
312 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
313 if (!this.classDesc) {
314 this.classDesc = this.desc;
321 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
322 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
323 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
327 var namespaces = this.comment.getTag("namespace");
328 if (namespaces.length) {
329 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
330 this.isNamespace = true;
334 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
335 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
339 var params = this.comment.getTag("param");
341 // user-defined params overwrite those with same name defined by the parser
342 var thisParams = this.getParams();
344 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
345 this.setParams(params);
347 else { // need to overlay these user-defined params on to existing parser-defined params
348 for (var i = 0, l = params.length; i < l; i++) {
350 if (params[i].type) thisParams[i].type = params[i].type;
351 thisParams[i].name = params[i].name;
352 thisParams[i].desc = params[i].desc;
353 thisParams[i].isOptional = params[i].isOptional;
354 thisParams[i].defaultValue = params[i].defaultValue;
356 else thisParams[i] = params[i];
362 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
363 assertEqual(sym.params.length, 1, "parser defined param is found.");
365 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
366 assertEqual(sym.params.length, 1, "user defined param is found.");
367 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
368 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
370 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
371 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
372 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
373 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
375 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
376 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
377 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
378 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
380 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
381 assertEqual(sym.params.length, 1, "user defined param with description is found.");
382 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
386 if (this.comment.getTag("constructor").length) {
387 this.isa = "CONSTRUCTOR";
391 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
392 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
396 if (this.comment.getTag("static").length) {
397 this.isStatic = true;
398 if (this.isa == "CONSTRUCTOR") {
399 this.isNamespace = true;
404 if (this.comment.getTag("singleton").length) {
405 this.isStatic = true;
406 //print('------------- got singleton ---------------' + this.isa);
407 //if (this.isa == "CONSTRUCTOR") {
408 // this.isNamespace = true;
415 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
416 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
417 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
421 if (this.comment.getTag("inner").length) {
423 this.isStatic = false;
427 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
428 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
429 assertEqual(sym.isInner, true, "@inner makes isInner true.");
433 if (this.comment.getTag("field").length) {
438 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
439 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
443 if (this.comment.getTag("function").length) {
444 this.isa = "FUNCTION";
448 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
449 // change a property to a function..
450 this.isa = "FUNCTION";
455 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
456 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
460 var events = this.comment.getTag("event");
462 this.isa = "FUNCTION";
467 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
468 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
469 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
473 var names = this.comment.getTag("name");
475 this.setName(names[0].desc);
483 var properties = this.comment.getTag("property");
484 if (properties.length) {
485 thisProperties = this.properties;
486 for (var i = 0; i < properties.length; i++) {
487 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
488 // TODO: shouldn't the following happen in the addProperty method of Symbol?
489 property.name = properties[i].name;
490 property.memberOf = this.alias;
491 if (properties[i].type) property.type = properties[i].type;
492 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
493 this.addProperty(property);
494 imports.Parser.Parser.addSymbol(property);
499 var conf = this.comment.getTag("cfg");
501 for (var i = 0; i < conf.length; i++) {
502 this.addConfig(conf[i]);
511 var returns = this.comment.getTag("return");
512 if (returns.length) { // there can be many return tags in a single doclet
513 this.returns = returns;
514 this.type = returns.map(function($){return $.type}).join(", ");
522 this.exceptions = this.comment.getTag("throws");
529 var requires = this.comment.getTag("requires");
530 if (requires.length) {
531 this.requires = requires.map(function($){return $.desc});
539 var types = this.comment.getTag("type");
541 this.type = types[0].desc; //multiple type tags are ignored
549 if (this.comment.getTag("private").length || this.isInner) {
550 this.isPrivate = true;
554 if (this.comment.getTag("ignore").length) {
555 this.isIgnored = true;
562 // @inherits ... as ...
563 var inherits = this.comment.getTag("inherits");
564 if (inherits.length) {
565 for (var i = 0; i < inherits.length; i++) {
566 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
567 var inAlias = RegExp.$1;
568 var inAs = RegExp.$2 || inAlias;
570 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
573 inAs = inAs.replace(/\.prototype\.?/g, "#");
574 inAs = inAs.replace(/^this\.?/, "#");
577 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
579 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
582 inAs = this.alias + joiner + inAs;
585 this.inherits.push({alias: inAlias, as: inAs});
594 this.augments = this.comment.getTag("augments");
597 if (this.comment.getTag("extends")) {
598 this.augments = this.comment.getTag("extends");
603 var defaults = this.comment.getTag("default");
604 if (defaults.length) {
605 if (this.is("OBJECT")) {
606 this.defaultValue = defaults[0].desc;
615 var memberOfs = this.comment.getTag("memberOf");
616 if (memberOfs.length) {
617 this.memberOf = memberOfs[0].desc;
618 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
619 this.name = this.name.split('.').pop();
620 this.name = this.name.split('#').pop();
621 this.name = this.memberOf + this.name;
622 this._name = this.name
623 this.alias = this.name;
631 if (this.comment.getTag("public").length) {
632 this.isPrivate = false;
640 is : function(what) {
641 return this.isa === what;
644 isBuiltin : function() {
645 return SymbolSet.isBuiltin(this.alias);
648 setType : function(/**String*/comment, /**Boolean*/overwrite) {
649 if (!overwrite && this.type) return;
650 var typeComment = DocComment.unwrapComment(comment);
651 this.type = typeComment;
654 inherit : function(symbol) {
655 if (!this.hasMember(symbol.name) && !symbol.isInner) {
656 if (symbol.is("FUNCTION"))
657 this.methods.push(symbol);
658 else if (symbol.is("OBJECT"))
659 this.properties.push(symbol);
663 hasMember : function(name) {
664 return (this.hasMethod(name) || this.hasProperty(name));
667 addMember : function(symbol) {
668 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
670 if (symbol.comment.getTag("cfg").length == 1) {
671 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
672 this.addConfig(symbol.comment.getTag("cfg")[0]);
676 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
677 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
680 hasMethod : function(name) {
681 var thisMethods = this.methods;
682 for (var i = 0, l = thisMethods.length; i < l; i++) {
683 if (thisMethods[i].name == name) return true;
684 if (thisMethods[i].alias == name) return true;
689 addMethod : function(symbol) {
690 var methodAlias = symbol.alias;
691 var thisMethods = this.methods;
692 for (var i = 0, l = thisMethods.length; i < l; i++) {
693 if (thisMethods[i].alias == methodAlias) {
694 thisMethods[i] = symbol; // overwriting previous method
698 thisMethods.push(symbol); // new method with this alias
701 hasProperty : function(name) {
702 var thisProperties = this.properties;
703 for (var i = 0, l = thisProperties.length; i < l; i++) {
704 if (thisProperties[i].name == name) return true;
705 if (thisProperties[i].alias == name) return true;
710 addProperty : function(symbol) {
711 var propertyAlias = symbol.alias;
712 var thisProperties = this.properties;
713 for (var i = 0, l = thisProperties.length; i < l; i++) {
714 if (thisProperties[i].alias == propertyAlias) {
715 thisProperties[i] = symbol; // overwriting previous property
720 thisProperties.push(symbol); // new property with this alias
723 addDocTag : function(docTag)
725 this.comment.tags.push(docTag);
726 if (docTag.title == 'cfg') {
727 this.addConfig(docTag);
732 addConfig : function(docTag)
734 if (typeof(docTag['memberOf']) == 'undefined') {
735 // remove prototype data...
736 //var a = this.alias.split('#')[0];
737 //docTag.memberOf = a;
738 docTag.memberOf = this.alias;
740 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
741 this.cfgs[docTag.name] = docTag;
745 configToArray: function()
748 for(var ci in this.cfgs) {
749 // dont show hidden!!
750 if (this.cfgs[ci].desc.match(/@hide/)) {
753 r.push(this.cfgs[ci]);
761 * Elements that are not serialized
765 '$args' // not needed AFAIK
768 Symbol.srcFile = ""; //running reference to the current file being parsed
771 Symbol.fromDump = function(t)
773 var ns = new Symbol();
775 if (typeof(ns[i]) == "undefined") {
776 println("ERR:no default for Symbol:"+ i);