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