public function parse(Parser $parser, Stream $stream) { $environment = $parser->getEnvironment(); $node = new TagNode($environment->getTag('for'), ['save_temp_var' => true, 'create_stack' => true, 'variables' => 1]); $node->addChild($parser->parseExpression($stream), 'source'); $temp = $node->addChild(new TempVariableNode('element'), 'loop_variable_0'); $loopBody = $node->addChild(new RootNode(), 'loop_body'); if ($stream->current()->test(Token::IDENTIFIER, 'as')) { $arrayNode = new ArrayNode(); $arrayNode->add($temp, new DataNode($stream->expect(Token::VARIABLE)->getValue())); $setOperator = $parser->getEnvironment()->getBinaryOperators()->getOperator(':'); $varNode = $setOperator->createNode($temp, $arrayNode); $loopBody->addChild(new ExpressionNode($varNode)); $stream->next(); } $stream->expectCurrent(Token::IDENTIFIER, 'using'); $loopBody->addChild($this->helper->createRenderFunctionNode($parser->parseExpression($stream), $temp)); return $node; }
private function parseArray() { $node = new ArrayNode(); $token = $this->stream->current(); //iterate over tokens while (!$token->test(Token::PUNCTUATION, ']')) { //Checking here allows for a trailing comma. if ($this->stream->nextTokenIf(Token::PUNCTUATION, ']')) { break; } //expressions are allowed as both array keys and values. $value = $this->parseExpression(true); if ($this->stream->current()->test(Token::PUNCTUATION, [':', '=>'])) { //the previous value was a key $key = $value; $value = $this->parseExpression(true); } else { $key = null; } $node->add($value, $key); $token = $this->stream->expectCurrent(Token::PUNCTUATION, [',', ']']); } //push array node to operand stack $this->operandStack->push($node); }