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