1 //<script type="text/javascript">
3 XObject = imports.XObject.XObject;
5 SymbolSet = imports.SymbolSet.SymbolSet;
7 DocComment = imports.DocComment.DocComment;
8 DocTag = imports.DocTag.DocTag;
10 * Create a new Symbol.
11 * @class Represents a symbol in the source code.
14 Symbol = XObject.define(
17 if (arguments.length) this.populate.apply(this, arguments);
27 $args : [], // original arguments used when constructing.
31 augments : [], // Doctag[]
39 exceptions : [], // Doctag[]
40 inherits : [], // Doctag[]
42 isa : "OBJECT", // OBJECT//FUNCTION
51 methods : [], // Symbol[]
53 _params : [], //Doctag[]
54 properties : [], //Doctag[]
55 requires : [], //Doctag[]
56 returns : [], //Doctag[]
72 var ret = { '*object' : 'Symbol' };
74 if (Symbol.hide.indexOf(i) > -1) {
77 switch (typeof(this[i])) {
93 case 'cfgs': // key val of doctags..
106 print("object? :" + i);
114 ret[i] = this[i]; continue;
116 print("unknown type:" + typeof(this[i]));
126 // only initialize arrays / objects..
131 this.exceptions = [];
134 this.isa = "OBJECT"; // OBJECT//FUNCTION
137 this.properties = [];
146 this.inheritsFrom = [];
147 this.childClasses = [];
149 this.comment = new DocComment();
150 this.comment.isUserComment = false;
155 serialize : function() {
157 for (var p in this) {
163 for (var i in keys) {
164 if (typeof this[keys[i]] == "function") continue;
165 out += " " +keys[i]+" => "+
167 (typeof(this[keys[i]]) != "object") ?
169 "[" +typeof(this[keys[i]])+"]"
173 return "\n{\n" + out + "}\n";
177 var clone = new Symbol();
178 clone.populate.apply(clone, this.$args); // repopulate using the original arguments
179 clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
186 //__defineSetter__("name",
187 setName : function(in_n) {
188 n = in_n.replace(/^_global_[.#-]/, "");
189 n = n.replace(/\.prototype\.?/g, '#');
190 n = n.replace(/\#prototype\.?/g, '#');
191 n = n.replace(/#$/g, '');
193 this.name = n; // real!
194 print("Symbol: setName(" + in_n + ') => ' + this.name );
197 //__defineGetter__("name",
198 getName : function() {
202 //__defineSetter__("params",
203 setParams :function(v) {
204 for (var i = 0, l = v.length; i < l; i++) {
205 if (typeof(v[i]) == 'string') {
206 this._params[i] = new DocTag( "param "+ v[i] );
209 if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
210 var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
211 this._params[i] = new DocTag(
212 "param"+ ( (ty) ? " {"+ty+"}":"")+" " + v[i].name);
215 // default is doctag..
216 this._params[i] = v[i];
219 this.params = this._params;
224 //__defineGetter__("params",
225 getParams : function() { return this._params; },
230 /** Object[] */ params,
232 /** DocComment */ comment
234 this.$args = arguments;
235 //println("Symbol created: " + isa + ":" + name);
237 this.namespace = Symbol.namespace;
238 this.alias = this.getName();
239 this.setParams(params);
240 this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
241 this.comment = comment || new DocComment("");
242 this.srcFile = Symbol.srcFile;
246 if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
250 //if (typeof PluginManager != "undefined") {
251 // PluginManager.run("onSymbol", this);
255 setTags : function() {
257 var authors = this.comment.getTag("author");
258 if (authors.length) {
259 this.author = authors.map(function($){return $.desc;}).join(", ");
263 assert("testing Symbol");
265 requires("../lib/JSDOC/DocComment.js");
266 requires("../frame/String.js");
267 requires("../lib/JSDOC/DocTag.js");
269 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
270 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
273 var mth = this.comment.getTag("method");
275 this.isa = "FUNCTION";
278 var descs = this.comment.getTag("desc");
280 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
284 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
285 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
289 if (this.is("FILE")) {
290 if (!this.alias) this.alias = this.srcFile;
292 var overviews = this.comment.getTag("overview");
293 if (overviews.length) {
294 this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
299 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
300 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
304 var sinces = this.comment.getTag("since");
306 this.since = sinces.map(function($){return $.desc;}).join(", ");
310 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
311 assertEqual(sym.since, "1.01", "@since tag, description is found.");
315 if (this.comment.getTag("constant").length) {
316 this.isConstant = true;
321 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
322 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
326 var versions = this.comment.getTag("version");
327 if (versions.length) {
328 this.version = versions.map(function($){return $.desc;}).join(", ");
332 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
333 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
337 var deprecateds = this.comment.getTag("deprecated");
338 if (deprecateds.length) {
339 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
343 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
344 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
348 var examples = this.comment.getTag("example");
349 if (examples.length) {
350 this.example = examples[0];
354 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n is an example.*"+"/"));
355 assertEqual(sym.example, "This\n is an example.", "@deprecated tag, desc is found.");
359 var sees = this.comment.getTag("see");
361 var thisSee = this.see;
362 sees.map(function($){thisSee.push($.desc);});
366 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
367 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
371 var classes = this.comment.getTag("class");
372 if (classes.length) {
373 this.isa = "CONSTRUCTOR";
374 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
375 if (!this.classDesc) {
376 this.classDesc = this.desc;
383 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
384 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
385 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
389 var namespaces = this.comment.getTag("namespace");
390 if (namespaces.length) {
391 // scope for this file...
392 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
393 this.isNamespace = true;
397 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
398 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
402 var params = this.comment.getTag("param");
404 // user-defined params overwrite those with same name defined by the parser
405 var thisParams = this.getParams();
407 if (thisParams.length == 0) {
408 // none exist yet, so just bung all these user-defined params
410 this.setParams(params);
412 // need to overlay these user-defined params on to
413 // existing parser-defined params
414 for (var i = 0, l = params.length; i < l; i++) {
416 if (params[i].type) {
417 thisParams[i].type = params[i].type;
419 thisParams[i].name = params[i].name;
420 thisParams[i].desc = params[i].desc;
421 thisParams[i].isOptional = params[i].isOptional;
422 thisParams[i].defaultValue = params[i].defaultValue;
426 thisParams[i] = params[i];
433 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
434 assertEqual(sym.params.length, 1, "parser defined param is found.");
436 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
437 assertEqual(sym.params.length, 1, "user defined param is found.");
438 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
439 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
441 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
442 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
443 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
444 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
446 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
447 assertEqual(sym.params.length, 2, "user defined params overlay parser defined params.");
448 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
449 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
451 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
452 assertEqual(sym.params.length, 1, "user defined param with description is found.");
453 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
457 if (this.comment.getTag("constructor").length) {
458 this.isa = "CONSTRUCTOR";
462 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
463 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
467 if (this.comment.getTag("static").length) {
468 this.isStatic = true;
469 // replace '#' with '.'
471 this.name = this.name.split('#').join('.');
472 this._name = this.name
473 this.alias = this.name;
475 if (this.isa == "CONSTRUCTOR") {
476 this.isNamespace = true;
481 if (this.comment.getTag("singleton").length) {
482 this.isStatic = true;
483 if (this.isa == "CONSTRUCTOR") {
484 this.isNamespace = true;
491 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
492 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
493 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
497 if (this.comment.getTag("inner").length) {
499 this.isStatic = false;
503 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
504 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
505 assertEqual(sym.isInner, true, "@inner makes isInner true.");
509 if (this.comment.getTag("field").length) {
514 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
515 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
519 if (this.comment.getTag("function").length) {
520 this.isa = "FUNCTION";
524 if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
525 // change a property to a function..
526 this.isa = "FUNCTION";
531 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
532 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
536 var events = this.comment.getTag("event");
538 this.isa = "FUNCTION";
543 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
544 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
545 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
549 var names = this.comment.getTag("name");
551 this.setName(names[0].desc);
559 var properties = this.comment.getTag("property");
560 if (properties.length) {
561 thisProperties = this.properties;
562 for (var i = 0; i < properties.length; i++) {
563 var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
564 // TODO: shouldn't the following happen in the addProperty method of Symbol?
565 property.name = properties[i].name;
566 property.memberOf = this.alias;
567 if (properties[i].type) property.type = properties[i].type;
568 if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
569 this.addProperty(property);
570 //Symbol.parser.addSymbol(property);
571 imports.ScopeNamer.ScopeNamer.addSymbol(property);
576 var conf = this.comment.getTag("cfg");
578 for (var i = 0; i < conf.length; i++) {
579 this.addConfig(conf[i]);
588 var returns = this.comment.getTag("return");
589 if (returns.length) { // there can be many return tags in a single doclet
590 this.returns = returns;
591 this.type = returns.map(function($){return $.type}).join(", ");
599 this.exceptions = this.comment.getTag("throws");
606 var requires = this.comment.getTag("requires");
607 if (requires.length) {
608 this.requires = requires.map(function($){return $.desc});
616 var types = this.comment.getTag("type");
618 this.type = types[0].type; //multiple type tags are ignored
619 if (types[0].desc.length && !this.desc.length) {
620 this.desc = types[0].desc;
629 if (this.comment.getTag("private").length || this.isInner) {
630 this.isPrivate = true;
634 if (this.comment.getTag("ignore").length) {
635 this.isIgnored = true;
642 // @inherits ... as ...
643 var inherits = this.comment.getTag("inherits");
644 if (inherits.length) {
645 for (var i = 0; i < inherits.length; i++) {
646 if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
647 var inAlias = RegExp.$1;
648 var inAs = RegExp.$2 || inAlias;
650 if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
653 inAs = inAs.replace(/\.prototype\.?/g, "#");
654 inAs = inAs.replace(/^this\.?/, "#");
657 if (inAs.indexOf(inAlias) != 0) { //not a full namepath
659 if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
662 inAs = this.alias + joiner + inAs;
665 this.inherits.push({alias: inAlias, as: inAs});
674 this.augments = this.comment.getTag("augments");
677 if (this.comment.getTag("extends")) {
678 this.augments = this.comment.getTag("extends");
683 var defaults = this.comment.getTag("default");
684 if (defaults.length) {
685 if (this.is("OBJECT")) {
686 this.defaultValue = defaults[0].desc;
695 var memberOfs = this.comment.getTag("memberOf");
696 if (memberOfs.length) {
697 this.memberOf = memberOfs[0].desc;
698 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
699 this.name = this.name.split('.').pop();
700 this.name = this.name.split('#').pop();
701 this.name = this.memberOf + this.name;
702 this._name = this.name
703 this.alias = this.name;
711 if (this.comment.getTag("public").length) {
712 this.isPrivate = false;
720 is : function(what) {
721 return this.isa === what;
724 isBuiltin : function() {
725 return SymbolSet.isBuiltin(this.alias);
728 setType : function(/**String*/comment, /**Boolean*/overwrite) {
729 if (!overwrite && this.type) return;
730 var typeComment = DocComment.unwrapComment(comment);
731 this.type = typeComment;
734 inherit : function(symbol) {
735 if (!this.hasMember(symbol.name) && !symbol.isInner) {
736 if (symbol.is("FUNCTION"))
737 this.methods.push(symbol);
738 else if (symbol.is("OBJECT"))
739 this.properties.push(symbol);
743 hasMember : function(name) {
744 return (this.hasMethod(name) || this.hasProperty(name));
747 addMember : function(symbol) {
748 //println("ADDMEMBER: " + this.name + " ++ " + symbol.name);
749 if (this.is("FUNCTION")) {
752 if (symbol.comment.getTag("cfg").length == 1) {
753 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
754 this.addConfig(symbol.comment.getTag("cfg")[0]);
758 if (symbol.is("FUNCTION")) {
759 this.addMethod(symbol);
762 if (symbol.is("OBJECT")) {
763 this.addProperty(symbol);
769 hasMethod : function(name) {
770 var thisMethods = this.methods;
771 for (var i = 0, l = thisMethods.length; i < l; i++) {
772 if (thisMethods[i].name == name) return true;
773 if (thisMethods[i].alias == name) return true;
778 addMethod : function(symbol) {
779 var methodAlias = symbol.alias;
780 var thisMethods = this.methods;
781 for (var i = 0, l = thisMethods.length; i < l; i++) {
782 if (thisMethods[i].alias == methodAlias) {
783 thisMethods[i] = symbol; // overwriting previous method
787 thisMethods.push(symbol); // new method with this alias
790 hasProperty : function(name) {
791 var thisProperties = this.properties;
792 for (var i = 0, l = thisProperties.length; i < l; i++) {
793 if (thisProperties[i].name == name) return true;
794 if (thisProperties[i].alias == name) return true;
799 addProperty : function(symbol) {
800 var propertyAlias = symbol.alias;
801 var thisProperties = this.properties;
802 for (var i = 0, l = thisProperties.length; i < l; i++) {
803 if (thisProperties[i].alias == propertyAlias) {
804 thisProperties[i] = symbol; // overwriting previous property
809 thisProperties.push(symbol); // new property with this alias
812 addDocTag : function(docTag)
814 this.comment.tags.push(docTag);
815 if (docTag.title == 'cfg') {
816 this.addConfig(docTag);
821 addConfig : function(docTag)
823 if (typeof(docTag['memberOf']) == 'undefined') {
824 // remove prototype data...
825 //var a = this.alias.split('#')[0];
826 //docTag.memberOf = a;
827 docTag.memberOf = this.alias;
829 if (typeof(this.cfgs[docTag.name]) == 'undefined') {
830 this.cfgs[docTag.name] = docTag;
834 configToArray: function()
837 for(var ci in this.cfgs) {
838 // dont show hidden!!
839 if (this.cfgs[ci].desc.match(/@hide/)) {
842 r.push(this.cfgs[ci]);
850 * Elements that are not serialized
854 '$args' // not needed AFAIK
857 Symbol.srcFile = ""; //running reference to the current file being parsed
858 Symbol.namespace = ""; //running reference to the current file being parsed
861 Symbol.fromDump = function(t)
863 var ns = new Symbol();
865 if (typeof(ns[i]) == "undefined") {
866 println("ERR:no default for Symbol:"+ i);
872 Symbol.parser = false; // set up by parser..