JSDOC/Packer.vala
[gnome.introspection-doc-generator] / JSDOC / ScopeParser.js
1 //<Script type="text/javascript">
2
3 const Scope = imports.Scope.Scope;
4 const TokenStream = imports.TokenStream.TokenStream;
5 /**
6 * Scope stuff
7
8 * This code now uses the collapsed tree
9
10
11
12 */
13
14 const 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|| 
305                            // print("AFTER EXP: " + token.toString());
306                             if (token.data == ';') {
307                                 break;
308                             }
309                         }
310                     }
311                     
312                     //print("VAR:")
313                     //this.ts.dump(vstart , this.ts.cursor);
314                     
315                     break;
316                     
317                     
318                 case "KEYW.FUNCTION":
319                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
320                     //    print('SCOPE-FUNC:' + JSON.stringify(token,null,4));
321                     //println("<i>"+token.data+"</i>");
322                      var bn = this.braceNesting;
323                     this.parseFunctionDeclaration(scope);
324                      this.braceNesting = bn;
325                     break;
326
327                 case "PUNC.LEFT_CURLY": // {
328                 case "PUNC.LEFT_PAREN": // (    
329                 case "PUNC.LEFT_BRACE": // [
330                     //print('SCOPE-CURLY/PAREN:' + token.toString());
331                     //println("<i>"+token.data+"</i>");
332                     var curTS = this.ts;
333                     if (token.props) {
334                         
335                         // { a : ... , c : .... }
336                         
337                         for (var prop in token.props) {
338                             
339                             
340                           //  print('SCOPE-PROPS:' + JSON.stringify(token.props[prop],null,4));
341                             if (token.props[prop].val[0].data == 'function') {
342                                 // parse a function..
343                                 this.ts = new TokenStream(token.props[prop].val);
344                                 this.ts.nextTok();
345                                 this.parseFunctionDeclaration(scope);
346                                 
347                                 continue;
348                             }
349                             // key value..
350                             
351                             this.ts = new TokenStream(token.props[prop].val);
352                             this.parseExpression(scope);
353                             
354                         }
355                         this.ts = curTS;
356                         
357                         // it's an object literal..
358                         // the values could be replaced..
359                         break;
360                     }
361                     
362                     // ( ... ) or { .... } not object literals..
363                     
364                     var _this = this;
365                     for (var xx =0; xx < token.items.length; xx++) {
366                                 expr = token.items[xx];
367                     //token.items.forEach(function(expr) {
368                             //print(expr.toString());
369                            _this.ts = new TokenStream(expr);
370                             //if (curTS.data == '(') {
371                                 _this.parseScope(scope)
372                             //} else {
373                               //  _this.parseExpression(scope)
374                             //}
375                           
376                     }  
377                     this.ts = curTS;
378                     //print("NOT PROPS"); Seed.quit();
379                     
380                     //isObjectLitAr.push(false);
381                     //this.braceNesting++;
382                     
383                     //print(">>>>>> OBJLIT PUSH(false)" + this.braceNesting);
384                     break;
385
386                 case "PUNC.RIGHT_CURLY": // }
387                     //print("<< EXIT SCOPE");
388                     return;
389               
390                 case "KEYW.WITH":
391                     //print('SCOPE-WITH:' + token.toString());
392                     //println("<i>"+token.data+"</i>");   
393                     if (this.mode == "BUILDING_SYMBOL_TREE") {
394                         // Inside a 'with' block, it is impossible to figure out
395                         // statically whether a symbol is a local variable or an
396                         // object member. As a consequence, the only thing we can
397                         // do is turn the obfuscation off for the highest scope
398                         // containing the 'with' block.
399                         this.protectScopeFromObfuscation(scope);
400                         this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
401                     }
402                     break;
403
404                 case "KEYW.CATCH":
405                     //print('SCOPE-CATCH:' + token.toString());
406                     //println("<i>"+token.data+"</i>");
407                     this.parseCatch(scope);
408                     break;
409
410                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
411                 case "STRN.SINGLE_QUOTE":
412                   //  print('SCOPE-STRING:' + token.toString());
413                     //println("<i>"+token.data+"</i>");
414
415                     if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
416                         // then we are in an object lit.. -> we need to flag the brace as such...
417                         isObjectLitAr.pop();
418                         isObjectLitAr.push(true);
419                         //print(">>>>>> OBJLIT REPUSH(true)");
420                     }
421                     isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
422                     
423                     if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
424                         ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
425                         // see if we can replace..
426                         // remove the quotes..
427                         // should do a bit more checking!!!! (what about wierd char's in the string..
428                         var str = token.data.substring(1,token.data.length-1);
429                         if (/^[a-z_]+$/i.test(str) && ScopeParser.idents.indexOf(str) < 0) {
430                             token.outData = str;
431                         }
432                         
433                          
434                         
435                     }
436                     
437                     break;
438                 
439                 case "NAME.NAME":
440                     //print('SCOPE-NAME:' + token.toString());
441                     //print("DEAL WITH NAME:");
442                     // got identifier..
443                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
444                     // skip anyting with "." before it..!!
445                      
446                     if (this.ts.lookTok(-1).data == ".") {
447                         // skip, it's an object prop.
448                         //println("<i>"+token.data+"</i>");
449                         break;
450                     }
451                     //print("SYMBOL: " + token.toString());
452                     
453                     symbol = token.data;
454                     if (symbol == 'this') {
455                         break;
456                     }
457                     if (this.mode == 'PASS2_SYMBOL_TREE') {
458                         
459                         //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
460                         
461                         //print ("MUNGE?" + symbol);
462                         
463                         //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
464                              
465                             //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
466                         identifier = this.getIdentifier(symbol, scope, token);
467                         
468                         if (identifier == false) {
469 // BUG!find out where builtin is defined...
470                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
471                                 // Here, we found an undeclared and un-namespaced symbol that is
472                                 // 3 characters or less in length. Declare it in the global scope.
473                                 // We don't need to declare longer symbols since they won't cause
474                                 // any conflict with other munged symbols.
475                                 this.globalScope.declareIdentifier(symbol, token);
476                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
477                             }
478                             
479                             //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
480                         } else {
481                             token.identifier = identifier;
482                             identifier.refcount++;
483                         }
484                     }   
485                     
486                     break;
487                     //println("<B>SID</B>");
488                 default:
489                     if (token.type != 'KEYW') {
490                         break;
491                     }
492                     //print('SCOPE-KEYW:' + token.toString());
493                    // print("Check eval:");
494                 
495                     symbol = token.data;
496                     
497                      if (this.mode == 'BUILDING_SYMBOL_TREE') {
498
499                         if (token.name == "EVAL") {
500                             
501                             //print(JSON.stringify(token, null,4));
502                             // look back one and see if we can find a comment!!!
503                             //if (this.ts.look(-1).type == "COMM") {
504                             if (token.prefix && token.prefix.match(/eval/)) {
505                                 // look for eval:var:noreplace\n
506                                 //print("MATCH!?");
507                                 var _t = this;
508                                 token.prefix.replace(/eval:var:([a-z_]+)/ig, function(m, a) {
509                                     //print("GOT: " + a);
510                                     var hi = _t.getIdentifier(a, scope, token);
511                                    // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
512                                     if (hi) {
513                                       //  print("PROTECT "+a+" from munge");
514                                         //print(JSON.stringify(hi,null,4));
515                                         hi.toMunge = false;
516                                     }
517                                     
518                                 });
519                                 
520                                 
521                             } else {
522                                 
523                             
524                                 this.protectScopeFromObfuscation(scope);
525                                 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);
526                             }
527
528                         }
529
530                     }
531                     break;
532                 
533                 
534             } // end switch
535             
536             
537             //print("parseScope TOK : " + token.toString()); 
538             token = this.ts.nextTok();
539             //if (this.ts.nextT()) break;
540             
541         }
542         //print("<<< EXIT SCOPE");
543         //print("<<<<<<<EXIT SCOPE ERR?" +this.scopes.length);
544     },
545
546     expN : 0,
547     parseExpression : function(scope) {
548
549         // Parse the expression until we encounter a comma or a semi-colon
550         // in the same brace nesting, bracket nesting and paren nesting.
551         // Parse functions if any...
552         //println("<i>EXP</i><BR/>");
553         !this.debug || print("PARSE EXPR");
554         this.expN++;
555          
556         // for printing stuff..
557        
558         
559         
560         var symbol;
561         var token;
562         
563         var identifier;
564
565         var expressionBraceNesting = this.braceNesting + 0;
566         var bracketNesting = 0;
567         var parensNesting = 0;
568         var isInObjectLitAr;
569         var isObjectLitAr = [ false ];
570         
571         
572             
573         
574         //print(scopeIndent + ">> ENTER EXPRESSION" + this.expN);
575         while ((token = this.ts.nextTok())) {
576      
577         
578             
579            /*
580             // moved out of loop?
581            currentScope = this.scopes[this.scopes.length-1];
582             
583             var scopeIndent = ''; 
584             this.scopes.forEach(function() {
585                 scopeIndent += '   '; 
586             });
587            */ 
588            
589            //this.dumpToken(token,  this.scopes, this.braceNesting );
590             //print('EXPR' +  token.toString());
591             
592             
593             //println("<i>"+token.data+"</i>");
594             //this.log("EXP:" + token.data);
595             switch (token.type) {
596                 case 'PUNC':
597                     //print("EXPR-PUNC:" + token.toString());
598                     
599                     switch(token.data) {
600                          
601                         case ';':
602                             //print("<< EXIT EXPRESSION");
603                             break;
604
605                         case ',':
606                             
607                             break;
608
609                        
610                         case '(': //Token.LP:
611                         case '{': //Token.LC:
612                         case '[': //Token.LB:
613                             //print('SCOPE-CURLY/PAREN/BRACE:' + token.toString());
614                            // print('SCOPE-CURLY/PAREN/BRACE:' + JSON.stringify(token, null,4));
615                             //println("<i>"+token.data+"</i>");
616                             var curTS = this.ts;
617                             if (token.props) {
618                                 
619                                 for (var prop in token.props) {
620                                     if (!token.props[prop].val.length) {
621                                         print(JSON.stringify(token.props, null,4));
622                                     }
623                                     
624                                     if (token.props[prop].val[0].data == 'function') {
625                                         // parse a function..
626                                         this.ts = new TokenStream(token.props[prop].val);
627                                         this.ts.nextTok();
628                                         this.parseFunctionDeclaration(scope);
629                                         continue;
630                                     }
631                                     // key value..
632                                     
633                                     this.ts = new TokenStream(token.props[prop].val);
634                                     this.parseExpression(scope);
635                                     
636                                 }
637                                 this.ts = curTS;
638                                 
639                                 // it's an object literal..
640                                 // the values could be replaced..
641                                 break;
642                             }
643                             
644                             
645                             var _this = this;
646                             token.items.forEach(function(expr) {
647                                   _this.ts = new TokenStream(expr);
648                                   _this.parseExpression(scope)
649                             });
650                             this.ts = curTS;
651                         
652                         
653                     
654                             ///print(">>>>> EXP PUSH(false)"+this.braceNesting);
655                             break;
656
657                        
658                         
659                          
660                             
661                         case ')': //Token.RP:
662                         case ']': //Token.RB:
663                         case '}': //Token.RB:
664                             //print("<< EXIT EXPRESSION");
665                             return;
666                            
667  
668              
669                             parensNesting++;
670                             break;
671
672                         
673                             
674                     }
675                     break;
676                     
677                 case 'STRN': // used for object lit detection..
678                     //if (this.mode == 'BUILDING_SYMBOL_TREE')    
679                         //print("EXPR-STR:" + JSON.stringify(token, null, 4));
680                
681                      
682                     break;
683                 
684                       
685              
686                 case 'NAME':
687                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
688                         
689                         //print("EXPR-NAME:" + JSON.stringify(token, null, 4));
690                     } else {
691                         //print("EXPR-NAME:" + token.toString());
692                     }
693                     symbol = token.data;
694                     //print("in NAME = " + token.toString());
695                     //print("in NAME 0: " + this.ts.look(0).toString());
696                     //print("in NAME 2: " + this.ts.lookTok(2).toString());
697                     
698                     //print(this.ts.lookTok(-1).data);
699                     // prefixed with '.'
700                     if (this.ts.lookTok(-1).data == ".") {
701                         //skip '.'
702                         break;
703                     }
704                     if (symbol == 'this') {
705                         break;
706                        }
707                     
708                     if (this.mode == 'PASS2_SYMBOL_TREE') {
709
710                         identifier = this.getIdentifier(symbol, scope, token);
711                         //println("<B>??</B>");
712                         if (identifier == false) {
713
714                             if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
715                                 // Here, we found an undeclared and un-namespaced symbol that is
716                                 // 3 characters or less in length. Declare it in the global scope.
717                                 // We don't need to declare longer symbols since they won't cause
718                                 // any conflict with other munged symbols.
719                                 this.globalScope.declareIdentifier(symbol, token);
720                                 this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
721                                 //print("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')');
722                                 //throw "OOPS";
723                             } else {
724                                 //print("undeclared:" + token.toString())
725                             }
726                             
727                             
728                         } else {
729                             //println("<B>++</B>");
730                             token.identifier = identifier;
731                             identifier.refcount++;
732                         }
733                         
734                     }
735                     break;
736                     
737                     
738                     
739                     
740                     //println("<B>EID</B>");
741                 case 'KEYW':   
742                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
743                     //    print("EXPR-KEYW:" + JSON.stringify(token, null, 4));
744                     
745                     //print('EXPR-KEYW:' + token.toString());
746                     if (token.name == "FUNCTION") {
747                         
748                         this.parseFunctionDeclaration(scope);
749                         break;
750                     }
751                
752                      
753                     symbol = token.data;
754                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
755                         
756                         if (token.name == "EVAL") {
757                             //print(JSON.stringify(token,null,4));
758                             if (token.prefix && token.prefix.match(/eval:var:/g)) {
759                                 // look for eval:var:noreplace\n
760                                // print("GOT MATCH?");
761                                 var _t = this;
762                                 token.prefix.replace(/eval:var:([a-z]+)/ig, function(m, a) {
763                                     
764                                     //print("PROTECT: " + a);
765                                     
766                                     
767                                     var hi = _t.getIdentifier(a, scope, token);
768                                    //println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING"));
769                                     if (hi) {
770                                       //  println("PROTECT "+a+" from munge");
771                                         hi.toMunge = false;
772                                     }
773                                     
774                                     
775                                 });
776                                 
777                             } else {
778                                 this.protectScopeFromObfuscation(scope);
779                                 this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true);
780                             }
781                             
782
783                         }
784                         break;
785                     } 
786                 default:
787                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
788                     //    print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
789                     break;
790             }
791             
792         }
793         //print("<< EXIT EXPRESSION");
794         this.expN--;
795     },
796
797
798     parseCatch : function(scope) {
799
800         var symbol;
801         var token;
802          
803         var identifier;
804         
805         //token = getToken(-1);
806         //assert token.getType() == Token.CATCH;
807         token = this.ts.nextTok(1);
808         token = this.ts.nextTok(1);
809         
810         
811         //print(JSON.stringify(this.ts,null,4));
812         //assert token.getType() == Token.LP; (
813         //token = this.ts.nextTok();
814         //assert token.getType() == Token.NAME;
815         
816         symbol = token.items[0][0].data;
817         
818
819         if (this.mode == 'BUILDING_SYMBOL_TREE') {
820             // We must declare the exception identifier in the containing function
821             // scope to avoid errors related to the obfuscation process. No need to
822             // display a warning if the symbol was already declared here...
823             scope.declareIdentifier(symbol, token.items[0][0]);
824         } else {
825             //?? why inc the refcount?? - that should be set when building the tree???
826             identifier = this.getIdentifier(symbol, scope, token.items[0][0]);
827             identifier.refcount++;
828         }
829         
830         //token = this.ts.nextTok();
831         //assert token.getType() == Token.RP; // )
832     },
833     
834     parseFunctionDeclaration : function(scope) 
835     {
836         //print("PARSE FUNCTION");
837         var symbol;
838         var token;
839         
840         var fnScope = false;
841         var identifier;
842         var b4braceNesting = this.braceNesting + 0;
843         
844         //this.logR("<B>PARSING FUNCTION</B>");
845         
846
847         token = this.ts.nextTok();
848         if (token.type == "NAME") {
849             if (this.mode == 'BUILDING_SYMBOL_TREE') {
850                 // Get the name of the function and declare it in the current scope.
851                 symbol = token.data;
852                 if (scope.getIdentifier(symbol,token) != false) {
853                     this.warn("The function " + symbol + " has already been declared in the same scope...", true);
854                 }
855                 scope.declareIdentifier(symbol,token);
856             }
857             token =  this.ts.nextTok();
858         }
859         
860         
861         // return function() {.... 
862         while (token.data != "(") {
863             //print(token.toString());
864             token =  this.ts.nextTok();
865              
866         }
867         
868         
869         //assert token.getType() == Token.LP;
870         if (this.mode == 'BUILDING_SYMBOL_TREE') {
871             fnScope = new Scope(1, scope, token.n, '', token);
872             
873             //println("STORING SCOPE" + this.ts.cursor);
874             
875             this.indexedScopes[token.id] = fnScope;
876             
877         } else {
878             //qln("FETCHING SCOPE" + this.ts.cursor);
879             fnScope = this.indexedScopes[token.id];
880         }
881         //if (this.mode == 'BUILDING_SYMBOL_TREE') 
882         //  print('FUNC-PARSE:' + JSON.stringify(token,null,4));
883         // Parse function arguments.
884         var args = token.items;
885         for (var argpos =0; argpos < args.length; argpos++) {
886              
887             token = args[argpos][0];
888             //print ("FUNC ARGS: " + token.toString())
889             //assert token.getType() == Token.NAME ||
890             //        token.getType() == Token.COMMA;
891             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
892                 symbol = token.data;
893                 identifier = fnScope.declareIdentifier(symbol,token);
894                 if (symbol == "$super" && argpos == 0) {
895                     // Exception for Prototype 1.6...
896                     identifier.preventMunging();
897                 }
898                 //argpos++;
899             }
900         }
901         
902         token = this.ts.nextTok();
903         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
904         //Seed.quit();
905         //print(token.toString());
906         // assert token.getType() == Token.LC;
907         //this.braceNesting++;
908         
909         //token = this.ts.nextTok();
910         //print(token.toString());
911         var outTS = this.ts;
912         var _this = this;
913         token.items.forEach(function(tar) {
914             _this.ts = new TokenStream(tar);
915             _this.parseScope(fnScope);
916             
917             
918         });
919         
920         //print(JSON.stringify(this.ts,null,4));
921         //this.parseScope(fnScope);
922         this.ts = outTS;
923         // now pop it off the stack!!!
924        
925         //this.braceNesting = b4braceNesting;
926         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
927         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
928         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
929     },
930     
931     protectScopeFromObfuscation : function(scope) {
932             //assert scope != null;
933         
934         if (scope == this.globalScope) {
935             // The global scope does not get obfuscated,
936             // so we don't need to worry about it...
937             return;
938         }
939
940         // Find the highest local scope containing the specified scope.
941         while (scope && scope.parent != this.globalScope) {
942             scope = scope.parent;
943         }
944
945         //assert scope.getParentScope() == globalScope;
946         scope.preventMunging();
947     },
948     
949     getIdentifier: function(symbol, scope, token) {
950         var identifier;
951         while (scope != false) {
952             identifier = scope.getIdentifier(symbol, token);
953             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
954             if (identifier) {
955                 return identifier;
956             }
957             scope = scope.parent;
958         }
959         return false;
960     }
961 };