use of org.mozilla.javascript.ast.GeneratorExpressionLoop in project HL4A by HL4A.
the class IRFactory method genExprTransformHelper.
private Node genExprTransformHelper(GeneratorExpression node) {
decompiler.addToken(Token.LP);
int lineno = node.getLineno();
Node expr = transform(node.getResult());
List<GeneratorExpressionLoop> loops = node.getLoops();
int numLoops = loops.size();
// Walk through loops, collecting and defining their iterator symbols.
Node[] iterators = new Node[numLoops];
Node[] iteratedObjs = new Node[numLoops];
for (int i = 0; i < numLoops; i++) {
GeneratorExpressionLoop acl = loops.get(i);
decompiler.addName(" ");
decompiler.addToken(Token.FOR);
decompiler.addToken(Token.LP);
AstNode iter = acl.getIterator();
String name = null;
if (iter.getType() == Token.NAME) {
name = iter.getString();
decompiler.addName(name);
} else {
// destructuring assignment
decompile(iter);
name = currentScriptOrFn.getNextTempName();
defineSymbol(Token.LP, name, false);
expr = createBinary(Token.COMMA, createAssignment(Token.ASSIGN, iter, createName(name)), expr);
}
Node init = createName(name);
// Define as a let since we want the scope of the variable to
// be restricted to the array comprehension
defineSymbol(Token.LET, name, false);
iterators[i] = init;
if (acl.isForOf()) {
decompiler.addName("of ");
} else {
decompiler.addToken(Token.IN);
}
iteratedObjs[i] = transform(acl.getIteratedObject());
decompiler.addToken(Token.RP);
}
// generate code for tmpArray.push(body)
Node yield = new Node(Token.YIELD, expr, node.getLineno());
Node body = new Node(Token.EXPR_VOID, yield, lineno);
if (node.getFilter() != null) {
decompiler.addName(" ");
decompiler.addToken(Token.IF);
decompiler.addToken(Token.LP);
body = createIf(transform(node.getFilter()), body, null, lineno);
decompiler.addToken(Token.RP);
}
// Now walk loops in reverse to build up the body statement.
int pushed = 0;
try {
for (int i = numLoops - 1; i >= 0; i--) {
GeneratorExpressionLoop acl = loops.get(i);
Scope loop = createLoopNode(// no label
null, acl.getLineno());
pushScope(loop);
pushed++;
body = createForIn(Token.LET, loop, iterators[i], iteratedObjs[i], body, acl.isForEach(), acl.isForOf());
}
} finally {
for (int i = 0; i < pushed; i++) {
popScope();
}
}
decompiler.addToken(Token.RP);
return body;
}
use of org.mozilla.javascript.ast.GeneratorExpressionLoop in project HL4A by HL4A.
the class Parser method generatorExpressionLoop.
private GeneratorExpressionLoop generatorExpressionLoop() throws IOException {
if (nextToken() != Token.FOR)
codeBug();
int pos = ts.tokenBeg;
int lp = -1, rp = -1, inPos = -1;
GeneratorExpressionLoop pn = new GeneratorExpressionLoop(pos);
pushScope(pn);
try {
if (mustMatchToken(Token.LP, "msg.no.paren.for")) {
lp = ts.tokenBeg - pos;
}
AstNode iter = null;
switch(peekToken()) {
case Token.LB:
case Token.LC:
// handle destructuring assignment
iter = destructuringPrimaryExpr();
markDestructuring(iter);
break;
case Token.NAME:
consumeToken();
iter = createNameNode();
break;
default:
reportError("msg.bad.var");
}
// be restricted to the array comprehension
if (iter.getType() == Token.NAME) {
defineSymbol(Token.LET, ts.getString(), true);
}
if (mustMatchToken(Token.IN, "msg.in.after.for.name"))
inPos = ts.tokenBeg - pos;
AstNode obj = expr();
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
rp = ts.tokenBeg - pos;
pn.setLength(ts.tokenEnd - pos);
pn.setIterator(iter);
pn.setIteratedObject(obj);
pn.setInPosition(inPos);
pn.setParens(lp, rp);
return pn;
} finally {
popScope();
}
}
use of org.mozilla.javascript.ast.GeneratorExpressionLoop in project HL4A by HL4A.
the class Parser method generatorExpression.
private AstNode generatorExpression(AstNode result, int pos, boolean inFunctionParams) throws IOException {
List<GeneratorExpressionLoop> loops = new ArrayList<GeneratorExpressionLoop>();
while (peekToken() == Token.FOR) {
loops.add(generatorExpressionLoop());
}
int ifPos = -1;
ConditionData data = null;
if (peekToken() == Token.IF) {
consumeToken();
ifPos = ts.tokenBeg - pos;
data = condition();
}
if (!inFunctionParams) {
mustMatchToken(Token.RP, "msg.no.paren.let");
}
GeneratorExpression pn = new GeneratorExpression(pos, ts.tokenEnd - pos);
pn.setResult(result);
pn.setLoops(loops);
if (data != null) {
pn.setIfPosition(ifPos);
pn.setFilter(data.condition);
pn.setFilterLp(data.lp - pos);
pn.setFilterRp(data.rp - pos);
}
return pn;
}
Aggregations