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