1 //<Script type="text/javascript">
6 * // FIXME - I need this to do next() without doccomments..
9 Identifier = imports.Identifier.Identifier
10 XObject = imports.XObject.XObject;
13 function Scope(braceN, parent, startTokN, lastIdent, token)
15 if (lastIdent.length) {
16 // println("NEW SCOPE: " + lastIdent);
22 this.identifiers = { };
25 this.ident = lastIdent;
26 this.gid = Scope.gid++;
28 //print("ADD SCOPE(" + this.gid + ") TO "+ (parent ? this.parent.gid : 'TOP') + ' : ' +
29 // (token ? token.toString() : ''));
32 this.parent.subScopes.push(this);
49 identifiers : false, // map of identifiers to {Identifier} objects
55 protectedVars : {}, // only used by to parent..
58 * dump the scope to StdOut...
61 dump : function(indent)
63 indent = indent || '';
65 indent + "Scope: " + this.id + "\n" +
66 indent + "Started: " + ( this.token ? this.token.line : 'TOP' ) + "\n" +
67 indent + "- " + XObject.keys(this.identifiers).join(", ") + "\n"
69 this.subScopes.forEach(function(s) {
77 declareIdentifier : function(symbol, token)
80 //print("SCOPE : " + this.gid + " :SYM: " + symbol + " " + token.toString()+"");
82 if (typeof(this.identifiers[symbol])== 'undefined') {
84 this.identifiers[symbol] = new Identifier(symbol, this);
87 if (typeof(token) != 'undefined') { // shoudl this happen?
88 token.identifier = this.identifiers[symbol];
91 if (this.braceN < 0) {
92 // then it's global...
93 this.identifiers[symbol].toMunge = false;
97 this.addToParentScope(symbol);
98 return this.identifiers[symbol];
100 getIdentifier : function(symbol, token) {
101 if (typeof(this.identifiers[symbol])== 'undefined') {
102 if (['String', 'Date'].indexOf(symbol)> -1) {
106 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
109 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
110 return this.identifiers[symbol];
113 addHint : function(varName, varType) {
114 this.hint[varName] = varType;
116 preventMunging : function() {
120 usedsymcache : false,
122 getUsedSymbols : function() {
125 // if (this.usedsymcache !== false) {
126 // return this.usedsymcache;
129 var idents = this.identifiers;
130 for(var i in idents) {
131 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
132 var identifier = this.identifiers[i];
133 var mungedValue = identifier.mungedValue
134 if (!mungedValue.length) {
135 //println(identifier.toSource());
136 mungedValue = identifier.name;
138 result.push(mungedValue);
140 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
141 //this.usedsymcache = result;
145 getAllUsedSymbols :function() {
146 var result = this.getUsedSymbols();
147 var scope = this.parent;
148 while (scope !== false) {
149 //println("addused:"+scope.id);
150 result = result.concat(scope.getUsedSymbols());
151 scope = scope.parent;
153 //println("Done - addused");
156 /** - we need to register short vairalbes so they never get munged into.. */
157 addToParentScope: function(ident)
159 if (ident.length > 2) {
162 var scope = this.parent;
163 while (scope !== false) {
164 //println("addused:"+scope.id);
166 scope.protectedVars[ident] = true;
168 scope = scope.parent;
172 isProtectedVar: function(ident)
174 if (ident.length > 2) {
177 var scope = this.parent;
178 while (scope !== false) {
179 //println("addused:"+scope.id);
181 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
183 scope = scope.parent;
189 * set's all the munged values on the identifiers.
198 // Stop right here if this scope was flagged as unsafe for munging.
199 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
211 // Do not munge symbols in the global scope!
215 for (var ii in this.identifiers) {
218 //print("MUNGE: " + all.join(', '));
220 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
222 var freeSymbols = [];
223 var sy = this.getAllUsedSymbols();
225 var addSyms=function(batch)
227 for(var i =0;i<batch.length;i++) {
228 if (sy.indexOf(batch[i]) > -1) {
231 freeSymbols.push(batch[i]);
238 //println(freeSymbols.toSource());
240 //println("MUNGE: Replacing " + this.id+"</BR>");
241 for (var i in this.identifiers) {
243 // is the identifer in the global scope!?!!?
246 if (!this.identifiers[i].toMunge) {
247 //print("SKIP toMunge==false : " + i)
251 if (this.isProtectedVar(i)) {
252 //print("SKIP PROTECTED: " + i)
258 //if (this.identifiers[i].constructor != Identifier) {
259 // print("SKIP NOT IDENTIFIER : " + i)
262 // println("IDENT:" +i+'</BR>');
264 if (!repsym.length) {
265 if (!freeSymbols.length) {
268 repsym = freeSymbols.shift(); // pop off beginngin???
271 var identifier = this.identifiers[i];
272 //println(typeof(identifier.name));
273 var mungedValue = identifier.name;
275 //println([ repsym,mungedValue ]);
277 if (this.mungeM && repsym.length < mungedValue.length) {
278 //print("REPLACE:"+ mungedValue +" with " + repsym );
279 mungedValue = repsym;
283 identifier.mungedValue = mungedValue;
285 //println("MUNGE: Done " + this.id+"</BR>");
288 //println("Doing sub scopes");
289 for (var j = 0; j < this.subScopes.length; j++) {
290 var ss = this.subScopes[j];
302 XObject.extend(Scope, {
304 builtin : ["NaN","top"],
305 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
312 if (File.exists('/tmp/var_list_ones.js')) {
313 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
314 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
315 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
318 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(',');
320 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
321 for(var i = 0; i < a.length; i++) {
322 for(var j = 0; j < n.length; j++) {
323 var tw = a[i] + n[j];
324 if (this.skips.indexOf(tw) < 0) {
329 for(var k = 0; k < n.length; k++) {
330 var thr = a[i] + n[j] + n[k];
331 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
332 if (this.skips.indexOf(thr) < 0) {
334 this.threes.push(thr);
341 //println("done creating var list");
342 //println("threes="+ this.threes.toSource());
348 // init the scope constants..