1 //<Script type="text/javascript">
6 * // FIXME - I need this to do next() without doccomments..
9 const Identifier = imports.Identifier.Identifier
10 const XObject = imports.XObject.XObject;
14 function Scope(braceN, parent, startTokN, lastIdent, token)
16 if (lastIdent.length) {
17 // println("NEW SCOPE: " + lastIdent);
23 this.identifiers = { };
26 this.ident = lastIdent;
27 this.gid = Scope.gid++;
29 //print("ADD SCOPE(" + this.gid + ") TO "+ (parent ? this.parent.gid : 'TOP') + ' : ' +
30 // (token ? token.toString() : ''));
33 this.parent.subScopes.push(this);
50 identifiers : false, // map of identifiers to {Identifier} objects
56 protectedVars : {}, // only used by to parent..
59 * dump the scope to StdOut...
62 dump : function(indent)
64 indent = indent || '';
66 for (var k in this.identifiers) {
67 str += str.length ? ", " : "";
68 str += k + '=>' + this.identifiers[k].mungedValue;
72 indent + "Scope: " + this.id + "\n" +
74 indent + "Started: " + ( this.token ? this.token.toString(): 'TOP' ) + "\n" +
75 indent + "- " + str + "\n"
77 this.subScopes.forEach(function(s) {
85 declareIdentifier : function(symbol, token)
88 //print("SCOPE : " + this.gid + " :SYM: " + symbol + " " + token.toString()+"");
90 if (typeof(this.identifiers[symbol])== 'undefined') {
92 this.identifiers[symbol] = new Identifier(symbol, this);
95 if (typeof(token) != 'undefined') { // shoudl this happen?
96 token.identifier = this.identifiers[symbol];
99 if (this.braceN < 0) {
100 // then it's global...
101 this.identifiers[symbol].toMunge = false;
105 this.addToParentScope(symbol);
106 return this.identifiers[symbol];
108 getIdentifier : function(symbol, token) {
109 if (typeof(this.identifiers[symbol])== 'undefined') {
110 if (['String', 'Date'].indexOf(symbol)> -1) {
114 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
117 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
118 return this.identifiers[symbol];
121 addHint : function(varName, varType) {
122 this.hint[varName] = varType;
124 preventMunging : function() {
128 usedsymcache : false,
130 getUsedSymbols : function() {
133 // if (this.usedsymcache !== false) {
134 // return this.usedsymcache;
137 var idents = this.identifiers;
138 for(var i in idents) {
139 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
140 var identifier = this.identifiers[i];
141 var mungedValue = identifier.mungedValue
142 if (!mungedValue.length) {
143 //println(identifier.toSource());
144 mungedValue = identifier.name;
146 result.push(mungedValue);
148 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
149 //this.usedsymcache = result;
153 getAllUsedSymbols :function() {
154 var result = this.getUsedSymbols();
155 var scope = this.parent;
156 while (scope !== false) {
157 //println("addused:"+scope.id);
158 result = result.concat(scope.getUsedSymbols());
159 scope = scope.parent;
161 //println("Done - addused");
164 /** - we need to register short vairalbes so they never get munged into.. */
165 addToParentScope: function(ident)
167 if (ident.length > 2) {
170 var scope = this.parent;
171 while (scope !== false) {
172 //println("addused:"+scope.id);
174 scope.protectedVars[ident] = true;
176 scope = scope.parent;
180 isProtectedVar: function(ident)
182 if (ident.length > 2) {
185 var scope = this.parent;
186 while (scope !== false) {
187 //println("addused:"+scope.id);
189 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
191 scope = scope.parent;
197 * set's all the munged values on the identifiers.
206 // Stop right here if this scope was flagged as unsafe for munging.
207 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
219 // Do not munge symbols in the global scope!
223 for (var ii in this.identifiers) {
226 //print("MUNGE: " + all.join(', '));
228 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
230 var freeSymbols = [];
231 var sy = this.getAllUsedSymbols();
233 var addSyms=function(batch)
235 for(var i =0;i<batch.length;i++) {
236 if (sy.indexOf(batch[i]) > -1) {
239 freeSymbols.push(batch[i]);
246 //println(freeSymbols.toSource());
248 //println("MUNGE: Replacing " + this.id+"</BR>");
249 for (var i in this.identifiers) {
251 // is the identifer in the global scope!?!!?
254 if (!this.identifiers[i].toMunge) {
255 //print("SKIP toMunge==false : " + i)
259 if (this.isProtectedVar(i)) {
260 //print("SKIP PROTECTED: " + i)
266 //if (this.identifiers[i].constructor != Identifier) {
267 // print("SKIP NOT IDENTIFIER : " + i)
270 // println("IDENT:" +i+'</BR>');
272 if (!repsym.length) {
273 if (!freeSymbols.length) {
276 repsym = freeSymbols.shift(); // pop off beginngin???
279 var identifier = this.identifiers[i];
280 //println(typeof(identifier.name));
281 var mungedValue = identifier.name;
283 //println([ repsym,mungedValue ]);
285 if (this.mungeM && repsym.length < mungedValue.length) {
286 //print("REPLACE:"+ mungedValue +" with " + repsym );
287 mungedValue = repsym;
291 identifier.mungedValue = mungedValue;
293 //println("MUNGE: Done " + this.id+"</BR>");
296 //println("Doing sub scopes");
297 for (var j = 0; j < this.subScopes.length; j++) {
298 var ss = this.subScopes[j];
310 XObject.extend(Scope, {
312 builtin : ["NaN","top"],
313 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
320 if (File.exists('/tmp/var_list_ones.js')) {
321 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
322 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
323 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
326 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(',');
328 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
329 for(var i = 0; i < a.length; i++) {
330 for(var j = 0; j < n.length; j++) {
331 var tw = a[i] + n[j];
332 if (this.skips.indexOf(tw) < 0) {
337 for(var k = 0; k < n.length; k++) {
338 var thr = a[i] + n[j] + n[k];
339 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
340 if (this.skips.indexOf(thr) < 0) {
342 this.threes.push(thr);
349 //println("done creating var list");
350 //println("threes="+ this.threes.toSource());
356 // init the scope constants..