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