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