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