Script parsers refactoring

This commit is contained in:
Jindra Petřík
2015-05-29 06:43:09 +02:00
parent 8ebc170326
commit ad1971b7c5
2 changed files with 63 additions and 74 deletions

View File

@@ -2012,22 +2012,11 @@ public class ActionScriptParser {
break;
}
}
switch (lookahead.type) {
case DOT: //member
case BRACKET_OPEN: //member
case PARENT_OPEN: //function call
case FILTER:
lhs = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, lhs, registerVars, inFunction, inMethod, variables);
break;
default:
if (lhs instanceof ParenthesisItem) {
GraphTargetItem coerced = expression(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, allowRemainder, variables);
if (coerced != null && isType(((ParenthesisItem) lhs).value)) {
lhs = new CoerceAVM2Item(null, ((ParenthesisItem) lhs).value, coerced);
}
}
if (lhs instanceof ParenthesisItem) {
GraphTargetItem coerced = expression(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, allowRemainder, variables);
if (coerced != null && isType(((ParenthesisItem) lhs).value)) {
lhs = new CoerceAVM2Item(null, ((ParenthesisItem) lhs).value, coerced);
}
}
if (debugMode) {
@@ -2042,15 +2031,15 @@ public class ActionScriptParser {
}
GraphTargetItem ret = null;
ParsedSymbol s = lex();
boolean allowMemberOrCall = false;
switch (s.type) {
case XML_STARTTAG_BEGIN:
lexer.pushback(s);
ret = xml(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, variables);
break;
case STRING:
ret = new StringAVM2Item(null, s.value.toString());
allowMemberOrCall = true;
break;
case NEGATE:
ret = expression(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, false, variables);
@@ -2086,7 +2075,6 @@ public class ActionScriptParser {
break;
case TYPEOF:
ret = new TypeOfAVM2Item(null, expression(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, false, variables));
break;
case TRUE:
ret = new BooleanAVM2Item(null, true);
@@ -2127,14 +2115,13 @@ public class ActionScriptParser {
}
}
ret = new NewObjectAVM2Item(null, nvs);
ret = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, ret, registerVars, inFunction, inMethod, variables);
allowMemberOrCall = true;
break;
case BRACKET_OPEN: //Array literal or just brackets
lexer.pushback(s);
List<GraphTargetItem> inBrackets = new ArrayList<>();
int arrCnt = brackets(thisType, pkg, needsActivation, importedClasses, openedNamespaces, inBrackets, registerVars, inFunction, inMethod, variables);
ret = new NewArrayAVM2Item(null, inBrackets);
ret = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, ret, registerVars, inFunction, inMethod, variables);
break;
case FUNCTION:
@@ -2147,7 +2134,7 @@ public class ActionScriptParser {
}
needsActivation.setVal(true);
ret = function(pkg, false, needsActivation, importedClasses, 0/*?*/, thisType, openedNamespaces, fname, false, variables);
ret = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, ret, registerVars, inFunction, inMethod, variables);
allowMemberOrCall = true;
break;
case NAN:
ret = new NanAVM2Item(null);
@@ -2193,8 +2180,7 @@ public class ActionScriptParser {
case PARENT_OPEN:
ret = new ParenthesisItem(null, expression(thisType, pkg, needsActivation, importedClasses, openedNamespaces, registerVars, inFunction, inMethod, true, variables));
expectedType(SymbolType.PARENT_CLOSE);
ret = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, ret, registerVars, inFunction, inMethod, variables);
allowMemberOrCall = true;
break;
case NEW:
s = lex();
@@ -2242,30 +2228,25 @@ public class ActionScriptParser {
case SUPER:
case ATTRIBUTE:
lexer.pushback(s);
GraphTargetItem var = name(thisType, pkg, needsActivation, false, openedNamespaces, registerVars, inFunction, inMethod, variables, importedClasses);
var = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, var, registerVars, inFunction, inMethod, variables);
ret = var;
ret = name(thisType, pkg, needsActivation, false, openedNamespaces, registerVars, inFunction, inMethod, variables, importedClasses);
allowMemberOrCall = true;
//var = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, var, registerVars, inFunction, inMethod, variables);
//ret = var;
break;
default:
GraphTargetItem excmd = expressionCommands(s, registerVars, inFunction, inMethod, -1, variables);
if (excmd != null) {
//?
ret = excmd;
allowMemberOrCall = true; //?
break;
}
lexer.pushback(s);
}
/*if (allowRemainder && existsRemainder) {
GraphTargetItem rem = ret;
do {
rem = expressionRemainder(thisType, pkg, needsActivation, openedNamespaces, rem, registerVars, inFunction, inMethod, assocRight, variables, importedClasses);
if (rem != null) {
ret = rem;
}
} while ((!assocRight) && (rem != null));
ret = fixPrecedence(ret);
}*/
if (allowMemberOrCall && ret != null) {
ret = memberOrCall(thisType, pkg, needsActivation, importedClasses, openedNamespaces, ret, registerVars, inFunction, inMethod, variables);
}
if (debugMode) {
System.out.println("/primary");
}

View File

