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