4 @class Represents a symbol in the source code.
11 public class Symbol : Object
19 // probably needs vairous CTORS..
22 private string _name = "";
24 get { return this._name; }
25 set { this._name = value }
29 string defaultValue : "",
34 augments : [], // Doctag[]
35 exceptions : [], // Doctag[]
36 inherits : [], // Doctag[]
37 methods : [], // Symbol[]
39 _params : [], //Doctag[]
40 properties : [], //Doctag[]
41 requires : [], //Doctag[]
42 returns : [], //Doctag[]
52 //$args : [], // original arguments used when constructing.
54 public string alias = "";
57 string classDesc = "";
59 string deprecated = "";
65 string isa = "OBJECT"; // OBJECT//FUNCTION
67 public bool isEvent = false;
68 public bool isConstant = false;
69 public bool isIgnored = false;
70 public bool isInner = false;
71 public bool isNamespace = false;
72 public bool isPrivate = false;
73 public bool isStatic = false;
89 public void initArrays()
91 // only initialize arrays / objects..
101 this.isa = "OBJECT"; // OBJECT//FUNCTION
104 this.properties = [];
113 this.inheritsFrom = [];
114 this.childClasses = [];
116 this.comment = new DocComment();
117 this.comment.isUserComment = false;
122 Public Symbol.new_builtin(string name)
126 this.srcFile = DocParser.currentSourceFile;
127 this.setName( name );
128 this.alias = this.getName();
129 this.isa = "CONSTRUCTOR";
130 this.comment = new DocComment("");
131 this.comment.isUserComment = false;
132 this.isNamespace = false;
134 this.isPrivate = false;
142 //__defineSetter__("name",
143 setName : function(n) {
144 n = n.replace(/^_global_[.#-]/, "");
145 n = n.replace(/\.prototype\.?/g, '#');
146 n = n.replace(/#$/g, '');
148 this.name = n; // real!
151 //__defineGetter__("name",
152 getName : function() { return this._name; },
154 //__defineSetter__("params",
155 setParams :function(v) {
156 for (var i = 0, l = v.length; i < l; i++) {
157 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
158 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
159 this._params[i] = new DocTag(
160 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
163 this._params[i] = v[i];
166 this.params = this._params;
171 //__defineGetter__("params",
172 public getParams : function() {
177 public Symbol.new_populate_with_args(
179 Gee.ArrayList<string> params, // fixme???
183 this.$args = arguments;
184 //println("Symbol created: " + isa + ":" + name);
186 this.alias = this.getName();
187 this.setParams(params);
188 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
189 this.comment = comment || new DocComment("")
191 this.srcFile = DocParser.currentSourceFile;
195 if (this.is("FILE") && !this.alias) { // this will never hapen???
196 this.alias = this.srcFile;
203 setTags : function() {
205 var authors = this.comment.getTag("author");
206 if (authors.length) {
207 this.author = authors.map(function($){return $.desc;}).join(", ");
211 assert("testing Symbol");
213 requires("../lib/JSDOC/DocComment.js");
214 requires("../frame/String.js");
215 requires("../lib/JSDOC/DocTag.js");
217 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
218 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
221 var mth = this.comment.getTag("method");
223 this.isa = "FUNCTION";
226 var descs = this.comment.getTag("desc");
228 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
232 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
233 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
237 if (this.is("FILE")) {
238 if (!this.alias) this.alias = this.srcFile;
240 var overviews = this.comment.getTag("overview");
241 if (overviews.length) {
242 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
247 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
248 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
252 var sinces = this.comment.getTag("since");
254 this.since = sinces.map(function($){return $.desc;}).join(", ");
258 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
259 assertEqual(sym.since, "1.01", "@since tag, description is found.");
263 if (this.comment.getTag("constant").length) {
264 this.isConstant = true;
269 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
270 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
274 var versions = this.comment.getTag("version");
275 if (versions.length) {
276 this.version = versions.map(function($){return $.desc;}).join(", ");
280 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
281 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
285 var deprecateds = this.comment.getTag("deprecated");
286 if (deprecateds.length) {
287 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
292 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
296 var examples = this.comment.getTag("example");
297 if (examples.length) {
298 this.example = examples[0];
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
303 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
307 var sees = this.comment.getTag("see");
309 var thisSee = this.see;
310 sees.map(function($){thisSee.push($.desc);});
314 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
315 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
319 var classes = this.comment.getTag("class");
320 if (classes.length) {
321 //print(JSON.stringify(this,null,4));
322 this.isa = "CONSTRUCTOR";
323 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
324 if (!this.classDesc) {
325 this.classDesc = this.desc;
332 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
333 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
334 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
338 var namespaces = this.comment.getTag("namespace");
339 if (namespaces.length) {
340 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
341 this.isNamespace = true;
345 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
346 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
350 var params = this.comment.getTag("param");
352 // user-defined params overwrite those with same name defined by the parser
353 var thisParams = this.getParams();
355 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
356 this.setParams(params);
358 else { // need to overlay these user-defined params on to existing parser-defined params
359 for (var i = 0, l = params.length; i < l; i++) {
361 if (params[i].type) thisParams[i].type = params[i].type;
362 thisParams[i].name = params[i].name;
363 thisParams[i].desc = params[i].desc;
364 thisParams[i].isOptional = params[i].isOptional;
365 thisParams[i].defaultValue = params[i].defaultValue;
367 else thisParams[i] = params[i];
373 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
374 assertEqual(sym.params.length, 1, "parser defined param is found.");
376 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
377 assertEqual(sym.params.length, 1, "user defined param is found.");
378 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
379 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
381 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
382 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
383 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
384 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
386 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
387 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
388 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
389 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
391 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
392 assertEqual(sym.params.length, 1, "user defined param with description is found.");
393 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
397 if (this.comment.getTag("constructor").length) {
398 this.isa = "CONSTRUCTOR";
402 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
403 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
407 if (this.comment.getTag("static").length) {
408 this.isStatic = true;
409 if (this.isa == "CONSTRUCTOR") {
410 this.isNamespace = true;
415 if (this.comment.getTag("singleton").length) {
416 this.isStatic = true;
417 //print('------------- got singleton ---------------' + this.isa);
418 //if (this.isa == "CONSTRUCTOR") {
419 // this.isNamespace = true;
426 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
427 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
428 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
432 if (this.comment.getTag("inner").length) {
434 this.isStatic = false;
438 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
439 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
440 assertEqual(sym.isInner, true, "@inner makes isInner true.");
444 if (this.comment.getTag("field").length) {
449 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
450 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
454 if (this.comment.getTag("function").length) {
455 this.isa = "FUNCTION";
459 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
460 // change a property to a function..
461 this.isa = "FUNCTION";
466 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
467 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
471 var events = this.comment.getTag("event");
473 this.isa = "FUNCTION";
478 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
479 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
480 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
484 var names = this.comment.getTag("name");
486 this.setName(names[0].desc);
494 var properties = this.comment.getTag("property");
495 if (properties.length) {
496 thisProperties = this.properties;
497 for (var i = 0; i < properties.length; i++) {
498 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
499 // TODO: shouldn't the following happen in the addProperty method of Symbol?
500 property.name = properties[i].name;
501 property.memberOf = this.alias;
502 if (properties[i].type) property.type = properties[i].type;
503 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
504 this.addProperty(property);
505 imports.Parser.Parser.addSymbol(property);
510 var conf = this.comment.getTag("cfg");
512 for (var i = 0; i < conf.length; i++) {
513 this.addConfig(conf[i]);
522 var returns = this.comment.getTag("return");
523 if (returns.length) { // there can be many return tags in a single doclet
524 this.returns = returns;
525 this.type = returns.map(function($){return $.type}).join(", ");
533 this.exceptions = this.comment.getTag("throws");
540 var requires = this.comment.getTag("requires");
541 if (requires.length) {
542 this.requires = requires.map(function($){return $.desc});
550 var types = this.comment.getTag("type");
552 this.type = types[0].desc; //multiple type tags are ignored
560 if (this.comment.getTag("private").length || this.isInner) {
561 this.isPrivate = true;
565 if (this.comment.getTag("ignore").length) {
566 this.isIgnored = true;
573 // @inherits ... as ...
574 var inherits = this.comment.getTag("inherits");
575 if (inherits.length) {
576 for (var i = 0; i < inherits.length; i++) {
577 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
578 var inAlias = RegExp.$1;
579 var inAs = RegExp.$2 || inAlias;
581 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
584 inAs = inAs.replace(/\.prototype\.?/g, "#");
585 inAs = inAs.replace(/^this\.?/, "#");
588 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
590 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
593 inAs = this.alias + joiner + inAs;
596 this.inherits.push({alias: inAlias, as: inAs});
605 this.augments = this.comment.getTag("augments");
608 if (this.comment.getTag("extends")) {
609 this.augments = this.comment.getTag("extends");
614 var defaults = this.comment.getTag("default");
615 if (defaults.length) {
616 if (this.is("OBJECT")) {
617 this.defaultValue = defaults[0].desc;
626 var memberOfs = this.comment.getTag("memberOf");
627 if (memberOfs.length) {
628 this.memberOf = memberOfs[0].desc;
629 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
630 this.name = this.name.split('.').pop();
631 this.name = this.name.split('#').pop();
632 this.name = this.memberOf + this.name;
633 this._name = this.name
634 this.alias = this.name;
642 if (this.comment.getTag("public").length) {
643 this.isPrivate = false;
651 is : function(what) {
652 return this.isa === what;
655 isBuiltin : function() {
656 return SymbolSet.isBuiltin(this.alias);
659 setType : function(/**String*/comment, /**Boolean*/overwrite) {
660 if (!overwrite && this.type) return;
661 var typeComment = DocComment.unwrapComment(comment);
662 this.type = typeComment;
665 inherit : function(symbol) {
666 if (!this.hasMember(symbol.name) && !symbol.isInner) {
667 if (symbol.is("FUNCTION"))
668 this.methods.push(symbol);
669 else if (symbol.is("OBJECT"))
670 this.properties.push(symbol);
674 hasMember : function(name) {
675 return (this.hasMethod(name) || this.hasProperty(name));
678 addMember : function(symbol) {
679 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
681 if (symbol.comment.getTag("cfg").length == 1) {
682 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
683 this.addConfig(symbol.comment.getTag("cfg")[0]);
687 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
688 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
691 hasMethod : function(name) {
692 var thisMethods = this.methods;
693 for (var i = 0, l = thisMethods.length; i < l; i++) {
694 if (thisMethods[i].name == name) return true;
695 if (thisMethods[i].alias == name) return true;
700 addMethod : function(symbol) {
701 var methodAlias = symbol.alias;
702 var thisMethods = this.methods;
703 for (var i = 0, l = thisMethods.length; i < l; i++) {
704 if (thisMethods[i].alias == methodAlias) {
705 thisMethods[i] = symbol; // overwriting previous method
709 thisMethods.push(symbol); // new method with this alias
712 hasProperty : function(name) {
713 var thisProperties = this.properties;
714 for (var i = 0, l = thisProperties.length; i < l; i++) {
715 if (thisProperties[i].name == name) return true;
716 if (thisProperties[i].alias == name) return true;
721 addProperty : function(symbol) {
722 var propertyAlias = symbol.alias;
723 var thisProperties = this.properties;
724 for (var i = 0, l = thisProperties.length; i < l; i++) {
725 if (thisProperties[i].alias == propertyAlias) {
726 thisProperties[i] = symbol; // overwriting previous property
731 thisProperties.push(symbol); // new property with this alias
734 addDocTag : function(docTag)
736 this.comment.tags.push(docTag);
737 if (docTag.title == 'cfg') {
738 this.addConfig(docTag);
743 addConfig : function(docTag)
745 if (typeof(docTag['memberOf']) == 'undefined') {
746 // remove prototype data...
747 //var a = this.alias.split('#')[0];
748 //docTag.memberOf = a;
749 docTag.memberOf = this.alias;
751 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
752 this.cfgs[docTag.name] = docTag;
756 configToArray: function()
759 for(var ci in this.cfgs) {
760 // dont show hidden!!
761 if (this.cfgs[ci].desc.match(/@hide/)) {
764 r.push(this.cfgs[ci]);
772 * Elements that are not serialized
776 '$args' // not needed AFAIK
779 Symbol.srcFile = ""; //running reference to the current file being parsed
782 Symbol.fromDump = function(t)
784 var ns = new Symbol();
786 if (typeof(ns[i]) == "undefined") {
787 println("ERR:no default for Symbol:"+ i);