42e36c50031a89b1514c19e159d76aacfa16d1cb
[gnome.introspection-doc-generator] / JSDOC / ScopeParser.js
1 //<Script type="text/javascript">
2
3 Scope = imports.Scope.Scope;
4 TokenStream = imports.TokenStream.TokenStream;
5 /**
6 * Scope stuff
7
8 * // FIXME - I need this to do next() without doccomments..
9
10
11
12 * Need to make this alot simpler...
13
14 * so debugging is possible.
15
16
17 * at present it just runs along the stream and finds stuff then calls parseExpr .. etc,,
18
19
20 * It would be better to parse blocks of code rather than the whole stream..
21
22
23
24
25
26 */
27
28 ScopeParser = function(ts) {
29     this.ts = ts; // {TokenStream}
30     this.warnings = [];
31     this.scopes = [];
32     this.indexedScopes = {};
33     this.timer = new Date() * 1;
34     this.debug = false;
35 }
36
37 // list of keywords that should not be used in object literals.
38 ScopeParser.idents = [
39         "break",         
40         "case",          
41         "continue",     
42         "default",      
43         "delete",       
44         "do",            
45         "else",         
46         "export",       
47         "false",        
48         "for",          
49         "function",     
50         "if",           
51         "import",       
52         "in",           
53         "new",          
54         "null",         
55         "return",       
56         "switch",       
57         "this",         
58         "true",         
59         "typeof",       
60         "var",          
61         "void",         
62         "while",        
63         "with",         
64
65         "catch",        
66         "class",        
67         "const",        
68         "debugger",     
69         "enum",         
70         "extends",      
71         "finally",      
72         "super",        
73         "throw",         
74         "try",          
75
76         "abstract",     
77         "boolean",      
78         "byte",         
79         "char",         
80         "double",       
81         "final",        
82         "float",        
83         "goto",         
84         "implements", 
85         "instanceof",
86         "int",           
87         "interface",     
88         "long",          
89         "native",       
90         "package",      
91         "private",      
92         "protected",     
93         "public",        
94         "short",        
95         "static",       
96         "synchronized",  
97         "throws",        
98         "transient",     
99                 "include",       
100                 "undefined"
101 ];
102
103
104 ScopeParser.prototype = {
105     timer: 0,
106     timerPrint: function (str) {
107         var ntime = new Date() * 1;
108         var tdif =  ntime -this.timer;
109         this.timer = ntime;
110         var pref = '';
111         if (tdif > 100) { //slower ones..
112             pref = '***';
113         }
114         println(pref+'['+tdif+']'+str);
115         
116     },
117     warn: function(s) {
118         //print('****************' + s);
119         this.warnings.push(s);
120         //println("WARNING:" + htmlescape(s) + "<BR>");
121     },
122     // defaults should not be initialized here =- otherwise they get duped on new, rather than initalized..
123     warnings : false,
124     ts : false,
125     scopes : false,
126     global : false,
127     mode : "", //"BUILDING_SYMBOL_TREE",
128     braceNesting : 0,
129     indexedScopes : false,
130     munge: true,
131
132
133
134
135
136     buildSymbolTree : function()
137     {
138         //println("<PRE>");
139         
140         this.ts.rewind();
141         this.braceNesting = 0;
142         this.scopes = [];
143         
144         print(JSON.stringify(this.ts.tokens, null,4));
145         
146         
147         this.globalScope = new  Scope(-1, false, -1, '');
148         indexedScopes = { 0 : this.globalScope };
149         
150         this.mode = 'BUILDING_SYMBOL_TREE';
151         this.parseScope(this.globalScope);
152         
153         //print("---------------END PASS 1 ---------------- ");
154         
155     },
156     mungeSymboltree : function()
157     {
158
159         if (!this.munge) {
160             return;
161         }
162
163         // One problem with obfuscation resides in the use of undeclared
164         // and un-namespaced global symbols that are 3 characters or less
165         // in length. Here is an example:
166         //
167         //     var declaredGlobalVar;
168         //
169         //     function declaredGlobalFn() {
170         //         var localvar;
171         //         localvar = abc; // abc is an undeclared global symbol
172         //     }
173         //
174         // In the example above, there is a slim chance that localvar may be
175         // munged to 'abc', conflicting with the undeclared global symbol
176         // abc, creating a potential bug. The following code detects such
177         // global symbols. This must be done AFTER the entire file has been
178         // parsed, and BEFORE munging the symbol tree. Note that declaring
179         // extra symbols in the global scope won't hurt.
180         //
181         // Note: Since we go through all the tokens to do this, we also use
182         // the opportunity to count how many times each identifier is used.
183
184         this.ts.rewind();
185         this.braceNesting = 0;
186         this.scopes= [];
187         this.mode = 'PASS2_SYMBOL_TREE';
188         
189         //println("MUNGING?");
190         
191         this.parseScope(this.globalScope);
192         this.globalScope.munge();
193     },
194
195
196     log : function(str)
197     {
198         print ("                    ".substring(0, this.braceNesting*2) + str);
199         
200         //println("<B>LOG:</B>" + htmlescape(str) + "<BR/>\n");
201     },
202     logR : function(str)
203     {
204             //println("<B>LOG:</B>" + str + "<BR/>");
205     },
206
207      
208     
209
210
211     parseScope : function(scope) // parse a token stream..
212     {
213         //this.timerPrint("parseScope EnterScope"); 
214         //this.log(">>> ENTER SCOPE" + this.scopes.length);
215         var symbol;
216         var token;
217         
218         var identifier;
219
220         var expressionBraceNesting = this.braceNesting + 0;
221         
222         var parensNesting = 0;
223         
224         var isObjectLitAr = [ false ];
225         var isInObjectLitAr;
226         thisScope = scope;
227         if (thisScope && thisScope.gid != this.scopes[this.scopes.length-1]) {
228             this.scopes.push(scope);
229         } else {
230             thisScope = this.scopes[this.scopes.length-1]
231         }
232        
233         //var scopeIndent = ''; 
234         //this.scopes.forEach(function() {
235         //    scopeIndent += '   '; 
236         //});
237         //print(">> ENTER SCOPE");
238         
239         
240         
241         
242         token = this.ts.lookTok(1);
243         while (token) {
244           //  this.timerPrint("parseScope AFTER lookT: " + token.toString()); 
245             //this.dumpToken(token , this.scopes, this.braceNesting);
246             //print('SCOPE:' + token.toString());
247             //this.log(token.data);
248             if (token.type == 'NAME') {
249             //    print('*' + token.data);
250             }
251             switch(token.type + '.' + token.name) {
252                 case "KEYW.VAR":
253                 case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
254                     //print('SCOPE-VAR:' + token.toString());
255                     var vstart = this.ts.cursor +1;
256                     
257                     //this.log("parseScope GOT VAR/CONST : " + token.toString()); 
258                     while (true) {
259                         token = this.ts.nextTok();
260                         //!this.debug|| print( token.toString());
261                         print('SCOPE-VAR-VAL:' + JSON.stringify(token, null, 4));
262                         if (!token) { // can return false at EOF!
263                             break;
264                         }
265                         if (token.name == "VAR" || token.data == ',') { // kludge..
266                             continue;
267                         }
268                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
269                         if (token.type != "NAME") {
270                             for(var i = Math.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
271                                 print(this.ts.tokens[i].toString());
272                             }
273                             
274                             print( "var without ident");
275                             Seed.quit()
276                         }
277                         
278
279                         if (this.mode == "BUILDING_SYMBOL_TREE") {
280                             identifier = thisScope.getIdentifier(token.data,token) ;
281                             
282                             if (identifier == false) {
283                                 thisScope.declareIdentifier(token.data, token);
284                             } else {
285                                 token.identifier = identifier;
286                                 this.warn("(SCOPE) The variable " + token.data  + ' (line:' + token.line + ")  has already been declared in the same scope...");
287                             }
288                         }
289
290                         token = this.ts.nextTok();
291                         !this.debug|| print(token.toString());
292                         /*
293                         assert token.getType() == Token.SEMI ||
294                                 token.getType() == Token.ASSIGN ||
295                                 token.getType() == Token.COMMA ||
296                                 token.getType() == Token.IN;
297                         */
298                         if (token.name == "IN") {
299                             break;
300                         } else {
301                             //var bn = this.braceNesting;
302                             var bn = this.braceNesting;
303                             var nts = [];
304                             while (true) {
305                                 var n = this.ts.lookTok();
306                                 print(n.toString());
307                                 if (!n || n.type == 'VOID' || n.data == ',') {
308                                     break;
309                                 }
310                                 nts.push(n);
311                                 this.ts.nextTok();
312                             }
313                             if (nts.length) {
314                                 var TS = this.ts;
315                                 this.ts = new TokenStream(nts);
316                                 this.parseExpression();
317                                 this.ts = TS;
318                             }
319                                
320                             this.braceNesting = bn;
321                             //this.braceNesting = bn;
322                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
323                             
324                             token = this.ts.lookTok(1);
325                             !this.debug|| print("AFTER EXP: " + token.toString());
326                             if (token.data == ';') {
327                                 break;
328                             }
329                         }
330                     }
331                     
332                     //print("VAR:")
333                     //this.ts.dump(vstart , this.ts.cursor);
334                     
335                     break;
336                 case "KEYW.FUNCTION":
337                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
338                     //    print('SCOPE-FUNC:' + JSON.stringify(token,null,4));
339                     //println("<i>"+token.data+"</i>");
340                      var bn = this.braceNesting;
341                     this.parseFunctionDeclaration();
342                      this.braceNesting = bn;
343                     break;
344
345                 case "PUNC.LEFT_CURLY": // {
346                 case "PUNC.LEFT_PAREN": // (    
347                 case "PUNC.LEFT_BRACE": // [
348                     //print('SCOPE-CURLY/PAREN:' + token.toString());
349                     //println("<i>"+token.data+"</i>");
350                     var curTS = this.ts;
351                     if (token.props) {
352                         
353                         for (var prop in token.props) {
354                             
355                             
356                           //  print('SCOPE-PROPS:' + JSON.stringify(token.props[prop],null,4));
357                             if (token.props[prop].val[0].data == 'function') {
358                                 // parse a function..
359                                 this.ts = new TokenStream(token.props[prop].val);
360                                 this.ts.nextTok();
361                                 this.parseFunctionDeclaration();
362                                 
363                                 continue;
364                             }
365                             // key value..
366                             
367                             this.ts = new TokenStream(token.props[prop].val);
368                             this.parseExpression();
369                             
370                         }
371                         this.ts = curTS;
372                         
373                         // it's an object literal..
374                         // the values could be replaced..
375                         break;
376                     }
377                     
378                     
379                     var _this = this;
380                     token.items.forEach(function(expr) {
381                           _this.ts = new TokenStream(expr);
382                           _this.parseExpression()
383                     });
384                     this.ts = curTS;
385                     //print("NOT PROPS"); Seed.quit();
386                     
387                     //isObjectLitAr.push(false);
388                     //this.braceNesting++;
389                     
390                     //print(">>>>>> OBJLIT PUSH(false)" + this.braceNesting);
391                     break;
392
393                 case "PUNC.RIGHT_CURLY": // }
394                     //print("<< EXIT SCOPE");
395                     return;
396                 /*
397                     //println("<i>"+token.data+"</i>");
398                     this.braceNesting--;
399                     isObjectLitAr.pop();
400                     //print(">>>>>> OBJLIT POP"+ this.braceNesting);
401                         //assert braceNesting >= scope.getBra ceNesting();
402                     
403                     if (this.braceNesting < expressionBraceNesting) {
404                         var ls = this.scopes.pop();
405                         ls.getUsedSymbols();
406                         // eat symbol if we are currently at { 
407                         if (this.ts.look(0).data == '{') {
408                             this.ts.nextTok();
409                         }
410                         
411                         print("<<<<<<<EXIT SCOPE" +this.scopes.length);
412                         return;
413                     }
414                     break;
415 */
416                 case "KEYW.WITH":
417                     //print('SCOPE-WITH:' + token.toString());
418                     //println("<i>"+token.data+"</i>");   
419                     if (this.mode == "BUILDING_SYMBOL_TREE") {
420                         // Inside a 'with' block, it is impossible to figure out
421                         // statically whether a symbol is a local variable or an
422                         // object member. As a consequence, the only thing we can
423                         // do is turn the obfuscation off for the highest scope
424                         // containing the 'with' block.
425                         this.protectScopeFromObfuscation(thisScope);
426                         this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
427                     }
428                     break;
429
430                 case "KEYW.CATCH":
431                     //print('SCOPE-CATCH:' + token.toString());
432                     //println("<i>"+token.data+"</i>");
433                     this.parseCatch();
434                     break;
435                 /*
436                 case Token.SPECIALCOMMENT:
437                         if (mode == BUILDING_SYMBOL_TREE) {
438                             protectScopeFromObfuscation(scope);
439                             this.warn("Using JScript conditional comments is not recommended." + (munge ? " Moreover, using JScript conditional comments reduces the level of compression." : ""), true);
440                         }
441                         break;
442                 */
443                 
444                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
445                 case "STRN.SINGLE_QUOTE":
446                   //  print('SCOPE-STRING:' + token.toString());
447                     //println("<i>"+token.data+"</i>");
448
449                     if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
450                         // then we are in an object lit.. -> we need to flag the brace as such...
451                         isObjectLitAr.pop();
452                         isObjectLitAr.push(true);
453                         //print(">>>>>> OBJLIT REPUSH(true)");
454                     }
455                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
456                     
457                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
458                         ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
459                         // see if we can replace..
460                         // remove the quotes..
461                         // should do a bit more checking!!!! (what about wierd char's in the string..
462                         var str = token.data.substring(1,token.data.length-1);
463                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
464                             token.outData = str;
465                         }
466                         
467                          
468                         
469                     }
470                     
471                     
472                     
473                     break;
474                 
475                 case "NAME.NAME":
476                     //print('SCOPE-NAME:' + token.toString());
477                     //print("DEAL WITH NAME:");
478                     // got identifier..
479                     
480                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
481                      
482                     
483                     // skip anyting with "." before it..!!
484                      
485                     if (this.ts.lookTok(-1).data == ".") {
486                         // skip, it's an object prop.
487                         //println("<i>"+token.data+"</i>");
488                         break;
489                     }
490                     //print("SYMBOL: " + token.toString());
491                     
492                     symbol = token.data;
493                     if (symbol == 'this') {
494                         break;
495                     }
496                     if (this.mode == 'PASS2_SYMBOL_TREE') {
497                         
498                         //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
499                         
500                         //print ("MUNGE?" + symbol);
501                         
502                         //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
503                              
504                             //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
505                         identifier = this.getIdentifier(symbol, thisScope, token);
506                         
507                         if (identifier == false) {
508 // BUG!find out where builtin is defined...
509                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
510                                 // Here, we found an undeclared and un-namespaced symbol that is
511                                 // 3 characters or less in length. Declare it in the global scope.
512                                 // We don't need to declare longer symbols since they won't cause
513                                 // any conflict with other munged symbols.
514                                 this.globalScope.declareIdentifier(symbol, token);
515                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
516                             }
517                             
518                             //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
519                         } else {
520                             token.identifier = identifier;
521                             identifier.refcount++;
522                         }
523                     }   
524                     
525                     break;
526                     //println("<B>SID</B>");
527                 default:
528                     if (token.type != 'KEYW') {
529                         break;
530                     }
531                     //print('SCOPE-KEYW:' + token.toString());
532                    // print("Check eval:");
533                 
534                     symbol = token.data;
535                     
536                      if (this.mode == 'BUILDING_SYMBOL_TREE') {
537
538                         if (symbol == "eval") {
539                             // look back one and see if we can find a comment!!!
540                             //if (this.ts.look(-1).type == "COMM") {
541                             if (token.prefix && token.prefix.match('/eval/')) {
542                                 // look for eval:var:noreplace\n
543                                 var _t = this;
544                                 token.prefix.replace(/eval:var:([a-z_]+)/ig, function(m, a) {
545                                     
546                                     var hi = _t.getIdentifier(a, thisScope, token);
547                                    // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
548                                     if (hi) {
549                                      //   println("PROTECT "+a+" from munge");
550                                         hi.toMunge = false;
551                                     }
552                                     
553                                 });
554                                 
555                                 
556                             } else {
557                                 
558                             
559                                 this.protectScopeFromObfuscation(thisScope);
560                                 this.warn("Using 'eval' is not recommended. (use  eval:var:noreplace in comments to optimize) " + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
561                             }
562
563                         }
564
565                     }
566                     break;
567                 
568                 
569             } // end switch
570             
571             
572             //print("parseScope TOK : " + token.toString()); 
573             token = this.ts.nextTok();
574             //if (this.ts.nextT()) break;
575             
576         }
577         //print("<<< EXIT SCOPE");
578         //print("<<<<<<<EXIT SCOPE ERR?" +this.scopes.length);
579     },
580
581     expN : 0,
582     parseExpression : function() {
583
584         // Parse the expression until we encounter a comma or a semi-colon
585         // in the same brace nesting, bracket nesting and paren nesting.
586         // Parse functions if any...
587         //println("<i>EXP</i><BR/>");
588         !this.debug || print("PARSE EXPR");
589         this.expN++;
590          
591         // for printing stuff..
592        
593         
594         
595         var symbol;
596         var token;
597         var currentScope;
598         var identifier;
599
600         var expressionBraceNesting = this.braceNesting + 0;
601         var bracketNesting = 0;
602         var parensNesting = 0;
603         var isInObjectLitAr;
604         var isObjectLitAr = [ false ];
605         
606         currentScope = this.scopes[this.scopes.length-1];
607             
608         
609         //print(scopeIndent + ">> ENTER EXPRESSION" + this.expN);
610         while (token = this.ts.nextTok()) {
611      
612         
613             
614            /*
615             // moved out of loop?
616            currentScope = this.scopes[this.scopes.length-1];
617             
618             var scopeIndent = ''; 
619             this.scopes.forEach(function() {
620                 scopeIndent += '   '; 
621             });
622            */ 
623            
624            //this.dumpToken(token,  this.scopes, this.braceNesting );
625            //print('EXP' +  token.toString());
626             
627             
628             //println("<i>"+token.data+"</i>");
629             //this.log("EXP:" + token.data);
630             switch (token.type) {
631                 case 'PUNC':
632                     //print("EXPR-PUNC:" + token.toString());
633                     
634                     switch(token.data) {
635                          
636                         case ';':
637                             //print("<< EXIT EXPRESSION");
638                             break;
639
640                         case ',':
641                             
642                             break;
643
644                        
645                         case '(': //Token.LP:
646                         case '{': //Token.LC:
647                         case '[': //Token.LB:
648                             //print('SCOPE-CURLY/PAREN/BRACE:' + token.toString());
649                            // print('SCOPE-CURLY/PAREN/BRACE:' + JSON.stringify(token, null,4));
650                             //println("<i>"+token.data+"</i>");
651                             var curTS = this.ts;
652                             if (token.props) {
653                                 
654                                 for (var prop in token.props) {
655                                     if (token.props[prop].val[0].data == 'function') {
656                                         // parse a function..
657                                         this.ts = new TokenStream(token.props[prop].val);
658                                         this.ts.nextTok();
659                                         this.parseFunctionDeclaration();
660                                         continue;
661                                     }
662                                     // key value..
663                                     
664                                     this.ts = new TokenStream(token.props[prop].val);
665                                     this.parseExpression();
666                                     
667                                 }
668                                 this.ts = curTS;
669                                 
670                                 // it's an object literal..
671                                 // the values could be replaced..
672                                 break;
673                             }
674                             
675                             
676                             var _this = this;
677                             token.items.forEach(function(expr) {
678                                   _this.ts = new TokenStream(expr);
679                                   _this.parseExpression()
680                             });
681                             this.ts = curTS;
682                         
683                         
684                     
685                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
686                             break;
687
688                        
689                         
690                          
691                             
692                         case ')': //Token.RP:
693                         case ']': //Token.RB:
694                         case '}': //Token.RB:
695                             //print("<< EXIT EXPRESSION");
696                             return;
697                            
698  
699              
700                             parensNesting++;
701                             break;
702
703                         
704                             
705                     }
706                     break;
707                     
708                 case 'STRN': // used for object lit detection..
709                     //if (this.mode == 'BUILDING_SYMBOL_TREE')    
710                         //print("EXPR-STR:" + JSON.stringify(token, null, 4));
711                
712                      
713                     break;
714                 
715                       
716              
717                 case 'NAME':
718                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
719                         
720                         //print("EXPR-NAME:" + JSON.stringify(token, null, 4));
721                     } else {
722                         //print("EXPR-NAME:" + token.toString());
723                     }
724                     symbol = token.data;
725                     //print("in NAME = " + token.toString());
726                     //print("in NAME 0: " + this.ts.look(0).toString());
727                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
728                     
729                     //print(this.ts.lookTok(-1).data);
730                     // prefixed with '.'
731                     if (this.ts.lookTok(-1).data == ".") {
732                         //skip '.'
733                         break;
734                     }
735                     if (symbol == 'this') {
736                         break;
737                        }
738                     
739                     if (this.mode == 'PASS2_SYMBOL_TREE') {
740
741                         identifier = this.getIdentifier(symbol, currentScope, token);
742                         //println("<B>??</B>");
743                         if (identifier == false) {
744
745                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
746                                 // Here, we found an undeclared and un-namespaced symbol that is
747                                 // 3 characters or less in length. Declare it in the global scope.
748                                 // We don't need to declare longer symbols since they won't cause
749                                 // any conflict with other munged symbols.
750                                 this.globalScope.declareIdentifier(symbol, token);
751                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
752                                 //print("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')');
753                                 //throw "OOPS";
754                             } else {
755                                 //print("undeclared:" + token.toString())
756                             }
757                             
758                             
759                         } else {
760                             //println("<B>++</B>");
761                             token.identifier = identifier;
762                             identifier.refcount++;
763                         }
764                         
765                     }
766                     break;
767                     
768                     
769                     
770                     
771                     //println("<B>EID</B>");
772                 case 'KEYW':   
773                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
774                     //    print("EXPR-KEYW:" + JSON.stringify(token, null, 4));
775                     if (token.name == "FUNCTION") {
776                         
777                         this.parseFunctionDeclaration();
778                         break;
779                     }
780                
781                     
782              
783                     symbol = token.data;
784                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
785
786                         if (symbol == "eval") {
787                             if (token.prefix && token.prefix.match('/eval/')) {
788                                 // look for eval:var:noreplace\n
789                                 var _t = this;
790                                 token.prefix.replace(/eval:var:([a-z]+)/ig, function(m, a) {
791                                     var hi = _t.getIdentifier(a, currentScope, token);
792                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
793                                     if (hi) {
794                                       //  println("PROTECT "+a+" from munge");
795                                         hi.toMunge = false;
796                                     }
797                                     
798                                     
799                                 });
800                                 
801                             } else {
802                                 this.protectScopeFromObfuscation(currentScope);
803                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
804                             }
805                             
806
807                         }
808                         break;
809                     } 
810                 default:
811                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
812                     //    print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
813                     break;
814             }
815             
816         }
817         //print("<< EXIT EXPRESSION");
818         this.expN--;
819     },
820
821
822     parseCatch : function() {
823
824         var symbol;
825         var token;
826         var currentScope;
827         var identifier;
828
829         //token = getToken(-1);
830         //assert token.getType() == Token.CATCH;
831         token = this.ts.nextTok();
832         //assert token.getType() == Token.LP; (
833         //token = this.ts.nextTok();
834         //assert token.getType() == Token.NAME;
835         
836         symbol = token.items[0].data;
837         currentScope = this.scopes[this.scopes.length-1];
838
839         if (this.mode == 'BUILDING_SYMBOL_TREE') {
840             // We must declare the exception identifier in the containing function
841             // scope to avoid errors related to the obfuscation process. No need to
842             // display a warning if the symbol was already declared here...
843             currentScope.declareIdentifier(symbol, token);
844         } else {
845             //?? why inc the refcount?? - that should be set when building the tree???
846             identifier = this.getIdentifier(symbol, currentScope, token);
847             identifier.refcount++;
848         }
849
850         token = this.ts.nextTok();
851         //assert token.getType() == Token.RP; // )
852     },
853     
854     parseFunctionDeclaration : function() 
855     {
856         //print("PARSE FUNCTION");
857         var symbol;
858         var token;
859         var currentScope  = false; 
860         var fnScope = false;
861         var identifier;
862         var b4braceNesting = this.braceNesting + 0;
863         
864         //this.logR("<B>PARSING FUNCTION</B>");
865         currentScope = this.scopes[this.scopes.length-1];
866
867         token = this.ts.nextTok();
868         if (token.type == "NAME") {
869             if (this.mode == 'BUILDING_SYMBOL_TREE') {
870                 // Get the name of the function and declare it in the current scope.
871                 symbol = token.data;
872                 if (currentScope.getIdentifier(symbol,token) != false) {
873                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
874                 }
875                 currentScope.declareIdentifier(symbol,token);
876             }
877             token =  this.ts.nextTok();
878         }
879         // return function() {.... 
880         if (token.name == "RETURN") {
881             token =  this.ts.nextTok();
882         }
883         if (token.name == "FUNCTION") {
884             token =  this.ts.nextTok();
885         }
886         //assert token.getType() == Token.LP;
887         if (this.mode == 'BUILDING_SYMBOL_TREE') {
888             fnScope = new Scope(1, currentScope, token.n, '', token);
889             
890             //println("STORING SCOPE" + this.ts.cursor);
891             
892             this.indexedScopes[token.id] = fnScope;
893             
894         } else {
895             //qln("FETCHING SCOPE" + this.ts.cursor);
896             fnScope = this.indexedScopes[token.id];
897         }
898         //if (this.mode == 'BUILDING_SYMBOL_TREE') 
899            // print('FUNC-PARSE:' + JSON.stringify(token,null,4));
900         // Parse function arguments.
901         var args = token.items;
902         for (var argpos =0; argpos < args.length; argpos++) {
903              
904             token = args[argpos][0];
905             //print ("FUNC ARGS: " + token.toString())
906             //assert token.getType() == Token.NAME ||
907             //        token.getType() == Token.COMMA;
908             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
909                 symbol = token.data;
910                 identifier = fnScope.declareIdentifier(symbol,token);
911                 if (symbol == "$super" && argpos == 0) {
912                     // Exception for Prototype 1.6...
913                     identifier.preventMunging();
914                 }
915                 //argpos++;
916             }
917         }
918         
919         token = this.ts.nextTok();
920         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
921         //Seed.quit();
922         //print(token.toString());
923         // assert token.getType() == Token.LC;
924         //this.braceNesting++;
925         
926         //token = this.ts.nextTok();
927         //print(token.toString());
928         var outTS = this.ts;
929         var _this = this;
930         token.items.forEach(function(tar) {
931             _this.ts = new TokenStream(tar);
932             _this.parseScope(fnScope);
933             
934             
935         });
936         
937         //print(JSON.stringify(this.ts,null,4));
938         //this.parseScope(fnScope);
939         this.ts = outTS;
940         // now pop it off the stack!!!
941        
942         //this.braceNesting = b4braceNesting;
943         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
944         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
945         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
946     },
947     
948     protectScopeFromObfuscation : function(scope) {
949             //assert scope != null;
950         
951         if (scope == this.globalScope) {
952             // The global scope does not get obfuscated,
953             // so we don't need to worry about it...
954             return;
955         }
956
957         // Find the highest local scope containing the specified scope.
958         while (scope && scope.parent != this.globalScope) {
959             scope = scope.parent;
960         }
961
962         //assert scope.getParentScope() == globalScope;
963         scope.preventMunging();
964     },
965     
966     getIdentifier: function(symbol, scope, token) {
967         var identifier;
968         while (scope != false) {
969             identifier = scope.getIdentifier(symbol, token);
970             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
971             if (identifier) {
972                 return identifier;
973             }
974             scope = scope.parent;
975         }
976         return false;
977     }
978 };