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);
28 switch (typeof(this[i])) {
32 print("object? :" + i);
35 ret[i] = this[i]; continue;
37 print("unknown type:" + typeof(this[i]));
48 this.defaultValue = "";
56 this.comment = { isUserComment: false };
57 //this.defaultValue = null;
64 this.inheritsFrom = [];
65 this.isa = "OBJECT"; // OBJECT//FUNCTION
67 this.isConstant = false;
68 this.isIgnored = false;
70 this.isNamespace = false;
71 this.isPrivate = false;
72 this.isStatic = false;
85 this.childClasses = [];
90 serialize : function() {
99 if (typeof this[keys[i]] == "function") continue;
100 out += " " +keys[i]+" => "+
102 (typeof(this[keys[i]]) != "object") ?
104 "[" +typeof(this[keys[i]])+"]"
108 return "\n{\n" + out + "}\n";
112 var clone = new Symbol();
113 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
114 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
121 //__defineSetter__("name",
122 setName : function(n) {
123 n = n.replace(/^_global_[.#-]/, "");
124 n = n.replace(/\.prototype\.?/g, '#');
125 n = n.replace(/#$/g, '');
127 this.name = n; // real!
130 //__defineGetter__("name",
131 getName : function() { return this._name; },
133 //__defineSetter__("params",
134 setParams :function(v) {
135 for (var i = 0, l = v.length; i < l; i++) {
136 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
137 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
138 this._params[i] = new DocTag(
139 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
142 this._params[i] = v[i];
145 this.params = this._params;
150 //__defineGetter__("params",
151 getParams : function() { return this._params; },
156 /** Object[] */ params,
158 /** DocComment */ comment
160 this.$args = arguments;
161 //println("Symbol created: " + isa + ":" + name);
163 this.alias = this.getName();
164 this.setParams(params);
165 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
166 this.comment = comment || new DocComment("");
167 this.srcFile = Symbol.srcFile;
171 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
175 //if (typeof PluginManager != "undefined") {
176 // PluginManager.run("onSymbol", this);
180 setTags : function() {
182 var authors = this.comment.getTag("author");
183 if (authors.length) {
184 this.author = authors.map(function($){return $.desc;}).join(", ");
188 assert("testing Symbol");
190 requires("../lib/JSDOC/DocComment.js");
191 requires("../frame/String.js");
192 requires("../lib/JSDOC/DocTag.js");
194 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
195 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
198 var mth = this.comment.getTag("method");
200 this.isa = "FUNCTION";
203 var descs = this.comment.getTag("desc");
205 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
209 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
210 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
214 if (this.is("FILE")) {
215 if (!this.alias) this.alias = this.srcFile;
217 var overviews = this.comment.getTag("overview");
218 if (overviews.length) {
219 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
224 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
225 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
229 var sinces = this.comment.getTag("since");
231 this.since = sinces.map(function($){return $.desc;}).join(", ");
235 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
236 assertEqual(sym.since, "1.01", "@since tag, description is found.");
240 if (this.comment.getTag("constant").length) {
241 this.isConstant = true;
246 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
247 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
251 var versions = this.comment.getTag("version");
252 if (versions.length) {
253 this.version = versions.map(function($){return $.desc;}).join(", ");
257 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
258 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
262 var deprecateds = this.comment.getTag("deprecated");
263 if (deprecateds.length) {
264 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
268 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
269 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
273 var examples = this.comment.getTag("example");
274 if (examples.length) {
275 this.example = examples[0];
279 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
280 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
284 var sees = this.comment.getTag("see");
286 var thisSee = this.see;
287 sees.map(function($){thisSee.push($.desc);});
291 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
292 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
296 var classes = this.comment.getTag("class");
297 if (classes.length) {
298 this.isa = "CONSTRUCTOR";
299 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
300 if (!this.classDesc) {
301 this.classDesc = this.desc;
308 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
309 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
310 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
314 var namespaces = this.comment.getTag("namespace");
315 if (namespaces.length) {
316 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
317 this.isNamespace = true;
321 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
322 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
326 var params = this.comment.getTag("param");
328 // user-defined params overwrite those with same name defined by the parser
329 var thisParams = this.getParams();
331 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
332 this.setParams(params);
334 else { // need to overlay these user-defined params on to existing parser-defined params
335 for (var i = 0, l = params.length; i < l; i++) {
337 if (params[i].type) thisParams[i].type = params[i].type;
338 thisParams[i].name = params[i].name;
339 thisParams[i].desc = params[i].desc;
340 thisParams[i].isOptional = params[i].isOptional;
341 thisParams[i].defaultValue = params[i].defaultValue;
343 else thisParams[i] = params[i];
349 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
350 assertEqual(sym.params.length, 1, "parser defined param is found.");
352 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
353 assertEqual(sym.params.length, 1, "user defined param is found.");
354 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
355 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
357 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
358 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
359 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
360 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
362 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
363 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
364 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
365 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
367 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
368 assertEqual(sym.params.length, 1, "user defined param with description is found.");
369 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
373 if (this.comment.getTag("constructor").length) {
374 this.isa = "CONSTRUCTOR";
378 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
379 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
383 if (this.comment.getTag("static").length) {
384 this.isStatic = true;
385 if (this.isa == "CONSTRUCTOR") {
386 this.isNamespace = true;
391 if (this.comment.getTag("singleton").length) {
392 this.isStatic = true;
393 //if (this.isa == "CONSTRUCTOR") {
394 // this.isNamespace = true;
401 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
402 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
403 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
407 if (this.comment.getTag("inner").length) {
409 this.isStatic = false;
413 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
414 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
415 assertEqual(sym.isInner, true, "@inner makes isInner true.");
419 if (this.comment.getTag("field").length) {
424 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
425 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
429 if (this.comment.getTag("function").length) {
430 this.isa = "FUNCTION";
434 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
435 // change a property to a function..
436 this.isa = "FUNCTION";
441 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
442 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
446 var events = this.comment.getTag("event");
448 this.isa = "FUNCTION";
453 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
454 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
455 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
459 var names = this.comment.getTag("name");
461 this.setName(names[0].desc);
469 var properties = this.comment.getTag("property");
470 if (properties.length) {
471 thisProperties = this.properties;
472 for (var i = 0; i < properties.length; i++) {
473 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
474 // TODO: shouldn't the following happen in the addProperty method of Symbol?
475 property.name = properties[i].name;
476 property.memberOf = this.alias;
477 if (properties[i].type) property.type = properties[i].type;
478 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
479 this.addProperty(property);
480 imports.Parser.Parser.addSymbol(property);
485 var conf = this.comment.getTag("cfg");
487 for (var i = 0; i < conf.length; i++) {
488 this.addConfig(conf[i]);
497 var returns = this.comment.getTag("return");
498 if (returns.length) { // there can be many return tags in a single doclet
499 this.returns = returns;
500 this.type = returns.map(function($){return $.type}).join(", ");
508 this.exceptions = this.comment.getTag("throws");
515 var requires = this.comment.getTag("requires");
516 if (requires.length) {
517 this.requires = requires.map(function($){return $.desc});
525 var types = this.comment.getTag("type");
527 this.type = types[0].desc; //multiple type tags are ignored
535 if (this.comment.getTag("private").length || this.isInner) {
536 this.isPrivate = true;
540 if (this.comment.getTag("ignore").length) {
541 this.isIgnored = true;
548 // @inherits ... as ...
549 var inherits = this.comment.getTag("inherits");
550 if (inherits.length) {
551 for (var i = 0; i < inherits.length; i++) {
552 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
553 var inAlias = RegExp.$1;
554 var inAs = RegExp.$2 || inAlias;
556 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
559 inAs = inAs.replace(/\.prototype\.?/g, "#");
560 inAs = inAs.replace(/^this\.?/, "#");
563 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
565 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
568 inAs = this.alias + joiner + inAs;
571 this.inherits.push({alias: inAlias, as: inAs});
580 this.augments = this.comment.getTag("augments");
583 if (this.comment.getTag("extends")) {
584 this.augments = this.comment.getTag("extends");
589 var defaults = this.comment.getTag("default");
590 if (defaults.length) {
591 if (this.is("OBJECT")) {
592 this.defaultValue = defaults[0].desc;
601 var memberOfs = this.comment.getTag("memberOf");
602 if (memberOfs.length) {
603 this.memberOf = memberOfs[0].desc;
604 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
612 if (this.comment.getTag("public").length) {
613 this.isPrivate = false;
621 is : function(what) {
622 return this.isa === what;
625 isBuiltin : function() {
626 return SymbolSet.isBuiltin(this.alias);
629 setType : function(/**String*/comment, /**Boolean*/overwrite) {
630 if (!overwrite && this.type) return;
631 var typeComment = DocComment.unwrapComment(comment);
632 this.type = typeComment;
635 inherit : function(symbol) {
636 if (!this.hasMember(symbol.name) && !symbol.isInner) {
637 if (symbol.is("FUNCTION"))
638 this.methods.push(symbol);
639 else if (symbol.is("OBJECT"))
640 this.properties.push(symbol);
644 hasMember : function(name) {
645 return (this.hasMethod(name) || this.hasProperty(name));
648 addMember : function(symbol) {
649 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
651 if (symbol.comment.getTag("cfg").length == 1) {
652 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
653 this.addConfig(symbol.comment.getTag("cfg")[0]);
657 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
658 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
661 hasMethod : function(name) {
662 var thisMethods = this.methods;
663 for (var i = 0, l = thisMethods.length; i < l; i++) {
664 if (thisMethods[i].name == name) return true;
665 if (thisMethods[i].alias == name) return true;
670 addMethod : function(symbol) {
671 var methodAlias = symbol.alias;
672 var thisMethods = this.methods;
673 for (var i = 0, l = thisMethods.length; i < l; i++) {
674 if (thisMethods[i].alias == methodAlias) {
675 thisMethods[i] = symbol; // overwriting previous method
679 thisMethods.push(symbol); // new method with this alias
682 hasProperty : function(name) {
683 var thisProperties = this.properties;
684 for (var i = 0, l = thisProperties.length; i < l; i++) {
685 if (thisProperties[i].name == name) return true;
686 if (thisProperties[i].alias == name) return true;
691 addProperty : function(symbol) {
692 var propertyAlias = symbol.alias;
693 var thisProperties = this.properties;
694 for (var i = 0, l = thisProperties.length; i < l; i++) {
695 if (thisProperties[i].alias == propertyAlias) {
696 thisProperties[i] = symbol; // overwriting previous property
701 thisProperties.push(symbol); // new property with this alias
704 addDocTag : function(docTag)
706 this.comment.tags.push(docTag);
707 if (docTag.title == 'cfg') {
708 this.addConfig(docTag);
713 addConfig : function(docTag)
715 if (typeof(docTag['memberOf']) == 'undefined') {
716 // remove prototype data...
717 //var a = this.alias.split('#')[0];
718 //docTag.memberOf = a;
719 docTag.memberOf = this.alias;
721 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
722 this.cfgs[docTag.name] = docTag;
726 configToArray: function()
729 for(var ci in this.cfgs) {
730 // dont show hidden!!
731 if (this.cfgs[ci].desc.match(/@hide/)) {
734 r.push(this.cfgs[ci]);
745 Symbol.srcFile = ""; //running reference to the current file being parsed
748 Symbol.fromDump = function(t)
750 var ns = new Symbol();
752 if (typeof(ns[i]) == "undefined") {
753 println("ERR:no default for Symbol:"+ i);