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