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.
32 comment : { isUserComment: false },
40 isa : "OBJECT", // OBJECT//FUNCTION
69 function array2object(a , m)
73 a.forEach(function(e) {
74 r.push(m ? e : e.toObject());
79 switch (typeof(this[i])) {
84 print("object? :" + i);
88 ret[i] = this[i]; continue;
90 print("unknown type:" + typeof(this[i]));
101 this.defaultValue = "";
109 this.comment = { isUserComment: false };
110 //this.defaultValue = null;
111 this.deprecated = "";
115 this.exceptions = [];
117 this.inheritsFrom = [];
118 this.isa = "OBJECT"; // OBJECT//FUNCTION
119 this.isEvent = false;
120 this.isConstant = false;
121 this.isIgnored = false;
122 this.isInner = false;
123 this.isNamespace = false;
124 this.isPrivate = false;
125 this.isStatic = false;
130 this.properties = [];
138 this.childClasses = [];
143 serialize : function() {
145 for (var p in this) {
151 for (var i in keys) {
152 if (typeof this[keys[i]] == "function") continue;
153 out += " " +keys[i]+" => "+
155 (typeof(this[keys[i]]) != "object") ?
157 "[" +typeof(this[keys[i]])+"]"
161 return "\n{\n" + out + "}\n";
165 var clone = new Symbol();
166 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
167 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
174 //__defineSetter__("name",
175 setName : function(n) {
176 n = n.replace(/^_global_[.#-]/, "");
177 n = n.replace(/\.prototype\.?/g, '#');
178 n = n.replace(/#$/g, '');
180 this.name = n; // real!
183 //__defineGetter__("name",
184 getName : function() { return this._name; },
186 //__defineSetter__("params",
187 setParams :function(v) {
188 for (var i = 0, l = v.length; i < l; i++) {
189 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
190 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
191 this._params[i] = new DocTag(
192 "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
195 this._params[i] = v[i];
198 this.params = this._params;
203 //__defineGetter__("params",
204 getParams : function() { return this._params; },
209 /** Object[] */ params,
211 /** DocComment */ comment
213 this.$args = arguments;
214 //println("Symbol created: " + isa + ":" + name);
216 this.alias = this.getName();
217 this.setParams(params);
218 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
219 this.comment = comment || new DocComment("");
220 this.srcFile = Symbol.srcFile;
224 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
228 //if (typeof PluginManager != "undefined") {
229 // PluginManager.run("onSymbol", this);
233 setTags : function() {
235 var authors = this.comment.getTag("author");
236 if (authors.length) {
237 this.author = authors.map(function($){return $.desc;}).join(", ");
241 assert("testing Symbol");
243 requires("../lib/JSDOC/DocComment.js");
244 requires("../frame/String.js");
245 requires("../lib/JSDOC/DocTag.js");
247 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
248 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
251 var mth = this.comment.getTag("method");
253 this.isa = "FUNCTION";
256 var descs = this.comment.getTag("desc");
258 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
262 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
263 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
267 if (this.is("FILE")) {
268 if (!this.alias) this.alias = this.srcFile;
270 var overviews = this.comment.getTag("overview");
271 if (overviews.length) {
272 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
277 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
278 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
282 var sinces = this.comment.getTag("since");
284 this.since = sinces.map(function($){return $.desc;}).join(", ");
288 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
289 assertEqual(sym.since, "1.01", "@since tag, description is found.");
293 if (this.comment.getTag("constant").length) {
294 this.isConstant = true;
299 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
300 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
304 var versions = this.comment.getTag("version");
305 if (versions.length) {
306 this.version = versions.map(function($){return $.desc;}).join(", ");
310 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
311 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
315 var deprecateds = this.comment.getTag("deprecated");
316 if (deprecateds.length) {
317 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
321 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
322 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
326 var examples = this.comment.getTag("example");
327 if (examples.length) {
328 this.example = examples[0];
332 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
333 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
337 var sees = this.comment.getTag("see");
339 var thisSee = this.see;
340 sees.map(function($){thisSee.push($.desc);});
344 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
345 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
349 var classes = this.comment.getTag("class");
350 if (classes.length) {
351 this.isa = "CONSTRUCTOR";
352 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
353 if (!this.classDesc) {
354 this.classDesc = this.desc;
361 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
362 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
363 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
367 var namespaces = this.comment.getTag("namespace");
368 if (namespaces.length) {
369 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
370 this.isNamespace = true;
374 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
375 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
379 var params = this.comment.getTag("param");
381 // user-defined params overwrite those with same name defined by the parser
382 var thisParams = this.getParams();
384 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
385 this.setParams(params);
387 else { // need to overlay these user-defined params on to existing parser-defined params
388 for (var i = 0, l = params.length; i < l; i++) {
390 if (params[i].type) thisParams[i].type = params[i].type;
391 thisParams[i].name = params[i].name;
392 thisParams[i].desc = params[i].desc;
393 thisParams[i].isOptional = params[i].isOptional;
394 thisParams[i].defaultValue = params[i].defaultValue;
396 else thisParams[i] = params[i];
402 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
403 assertEqual(sym.params.length, 1, "parser defined param is found.");
405 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
406 assertEqual(sym.params.length, 1, "user defined param is found.");
407 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
408 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
410 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
411 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
412 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
413 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
415 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
416 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
417 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
418 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
420 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
421 assertEqual(sym.params.length, 1, "user defined param with description is found.");
422 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
426 if (this.comment.getTag("constructor").length) {
427 this.isa = "CONSTRUCTOR";
431 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
432 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
436 if (this.comment.getTag("static").length) {
437 this.isStatic = true;
438 if (this.isa == "CONSTRUCTOR") {
439 this.isNamespace = true;
444 if (this.comment.getTag("singleton").length) {
445 this.isStatic = true;
446 //if (this.isa == "CONSTRUCTOR") {
447 // this.isNamespace = true;
454 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
455 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
456 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
460 if (this.comment.getTag("inner").length) {
462 this.isStatic = false;
466 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
467 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
468 assertEqual(sym.isInner, true, "@inner makes isInner true.");
472 if (this.comment.getTag("field").length) {
477 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
478 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
482 if (this.comment.getTag("function").length) {
483 this.isa = "FUNCTION";
487 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
488 // change a property to a function..
489 this.isa = "FUNCTION";
494 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
495 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
499 var events = this.comment.getTag("event");
501 this.isa = "FUNCTION";
506 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
507 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
508 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
512 var names = this.comment.getTag("name");
514 this.setName(names[0].desc);
522 var properties = this.comment.getTag("property");
523 if (properties.length) {
524 thisProperties = this.properties;
525 for (var i = 0; i < properties.length; i++) {
526 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
527 // TODO: shouldn't the following happen in the addProperty method of Symbol?
528 property.name = properties[i].name;
529 property.memberOf = this.alias;
530 if (properties[i].type) property.type = properties[i].type;
531 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
532 this.addProperty(property);
533 imports.Parser.Parser.addSymbol(property);
538 var conf = this.comment.getTag("cfg");
540 for (var i = 0; i < conf.length; i++) {
541 this.addConfig(conf[i]);
550 var returns = this.comment.getTag("return");
551 if (returns.length) { // there can be many return tags in a single doclet
552 this.returns = returns;
553 this.type = returns.map(function($){return $.type}).join(", ");
561 this.exceptions = this.comment.getTag("throws");
568 var requires = this.comment.getTag("requires");
569 if (requires.length) {
570 this.requires = requires.map(function($){return $.desc});
578 var types = this.comment.getTag("type");
580 this.type = types[0].desc; //multiple type tags are ignored
588 if (this.comment.getTag("private").length || this.isInner) {
589 this.isPrivate = true;
593 if (this.comment.getTag("ignore").length) {
594 this.isIgnored = true;
601 // @inherits ... as ...
602 var inherits = this.comment.getTag("inherits");
603 if (inherits.length) {
604 for (var i = 0; i < inherits.length; i++) {
605 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
606 var inAlias = RegExp.$1;
607 var inAs = RegExp.$2 || inAlias;
609 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
612 inAs = inAs.replace(/\.prototype\.?/g, "#");
613 inAs = inAs.replace(/^this\.?/, "#");
616 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
618 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
621 inAs = this.alias + joiner + inAs;
624 this.inherits.push({alias: inAlias, as: inAs});
633 this.augments = this.comment.getTag("augments");
636 if (this.comment.getTag("extends")) {
637 this.augments = this.comment.getTag("extends");
642 var defaults = this.comment.getTag("default");
643 if (defaults.length) {
644 if (this.is("OBJECT")) {
645 this.defaultValue = defaults[0].desc;
654 var memberOfs = this.comment.getTag("memberOf");
655 if (memberOfs.length) {
656 this.memberOf = memberOfs[0].desc;
657 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
665 if (this.comment.getTag("public").length) {
666 this.isPrivate = false;
674 is : function(what) {
675 return this.isa === what;
678 isBuiltin : function() {
679 return SymbolSet.isBuiltin(this.alias);
682 setType : function(/**String*/comment, /**Boolean*/overwrite) {
683 if (!overwrite && this.type) return;
684 var typeComment = DocComment.unwrapComment(comment);
685 this.type = typeComment;
688 inherit : function(symbol) {
689 if (!this.hasMember(symbol.name) && !symbol.isInner) {
690 if (symbol.is("FUNCTION"))
691 this.methods.push(symbol);
692 else if (symbol.is("OBJECT"))
693 this.properties.push(symbol);
697 hasMember : function(name) {
698 return (this.hasMethod(name) || this.hasProperty(name));
701 addMember : function(symbol) {
702 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
704 if (symbol.comment.getTag("cfg").length == 1) {
705 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
706 this.addConfig(symbol.comment.getTag("cfg")[0]);
710 if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
711 else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
714 hasMethod : function(name) {
715 var thisMethods = this.methods;
716 for (var i = 0, l = thisMethods.length; i < l; i++) {
717 if (thisMethods[i].name == name) return true;
718 if (thisMethods[i].alias == name) return true;
723 addMethod : function(symbol) {
724 var methodAlias = symbol.alias;
725 var thisMethods = this.methods;
726 for (var i = 0, l = thisMethods.length; i < l; i++) {
727 if (thisMethods[i].alias == methodAlias) {
728 thisMethods[i] = symbol; // overwriting previous method
732 thisMethods.push(symbol); // new method with this alias
735 hasProperty : function(name) {
736 var thisProperties = this.properties;
737 for (var i = 0, l = thisProperties.length; i < l; i++) {
738 if (thisProperties[i].name == name) return true;
739 if (thisProperties[i].alias == name) return true;
744 addProperty : function(symbol) {
745 var propertyAlias = symbol.alias;
746 var thisProperties = this.properties;
747 for (var i = 0, l = thisProperties.length; i < l; i++) {
748 if (thisProperties[i].alias == propertyAlias) {
749 thisProperties[i] = symbol; // overwriting previous property
754 thisProperties.push(symbol); // new property with this alias
757 addDocTag : function(docTag)
759 this.comment.tags.push(docTag);
760 if (docTag.title == 'cfg') {
761 this.addConfig(docTag);
766 addConfig : function(docTag)
768 if (typeof(docTag['memberOf']) == 'undefined') {
769 // remove prototype data...
770 //var a = this.alias.split('#')[0];
771 //docTag.memberOf = a;
772 docTag.memberOf = this.alias;
774 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
775 this.cfgs[docTag.name] = docTag;
779 configToArray: function()
782 for(var ci in this.cfgs) {
783 // dont show hidden!!
784 if (this.cfgs[ci].desc.match(/@hide/)) {
787 r.push(this.cfgs[ci]);
798 Symbol.srcFile = ""; //running reference to the current file being parsed
801 Symbol.fromDump = function(t)
803 var ns = new Symbol();
805 if (typeof(ns[i]) == "undefined") {
806 println("ERR:no default for Symbol:"+ i);