function Parser::parseForVarStatement
Parses a for(var ...) statement
Parameters
Token $forToken Token that corresponds to the "for" keyword:
Return value
Node\Node|null
1 call to Parser::parseForVarStatement()
- Parser::parseIterationStatement in vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php - Parses do-while, while, for, for-in and for-of statements
File
-
vendor/
mck89/ peast/ lib/ Peast/ Syntax/ Parser.php, line 1066
Class
- Parser
- Parser class
Namespace
Peast\SyntaxCode
protected function parseForVarStatement($forToken) {
if (!($varToken = $this->scanner
->consume("var"))) {
return null;
}
$state = $this->scanner
->getState();
if (($decl = $this->isolateContext(array(
"allowIn" => false,
), "parseVariableDeclarationList")) && ($varEndPosition = $this->scanner
->getPosition()) && $this->scanner
->consume(";")) {
$init = $this->createNode("VariableDeclaration", $varToken);
$init->setKind($init::KIND_VAR);
$init->setDeclarations($decl);
$init = $this->completeNode($init, $varEndPosition);
$test = $this->isolateContext(array(
"allowIn" => true,
), "parseExpression");
if ($this->scanner
->consume(";")) {
$update = $this->isolateContext(array(
"allowIn" => true,
), "parseExpression");
if ($this->scanner
->consume(")") && ($body = $this->parseStatement())) {
$node = $this->createNode("ForStatement", $forToken);
$node->setInit($init);
$node->setTest($test);
$node->setUpdate($update);
$node->setBody($body);
return $this->completeNode($node);
}
}
}
else {
$this->scanner
->setState($state);
if ($decl = $this->parseForBinding()) {
$init = null;
if ($this->features->forInInitializer && $decl->getId()
->getType() === "Identifier") {
$init = $this->parseInitializer();
}
if ($init) {
$decl->setInit($init);
$decl->location->end = $init->location->end;
}
$left = $this->createNode("VariableDeclaration", $varToken);
$left->setKind($left::KIND_VAR);
$left->setDeclarations(array(
$decl,
));
$left = $this->completeNode($left);
if ($this->scanner
->consume("in")) {
if ($init && $this->scanner
->getStrictMode()) {
$this->error("For-in variable initializer not allowed in " . "strict mode");
}
if (($right = $this->isolateContext(array(
"allowIn" => true,
), "parseExpression")) && $this->scanner
->consume(")") && ($body = $this->parseStatement())) {
$node = $this->createNode("ForInStatement", $forToken);
$node->setLeft($left);
$node->setRight($right);
$node->setBody($body);
return $this->completeNode($node);
}
}
elseif (!$init && $this->scanner
->consume("of")) {
if (($right = $this->isolateContext(array(
"allowIn" => true,
), "parseAssignmentExpression")) && $this->scanner
->consume(")") && ($body = $this->parseStatement())) {
$node = $this->createNode("ForOfStatement", $forToken);
$node->setLeft($left);
$node->setRight($right);
$node->setBody($body);
return $this->completeNode($node);
}
}
}
}
$this->error();
}