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