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