private void parseExpression() { // Parse the expression until we encounter a comma or a semi-colon // in the same brace nesting, bracket nesting(方括号嵌套) and paren nesting. // Parse functions if any... String symbol; JavaScriptToken token; ScriptOrFnScope currentScope; JavaScriptIdentifier identifier; int expressionBraceNesting = braceNesting; // 花括号嵌套 int bracketNesting = 0; // 方括号嵌套 int parensNesting = 0; // int length = tokens.size(); while (offset < length) { token = consumeToken(); currentScope = getCurrentScope(); // return scopes.peek(); switch (token.getType()) { case Token.SEMI: case Token.COMMA: if (braceNesting == expressionBraceNesting && bracketNesting == 0 && parensNesting == 0) { return; } break; case Token.FUNCTION: parseFunctionDeclaration(); break; case Token.LC: braceNesting++; break; case Token.RC: braceNesting--; assert braceNesting >= expressionBraceNesting; break; case Token.LB: bracketNesting++; break; case Token.RB: bracketNesting--; break; case Token.LP: parensNesting++; break; case Token.RP: parensNesting--; break; case Token.CONDCOMMENT: if (mode == BUILDING_SYMBOL_TREE) { protectScopeFromObfuscation(currentScope); warn("不推荐使用JScript条件注释。" + (munge ? "使用JScript条件注释会降低压缩水平!" : ""), true); } break; case Token.NAME: symbol = token.getValue(); if (mode == BUILDING_SYMBOL_TREE) { if (symbol.equals("eval")) { protectScopeFromObfuscation(currentScope); warn("不推荐使用'eval'关键字。" + (munge ? "使用'eval'会降低压缩水平!" : ""), true); } } else if (mode == CHECKING_SYMBOL_TREE) { if ((offset < 2 || (getToken(-2).getType() != Token.DOT && getToken(-2).getType() != Token.GET && getToken(-2).getType() != Token.SET)) && getToken(0).getType() != Token.OBJECTLIT) { identifier = getIdentifier(symbol, currentScope); if (identifier == null) { if (symbol.length() <= 3 && !builtin.contains(symbol)) { // 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. globalScope.declareIdentifier(symbol, false); // I removed the warning since was only being // done when // for identifiers 3 chars or less, and was just // causing // noise for people who happen to rely on an // externally // declared variable that happen to be that // short. We either // should always warn or never warn -- the fact // that we // declare the short symbols in the global space // doesn't // change anything. // warn("Found an undeclared symbol: " + symbol, // true); } } else { identifier.incrementRefcount(); } } } break; } } }
private void parseScope(ScriptOrFnScope scope) { String symbol; JavaScriptToken token; JavaScriptIdentifier identifier; int length = tokens.size(); enterScope(scope); // 进入范围(将scope压入栈scopes中) while (offset < length) { // 在此函数调用前已设置为0 token = consumeToken(); // 消费tokens,offset自增 switch (token.getType()) { case Token.VAR: // 注意后面没有break; if (mode == BUILDING_SYMBOL_TREE) { scope.incrementVarCount(); // 当前范围内的var数量加1 } /* FALLSTHROUGH(falls through) 失败 */ case Token.CONST: // The var keyword is followed by at least one symbol name. // If several symbols follow, they are comma(逗号) separated. for (; ; ) { token = consumeToken(); assert token.getType() == Token.NAME; // 判断关键字var、const后的变量是否已经定义,未定义则定义,已定义则输出警告 if (mode == BUILDING_SYMBOL_TREE) { symbol = token.getValue(); if (scope.getIdentifier(symbol) == null) { scope.declareIdentifier(symbol, true); // 声明标识符 } else { warn("变量" + symbol + "已经在相同的作用域中声明", true); } } token = getToken(0); // offset不自增,token.getValue()的值一般为'='或';' assert token.getType() == Token.SEMI || token.getType() == Token.ASSIGN || token.getType() == Token.COMMA || token.getType() == Token.IN; if (token.getType() == Token.IN) { break; } else { parseExpression(); // 解释表达式(例如解释var a = 'u';或var a;) token = getToken(-1); // 得到的是';' if (token.getType() == Token.SEMI) { // 变量声明完成 break; } } } break; case Token.FUNCTION: parseFunctionDeclaration(); break; case Token.LC: braceNesting++; break; case Token.RC: braceNesting--; assert braceNesting >= scope.getBraceNesting(); if (braceNesting == scope.getBraceNesting()) { // 说明要离开当前作用域了 leaveCurrentScope(); // 离开当前作用域 return; } break; case Token.WITH: if (mode == BUILDING_SYMBOL_TREE) { // Inside a 'with' block, it is impossible to figure out // statically whether a symbol is a local variable or an // object member. As a consequence, the only thing we can // do is turn the obfuscation off for the highest scope // containing the 'with' block. protectScopeFromObfuscation(scope); warn("不推荐使用with关键字。" + (munge ? " 使用with会降低压缩水平" : ""), true); } break; case Token.CATCH: parseCatch(); break; case Token.CONDCOMMENT: if (mode == BUILDING_SYMBOL_TREE) { protectScopeFromObfuscation(scope); warn("不推荐使用JScript条件注释。" + (munge ? "使用JScript条件解释会降低压缩水平。" : ""), true); } break; case Token.NAME: symbol = token.getValue(); if (mode == BUILDING_SYMBOL_TREE) { if (symbol.equals("eval")) { protectScopeFromObfuscation(scope); warn("不推荐使用'eval'关键字。" + (munge ? "使用'eval'会降低压缩水平!" : ""), true); } } else if (mode == CHECKING_SYMBOL_TREE) { if ((offset < 2 || getToken(-2).getType() != Token.DOT) && getToken(0).getType() != Token.OBJECTLIT) { identifier = getIdentifier(symbol, scope); if (identifier == null) { if (symbol.length() <= 3 && !builtin.contains(symbol)) { // 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. globalScope.declareIdentifier(symbol, false); // warn("Found an undeclared symbol: " + symbol, // true); } } else { identifier.incrementRefcount(); } } } break; } } }
private static boolean isValidIdentifier(String s) { Matcher m = SIMPLE_IDENTIFIER_NAME_PATTERN.matcher(s); return (m.matches() && !reserved.contains(s)); }