JSDOC/CompressWhite.js
[gnome.introspection-doc-generator] / JSDOC / ScopeParser.js
index 3ffbe29..717bbf5 100644 (file)
@@ -14,7 +14,7 @@ ScopeParser = function(ts) {
     this.scopes = [];
     this.indexedScopes = {};
     this.timer = new Date() * 1;
-   
+    this.debug = false;
 }
 
 // list of keywords that should not be used in object literals.
@@ -163,7 +163,7 @@ ScopeParser.prototype = {
         this.ts.rewind();
         this.braceNesting = 0;
         this.scopes= [];
-        this.mode = 'CHECKING_SYMBOL_TREE';
+        this.mode = 'PASS2_SYMBOL_TREE';
         
         //println("MUNGING?");
         
@@ -201,29 +201,31 @@ ScopeParser.prototype = {
         var isObjectLitAr = [ false ];
         
         this.scopes.push(scope);
-        token = this.ts.lookTok();
+        token = this.ts.lookTok(1);
         while (token) {
           //  this.timerPrint("parseScope AFTER lookT: " + token.toString()); 
              
-            //println("START<i>"+token.data+"</i>");
-            switch(token.type + '.' token.name) {
+            !this.debug|| print(token.type + '.' + token.name+ ":" + token.data);
+            switch(token.type + '.' token.name) {
                 case "KEYW.VAR":
                 case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
                     
                     //this.log("parseScope GOT VAR/CONST : " + token.toString()); 
                     while (true) {
-                        token = this.ts.next();
-                        
-                        if (token.name == "var") { // kludge..
-                            continue;
-                        }
+                        token = this.ts.nextTok();
+                        !this.debug|| print( token.toString());
+                      
                         if (!token) { // can return false at EOF!
                             break;
                         }
+                        if (token.name == "VAR") { // kludge..
+                            continue;
+                        }
                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
-                        if (!token.type =="NAME") {
-                            println(token.toString());
-                            throw "var without ident";
+                        if (token.type !="NAME") {
+                            print(token.toString());
+                            print( "var without ident");
+                            Seed.quit()
                         }
                         
 
@@ -234,25 +236,27 @@ ScopeParser.prototype = {
                                 scope.declareIdentifier(token.data, token);
                             } else {
                                 token.identifier = identifier;
-                                this.warn("The variable " + symbol + " has already been declared in the same scope...");
+                                this.warn("The variable " + token.data  + ' (line:' + token.line + ")  has already been declared in the same scope...");
                             }
                         }
 
-                        token = this.ts.next();
+                        token = this.ts.nextTok();
+                        !this.debug|| print(token.toString());
                         /*
                         assert token.getType() == Token.SEMI ||
                                 token.getType() == Token.ASSIGN ||
                                 token.getType() == Token.COMMA ||
                                 token.getType() == Token.IN;
                         */
-                        if (token.name == "IN")) {
+                        if (token.name == "IN") {
                             break;
                         } else {
                             this.parseExpression();
                             //this.logR("parseScope DONE  : <B>ParseExpression</B> - tok is:" + this.ts.lookT(0).toString()); 
                             
-                            
-                            if (this.ts.look(0).data == ';') {
+                            token = this.ts.lookTok(1);
+                            !this.debug|| print("AFTER EXP: " + token.toString());
+                            if (token.data == ';') {
                                 break;
                             }
                         }
@@ -310,15 +314,15 @@ ScopeParser.prototype = {
                 case "STRN.DOUBLE_QUOTE": // used for object lit detection..
                 case "STRN.SINGLE_QUOTE":
                     //println("<i>"+token.data+"</i>");
-                    if (this.ts.look(-1).data == '{' && this.ts.look(1).data == ':') {
+                    if (this.ts.lookTok(-1).data == '{' && this.ts.lookTok(1).data == ':') {
                         // then we are in an object lit.. -> we need to flag the brace as such...
                         isObjectLitAr.pop();
                         isObjectLitAr.push(true);
                     }
                     var isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
                     
-                    if (isInObjectLitAr &&  this.ts.look(1).data == ':' &&
-                        ( this.ts.look(-1).data == '{'  ||  this.ts.look(-1).data == ':' )) {
+                    if (isInObjectLitAr &&  this.ts.lookTok(1).data == ':' &&
+                        ( this.ts.lookTok(-1).data == '{'  ||  this.ts.lookTok(-1).data == ':' )) {
                         // see if we can replace..
                         // remove the quotes..
                         // should do a bit more checking!!!! (what about wierd char's in the string..
@@ -335,23 +339,22 @@ ScopeParser.prototype = {
                     
                     break;
                 
+                case "NAME.NAME":
                 
-                default: 
-                    if ((token.type != "NAME") && (token.type != "KEYW"){
-                        break;
-                    }
+                    //print("DEAL WITH NAME:");
                     // got identifier..
                     
                     // look for  { ** : <- indicates obj literal.. ** this could occur with numbers ..
-                    if ((this.ts.look(-1).data == "{") && (this.ts.look(1).data == ":")) {
+                    if ((this.ts.lookTok(-1).data == "{") && (this.ts.lookTok(1).data == ":")) {
                         isObjectLitAr.pop();
                         isObjectLitAr.push(true);
                         //println("<i>"+token.data+"</i>");
                         break;
                     }
+                   // print("DEAL WITH obj lit:");
                     var isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
                     
-                    if (isInObjectLitAr && (this.ts.look(1).data == ":") && (this.ts.look(-1).data == ",")) {
+                    if (isInObjectLitAr && (this.ts.lookTok(1).data == ":") && (this.ts.lookTok(-1).data == ",")) {
                         // skip, it's an object lit key..
                         //println("<i>"+token.data+"</i>");
                         break;
@@ -359,16 +362,49 @@ ScopeParser.prototype = {
                     
                     
                     // skip anyting with "." before it..!!
-                    
-                    if (this.ts.look(-1).data(".")) {
+                     
+                    if (this.ts.lookTok(-1).data == ".") {
                         // skip, it's an object prop.
                         //println("<i>"+token.data+"</i>");
                         break;
                     }
+                    symbol = token.data;
+                    if (this.mode == 'PASS2_SYMBOL_TREE') {
+                        
+                        //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
+                        
+                        //print ("MUNGE?" + symbol);
+                        
+                        //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
+                             
+                            //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
+                        identifier = this.getIdentifier(symbol, scope);
+                        
+                        if (identifier == false) {
+// BUG!find out where builtin is defined...
+                            if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
+                                // Here, we found an undeclared and un-namespaced symbol that is
+                                // 3 characters or less in length. Declare it in the global scope.
+                                // We don't need to declare longer symbols since they won't cause
+                                // any conflict with other munged symbols.
+                                this.globalScope.declareIdentifier(symbol, token);
+                                this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
+                            }
+                            
+                            //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
+                        } else {
+                            token.identifier = identifier;
+                            identifier.refcount++;
+                        }
+                    }   
                     
+                    break;
                     //println("<B>SID</B>");
-                   
-                
+                default:
+                    if (token.type != 'KEYW') {
+                        break;
+                    }
+                   // print("Check eval:");
                 
                     symbol = token.data;
                     
@@ -400,34 +436,6 @@ ScopeParser.prototype = {
 
                         }
 
-                    } else   if (this.mode == 'CHECKING_SYMBOL_TREE') {
-                        
-                        //println("GOT IDENT: -2 : " + this.ts.lookT(-2).toString() + " <BR> ..... -1 :  " +  this.ts.lookT(-1).toString() + " <BR> "); 
-                        
-                        
-                        
-                        //println("GOT IDENT: <B>" + symbol + "</B><BR/>");
-                             
-                            //println("GOT IDENT (2): <B>" + symbol + "</B><BR/>");
-                        identifier = this.getIdentifier(symbol, scope);
-                        
-                        if (identifier == false) {
-// BUG!find out where builtin is defined...
-                            if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
-                                // Here, we found an undeclared and un-namespaced symbol that is
-                                // 3 characters or less in length. Declare it in the global scope.
-                                // We don't need to declare longer symbols since they won't cause
-                                // any conflict with other munged symbols.
-                                this.globalScope.declareIdentifier(symbol, token);
-                                this.warn("Found an undeclared symbol: " + symbol, true);
-                            }
-                            
-                            //println("GOT IDENT IGNORE(3): <B>" + symbol + "</B><BR/>");
-                        } else {
-                            token.identifier = identifier;
-                            identifier.refcount++;
-                        }
-                       
                     }
                     break;
                 
@@ -436,7 +444,7 @@ ScopeParser.prototype = {
             
             
             //this.timerPrint("parseScope TOK : " + token.toString()); 
-            token = this.ts.next();
+            token = this.ts.nextTok();
             //if (this.ts.nextT()) break;
             
         }
@@ -449,6 +457,7 @@ ScopeParser.prototype = {
         // in the same brace nesting, bracket nesting and paren nesting.
         // Parse functions if any...
         //println("<i>EXP</i><BR/>");
+        !this.debug || print("PARSE EXPR");
         var symbol;
         var token;
         var currentScope;
@@ -459,62 +468,63 @@ ScopeParser.prototype = {
         var parensNesting = 0;
 
         var isObjectLitAr = [ false ];
-        while (token = this.ts.lookT()) {
+        while (token = this.ts.lookTok()) {
      
 
             
             currentScope = this.scopes[this.scopes.length-1];
             
             //println("<i>"+token.data+"</i>");
-            
+            !this.debug|| print ("EXP" + token.toString());
             switch (token.type) {
+                case 'PUNC':
+                    switch(token.data) {
+                         
+                        case ';':
+                        case ',':
+                            if (this.braceNesting == expressionBraceNesting &&
+                                    bracketNesting == 0 &&
+                                    parensNesting == 0) {
+                                
+                                return;
+                            }
+                            break;
 
-                case 'semicolon':
-                case 'comma':
-                    if (this.braceNesting == expressionBraceNesting &&
-                            bracketNesting == 0 &&
-                            parensNesting == 0) {
-                        return;
-                    }
-                    break;
-
-                case 'function':
-                    this.parseFunctionDeclaration();
-                    break;
+                       
 
-                case 'lbrace': //Token.LC:
-                    isObjectLitAr.push(false);
-                    
-                    this.braceNesting++;
-                    break;
+                        case '{': //Token.LC:
+                            isObjectLitAr.push(false);
+                            
+                            this.braceNesting++;
+                            break;
 
-                case 'rbrace': //Token.RC:
-                    this.braceNesting--;
-                    isObjectLitAr.pop();
-                    
-                   // assert braceNesting >= expressionBraceNesting;
-                    break;
+                        case '}': //Token.RC:
+                            this.braceNesting--;
+                            isObjectLitAr.pop();
+                            
+                           // assert braceNesting >= expressionBraceNesting;
+                            break;
 
-                case 'lbracket': //Token.LB:
-                    bracketNesting++;
-                    break;
+                        case '[': //Token.LB:
+                            bracketNesting++;
+                            break;
 
-                case 'rbracket': //Token.RB:
-                    bracketNesting--;
-                    break;
+                        case ']': //Token.RB:
+                            bracketNesting--;
+                            break;
 
-                case 'lparen': //Token.LP:
-                    parensNesting++;
-                    break;
+                        case '(': //Token.LP:
+                            parensNesting++;
+                            break;
 
-                case 'rparen': //Token.RP:
-                    parensNesting--;
+                        case ')': //Token.RP:
+                            parensNesting--;
+                            break;
+                    }
                     break;
                     
-                    
-                   
-                case 'string': // used for object lit detection..
-                    if (this.ts.lookT(-1).isType('lbrace') && this.ts.lookT(1).isType('colon')) {
+                case 'STRN': // used for object lit detection..
+                    if (this.ts.lookTok(-1).data == "{" && this.ts.lookTok(1).data == ":" ) {
                         // then we are in an object lit.. -> we need to flag the brace as such...
                         isObjectLitAr.pop();
                         isObjectLitAr.push(true);
@@ -523,8 +533,8 @@ ScopeParser.prototype = {
                     
                      
                     var isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
-                    if (isInObjectLitAr &&  this.ts.lookT(1).isType('colon') &&
-                        ( this.ts.lookT(-11).isType('lbrace') ||  this.ts.lookT(-1).isType('comma'))) {
+                    if (isInObjectLitAr &&  this.ts.lookTok(1).data == ":"  &&
+                        ( this.ts.lookTok(-1).data == "{"  ||  this.ts.lookTok(-1).data == "," )) {
                         // see if we can replace..
                         // remove the quotes..
                         var str = token.data.substring(1,token.data.length-1);
@@ -538,43 +548,73 @@ ScopeParser.prototype = {
                     
                     break;
                 
-                  
-                    
-                /*
-                case Token.SPECIALCOMMENT:
-                    if (mode == BUILDING_SYMBOL_TREE) {
-                        protectScopeFromObfuscation(currentScope);
-                        this.warn("Using JScript conditional comments is not recommended." + (munge ? " Moreover, using JScript conditional comments reduces the level of compression!" : ""), true);
-                    }
-                    break;
-                */
-                case 'identifier':
+                      
+             
+                case 'NAME':
+               
                     symbol = token.data;
-                    if (this.ts.lookT(-1).isType('lbrace') && this.ts.lookT(1).isType('colon')) {
+                    if (this.ts.lookTok(-1).data == "{"  && this.ts.lookTok(1).data == ":") {
                         // then we are in an object lit.. -> we need to flag the brace as such...
                         isObjectLitAr.pop();
                         isObjectLitAr.push(true);
                         break;
                     }
                     var isInObjectLitAr = isObjectLitAr[isObjectLitAr.length-1];
-                    if (isInObjectLitAr && this.ts.lookT(-1).isType('comma') && this.ts.lookT(1).isType('colon')) {
+                    if (isInObjectLitAr && this.ts.lookTok(-1).data == "," && this.ts.lookTok(1).data == ":") {
                         break;
                     }
                     
-                    if (this.ts.lookT(-1).isType('dot')) {
+                    if (this.ts.lookTok(-1).data == ".") {
                         //skip '.'
                         break;
                     }
                     
+                     if (this.mode == 'PASS2_SYMBOL_TREE') {
+
+                        identifier = this.getIdentifier(symbol, currentScope);
+                        //println("<B>??</B>");
+                        if (identifier == false) {
+
+                            if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
+                                // Here, we found an undeclared and un-namespaced symbol that is
+                                // 3 characters or less in length. Declare it in the global scope.
+                                // We don't need to declare longer symbols since they won't cause
+                                // any conflict with other munged symbols.
+                                this.globalScope.declareIdentifier(symbol, token);
+                                this.warn("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')', true);
+                            } else {
+                                //println("undeclared")
+                            }
+                            
+                            
+                        } else {
+                            //println("<B>++</B>");
+                            token.identifier = identifier;
+                            identifier.refcount++;
+                        }
+                        
+                    }
+                    break;
                     
                     
-                    //println("<B>EID</B>");
                     
                     
+                    //println("<B>EID</B>");
+                 case 'KEYW':   
+                 
+                    if (token.name == "FUNCTION") {
+                        
+                        this.parseFunctionDeclaration();
+                        break;
+                    }
+               
+                    
+             
+                    symbol = token.data;
                     if (this.mode == 'BUILDING_SYMBOL_TREE') {
 
                         if (symbol == "eval") {
-                            if (this.ts.look(-1).isDoc()) {
+                            if (this.ts.look(-1).type == 'COMM') {
                                 // look for eval:var:noreplace\n
                                 var _t = this;
                                 this.ts.look(-1).data.replace(/eval:var:([a-z]+)/ig, function(m, a) {
@@ -597,34 +637,9 @@ ScopeParser.prototype = {
                         }
                         break;
                     } 
-                    if (this.mode == 'CHECKING_SYMBOL_TREE') {
-
-                        identifier = this.getIdentifier(symbol, currentScope);
-                        //println("<B>??</B>");
-                        if (identifier == false) {
-
-                            if (symbol.length <= 3 && JSDOC.Scope.builtin.indexOf(symbol) < 0) {
-                                // Here, we found an undeclared and un-namespaced symbol that is
-                                // 3 characters or less in length. Declare it in the global scope.
-                                // We don't need to declare longer symbols since they won't cause
-                                // any conflict with other munged symbols.
-                                this.globalScope.declareIdentifier(symbol, token);
-                                this.warn("Found an undeclared symbol: " + symbol, true);
-                            } else {
-                                //println("undeclared")
-                            }
-                            
-                            
-                        } else {
-                            //println("<B>++</B>");
-                            token.identifier = identifier;
-                            identifier.refcount++;
-                        }
-                        
-                    }
-                    break;
+                   
             }
-            if (!this.ts.nextT()) break;
+            if (!this.ts.nextTok()) break;
         }
     },
 
@@ -638,9 +653,9 @@ ScopeParser.prototype = {
 
         //token = getToken(-1);
         //assert token.getType() == Token.CATCH;
-        token = this.ts.nextT();
+        token = this.ts.nextTok();
         //assert token.getType() == Token.LP; (
-        token = this.ts.nextT();
+        token = this.ts.nextTok();
         //assert token.getType() == Token.NAME;
         
         symbol = token.data;
@@ -657,23 +672,23 @@ ScopeParser.prototype = {
             identifier.refcount++;
         }
 
-        token = this.ts.nextT();
+        token = this.ts.nextTok();
         //assert token.getType() == Token.RP; // )
     },
     
     parseFunctionDeclaration : function() 
     {
-
+       // print("PARSE FUNCTION");
         var symbol;
-        var  token;
+        var token;
         var currentScope  = false; 
         var fnScope = false;
         var identifier;
         //this.logR("<B>PARSING FUNCTION</B>");
         currentScope = this.scopes[this.scopes.length-1];
 
-        token = this.ts.nextT();
-        if (token.isType('identifier')) {
+        token = this.ts.nextTok();
+        if (token.type == "NAME") {
             if (this.mode == 'BUILDING_SYMBOL_TREE') {
                 // Get the name of the function and declare it in the current scope.
                 symbol = token.data;
@@ -682,7 +697,7 @@ ScopeParser.prototype = {
                 }
                 currentScope.declareIdentifier(symbol,token);
             }
-            token =  this.ts.nextT();
+            token =  this.ts.nextTok();
         }
 
         //assert token.getType() == Token.LP;
@@ -700,12 +715,12 @@ ScopeParser.prototype = {
         
         // Parse function arguments.
         var argpos = 0;
-        while (!this.ts.lookT().isType('rparen')) { //(token = consumeToken()).getType() != Token.RP) {
-            token = this.ts.nextT();
-           
+        while (this.ts.lookTok().data != ')') { //(token = consumeToken()).getType() != Token.RP) {
+            token = this.ts.nextTok();
+           // print ("FUNC ARGS: " + token.toString())
             //assert token.getType() == Token.NAME ||
             //        token.getType() == Token.COMMA;
-            if (token.isType('identifier') && this.mode == 'BUILDING_SYMBOL_TREE') {
+            if (token.type == 'NAME' && this.mode == 'BUILDING_SYMBOL_TREE') {
                 symbol = token.data;
                 identifier = fnScope.declareIdentifier(symbol,token);
                 if (symbol == "$super" && argpos == 0) {
@@ -716,12 +731,12 @@ ScopeParser.prototype = {
             }
         }
 
-        token = this.ts.nextT();
+        token = this.ts.nextTok();
         // assert token.getType() == Token.LC;
         this.braceNesting++;
 
-        token = this.ts.nextT();
-        if (token.isType('string') && this.ts.lookT(1).isType('semicolon')) {
+        token = this.ts.nextTok();
+        if (token.type == "STRN" && this.ts.lookTok(1).data == ';') {
             /*
             
             NOT SUPPORTED YET!?!!?!
@@ -806,4 +821,4 @@ ScopeParser.prototype = {
         }
         return false;
     }
-});
\ No newline at end of file
+};
\ No newline at end of file