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 Public Symbol.new_builtin(string name)
118 this.setName( name );
119 this.alias = this.getName();
120 this.isa = "CONSTRUCTOR";
121 this.comment = new DocComment("");
122 this.isNamespace = false;
124 this.isPrivate = false;
128 //__defineSetter__("name",
129 setName : function(n) {
130 n = n.replace(/^_global_[.#-]/, "");
131 n = n.replace(/\.prototype\.?/g, '#');
132 n = n.replace(/#$/g, '');
134 this.name = n; // real!
137 //__defineGetter__("name",
138 getName : function() { return this._name; },
140 //__defineSetter__("params",
141 setParams :function(v) {
142 for (var i = 0, l = v.length; i < l; i++) {
143 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
144 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
145 this._params[i] = new DocTag(
146 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
149 this._params[i] = v[i];
152 this.params = this._params;
157 //__defineGetter__("params",
158 getParams : function() { return this._params; },
163 /** Object[] */ params,
165 /** DocComment */ comment
167 this.$args = arguments;
168 //println("Symbol created: " + isa + ":" + name);
170 this.alias = this.getName();
171 this.setParams(params);
172 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
173 this.comment = comment || new DocComment("");
174 this.srcFile = Symbol.srcFile;
178 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
182 //if (typeof PluginManager != "undefined") {
183 // PluginManager.run("onSymbol", this);
187 setTags : function() {
189 var authors = this.comment.getTag("author");
190 if (authors.length) {
191 this.author = authors.map(function($){return $.desc;}).join(", ");
195 assert("testing Symbol");
197 requires("../lib/JSDOC/DocComment.js");
198 requires("../frame/String.js");
199 requires("../lib/JSDOC/DocTag.js");
201 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
202 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
205 var mth = this.comment.getTag("method");
207 this.isa = "FUNCTION";
210 var descs = this.comment.getTag("desc");
212 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
216 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
217 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
221 if (this.is("FILE")) {
222 if (!this.alias) this.alias = this.srcFile;
224 var overviews = this.comment.getTag("overview");
225 if (overviews.length) {
226 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
231 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
232 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
236 var sinces = this.comment.getTag("since");
238 this.since = sinces.map(function($){return $.desc;}).join(", ");
242 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
243 assertEqual(sym.since, "1.01", "@since tag, description is found.");
247 if (this.comment.getTag("constant").length) {
248 this.isConstant = true;
253 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
254 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
258 var versions = this.comment.getTag("version");
259 if (versions.length) {
260 this.version = versions.map(function($){return $.desc;}).join(", ");
264 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
265 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
269 var deprecateds = this.comment.getTag("deprecated");
270 if (deprecateds.length) {
271 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
275 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
276 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
280 var examples = this.comment.getTag("example");
281 if (examples.length) {
282 this.example = examples[0];
286 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
287 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
291 var sees = this.comment.getTag("see");
293 var thisSee = this.see;
294 sees.map(function($){thisSee.push($.desc);});
298 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
299 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
303 var classes = this.comment.getTag("class");
304 if (classes.length) {
305 //print(JSON.stringify(this,null,4));
306 this.isa = "CONSTRUCTOR";
307 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
308 if (!this.classDesc) {
309 this.classDesc = this.desc;
316 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
317 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
318 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
322 var namespaces = this.comment.getTag("namespace");
323 if (namespaces.length) {
324 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
325 this.isNamespace = true;
329 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
330 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
334 var params = this.comment.getTag("param");
336 // user-defined params overwrite those with same name defined by the parser
337 var thisParams = this.getParams();
339 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
340 this.setParams(params);
342 else { // need to overlay these user-defined params on to existing parser-defined params
343 for (var i = 0, l = params.length; i < l; i++) {
345 if (params[i].type) thisParams[i].type = params[i].type;
346 thisParams[i].name = params[i].name;
347 thisParams[i].desc = params[i].desc;
348 thisParams[i].isOptional = params[i].isOptional;
349 thisParams[i].defaultValue = params[i].defaultValue;
351 else thisParams[i] = params[i];
357 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
358 assertEqual(sym.params.length, 1, "parser defined param is found.");
360 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
361 assertEqual(sym.params.length, 1, "user defined param is found.");
362 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
363 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
365 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
366 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
367 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
368 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
370 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
371 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
372 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
373 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
375 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
376 assertEqual(sym.params.length, 1, "user defined param with description is found.");
377 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
381 if (this.comment.getTag("constructor").length) {
382 this.isa = "CONSTRUCTOR";
386 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
387 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
391 if (this.comment.getTag("static").length) {
392 this.isStatic = true;
393 if (this.isa == "CONSTRUCTOR") {
394 this.isNamespace = true;
399 if (this.comment.getTag("singleton").length) {
400 this.isStatic = true;
401 //print('------------- got singleton ---------------' + this.isa);
402 //if (this.isa == "CONSTRUCTOR") {
403 // this.isNamespace = true;
410 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
411 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
412 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
416 if (this.comment.getTag("inner").length) {
418 this.isStatic = false;
422 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
423 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
424 assertEqual(sym.isInner, true, "@inner makes isInner true.");
428 if (this.comment.getTag("field").length) {
433 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
434 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
438 if (this.comment.getTag("function").length) {
439 this.isa = "FUNCTION";
443 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
444 // change a property to a function..
445 this.isa = "FUNCTION";
450 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
451 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
455 var events = this.comment.getTag("event");
457 this.isa = "FUNCTION";
462 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
463 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
464 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
468 var names = this.comment.getTag("name");
470 this.setName(names[0].desc);
478 var properties = this.comment.getTag("property");
479 if (properties.length) {
480 thisProperties = this.properties;
481 for (var i = 0; i < properties.length; i++) {
482 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
483 // TODO: shouldn't the following happen in the addProperty method of Symbol?
484 property.name = properties[i].name;
485 property.memberOf = this.alias;
486 if (properties[i].type) property.type = properties[i].type;
487 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
488 this.addProperty(property);
489 imports.Parser.Parser.addSymbol(property);
494 var conf = this.comment.getTag("cfg");
496 for (var i = 0; i < conf.length; i++) {
497 this.addConfig(conf[i]);
506 var returns = this.comment.getTag("return");
507 if (returns.length) { // there can be many return tags in a single doclet
508 this.returns = returns;
509 this.type = returns.map(function($){return $.type}).join(", ");
517 this.exceptions = this.comment.getTag("throws");
524 var requires = this.comment.getTag("requires");
525 if (requires.length) {
526 this.requires = requires.map(function($){return $.desc});
534 var types = this.comment.getTag("type");
536 this.type = types[0].desc; //multiple type tags are ignored
544 if (this.comment.getTag("private").length || this.isInner) {
545 this.isPrivate = true;
549 if (this.comment.getTag("ignore").length) {
550 this.isIgnored = true;
557 // @inherits ... as ...
558 var inherits = this.comment.getTag("inherits");
559 if (inherits.length) {
560 for (var i = 0; i < inherits.length; i++) {
561 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
562 var inAlias = RegExp.$1;
563 var inAs = RegExp.$2 || inAlias;
565 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
568 inAs = inAs.replace(/\.prototype\.?/g, "#");
569 inAs = inAs.replace(/^this\.?/, "#");
572 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
574 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
577 inAs = this.alias + joiner + inAs;
580 this.inherits.push({alias: inAlias, as: inAs});
589 this.augments = this.comment.getTag("augments");
592 if (this.comment.getTag("extends")) {
593 this.augments = this.comment.getTag("extends");
598 var defaults = this.comment.getTag("default");
599 if (defaults.length) {
600 if (this.is("OBJECT")) {
601 this.defaultValue = defaults[0].desc;
610 var memberOfs = this.comment.getTag("memberOf");
611 if (memberOfs.length) {
612 this.memberOf = memberOfs[0].desc;
613 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
614 this.name = this.name.split('.').pop();
615 this.name = this.name.split('#').pop();
616 this.name = this.memberOf + this.name;
617 this._name = this.name
618 this.alias = this.name;
626 if (this.comment.getTag("public").length) {
627 this.isPrivate = false;
635 is : function(what) {
636 return this.isa === what;
639 isBuiltin : function() {
640 return SymbolSet.isBuiltin(this.alias);
643 setType : function(/**String*/comment, /**Boolean*/overwrite) {
644 if (!overwrite && this.type) return;
645 var typeComment = DocComment.unwrapComment(comment);
646 this.type = typeComment;
649 inherit : function(symbol) {
650 if (!this.hasMember(symbol.name) && !symbol.isInner) {
651 if (symbol.is("FUNCTION"))
652 this.methods.push(symbol);
653 else if (symbol.is("OBJECT"))
654 this.properties.push(symbol);
658 hasMember : function(name) {
659 return (this.hasMethod(name) || this.hasProperty(name));
662 addMember : function(symbol) {
663 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
665 if (symbol.comment.getTag("cfg").length == 1) {
666 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
667 this.addConfig(symbol.comment.getTag("cfg")[0]);
671 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
672 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
675 hasMethod : function(name) {
676 var thisMethods = this.methods;
677 for (var i = 0, l = thisMethods.length; i < l; i++) {
678 if (thisMethods[i].name == name) return true;
679 if (thisMethods[i].alias == name) return true;
684 addMethod : function(symbol) {
685 var methodAlias = symbol.alias;
686 var thisMethods = this.methods;
687 for (var i = 0, l = thisMethods.length; i < l; i++) {
688 if (thisMethods[i].alias == methodAlias) {
689 thisMethods[i] = symbol; // overwriting previous method
693 thisMethods.push(symbol); // new method with this alias
696 hasProperty : function(name) {
697 var thisProperties = this.properties;
698 for (var i = 0, l = thisProperties.length; i < l; i++) {
699 if (thisProperties[i].name == name) return true;
700 if (thisProperties[i].alias == name) return true;
705 addProperty : function(symbol) {
706 var propertyAlias = symbol.alias;
707 var thisProperties = this.properties;
708 for (var i = 0, l = thisProperties.length; i < l; i++) {
709 if (thisProperties[i].alias == propertyAlias) {
710 thisProperties[i] = symbol; // overwriting previous property
715 thisProperties.push(symbol); // new property with this alias
718 addDocTag : function(docTag)
720 this.comment.tags.push(docTag);
721 if (docTag.title == 'cfg') {
722 this.addConfig(docTag);
727 addConfig : function(docTag)
729 if (typeof(docTag['memberOf']) == 'undefined') {
730 // remove prototype data...
731 //var a = this.alias.split('#')[0];
732 //docTag.memberOf = a;
733 docTag.memberOf = this.alias;
735 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
736 this.cfgs[docTag.name] = docTag;
740 configToArray: function()
743 for(var ci in this.cfgs) {
744 // dont show hidden!!
745 if (this.cfgs[ci].desc.match(/@hide/)) {
748 r.push(this.cfgs[ci]);
756 * Elements that are not serialized
760 '$args' // not needed AFAIK
763 Symbol.srcFile = ""; //running reference to the current file being parsed
766 Symbol.fromDump = function(t)
768 var ns = new Symbol();
770 if (typeof(ns[i]) == "undefined") {
771 println("ERR:no default for Symbol:"+ i);