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