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