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