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..
56 declareIdentifier : function(symbol, token) {
58 //print("SCOPE : " + this.gid + " : " + token.toString()+"");
60 if (typeof(this.identifiers[symbol])== 'undefined') {
62 this.identifiers[symbol] = new Identifier(symbol, this);
65 if (typeof(token) != 'undefined') { // shoudl this happen?
66 token.identifier = this.identifiers[symbol];
69 if (this.braceN < 0) {
70 // then it's global...
71 this.identifiers[symbol].toMunge = false;
73 this.addToParentScope(symbol);
74 return this.identifiers[symbol];
76 getIdentifier : function(symbol, token) {
77 if (typeof(this.identifiers[symbol])== 'undefined') {
78 if (['String', 'Date'].indexOf(symbol)> -1) {
82 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
85 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
86 return this.identifiers[symbol];
89 addHint : function(varName, varType) {
90 this.hint[varName] = varType;
92 preventMunging : function() {
98 getUsedSymbols : function() {
101 // if (this.usedsymcache !== false) {
102 // return this.usedsymcache;
105 var idents = this.identifiers;
106 for(var i in idents) {
107 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
108 var identifier = this.identifiers[i];
109 var mungedValue = identifier.mungedValue
110 if (!mungedValue.length) {
111 //println(identifier.toSource());
112 mungedValue = identifier.name;
114 result.push(mungedValue);
116 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
117 //this.usedsymcache = result;
121 getAllUsedSymbols :function() {
122 var result = this.getUsedSymbols();
123 var scope = this.parent;
124 while (scope !== false) {
125 //println("addused:"+scope.id);
126 result = result.concat(scope.getUsedSymbols());
127 scope = scope.parent;
129 //println("Done - addused");
132 /** - we need to register short vairalbes so they never get munged into.. */
133 addToParentScope: function(ident)
135 if (ident.length > 2) {
138 var scope = this.parent;
139 while (scope !== false) {
140 //println("addused:"+scope.id);
142 scope.protectedVars[ident] = true;
144 scope = scope.parent;
148 isProtectedVar: function(ident)
150 if (ident.length > 2) {
153 var scope = this.parent;
154 while (scope !== false) {
155 //println("addused:"+scope.id);
157 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
159 scope = scope.parent;
165 * set's all the munged values on the identifiers.
174 // Stop right here if this scope was flagged as unsafe for munging.
175 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
187 // Do not munge symbols in the global scope!
191 for (var ii in this.identifiers) {
194 //print("MUNGE: " + all.join(', '));
196 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
198 var freeSymbols = [];
199 var sy = this.getAllUsedSymbols();
201 var addSyms=function(batch)
203 for(var i =0;i<batch.length;i++) {
204 if (sy.indexOf(batch[i]) > -1) {
207 freeSymbols.push(batch[i]);
214 //println(freeSymbols.toSource());
216 //println("MUNGE: Replacing " + this.id+"</BR>");
217 for (var i in this.identifiers) {
219 // is the identifer in the global scope!?!!?
222 if (!this.identifiers[i].toMunge) {
223 //print("SKIP toMunge==false : " + i)
227 if (this.isProtectedVar(i)) {
228 //print("SKIP PROTECTED: " + i)
234 //if (this.identifiers[i].constructor != Identifier) {
235 // print("SKIP NOT IDENTIFIER : " + i)
238 // println("IDENT:" +i+'</BR>');
240 if (!repsym.length) {
241 if (!freeSymbols.length) {
242 addSyms(JSDOC.Scope.twos);
244 repsym = freeSymbols.shift(); // pop off beginngin???
247 var identifier = this.identifiers[i];
248 //println(typeof(identifier.name));
249 var mungedValue = identifier.name;
251 //println([ repsym,mungedValue ]);
253 if (this.mungeM && repsym.length < mungedValue.length) {
254 //print("REPLACE:"+ mungedValue +" with " + repsym );
255 mungedValue = repsym;
259 identifier.mungedValue = mungedValue;
261 //println("MUNGE: Done " + this.id+"</BR>");
264 //println("Doing sub scopes");
265 for (var j = 0; j < this.subScopes.length; j++) {
266 var ss = this.subScopes[j];
278 XObject.extend(Scope, {
280 builtin : ["NaN","top"],
281 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
288 if (File.exists('/tmp/var_list_ones.js')) {
289 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
290 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
291 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
294 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(',');
296 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
297 for(var i = 0; i < a.length; i++) {
298 for(var j = 0; j < n.length; j++) {
299 var tw = a[i] + n[j];
300 if (this.skips.indexOf(tw) < 0) {
305 for(var k = 0; k < n.length; k++) {
306 var thr = a[i] + n[j] + n[k];
307 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
308 if (this.skips.indexOf(thr) < 0) {
310 this.threes.push(thr);
317 //println("done creating var list");
318 //println("threes="+ this.threes.toSource());
324 // init the scope constants..