1 //<Script type="text/javascript">
4 Identifier = imports.Identifier.Identifier
5 XObject = imports.XObject.XObject;
12 * // FIXME - I need this to do next() without doccomments..
16 function Scope(braceN, parent, startTokN, lastIdent, token)
18 if (lastIdent.length) {
19 // println("NEW SCOPE: " + lastIdent);
25 this.identifiers = { };
28 this.ident = lastIdent;
29 this.gid = Scope.gid++;
31 //print("ADD SCOPE(" + this.gid + ") TO "+ (parent ? this.parent.gid : 'TOP') + ' : ' +
32 // (token ? token.toString() : ''));
35 this.parent.subScopes.push(this);
52 identifiers : false, // map of identifiers to {Identifier} objects
58 protectedVars : {}, // only used by to parent..
61 * dump the scope to StdOut...
64 dump : function(indent)
66 indent = indent || '';
68 indent + "Scope: " + this.id + "\n" +
69 indent + "Started: " + ( this.token ? this.token.line : 'TOP' ) + "\n" +
70 indent + "- " + XObject.keys(this.identifiers).join(", ") + "\n"
72 this.subScopes.forEach(function(s) {
80 declareIdentifier : function(symbol, token)
83 //print("SCOPE : " + this.gid + " :SYM: " + symbol + " " + token.toString()+"");
85 if (typeof(this.identifiers[symbol])== 'undefined') {
87 this.identifiers[symbol] = new Identifier(symbol, this);
90 if (typeof(token) != 'undefined') { // shoudl this happen?
91 token.identifier = this.identifiers[symbol];
94 if (this.braceN < 0) {
95 // then it's global...
96 this.identifiers[symbol].toMunge = false;
100 this.addToParentScope(symbol);
101 return this.identifiers[symbol];
103 getIdentifier : function(symbol, token) {
104 if (typeof(this.identifiers[symbol])== 'undefined') {
105 if (['String', 'Date'].indexOf(symbol)> -1) {
109 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
112 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
113 return this.identifiers[symbol];
116 addHint : function(varName, varType) {
117 this.hint[varName] = varType;
119 preventMunging : function() {
123 usedsymcache : false,
125 getUsedSymbols : function() {
128 // if (this.usedsymcache !== false) {
129 // return this.usedsymcache;
132 var idents = this.identifiers;
133 for(var i in idents) {
134 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
135 var identifier = this.identifiers[i];
136 var mungedValue = identifier.mungedValue
137 if (!mungedValue.length) {
138 //println(identifier.toSource());
139 mungedValue = identifier.name;
141 result.push(mungedValue);
143 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
144 //this.usedsymcache = result;
148 getAllUsedSymbols :function() {
149 var result = this.getUsedSymbols();
150 var scope = this.parent;
151 while (scope !== false) {
152 //println("addused:"+scope.id);
153 result = result.concat(scope.getUsedSymbols());
154 scope = scope.parent;
156 //println("Done - addused");
159 /** - we need to register short vairalbes so they never get munged into.. */
160 addToParentScope: function(ident)
162 if (ident.length > 2) {
165 var scope = this.parent;
166 while (scope !== false) {
167 //println("addused:"+scope.id);
169 scope.protectedVars[ident] = true;
171 scope = scope.parent;
175 isProtectedVar: function(ident)
177 if (ident.length > 2) {
180 var scope = this.parent;
181 while (scope !== false) {
182 //println("addused:"+scope.id);
184 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
186 scope = scope.parent;
192 * set's all the munged values on the identifiers.
201 // Stop right here if this scope was flagged as unsafe for munging.
202 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
214 // Do not munge symbols in the global scope!
218 for (var ii in this.identifiers) {
221 //print("MUNGE: " + all.join(', '));
223 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
225 var freeSymbols = [];
226 var sy = this.getAllUsedSymbols();
228 var addSyms=function(batch)
230 for(var i =0;i<batch.length;i++) {
231 if (sy.indexOf(batch[i]) > -1) {
234 freeSymbols.push(batch[i]);
241 //println(freeSymbols.toSource());
243 //println("MUNGE: Replacing " + this.id+"</BR>");
244 for (var i in this.identifiers) {
246 // is the identifer in the global scope!?!!?
249 if (!this.identifiers[i].toMunge) {
250 //print("SKIP toMunge==false : " + i)
254 if (this.isProtectedVar(i)) {
255 //print("SKIP PROTECTED: " + i)
261 //if (this.identifiers[i].constructor != Identifier) {
262 // print("SKIP NOT IDENTIFIER : " + i)
265 // println("IDENT:" +i+'</BR>');
267 if (!repsym.length) {
268 if (!freeSymbols.length) {
271 repsym = freeSymbols.shift(); // pop off beginngin???
274 var identifier = this.identifiers[i];
275 //println(typeof(identifier.name));
276 var mungedValue = identifier.name;
278 //println([ repsym,mungedValue ]);
280 if (this.mungeM && repsym.length < mungedValue.length) {
281 //print("REPLACE:"+ mungedValue +" with " + repsym );
282 mungedValue = repsym;
286 identifier.mungedValue = mungedValue;
288 //println("MUNGE: Done " + this.id+"</BR>");
291 //println("Doing sub scopes");
292 for (var j = 0; j < this.subScopes.length; j++) {
293 var ss = this.subScopes[j];
305 XObject.extend(Scope, {
307 builtin : ["NaN","top"],
308 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
315 if (File.exists('/tmp/var_list_ones.js')) {
316 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
317 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
318 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
321 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(',');
323 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
324 for(var i = 0; i < a.length; i++) {
325 for(var j = 0; j < n.length; j++) {
326 var tw = a[i] + n[j];
327 if (this.skips.indexOf(tw) < 0) {
332 for(var k = 0; k < n.length; k++) {
333 var thr = a[i] + n[j] + n[k];
334 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
335 if (this.skips.indexOf(thr) < 0) {
337 this.threes.push(thr);
344 //println("done creating var list");
345 //println("threes="+ this.threes.toSource());
351 // init the scope constants..