JSDOC/ScopeParser.js
[gnome.introspection-doc-generator] / JSDOC / ScopeParser.js
1 //<Script type="text/javascript">
2
3 Scope = imports['JSDOC/Scope.js'].Scope;
4
5 /**
6 * Scope stuff
7
8 * // FIXME - I need this to do next() without doccomments..
9 */
10
11 ScopeParser = function(ts) {
12     this.ts = ts; // {TokenStream}
13     this.warnings = [];
14     this.scopes = [];
15     this.indexedScopes = {};
16     this.timer = new Date() * 1;
17     this.debug = false;
18 }
19
20 // list of keywords that should not be used in object literals.
21 ScopeParser.idents = [
22         "break",         
23         "case",          
24         "continue",     
25         "default",      
26         "delete",       
27         "do",            
28         "else",         
29         "export",       
30         "false",        
31         "for",          
32         "function",     
33         "if",           
34         "import",       
35         "in",           
36         "new",          
37         "null",         
38         "return",       
39         "switch",       
40         "this",         
41         "true",         
42         "typeof",       
43         "var",          
44         "void",         
45         "while",        
46         "with",         
47
48         "catch",        
49         "class",        
50         "const",        
51         "debugger",     
52         "enum",         
53         "extends",      
54         "finally",      
55         "super",        
56         "throw",         
57         "try",          
58
59         "abstract",     
60         "boolean",      
61         "byte",         
62         "char",         
63         "double",       
64         "final",        
65         "float",        
66         "goto",         
67         "implements", 
68         "instanceof",
69         "int",           
70         "interface",     
71         "long",          
72         "native",       
73         "package",      
74         "private",      
75         "protected",     
76         "public",        
77         "short",        
78         "static",       
79         "synchronized",  
80         "throws",        
81         "transient",     
82                 "include",       
83                 "undefined"
84 ];
85
86
87 ScopeParser.prototype = {
88     timer: 0,
89     timerPrint: function (str) {
90         var ntime = new Date() * 1;
91         var tdif =  ntime -this.timer;
92         this.timer = ntime;
93         var pref = '';
94         if (tdif > 100) { //slower ones..
95             pref = '***';
96         }
97         println(pref+'['+tdif+']'+str);
98         
99     },
100     warn: function(s) {
101         //print('****************' + s);
102         this.warnings.push(s);
103         //println("WARNING:" + htmlescape(s) + "<BR>");
104     },
105     // defaults should not be initialized here =- otherwise they get duped on new, rather than initalized..
106     warnings : false,
107     ts : false,
108     scopes : false,
109     global : false,
110     mode : "", //"BUILDING_SYMBOL_TREE",
111     braceNesting : 0,
112     indexedScopes : false,
113     munge: true,
114
115
116
117
118
119     buildSymbolTree : function()
120     {
121         //println("<PRE>");
122         
123         this.ts.rewind();
124         this.braceNesting = 0;
125         this.scopes = [];
126         
127         
128         
129         
130         this.globalScope = new  Scope(-1, false, -1, '');
131         indexedScopes = { 0 : this.globalScope };
132         
133         this.mode = 'BUILDING_SYMBOL_TREE';
134         this.parseScope(this.globalScope);
135         
136         //print("---------------END PASS 1 ---------------- ");
137         
138     },
139     mungeSymboltree : function()
140     {
141
142         if (!this.munge) {
143             return;
144         }
145
146         // One problem with obfuscation resides in the use of undeclared
147         // and un-namespaced global symbols that are 3 characters or less
148         // in length. Here is an example:
149         //
150         //     var declaredGlobalVar;
151         //
152         //     function declaredGlobalFn() {
153         //         var localvar;
154         //         localvar = abc; // abc is an undeclared global symbol
155         //     }
156         //
157         // In the example above, there is a slim chance that localvar may be
158         // munged to 'abc', conflicting with the undeclared global symbol
159         // abc, creating a potential bug. The following code detects such
160         // global symbols. This must be done AFTER the entire file has been
161         // parsed, and BEFORE munging the symbol tree. Note that declaring
162         // extra symbols in the global scope won't hurt.
163         //
164         // Note: Since we go through all the tokens to do this, we also use
165         // the opportunity to count how many times each identifier is used.
166
167         this.ts.rewind();
168         this.braceNesting = 0;
169         this.scopes= [];
170         this.mode = 'PASS2_SYMBOL_TREE';
171         
172         //println("MUNGING?");
173         
174         this.parseScope(this.globalScope);
175         this.globalScope.munge();
176     },
177
178
179     log : function(str)
180     {
181         print ("                    ".substring(0, this.braceNesting*2) + str);
182         
183         //println("<B>LOG:</B>" + htmlescape(str) + "<BR/>\n");
184     },
185     logR : function(str)
186     {
187             //println("<B>LOG:</B>" + str + "<BR/>");
188     },
189
190
191
192
193
194
195     parseScope : function(scope) // parse a token stream..
196     {
197         //this.timerPrint("parseScope EnterScope"); 
198         //this.log(">>> ENTER SCOPE" + this.scopes.length);
199         var symbol;
200         var token;
201         
202         var identifier;
203
204         var expressionBraceNesting = this.braceNesting;
205         
206         var parensNesting = 0;
207         
208         var isObjectLitAr = [ false ];
209         var isInObjectLitAr;
210         this.scopes.push(scope);
211        
212         
213         token = this.ts.lookTok(1);
214         while (token) {
215           //  this.timerPrint("parseScope AFTER lookT: " + token.toString()); 
216              //print(scopeIndent + 'OBJLIT.len='+ isObjectLitAr.length + ' ' + token.toString());
217             //this.log(token.data);
218             if (token.type == 'NAME') {
219             //    print('*' + token.data);
220             }
221             switch(token.type + '.' + token.name) {
222                 case "KEYW.VAR":
223                 case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
224                     
225                     //this.log("parseScope GOT VAR/CONST : " + token.toString()); 
226                     while (true) {
227                         token = this.ts.nextTok();
228                         !this.debug|| print( token.toString());
229                       
230                         if (!token) { // can return false at EOF!
231                             break;
232                         }
233                         if (token.name == "VAR" || token.data == ',') { // kludge..
234                             continue;
235                         }
236                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
237                         if (token.type !="NAME") {
238                             for(var i = Math.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
239                                 print(this.ts.tokens[i].toString());
240                             }
241                             
242                             print( "var without ident");
243                             Seed.quit()
244                         }
245                         
246
247                         if (this.mode == "BUILDING_SYMBOL_TREE") {
248                             identifier = scope.getIdentifier(token.data) ;
249                             
250                             if (identifier == false) {
251                                 scope.declareIdentifier(token.data, token);
252                             } else {
253                                 token.identifier = identifier;
254                                 this.warn("(SCOPE) The variable " + token.data  + ' (line:' + token.line + ")  has already been declared in the same scope...");
255                             }
256                         }
257
258                         token = this.ts.nextTok();
259                         !this.debug|| print(token.toString());
260                         /*
261                         assert token.getType() == Token.SEMI ||
262                                 token.getType() == Token.ASSIGN ||
263                                 token.getType() == Token.COMMA ||
264                                 token.getType() == Token.IN;
265                         */
266                         if (token.name == "IN") {
267                             break;
268                         } else {
269                             //var bn = this.braceNesting;
270                             this.parseExpression();
271                             //this.braceNesting = bn;
272                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
273                             
274                             token = this.ts.lookTok(1);
275                             !this.debug|| print("AFTER EXP: " + token.toString());
276                             if (token.data == ';') {
277                                 break;
278                             }
279                         }
280                     }
281                     break;
282                 case "KEYW.FUNCTION":
283                     //println("<i>"+token.data+"</i>");
284                      var bn = this.braceNesting;
285                     this.parseFunctionDeclaration();
286                      this.braceNesting = bn;
287                     break;
288
289                 case "PUNC.LEFT_CURLY": // {
290                     //println("<i>"+token.data+"</i>");
291                     isObjectLitAr.push(false);
292                     this.braceNesting++;
293                     
294                     //print(">>>>>> OBJLIT PUSH(false)" + this.braceNesting);
295                     break;
296
297                 case "PUNC.RIGHT_CURLY": // }
298                     //println("<i>"+token.data+"</i>");
299                     this.braceNesting--;
300                     isObjectLitAr.pop();
301                     //print(">>>>>> OBJLIT POP"+ this.braceNesting);
302                         //assert braceNesting >= scope.getBra ceNesting();
303                     
304                     if (this.braceNesting < expressionBraceNesting) {
305                         var ls = this.scopes.pop();
306                         ls.getUsedSymbols();
307                         // eat symbol if we are currently at { 
308                         if (this.ts.look(0).data == '{') {
309                             this.ts.nextTok();
310                         }
311                         
312                         //print("<<<<<<<EXIT SCOPE" +this.scopes.length);
313                         return;
314                     }
315                     break;
316
317                 case "KEYW.WITH":
318                     //println("<i>"+token.data+"</i>");   
319                     if (this.mode == "BUILDING_SYMBOL_TREE") {
320                         // Inside a 'with' block, it is impossible to figure out
321                         // statically whether a symbol is a local variable or an
322                         // object member. As a consequence, the only thing we can
323                         // do is turn the obfuscation off for the highest scope
324                         // containing the 'with' block.
325                         this.protectScopeFromObfuscation(scope);
326                         this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
327                     }
328                     break;
329
330                 case "KEYW.CATCH":
331                     //println("<i>"+token.data+"</i>");
332                     this.parseCatch();
333                     break;
334                 /*
335                 case Token.SPECIALCOMMENT:
336                         if (mode == BUILDING_SYMBOL_TREE) {
337                             protectScopeFromObfuscation(scope);
338                             this.warn("Using JScript conditional comments is not recommended." + (munge ? " Moreover, using JScript conditional comments reduces the level of compression." : ""), true);
339                         }
340                         break;
341                 */
342                 
343                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
344                 case "STRN.SINGLE_QUOTE":
345                     //println("<i>"+token.data+"</i>");
346                     if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
347                         // then we are in an object lit.. -> we need to flag the brace as such...
348                         isObjectLitAr.pop();
349                         isObjectLitAr.push(true);
350                         //print(">>>>>> OBJLIT REPUSH(true)");
351                     }
352                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
353                     
354                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
355                         ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
356                         // see if we can replace..
357                         // remove the quotes..
358                         // should do a bit more checking!!!! (what about wierd char's in the string..
359                         var str = token.data.substring(1,token.data.length-1);
360                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
361                             token.outData = str;
362                         }
363                         
364                          
365                         
366                     }
367                     
368                     
369                     
370                     break;
371                 
372                 case "NAME.NAME":
373                 
374                     //print("DEAL WITH NAME:");
375                     // got identifier..
376                     
377                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
378                     if ((this.ts.lookTok(-1).data == "{") && (this.ts.lookTok(1).data == ":")) {
379                         isObjectLitAr.pop();
380                         isObjectLitAr.push(true);
381                         //print(">>>>>> OBJLIT REPUSH(true)");
382                         //println("<i>"+token.data+"</i>");
383                         break;
384                     }
385                    // print("DEAL WITH obj lit:");
386                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
387                     
388                     if (isInObjectLitAr && (this.ts.lookTok(1).data == ":") && (this.ts.lookTok(-1).data == ",")) {
389                         // skip, it's an object lit key..
390                         //println("<i>"+token.data+"</i>");
391                         break;
392                     }
393                     
394                     
395                     // skip anyting with "." before it..!!
396                      
397                     if (this.ts.lookTok(-1).data == ".") {
398                         // skip, it's an object prop.
399                         //println("<i>"+token.data+"</i>");
400                         break;
401                     }
402                     symbol = token.data;
403                     if (this.mode == 'PASS2_SYMBOL_TREE') {
404                         
405                         //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
406                         
407                         //print ("MUNGE?" + symbol);
408                         
409                         //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
410                              
411                             //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
412                         identifier = this.getIdentifier(symbol, scope);
413                         
414                         if (identifier == false) {
415 // BUG!find out where builtin is defined...
416                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
417                                 // Here, we found an undeclared and un-namespaced symbol that is
418                                 // 3 characters or less in length. Declare it in the global scope.
419                                 // We don't need to declare longer symbols since they won't cause
420                                 // any conflict with other munged symbols.
421                                 this.globalScope.declareIdentifier(symbol, token);
422                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
423                             }
424                             
425                             //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
426                         } else {
427                             token.identifier = identifier;
428                             identifier.refcount++;
429                         }
430                     }   
431                     
432                     break;
433                     //println("<B>SID</B>");
434                 default:
435                     if (token.type != 'KEYW') {
436                         break;
437                     }
438                    // print("Check eval:");
439                 
440                     symbol = token.data;
441                     
442                      if (this.mode == 'BUILDING_SYMBOL_TREE') {
443
444                         if (symbol == "eval") {
445                             // look back one and see if we can find a comment!!!
446                             if (this.ts.look(-1).type == "COMM") {
447                                 // look for eval:var:noreplace\n
448                                 var _t = this;
449                                 this.ts.look(-1).data.replace(/eval:var:([a-z_]+)/ig, function(m, a) {
450                                     
451                                     var hi = _t.getIdentifier(a, scope);
452                                    // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
453                                     if (hi) {
454                                      //   println("PROTECT "+a+" from munge");
455                                         hi.toMunge = false;
456                                     }
457                                     
458                                 });
459                                 
460                                 
461                             } else {
462                                 
463                             
464                                 this.protectScopeFromObfuscation(scope);
465                                 this.warn("Using 'eval' is not recommended. (use  eval:var:noreplace in comments to optimize) " + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
466                             }
467
468                         }
469
470                     }
471                     break;
472                 
473                 
474             } // end switch
475             
476             
477             //this.timerPrint("parseScope TOK : " + token.toString()); 
478             token = this.ts.nextTok();
479             //if (this.ts.nextT()) break;
480             
481         }
482         //print("<<<<<<<EXIT SCOPE ERR?" +this.scopes.length);
483     },
484
485
486     parseExpression : function() {
487
488         // Parse the expression until we encounter a comma or a semi-colon
489         // in the same brace nesting, bracket nesting and paren nesting.
490         // Parse functions if any...
491         //println("<i>EXP</i><BR/>");
492         !this.debug || print("PARSE EXPR");
493         print(">> ENTER EXPRESSION");
494          
495         // for printing stuff..
496        
497         
498         
499         var symbol;
500         var token;
501         var currentScope;
502         var identifier;
503
504         var expressionBraceNesting = this.braceNesting;
505         var bracketNesting = 0;
506         var parensNesting = 0;
507         var isInObjectLitAr;
508         var isObjectLitAr = [ false ];
509         
510         currentScope = this.scopes[this.scopes.length-1];
511             
512             var scopeIndent = ''; 
513             this.scopes.forEach(function() {
514                 scopeIndent += '   '; 
515             });
516         
517         while (token = this.ts.lookTok()) {
518      
519
520             
521            /*
522             // moved out of loop?
523            
524            */ 
525             print(scopeIndent + 'OBJLIT.len='+ isObjectLitAr.length + ' ' + token.data);
526             
527             //println("<i>"+token.data+"</i>");
528             //this.log("EXP:" + token.data);
529             switch (token.type) {
530                 case 'PUNC':
531                     switch(token.data) {
532                          
533                         case ';':
534                         case ',':
535                             if (this.braceNesting == expressionBraceNesting &&
536                                     bracketNesting == 0 &&
537                                     parensNesting == 0) {
538                                 print(scopeIndent + "<< EXIT EXPRESSION");
539                                 return;
540                             }
541                             break;
542
543                        
544
545                         case '{': //Token.LC:
546                             isObjectLitAr.push(false);
547                             
548                             this.braceNesting++;
549                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
550                             break;
551
552                         case '}': //Token.RC:
553                             this.braceNesting--;
554                             isObjectLitAr.pop();
555                             //print(">>>>> EXP POP" + this.braceNesting);    
556                            // assert braceNesting >= expressionBraceNesting;
557                             break;
558
559                         case '[': //Token.LB:
560                             bracketNesting++;
561                             break;
562
563                         case ']': //Token.RB:
564                             bracketNesting--;
565                             break;
566
567                         case '(': //Token.LP:
568                             parensNesting++;
569                             break;
570
571                         case ')': //Token.RP:
572                             parensNesting--;
573                             break;
574                     }
575                     break;
576                     
577                 case 'STRN': // used for object lit detection..
578                     if (this.ts.lookTok(-1).data == "{" && this.ts.lookTok(1).data == ":" ) {
579                         // then we are in an object lit.. -> we need to flag the brace as such...
580                         isObjectLitAr.pop();
581                         isObjectLitAr.push(true);
582                         //print(">>>>> EXP PUSH(true)");
583                     }
584                     
585                     
586                      
587                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
588                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ":"  &&
589                         ( this.ts.lookTok(-1).data == "{"  ||  this.ts.lookTok(-1).data == "," )) {
590                         // see if we can replace..
591                         // remove the quotes..
592                         var str = token.data.substring(1,token.data.length-1);
593                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
594                             token.outData = str;
595                         }
596                         
597                          
598                         
599                     }
600                     
601                     break;
602                 
603                       
604              
605                 case 'NAME':
606                
607                     symbol = token.data;
608                     //print("in NAME = " + token.toString());
609                     //print("in NAME 0: " + this.ts.look(0).toString());
610                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
611                     if (this.ts.look(0).data == "{"  && this.ts.lookTok(2).data == ":") {
612                         // then we are in an object lit.. -> we need to flag the brace as such...
613                         isObjectLitAr.pop();
614                         isObjectLitAr.push(true);
615                          //print(">>>>> EXP  PUSH(true)");
616                         break;
617                     }
618                     
619                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
620                     //print ("isInObjectLitAr : " + isInObjectLitAr + ' ' + token.toString());
621                     
622                     if (isInObjectLitAr && this.ts.lookTok(0).data == "," && this.ts.lookTok(2).data == ":") {
623                         break;
624                     }
625                     //print(this.ts.lookTok(0).data);
626                     if (this.ts.lookTok(0).data == ".") {
627                         //skip '.'
628                         break;
629                     }
630                     
631                      if (this.mode == 'PASS2_SYMBOL_TREE') {
632
633                         identifier = this.getIdentifier(symbol, currentScope);
634                         //println("<B>??</B>");
635                         if (identifier == false) {
636
637                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
638                                 // Here, we found an undeclared and un-namespaced symbol that is
639                                 // 3 characters or less in length. Declare it in the global scope.
640                                 // We don't need to declare longer symbols since they won't cause
641                                 // any conflict with other munged symbols.
642                                 this.globalScope.declareIdentifier(symbol, token);
643                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
644                             } else {
645                                 //println("undeclared")
646                             }
647                             
648                             
649                         } else {
650                             //println("<B>++</B>");
651                             token.identifier = identifier;
652                             identifier.refcount++;
653                         }
654                         
655                     }
656                     break;
657                     
658                     
659                     
660                     
661                     //println("<B>EID</B>");
662                  case 'KEYW':   
663                  
664                     if (token.name == "FUNCTION") {
665                         
666                         this.parseFunctionDeclaration();
667                         break;
668                     }
669                
670                     
671              
672                     symbol = token.data;
673                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
674
675                         if (symbol == "eval") {
676                             if (this.ts.look(-1).type == 'COMM') {
677                                 // look for eval:var:noreplace\n
678                                 var _t = this;
679                                 this.ts.look(-1).data.replace(/eval:var:([a-z]+)/ig, function(m, a) {
680                                     var hi = _t.getIdentifier(a, currentScope);
681                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
682                                     if (hi) {
683                                       //  println("PROTECT "+a+" from munge");
684                                         hi.toMunge = false;
685                                     }
686                                     
687                                     
688                                 });
689                                 
690                             } else {
691                                 this.protectScopeFromObfuscation(currentScope);
692                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
693                             }
694                             
695
696                         }
697                         break;
698                     } 
699                    
700             }
701             if (!this.ts.nextTok()) break;
702         }
703         print(scopeIndent + "<< EXIT EXPRESSION");
704     },
705
706
707     parseCatch : function() {
708
709         var symbol;
710         var token;
711         var currentScope;
712         var identifier;
713
714         //token = getToken(-1);
715         //assert token.getType() == Token.CATCH;
716         token = this.ts.nextTok();
717         //assert token.getType() == Token.LP; (
718         token = this.ts.nextTok();
719         //assert token.getType() == Token.NAME;
720         
721         symbol = token.data;
722         currentScope = this.scopes[this.scopes.length-1];
723
724         if (this.mode == 'BUILDING_SYMBOL_TREE') {
725             // We must declare the exception identifier in the containing function
726             // scope to avoid errors related to the obfuscation process. No need to
727             // display a warning if the symbol was already declared here...
728             currentScope.declareIdentifier(symbol, token);
729         } else {
730             //?? why inc the refcount?? - that should be set when building the tree???
731             identifier = this.getIdentifier(symbol, currentScope);
732             identifier.refcount++;
733         }
734
735         token = this.ts.nextTok();
736         //assert token.getType() == Token.RP; // )
737     },
738     
739     parseFunctionDeclaration : function() 
740     {
741        // print("PARSE FUNCTION");
742         var symbol;
743         var token;
744         var currentScope  = false; 
745         var fnScope = false;
746         var identifier;
747         //this.logR("<B>PARSING FUNCTION</B>");
748         currentScope = this.scopes[this.scopes.length-1];
749
750         token = this.ts.nextTok();
751         if (token.type == "NAME") {
752             if (this.mode == 'BUILDING_SYMBOL_TREE') {
753                 // Get the name of the function and declare it in the current scope.
754                 symbol = token.data;
755                 if (currentScope.getIdentifier(symbol) != false) {
756                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
757                 }
758                 currentScope.declareIdentifier(symbol,token);
759             }
760             token =  this.ts.nextTok();
761         }
762
763         //assert token.getType() == Token.LP;
764         if (this.mode == 'BUILDING_SYMBOL_TREE') {
765             fnScope = new Scope(this.braceNesting, currentScope, token.n, '');
766             
767             //println("STORING SCOPE" + this.ts.cursor);
768             
769             this.indexedScopes[this.ts.cursor] = fnScope;
770             
771         } else {
772             //qln("FETCHING SCOPE" + this.ts.cursor);
773             fnScope = this.indexedScopes[this.ts.cursor];
774           
775         }
776         
777         // Parse function arguments.
778         var argpos = 0;
779         while (this.ts.lookTok().data != ')') { //(token = consumeToken()).getType() != Token.RP) {
780             token = this.ts.nextTok();
781            // print ("FUNC ARGS: " + token.toString())
782             //assert token.getType() == Token.NAME ||
783             //        token.getType() == Token.COMMA;
784             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
785                 symbol = token.data;
786                 identifier = fnScope.declareIdentifier(symbol,token);
787                 if (symbol == "$super" && argpos == 0) {
788                     // Exception for Prototype 1.6...
789                     identifier.preventMunging();
790                 }
791                 argpos++;
792             }
793         }
794
795         token = this.ts.nextTok();
796         // assert token.getType() == Token.LC;
797         this.braceNesting++;
798
799         token = this.ts.nextTok();
800         if (token.type == "STRN" && this.ts.lookTok(1).data == ';') {
801             /*
802             
803             NOT SUPPORTED YET!?!!?!
804             
805             // This is a hint. Hints are empty statements that look like
806             // "localvar1:nomunge, localvar2:nomunge"; They allow developers
807             // to prevent specific symbols from getting obfuscated (some heretic
808             // implementations, such as Prototype 1.6, require specific variable
809             // names, such as $super for example, in order to work appropriately.
810             // Note: right now, only "nomunge" is supported in the right hand side
811             // of a hint. However, in the future, the right hand side may contain
812             // other values.
813             consumeToken();
814             String hints = token.getValue();
815             // Remove the leading and trailing quotes...
816             hints = hints.substring(1, hints.length() - 1).trim();
817             StringTokenizer st1 = new StringTokenizer(hints, ",");
818             while (st1.hasMoreTokens()) {
819                 String hint = st1.nextToken();
820                 int idx = hint.indexOf(':');
821                 if (idx <= 0 || idx >= hint.length() - 1) {
822                     if (mode == BUILDING_SYMBOL_TREE) {
823                         // No need to report the error twice, hence the test...
824                         this.warn("Invalid hint syntax: " + hint, true);
825                     }
826                     break;
827                 }
828                 String variableName = hint.substring(0, idx).trim();
829                 String variableType = hint.substring(idx + 1).trim();
830                 if (mode == BUILDING_SYMBOL_TREE) {
831                     fnScope.addHint(variableName, variableType);
832                 } else if (mode == CHECKING_SYMBOL_TREE) {
833                     identifier = fnScope.getIdentifier(variableName);
834                     if (identifier != null) {
835                         if (variableType.equals("nomunge")) {
836                             identifier.preventMunging();
837                         } else {
838                             this.warn("Unsupported hint value: " + hint, true);
839                         }
840                     } else {
841                         this.warn("Hint refers to an unknown identifier: " + hint, true);
842                     }
843                 }
844             }
845             */
846         }
847
848         this.parseScope(fnScope);
849         // now pop it off the stack!!!
850        
851         
852         
853     },
854     
855     protectScopeFromObfuscation : function(scope) {
856             //assert scope != null;
857         
858         if (scope == this.globalScope) {
859             // The global scope does not get obfuscated,
860             // so we don't need to worry about it...
861             return;
862         }
863
864         // Find the highest local scope containing the specified scope.
865         while (scope && scope.parent != this.globalScope) {
866             scope = scope.parent;
867         }
868
869         //assert scope.getParentScope() == globalScope;
870         scope.preventMunging();
871     },
872     
873     getIdentifier: function(symbol, scope) {
874         var identifier;
875         while (scope != false) {
876             identifier = scope.getIdentifier(symbol);
877             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
878             if (identifier) {
879                 return identifier;
880             }
881             scope = scope.parent;
882         }
883         return false;
884     }
885 };