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 , m)
73 a.forEach(function(e) {
74 r.push(m ? e : e.toObject());
80 if (Symbol.hide.indexOf(i) > -1) {
83 switch (typeof(this[i])) {
90 ret[i] = array2object(this[i],true);
95 print(JSON.stringify(this[i]));
96 ret[i] = this[i].toObject();
100 print("object? :" + i);
107 ret[i] = this[i]; continue;
109 print("unknown type:" + typeof(this[i]));
119 // only initialize arrays / objects..
124 this.exceptions = [];
126 this.inheritsFrom = [];
127 this.isa = "OBJECT"; // OBJECT//FUNCTION
130 this.properties = [];
135 this.childClasses = [];
138 this.comment = new DocComment();
139 this.comment.isUserComment = false;
144 serialize : function() {
146 for (var p in this) {
152 for (var i in keys) {
153 if (typeof this[keys[i]] == "function") continue;
154 out += " " +keys[i]+" => "+
156 (typeof(this[keys[i]]) != "object") ?
158 "[" +typeof(this[keys[i]])+"]"
162 return "\n{\n" + out + "}\n";
166 var clone = new Symbol();
167 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
168 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
175 //__defineSetter__("name",
176 setName : function(n) {
177 n = n.replace(/^_global_[.#-]/, "");
178 n = n.replace(/\.prototype\.?/g, '#');
179 n = n.replace(/#$/g, '');
181 this.name = n; // real!
184 //__defineGetter__("name",
185 getName : function() { return this._name; },
187 //__defineSetter__("params",
188 setParams :function(v) {
189 for (var i = 0, l = v.length; i < l; i++) {
190 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
191 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
192 this._params[i] = new DocTag(
193 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
196 this._params[i] = v[i];
199 this.params = this._params;
204 //__defineGetter__("params",
205 getParams : function() { return this._params; },
210 /** Object[] */ params,
212 /** DocComment */ comment
214 this.$args = arguments;
215 //println("Symbol created: " + isa + ":" + name);
217 this.alias = this.getName();
218 this.setParams(params);
219 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
220 this.comment = comment || new DocComment("");
221 this.srcFile = Symbol.srcFile;
225 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
229 //if (typeof PluginManager != "undefined") {
230 // PluginManager.run("onSymbol", this);
234 setTags : function() {
236 var authors = this.comment.getTag("author");
237 if (authors.length) {
238 this.author = authors.map(function($){return $.desc;}).join(", ");
242 assert("testing Symbol");
244 requires("../lib/JSDOC/DocComment.js");
245 requires("../frame/String.js");
246 requires("../lib/JSDOC/DocTag.js");
248 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
249 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
252 var mth = this.comment.getTag("method");
254 this.isa = "FUNCTION";
257 var descs = this.comment.getTag("desc");
259 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
263 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
264 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
268 if (this.is("FILE")) {
269 if (!this.alias) this.alias = this.srcFile;
271 var overviews = this.comment.getTag("overview");
272 if (overviews.length) {
273 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
278 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
279 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
283 var sinces = this.comment.getTag("since");
285 this.since = sinces.map(function($){return $.desc;}).join(", ");
289 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
290 assertEqual(sym.since, "1.01", "@since tag, description is found.");
294 if (this.comment.getTag("constant").length) {
295 this.isConstant = true;
300 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
301 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
305 var versions = this.comment.getTag("version");
306 if (versions.length) {
307 this.version = versions.map(function($){return $.desc;}).join(", ");
311 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
312 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
316 var deprecateds = this.comment.getTag("deprecated");
317 if (deprecateds.length) {
318 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
322 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
323 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
327 var examples = this.comment.getTag("example");
328 if (examples.length) {
329 this.example = examples[0];
333 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
334 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
338 var sees = this.comment.getTag("see");
340 var thisSee = this.see;
341 sees.map(function($){thisSee.push($.desc);});
345 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
346 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
350 var classes = this.comment.getTag("class");
351 if (classes.length) {
352 this.isa = "CONSTRUCTOR";
353 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
354 if (!this.classDesc) {
355 this.classDesc = this.desc;
362 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
363 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
364 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
368 var namespaces = this.comment.getTag("namespace");
369 if (namespaces.length) {
370 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
371 this.isNamespace = true;
375 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
376 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
380 var params = this.comment.getTag("param");
382 // user-defined params overwrite those with same name defined by the parser
383 var thisParams = this.getParams();
385 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
386 this.setParams(params);
388 else { // need to overlay these user-defined params on to existing parser-defined params
389 for (var i = 0, l = params.length; i < l; i++) {
391 if (params[i].type) thisParams[i].type = params[i].type;
392 thisParams[i].name = params[i].name;
393 thisParams[i].desc = params[i].desc;
394 thisParams[i].isOptional = params[i].isOptional;
395 thisParams[i].defaultValue = params[i].defaultValue;
397 else thisParams[i] = params[i];
403 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
404 assertEqual(sym.params.length, 1, "parser defined param is found.");
406 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
407 assertEqual(sym.params.length, 1, "user defined param is found.");
408 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
409 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
411 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
412 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
413 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
414 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
416 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
417 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
418 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
419 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
421 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
422 assertEqual(sym.params.length, 1, "user defined param with description is found.");
423 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
427 if (this.comment.getTag("constructor").length) {
428 this.isa = "CONSTRUCTOR";
432 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
433 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
437 if (this.comment.getTag("static").length) {
438 this.isStatic = true;
439 if (this.isa == "CONSTRUCTOR") {
440 this.isNamespace = true;
445 if (this.comment.getTag("singleton").length) {
446 this.isStatic = true;
447 //if (this.isa == "CONSTRUCTOR") {
448 // this.isNamespace = true;
455 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
456 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
457 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
461 if (this.comment.getTag("inner").length) {
463 this.isStatic = false;
467 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
468 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
469 assertEqual(sym.isInner, true, "@inner makes isInner true.");
473 if (this.comment.getTag("field").length) {
478 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
479 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
483 if (this.comment.getTag("function").length) {
484 this.isa = "FUNCTION";
488 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
489 // change a property to a function..
490 this.isa = "FUNCTION";
495 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
496 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
500 var events = this.comment.getTag("event");
502 this.isa = "FUNCTION";
507 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
508 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
509 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
513 var names = this.comment.getTag("name");
515 this.setName(names[0].desc);
523 var properties = this.comment.getTag("property");
524 if (properties.length) {
525 thisProperties = this.properties;
526 for (var i = 0; i < properties.length; i++) {
527 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
528 // TODO: shouldn't the following happen in the addProperty method of Symbol?
529 property.name = properties[i].name;
530 property.memberOf = this.alias;
531 if (properties[i].type) property.type = properties[i].type;
532 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
533 this.addProperty(property);
534 imports.Parser.Parser.addSymbol(property);
539 var conf = this.comment.getTag("cfg");
541 for (var i = 0; i < conf.length; i++) {
542 this.addConfig(conf[i]);
551 var returns = this.comment.getTag("return");
552 if (returns.length) { // there can be many return tags in a single doclet
553 this.returns = returns;
554 this.type = returns.map(function($){return $.type}).join(", ");
562 this.exceptions = this.comment.getTag("throws");
569 var requires = this.comment.getTag("requires");
570 if (requires.length) {
571 this.requires = requires.map(function($){return $.desc});
579 var types = this.comment.getTag("type");
581 this.type = types[0].desc; //multiple type tags are ignored
589 if (this.comment.getTag("private").length || this.isInner) {
590 this.isPrivate = true;
594 if (this.comment.getTag("ignore").length) {
595 this.isIgnored = true;
602 // @inherits ... as ...
603 var inherits = this.comment.getTag("inherits");
604 if (inherits.length) {
605 for (var i = 0; i < inherits.length; i++) {
606 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
607 var inAlias = RegExp.$1;
608 var inAs = RegExp.$2 || inAlias;
610 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
613 inAs = inAs.replace(/\.prototype\.?/g, "#");
614 inAs = inAs.replace(/^this\.?/, "#");
617 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
619 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
622 inAs = this.alias + joiner + inAs;
625 this.inherits.push({alias: inAlias, as: inAs});
634 this.augments = this.comment.getTag("augments");
637 if (this.comment.getTag("extends")) {
638 this.augments = this.comment.getTag("extends");
643 var defaults = this.comment.getTag("default");
644 if (defaults.length) {
645 if (this.is("OBJECT")) {
646 this.defaultValue = defaults[0].desc;
655 var memberOfs = this.comment.getTag("memberOf");
656 if (memberOfs.length) {
657 this.memberOf = memberOfs[0].desc;
658 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
666 if (this.comment.getTag("public").length) {
667 this.isPrivate = false;
675 is : function(what) {
676 return this.isa === what;
679 isBuiltin : function() {
680 return SymbolSet.isBuiltin(this.alias);
683 setType : function(/**String*/comment, /**Boolean*/overwrite) {
684 if (!overwrite && this.type) return;
685 var typeComment = DocComment.unwrapComment(comment);
686 this.type = typeComment;
689 inherit : function(symbol) {
690 if (!this.hasMember(symbol.name) && !symbol.isInner) {
691 if (symbol.is("FUNCTION"))
692 this.methods.push(symbol);
693 else if (symbol.is("OBJECT"))
694 this.properties.push(symbol);
698 hasMember : function(name) {
699 return (this.hasMethod(name) || this.hasProperty(name));
702 addMember : function(symbol) {
703 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
705 if (symbol.comment.getTag("cfg").length == 1) {
706 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
707 this.addConfig(symbol.comment.getTag("cfg")[0]);
711 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
712 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
715 hasMethod : function(name) {
716 var thisMethods = this.methods;
717 for (var i = 0, l = thisMethods.length; i < l; i++) {
718 if (thisMethods[i].name == name) return true;
719 if (thisMethods[i].alias == name) return true;
724 addMethod : function(symbol) {
725 var methodAlias = symbol.alias;
726 var thisMethods = this.methods;
727 for (var i = 0, l = thisMethods.length; i < l; i++) {
728 if (thisMethods[i].alias == methodAlias) {
729 thisMethods[i] = symbol; // overwriting previous method
733 thisMethods.push(symbol); // new method with this alias
736 hasProperty : function(name) {
737 var thisProperties = this.properties;
738 for (var i = 0, l = thisProperties.length; i < l; i++) {
739 if (thisProperties[i].name == name) return true;
740 if (thisProperties[i].alias == name) return true;
745 addProperty : function(symbol) {
746 var propertyAlias = symbol.alias;
747 var thisProperties = this.properties;
748 for (var i = 0, l = thisProperties.length; i < l; i++) {
749 if (thisProperties[i].alias == propertyAlias) {
750 thisProperties[i] = symbol; // overwriting previous property
755 thisProperties.push(symbol); // new property with this alias
758 addDocTag : function(docTag)
760 this.comment.tags.push(docTag);
761 if (docTag.title == 'cfg') {
762 this.addConfig(docTag);
767 addConfig : function(docTag)
769 if (typeof(docTag['memberOf']) == 'undefined') {
770 // remove prototype data...
771 //var a = this.alias.split('#')[0];
772 //docTag.memberOf = a;
773 docTag.memberOf = this.alias;
775 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
776 this.cfgs[docTag.name] = docTag;
780 configToArray: function()
783 for(var ci in this.cfgs) {
784 // dont show hidden!!
785 if (this.cfgs[ci].desc.match(/@hide/)) {
788 r.push(this.cfgs[ci]);
796 * Elements that are not serialized
800 '$args' // not needed AFAIK
803 Symbol.srcFile = ""; //running reference to the current file being parsed
806 Symbol.fromDump = function(t)
808 var ns = new Symbol();
810 if (typeof(ns[i]) == "undefined") {
811 println("ERR:no default for Symbol:"+ i);