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 print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + symbol);
81 return this.identifiers[symbol];
84 addHint : function(varName, varType) {
85 this.hint[varName] = varType;
87 preventMunging : function() {
93 getUsedSymbols : function() {
96 // if (this.usedsymcache !== false) {
97 // return this.usedsymcache;
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;
109 result.push(mungedValue);
111 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
112 //this.usedsymcache = result;
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;
124 //println("Done - addused");
127 /** - we need to register short vairalbes so they never get munged into.. */
128 addToParentScope: function(ident)
130 if (ident.length > 2) {
133 var scope = this.parent;
134 while (scope !== false) {
135 //println("addused:"+scope.id);
137 scope.protectedVars[ident] = true;
139 scope = scope.parent;
143 isProtectedVar: function(ident)
145 if (ident.length > 2) {
148 var scope = this.parent;
149 while (scope !== false) {
150 //println("addused:"+scope.id);
152 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
154 scope = scope.parent;
160 * set's all the munged values on the identifiers.
169 // Stop right here if this scope was flagged as unsafe for munging.
170 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
182 // Do not munge symbols in the global scope!
186 for (var ii in this.identifiers) {
189 //print("MUNGE: " + all.join(', '));
191 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
193 var freeSymbols = [];
194 var sy = this.getAllUsedSymbols();
196 var addSyms=function(batch)
198 for(var i =0;i<batch.length;i++) {
199 if (sy.indexOf(batch[i]) > -1) {
202 freeSymbols.push(batch[i]);
209 //println(freeSymbols.toSource());
211 //println("MUNGE: Replacing " + this.id+"</BR>");
212 for (var i in this.identifiers) {
214 // is the identifer in the global scope!?!!?
217 if (!this.identifiers[i].toMunge) {
218 //print("SKIP toMunge==false : " + i)
222 if (this.isProtectedVar(i)) {
223 //print("SKIP PROTECTED: " + i)
229 //if (this.identifiers[i].constructor != Identifier) {
230 // print("SKIP NOT IDENTIFIER : " + i)
233 // println("IDENT:" +i+'</BR>');
235 if (!repsym.length) {
236 if (!freeSymbols.length) {
237 addSyms(JSDOC.Scope.twos);
239 repsym = freeSymbols.shift(); // pop off beginngin???
242 var identifier = this.identifiers[i];
243 //println(typeof(identifier.name));
244 var mungedValue = identifier.name;
246 //println([ repsym,mungedValue ]);
248 if (this.mungeM && repsym.length < mungedValue.length) {
249 //print("REPLACE:"+ mungedValue +" with " + repsym );
250 mungedValue = repsym;
254 identifier.mungedValue = mungedValue;
256 //println("MUNGE: Done " + this.id+"</BR>");
259 //println("Doing sub scopes");
260 for (var j = 0; j < this.subScopes.length; j++) {
261 var ss = this.subScopes[j];
273 XObject.extend(Scope, {
275 builtin : ["NaN","top"],
276 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
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'));
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(',');
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) {
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) {
305 this.threes.push(thr);
312 //println("done creating var list");
313 //println("threes="+ this.threes.toSource());
319 // init the scope constants..