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