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