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);
26 $args : [], // original arguments used when constructing.
29 augments : false, // Doctag
40 isa : "OBJECT", // OBJECT//FUNCTION
69 function array2object(a)
73 a.forEach(function(e) {
80 if (Symbol.hide.indexOf(i) > -1) {
83 switch (typeof(this[i])) {
90 ret[i] = array2object(this[i],true);
96 print(JSON.stringify(this[i]));
97 ret[i] = this[i].toObject();
101 print("object? :" + i);
108 ret[i] = this[i]; continue;
110 print("unknown type:" + typeof(this[i]));
120 // only initialize arrays / objects..
125 this.exceptions = [];
127 this.inheritsFrom = [];
128 this.isa = "OBJECT"; // OBJECT//FUNCTION
131 this.properties = [];
136 this.childClasses = [];
139 this.comment = new DocComment();
140 this.comment.isUserComment = false;
145 serialize : function() {
147 for (var p in this) {
153 for (var i in keys) {
154 if (typeof this[keys[i]] == "function") continue;
155 out += " " +keys[i]+" => "+
157 (typeof(this[keys[i]]) != "object") ?
159 "[" +typeof(this[keys[i]])+"]"
163 return "\n{\n" + out + "}\n";
167 var clone = new Symbol();
168 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
169 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
176 //__defineSetter__("name",
177 setName : function(n) {
178 n = n.replace(/^_global_[.#-]/, "");
179 n = n.replace(/\.prototype\.?/g, '#');
180 n = n.replace(/#$/g, '');
182 this.name = n; // real!
185 //__defineGetter__("name",
186 getName : function() { return this._name; },
188 //__defineSetter__("params",
189 setParams :function(v) {
190 for (var i = 0, l = v.length; i < l; i++) {
191 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
192 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
193 this._params[i] = new DocTag(
194 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
197 this._params[i] = v[i];
200 this.params = this._params;
205 //__defineGetter__("params",
206 getParams : function() { return this._params; },
211 /** Object[] */ params,
213 /** DocComment */ comment
215 this.$args = arguments;
216 //println("Symbol created: " + isa + ":" + name);
218 this.alias = this.getName();
219 this.setParams(params);
220 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
221 this.comment = comment || new DocComment("");
222 this.srcFile = Symbol.srcFile;
226 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
230 //if (typeof PluginManager != "undefined") {
231 // PluginManager.run("onSymbol", this);
235 setTags : function() {
237 var authors = this.comment.getTag("author");
238 if (authors.length) {
239 this.author = authors.map(function($){return $.desc;}).join(", ");
243 assert("testing Symbol");
245 requires("../lib/JSDOC/DocComment.js");
246 requires("../frame/String.js");
247 requires("../lib/JSDOC/DocTag.js");
249 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
250 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
253 var mth = this.comment.getTag("method");
255 this.isa = "FUNCTION";
258 var descs = this.comment.getTag("desc");
260 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
264 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
265 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
269 if (this.is("FILE")) {
270 if (!this.alias) this.alias = this.srcFile;
272 var overviews = this.comment.getTag("overview");
273 if (overviews.length) {
274 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
279 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
280 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
284 var sinces = this.comment.getTag("since");
286 this.since = sinces.map(function($){return $.desc;}).join(", ");
290 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
291 assertEqual(sym.since, "1.01", "@since tag, description is found.");
295 if (this.comment.getTag("constant").length) {
296 this.isConstant = true;
301 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
302 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
306 var versions = this.comment.getTag("version");
307 if (versions.length) {
308 this.version = versions.map(function($){return $.desc;}).join(", ");
312 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
313 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
317 var deprecateds = this.comment.getTag("deprecated");
318 if (deprecateds.length) {
319 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
323 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
324 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
328 var examples = this.comment.getTag("example");
329 if (examples.length) {
330 this.example = examples[0];
334 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
335 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
339 var sees = this.comment.getTag("see");
341 var thisSee = this.see;
342 sees.map(function($){thisSee.push($.desc);});
346 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
347 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
351 var classes = this.comment.getTag("class");
352 if (classes.length) {
353 this.isa = "CONSTRUCTOR";
354 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
355 if (!this.classDesc) {
356 this.classDesc = this.desc;
363 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
364 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
365 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
369 var namespaces = this.comment.getTag("namespace");
370 if (namespaces.length) {
371 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
372 this.isNamespace = true;
376 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
377 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
381 var params = this.comment.getTag("param");
383 // user-defined params overwrite those with same name defined by the parser
384 var thisParams = this.getParams();
386 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
387 this.setParams(params);
389 else { // need to overlay these user-defined params on to existing parser-defined params
390 for (var i = 0, l = params.length; i < l; i++) {
392 if (params[i].type) thisParams[i].type = params[i].type;
393 thisParams[i].name = params[i].name;
394 thisParams[i].desc = params[i].desc;
395 thisParams[i].isOptional = params[i].isOptional;
396 thisParams[i].defaultValue = params[i].defaultValue;
398 else thisParams[i] = params[i];
404 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
405 assertEqual(sym.params.length, 1, "parser defined param is found.");
407 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
408 assertEqual(sym.params.length, 1, "user defined param is found.");
409 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
410 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
412 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
413 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
414 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
415 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
417 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
418 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
419 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
420 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
422 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
423 assertEqual(sym.params.length, 1, "user defined param with description is found.");
424 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
428 if (this.comment.getTag("constructor").length) {
429 this.isa = "CONSTRUCTOR";
433 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
434 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
438 if (this.comment.getTag("static").length) {
439 this.isStatic = true;
440 if (this.isa == "CONSTRUCTOR") {
441 this.isNamespace = true;
446 if (this.comment.getTag("singleton").length) {
447 this.isStatic = true;
448 //if (this.isa == "CONSTRUCTOR") {
449 // this.isNamespace = true;
456 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
457 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
458 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
462 if (this.comment.getTag("inner").length) {
464 this.isStatic = false;
468 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
469 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
470 assertEqual(sym.isInner, true, "@inner makes isInner true.");
474 if (this.comment.getTag("field").length) {
479 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
480 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
484 if (this.comment.getTag("function").length) {
485 this.isa = "FUNCTION";
489 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
490 // change a property to a function..
491 this.isa = "FUNCTION";
496 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
497 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
501 var events = this.comment.getTag("event");
503 this.isa = "FUNCTION";
508 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
509 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
510 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
514 var names = this.comment.getTag("name");
516 this.setName(names[0].desc);
524 var properties = this.comment.getTag("property");
525 if (properties.length) {
526 thisProperties = this.properties;
527 for (var i = 0; i < properties.length; i++) {
528 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
529 // TODO: shouldn't the following happen in the addProperty method of Symbol?
530 property.name = properties[i].name;
531 property.memberOf = this.alias;
532 if (properties[i].type) property.type = properties[i].type;
533 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
534 this.addProperty(property);
535 imports.Parser.Parser.addSymbol(property);
540 var conf = this.comment.getTag("cfg");
542 for (var i = 0; i < conf.length; i++) {
543 this.addConfig(conf[i]);
552 var returns = this.comment.getTag("return");
553 if (returns.length) { // there can be many return tags in a single doclet
554 this.returns = returns;
555 this.type = returns.map(function($){return $.type}).join(", ");
563 this.exceptions = this.comment.getTag("throws");
570 var requires = this.comment.getTag("requires");
571 if (requires.length) {
572 this.requires = requires.map(function($){return $.desc});
580 var types = this.comment.getTag("type");
582 this.type = types[0].desc; //multiple type tags are ignored
590 if (this.comment.getTag("private").length || this.isInner) {
591 this.isPrivate = true;
595 if (this.comment.getTag("ignore").length) {
596 this.isIgnored = true;
603 // @inherits ... as ...
604 var inherits = this.comment.getTag("inherits");
605 if (inherits.length) {
606 for (var i = 0; i < inherits.length; i++) {
607 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
608 var inAlias = RegExp.$1;
609 var inAs = RegExp.$2 || inAlias;
611 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
614 inAs = inAs.replace(/\.prototype\.?/g, "#");
615 inAs = inAs.replace(/^this\.?/, "#");
618 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
620 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
623 inAs = this.alias + joiner + inAs;
626 this.inherits.push({alias: inAlias, as: inAs});
635 this.augments = this.comment.getTag("augments");
638 if (this.comment.getTag("extends")) {
639 this.augments = this.comment.getTag("extends");
644 var defaults = this.comment.getTag("default");
645 if (defaults.length) {
646 if (this.is("OBJECT")) {
647 this.defaultValue = defaults[0].desc;
656 var memberOfs = this.comment.getTag("memberOf");
657 if (memberOfs.length) {
658 this.memberOf = memberOfs[0].desc;
659 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
667 if (this.comment.getTag("public").length) {
668 this.isPrivate = false;
676 is : function(what) {
677 return this.isa === what;
680 isBuiltin : function() {
681 return SymbolSet.isBuiltin(this.alias);
684 setType : function(/**String*/comment, /**Boolean*/overwrite) {
685 if (!overwrite && this.type) return;
686 var typeComment = DocComment.unwrapComment(comment);
687 this.type = typeComment;
690 inherit : function(symbol) {
691 if (!this.hasMember(symbol.name) && !symbol.isInner) {
692 if (symbol.is("FUNCTION"))
693 this.methods.push(symbol);
694 else if (symbol.is("OBJECT"))
695 this.properties.push(symbol);
699 hasMember : function(name) {
700 return (this.hasMethod(name) || this.hasProperty(name));
703 addMember : function(symbol) {
704 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
706 if (symbol.comment.getTag("cfg").length == 1) {
707 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
708 this.addConfig(symbol.comment.getTag("cfg")[0]);
712 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
713 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
716 hasMethod : function(name) {
717 var thisMethods = this.methods;
718 for (var i = 0, l = thisMethods.length; i < l; i++) {
719 if (thisMethods[i].name == name) return true;
720 if (thisMethods[i].alias == name) return true;
725 addMethod : function(symbol) {
726 var methodAlias = symbol.alias;
727 var thisMethods = this.methods;
728 for (var i = 0, l = thisMethods.length; i < l; i++) {
729 if (thisMethods[i].alias == methodAlias) {
730 thisMethods[i] = symbol; // overwriting previous method
734 thisMethods.push(symbol); // new method with this alias
737 hasProperty : function(name) {
738 var thisProperties = this.properties;
739 for (var i = 0, l = thisProperties.length; i < l; i++) {
740 if (thisProperties[i].name == name) return true;
741 if (thisProperties[i].alias == name) return true;
746 addProperty : function(symbol) {
747 var propertyAlias = symbol.alias;
748 var thisProperties = this.properties;
749 for (var i = 0, l = thisProperties.length; i < l; i++) {
750 if (thisProperties[i].alias == propertyAlias) {
751 thisProperties[i] = symbol; // overwriting previous property
756 thisProperties.push(symbol); // new property with this alias
759 addDocTag : function(docTag)
761 this.comment.tags.push(docTag);
762 if (docTag.title == 'cfg') {
763 this.addConfig(docTag);
768 addConfig : function(docTag)
770 if (typeof(docTag['memberOf']) == 'undefined') {
771 // remove prototype data...
772 //var a = this.alias.split('#')[0];
773 //docTag.memberOf = a;
774 docTag.memberOf = this.alias;
776 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
777 this.cfgs[docTag.name] = docTag;
781 configToArray: function()
784 for(var ci in this.cfgs) {
785 // dont show hidden!!
786 if (this.cfgs[ci].desc.match(/@hide/)) {
789 r.push(this.cfgs[ci]);
797 * Elements that are not serialized
801 '$args' // not needed AFAIK
804 Symbol.srcFile = ""; //running reference to the current file being parsed
807 Symbol.fromDump = function(t)
809 var ns = new Symbol();
811 if (typeof(ns[i]) == "undefined") {
812 println("ERR:no default for Symbol:"+ i);