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