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                             if (token.prefix && token.prefix.match('/eval/')) {
526                                 // look for eval:var:noreplace\n
527                                 var _t = this;
528                                 token.prefix.replace(/eval:var:([a-z_]+)/ig, function(m, a) {
529                                     
530                                     var hi = _t.getIdentifier(a, thisScope, token);
531                                    // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
532                                     if (hi) {
533                                      //   println("PROTECT "+a+" from munge");
534                                         hi.toMunge = false;
535                                     }
536                                     
537                                 });
538                                 
539                                 
540                             } else {
541                                 
542                             
543                                 this.protectScopeFromObfuscation(thisScope);
544                                 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);
545                             }
546
547                         }
548
549                     }
550                     break;
551                 
552                 
553             } // end switch
554             
555             
556             //print("parseScope TOK : " + token.toString()); 
557             token = this.ts.nextTok();
558             //if (this.ts.nextT()) break;
559             
560         }
561         //print("<<< EXIT SCOPE");
562         //print("<<<<<<<EXIT SCOPE ERR?" +this.scopes.length);
563     },
564
565     expN : 0,
566     parseExpression : function() {
567
568         // Parse the expression until we encounter a comma or a semi-colon
569         // in the same brace nesting, bracket nesting and paren nesting.
570         // Parse functions if any...
571         //println("<i>EXP</i><BR/>");
572         !this.debug || print("PARSE EXPR");
573         this.expN++;
574          
575         // for printing stuff..
576        
577         
578         
579         var symbol;
580         var token;
581         var currentScope;
582         var identifier;
583
584         var expressionBraceNesting = this.braceNesting + 0;
585         var bracketNesting = 0;
586         var parensNesting = 0;
587         var isInObjectLitAr;
588         var isObjectLitAr = [ false ];
589         
590         currentScope = this.scopes[this.scopes.length-1];
591             
592         
593         //print(scopeIndent + ">> ENTER EXPRESSION" + this.expN);
594         while (token = this.ts.nextTok()) {
595      
596         
597             
598            /*
599             // moved out of loop?
600            currentScope = this.scopes[this.scopes.length-1];
601             
602             var scopeIndent = ''; 
603             this.scopes.forEach(function() {
604                 scopeIndent += '   '; 
605             });
606            */ 
607            
608            //this.dumpToken(token,  this.scopes, this.braceNesting );
609            //print('EXP' +  token.toString());
610             
611             
612             //println("<i>"+token.data+"</i>");
613             //this.log("EXP:" + token.data);
614             switch (token.type) {
615                 case 'PUNC':
616                     //print("EXPR-PUNC:" + token.toString());
617                     
618                     switch(token.data) {
619                          
620                         case ';':
621                             //print("<< EXIT EXPRESSION");
622                             break;
623
624                         case ',':
625                             
626                             break;
627
628                        
629                         case '(': //Token.LP:
630                         case '{': //Token.LC:
631                         case '[': //Token.LB:
632                             //print('SCOPE-CURLY/PAREN/BRACE:' + token.toString());
633                            // print('SCOPE-CURLY/PAREN/BRACE:' + JSON.stringify(token, null,4));
634                             //println("<i>"+token.data+"</i>");
635                             var curTS = this.ts;
636                             if (token.props) {
637                                 
638                                 for (var prop in token.props) {
639                                     if (token.props[prop].val[0].data == 'function') {
640                                         // parse a function..
641                                         this.ts = new TokenStream(token.props[prop].val);
642                                         this.ts.nextTok();
643                                         this.parseFunctionDeclaration();
644                                         continue;
645                                     }
646                                     // key value..
647                                     
648                                     this.ts = new TokenStream(token.props[prop].val);
649                                     this.parseExpression();
650                                     
651                                 }
652                                 this.ts = curTS;
653                                 
654                                 // it's an object literal..
655                                 // the values could be replaced..
656                                 break;
657                             }
658                             
659                             
660                             var _this = this;
661                             token.items.forEach(function(expr) {
662                                   _this.ts = new TokenStream(expr);
663                                   _this.parseExpression()
664                             });
665                             this.ts = curTS;
666                         
667                         
668                     
669                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
670                             break;
671
672                        
673                         
674                          
675                             
676                         case ')': //Token.RP:
677                         case ']': //Token.RB:
678                         case '}': //Token.RB:
679                             //print("<< EXIT EXPRESSION");
680                             return;
681                            
682  
683              
684                             parensNesting++;
685                             break;
686
687                         
688                             
689                     }
690                     break;
691                     
692                 case 'STRN': // used for object lit detection..
693                     //if (this.mode == 'BUILDING_SYMBOL_TREE')    
694                         //print("EXPR-STR:" + JSON.stringify(token, null, 4));
695                
696                      
697                     break;
698                 
699                       
700              
701                 case 'NAME':
702                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
703                         
704                         //print("EXPR-NAME:" + JSON.stringify(token, null, 4));
705                     } else {
706                         //print("EXPR-NAME:" + token.toString());
707                     }
708                     symbol = token.data;
709                     //print("in NAME = " + token.toString());
710                     //print("in NAME 0: " + this.ts.look(0).toString());
711                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
712                     
713                     //print(this.ts.lookTok(-1).data);
714                     // prefixed with '.'
715                     if (this.ts.lookTok(-1).data == ".") {
716                         //skip '.'
717                         break;
718                     }
719                     if (symbol == 'this') {
720                         break;
721                        }
722                     
723                     if (this.mode == 'PASS2_SYMBOL_TREE') {
724
725                         identifier = this.getIdentifier(symbol, currentScope, token);
726                         //println("<B>??</B>");
727                         if (identifier == false) {
728
729                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
730                                 // Here, we found an undeclared and un-namespaced symbol that is
731                                 // 3 characters or less in length. Declare it in the global scope.
732                                 // We don't need to declare longer symbols since they won't cause
733                                 // any conflict with other munged symbols.
734                                 this.globalScope.declareIdentifier(symbol, token);
735                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
736                                 //print("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')');
737                                 //throw "OOPS";
738                             } else {
739                                 //print("undeclared:" + token.toString())
740                             }
741                             
742                             
743                         } else {
744                             //println("<B>++</B>");
745                             token.identifier = identifier;
746                             identifier.refcount++;
747                         }
748                         
749                     }
750                     break;
751                     
752                     
753                     
754                     
755                     //println("<B>EID</B>");
756                 case 'KEYW':   
757                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
758                     //    print("EXPR-KEYW:" + JSON.stringify(token, null, 4));
759                     if (token.name == "FUNCTION") {
760                         
761                         this.parseFunctionDeclaration();
762                         break;
763                     }
764                
765                     
766              
767                     symbol = token.data;
768                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
769
770                         if (symbol == "eval") {
771                             if (token.prefix && token.prefix.match('/eval/')) {
772                                 // look for eval:var:noreplace\n
773                                 var _t = this;
774                                 token.prefix.replace(/eval:var:([a-z]+)/ig, function(m, a) {
775                                     var hi = _t.getIdentifier(a, currentScope, token);
776                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
777                                     if (hi) {
778                                       //  println("PROTECT "+a+" from munge");
779                                         hi.toMunge = false;
780                                     }
781                                     
782                                     
783                                 });
784                                 
785                             } else {
786                                 this.protectScopeFromObfuscation(currentScope);
787                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
788                             }
789                             
790
791                         }
792                         break;
793                     } 
794                 default:
795                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
796                     //    print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
797                     break;
798             }
799             
800         }
801         //print("<< EXIT EXPRESSION");
802         this.expN--;
803     },
804
805
806     parseCatch : function() {
807
808         var symbol;
809         var token;
810         var currentScope;
811         var identifier;
812
813         //token = getToken(-1);
814         //assert token.getType() == Token.CATCH;
815         token = this.ts.nextTok();
816         //assert token.getType() == Token.LP; (
817         //token = this.ts.nextTok();
818         //assert token.getType() == Token.NAME;
819         
820         symbol = token.items[0].data;
821         currentScope = this.scopes[this.scopes.length-1];
822
823         if (this.mode == 'BUILDING_SYMBOL_TREE') {
824             // We must declare the exception identifier in the containing function
825             // scope to avoid errors related to the obfuscation process. No need to
826             // display a warning if the symbol was already declared here...
827             currentScope.declareIdentifier(symbol, token);
828         } else {
829             //?? why inc the refcount?? - that should be set when building the tree???
830             identifier = this.getIdentifier(symbol, currentScope, token);
831             identifier.refcount++;
832         }
833
834         token = this.ts.nextTok();
835         //assert token.getType() == Token.RP; // )
836     },
837     
838     parseFunctionDeclaration : function() 
839     {
840         //print("PARSE FUNCTION");
841         var symbol;
842         var token;
843         var currentScope  = false; 
844         var fnScope = false;
845         var identifier;
846         var b4braceNesting = this.braceNesting + 0;
847         
848         //this.logR("<B>PARSING FUNCTION</B>");
849         currentScope = this.scopes[this.scopes.length-1];
850
851         token = this.ts.nextTok();
852         if (token.type == "NAME") {
853             if (this.mode == 'BUILDING_SYMBOL_TREE') {
854                 // Get the name of the function and declare it in the current scope.
855                 symbol = token.data;
856                 if (currentScope.getIdentifier(symbol,token) != false) {
857                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
858                 }
859                 currentScope.declareIdentifier(symbol,token);
860             }
861             token =  this.ts.nextTok();
862         }
863         // return function() {.... 
864         if (token.name == "RETURN") {
865             token =  this.ts.nextTok();
866         }
867
868         //assert token.getType() == Token.LP;
869         if (this.mode == 'BUILDING_SYMBOL_TREE') {
870             fnScope = new Scope(1, currentScope, token.n, '', token);
871             
872             //println("STORING SCOPE" + this.ts.cursor);
873             
874             this.indexedScopes[token.id] = fnScope;
875             
876         } else {
877             //qln("FETCHING SCOPE" + this.ts.cursor);
878             fnScope = this.indexedScopes[token.id];
879         }
880         //if (this.mode == 'BUILDING_SYMBOL_TREE') 
881             print('FUNC-PARSE:' + JSON.stringify(token,null,4));
882         // Parse function arguments.
883         var args = token.items;
884         for (var argpos =0; argpos < args.length; argpos++) {
885              
886             token = args[argpos][0];
887             //print ("FUNC ARGS: " + token.toString())
888             //assert token.getType() == Token.NAME ||
889             //        token.getType() == Token.COMMA;
890             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
891                 symbol = token.data;
892                 identifier = fnScope.declareIdentifier(symbol,token);
893                 if (symbol == "$super" && argpos == 0) {
894                     // Exception for Prototype 1.6...
895                     identifier.preventMunging();
896                 }
897                 //argpos++;
898             }
899         }
900         
901         token = this.ts.nextTok();
902         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
903         //Seed.quit();
904         //print(token.toString());
905         // assert token.getType() == Token.LC;
906         //this.braceNesting++;
907         
908         //token = this.ts.nextTok();
909         //print(token.toString());
910         var outTS = this.ts;
911         var _this = this;
912         token.items.forEach(function(tar) {
913             _this.ts = new TokenStream(tar);
914             _this.parseScope(fnScope);
915             
916             
917         });
918         
919         //print(JSON.stringify(this.ts,null,4));
920         //this.parseScope(fnScope);
921         this.ts = outTS;
922         // now pop it off the stack!!!
923        
924         //this.braceNesting = b4braceNesting;
925         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
926         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
927         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
928     },
929     
930     protectScopeFromObfuscation : function(scope) {
931             //assert scope != null;
932         
933         if (scope == this.globalScope) {
934             // The global scope does not get obfuscated,
935             // so we don't need to worry about it...
936             return;
937         }
938
939         // Find the highest local scope containing the specified scope.
940         while (scope && scope.parent != this.globalScope) {
941             scope = scope.parent;
942         }
943
944         //assert scope.getParentScope() == globalScope;
945         scope.preventMunging();
946     },
947     
948     getIdentifier: function(symbol, scope, token) {
949         var identifier;
950         while (scope != false) {
951             identifier = scope.getIdentifier(symbol, token);
952             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
953             if (identifier) {
954                 return identifier;
955             }
956             scope = scope.parent;
957         }
958         return false;
959     }
960 };