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