use of org.mozilla.javascript.ast.VariableDeclaration in project HL4A by HL4A.
the class Parser method statementHelper.
private AstNode statementHelper() throws IOException {
// If the statement is set, then it's been told its label by now.
if (currentLabel != null && currentLabel.getStatement() != null)
currentLabel = null;
AstNode pn = null;
int tt = peekToken(), pos = ts.tokenBeg;
switch(tt) {
case Token.IF:
return ifStatement();
case Token.SWITCH:
return switchStatement();
case Token.WHILE:
return whileLoop();
case Token.DO:
return doLoop();
case Token.FOR:
return forLoop();
case Token.TRY:
return tryStatement();
case Token.THROW:
pn = throwStatement();
break;
case Token.BREAK:
pn = breakStatement();
break;
case Token.CONTINUE:
pn = continueStatement();
break;
case Token.WITH:
if (this.inUseStrictDirective) {
reportError("msg.no.with.strict");
}
return withStatement();
case Token.CONST:
case Token.VAR:
consumeToken();
int lineno = ts.lineno;
pn = variables(currentToken, ts.tokenBeg, true);
pn.setLineno(lineno);
break;
case Token.LET:
pn = letStatement();
if (pn instanceof VariableDeclaration && peekToken() == Token.SEMI)
break;
return pn;
case Token.RETURN:
case Token.YIELD:
pn = returnOrYield(tt, false);
break;
case Token.DEBUGGER:
consumeToken();
pn = new KeywordLiteral(ts.tokenBeg, ts.tokenEnd - ts.tokenBeg, tt);
pn.setLineno(ts.lineno);
break;
case Token.LC:
return block();
case Token.ERROR:
consumeToken();
return makeErrorNode();
case Token.SEMI:
consumeToken();
pos = ts.tokenBeg;
pn = new EmptyStatement(pos, ts.tokenEnd - pos);
pn.setLineno(ts.lineno);
return pn;
case Token.FUNCTION:
consumeToken();
return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT);
case Token.DEFAULT:
pn = defaultXmlNamespace();
break;
case Token.NAME:
pn = nameOrLabel();
if (pn instanceof ExpressionStatement)
break;
// LabeledStatement
return pn;
default:
lineno = ts.lineno;
pn = new ExpressionStatement(expr(), !insideFunction());
pn.setLineno(lineno);
break;
}
autoInsertSemicolon(pn);
return pn;
}
use of org.mozilla.javascript.ast.VariableDeclaration in project HL4A by HL4A.
the class Parser method forLoop.
private Loop forLoop() throws IOException {
if (currentToken != Token.FOR)
codeBug();
consumeToken();
int forPos = ts.tokenBeg, lineno = ts.lineno;
boolean isForEach = false, isForIn = false, isForOf = false;
int eachPos = -1, inPos = -1, lp = -1, rp = -1;
// init is also foo in 'foo in object'
AstNode init = null;
// cond is also object in 'foo in object'
AstNode cond = null;
AstNode incr = null;
Loop pn = null;
Scope tempScope = new Scope();
// decide below what AST class to use
pushScope(tempScope);
try {
// See if this is a for each () instead of just a for ()
if (matchToken(Token.NAME)) {
if ("each".equals(ts.getString())) {
isForEach = true;
eachPos = ts.tokenBeg - forPos;
} else {
reportError("msg.no.paren.for");
}
}
if (mustMatchToken(Token.LP, "msg.no.paren.for"))
lp = ts.tokenBeg - forPos;
int tt = peekToken();
init = forLoopInit(tt);
if (matchToken(Token.IN)) {
isForIn = true;
inPos = ts.tokenBeg - forPos;
// object over which we're iterating
cond = expr();
} else if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6 && matchToken(Token.NAME) && "of".equals(ts.getString())) {
isForOf = true;
inPos = ts.tokenBeg - forPos;
// object over which we're iterating
cond = expr();
} else {
// ordinary for-loop
mustMatchToken(Token.SEMI, "msg.no.semi.for");
if (peekToken() == Token.SEMI) {
// no loop condition
cond = new EmptyExpression(ts.tokenBeg, 1);
cond.setLineno(ts.lineno);
} else {
cond = expr();
}
mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");
int tmpPos = ts.tokenEnd;
if (peekToken() == Token.RP) {
incr = new EmptyExpression(tmpPos, 1);
incr.setLineno(ts.lineno);
} else {
incr = expr();
}
}
if (mustMatchToken(Token.RP, "msg.no.paren.for.ctrl"))
rp = ts.tokenBeg - forPos;
if (isForIn || isForOf) {
ForInLoop fis = new ForInLoop(forPos);
if (init instanceof VariableDeclaration) {
// check that there was only one variable given
if (((VariableDeclaration) init).getVariables().size() > 1) {
reportError("msg.mult.index");
}
}
if (isForOf && isForEach) {
reportError("msg.invalid.for.each");
}
fis.setIterator(init);
fis.setIteratedObject(cond);
fis.setInPosition(inPos);
fis.setIsForEach(isForEach);
fis.setEachPosition(eachPos);
fis.setIsForOf(isForOf);
pn = fis;
} else {
ForLoop fl = new ForLoop(forPos);
fl.setInitializer(init);
fl.setCondition(cond);
fl.setIncrement(incr);
pn = fl;
}
// replace temp scope with the new loop object
currentScope.replaceWith(pn);
popScope();
// We have to parse the body -after- creating the loop node,
// so that the loop node appears in the loopSet, allowing
// break/continue statements to find the enclosing loop.
enterLoop(pn);
try {
AstNode body = statement();
pn.setLength(getNodeEnd(body) - forPos);
pn.setBody(body);
} finally {
exitLoop();
}
} finally {
if (currentScope == tempScope) {
popScope();
}
}
pn.setParens(lp, rp);
pn.setLineno(lineno);
return pn;
}
Aggregations