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