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