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