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);
27 function array2object(a , m)
31 a.forEach(function(e) {
32 r.push(m ? e : e.toObject());
37 switch (typeof(this[i])) {
42 print("object? :" + i);
46 ret[i] = this[i]; continue;
48 print("unknown type:" + typeof(this[i]));
59 this.defaultValue = "";
67 this.comment = { isUserComment: false };
68 //this.defaultValue = null;
75 this.inheritsFrom = [];
76 this.isa = "OBJECT"; // OBJECT//FUNCTION
78 this.isConstant = false;
79 this.isIgnored = false;
81 this.isNamespace = false;
82 this.isPrivate = false;
83 this.isStatic = false;
96 this.childClasses = [];
101 serialize : function() {
103 for (var p in this) {
109 for (var i in keys) {
110 if (typeof this[keys[i]] == "function") continue;
111 out += " " +keys[i]+" => "+
113 (typeof(this[keys[i]]) != "object") ?
115 "[" +typeof(this[keys[i]])+"]"
119 return "\n{\n" + out + "}\n";
123 var clone = new Symbol();
124 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
125 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
132 //__defineSetter__("name",
133 setName : function(n) {
134 n = n.replace(/^_global_[.#-]/, "");
135 n = n.replace(/\.prototype\.?/g, '#');
136 n = n.replace(/#$/g, '');
138 this.name = n; // real!
141 //__defineGetter__("name",
142 getName : function() { return this._name; },
144 //__defineSetter__("params",
145 setParams :function(v) {
146 for (var i = 0, l = v.length; i < l; i++) {
147 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
148 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
149 this._params[i] = new DocTag(
150 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
153 this._params[i] = v[i];
156 this.params = this._params;
161 //__defineGetter__("params",
162 getParams : function() { return this._params; },
167 /** Object[] */ params,
169 /** DocComment */ comment
171 this.$args = arguments;
172 //println("Symbol created: " + isa + ":" + name);
174 this.alias = this.getName();
175 this.setParams(params);
176 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
177 this.comment = comment || new DocComment("");
178 this.srcFile = Symbol.srcFile;
182 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
186 //if (typeof PluginManager != "undefined") {
187 // PluginManager.run("onSymbol", this);
191 setTags : function() {
193 var authors = this.comment.getTag("author");
194 if (authors.length) {
195 this.author = authors.map(function($){return $.desc;}).join(", ");
199 assert("testing Symbol");
201 requires("../lib/JSDOC/DocComment.js");
202 requires("../frame/String.js");
203 requires("../lib/JSDOC/DocTag.js");
205 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
206 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
209 var mth = this.comment.getTag("method");
211 this.isa = "FUNCTION";
214 var descs = this.comment.getTag("desc");
216 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
220 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
221 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
225 if (this.is("FILE")) {
226 if (!this.alias) this.alias = this.srcFile;
228 var overviews = this.comment.getTag("overview");
229 if (overviews.length) {
230 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
235 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
236 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
240 var sinces = this.comment.getTag("since");
242 this.since = sinces.map(function($){return $.desc;}).join(", ");
246 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
247 assertEqual(sym.since, "1.01", "@since tag, description is found.");
251 if (this.comment.getTag("constant").length) {
252 this.isConstant = true;
257 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
258 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
262 var versions = this.comment.getTag("version");
263 if (versions.length) {
264 this.version = versions.map(function($){return $.desc;}).join(", ");
268 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
269 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
273 var deprecateds = this.comment.getTag("deprecated");
274 if (deprecateds.length) {
275 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
279 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
280 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
284 var examples = this.comment.getTag("example");
285 if (examples.length) {
286 this.example = examples[0];
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
291 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
295 var sees = this.comment.getTag("see");
297 var thisSee = this.see;
298 sees.map(function($){thisSee.push($.desc);});
302 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
303 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
307 var classes = this.comment.getTag("class");
308 if (classes.length) {
309 this.isa = "CONSTRUCTOR";
310 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
311 if (!this.classDesc) {
312 this.classDesc = this.desc;
319 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
320 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
321 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
325 var namespaces = this.comment.getTag("namespace");
326 if (namespaces.length) {
327 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
328 this.isNamespace = true;
332 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
333 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
337 var params = this.comment.getTag("param");
339 // user-defined params overwrite those with same name defined by the parser
340 var thisParams = this.getParams();
342 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
343 this.setParams(params);
345 else { // need to overlay these user-defined params on to existing parser-defined params
346 for (var i = 0, l = params.length; i < l; i++) {
348 if (params[i].type) thisParams[i].type = params[i].type;
349 thisParams[i].name = params[i].name;
350 thisParams[i].desc = params[i].desc;
351 thisParams[i].isOptional = params[i].isOptional;
352 thisParams[i].defaultValue = params[i].defaultValue;
354 else thisParams[i] = params[i];
360 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
361 assertEqual(sym.params.length, 1, "parser defined param is found.");
363 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
364 assertEqual(sym.params.length, 1, "user defined param is found.");
365 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
366 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
368 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
369 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
370 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
371 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
373 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
374 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
375 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
376 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
378 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
379 assertEqual(sym.params.length, 1, "user defined param with description is found.");
380 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
384 if (this.comment.getTag("constructor").length) {
385 this.isa = "CONSTRUCTOR";
389 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
390 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
394 if (this.comment.getTag("static").length) {
395 this.isStatic = true;
396 if (this.isa == "CONSTRUCTOR") {
397 this.isNamespace = true;
402 if (this.comment.getTag("singleton").length) {
403 this.isStatic = true;
404 //if (this.isa == "CONSTRUCTOR") {
405 // this.isNamespace = true;
412 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
413 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
414 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
418 if (this.comment.getTag("inner").length) {
420 this.isStatic = false;
424 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
425 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
426 assertEqual(sym.isInner, true, "@inner makes isInner true.");
430 if (this.comment.getTag("field").length) {
435 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
436 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
440 if (this.comment.getTag("function").length) {
441 this.isa = "FUNCTION";
445 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
446 // change a property to a function..
447 this.isa = "FUNCTION";
452 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
453 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
457 var events = this.comment.getTag("event");
459 this.isa = "FUNCTION";
464 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
465 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
466 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
470 var names = this.comment.getTag("name");
472 this.setName(names[0].desc);
480 var properties = this.comment.getTag("property");
481 if (properties.length) {
482 thisProperties = this.properties;
483 for (var i = 0; i < properties.length; i++) {
484 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
485 // TODO: shouldn't the following happen in the addProperty method of Symbol?
486 property.name = properties[i].name;
487 property.memberOf = this.alias;
488 if (properties[i].type) property.type = properties[i].type;
489 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
490 this.addProperty(property);
491 imports.Parser.Parser.addSymbol(property);
496 var conf = this.comment.getTag("cfg");
498 for (var i = 0; i < conf.length; i++) {
499 this.addConfig(conf[i]);
508 var returns = this.comment.getTag("return");
509 if (returns.length) { // there can be many return tags in a single doclet
510 this.returns = returns;
511 this.type = returns.map(function($){return $.type}).join(", ");
519 this.exceptions = this.comment.getTag("throws");
526 var requires = this.comment.getTag("requires");
527 if (requires.length) {
528 this.requires = requires.map(function($){return $.desc});
536 var types = this.comment.getTag("type");
538 this.type = types[0].desc; //multiple type tags are ignored
546 if (this.comment.getTag("private").length || this.isInner) {
547 this.isPrivate = true;
551 if (this.comment.getTag("ignore").length) {
552 this.isIgnored = true;
559 // @inherits ... as ...
560 var inherits = this.comment.getTag("inherits");
561 if (inherits.length) {
562 for (var i = 0; i < inherits.length; i++) {
563 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
564 var inAlias = RegExp.$1;
565 var inAs = RegExp.$2 || inAlias;
567 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
570 inAs = inAs.replace(/\.prototype\.?/g, "#");
571 inAs = inAs.replace(/^this\.?/, "#");
574 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
576 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
579 inAs = this.alias + joiner + inAs;
582 this.inherits.push({alias: inAlias, as: inAs});
591 this.augments = this.comment.getTag("augments");
594 if (this.comment.getTag("extends")) {
595 this.augments = this.comment.getTag("extends");
600 var defaults = this.comment.getTag("default");
601 if (defaults.length) {
602 if (this.is("OBJECT")) {
603 this.defaultValue = defaults[0].desc;
612 var memberOfs = this.comment.getTag("memberOf");
613 if (memberOfs.length) {
614 this.memberOf = memberOfs[0].desc;
615 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
623 if (this.comment.getTag("public").length) {
624 this.isPrivate = false;
632 is : function(what) {
633 return this.isa === what;
636 isBuiltin : function() {
637 return SymbolSet.isBuiltin(this.alias);
640 setType : function(/**String*/comment, /**Boolean*/overwrite) {
641 if (!overwrite && this.type) return;
642 var typeComment = DocComment.unwrapComment(comment);
643 this.type = typeComment;
646 inherit : function(symbol) {
647 if (!this.hasMember(symbol.name) && !symbol.isInner) {
648 if (symbol.is("FUNCTION"))
649 this.methods.push(symbol);
650 else if (symbol.is("OBJECT"))
651 this.properties.push(symbol);
655 hasMember : function(name) {
656 return (this.hasMethod(name) || this.hasProperty(name));
659 addMember : function(symbol) {
660 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
662 if (symbol.comment.getTag("cfg").length == 1) {
663 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
664 this.addConfig(symbol.comment.getTag("cfg")[0]);
668 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
669 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
672 hasMethod : function(name) {
673 var thisMethods = this.methods;
674 for (var i = 0, l = thisMethods.length; i < l; i++) {
675 if (thisMethods[i].name == name) return true;
676 if (thisMethods[i].alias == name) return true;
681 addMethod : function(symbol) {
682 var methodAlias = symbol.alias;
683 var thisMethods = this.methods;
684 for (var i = 0, l = thisMethods.length; i < l; i++) {
685 if (thisMethods[i].alias == methodAlias) {
686 thisMethods[i] = symbol; // overwriting previous method
690 thisMethods.push(symbol); // new method with this alias
693 hasProperty : function(name) {
694 var thisProperties = this.properties;
695 for (var i = 0, l = thisProperties.length; i < l; i++) {
696 if (thisProperties[i].name == name) return true;
697 if (thisProperties[i].alias == name) return true;
702 addProperty : function(symbol) {
703 var propertyAlias = symbol.alias;
704 var thisProperties = this.properties;
705 for (var i = 0, l = thisProperties.length; i < l; i++) {
706 if (thisProperties[i].alias == propertyAlias) {
707 thisProperties[i] = symbol; // overwriting previous property
712 thisProperties.push(symbol); // new property with this alias
715 addDocTag : function(docTag)
717 this.comment.tags.push(docTag);
718 if (docTag.title == 'cfg') {
719 this.addConfig(docTag);
724 addConfig : function(docTag)
726 if (typeof(docTag['memberOf']) == 'undefined') {
727 // remove prototype data...
728 //var a = this.alias.split('#')[0];
729 //docTag.memberOf = a;
730 docTag.memberOf = this.alias;
732 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
733 this.cfgs[docTag.name] = docTag;
737 configToArray: function()
740 for(var ci in this.cfgs) {
741 // dont show hidden!!
742 if (this.cfgs[ci].desc.match(/@hide/)) {
745 r.push(this.cfgs[ci]);
756 Symbol.srcFile = ""; //running reference to the current file being parsed
759 Symbol.fromDump = function(t)
761 var ns = new Symbol();
763 if (typeof(ns[i]) == "undefined") {
764 println("ERR:no default for Symbol:"+ i);