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 + " :SYM: " + symbol + " " + 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;
75 this.addToParentScope(symbol);
76 return this.identifiers[symbol];
78 getIdentifier : function(symbol, token) {
79 if (typeof(this.identifiers[symbol])== 'undefined') {
80 if (['String', 'Date'].indexOf(symbol)> -1) {
84 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
87 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
88 return this.identifiers[symbol];
91 addHint : function(varName, varType) {
92 this.hint[varName] = varType;
94 preventMunging : function() {
100 getUsedSymbols : function() {
103 // if (this.usedsymcache !== false) {
104 // return this.usedsymcache;
107 var idents = this.identifiers;
108 for(var i in idents) {
109 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
110 var identifier = this.identifiers[i];
111 var mungedValue = identifier.mungedValue
112 if (!mungedValue.length) {
113 //println(identifier.toSource());
114 mungedValue = identifier.name;
116 result.push(mungedValue);
118 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
119 //this.usedsymcache = result;
123 getAllUsedSymbols :function() {
124 var result = this.getUsedSymbols();
125 var scope = this.parent;
126 while (scope !== false) {
127 //println("addused:"+scope.id);
128 result = result.concat(scope.getUsedSymbols());
129 scope = scope.parent;
131 //println("Done - addused");
134 /** - we need to register short vairalbes so they never get munged into.. */
135 addToParentScope: function(ident)
137 if (ident.length > 2) {
140 var scope = this.parent;
141 while (scope !== false) {
142 //println("addused:"+scope.id);
144 scope.protectedVars[ident] = true;
146 scope = scope.parent;
150 isProtectedVar: function(ident)
152 if (ident.length > 2) {
155 var scope = this.parent;
156 while (scope !== false) {
157 //println("addused:"+scope.id);
159 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
161 scope = scope.parent;
167 * set's all the munged values on the identifiers.
176 // Stop right here if this scope was flagged as unsafe for munging.
177 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
189 // Do not munge symbols in the global scope!
193 for (var ii in this.identifiers) {
196 //print("MUNGE: " + all.join(', '));
198 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
200 var freeSymbols = [];
201 var sy = this.getAllUsedSymbols();
203 var addSyms=function(batch)
205 for(var i =0;i<batch.length;i++) {
206 if (sy.indexOf(batch[i]) > -1) {
209 freeSymbols.push(batch[i]);
216 //println(freeSymbols.toSource());
218 //println("MUNGE: Replacing " + this.id+"</BR>");
219 for (var i in this.identifiers) {
221 // is the identifer in the global scope!?!!?
224 if (!this.identifiers[i].toMunge) {
225 //print("SKIP toMunge==false : " + i)
229 if (this.isProtectedVar(i)) {
230 //print("SKIP PROTECTED: " + i)
236 //if (this.identifiers[i].constructor != Identifier) {
237 // print("SKIP NOT IDENTIFIER : " + i)
240 // println("IDENT:" +i+'</BR>');
242 if (!repsym.length) {
243 if (!freeSymbols.length) {
246 repsym = freeSymbols.shift(); // pop off beginngin???
249 var identifier = this.identifiers[i];
250 //println(typeof(identifier.name));
251 var mungedValue = identifier.name;
253 //println([ repsym,mungedValue ]);
255 if (this.mungeM && repsym.length < mungedValue.length) {
256 //print("REPLACE:"+ mungedValue +" with " + repsym );
257 mungedValue = repsym;
261 identifier.mungedValue = mungedValue;
263 //println("MUNGE: Done " + this.id+"</BR>");
266 //println("Doing sub scopes");
267 for (var j = 0; j < this.subScopes.length; j++) {
268 var ss = this.subScopes[j];
280 XObject.extend(Scope, {
282 builtin : ["NaN","top"],
283 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
290 if (File.exists('/tmp/var_list_ones.js')) {
291 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
292 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
293 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
296 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(',');
298 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
299 for(var i = 0; i < a.length; i++) {
300 for(var j = 0; j < n.length; j++) {
301 var tw = a[i] + n[j];
302 if (this.skips.indexOf(tw) < 0) {
307 for(var k = 0; k < n.length; k++) {
308 var thr = a[i] + n[j] + n[k];
309 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
310 if (this.skips.indexOf(thr) < 0) {
312 this.threes.push(thr);
319 //println("done creating var list");
320 //println("threes="+ this.threes.toSource());
326 // init the scope constants..