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         print(">> ENTER EXPRESSION");
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         while (token = this.ts.lookTok()) {
510      
511
512             
513             currentScope = this.scopes[this.scopes.length-1];
514             
515             var scopeIndent = ''; 
516             this.scopes.forEach(function() {
517                 scopeIndent += '   '; 
518             });
519             
520             print(scopeIndent + 'OBJLIT.len='+ isObjectLitAr.length + ' ' + token.data);
521             
522             //println("<i>"+token.data+"</i>");
523             //this.log("EXP:" + token.data);
524             switch (token.type) {
525                 case 'PUNC':
526                     switch(token.data) {
527                          
528                         case ';':
529                         case ',':
530                             if (this.braceNesting == expressionBraceNesting &&
531                                     bracketNesting == 0 &&
532                                     parensNesting == 0) {
533                                 print(scopeIndent + "<< EXIT EXPRESSION");
534                                 return;
535                             }
536                             break;
537
538                        
539
540                         case '{': //Token.LC:
541                             isObjectLitAr.push(false);
542                             
543                             this.braceNesting++;
544                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
545                             break;
546
547                         case '}': //Token.RC:
548                             this.braceNesting--;
549                             isObjectLitAr.pop();
550                             //print(">>>>> EXP POP" + this.braceNesting);    
551                            // assert braceNesting >= expressionBraceNesting;
552                             break;
553
554                         case '[': //Token.LB:
555                             bracketNesting++;
556                             break;
557
558                         case ']': //Token.RB:
559                             bracketNesting--;
560                             break;
561
562                         case '(': //Token.LP:
563                             parensNesting++;
564                             break;
565
566                         case ')': //Token.RP:
567                             parensNesting--;
568                             break;
569                     }
570                     break;
571                     
572                 case 'STRN': // used for object lit detection..
573                     if (this.ts.lookTok(-1).data == "{" && this.ts.lookTok(1).data == ":" ) {
574                         // then we are in an object lit.. -> we need to flag the brace as such...
575                         isObjectLitAr.pop();
576                         isObjectLitAr.push(true);
577                         //print(">>>>> EXP PUSH(true)");
578                     }
579                     
580                     
581                      
582                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
583                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ":"  &&
584                         ( this.ts.lookTok(-1).data == "{"  ||  this.ts.lookTok(-1).data == "," )) {
585                         // see if we can replace..
586                         // remove the quotes..
587                         var str = token.data.substring(1,token.data.length-1);
588                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
589                             token.outData = str;
590                         }
591                         
592                          
593                         
594                     }
595                     
596                     break;
597                 
598                       
599              
600                 case 'NAME':
601                
602                     symbol = token.data;
603                     //print("in NAME = " + token.toString());
604                     //print("in NAME 0: " + this.ts.look(0).toString());
605                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
606                     if (this.ts.look(0).data == "{"  && this.ts.lookTok(2).data == ":") {
607                         // then we are in an object lit.. -> we need to flag the brace as such...
608                         isObjectLitAr.pop();
609                         isObjectLitAr.push(true);
610                          //print(">>>>> EXP  PUSH(true)");
611                         break;
612                     }
613                     
614                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
615                     //print ("isInObjectLitAr : " + isInObjectLitAr + ' ' + token.toString());
616                     
617                     if (isInObjectLitAr && this.ts.lookTok(0).data == "," && this.ts.lookTok(2).data == ":") {
618                         break;
619                     }
620                     //print(this.ts.lookTok(0).data);
621                     if (this.ts.lookTok(0).data == ".") {
622                         //skip '.'
623                         break;
624                     }
625                     
626                      if (this.mode == 'PASS2_SYMBOL_TREE') {
627
628                         identifier = this.getIdentifier(symbol, currentScope);
629                         //println("<B>??</B>");
630                         if (identifier == false) {
631
632                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
633                                 // Here, we found an undeclared and un-namespaced symbol that is
634                                 // 3 characters or less in length. Declare it in the global scope.
635                                 // We don't need to declare longer symbols since they won't cause
636                                 // any conflict with other munged symbols.
637                                 this.globalScope.declareIdentifier(symbol, token);
638                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
639                             } else {
640                                 //println("undeclared")
641                             }
642                             
643                             
644                         } else {
645                             //println("<B>++</B>");
646                             token.identifier = identifier;
647                             identifier.refcount++;
648                         }
649                         
650                     }
651                     break;
652                     
653                     
654                     
655                     
656                     //println("<B>EID</B>");
657                  case 'KEYW':   
658                  
659                     if (token.name == "FUNCTION") {
660                         
661                         this.parseFunctionDeclaration();
662                         break;
663                     }
664                
665                     
666              
667                     symbol = token.data;
668                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
669
670                         if (symbol == "eval") {
671                             if (this.ts.look(-1).type == 'COMM') {
672                                 // look for eval:var:noreplace\n
673                                 var _t = this;
674                                 this.ts.look(-1).data.replace(/eval:var:([a-z]+)/ig, function(m, a) {
675                                     var hi = _t.getIdentifier(a, currentScope);
676                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
677                                     if (hi) {
678                                       //  println("PROTECT "+a+" from munge");
679                                         hi.toMunge = false;
680                                     }
681                                     
682                                     
683                                 });
684                                 
685                             } else {
686                                 this.protectScopeFromObfuscation(currentScope);
687                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
688                             }
689                             
690
691                         }
692                         break;
693                     } 
694                    
695             }
696             if (!this.ts.nextTok()) break;
697         }
698     },
699
700
701     parseCatch : function() {
702
703         var symbol;
704         var token;
705         var currentScope;
706         var identifier;
707
708         //token = getToken(-1);
709         //assert token.getType() == Token.CATCH;
710         token = this.ts.nextTok();
711         //assert token.getType() == Token.LP; (
712         token = this.ts.nextTok();
713         //assert token.getType() == Token.NAME;
714         
715         symbol = token.data;
716         currentScope = this.scopes[this.scopes.length-1];
717
718         if (this.mode == 'BUILDING_SYMBOL_TREE') {
719             // We must declare the exception identifier in the containing function
720             // scope to avoid errors related to the obfuscation process. No need to
721             // display a warning if the symbol was already declared here...
722             currentScope.declareIdentifier(symbol, token);
723         } else {
724             //?? why inc the refcount?? - that should be set when building the tree???
725             identifier = this.getIdentifier(symbol, currentScope);
726             identifier.refcount++;
727         }
728
729         token = this.ts.nextTok();
730         //assert token.getType() == Token.RP; // )
731     },
732     
733     parseFunctionDeclaration : function() 
734     {
735        // print("PARSE FUNCTION");
736         var symbol;
737         var token;
738         var currentScope  = false; 
739         var fnScope = false;
740         var identifier;
741         //this.logR("<B>PARSING FUNCTION</B>");
742         currentScope = this.scopes[this.scopes.length-1];
743
744         token = this.ts.nextTok();
745         if (token.type == "NAME") {
746             if (this.mode == 'BUILDING_SYMBOL_TREE') {
747                 // Get the name of the function and declare it in the current scope.
748                 symbol = token.data;
749                 if (currentScope.getIdentifier(symbol) != false) {
750                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
751                 }
752                 currentScope.declareIdentifier(symbol,token);
753             }
754             token =  this.ts.nextTok();
755         }
756
757         //assert token.getType() == Token.LP;
758         if (this.mode == 'BUILDING_SYMBOL_TREE') {
759             fnScope = new Scope(this.braceNesting, currentScope, token.n, '');
760             
761             //println("STORING SCOPE" + this.ts.cursor);
762             
763             this.indexedScopes[this.ts.cursor] = fnScope;
764             
765         } else {
766             //qln("FETCHING SCOPE" + this.ts.cursor);
767             fnScope = this.indexedScopes[this.ts.cursor];
768           
769         }
770         
771         // Parse function arguments.
772         var argpos = 0;
773         while (this.ts.lookTok().data != ')') { //(token = consumeToken()).getType() != Token.RP) {
774             token = this.ts.nextTok();
775            // print ("FUNC ARGS: " + token.toString())
776             //assert token.getType() == Token.NAME ||
777             //        token.getType() == Token.COMMA;
778             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
779                 symbol = token.data;
780                 identifier = fnScope.declareIdentifier(symbol,token);
781                 if (symbol == "$super" && argpos == 0) {
782                     // Exception for Prototype 1.6...
783                     identifier.preventMunging();
784                 }
785                 argpos++;
786             }
787         }
788
789         token = this.ts.nextTok();
790         // assert token.getType() == Token.LC;
791         this.braceNesting++;
792
793         token = this.ts.nextTok();
794         if (token.type == "STRN" && this.ts.lookTok(1).data == ';') {
795             /*
796             
797             NOT SUPPORTED YET!?!!?!
798             
799             // This is a hint. Hints are empty statements that look like
800             // "localvar1:nomunge, localvar2:nomunge"; They allow developers
801             // to prevent specific symbols from getting obfuscated (some heretic
802             // implementations, such as Prototype 1.6, require specific variable
803             // names, such as $super for example, in order to work appropriately.
804             // Note: right now, only "nomunge" is supported in the right hand side
805             // of a hint. However, in the future, the right hand side may contain
806             // other values.
807             consumeToken();
808             String hints = token.getValue();
809             // Remove the leading and trailing quotes...
810             hints = hints.substring(1, hints.length() - 1).trim();
811             StringTokenizer st1 = new StringTokenizer(hints, ",");
812             while (st1.hasMoreTokens()) {
813                 String hint = st1.nextToken();
814                 int idx = hint.indexOf(':');
815                 if (idx <= 0 || idx >= hint.length() - 1) {
816                     if (mode == BUILDING_SYMBOL_TREE) {
817                         // No need to report the error twice, hence the test...
818                         this.warn("Invalid hint syntax: " + hint, true);
819                     }
820                     break;
821                 }
822                 String variableName = hint.substring(0, idx).trim();
823                 String variableType = hint.substring(idx + 1).trim();
824                 if (mode == BUILDING_SYMBOL_TREE) {
825                     fnScope.addHint(variableName, variableType);
826                 } else if (mode == CHECKING_SYMBOL_TREE) {
827                     identifier = fnScope.getIdentifier(variableName);
828                     if (identifier != null) {
829                         if (variableType.equals("nomunge")) {
830                             identifier.preventMunging();
831                         } else {
832                             this.warn("Unsupported hint value: " + hint, true);
833                         }
834                     } else {
835                         this.warn("Hint refers to an unknown identifier: " + hint, true);
836                     }
837                 }
838             }
839             */
840         }
841
842         this.parseScope(fnScope);
843         // now pop it off the stack!!!
844        
845         
846         
847     },
848     
849     protectScopeFromObfuscation : function(scope) {
850             //assert scope != null;
851         
852         if (scope == this.globalScope) {
853             // The global scope does not get obfuscated,
854             // so we don't need to worry about it...
855             return;
856         }
857
858         // Find the highest local scope containing the specified scope.
859         while (scope && scope.parent != this.globalScope) {
860             scope = scope.parent;
861         }
862
863         //assert scope.getParentScope() == globalScope;
864         scope.preventMunging();
865     },
866     
867     getIdentifier: function(symbol, scope) {
868         var identifier;
869         while (scope != false) {
870             identifier = scope.getIdentifier(symbol);
871             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
872             if (identifier) {
873                 return identifier;
874             }
875             scope = scope.parent;
876         }
877         return false;
878     }
879 };