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 indent + "Scope: " + this.id + "\n" +
67 indent + "Started: " + ( this.token ? this.token.line : 'TOP' ) + "\n" +
68 indent + "- " + XObject.keys(this.identifiers).join(", ") + "\n"
70 this.subScopes.forEach(function(s) {
78 declareIdentifier : function(symbol, token)
81 //print("SCOPE : " + this.gid + " :SYM: " + symbol + " " + token.toString()+"");
83 if (typeof(this.identifiers[symbol])== 'undefined') {
85 this.identifiers[symbol] = new Identifier(symbol, this);
88 if (typeof(token) != 'undefined') { // shoudl this happen?
89 token.identifier = this.identifiers[symbol];
92 if (this.braceN < 0) {
93 // then it's global...
94 this.identifiers[symbol].toMunge = false;
98 this.addToParentScope(symbol);
99 return this.identifiers[symbol];
101 getIdentifier : function(symbol, token) {
102 if (typeof(this.identifiers[symbol])== 'undefined') {
103 if (['String', 'Date'].indexOf(symbol)> -1) {
107 //print("SCOPE : " + this.gid +" = SYMBOL NOT FOUND?" + token.toString());
110 //print("SCOPE : " + this.gid +" = FOUND:" + token.toString());
111 return this.identifiers[symbol];
114 addHint : function(varName, varType) {
115 this.hint[varName] = varType;
117 preventMunging : function() {
121 usedsymcache : false,
123 getUsedSymbols : function() {
126 // if (this.usedsymcache !== false) {
127 // return this.usedsymcache;
130 var idents = this.identifiers;
131 for(var i in idents) {
132 //println('<b>'+i+'</b>='+typeof(idents[i]) +'<br/>');
133 var identifier = this.identifiers[i];
134 var mungedValue = identifier.mungedValue
135 if (!mungedValue.length) {
136 //println(identifier.toSource());
137 mungedValue = identifier.name;
139 result.push(mungedValue);
141 //println("Symbols for ("+ this.id +"): <B>" + result.join(',') + "</B><BR/>");
142 //this.usedsymcache = result;
146 getAllUsedSymbols :function() {
147 var result = this.getUsedSymbols();
148 var scope = this.parent;
149 while (scope !== false) {
150 //println("addused:"+scope.id);
151 result = result.concat(scope.getUsedSymbols());
152 scope = scope.parent;
154 //println("Done - addused");
157 /** - we need to register short vairalbes so they never get munged into.. */
158 addToParentScope: function(ident)
160 if (ident.length > 2) {
163 var scope = this.parent;
164 while (scope !== false) {
165 //println("addused:"+scope.id);
167 scope.protectedVars[ident] = true;
169 scope = scope.parent;
173 isProtectedVar: function(ident)
175 if (ident.length > 2) {
178 var scope = this.parent;
179 while (scope !== false) {
180 //println("addused:"+scope.id);
182 if (typeof(scope.protectedVars[ident]) != 'undefined') return true;
184 scope = scope.parent;
190 * set's all the munged values on the identifiers.
199 // Stop right here if this scope was flagged as unsafe for munging.
200 // println("MUNGE: SKIP - Scope" + this.id+"</BR>");
212 // Do not munge symbols in the global scope!
216 for (var ii in this.identifiers) {
219 //print("MUNGE: " + all.join(', '));
221 //println("MUNGE: Building FreeSyms:" + this.id+"</BR>");
223 var freeSymbols = [];
224 var sy = this.getAllUsedSymbols();
226 var addSyms=function(batch)
228 for(var i =0;i<batch.length;i++) {
229 if (sy.indexOf(batch[i]) > -1) {
232 freeSymbols.push(batch[i]);
239 //println(freeSymbols.toSource());
241 //println("MUNGE: Replacing " + this.id+"</BR>");
242 for (var i in this.identifiers) {
244 // is the identifer in the global scope!?!!?
247 if (!this.identifiers[i].toMunge) {
248 //print("SKIP toMunge==false : " + i)
252 if (this.isProtectedVar(i)) {
253 //print("SKIP PROTECTED: " + i)
259 //if (this.identifiers[i].constructor != Identifier) {
260 // print("SKIP NOT IDENTIFIER : " + i)
263 // println("IDENT:" +i+'</BR>');
265 if (!repsym.length) {
266 if (!freeSymbols.length) {
269 repsym = freeSymbols.shift(); // pop off beginngin???
272 var identifier = this.identifiers[i];
273 //println(typeof(identifier.name));
274 var mungedValue = identifier.name;
276 //println([ repsym,mungedValue ]);
278 if (this.mungeM && repsym.length < mungedValue.length) {
279 //print("REPLACE:"+ mungedValue +" with " + repsym );
280 mungedValue = repsym;
284 identifier.mungedValue = mungedValue;
286 //println("MUNGE: Done " + this.id+"</BR>");
289 //println("Doing sub scopes");
290 for (var j = 0; j < this.subScopes.length; j++) {
291 var ss = this.subScopes[j];
303 XObject.extend(Scope, {
305 builtin : ["NaN","top"],
306 skips : [ 'as', 'is', 'do', 'if', 'in', 'for', 'int', 'new', 'try', 'use', 'var', "NaN","top"],
313 if (File.exists('/tmp/var_list_ones.js')) {
314 eval("JSDOC.Scope.ones = " + File.read('/tmp/var_list_ones.js'));
315 eval("JSDOC.Scope.twos = " + File.read('/tmp/var_twos_ones.js'));
316 eval("JSDOC.Scope.threes = " + File.read('/tmp/var_threes_ones.js'));
319 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(',');
321 var n = a.concat( '0,1,2,3,4,5,6,7,8,9'.split(','));
322 for(var i = 0; i < a.length; i++) {
323 for(var j = 0; j < n.length; j++) {
324 var tw = a[i] + n[j];
325 if (this.skips.indexOf(tw) < 0) {
330 for(var k = 0; k < n.length; k++) {
331 var thr = a[i] + n[j] + n[k];
332 //println("thr="+ thr + ":iOf="+this.skips.indexOf(thr) );
333 if (this.skips.indexOf(thr) < 0) {
335 this.threes.push(thr);
342 //println("done creating var list");
343 //println("threes="+ this.threes.toSource());
349 // init the scope constants..