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 : [], // Doctag[]
37 exceptions : [], // Doctag[]
38 inherits : [], // Doctag[]
40 isa : "OBJECT", // OBJECT//FUNCTION
49 methods : [], // Symbol[]
69 function array2object(a)
73 a.forEach(function(e) {
81 if (Symbol.hide.indexOf(i) > -1) {
84 switch (typeof(this[i])) {
95 ret[i] = array2object(this[i],true);
101 print(JSON.stringify(this[i]));
102 ret[i] = this[i].toObject();
106 print("object? :" + i);
113 ret[i] = this[i]; continue;
115 print("unknown type:" + typeof(this[i]));
125 // only initialize arrays / objects..
130 this.exceptions = [];
132 //this.inheritsFrom = [];
133 this.isa = "OBJECT"; // OBJECT//FUNCTION
136 this.properties = [];
141 this.childClasses = [];
144 this.comment = new DocComment();
145 this.comment.isUserComment = false;
150 serialize : function() {
152 for (var p in this) {
158 for (var i in keys) {
159 if (typeof this[keys[i]] == "function") continue;
160 out += " " +keys[i]+" => "+
162 (typeof(this[keys[i]]) != "object") ?
164 "[" +typeof(this[keys[i]])+"]"
168 return "\n{\n" + out + "}\n";
172 var clone = new Symbol();
173 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
174 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
181 //__defineSetter__("name",
182 setName : function(n) {
183 n = n.replace(/^_global_[.#-]/, "");
184 n = n.replace(/\.prototype\.?/g, '#');
185 n = n.replace(/#$/g, '');
187 this.name = n; // real!
190 //__defineGetter__("name",
191 getName : function() { return this._name; },
193 //__defineSetter__("params",
194 setParams :function(v) {
195 for (var i = 0, l = v.length; i < l; i++) {
196 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
197 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
198 this._params[i] = new DocTag(
199 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
202 this._params[i] = v[i];
205 this.params = this._params;
210 //__defineGetter__("params",
211 getParams : function() { return this._params; },
216 /** Object[] */ params,
218 /** DocComment */ comment
220 this.$args = arguments;
221 //println("Symbol created: " + isa + ":" + name);
223 this.alias = this.getName();
224 this.setParams(params);
225 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
226 this.comment = comment || new DocComment("");
227 this.srcFile = Symbol.srcFile;
231 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
235 //if (typeof PluginManager != "undefined") {
236 // PluginManager.run("onSymbol", this);
240 setTags : function() {
242 var authors = this.comment.getTag("author");
243 if (authors.length) {
244 this.author = authors.map(function($){return $.desc;}).join(", ");
248 assert("testing Symbol");
250 requires("../lib/JSDOC/DocComment.js");
251 requires("../frame/String.js");
252 requires("../lib/JSDOC/DocTag.js");
254 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
255 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
258 var mth = this.comment.getTag("method");
260 this.isa = "FUNCTION";
263 var descs = this.comment.getTag("desc");
265 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
269 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
270 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
274 if (this.is("FILE")) {
275 if (!this.alias) this.alias = this.srcFile;
277 var overviews = this.comment.getTag("overview");
278 if (overviews.length) {
279 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
284 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
285 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
289 var sinces = this.comment.getTag("since");
291 this.since = sinces.map(function($){return $.desc;}).join(", ");
295 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
296 assertEqual(sym.since, "1.01", "@since tag, description is found.");
300 if (this.comment.getTag("constant").length) {
301 this.isConstant = true;
306 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
307 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
311 var versions = this.comment.getTag("version");
312 if (versions.length) {
313 this.version = versions.map(function($){return $.desc;}).join(", ");
317 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
318 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
322 var deprecateds = this.comment.getTag("deprecated");
323 if (deprecateds.length) {
324 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
328 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
329 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
333 var examples = this.comment.getTag("example");
334 if (examples.length) {
335 this.example = examples[0];
339 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
340 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
344 var sees = this.comment.getTag("see");
346 var thisSee = this.see;
347 sees.map(function($){thisSee.push($.desc);});
351 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
352 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
356 var classes = this.comment.getTag("class");
357 if (classes.length) {
358 this.isa = "CONSTRUCTOR";
359 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
360 if (!this.classDesc) {
361 this.classDesc = this.desc;
368 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
369 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
370 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
374 var namespaces = this.comment.getTag("namespace");
375 if (namespaces.length) {
376 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
377 this.isNamespace = true;
381 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
382 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
386 var params = this.comment.getTag("param");
388 // user-defined params overwrite those with same name defined by the parser
389 var thisParams = this.getParams();
391 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
392 this.setParams(params);
394 else { // need to overlay these user-defined params on to existing parser-defined params
395 for (var i = 0, l = params.length; i < l; i++) {
397 if (params[i].type) thisParams[i].type = params[i].type;
398 thisParams[i].name = params[i].name;
399 thisParams[i].desc = params[i].desc;
400 thisParams[i].isOptional = params[i].isOptional;
401 thisParams[i].defaultValue = params[i].defaultValue;
403 else thisParams[i] = params[i];
409 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
410 assertEqual(sym.params.length, 1, "parser defined param is found.");
412 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
413 assertEqual(sym.params.length, 1, "user defined param is found.");
414 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
415 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
417 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
418 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
419 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
420 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
422 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
423 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
424 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
425 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
427 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
428 assertEqual(sym.params.length, 1, "user defined param with description is found.");
429 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
433 if (this.comment.getTag("constructor").length) {
434 this.isa = "CONSTRUCTOR";
438 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
439 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
443 if (this.comment.getTag("static").length) {
444 this.isStatic = true;
445 if (this.isa == "CONSTRUCTOR") {
446 this.isNamespace = true;
451 if (this.comment.getTag("singleton").length) {
452 this.isStatic = true;
453 //if (this.isa == "CONSTRUCTOR") {
454 // this.isNamespace = true;
461 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
462 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
463 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
467 if (this.comment.getTag("inner").length) {
469 this.isStatic = false;
473 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
474 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
475 assertEqual(sym.isInner, true, "@inner makes isInner true.");
479 if (this.comment.getTag("field").length) {
484 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
485 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
489 if (this.comment.getTag("function").length) {
490 this.isa = "FUNCTION";
494 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
495 // change a property to a function..
496 this.isa = "FUNCTION";
501 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
502 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
506 var events = this.comment.getTag("event");
508 this.isa = "FUNCTION";
513 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
514 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
515 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
519 var names = this.comment.getTag("name");
521 this.setName(names[0].desc);
529 var properties = this.comment.getTag("property");
530 if (properties.length) {
531 thisProperties = this.properties;
532 for (var i = 0; i < properties.length; i++) {
533 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
534 // TODO: shouldn't the following happen in the addProperty method of Symbol?
535 property.name = properties[i].name;
536 property.memberOf = this.alias;
537 if (properties[i].type) property.type = properties[i].type;
538 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
539 this.addProperty(property);
540 imports.Parser.Parser.addSymbol(property);
545 var conf = this.comment.getTag("cfg");
547 for (var i = 0; i < conf.length; i++) {
548 this.addConfig(conf[i]);
557 var returns = this.comment.getTag("return");
558 if (returns.length) { // there can be many return tags in a single doclet
559 this.returns = returns;
560 this.type = returns.map(function($){return $.type}).join(", ");
568 this.exceptions = this.comment.getTag("throws");
575 var requires = this.comment.getTag("requires");
576 if (requires.length) {
577 this.requires = requires.map(function($){return $.desc});
585 var types = this.comment.getTag("type");
587 this.type = types[0].desc; //multiple type tags are ignored
595 if (this.comment.getTag("private").length || this.isInner) {
596 this.isPrivate = true;
600 if (this.comment.getTag("ignore").length) {
601 this.isIgnored = true;
608 // @inherits ... as ...
609 var inherits = this.comment.getTag("inherits");
610 if (inherits.length) {
611 for (var i = 0; i < inherits.length; i++) {
612 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
613 var inAlias = RegExp.$1;
614 var inAs = RegExp.$2 || inAlias;
616 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
619 inAs = inAs.replace(/\.prototype\.?/g, "#");
620 inAs = inAs.replace(/^this\.?/, "#");
623 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
625 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
628 inAs = this.alias + joiner + inAs;
631 this.inherits.push({alias: inAlias, as: inAs});
640 this.augments = this.comment.getTag("augments");
643 if (this.comment.getTag("extends")) {
644 this.augments = this.comment.getTag("extends");
649 var defaults = this.comment.getTag("default");
650 if (defaults.length) {
651 if (this.is("OBJECT")) {
652 this.defaultValue = defaults[0].desc;
661 var memberOfs = this.comment.getTag("memberOf");
662 if (memberOfs.length) {
663 this.memberOf = memberOfs[0].desc;
664 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
672 if (this.comment.getTag("public").length) {
673 this.isPrivate = false;
681 is : function(what) {
682 return this.isa === what;
685 isBuiltin : function() {
686 return SymbolSet.isBuiltin(this.alias);
689 setType : function(/**String*/comment, /**Boolean*/overwrite) {
690 if (!overwrite && this.type) return;
691 var typeComment = DocComment.unwrapComment(comment);
692 this.type = typeComment;
695 inherit : function(symbol) {
696 if (!this.hasMember(symbol.name) && !symbol.isInner) {
697 if (symbol.is("FUNCTION"))
698 this.methods.push(symbol);
699 else if (symbol.is("OBJECT"))
700 this.properties.push(symbol);
704 hasMember : function(name) {
705 return (this.hasMethod(name) || this.hasProperty(name));
708 addMember : function(symbol) {
709 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
711 if (symbol.comment.getTag("cfg").length == 1) {
712 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
713 this.addConfig(symbol.comment.getTag("cfg")[0]);
717 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
718 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
721 hasMethod : function(name) {
722 var thisMethods = this.methods;
723 for (var i = 0, l = thisMethods.length; i < l; i++) {
724 if (thisMethods[i].name == name) return true;
725 if (thisMethods[i].alias == name) return true;
730 addMethod : function(symbol) {
731 var methodAlias = symbol.alias;
732 var thisMethods = this.methods;
733 for (var i = 0, l = thisMethods.length; i < l; i++) {
734 if (thisMethods[i].alias == methodAlias) {
735 thisMethods[i] = symbol; // overwriting previous method
739 thisMethods.push(symbol); // new method with this alias
742 hasProperty : function(name) {
743 var thisProperties = this.properties;
744 for (var i = 0, l = thisProperties.length; i < l; i++) {
745 if (thisProperties[i].name == name) return true;
746 if (thisProperties[i].alias == name) return true;
751 addProperty : function(symbol) {
752 var propertyAlias = symbol.alias;
753 var thisProperties = this.properties;
754 for (var i = 0, l = thisProperties.length; i < l; i++) {
755 if (thisProperties[i].alias == propertyAlias) {
756 thisProperties[i] = symbol; // overwriting previous property
761 thisProperties.push(symbol); // new property with this alias
764 addDocTag : function(docTag)
766 this.comment.tags.push(docTag);
767 if (docTag.title == 'cfg') {
768 this.addConfig(docTag);
773 addConfig : function(docTag)
775 if (typeof(docTag['memberOf']) == 'undefined') {
776 // remove prototype data...
777 //var a = this.alias.split('#')[0];
778 //docTag.memberOf = a;
779 docTag.memberOf = this.alias;
781 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
782 this.cfgs[docTag.name] = docTag;
786 configToArray: function()
789 for(var ci in this.cfgs) {
790 // dont show hidden!!
791 if (this.cfgs[ci].desc.match(/@hide/)) {
794 r.push(this.cfgs[ci]);
802 * Elements that are not serialized
806 '$args' // not needed AFAIK
809 Symbol.srcFile = ""; //running reference to the current file being parsed
812 Symbol.fromDump = function(t)
814 var ns = new Symbol();
816 if (typeof(ns[i]) == "undefined") {
817 println("ERR:no default for Symbol:"+ i);