1 //<Script type="text/javascript">
6 * // FIXME - I need this to do next() without doccomments..
9 Identifier = imports['JSDOC/Identifier.js'].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;
28 //println("ADD SCOPE(" + this.id + ") TO "+ (parent ? this.parent.id : 'TOP') + "<BR/>");
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 //println("ADD IDENT(" + this.id + "):<B>" + symbol+"</B><BR/>");
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 return (typeof(this.identifiers[symbol])== 'undefined') ? false : this.identifiers[symbol];
79 addHint : function(varName, varType) {
80 this.hint[varName] = varType;
82 preventMunging : function() {
88 getUsedSymbols : function() {
91 // if (this.usedsymcache !== false) {
92 // return this.usedsymcache;
95 var idents = this.identifiers;
96 for(var i in idents) {
97 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
98 var identifier = this.identifiers[i];
99 var mungedValue = identifier.mungedValue
100 if (!mungedValue.length) {
101 //println(identifier.toSource());
102 mungedValue = identifier.name;
104 result.push(mungedValue);
106 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
107 //this.usedsymcache = result;
111 getAllUsedSymbols :function() {
112 var result = this.getUsedSymbols();
113 var scope = this.parent;
114 while (scope !== false) {
115 //println("addused:"+scope.id);
116 result = result.concat(scope.getUsedSymbols());
117 scope = scope.parent;
119 //println("Done - addused");
122 /** - we need to register short vairalbes so they never get munged into.. */
123 addToParentScope: function(ident)
125 if (ident.length > 2) {
128 var scope = this.parent;
129 while (scope !== false) {
130 //println("addused:"+scope.id);
132 scope.protectedVars[ident] = true;
134 scope = scope.parent;
138 isProtectedVar: function(ident)
140 if (ident.length > 2) {
143 var scope = this.parent;
144 while (scope !== false) {
145 //println("addused:"+scope.id);
147 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
149 scope = scope.parent;
155 * set's all the munged values on the identifiers.
164 // Stop right here if this scope was flagged as unsafe for munging.
165 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
173 //println("MUNGE: Scope:" + this.id+"</BR>");
177 // Do not munge symbols in the global scope!
181 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
183 var freeSymbols = [];
184 var sy = this.getAllUsedSymbols();
186 var addSyms=function(batch)
188 for(var i =0;i<batch.length;i++) {
189 if (sy.indexOf(batch[i]) > -1) {
192 freeSymbols.push(batch[i]);
197 addSyms(JSDOC.Scope.ones);
200 //println(freeSymbols.toSource());
202 //println("MUNGE: Replacing " + this.id+"</BR>");
203 for (var i in this.identifiers) {
205 // is the identifer in the global scope!?!!?
208 if (!this.identifiers[i].toMunge) {
212 if (this.isProtectedVar(i)) {
218 if (this.identifiers[i].constructor != Identifier) {
221 // println("IDENT:" +i+'</BR>');
223 if (!repsym.length) {
224 if (!freeSymbols.length) {
225 addSyms(JSDOC.Scope.twos);
227 repsym = freeSymbols.shift(); // pop off beginngin???
230 var identifier = this.identifiers[i];
231 //println(typeof(identifier.name));
232 var mungedValue = identifier.name;
234 //println([ repsym,mungedValue ]);
236 if (this.mungeM && repsym.length < mungedValue.length) {
237 //println("REPLACE:"+ mungedValue +" with " + repsym + "<BR>");
238 mungedValue = repsym;
241 identifier.mungedValue = mungedValue;
243 //println("MUNGE: Done " + this.id+"</BR>");
246 //println("Doing sub scopes");
247 for (var i = 0; i < this.subScopes.length; i++) {
248 var ss = this.subScopes[i];
260 XObject.extend(Scope, {
262 builtin : ["NaN","top"],
263 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
270 if (File.exists('/tmp/var_list_ones.js')) {
271 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
272 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
273 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
276 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(',');
278 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
279 for(var i = 0; i < a.length; i++) {
280 for(var j = 0; j < n.length; j++) {
281 var tw = a[i] + n[j];
282 if (this.skips.indexOf(tw) < 0) {
287 for(var k = 0; k < n.length; k++) {
288 var thr = a[i] + n[j] + n[k];
289 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
290 if (this.skips.indexOf(thr) < 0) {
292 this.threes.push(thr);
299 //println("done creating var list");
300 //println("threes="+ this.threes.toSource());
306 // init the scope constants..