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