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         
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         this.scopes.push(scope);
227        
228         //var scopeIndent = ''; 
229         //this.scopes.forEach(function() {
230         //    scopeIndent += '   '; 
231         //});
232         print(">> ENTER SCOPE");
233         
234         
235         
236         
237         token = this.ts.lookTok(1);
238         while (token) {
239           //  this.timerPrint("parseScope AFTER lookT: " + token.toString()); 
240             //this.dumpToken(token , this.scopes, this.braceNesting);
241             print('SCOPE:' + token.toString());
242             //this.log(token.data);
243             if (token.type == 'NAME') {
244             //    print('*' + token.data);
245             }
246             switch(token.type + '.' + token.name) {
247                 case "KEYW.VAR":
248                 case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
249                     print('SCOPE-VAR:' + token.toString());
250                     var vstart = this.ts.cursor +1;
251                     
252                     //this.log("parseScope GOT VAR/CONST : " + token.toString()); 
253                     while (true) {
254                         token = this.ts.nextTok();
255                         //!this.debug|| print( token.toString());
256                         print('SCOPE-VAR:' + token.toString());
257                         if (!token) { // can return false at EOF!
258                             break;
259                         }
260                         if (token.name == "VAR" || token.data == ',') { // kludge..
261                             continue;
262                         }
263                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
264                         if (token.type != "NAME") {
265                             for(var i = Math.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
266                                 print(this.ts.tokens[i].toString());
267                             }
268                             
269                             print( "var without ident");
270                             Seed.quit()
271                         }
272                         
273
274                         if (this.mode == "BUILDING_SYMBOL_TREE") {
275                             identifier = scope.getIdentifier(token.data) ;
276                             
277                             if (identifier == false) {
278                                 scope.declareIdentifier(token.data, token);
279                             } else {
280                                 token.identifier = identifier;
281                                 this.warn("(SCOPE) The variable " + token.data  + ' (line:' + token.line + ")  has already been declared in the same scope...");
282                             }
283                         }
284
285                         token = this.ts.nextTok();
286                         !this.debug|| print(token.toString());
287                         /*
288                         assert token.getType() == Token.SEMI ||
289                                 token.getType() == Token.ASSIGN ||
290                                 token.getType() == Token.COMMA ||
291                                 token.getType() == Token.IN;
292                         */
293                         if (token.name == "IN") {
294                             break;
295                         } else {
296                             //var bn = this.braceNesting;
297                             var bn = this.braceNesting;
298                             this.parseExpression();
299                             this.braceNesting = bn;
300                             //this.braceNesting = bn;
301                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
302                             
303                             token = this.ts.lookTok(1);
304                             !this.debug|| print("AFTER EXP: " + token.toString());
305                             if (token.data == ';') {
306                                 break;
307                             }
308                         }
309                     }
310                     
311                     //print("VAR:")
312                     //this.ts.dump(vstart , this.ts.cursor);
313                     
314                     break;
315                 case "KEYW.FUNCTION":
316                     print('SCOPE-FUNC:' + JSON.stringify(token,null,4));
317                     //println("<i>"+token.data+"</i>");
318                      var bn = this.braceNesting;
319                     this.parseFunctionDeclaration();
320                      this.braceNesting = bn;
321                     break;
322
323                 case "PUNC.LEFT_CURLY": // {
324                     
325                     //println("<i>"+token.data+"</i>");
326                     var curTS = this.ts;
327                     if (token.props) {
328                         
329                         for (var prop in token.props) {
330                             if (token.props[prop].val.data == 'function') {
331                                 // parse a function..
332                                 this.parseFunctProp(token.props[prop]);
333                                 continue;
334                             }
335                             // key value..
336                             
337                             this.ts = new TokenStream(token.props[prop].val);
338                             this.parseScope(scope);
339                             
340                         }
341                         this.ts = curTS;
342                         
343                         // it's an object literal..
344                         // the values could be replaced..
345                         break;
346                     }
347                     
348                     print('SCOPE-CURLY:' + JSON.stringify(token,null,4));
349                     var _this = this;
350                     token.items.forEach(function(expr) {
351                           _this.ts = new TokenStream(expr);
352                           _this.parseExpression(scope)
353                     });
354                     this.ts = curTS;
355                     //print("NOT PROPS"); Seed.quit();
356                     
357                     //isObjectLitAr.push(false);
358                     //this.braceNesting++;
359                     
360                     //print(">>>>>> OBJLIT PUSH(false)" + this.braceNesting);
361                     break;
362 /*
363                 case "PUNC.RIGHT_CURLY": // }
364                     //println("<i>"+token.data+"</i>");
365                     this.braceNesting--;
366                     isObjectLitAr.pop();
367                     //print(">>>>>> OBJLIT POP"+ this.braceNesting);
368                         //assert braceNesting >= scope.getBra ceNesting();
369                     
370                     if (this.braceNesting < expressionBraceNesting) {
371                         var ls = this.scopes.pop();
372                         ls.getUsedSymbols();
373                         // eat symbol if we are currently at { 
374                         if (this.ts.look(0).data == '{') {
375                             this.ts.nextTok();
376                         }
377                         
378                         print("<<<<<<<EXIT SCOPE" +this.scopes.length);
379                         return;
380                     }
381                     break;
382 */
383                 case "KEYW.WITH":
384                     print('SCOPE-WITH:' + token.toString());
385                     //println("<i>"+token.data+"</i>");   
386                     if (this.mode == "BUILDING_SYMBOL_TREE") {
387                         // Inside a 'with' block, it is impossible to figure out
388                         // statically whether a symbol is a local variable or an
389                         // object member. As a consequence, the only thing we can
390                         // do is turn the obfuscation off for the highest scope
391                         // containing the 'with' block.
392                         this.protectScopeFromObfuscation(scope);
393                         this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
394                     }
395                     break;
396
397                 case "KEYW.CATCH":
398                     print('SCOPE-CATCH:' + token.toString());
399                     //println("<i>"+token.data+"</i>");
400                     this.parseCatch();
401                     break;
402                 /*
403                 case Token.SPECIALCOMMENT:
404                         if (mode == BUILDING_SYMBOL_TREE) {
405                             protectScopeFromObfuscation(scope);
406                             this.warn("Using JScript conditional comments is not recommended." + (munge ? " Moreover, using JScript conditional comments reduces the level of compression." : ""), true);
407                         }
408                         break;
409                 */
410                 
411                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
412                 case "STRN.SINGLE_QUOTE":
413                     print('SCOPE-STRING:' + token.toString());
414                     //println("<i>"+token.data+"</i>");
415
416                     if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
417                         // then we are in an object lit.. -> we need to flag the brace as such...
418                         isObjectLitAr.pop();
419                         isObjectLitAr.push(true);
420                         //print(">>>>>> OBJLIT REPUSH(true)");
421                     }
422                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
423                     
424                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
425                         ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
426                         // see if we can replace..
427                         // remove the quotes..
428                         // should do a bit more checking!!!! (what about wierd char's in the string..
429                         var str = token.data.substring(1,token.data.length-1);
430                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
431                             token.outData = str;
432                         }
433                         
434                          
435                         
436                     }
437                     
438                     
439                     
440                     break;
441                 
442                 case "NAME.NAME":
443                     print('SCOPE-NAME:' + token.toString());
444                     //print("DEAL WITH NAME:");
445                     // got identifier..
446                     
447                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
448                     if ((this.ts.lookTok(-1).data == "{") && (this.ts.lookTok(1).data == ":")) {
449                         isObjectLitAr.pop();
450                         isObjectLitAr.push(true);
451                         //print(">>>>>> OBJLIT REPUSH(true)");
452                         //println("<i>"+token.data+"</i>");
453                         break;
454                     }
455                    // print("DEAL WITH obj lit:");
456                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
457                     
458                     if (isInObjectLitAr && (this.ts.lookTok(1).data == ":") && (this.ts.lookTok(-1).data == ",")) {
459                         // skip, it's an object lit key..
460                         //println("<i>"+token.data+"</i>");
461                         break;
462                     }
463                     
464                     
465                     // skip anyting with "." before it..!!
466                      
467                     if (this.ts.lookTok(-1).data == ".") {
468                         // skip, it's an object prop.
469                         //println("<i>"+token.data+"</i>");
470                         break;
471                     }
472                     //print("SYMBOL: " + token.toString());
473                     
474                     symbol = token.data;
475                     if (this.mode == 'PASS2_SYMBOL_TREE') {
476                         
477                         //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
478                         
479                         //print ("MUNGE?" + symbol);
480                         
481                         //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
482                              
483                             //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
484                         identifier = this.getIdentifier(symbol, scope);
485                         
486                         if (identifier == false) {
487 // BUG!find out where builtin is defined...
488                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
489                                 // Here, we found an undeclared and un-namespaced symbol that is
490                                 // 3 characters or less in length. Declare it in the global scope.
491                                 // We don't need to declare longer symbols since they won't cause
492                                 // any conflict with other munged symbols.
493                                 this.globalScope.declareIdentifier(symbol, token);
494                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
495                             }
496                             
497                             //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
498                         } else {
499                             token.identifier = identifier;
500                             identifier.refcount++;
501                         }
502                     }   
503                     
504                     break;
505                     //println("<B>SID</B>");
506                 default:
507                     if (token.type != 'KEYW') {
508                         break;
509                     }
510                     print('SCOPE-KEYW:' + token.toString());
511                    // print("Check eval:");
512                 
513                     symbol = token.data;
514                     
515                      if (this.mode == 'BUILDING_SYMBOL_TREE') {
516
517                         if (symbol == "eval") {
518                             // look back one and see if we can find a comment!!!
519                             if (this.ts.look(-1).type == "COMM") {
520                                 // look for eval:var:noreplace\n
521                                 var _t = this;
522                                 this.ts.look(-1).data.replace(/eval:var:([a-z_]+)/ig, function(m, a) {
523                                     
524                                     var hi = _t.getIdentifier(a, scope);
525                                    // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
526                                     if (hi) {
527                                      //   println("PROTECT "+a+" from munge");
528                                         hi.toMunge = false;
529                                     }
530                                     
531                                 });
532                                 
533                                 
534                             } else {
535                                 
536                             
537                                 this.protectScopeFromObfuscation(scope);
538                                 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);
539                             }
540
541                         }
542
543                     }
544                     break;
545                 
546                 
547             } // end switch
548             
549             
550             //print("parseScope TOK : " + token.toString()); 
551             token = this.ts.nextTok();
552             //if (this.ts.nextT()) break;
553             
554         }
555         //print("<<<<<<<EXIT SCOPE ERR?" +this.scopes.length);
556     },
557
558     expN : 0,
559     parseExpression : function() {
560
561         // Parse the expression until we encounter a comma or a semi-colon
562         // in the same brace nesting, bracket nesting and paren nesting.
563         // Parse functions if any...
564         //println("<i>EXP</i><BR/>");
565         !this.debug || print("PARSE EXPR");
566         this.expN++;
567          
568         // for printing stuff..
569        
570         
571         
572         var symbol;
573         var token;
574         var currentScope;
575         var identifier;
576
577         var expressionBraceNesting = this.braceNesting + 0;
578         var bracketNesting = 0;
579         var parensNesting = 0;
580         var isInObjectLitAr;
581         var isObjectLitAr = [ false ];
582         
583         currentScope = this.scopes[this.scopes.length-1];
584             
585         
586         //print(scopeIndent + ">> ENTER EXPRESSION" + this.expN);
587         while (token = this.ts.nextTok()) {
588      
589         
590             
591            /*
592             // moved out of loop?
593            currentScope = this.scopes[this.scopes.length-1];
594             
595             var scopeIndent = ''; 
596             this.scopes.forEach(function() {
597                 scopeIndent += '   '; 
598             });
599            */ 
600            
601            //this.dumpToken(token,  this.scopes, this.braceNesting );
602            print('EXP' +  token.toString());
603             
604             
605             //println("<i>"+token.data+"</i>");
606             //this.log("EXP:" + token.data);
607             switch (token.type) {
608                 case 'PUNC':
609                     print("EXPR-PUNC:" + JSON.stringify(token, null, 4));
610                     
611                     switch(token.data) {
612                          
613                         case ';':
614                         case ',':
615                             if (this.braceNesting == expressionBraceNesting &&
616                                     bracketNesting == 0 &&
617                                     parensNesting == 0) {
618                                 print("<< EXIT EXPRESSION");
619                                 this.expN--;
620                                 return;
621                             }
622                             break;
623
624                        
625
626                         case '{': //Token.LC:
627                             isObjectLitAr.push(false);
628                             
629                             this.braceNesting++;
630                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
631                             break;
632
633                         case '}': //Token.RC:
634                             this.braceNesting--;
635                             isObjectLitAr.pop();
636                             //print(">>>>> EXP POP" + this.braceNesting);    
637                            // assert braceNesting >= expressionBraceNesting;
638                             break;
639
640                         case '[': //Token.LB:
641                         
642                             var ts = this.ts;
643                             var _this = this;
644                             token.items.forEach(function(ns) {
645                                  print("EXPR-PUNC-MAKE:" + JSON.stringify(ns, null, 4));
646                                 _this.ts = new TokenStream(ns);
647                                 //_this.ts.cursor--;
648                                 _this.parseExpression();
649                             });
650                             this.ts = ts;
651                             
652                             break;
653  
654                         case ']': //Token.RB:
655                             return;
656                            
657  
658                         case '(': //Token.LP:
659                             parensNesting++;
660                             break;
661
662                         case ')': //Token.RP:
663                             parensNesting--;
664                             break;
665                     }
666                     break;
667                     
668                 case 'STRN': // used for object lit detection..
669                     print("EXPR-STR:" + JSON.stringify(token, null, 4));
670                     if (this.ts.lookTok(-1).data == "{" && this.ts.lookTok(1).data == ":" ) {
671                         // then we are in an object lit.. -> we need to flag the brace as such...
672                         isObjectLitAr.pop();
673                         isObjectLitAr.push(true);
674                         //print(">>>>> EXP PUSH(true)");
675                     }
676                     
677                     
678                      
679                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
680                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ":"  &&
681                         ( this.ts.lookTok(-1).data == "{"  ||  this.ts.lookTok(-1).data == "," )) {
682                         // see if we can replace..
683                         // remove the quotes..
684                         var str = token.data.substring(1,token.data.length-1);
685                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
686                             token.outData = str;
687                         }
688                         
689                          
690                         
691                     }
692                     
693                     break;
694                 
695                       
696              
697                 case 'NAME':
698                     print("EXPR-NAME:" + JSON.stringify(token, null, 4));
699                     symbol = token.data;
700                     //print("in NAME = " + token.toString());
701                     //print("in NAME 0: " + this.ts.look(0).toString());
702                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
703                     if (this.ts.look(0).data == "{"  && this.ts.lookTok(2).data == ":") {
704                         // then we are in an object lit.. -> we need to flag the brace as such...
705                         isObjectLitAr.pop();
706                         isObjectLitAr.push(true);
707                          //print(">>>>> EXP  PUSH(true)");
708                         break;
709                     }
710                     
711                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
712                     //print ("isInObjectLitAr : " + isInObjectLitAr + ' ' + token.toString());
713                     
714                     if (isInObjectLitAr && this.ts.lookTok(0).data == "," && this.ts.lookTok(2).data == ":") {
715                         break;
716                     }
717                     //print(this.ts.lookTok(0).data);
718                     if (this.ts.lookTok(0).data == ".") {
719                         //skip '.'
720                         break;
721                     }
722                     
723                      if (this.mode == 'PASS2_SYMBOL_TREE') {
724
725                         identifier = this.getIdentifier(symbol, currentScope);
726                         //println("<B>??</B>");
727                         if (identifier == false) {
728
729                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
730                                 // Here, we found an undeclared and un-namespaced symbol that is
731                                 // 3 characters or less in length. Declare it in the global scope.
732                                 // We don't need to declare longer symbols since they won't cause
733                                 // any conflict with other munged symbols.
734                                 this.globalScope.declareIdentifier(symbol, token);
735                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
736                             } else {
737                                 //println("undeclared")
738                             }
739                             
740                             
741                         } else {
742                             //println("<B>++</B>");
743                             token.identifier = identifier;
744                             identifier.refcount++;
745                         }
746                         
747                     }
748                     break;
749                     
750                     
751                     
752                     
753                     //println("<B>EID</B>");
754                 case 'KEYW':   
755                     print("EXPR-KEYW:" + JSON.stringify(token, null, 4));
756                     if (token.name == "FUNCTION") {
757                         
758                         this.parseFunctionDeclaration();
759                         break;
760                     }
761                
762                     
763              
764                     symbol = token.data;
765                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
766
767                         if (symbol == "eval") {
768                             if (this.ts.look(-1).type == 'COMM') {
769                                 // look for eval:var:noreplace\n
770                                 var _t = this;
771                                 this.ts.look(-1).data.replace(/eval:var:([a-z]+)/ig, function(m, a) {
772                                     var hi = _t.getIdentifier(a, currentScope);
773                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
774                                     if (hi) {
775                                       //  println("PROTECT "+a+" from munge");
776                                         hi.toMunge = false;
777                                     }
778                                     
779                                     
780                                 });
781                                 
782                             } else {
783                                 this.protectScopeFromObfuscation(currentScope);
784                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
785                             }
786                             
787
788                         }
789                         break;
790                     } 
791                 default: 
792                     print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
793                     break;
794             }
795             
796         }
797         print("<< EXIT EXPRESSION");
798         this.expN--;
799     },
800
801
802     parseCatch : function() {
803
804         var symbol;
805         var token;
806         var currentScope;
807         var identifier;
808
809         //token = getToken(-1);
810         //assert token.getType() == Token.CATCH;
811         token = this.ts.nextTok();
812         //assert token.getType() == Token.LP; (
813         token = this.ts.nextTok();
814         //assert token.getType() == Token.NAME;
815         
816         symbol = token.data;
817         currentScope = this.scopes[this.scopes.length-1];
818
819         if (this.mode == 'BUILDING_SYMBOL_TREE') {
820             // We must declare the exception identifier in the containing function
821             // scope to avoid errors related to the obfuscation process. No need to
822             // display a warning if the symbol was already declared here...
823             currentScope.declareIdentifier(symbol, token);
824         } else {
825             //?? why inc the refcount?? - that should be set when building the tree???
826             identifier = this.getIdentifier(symbol, currentScope);
827             identifier.refcount++;
828         }
829
830         token = this.ts.nextTok();
831         //assert token.getType() == Token.RP; // )
832     },
833     
834     parseFunctionDeclaration : function() 
835     {
836         //print("PARSE FUNCTION");
837         var symbol;
838         var token;
839         var currentScope  = false; 
840         var fnScope = false;
841         var identifier;
842         var b4braceNesting = this.braceNesting + 0;
843         
844         //this.logR("<B>PARSING FUNCTION</B>");
845         currentScope = this.scopes[this.scopes.length-1];
846
847         token = this.ts.nextTok();
848         if (token.type == "NAME") {
849             if (this.mode == 'BUILDING_SYMBOL_TREE') {
850                 // Get the name of the function and declare it in the current scope.
851                 symbol = token.data;
852                 if (currentScope.getIdentifier(symbol) != false) {
853                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
854                 }
855                 currentScope.declareIdentifier(symbol,token);
856             }
857             token =  this.ts.nextTok();
858         }
859
860         //assert token.getType() == Token.LP;
861         if (this.mode == 'BUILDING_SYMBOL_TREE') {
862             fnScope = new Scope(this.braceNesting, currentScope, token.n, '');
863             
864             //println("STORING SCOPE" + this.ts.cursor);
865             
866             this.indexedScopes[this.ts.cursor] = fnScope;
867             
868         } else {
869             //qln("FETCHING SCOPE" + this.ts.cursor);
870             fnScope = this.indexedScopes[this.ts.cursor];
871           
872         }
873         
874         print('FUNC-PARSE:' + JSON.stringify(token,null,4));
875         // Parse function arguments.
876         var args = token.items;
877         for (var argpos =0; argpos < args.length; argpos++) {
878              
879             token = args[argpos][0];
880            // print ("FUNC ARGS: " + token.toString())
881             //assert token.getType() == Token.NAME ||
882             //        token.getType() == Token.COMMA;
883             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
884                 symbol = token.data;
885                 identifier = fnScope.declareIdentifier(symbol,token);
886                 if (symbol == "$super" && argpos == 0) {
887                     // Exception for Prototype 1.6...
888                     identifier.preventMunging();
889                 }
890                 argpos++;
891             }
892         }
893         
894         token = this.ts.nextTok();
895         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
896         //Seed.quit();
897         //print(token.toString());
898         // assert token.getType() == Token.LC;
899         //this.braceNesting++;
900         
901         //token = this.ts.nextTok();
902         //print(token.toString());
903         var outTS = this.ts;
904         var _this = this;
905         token.items.forEach(function(tar) {
906             _this.ts = new TokenStream(tar);
907             _this.parseScope(fnScope);
908             
909         });
910         
911         //print(JSON.stringify(this.ts,null,4));
912         //this.parseScope(fnScope);
913         this.ts = outTS;
914         // now pop it off the stack!!!
915        
916         //this.braceNesting = b4braceNesting;
917         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
918         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
919         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
920     },
921     
922     protectScopeFromObfuscation : function(scope) {
923             //assert scope != null;
924         
925         if (scope == this.globalScope) {
926             // The global scope does not get obfuscated,
927             // so we don't need to worry about it...
928             return;
929         }
930
931         // Find the highest local scope containing the specified scope.
932         while (scope && scope.parent != this.globalScope) {
933             scope = scope.parent;
934         }
935
936         //assert scope.getParentScope() == globalScope;
937         scope.preventMunging();
938     },
939     
940     getIdentifier: function(symbol, scope) {
941         var identifier;
942         while (scope != false) {
943             identifier = scope.getIdentifier(symbol);
944             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
945             if (identifier) {
946                 return identifier;
947             }
948             scope = scope.parent;
949         }
950         return false;
951     }
952 };