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