JSDOC/Scope.js
[gnome.introspection-doc-generator] / JSDOC / Scope.js
1 //<Script type="text/javascript">
2
3 /**
4 * Scope stuff
5
6 * // FIXME - I need this to do next() without doccomments..
7 */
8
9 const Identifier = imports.Identifier.Identifier
10 const XObject = imports.XObject.XObject; 
11
12 Scope.id = 1;
13
14 function Scope(braceN, parent, startTokN, lastIdent, token)
15 {
16     if (lastIdent.length) {
17        //  println("NEW SCOPE: " + lastIdent);
18     }
19     
20     this.braceN = braceN
21     this.parent = parent;
22     this.id = Scope.id++;
23     this.identifiers = { };
24     this.subScopes = [];
25     this.hints = { };
26     this.ident = lastIdent;
27     this.gid = Scope.gid++;
28     this.token = token;
29     //print("ADD SCOPE(" + this.gid + ") TO "+ (parent ? this.parent.gid : 'TOP') + ' : ' + 
30     //    (token ? token.toString() : ''));
31     
32     if (parent) {
33         this.parent.subScopes.push(this);
34     } 
35     
36 }
37
38
39
40
41
42
43
44 Scope.prototype = {
45     
46     id : 0,
47     braceN : -1,
48     parent : false,
49     subScopes : false,
50     identifiers : false,  // map of identifiers to {Identifier} objects
51     hints: false, 
52     mungeM : true, 
53     ident: '',
54     
55     munged : false,
56     protectedVars : {}, // only used by to parent..
57     
58     /**
59      * dump the scope to StdOut...
60      * 
61      */
62     dump : function(indent) 
63     {
64         indent = indent || '';
65         var str = '';
66         for (var k in this.identifiers) {
67             str += str.length ? ", " : "";
68             str += k + '=>' + this.identifiers[k].mungedValue;
69         }
70         
71         print(
72             indent + "Scope: " + this.id + "\n" +
73             indent + "Started: " + ( this.token ? this.token.line  : 'TOP' ) + "\n" +
74             indent + "- " + str + "\n"
75         );
76         this.subScopes.forEach(function(s) {
77             s.dump(indent + ' ');
78         });
79         
80         
81     },
82     
83     
84     declareIdentifier : function(symbol, token) 
85     {
86         
87         //print("SCOPE : " + this.gid +  " :SYM: " + symbol + " " + token.toString()+"");
88         
89         if (typeof(this.identifiers[symbol])== 'undefined') {
90             
91             this.identifiers[symbol] =  new Identifier(symbol, this);
92             
93         }
94         if (typeof(token) != 'undefined') { // shoudl this happen?
95             token.identifier = this.identifiers[symbol];
96             
97         }
98         if (this.braceN < 0) {
99                 // then it's global... 
100                 this.identifiers[symbol].toMunge  = false;
101         }
102          
103         
104         this.addToParentScope(symbol);
105         return this.identifiers[symbol];
106     },
107     getIdentifier : function(symbol, token) {
108         if (typeof(this.identifiers[symbol])== 'undefined') {
109             if (['String', 'Date'].indexOf(symbol)> -1) {
110                 return false;
111             }
112             
113             //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
114             return false;
115         }
116          //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
117         return this.identifiers[symbol];
118     },
119     
120     addHint : function(varName, varType) {
121         this.hint[varName] = varType;
122     },
123     preventMunging : function() {
124         this.mungeM = false;
125     },
126
127     usedsymcache : false,
128     
129     getUsedSymbols : function() {
130         
131         var result = [];
132        // if (this.usedsymcache !== false) {
133         //    return this.usedsymcache;
134         //}
135         
136         var idents = this.identifiers;
137         for(var i in idents) { 
138             //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
139             var identifier = this.identifiers[i];
140             var mungedValue = identifier.mungedValue
141             if (!mungedValue.length) {
142                 //println(identifier.toSource());
143                 mungedValue = identifier.name;
144             }
145             result.push(mungedValue);
146         }
147         //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
148         //this.usedsymcache = result;
149         return result;
150     },
151
152     getAllUsedSymbols :function() {
153         var result = this.getUsedSymbols();
154         var scope = this.parent;
155         while (scope !== false) {
156             //println("addused:"+scope.id);
157             result = result.concat(scope.getUsedSymbols());
158             scope = scope.parent;
159         }
160          //println("Done - addused");
161         return result;
162     },
163     /** - we need to register short vairalbes so they never get munged into.. */
164     addToParentScope: function(ident) 
165     {
166         if (ident.length > 2) {
167             return;
168         }
169         var scope = this.parent;
170         while (scope !== false) {
171             //println("addused:"+scope.id);
172             if (!scope.parent) {
173                 scope.protectedVars[ident] = true;
174             }
175             scope = scope.parent;
176         }
177         
178     },
179     isProtectedVar: function(ident)
180     {
181         if (ident.length > 2) {
182             return false;
183         }
184         var scope = this.parent;
185         while (scope !== false) {
186             //println("addused:"+scope.id);
187             if (!scope.parent) {
188                 if (typeof(scope.protectedVars[ident])  != 'undefined') return true;
189             }
190             scope = scope.parent;
191         }
192         return false;
193     },
194     
195     /**
196      * set's all the munged values on the identifiers.
197      * 
198      * 
199      */
200
201     munge :function() 
202     {
203
204         if (!this.mungeM) {
205             // Stop right here if this scope was flagged as unsafe for munging.
206            // println("MUNGE: SKIP -  Scope" + this.id+"</BR>");
207             return;
208         }
209         if (this.munged) {
210             return;
211         }
212         
213
214         
215         
216         var pickFromSet = 1;
217
218         // Do not munge symbols in the global scope!
219         if (this.parent) {
220             
221             var all = [];
222             for (var ii in this.identifiers) {
223                 all.push(ii);
224             }
225             //print("MUNGE: " + all.join(', '));
226             
227             //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
228             
229             var freeSymbols = [];
230             var sy = this.getAllUsedSymbols();
231             
232             var addSyms=function(batch)
233             {
234                 for(var i =0;i<batch.length;i++) {
235                     if (sy.indexOf(batch[i]) > -1) {
236                         continue;
237                     }
238                     freeSymbols.push(batch[i]);
239                 }
240             }
241              
242             addSyms(Scope.ones); 
243              
244             var repsym = '';
245             //println(freeSymbols.toSource());
246             
247             //println("MUNGE: Replacing " + this.id+"</BR>");
248             for (var i in  this.identifiers) {
249                 
250                 // is the identifer in the global scope!?!!?
251                 
252                 
253                 if (!this.identifiers[i].toMunge) {
254                     //print("SKIP toMunge==false : " + i)
255                     continue;
256                 }
257                 
258                 if (this.isProtectedVar(i)) {
259                     //print("SKIP PROTECTED: " + i)
260                     continue; // 
261                 }
262                 
263                 
264                 
265                 //if (this.identifiers[i].constructor !=  Identifier) {
266                 //    print("SKIP NOT IDENTIFIER : " + i)
267                 //    continue;
268                // }
269                // println("IDENT:" +i+'</BR>');
270                 
271                 if (!repsym.length) {
272                     if (!freeSymbols.length) {
273                         addSyms(Scope.twos); 
274                     }
275                     repsym = freeSymbols.shift(); // pop off beginngin???
276                 }
277                 
278                 var identifier = this.identifiers[i]; 
279                 //println(typeof(identifier.name));
280                 var mungedValue = identifier.name; 
281                 
282                 //println([     repsym,mungedValue ]);
283                 
284                 if (this.mungeM && repsym.length < mungedValue.length) {
285                     //print("REPLACE:"+ mungedValue +" with " + repsym );    
286                     mungedValue = repsym;
287                     repsym = '';
288                 }
289                 
290                 identifier.mungedValue =  mungedValue;
291             }
292             //println("MUNGE: Done " + this.id+"</BR>");
293         }
294         this.munged = true;
295         //println("Doing sub scopes");
296         for (var j = 0; j < this.subScopes.length; j++) {
297             var ss = this.subScopes[j];
298             ss.munge();
299         }
300     }
301  
302
303 };
304
305
306
307
308
309 XObject.extend(Scope, {
310     
311     builtin : ["NaN","top"],
312     skips : [  'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
313      
314     ones : [],
315     twos : [],
316     threes : [],
317     init : function () {
318         /* cache it later?
319         if (File.exists('/tmp/var_list_ones.js')) {
320             eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
321             eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
322             eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
323         }
324         */
325         this.ones = 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(',');
326         var a = this.ones;
327         var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
328         for(var i = 0; i < a.length; i++) {
329             for(var j = 0; j < n.length; j++) {
330                 var tw = a[i] + n[j];
331                 if (this.skips.indexOf(tw) < 0) {
332                     this.twos.push(tw);
333                 }
334                     
335                 /*
336                 for(var k = 0; k < n.length; k++) {
337                     var thr = a[i] + n[j] + n[k];
338                     //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
339                     if (this.skips.indexOf(thr)  < 0) {
340                         //println("+"+thr);
341                         this.threes.push(thr);
342                        }
343                     
344                 }
345                 */
346             }
347         }
348         //println("done creating var list");
349         //println("threes="+ this.threes.toSource());
350         //throw "DONE";
351         
352        
353     }
354 })
355 // init the scope constants..
356 Scope.init();
357 Scope.gid = 0;