JSDOC/ScopeParser.js
[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                                 if (!token || token.type == 'VOID' || token.data == ',') {
306                                     break;
307                                 }
308                                 nts.push(token);
309                                 token = this.ts.nextTok();
310                             }
311                             if (nts.length) {
312                                 var TS = this.ts;
313                                 this.ts = new TokenStream(nts);
314                                 this.parseExpression();
315                                 this.ts = TS;
316                             }
317                                
318                             this.braceNesting = bn;
319                             //this.braceNesting = bn;
320                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
321                             
322                             token = this.ts.lookTok(1);
323                             !this.debug|| print("AFTER EXP: " + token.toString());
324                             if (token.data == ';') {
325                                 break;
326                             }
327                         }
328                     }
329                     
330                     //print("VAR:")
331                     //this.ts.dump(vstart , this.ts.cursor);
332                     
333                     break;
334                 case "KEYW.FUNCTION":
335                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
336                     //    print('SCOPE-FUNC:' + JSON.stringify(token,null,4));
337                     //println("<i>"+token.data+"</i>");
338                      var bn = this.braceNesting;
339                     this.parseFunctionDeclaration();
340                      this.braceNesting = bn;
341                     break;
342
343                 case "PUNC.LEFT_CURLY": // {
344                 case "PUNC.LEFT_PAREN": // (    
345                 case "PUNC.LEFT_BRACE": // [
346                     //print('SCOPE-CURLY/PAREN:' + token.toString());
347                     //println("<i>"+token.data+"</i>");
348                     var curTS = this.ts;
349                     if (token.props) {
350                         
351                         for (var prop in token.props) {
352                             
353                             
354                           //  print('SCOPE-PROPS:' + JSON.stringify(token.props[prop],null,4));
355                             if (token.props[prop].val[0].data == 'function') {
356                                 // parse a function..
357                                 this.ts = new TokenStream(token.props[prop].val);
358                                 this.ts.nextTok();
359                                 this.parseFunctionDeclaration();
360                                 
361                                 continue;
362                             }
363                             // key value..
364                             
365                             this.ts = new TokenStream(token.props[prop].val);
366                             this.parseExpression();
367                             
368                         }
369                         this.ts = curTS;
370                         
371                         // it's an object literal..
372                         // the values could be replaced..
373                         break;
374                     }
375                     
376                     
377                     var _this = this;
378                     token.items.forEach(function(expr) {
379                           _this.ts = new TokenStream(expr);
380                           _this.parseExpression()
381                     });
382                     this.ts = curTS;
383                     //print("NOT PROPS"); Seed.quit();
384                     
385                     //isObjectLitAr.push(false);
386                     //this.braceNesting++;
387                     
388                     //print(">>>>>> OBJLIT PUSH(false)" + this.braceNesting);
389                     break;
390
391                 case "PUNC.RIGHT_CURLY": // }
392                     //print("<< EXIT SCOPE");
393                     return;
394                 /*
395                     //println("<i>"+token.data+"</i>");
396                     this.braceNesting--;
397                     isObjectLitAr.pop();
398                     //print(">>>>>> OBJLIT POP"+ this.braceNesting);
399                         //assert braceNesting >= scope.getBra ceNesting();
400                     
401                     if (this.braceNesting < expressionBraceNesting) {
402                         var ls = this.scopes.pop();
403                         ls.getUsedSymbols();
404                         // eat symbol if we are currently at { 
405                         if (this.ts.look(0).data == '{') {
406                             this.ts.nextTok();
407                         }
408                         
409                         print("<<<<<<<EXIT SCOPE" +this.scopes.length);
410                         return;
411                     }
412                     break;
413 */
414                 case "KEYW.WITH":
415                     //print('SCOPE-WITH:' + token.toString());
416                     //println("<i>"+token.data+"</i>");   
417                     if (this.mode == "BUILDING_SYMBOL_TREE") {
418                         // Inside a 'with' block, it is impossible to figure out
419                         // statically whether a symbol is a local variable or an
420                         // object member. As a consequence, the only thing we can
421                         // do is turn the obfuscation off for the highest scope
422                         // containing the 'with' block.
423                         this.protectScopeFromObfuscation(thisScope);
424                         this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
425                     }
426                     break;
427
428                 case "KEYW.CATCH":
429                     //print('SCOPE-CATCH:' + token.toString());
430                     //println("<i>"+token.data+"</i>");
431                     this.parseCatch();
432                     break;
433                 /*
434                 case Token.SPECIALCOMMENT:
435                         if (mode == BUILDING_SYMBOL_TREE) {
436                             protectScopeFromObfuscation(scope);
437                             this.warn("Using JScript conditional comments is not recommended." + (munge ? " Moreover, using JScript conditional comments reduces the level of compression." : ""), true);
438                         }
439                         break;
440                 */
441                 
442                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
443                 case "STRN.SINGLE_QUOTE":
444                   //  print('SCOPE-STRING:' + token.toString());
445                     //println("<i>"+token.data+"</i>");
446
447                     if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
448                         // then we are in an object lit.. -> we need to flag the brace as such...
449                         isObjectLitAr.pop();
450                         isObjectLitAr.push(true);
451                         //print(">>>>>> OBJLIT REPUSH(true)");
452                     }
453                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
454                     
455                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
456                         ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
457                         // see if we can replace..
458                         // remove the quotes..
459                         // should do a bit more checking!!!! (what about wierd char's in the string..
460                         var str = token.data.substring(1,token.data.length-1);
461                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
462                             token.outData = str;
463                         }
464                         
465                          
466                         
467                     }
468                     
469                     
470                     
471                     break;
472                 
473                 case "NAME.NAME":
474                     //print('SCOPE-NAME:' + token.toString());
475                     //print("DEAL WITH NAME:");
476                     // got identifier..
477                     
478                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
479                      
480                     
481                     // skip anyting with "." before it..!!
482                      
483                     if (this.ts.lookTok(-1).data == ".") {
484                         // skip, it's an object prop.
485                         //println("<i>"+token.data+"</i>");
486                         break;
487                     }
488                     //print("SYMBOL: " + token.toString());
489                     
490                     symbol = token.data;
491                     if (symbol == 'this') {
492                         break;
493                     }
494                     if (this.mode == 'PASS2_SYMBOL_TREE') {
495                         
496                         //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
497                         
498                         //print ("MUNGE?" + symbol);
499                         
500                         //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
501                              
502                             //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
503                         identifier = this.getIdentifier(symbol, thisScope, token);
504                         
505                         if (identifier == false) {
506 // BUG!find out where builtin is defined...
507                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
508                                 // Here, we found an undeclared and un-namespaced symbol that is
509                                 // 3 characters or less in length. Declare it in the global scope.
510                                 // We don't need to declare longer symbols since they won't cause
511                                 // any conflict with other munged symbols.
512                                 this.globalScope.declareIdentifier(symbol, token);
513                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
514                             }
515                             
516                             //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
517                         } else {
518                             token.identifier = identifier;
519                             identifier.refcount++;
520                         }
521                     }   
522                     
523                     break;
524                     //println("<B>SID</B>");
525                 default:
526                     if (token.type != 'KEYW') {
527                         break;
528                     }
529                     print('SCOPE-KEYW:' + token.toString());
530                    // print("Check eval:");
531                 
532                     symbol = token.data;
533                     
534                      if (this.mode == 'BUILDING_SYMBOL_TREE') {
535
536                         if (token.name == "EVAL") {
537                             
538                             print(JSON.stringify(token, null,4));
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('EXPR' +  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                     
776                     print('EXPR-KEYW:' + token.toString());
777                     if (token.name == "FUNCTION") {
778                         
779                         this.parseFunctionDeclaration();
780                         break;
781                     }
782                
783                     
784              
785                     symbol = token.data;
786                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
787                         
788                         if (token.name == "EVAL") {
789                             print(JSON.stringify(token,null,4));
790                             if (token.prefix && token.prefix.match(/eval:var:/g)) {
791                                 // look for eval:var:noreplace\n
792                                 var _t = this;
793                                 token.prefix.replace(/eval:var:([a-z]+)/ig, function(m, a) {
794                                     
795                                     print("PROTECT: " + a);
796                                     
797                                     
798                                     var hi = _t.getIdentifier(a, currentScope, token);
799                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
800                                     if (hi) {
801                                       //  println("PROTECT "+a+" from munge");
802                                         hi.toMunge = false;
803                                     }
804                                     
805                                     
806                                 });
807                                 
808                             } else {
809                                 this.protectScopeFromObfuscation(currentScope);
810                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
811                             }
812                             
813
814                         }
815                         break;
816                     } 
817                 default:
818                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
819                     //    print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
820                     break;
821             }
822             
823         }
824         //print("<< EXIT EXPRESSION");
825         this.expN--;
826     },
827
828
829     parseCatch : function() {
830
831         var symbol;
832         var token;
833         var currentScope;
834         var identifier;
835         
836         //token = getToken(-1);
837         //assert token.getType() == Token.CATCH;
838         token = this.ts.nextTok();
839         
840         //print(JSON.stringify(token,null,4));
841         //assert token.getType() == Token.LP; (
842         //token = this.ts.nextTok();
843         //assert token.getType() == Token.NAME;
844         
845         symbol = token.items[0][0].data;
846         currentScope = this.scopes[this.scopes.length-1];
847
848         if (this.mode == 'BUILDING_SYMBOL_TREE') {
849             // We must declare the exception identifier in the containing function
850             // scope to avoid errors related to the obfuscation process. No need to
851             // display a warning if the symbol was already declared here...
852             currentScope.declareIdentifier(symbol, token.items[0][0]);
853         } else {
854             //?? why inc the refcount?? - that should be set when building the tree???
855             identifier = this.getIdentifier(symbol, currentScope, token.items[0][0]);
856             identifier.refcount++;
857         }
858         
859         token = this.ts.nextTok();
860         //assert token.getType() == Token.RP; // )
861     },
862     
863     parseFunctionDeclaration : function() 
864     {
865         //print("PARSE FUNCTION");
866         var symbol;
867         var token;
868         var currentScope  = false; 
869         var fnScope = false;
870         var identifier;
871         var b4braceNesting = this.braceNesting + 0;
872         
873         //this.logR("<B>PARSING FUNCTION</B>");
874         currentScope = this.scopes[this.scopes.length-1];
875
876         token = this.ts.nextTok();
877         if (token.type == "NAME") {
878             if (this.mode == 'BUILDING_SYMBOL_TREE') {
879                 // Get the name of the function and declare it in the current scope.
880                 symbol = token.data;
881                 if (currentScope.getIdentifier(symbol,token) != false) {
882                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
883                 }
884                 currentScope.declareIdentifier(symbol,token);
885             }
886             token =  this.ts.nextTok();
887         }
888         
889         
890         // return function() {.... 
891         while (token.data != "(") {
892             print(token.toString());
893             token =  this.ts.nextTok();
894             
895             
896             
897         }
898         
899         
900         //assert token.getType() == Token.LP;
901         if (this.mode == 'BUILDING_SYMBOL_TREE') {
902             fnScope = new Scope(1, currentScope, token.n, '', token);
903             
904             //println("STORING SCOPE" + this.ts.cursor);
905             
906             this.indexedScopes[token.id] = fnScope;
907             
908         } else {
909             //qln("FETCHING SCOPE" + this.ts.cursor);
910             fnScope = this.indexedScopes[token.id];
911         }
912         //if (this.mode == 'BUILDING_SYMBOL_TREE') 
913         //  print('FUNC-PARSE:' + JSON.stringify(token,null,4));
914         // Parse function arguments.
915         var args = token.items;
916         for (var argpos =0; argpos < args.length; argpos++) {
917              
918             token = args[argpos][0];
919             //print ("FUNC ARGS: " + token.toString())
920             //assert token.getType() == Token.NAME ||
921             //        token.getType() == Token.COMMA;
922             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
923                 symbol = token.data;
924                 identifier = fnScope.declareIdentifier(symbol,token);
925                 if (symbol == "$super" && argpos == 0) {
926                     // Exception for Prototype 1.6...
927                     identifier.preventMunging();
928                 }
929                 //argpos++;
930             }
931         }
932         
933         token = this.ts.nextTok();
934         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
935         //Seed.quit();
936         //print(token.toString());
937         // assert token.getType() == Token.LC;
938         //this.braceNesting++;
939         
940         //token = this.ts.nextTok();
941         //print(token.toString());
942         var outTS = this.ts;
943         var _this = this;
944         token.items.forEach(function(tar) {
945             _this.ts = new TokenStream(tar);
946             _this.parseScope(fnScope);
947             
948             
949         });
950         
951         //print(JSON.stringify(this.ts,null,4));
952         //this.parseScope(fnScope);
953         this.ts = outTS;
954         // now pop it off the stack!!!
955        
956         //this.braceNesting = b4braceNesting;
957         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
958         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
959         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
960     },
961     
962     protectScopeFromObfuscation : function(scope) {
963             //assert scope != null;
964         
965         if (scope == this.globalScope) {
966             // The global scope does not get obfuscated,
967             // so we don't need to worry about it...
968             return;
969         }
970
971         // Find the highest local scope containing the specified scope.
972         while (scope && scope.parent != this.globalScope) {
973             scope = scope.parent;
974         }
975
976         //assert scope.getParentScope() == globalScope;
977         scope.preventMunging();
978     },
979     
980     getIdentifier: function(symbol, scope, token) {
981         var identifier;
982         while (scope != false) {
983             identifier = scope.getIdentifier(symbol, token);
984             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
985             if (identifier) {
986                 return identifier;
987             }
988             scope = scope.parent;
989         }
990         return false;
991     }
992 };