JSDOC/Symbol.js
[gnome.introspection-doc-generator] / JSDOC / Symbol.js
1 //<script type="text/javascript">
2
3 XObject         = imports.XObject.XObject;
4
5 SymbolSet       = imports.SymbolSet.SymbolSet;
6 //Parser          = imports.Parser.Parser;
7 DocComment      = imports.DocComment.DocComment;
8 DocTag          = imports.DocTag.DocTag;
9 /**
10         Create a new Symbol.
11         @class Represents a symbol in the source code.
12  */
13 Symbol = XObject.define(
14     function() {
15         this.init();
16         if (arguments.length) this.populate.apply(this, arguments);
17         
18     },
19     Object,
20     {
21         
22             
23         
24         toObject : function()
25         {
26             var ret = { };
27             for (var i in this) {
28                 switch (typeof(this[i])) {
29                     case 'function':
30                         continue;
31                     case 'object':
32                         print("object? :" + i);
33                     case 'string':
34                     case 'number':
35                         ret[i] = this[i]; continue;
36                     default:
37                         print("unknown type:" + typeof(this[i]));
38                         Seed.quit();
39                    }
40             }
41             
42             
43         },
44         
45         init : function() 
46         {
47             this.name = "";
48             this.defaultValue = "";
49             this.params = [];
50             this.$args = [];
51             this.addOn = "";
52             this.alias = "";
53             this.augments = [];
54             this.author = "";
55             this.classDesc = "";
56             this.comment = { isUserComment: false };
57             //this.defaultValue = null;
58             this.deprecated = "";
59             this.desc = "";
60             this.events = [];
61             this.example = "";
62             this.exceptions = [];
63             this.inherits = [];
64             this.inheritsFrom = [];
65             this.isa = "OBJECT"; // OBJECT//FUNCTION
66             this.isEvent = false;
67             this.isConstant = false;
68             this.isIgnored = false;
69             this.isInner = false;
70             this.isNamespace = false;
71             this.isPrivate = false;
72             this.isStatic = false;
73             this.memberOf = "";
74             this.methods = [];
75             this._name = "";
76             this._params = [];
77             this.properties = [];
78             this.requires = [];
79             this.returns = [];
80             this.see = [];
81             this.since = "";
82             this.srcFile = {};
83             this.type = "";
84             this.version = "";
85             this.childClasses = [];
86             this.cfgs = {};
87                
88         },
89
90         serialize : function() {
91             var keys = [];
92             for (var p in this) {
93                 keys.push (p);
94             }
95             keys = keys.sort();
96             
97             var out = "";
98             for (var i in keys) {
99                 if (typeof this[keys[i]] == "function") continue;
100                 out += "     " +keys[i]+" => "+
101                     (   
102                         (typeof(this[keys[i]]) != "object") ?  
103                             this[keys[i]] :
104                             "[" +typeof(this[keys[i]])+"]"
105                     ) + 
106                     ",\n";
107             }
108             return "\n{\n" + out + "}\n";
109         },
110
111         clone : function() {
112             var clone = new Symbol();
113             clone.populate.apply(clone, this.$args); // repopulate using the original arguments
114             clone.srcFile = this.srcFile; // not the current srcFile, the one when the original was made
115             return clone;
116         },
117
118
119
120
121         //__defineSetter__("name",
122         setName  : function(n) { 
123                 n = n.replace(/^_global_[.#-]/, ""); 
124                 n = n.replace(/\.prototype\.?/g, '#'); 
125                  n = n.replace(/#$/g, ''); 
126                 this._name = n;
127                 this.name = n; // real!
128             },
129         //);
130         //__defineGetter__("name",
131         getName : function() { return this._name; },
132         //);
133         //__defineSetter__("params", 
134         setParams  :function(v) {
135                 for (var i = 0, l = v.length; i < l; i++) {
136                     if (v[i].constructor != DocTag) { // may be a generic object parsed from signature, like {type:..., name:...}
137                         var ty = v[i].hasOwnProperty('type') ? v[i].type : '';
138                         this._params[i] = new DocTag(
139                             "param"+((ty)?" {"+ty+"}":"")+" "+v[i].name);
140                     }
141                     else {
142                         this._params[i] = v[i];
143                     }
144                 }
145                 this.params = this._params;
146             },
147         //);
148
149
150         //__defineGetter__("params",
151         getParams : function() { return this._params; },
152         //);
153
154         populate : function(
155                 /** String */ name,
156                 /** Object[] */ params,
157                 /** String */ isa,
158                 /** DocComment */ comment
159         ) {
160             this.$args = arguments;
161             //println("Symbol created: " + isa + ":" + name);
162             this.setName(name);
163             this.alias = this.getName();
164             this.setParams(params);
165             this.isa = (isa == "VIRTUAL")? "OBJECT":isa;
166             this.comment = comment || new DocComment("");
167             this.srcFile = Symbol.srcFile;
168             
169            
170             
171             if (this.is("FILE") && !this.alias) this.alias = this.srcFile;
172
173             this.setTags();
174             
175             //if (typeof PluginManager != "undefined") {
176             //    PluginManager.run("onSymbol", this);
177             //}
178         },
179
180         setTags : function() {
181             // @author
182             var authors = this.comment.getTag("author");
183             if (authors.length) {
184                 this.author = authors.map(function($){return $.desc;}).join(", ");
185             }
186             
187             /*~t
188                 assert("testing Symbol");
189                 
190                 requires("../lib/JSDOC/DocComment.js");
191                 requires("../frame/String.js");
192                 requires("../lib/JSDOC/DocTag.js");
193
194                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@author Joe Smith*"+"/"));
195                 assertEqual(sym.author, "Joe Smith", "@author tag, author is found.");
196             */
197             // @desc
198             var mth = this.comment.getTag("method");
199             if (mth.length) {
200                 this.isa = "FUNCTION";
201             }
202             // @desc
203             var descs = this.comment.getTag("desc");
204             if (descs.length) {
205                 this.desc = descs.map(function($){return $.desc;}).join("\n"); // multiple descriptions are concatenated into one
206             }
207             
208             /*~t
209                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@desc This is a description.*"+"/"));
210                 assertEqual(sym.desc, "This is a description.", "@desc tag, description is found.");
211             */
212             
213             // @overview
214             if (this.is("FILE")) {
215                 if (!this.alias) this.alias = this.srcFile;
216                 
217                 var overviews = this.comment.getTag("overview");
218                 if (overviews.length) {
219                     this.desc = [this.desc].concat(overviews.map(function($){return $.desc;})).join("\n");
220                 }
221             }
222             
223             /*~t
224                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@overview This is an overview.*"+"/"));
225                 assertEqual(sym.desc, "\nThis is an overview.", "@overview tag, description is found.");
226             */
227             
228             // @since
229             var sinces = this.comment.getTag("since");
230             if (sinces.length) {
231                 this.since = sinces.map(function($){return $.desc;}).join(", ");
232             }
233             
234             /*~t
235                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@since 1.01*"+"/"));
236                 assertEqual(sym.since, "1.01", "@since tag, description is found.");
237             */
238             
239             // @constant
240             if (this.comment.getTag("constant").length) {
241                 this.isConstant = true;
242                 this.isa = 'OBJECT';
243             }
244             
245             /*~t
246                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@constant*"+"/"));
247                 assertEqual(sym.isConstant, true, "@constant tag, isConstant set.");
248             */
249             
250             // @version
251             var versions = this.comment.getTag("version");
252             if (versions.length) {
253                 this.version = versions.map(function($){return $.desc;}).join(", ");
254             }
255             
256             /*~t
257                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@version 2.0x*"+"/"));
258                 assertEqual(sym.version, "2.0x", "@version tag, version is found.");
259             */
260             
261             // @deprecated
262             var deprecateds = this.comment.getTag("deprecated");
263             if (deprecateds.length) {
264                 this.deprecated = deprecateds.map(function($){return $.desc;}).join("\n");
265             }
266             
267             /*~t
268                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@deprecated Use other method.*"+"/"));
269                 assertEqual(sym.deprecated, "Use other method.", "@deprecated tag, desc is found.");
270             */
271             
272             // @example
273             var examples = this.comment.getTag("example");
274             if (examples.length) {
275                 this.example = examples[0];
276             }
277             
278             /*~t
279                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@example This\n  is an example.*"+"/"));
280                 assertEqual(sym.example, "This\n  is an example.", "@deprecated tag, desc is found.");
281             */
282             
283             // @see
284             var sees = this.comment.getTag("see");
285             if (sees.length) {
286                 var thisSee = this.see;
287                 sees.map(function($){thisSee.push($.desc);});
288             }
289             
290             /*~t
291                 var sym = new Symbol("foo", [], "FILE", new DocComment("/**@see The other thing.*"+"/"));
292                 assertEqual(sym.see, "The other thing.", "@see tag, desc is found.");
293             */
294             
295             // @class
296             var classes = this.comment.getTag("class");
297             if (classes.length) {
298                 this.isa = "CONSTRUCTOR";
299                 this.classDesc = classes[0].desc; // desc can't apply to the constructor as there is none.
300                 if (!this.classDesc) {
301                     this.classDesc = this.desc;
302                    }
303                 
304                 
305             }
306             
307             /*~t
308                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@class This describes the class.*"+"/"));
309                 assertEqual(sym.isa, "CONSTRUCTOR", "@class tag, makes symbol a constructor.");
310                 assertEqual(sym.classDesc, "This describes the class.", "@class tag, class description is found.");
311             */
312             
313             // @namespace
314             var namespaces = this.comment.getTag("namespace");
315             if (namespaces.length) {
316                 this.classDesc = namespaces[0].desc+"\n"+this.desc; // desc can't apply to the constructor as there is none.
317                 this.isNamespace = true;
318             }
319             
320             /*~t
321                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@namespace This describes the namespace.*"+"/"));
322                 assertEqual(sym.classDesc, "This describes the namespace.\n", "@namespace tag, class description is found.");
323             */
324             
325             // @param
326             var params = this.comment.getTag("param");
327             if (params.length) {
328                 // user-defined params overwrite those with same name defined by the parser
329                 var thisParams = this.getParams();
330
331                 if (thisParams.length == 0) { // none exist yet, so just bung all these user-defined params straight in
332                     this.setParams(params);
333                 }
334                 else { // need to overlay these user-defined params on to existing parser-defined params
335                     for (var i = 0, l = params.length; i < l; i++) {
336                         if (thisParams[i]) {
337                             if (params[i].type) thisParams[i].type = params[i].type;
338                             thisParams[i].name = params[i].name;
339                             thisParams[i].desc = params[i].desc;
340                             thisParams[i].isOptional = params[i].isOptional;
341                             thisParams[i].defaultValue = params[i].defaultValue;
342                         }
343                         else thisParams[i] = params[i];
344                     }
345                 }
346             }
347             
348             /*~t
349                 var sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.*"+"/"));
350                 assertEqual(sym.params.length, 1, "parser defined param is found.");
351                 
352                 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages*"+"/"));
353                 assertEqual(sym.params.length, 1, "user defined param is found.");
354                 assertEqual(sym.params[0].type, "array", "user defined param type is found.");
355                 assertEqual(sym.params[0].name, "pages", "user defined param name is found.");
356                 
357                 sym = new Symbol("foo", [{type: "array", name: "pages"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
358                 assertEqual(sym.params.length, 1, "user defined param overwrites parser defined param.");
359                 assertEqual(sym.params[0].type, "string", "user defined param type overwrites parser defined param type.");
360                 assertEqual(sym.params[0].name, "uid", "user defined param name overwrites parser defined param name.");
361             
362                 sym = new Symbol("foo", [{type: "array", name: "pages"}, {type: "number", name: "count"}], "FUNCTION", new DocComment("/**Description.\n@param {string} uid*"+"/"));
363                 assertEqual(sym.params.length, 2, "user defined params  overlay parser defined params.");
364                 assertEqual(sym.params[1].type, "number", "user defined param type overlays parser defined param type.");
365                 assertEqual(sym.params[1].name, "count", "user defined param name overlays parser defined param name.");
366
367                 sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**Description.\n@param {array} pages The pages description.*"+"/"));
368                 assertEqual(sym.params.length, 1, "user defined param with description is found.");
369                 assertEqual(sym.params[0].desc, "The pages description.", "user defined param description is found.");
370             */
371             
372             // @constructor
373             if (this.comment.getTag("constructor").length) {
374                 this.isa = "CONSTRUCTOR";
375             }
376             
377             /*~t
378                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@constructor*"+"/"));
379                 assertEqual(sym.isa, "CONSTRUCTOR", "@constructor tag, makes symbol a constructor.");
380             */
381             
382             // @static
383             if (this.comment.getTag("static").length) {
384                 this.isStatic = true;
385                 if (this.isa == "CONSTRUCTOR") {
386                     this.isNamespace = true;
387                 }
388             }
389             
390                 // @static
391             if (this.comment.getTag("singleton").length) {
392                 this.isStatic = true;
393                 //if (this.isa == "CONSTRUCTOR") {
394                 //      this.isNamespace = true;
395                 //}
396             }
397             
398             
399             
400             /*~t
401                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@static\n@constructor*"+"/"));
402                 assertEqual(sym.isStatic, true, "@static tag, makes isStatic true.");
403                 assertEqual(sym.isNamespace, true, "@static and @constructor tag, makes isNamespace true.");
404             */
405             
406             // @inner
407             if (this.comment.getTag("inner").length) {
408                 this.isInner = true;
409                 this.isStatic = false;
410             }
411             
412             /*~t
413                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@inner*"+"/"));
414                 assertEqual(sym.isStatic, false, "@inner tag, makes isStatic false.");
415                 assertEqual(sym.isInner, true, "@inner makes isInner true.");
416             */
417             
418             // @field
419             if (this.comment.getTag("field").length) {
420                 this.isa = "OBJECT";
421             }
422             
423             /*~t
424                 var sym = new Symbol("foo", [], "FUNCTION", new DocComment("/**@field*"+"/"));
425                 assertEqual(sym.isa, "OBJECT", "@field tag, makes symbol an object.");
426             */
427             
428             // @function
429             if (this.comment.getTag("function").length) {
430                 this.isa = "FUNCTION";
431             }
432             
433             // @param
434             if (this.comment.getTag("param").length && this.isa == "OBJECT" ) {
435                 // change a property to a function..
436                 this.isa = "FUNCTION";
437             }
438             
439             
440             /*~t
441                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@function*"+"/"));
442                 assertEqual(sym.isa, "FUNCTION", "@function tag, makes symbol a function.");
443             */
444             
445             // @event
446             var events = this.comment.getTag("event");
447             if (events.length) {
448                 this.isa = "FUNCTION";
449                 this.isEvent = true;
450             }
451             
452             /*~t
453                 var sym = new Symbol("foo", [], "OBJECT", new DocComment("/**@event*"+"/"));
454                 assertEqual(sym.isa, "FUNCTION", "@event tag, makes symbol a function.");
455                 assertEqual(sym.isEvent, true, "@event makes isEvent true.");
456             */
457             
458             // @name
459             var names = this.comment.getTag("name");
460             if (names.length) {
461                 this.setName(names[0].desc);
462             }
463             
464             /*~t
465                 // todo
466             */
467             
468             // @property
469             var properties = this.comment.getTag("property");
470             if (properties.length) {
471                 thisProperties = this.properties;
472                 for (var i = 0; i < properties.length; i++) {
473                     var property = new Symbol(this.alias+"#"+properties[i].name, [], "OBJECT", new DocComment("/**"+properties[i].desc+"\n@name "+properties[i].name+"\n@memberOf "+this.alias+"#*/"));
474                     // TODO: shouldn't the following happen in the addProperty method of Symbol?
475                     property.name = properties[i].name;
476                     property.memberOf = this.alias;
477                     if (properties[i].type) property.type = properties[i].type;
478                     if (properties[i].defaultValue) property.defaultValue = properties[i].defaultValue;
479                     this.addProperty(property);
480                     imports.Parser.Parser.addSymbol(property);
481                 }
482             }
483             
484             // config..
485             var conf = this.comment.getTag("cfg");
486             if (conf.length) {
487                 for (var i = 0; i < conf.length; i++) {
488                     this.addConfig(conf[i]);
489                 }
490             }
491             
492             /*~t
493                 // todo
494             */
495
496             // @return
497             var returns = this.comment.getTag("return");
498             if (returns.length) { // there can be many return tags in a single doclet
499                 this.returns = returns;
500                 this.type = returns.map(function($){return $.type}).join(", ");
501             }
502             
503             /*~t
504                 // todo
505             */
506             
507             // @exception
508             this.exceptions = this.comment.getTag("throws");
509             
510             /*~t
511                 // todo
512             */
513             
514             // @requires
515             var requires = this.comment.getTag("requires");
516             if (requires.length) {
517                 this.requires = requires.map(function($){return $.desc});
518             }
519             
520             /*~t
521                 // todo
522             */
523             
524             // @type
525             var types = this.comment.getTag("type");
526             if (types.length) {
527                 this.type = types[0].desc; //multiple type tags are ignored
528             }
529             
530             /*~t
531                 // todo
532             */
533             
534             // @private
535             if (this.comment.getTag("private").length || this.isInner) {
536                 this.isPrivate = true;
537             }
538             
539             // @ignore
540             if (this.comment.getTag("ignore").length) {
541                 this.isIgnored = true;
542             }
543             
544             /*~t
545                 // todo
546             */
547             
548             // @inherits ... as ...
549             var inherits = this.comment.getTag("inherits");
550             if (inherits.length) {
551                 for (var i = 0; i < inherits.length; i++) {
552                     if (/^\s*([a-z$0-9_.#-]+)(?:\s+as\s+([a-z$0-9_.#]+))?/i.test(inherits[i].desc)) {
553                         var inAlias = RegExp.$1;
554                         var inAs = RegExp.$2 || inAlias;
555
556                         if (inAlias) inAlias = inAlias.replace(/\.prototype\.?/g, "#");
557                         
558                         if (inAs) {
559                             inAs = inAs.replace(/\.prototype\.?/g, "#");
560                             inAs = inAs.replace(/^this\.?/, "#");
561                         }
562
563                         if (inAs.indexOf(inAlias) != 0) { //not a full namepath
564                             var joiner = ".";
565                             if (this.alias.charAt(this.alias.length-1) == "#" || inAs.charAt(0) == "#") {
566                                 joiner = "";
567                             }
568                             inAs = this.alias + joiner + inAs;
569                         }
570                     }
571                     this.inherits.push({alias: inAlias, as: inAs});
572                 }
573             }
574             
575             /*~t
576                 // todo
577             */
578
579             // @augments
580             this.augments = this.comment.getTag("augments");
581             
582             //@extends - Ext
583             if (this.comment.getTag("extends")) {   
584                 this.augments = this.comment.getTag("extends");
585             }
586             
587             
588             // @default
589             var defaults = this.comment.getTag("default");
590             if (defaults.length) {
591                 if (this.is("OBJECT")) {
592                     this.defaultValue = defaults[0].desc;
593                 }
594             }
595             
596             /*~t
597                 // todo
598             */
599             
600             // @memberOf
601             var memberOfs = this.comment.getTag("memberOf");
602             if (memberOfs.length) {
603                 this.memberOf = memberOfs[0].desc;
604                 this.memberOf = this.memberOf.replace(/\.prototype\.?/g, "#");
605             }
606
607             /*~t
608                 // todo
609             */
610             
611             // @public
612             if (this.comment.getTag("public").length) {
613                 this.isPrivate = false;
614             }
615             
616             /*~t
617                 // todo
618             */
619         },
620
621         is : function(what) {
622             return this.isa === what;
623         },
624
625         isBuiltin : function() {
626             return SymbolSet.isBuiltin(this.alias);
627         },
628
629         setType : function(/**String*/comment, /**Boolean*/overwrite) {
630             if (!overwrite && this.type) return;
631             var typeComment = DocComment.unwrapComment(comment);
632             this.type = typeComment;
633         },
634
635         inherit : function(symbol) {
636             if (!this.hasMember(symbol.name) && !symbol.isInner) {
637                 if (symbol.is("FUNCTION"))
638                     this.methods.push(symbol);
639                 else if (symbol.is("OBJECT"))
640                     this.properties.push(symbol);
641             }
642         },
643
644         hasMember : function(name) {
645             return (this.hasMethod(name) || this.hasProperty(name));
646         },
647
648         addMember : function(symbol) {
649             //println("ADDMEMBER: " + this.name +  " ++ " + symbol.name);
650             
651             if (symbol.comment.getTag("cfg").length == 1) { 
652                 symbol.comment.getTag("cfg")[0].memberOf = this.alias;
653                 this.addConfig(symbol.comment.getTag("cfg")[0]);
654                 return;
655             }
656             
657             if (symbol.is("FUNCTION")) { this.addMethod(symbol); }
658             else if (symbol.is("OBJECT")) { this.addProperty(symbol); }
659         },
660
661         hasMethod : function(name) {
662             var thisMethods = this.methods;
663             for (var i = 0, l = thisMethods.length; i < l; i++) {
664                 if (thisMethods[i].name == name) return true;
665                 if (thisMethods[i].alias == name) return true;
666             }
667             return false;
668         },
669
670         addMethod : function(symbol) {
671             var methodAlias = symbol.alias;
672             var thisMethods = this.methods;
673             for (var i = 0, l = thisMethods.length; i < l; i++) {
674                 if (thisMethods[i].alias == methodAlias) {
675                     thisMethods[i] = symbol; // overwriting previous method
676                     return;
677                 }
678             }
679             thisMethods.push(symbol); // new method with this alias
680         },
681
682         hasProperty : function(name) {
683             var thisProperties = this.properties;
684             for (var i = 0, l = thisProperties.length; i < l; i++) {
685                 if (thisProperties[i].name == name) return true;
686                 if (thisProperties[i].alias == name) return true;
687             }
688             return false;
689         },
690
691         addProperty : function(symbol) {
692             var propertyAlias = symbol.alias;
693             var thisProperties = this.properties;
694             for (var i = 0, l = thisProperties.length; i < l; i++) {
695                 if (thisProperties[i].alias == propertyAlias) {
696                     thisProperties[i] = symbol; // overwriting previous property
697                     return;
698                 }
699             }
700
701             thisProperties.push(symbol); // new property with this alias
702         },
703         
704         addDocTag : function(docTag)
705         {
706             this.comment.tags.push(docTag);
707             if (docTag.title == 'cfg') {
708                 this.addConfig(docTag);
709             }
710             
711         },
712         
713         addConfig : function(docTag)
714         {
715             if (typeof(docTag['memberOf']) == 'undefined') {
716                 // remove prototype data...
717                 //var a = this.alias.split('#')[0];
718                 //docTag.memberOf = a;
719                 docTag.memberOf = this.alias;
720             }
721             if (typeof(this.cfgs[docTag.name]) == 'undefined') {
722                 this.cfgs[docTag.name] = docTag;
723             }
724             
725         },
726         configToArray: function()
727         {
728             var r = [];
729             for(var ci in this.cfgs) {
730                 // dont show hidden!!
731                 if (this.cfgs[ci].desc.match(/@hide/)) {
732                     continue;
733                 }
734                 r.push(this.cfgs[ci]); 
735                
736             }
737             return r;
738         }
739 });
740
741
742
743
744
745 Symbol.srcFile = ""; //running reference to the current file being parsed
746
747
748 Symbol.fromDump = function(t)
749 {
750     var ns = new Symbol();
751     for (var i in t) {
752         if (typeof(ns[i]) == "undefined") {
753             println("ERR:no default for Symbol:"+ i);
754         }
755         ns[i] = t[i];
756     }
757     return ns;
758 }