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)
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'));
31 this.parent.subScopes.push(this);
48 identifiers : false, // map of identifiers to {Identifier} objects
54 protectedVars : {}, // only used by to parent..
55 declareIdentifier : function(symbol, token) {
57 print("SCOPE : " + this.gid + " : " + token.toString()+"");
59 if (typeof(this.identifiers[symbol])== 'undefined') {
61 this.identifiers[symbol] = new Identifier(symbol, this);
64 if (typeof(token) != 'undefined') { // shoudl this happen?
65 token.identifier = this.identifiers[symbol];
68 if (this.braceN < 0) {
69 // then it's global...
70 this.identifiers[symbol].toMunge = false;
72 this.addToParentScope(symbol);
73 return this.identifiers[symbol];
75 getIdentifier : function(symbol) {
76 if (typeof(this.identifiers[symbol])== 'undefined') {
77 if (['String', 'Date'].indexOf(symbol)> -1) {
81 print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + symbol);
84 print("SCOPE : " + this.gid +" = FOUND:" + symbol);
85 return this.identifiers[symbol];
88 addHint : function(varName, varType) {
89 this.hint[varName] = varType;
91 preventMunging : function() {
97 getUsedSymbols : function() {
100 // if (this.usedsymcache !== false) {
101 // return this.usedsymcache;
104 var idents = this.identifiers;
105 for(var i in idents) {
106 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
107 var identifier = this.identifiers[i];
108 var mungedValue = identifier.mungedValue
109 if (!mungedValue.length) {
110 //println(identifier.toSource());
111 mungedValue = identifier.name;
113 result.push(mungedValue);
115 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
116 //this.usedsymcache = result;
120 getAllUsedSymbols :function() {
121 var result = this.getUsedSymbols();
122 var scope = this.parent;
123 while (scope !== false) {
124 //println("addused:"+scope.id);
125 result = result.concat(scope.getUsedSymbols());
126 scope = scope.parent;
128 //println("Done - addused");
131 /** - we need to register short vairalbes so they never get munged into.. */
132 addToParentScope: function(ident)
134 if (ident.length > 2) {
137 var scope = this.parent;
138 while (scope !== false) {
139 //println("addused:"+scope.id);
141 scope.protectedVars[ident] = true;
143 scope = scope.parent;
147 isProtectedVar: function(ident)
149 if (ident.length > 2) {
152 var scope = this.parent;
153 while (scope !== false) {
154 //println("addused:"+scope.id);
156 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
158 scope = scope.parent;
164 * set's all the munged values on the identifiers.
173 // Stop right here if this scope was flagged as unsafe for munging.
174 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
186 // Do not munge symbols in the global scope!
190 for (var ii in this.identifiers) {
193 //print("MUNGE: " + all.join(', '));
195 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
197 var freeSymbols = [];
198 var sy = this.getAllUsedSymbols();
200 var addSyms=function(batch)
202 for(var i =0;i<batch.length;i++) {
203 if (sy.indexOf(batch[i]) > -1) {
206 freeSymbols.push(batch[i]);
213 //println(freeSymbols.toSource());
215 //println("MUNGE: Replacing " + this.id+"</BR>");
216 for (var i in this.identifiers) {
218 // is the identifer in the global scope!?!!?
221 if (!this.identifiers[i].toMunge) {
222 //print("SKIP toMunge==false : " + i)
226 if (this.isProtectedVar(i)) {
227 //print("SKIP PROTECTED: " + i)
233 //if (this.identifiers[i].constructor != Identifier) {
234 // print("SKIP NOT IDENTIFIER : " + i)
237 // println("IDENT:" +i+'</BR>');
239 if (!repsym.length) {
240 if (!freeSymbols.length) {
241 addSyms(JSDOC.Scope.twos);
243 repsym = freeSymbols.shift(); // pop off beginngin???
246 var identifier = this.identifiers[i];
247 //println(typeof(identifier.name));
248 var mungedValue = identifier.name;
250 //println([ repsym,mungedValue ]);
252 if (this.mungeM && repsym.length < mungedValue.length) {
253 //print("REPLACE:"+ mungedValue +" with " + repsym );
254 mungedValue = repsym;
258 identifier.mungedValue = mungedValue;
260 //println("MUNGE: Done " + this.id+"</BR>");
263 //println("Doing sub scopes");
264 for (var j = 0; j < this.subScopes.length; j++) {
265 var ss = this.subScopes[j];
277 XObject.extend(Scope, {
279 builtin : ["NaN","top"],
280 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
287 if (File.exists('/tmp/var_list_ones.js')) {
288 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
289 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
290 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
293 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(',');
295 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
296 for(var i = 0; i < a.length; i++) {
297 for(var j = 0; j < n.length; j++) {
298 var tw = a[i] + n[j];
299 if (this.skips.indexOf(tw) < 0) {
304 for(var k = 0; k < n.length; k++) {
305 var thr = a[i] + n[j] + n[k];
306 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
307 if (this.skips.indexOf(thr) < 0) {
309 this.threes.push(thr);
316 //println("done creating var list");
317 //println("threes="+ this.threes.toSource());
323 // init the scope constants..