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