From 939180352c4de01cd41bbe77ea7ac24c217aa6e2 Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Wed, 25 Nov 2015 16:41:41 +0800 Subject: [PATCH] JSDOC/ScopeParser.vala --- JSDOC/ScopeParser.vala | 137 ++++++++++++++++++++++++----------------- 1 file changed, 81 insertions(+), 56 deletions(-) diff --git a/JSDOC/ScopeParser.vala b/JSDOC/ScopeParser.vala index 65487a1..0a91510 100644 --- a/JSDOC/ScopeParser.vala +++ b/JSDOC/ScopeParser.vala @@ -15,26 +15,26 @@ namespace JSDOC { Gee.ArrayList warnings; bool debug = false; - string[] idents; + static Gee.ArrayList idents; + static bool initialized = false; - Scope global ; + Scope globalScope; ScopeParserMode mode; //braceNesting : 0, Gee.HashMap indexedScopes; bool munge = true; int expN = 0; - - public ScopeParser(TokenStream ts) { - this.ts = ts; // {TokenStream} - this.warnings = new Gee.ArrayList(); - this.globalScope = new Scope(-1, false, -1, ''); - this.indexedScopes = new Gee.HashMap(); - - //this.indexedg = {}; - //this.timer = new Date() * 1; - this.idents = { + int braceNesting = 0; + + + static void init() + { + if (ScopeParser.initialized) { + return; + } + string[] identsar = { "break", "case", @@ -99,6 +99,22 @@ namespace JSDOC { "include", "undefined" }; + ScopeParser.idents = new Gee.ArrayList(); + for(var i = 0 ; i < identsar.length;i++) { + ScopeParser.idents.add(identsar[i]); + } + } + + + public ScopeParser(TokenStream ts) { + this.ts = ts; // {TokenStream} + this.warnings = new Gee.ArrayList(); + + this.globalScope = new Scope(-1, null, -1, null); + this.indexedScopes = new Gee.HashMap(); + + //this.indexedg = {}; + //this.timer = new Date() * 1; } @@ -119,7 +135,7 @@ namespace JSDOC { - void buildSymbolTree() + public void buildSymbolTree() { //println("
");
 			
@@ -129,7 +145,7 @@ namespace JSDOC {
 		   // print(JSON.stringify(this.ts.tokens, null,4));
 			
 			
-			this.globalScope = new  Scope(-1, false, -1, '');
+			this.globalScope =new  Scope(-1, null, -1, null);
 			this.indexedScopes = new Gee.HashMap();
 			this.indexedScopes.set(0, this.globalScope );
 			
@@ -141,7 +157,7 @@ namespace JSDOC {
 			
 		}
 	
-		void mungeSymboltree()
+		public void mungeSymboltree()
 		{
 
 			if (!this.munge) {
@@ -232,7 +248,7 @@ namespace JSDOC {
 			    //if (token.type == 'NAME') {
 			    //    print('*' + token.data);
 			    //}
-			    switch(token.type + '.' + token.name) {
+			    switch(token.type + "." + token.name) {
 			        case "KEYW.VAR":
 			        case "KEYW.CONST": // not really relivant as it's only mozzy that does this.
 			            //print('SCOPE-VAR:' + token.toString());
@@ -251,10 +267,7 @@ namespace JSDOC {
 			                }
 			                //this.logR("parseScope GOT VAR  : " + token.toString() + ""); 
 			                if (token.type != "NAME") {
-			            		
-			                    for(var i = int.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
-			                        print(this.ts.tokens[i].toString());
-			                    }
+			            		this.ts.printRange( int.max(this.ts.cursor-10,0), this.ts.cursor);
 			                    
 			                    print( "var without ident");
 			                    GLib.Process.exit (0);
@@ -268,7 +281,8 @@ namespace JSDOC {
 			                        scope.declareIdentifier(token.data, token);
 			                    } else {
 			                        token.identifier = identifier;
-			                        this.warn("(SCOPE) The variable " + token.data  + " (line:" + token.line + ")  has already been declared in the same scope...");
+			                        this.warn("(SCOPE) The variable " + token.data  + 
+			                    		" (line:" + token.line.to_string() + ")  has already been declared in the same scope...");
 			                    }
 			                }
 
@@ -287,7 +301,7 @@ namespace JSDOC {
 			                    var bn = this.braceNesting;
 			                    var nts = new Gee.ArrayList();
 			                    while (true) {
-			                        if (!token || token.type == "VOID" || token.data == ",") {
+			                        if (token == null  || token.type == "VOID" || token.data == ",") {
 			                            break;
 			                        }
 			                        nts.add(token);
@@ -334,7 +348,7 @@ namespace JSDOC {
 			            //print('SCOPE-CURLY/PAREN:' + token.toString());
 			            //println(""+token.data+"");
 			            var curTS = this.ts;
-			            if (token.props.size() > 0) {
+			            if (token.props.size > 0) {
 			                
 			                // { a : ... , c : .... }
 			                var iter = token.props.map_iterator();
@@ -370,7 +384,7 @@ namespace JSDOC {
 			            
 			            
 	 	            for (var xx =0; xx < token.items.size; xx++) {
-						expr = token.items.get(xx);
+						var expr = token.items.get(xx);
 			            //token.items.forEach(function(expr) {
 			                    //print(expr.toString());
 			                   this.ts = new TokenStream(expr);
@@ -404,7 +418,8 @@ namespace JSDOC {
 			                // do is turn the obfuscation off for the highest scope
 			                // containing the 'with' block.
 			                this.protectScopeFromObfuscation(scope);
-			                this.warn("Using 'with' is not recommended." + (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""), true);
+			                this.warn("Using 'with' is not recommended." +
+			            		 (this.munge ? " Moreover, using 'with' reduces the level of compression!" : ""));
 			            }
 			            break;
 
@@ -425,7 +440,7 @@ namespace JSDOC {
 			                isObjectLitAr.add(true);
 			                //print(">>>>>> OBJLIT REPUSH(true)");
 			            }
-			            isInObjectLitAr = isObjectLitAr.get(isObjectLitAr.size-1);
+			            var isInObjectLitAr = isObjectLitAr.get(isObjectLitAr.size-1);
 			            
 			            if (isInObjectLitAr &&  this.ts.lookTok(1).data == ":" &&
 			                ( this.ts.lookTok(-1).data == "{"  ||  this.ts.lookTok(-1).data == ":" )) {
@@ -434,7 +449,7 @@ namespace JSDOC {
 			                // should do a bit more checking!!!! (what about wierd char's in the string..
 			                var str = token.data.substring(1,token.data.length-1);
 			                
-			                if (Regex.match_simple ("^[a-z_]+$", str,GLib.RegexCompileFlags.CASELESS) && this.idents.index_of(str) < 0) {
+			                if (Regex.match_simple ("^[a-z_]+$", str,GLib.RegexCompileFlags.CASELESS) && ScopeParser.idents.index_of(str) < 0) {
 			                    token.outData = str;
 			                }
 			                
@@ -458,7 +473,7 @@ namespace JSDOC {
 			            }
 			            //print("SYMBOL: " + token.toString());
 			            
-			            symbol = token.data;
+			            var symbol = token.data;
 			            if (symbol == "this") {
 			                break;
 			            }
@@ -476,13 +491,13 @@ namespace JSDOC {
 			                
 			                if (identifier == null) {
 	// BUG!find out where builtin is defined...
-			                    if (symbol.length <= 3 &&  Scope.builtin.indexOf(symbol) < 0) {
+			                    if (symbol.length <= 3 &&  Scope.builtin.index_of(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);
+			                        this.warn("Found an undeclared symbol: " + symbol + " (line:" + token.line.to_string() + ")");
 			                    }
 			                    
 			                    //println("GOT IDENT IGNORE(3): " + symbol + "
"); @@ -501,7 +516,7 @@ namespace JSDOC { //print('SCOPE-KEYW:' + token.toString()); // print("Check eval:"); - symbol = token.data; + var symbol = token.data; if (this.mode == ScopeParserMode.BUILDING_SYMBOL_TREE) { @@ -510,7 +525,7 @@ namespace JSDOC { //print(JSON.stringify(token, null,4)); // look back one and see if we can find a comment!!! //if (this.ts.look(-1).type == "COMM") { - if (token.prefix && token.prefix.match(/eval/)) { + if (token.prefix.length > 0 && Regex.match_simple ("eval",token.prefix)) { // look for eval:var:noreplace\n //print("MATCH!?"); var _t = this; @@ -521,11 +536,12 @@ namespace JSDOC { var a = match_info.fetch(0); var hi = this.getIdentifier(a, scope, token); // println("PROTECT "+a+" from munge" + (hi ? "FOUND" : "MISSING")); - if (hi.length > 0) { + if (hi != null) { // print("PROTECT "+a+" from munge"); //print(JSON.stringify(hi,null,4)); hi.toMunge = false; } + return false; }); @@ -534,7 +550,8 @@ namespace JSDOC { this.protectScopeFromObfuscation(scope); - 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); + 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!" : "")); } } @@ -575,12 +592,12 @@ namespace JSDOC { var expressionBraceNesting = this.braceNesting + 0; var bracketNesting = 0; var parensNesting = 0; - var isInObjectLitAr; + var isObjectLitAr = new Gee.ArrayList(); isObjectLitAr.add( false); - + Token token; //print(scopeIndent + ">> ENTER EXPRESSION" + this.expN); while (null != (token = this.ts.nextTok())) { @@ -730,13 +747,13 @@ namespace JSDOC { //println("??"); if (identifier == null) { - if (symbol.length <= 3 && Scope.builtin.indexOf(symbol) < 0) { + if (symbol.length <= 3 && Scope.builtin.index_of(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); + this.warn("Found an undeclared symbol: " + symbol + " (line:" + token.line.to_string() + ")"); //print("Found an undeclared symbol: " + symbol + ' (line:' + token.line + ')'); //throw "OOPS"; } else { @@ -801,7 +818,8 @@ namespace JSDOC { } else { this.protectScopeFromObfuscation(scope); - this.warn("Using 'eval' is not recommended." + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : ""), true); + this.warn("Using 'eval' is not recommended." + + (this.munge ? " Moreover, using 'eval' reduces the level of compression!" : "")); } @@ -825,8 +843,8 @@ namespace JSDOC { //token = getToken(-1); //assert token.getType() == Token.CATCH; - var token = this.ts.nextTok(1); - token = this.ts.nextTok(1); + var token = this.ts.nextTok(); + token = this.ts.nextTok(); //print(JSON.stringify(this.ts,null,4)); @@ -856,8 +874,7 @@ namespace JSDOC { { //print("PARSE FUNCTION"); - var fnScope = false; - + var b4braceNesting = this.braceNesting + 0; //this.logR("PARSING FUNCTION"); @@ -867,9 +884,9 @@ namespace JSDOC { if (token.type == "NAME") { if (this.mode == ScopeParserMode.BUILDING_SYMBOL_TREE) { // Get the name of the function and declare it in the current scope. - symbol = token.data; - if (scope.getIdentifier(symbol,token) != false) { - this.warn("The function " + symbol + " has already been declared in the same scope...", true); + var symbol = token.data; + if (scope.getIdentifier(symbol,token) != null) { + this.warn("The function " + symbol + " has already been declared in the same scope..."); } scope.declareIdentifier(symbol,token); } @@ -887,11 +904,11 @@ namespace JSDOC { Scope fnScope; //assert token.getType() == Token.LP; if (this.mode == ScopeParserMode.BUILDING_SYMBOL_TREE) { - fnScope = new Scope(1, scope, token.n, '', token); + fnScope = new Scope(1, scope, token.id, token); - //println("STORING SCOPE" + this.ts.cursor); + //println("STORING SCOPE" + this.ts.cursor); - this.indexedScopes[token.id] = fnScope; + this.indexedScopes.set(token.id,fnScope); } else { //qln("FETCHING SCOPE" + this.ts.cursor); @@ -912,7 +929,7 @@ namespace JSDOC { var identifier = fnScope.declareIdentifier(symbol,token); if (symbol == "$super" && argpos == 0) { // Exception for Prototype 1.6... - identifier.preventMunging(); + identifier.toMunge = false; } //argpos++; } @@ -955,17 +972,17 @@ namespace JSDOC { } // Find the highest local scope containing the specified scope. - while (scope && scope.parent != this.globalScope) { + while (scope != null && scope.parent != this.globalScope) { scope = scope.parent; } - + //assert scope.getParentScope() == globalScope; scope.preventMunging(); } - - Token? getIdentifier(string symbol, Scope in_scope, Token token) + + Identifier? getIdentifier(string symbol, Scope in_scope, Token token) { - Token identifier; + Identifier identifier; var scope = in_scope; while (scope != null) { identifier = scope.getIdentifier(symbol, token); @@ -977,5 +994,13 @@ namespace JSDOC { } return null; } + public void printWarnings() + { + foreach(var w in this.warnings) { + print("%s\n",w); + } + } + } -} \ No newline at end of file +} + -- 2.39.2