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