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