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