@@ -1565,6 +1565,7 @@ public class ActionScriptParser {
if (debugMode) {
System.out.println("primary:");
}
boolean allowMemberOrCall = false;
GraphTargetItem ret = null;
ParsedSymbol s = lex();
@@ -1610,7 +1611,7 @@ public class ActionScriptParser {
break;
case TYPEOF:
ret = new TypeOfActionItem(null, expression(inFunction, inMethod, false, variables));
allowMemberOrCall = true;
break;
case TRUE:
ret = new DirectValueActionItem(null, 0, Boolean.TRUE, new ArrayList<String>());
@@ -1646,12 +1647,14 @@ public class ActionScriptParser {
}
}
ret = new InitObjectActionItem(null, objectNames, objectValues);
allowMemberOrCall = true;
break;
case BRACKET_OPEN: //Array literal or just brackets
lexer.pushback(s);
List<GraphTargetItem> inBrackets = new ArrayList<>();
int arrCnt = brackets(inBrackets, inFunction, inMethod, variables);
ret = new InitArrayActionItem(null, inBrackets);
allowMemberOrCall = true;
break;
case FUNCTION:
s = lex();
@@ -1662,15 +1665,15 @@ public class ActionScriptParser {
lexer.pushback(s);
}
ret = function(true, fname, false, variables);
allowMemberOrCall = true;
break;
case STRING:
ret = pushConst(s.value.toString());
//ret = memberOrCall(ret, inFunction, inMethod, variables);
allowMemberOrCall = true;
break;
case NEWLINE:
ret = new DirectValueActionItem(null, 0, "\r", new ArrayList<String>());
allowMemberOrCall = true;
break;
case NAN:
ret = new DirectValueActionItem(null, 0, Double.NaN, new ArrayList<String>());
@@ -1713,7 +1716,7 @@ public class ActionScriptParser {
ret = new ParenthesisItem(null, expression(inFunction, inMethod, true, variables));
expectedType(SymbolType.PARENT_CLOSE);
//ret = memberOrCall(ret, inFunction, inMethod, variables);
allowMemberOrCall = true;
break;
case NEW:
GraphTargetItem newvar = expressionPrimary(false, inFunction, inMethod, false, variables);//variable(inFunction, inMethod, variables);
@@ -1734,6 +1737,7 @@ public class ActionScriptParser {
expectedType(SymbolType.PARENT_CLOSE);
//evar = memberOrCall(evar, inFunction, inMethod, variables);
ret = evar;
allowMemberOrCall = true;
break;
case IDENTIFIER:
@@ -1744,6 +1748,7 @@ public class ActionScriptParser {
} else {
ret = new VariableActionItem(s.value.toString(), null, false);
variables.add((VariableActionItem) ret);
allowMemberOrCall = true;
}
break;
@@ -1752,43 +1757,14 @@ public class ActionScriptParser {
if (excmd != null) {
//?
ret = excmd;
allowMemberOrCall = true; //?
break;
}
lexer.pushback(s);
}
if (ret != null) {
while (true) {
ParsedSymbol op = lex();
if (op.type == SymbolType.PARENT_OPEN) {
List<GraphTargetItem> args = call(inFunction, inMethod, variables);
if (ret instanceof GetMemberActionItem) {
GetMemberActionItem mem = (GetMemberActionItem) ret;
ret = new CallMethodActionItem(null, mem.object, mem.memberName, args);
} else if (ret instanceof VariableActionItem) {
VariableActionItem var = (VariableActionItem) ret;
ret = new CallFunctionActionItem(null, pushConst(var.getVariableName()), args);
} else {
ret = new CallFunctionActionItem(null, ret, args);
}
continue;
}
if (op.type == SymbolType.BRACKET_OPEN) {
GraphTargetItem rhs = expression(inFunction, inMethod, false, variables);
ret = new GetMemberActionItem(null, ret, rhs);
expectedType(SymbolType.BRACKET_CLOSE);
continue;
}
if (op.type == SymbolType.DOT) {
s = lex();
expected(s, lexer.yyline(), SymbolType.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP);
ret = new GetMemberActionItem(null, ret, pushConst(s.value.toString()));
continue;
}
lexer.pushback(op);
break;
}
if (allowMemberOrCall && ret != null) {
ret = memberOrCall(ret, inFunction, inMethod, variables);
}
if (debugMode) {
System.out.println("/primary");
@@ -1796,6 +1772,38 @@ public class ActionScriptParser {
return ret;
}
private GraphTargetItem memberOrCall(GraphTargetItem ret, boolean inFunction, boolean inMethod, List<VariableActionItem> variables) throws IOException, ActionParseException {
ParsedSymbol op = lex();
while (op.isType(SymbolType.PARENT_OPEN, SymbolType.BRACKET_OPEN, SymbolType.DOT)) {
if (op.type == SymbolType.PARENT_OPEN) {
List<GraphTargetItem> args = call(inFunction, inMethod, variables);
if (ret instanceof GetMemberActionItem) {
GetMemberActionItem mem = (GetMemberActionItem) ret;
ret = new CallMethodActionItem(null, mem.object, mem.memberName, args);
} else if (ret instanceof VariableActionItem) {
VariableActionItem var = (VariableActionItem) ret;
ret = new CallFunctionActionItem(null, pushConst(var.getVariableName()), args);
} else {
ret = new CallFunctionActionItem(null, ret, args);
}
}
if (op.type == SymbolType.BRACKET_OPEN) {
GraphTargetItem rhs = expression(inFunction, inMethod, false, variables);
ret = new GetMemberActionItem(null, ret, rhs);
expectedType(SymbolType.BRACKET_CLOSE);
}
if (op.type == SymbolType.DOT) {
ParsedSymbol s = lex();
expected(s, lexer.yyline(), SymbolType.IDENTIFIER, SymbolType.THIS, SymbolType.SUPER, SymbolType.STRING_OP);
ret = new GetMemberActionItem(null, ret, pushConst(s.value.toString()));
}
op = lex();
}
lexer.pushback(op);
return ret;
}
private DirectValueActionItem pushConst(String s) throws IOException, ActionParseException {
int index = constantPool.indexOf(s);
if (index == -1) {