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