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