Gee.ArrayList<string> warnings;
bool debug = false;
- string[] idents;
+ static Gee.ArrayList<string> idents;
+ static bool initialized = false;
- Scope global ;
+ Scope globalScope;
ScopeParserMode mode;
//braceNesting : 0,
Gee.HashMap<int,Scope> indexedScopes;
bool munge = true;
int expN = 0;
-
- public ScopeParser(TokenStream ts) {
- this.ts = ts; // {TokenStream}
- this.warnings = new Gee.ArrayList<string>();
- this.globalScope = new Scope(-1, false, -1, '');
- this.indexedScopes = new Gee.HashMap<int,Scope>();
-
- //this.indexedg = {};
- //this.timer = new Date() * 1;
- this.idents = {
+ int braceNesting = 0;
+
+
+ static void init()
+ {
+ if (ScopeParser.initialized) {
+ return;
+ }
+ string[] identsar = {
"break",
"case",
"include",
"undefined"
};
+ ScopeParser.idents = new Gee.ArrayList<string>();
+ 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<string>();
+
+ this.globalScope = new Scope(-1, null, -1, null);
+ this.indexedScopes = new Gee.HashMap<int,Scope>();
+
+ //this.indexedg = {};
+ //this.timer = new Date() * 1;
}
- void buildSymbolTree()
+ public void buildSymbolTree()
{
//println("<PRE>");
// 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<int,Scope>();
this.indexedScopes.set(0, this.globalScope );
}
- void mungeSymboltree()
+ public void mungeSymboltree()
{
if (!this.munge) {
//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());
}
//this.logR("parseScope GOT VAR : <B>" + token.toString() + "</B>");
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);
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...");
}
}
var bn = this.braceNesting;
var nts = new Gee.ArrayList<Token>();
while (true) {
- if (!token || token.type == "VOID" || token.data == ",") {
+ if (token == null || token.type == "VOID" || token.data == ",") {
break;
}
nts.add(token);
//print('SCOPE-CURLY/PAREN:' + token.toString());
//println("<i>"+token.data+"</i>");
var curTS = this.ts;
- if (token.props.size() > 0) {
+ if (token.props.size > 0) {
// { a : ... , c : .... }
var iter = token.props.map_iterator();
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);
// 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;
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 == ":" )) {
// 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;
}
}
//print("SYMBOL: " + token.toString());
- symbol = token.data;
+ var symbol = token.data;
if (symbol == "this") {
break;
}
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): <B>" + symbol + "</B><BR/>");
//print('SCOPE-KEYW:' + token.toString());
// print("Check eval:");
- symbol = token.data;
+ var symbol = token.data;
if (this.mode == ScopeParserMode.BUILDING_SYMBOL_TREE) {
//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;
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;
});
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!" : ""));
}
}
var expressionBraceNesting = this.braceNesting + 0;
var bracketNesting = 0;
var parensNesting = 0;
- var isInObjectLitAr;
- var isObjectLitAr = new Gee.ArrayList<boolean>();
+
+ var isObjectLitAr = new Gee.ArrayList<bool>();
isObjectLitAr.add( false);
-
+ Token token;
//print(scopeIndent + ">> ENTER EXPRESSION" + this.expN);
while (null != (token = this.ts.nextTok())) {
//println("<B>??</B>");
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 {
// println("PROTECT "+a+" from munge");
hi.toMunge = false;
}
-
+ return false;
});
} 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!" : ""));
}
}
- break;
+
}
+ break;
default:
//if (this.mode == 'BUILDING_SYMBOL_TREE')
// print("EXPR-SKIP:" + JSON.stringify(token, null, 4));
}
- void parseCatch(Scoope scope) {
+ void parseCatch(Scope scope) {
//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));
{
//print("PARSE FUNCTION");
- var fnScope = false;
-
+
var b4braceNesting = this.braceNesting + 0;
//this.logR("<B>PARSING FUNCTION</B>");
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);
}
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);
var identifier = fnScope.declareIdentifier(symbol,token);
if (symbol == "$super" && argpos == 0) {
// Exception for Prototype 1.6...
- identifier.preventMunging();
+ identifier.toMunge = false;
}
//argpos++;
}
}
token = this.ts.nextTok();
+ if (token == null) {
+ return;
+ }
//print('FUNC-BODY:' + JSON.stringify(token.items,null,4));
//Seed.quit();
//print(token.toString());
}
// 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);
}
return null;
}
+ public void printWarnings()
+ {
+ foreach(var w in this.warnings) {
+ print("%s\n",w);
+ }
+ }
+
}
-}
\ No newline at end of file
+}
+