JSDOC/ScopeParser.js
[app.jsdoc] / JSDOC / ScopeParser.js
1 //<Script type="text/javascript">
2
3 Scope = imports.Scope.Scope;
4 TokenStream = imports.TokenStream.TokenStream;
5 /**
6 * This code now uses the collapsed tree
7 * Used by packer to determine scope
8
9 * @class ScopeParser
10 * @scope JSDOC
11
12
13 */
14
15 ScopeParser = function(ts) {
16     this.ts = ts; // {TokenStream}
17     this.warnings = [];
18     this.indexedg = {};
19     this.timer = new Date() * 1;
20     this.debug = false;
21 }
22 /**
23  * @type {Ident}
24  * @static
25  * list of keywords that should not be used in object literals.
26  * 
27  */
28 ScopeParser.idents = [
29         "break",         
30         "case",          
31         "continue",     
32         "default",      
33         "delete",       
34         "do",            
35         "else",         
36         "export",       
37         "false",        
38         "for",          
39         "function",     
40         "if",           
41         "import",       
42         "in",           
43         "new",          
44         "null",         
45         "return",       
46         "switch",       
47         "this",         
48         "true",         
49         "typeof",       
50         "var",          
51         "void",         
52         "while",        
53         "with",         
54
55         "catch",        
56         "class",        
57         "const",        
58         "debugger",     
59         "enum",         
60         "extends",      
61         "finally",      
62         "super",        
63         "throw",         
64         "try",          
65
66         "abstract",     
67         "boolean",      
68         "byte",         
69         "char",         
70         "double",       
71         "final",        
72         "float",        
73         "goto",         
74         "implements", 
75         "instanceof",
76         "int",           
77         "interface",     
78         "long",          
79         "native",       
80         "package",      
81         "private",      
82         "protected",     
83         "public",        
84         "short",        
85         "static",       
86         "synchronized",  
87         "throws",        
88         "transient",     
89                 "include",       
90                 "undefined"
91 ];
92
93
94 ScopeParser.prototype = {
95     timer: 0,
96     timerPrint: function (str) {
97         var ntime = new Date() * 1;
98         var tdif =  ntime -this.timer;
99         this.timer = ntime;
100         var pref = '';
101         if (tdif > 100) { //slower ones..
102             pref = '***';
103         }
104         println(pref+'['+tdif+']'+str);
105         
106     },
107     warn: function(s) {
108         //print('****************' + s);
109         this.warnings.push(s);
110         //println("WARNING:" + htmlescape(s) + "<BR>");
111     },
112     // defaults should not be initialized here =- otherwise they get duped on new, rather than initalized..
113     warnings : false,
114     ts : false,
115     global : false,
116     mode : "", //"BUILDING_SYMBOL_TREE",
117     braceNesting : 0,
118     indexedScopes : false,
119     munge: true,
120
121
122
123
124
125     buildSymbolTree : function()
126     {
127         //println("<PRE>");
128         
129         this.ts.rewind();
130         this.braceNesting = 0;
131         
132        // print(JSON.stringify(this.ts.tokens, null,4));
133         
134         
135         this.globalScope = new  Scope(-1, false, -1, '');
136         this.indexedScopes = { 0 : this.globalScope };
137         
138         this.mode = 'BUILDING_SYMBOL_TREE';
139         this.parseScope(this.globalScope);
140         
141         //print("---------------END PASS 1 ---------------- ");
142         
143     },
144     mungeSymboltree : function()
145     {
146
147         if (!this.munge) {
148             return;
149         }
150
151         // One problem with obfuscation resides in the use of undeclared
152         // and un-namespaced global symbols that are 3 characters or less
153         // in length. Here is an example:
154         //
155         //     var declaredGlobalVar;
156         //
157         //     function declaredGlobalFn() {
158         //         var localvar;
159         //         localvar = abc; // abc is an undeclared global symbol
160         //     }
161         //
162         // In the example above, there is a slim chance that localvar may be
163         // munged to 'abc', conflicting with the undeclared global symbol
164         // abc, creating a potential bug. The following code detects such
165         // global symbols. This must be done AFTER the entire file has been
166         // parsed, and BEFORE munging the symbol tree. Note that declaring
167         // extra symbols in the global scope won't hurt.
168         //
169         // Note: Since we go through all the tokens to do this, we also use
170         // the opportunity to count how many times each identifier is used.
171
172         this.ts.rewind();
173         this.braceNesting = 0;
174         this.mode = 'PASS2_SYMBOL_TREE';
175         
176         //println("MUNGING?");
177         
178         this.parseScope(this.globalScope);
179         
180         //this.globalScope.dump();
181         
182         
183         this.globalScope.munge();
184     },
185
186
187     log : function(str)
188     {
189         print ("                    ".substring(0, this.braceNesting*2) + str);
190         
191         //println("<B>LOG:</B>" + htmlescape(str) + "<BR/>\n");
192     },
193     logR : function(str)
194     {
195             //println("<B>LOG:</B>" + str + "<BR/>");
196     },
197
198      
199     
200
201
202     parseScope : function(scope) // parse a token stream..
203     {
204         //this.timerPrint("parseScope EnterScope"); 
205         //this.log(">>> ENTER SCOPE" + this.scopes.length);
206         var symbol;
207         var token;
208         
209         var identifier;
210
211         var expressionBraceNesting = this.braceNesting + 0;
212         
213         var parensNesting = 0;
214         
215         var isObjectLitAr = [ false ];
216         var isInObjectLitAr;
217         
218        
219         //var scopeIndent = ''; 
220         //this.scopes.forEach(function() {
221         //    scopeIndent += '   '; 
222         //});
223         //print(">> ENTER SCOPE");
224         
225         
226         
227         
228         token = this.ts.lookTok(1);
229         while (token) {
230           //  this.timerPrint("parseScope AFTER lookT: " + token.toString()); 
231             //this.dumpToken(token , this.scopes, this.braceNesting);
232             //print('SCOPE:' + token.toString());
233             //this.log(token.data);
234             if (token.type == 'NAME') {
235             //    print('*' + token.data);
236             }
237             switch(token.type + '.' + token.name) {
238                 case "KEYW.VAR":
239                 case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
240                     //print('SCOPE-VAR:' + token.toString());
241                     var vstart = this.ts.cursor +1;
242                     
243                     //this.log("parseScope GOT VAR/CONST : " + token.toString()); 
244                     while (true) {
245                         token = this.ts.nextTok();
246                         //!this.debug|| print( token.toString());
247                        // print('SCOPE-VAR-VAL:' + JSON.stringify(token, null, 4));
248                         if (!token) { // can return false at EOF!
249                             break;
250                         }
251                         if (token.name == "VAR" || token.data == ',') { // kludge..
252                             continue;
253                         }
254                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
255                         if (token.type != "NAME") {
256                             for(var i = Math.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
257                                 print(this.ts.tokens[i].toString());
258                             }
259                             
260                             print( "var without ident");
261                             Seed.quit()
262                         }
263                         
264
265                         if (this.mode == "BUILDING_SYMBOL_TREE") {
266                             identifier = scope.getIdentifier(token.data,token) ;
267                             
268                             if (identifier == false) {
269                                 scope.declareIdentifier(token.data, token);
270                             } else {
271                                 token.identifier = identifier;
272                                 this.warn("(SCOPE) The variable " + token.data  + ' (line:' + token.line + ")  has already been declared in the same scope...");
273                             }
274                         }
275
276                         token = this.ts.nextTok();
277                         !this.debug|| print(token.toString());
278                         /*
279                         assert token.getType() == Token.SEMI ||
280                                 token.getType() == Token.ASSIGN ||
281                                 token.getType() == Token.COMMA ||
282                                 token.getType() == Token.IN;
283                         */
284                         if (token.name == "IN") {
285                             break;
286                         } else {
287                             //var bn = this.braceNesting;
288                             var bn = this.braceNesting;
289                             var nts = [];
290                             while (true) {
291                                 if (!token || token.type == 'VOID' || token.data == ',') {
292                                     break;
293                                 }
294                                 nts.push(token);
295                                 token = this.ts.nextTok();
296                             }
297                             if (nts.length) {
298                                 var TS = this.ts;
299                                 this.ts = new TokenStream(nts);
300                                 this.parseExpression(scope);
301                                 this.ts = TS;
302                             }
303                                
304                             this.braceNesting = bn;
305                             //this.braceNesting = bn;
306                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
307                             
308                             token = this.ts.lookTok(1);
309                             //!this.debug|| 
310                            // print("AFTER EXP: " + token.toString());
311                             if (token.data == ';') {
312                                 break;
313                             }
314                         }
315                     }
316                     
317                     //print("VAR:")
318                     //this.ts.dump(vstart , this.ts.cursor);
319                     
320                     break;
321                     
322                     
323                 case "KEYW.FUNCTION":
324                     //if (this.mode == 'BUILDING_SYMBOL_TREE') 
325                     //    print('SCOPE-FUNC:' + JSON.stringify(token,null,4));
326                     //println("<i>"+token.data+"</i>");
327                      var bn = this.braceNesting;
328                     this.parseFunctionDeclaration(scope);
329                      this.braceNesting = bn;
330                     break;
331
332                 case "PUNC.LEFT_CURLY": // {
333                 case "PUNC.LEFT_PAREN": // (    
334                 case "PUNC.LEFT_BRACE": // [
335                     //print('SCOPE-CURLY/PAREN:' + token.toString());
336                     //println("<i>"+token.data+"</i>");
337                     var curTS = this.ts;
338                     if (token.props) {
339                         
340                         for (var prop in token.props) {
341                             
342                             
343                           //  print('SCOPE-PROPS:' + JSON.stringify(token.props[prop],null,4));
344                             if (token.props[prop].val[0].data == 'function') {
345                                 // parse a function..
346                                 this.ts = new TokenStream(token.props[prop].val);
347                                 this.ts.nextTok();
348                                 this.parseFunctionDeclaration(scope);
349                                 
350                                 continue;
351                             }
352                             // key value..
353                             
354                             this.ts = new TokenStream(token.props[prop].val);
355                             this.parseExpression(scope);
356                             
357                         }
358                         this.ts = curTS;
359                         
360                         // it's an object literal..
361                         // the values could be replaced..
362                         break;
363                     }
364                     
365                     
366                     var _this = this;
367                     token.items.forEach(function(expr) {
368                            //print(JSON.stringify(expr, null,4));
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         var scope;
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         
870         
871         //assert token.getType() == Token.LP;
872         if (this.mode == 'BUILDING_SYMBOL_TREE') {
873             fnScope = new Scope(1, scope, token.n, '', token);
874             
875             //println("STORING SCOPE" + this.ts.cursor);
876             
877             this.indexedScopes[token.id] = fnScope;
878             
879         } else {
880             //qln("FETCHING SCOPE" + this.ts.cursor);
881             fnScope = this.indexedScopes[token.id];
882         }
883         //if (this.mode == 'BUILDING_SYMBOL_TREE') 
884         //  print('FUNC-PARSE:' + JSON.stringify(token,null,4));
885         // Parse function arguments.
886         var args = token.items;
887         for (var argpos =0; argpos < args.length; argpos++) {
888              
889             token = args[argpos][0];
890             //print ("FUNC ARGS: " + token.toString())
891             //assert token.getType() == Token.NAME ||
892             //        token.getType() == Token.COMMA;
893             if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
894                 symbol = token.data;
895                 identifier = fnScope.declareIdentifier(symbol,token);
896                 if (symbol == "$super" && argpos == 0) {
897                     // Exception for Prototype 1.6...
898                     identifier.preventMunging();
899                 }
900                 //argpos++;
901             }
902         }
903         
904         token = this.ts.nextTok();
905         //print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
906         //Seed.quit();
907         //print(token.toString());
908         // assert token.getType() == Token.LC;
909         //this.braceNesting++;
910         
911         //token = this.ts.nextTok();
912         //print(token.toString());
913         var outTS = this.ts;
914         var _this = this;
915         token.items.forEach(function(tar) {
916             _this.ts = new TokenStream(tar);
917             _this.parseScope(fnScope);
918             
919             
920         });
921         
922         //print(JSON.stringify(this.ts,null,4));
923         //this.parseScope(fnScope);
924         this.ts = outTS;
925         // now pop it off the stack!!!
926        
927         //this.braceNesting = b4braceNesting;
928         //print("ENDFN -1: " + this.ts.lookTok(-1).toString());
929         //print("ENDFN 0: " + this.ts.lookTok(0).toString());
930         //print("ENDFN 1: " + this.ts.lookTok(1).toString());
931     },
932     
933     protectScopeFromObfuscation : function(scope) {
934             //assert scope != null;
935         
936         if (scope == this.globalScope) {
937             // The global scope does not get obfuscated,
938             // so we don't need to worry about it...
939             return;
940         }
941
942         // Find the highest local scope containing the specified scope.
943         while (scope && scope.parent != this.globalScope) {
944             scope = scope.parent;
945         }
946
947         //assert scope.getParentScope() == globalScope;
948         scope.preventMunging();
949     },
950     
951     getIdentifier: function(symbol, scope, token) {
952         var identifier;
953         while (scope != false) {
954             identifier = scope.getIdentifier(symbol, token);
955             //println("ScopeParser.getIdentgetUsedSymbols("+symbol+")=" + scope.getUsedSymbols().join(','));
956             if (identifier) {
957                 return identifier;
958             }
959             scope = scope.parent;
960         }
961         return false;
962     }
963 };