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