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