From: Fabien Potencier Date: Tue, 8 Jun 2010 09:23:12 +0000 (+0200) Subject: made parseArrayExpression autonomous X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=3c22324909295d64ed4177bc5e3b1b0841e58c20;p=web%2Fkonrad%2Ftwig.git made parseArrayExpression autonomous --- diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index af6eb84..b9d6723 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -269,9 +269,7 @@ class Twig_ExpressionParser default: if ($token->test(Twig_Token::OPERATOR_TYPE, '[')) { - $this->parser->getStream()->next(); $node = $this->parseArrayExpression(); - $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, ']'); } elseif ($token->test(Twig_Token::OPERATOR_TYPE, '(')) { $this->parser->getStream()->next(); $node = $this->parseExpression(); @@ -290,38 +288,43 @@ class Twig_ExpressionParser public function parseArrayExpression() { + $stream = $this->parser->getStream(); + $stream->expect(Twig_Token::OPERATOR_TYPE, '['); $elements = array(); - while (!$this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, ']')) { + while (!$stream->test(Twig_Token::OPERATOR_TYPE, ']')) { if (!empty($elements)) { - $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, ','); + $stream->expect(Twig_Token::OPERATOR_TYPE, ','); // trailing ,? - if ($this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, ']')) { + if ($stream->test(Twig_Token::OPERATOR_TYPE, ']')) { + $stream->expect(Twig_Token::OPERATOR_TYPE, ']'); + return new Twig_Node_Expression_Array($elements, $this->parser->getCurrentToken()->getLine()); } } // hash or array element? if ( - $this->parser->getStream()->test(Twig_Token::STRING_TYPE) + $stream->test(Twig_Token::STRING_TYPE) || - $this->parser->getStream()->test(Twig_Token::NUMBER_TYPE) + $stream->test(Twig_Token::NUMBER_TYPE) ) { - if ($this->parser->getStream()->look()->test(Twig_Token::OPERATOR_TYPE, ':')) { + if ($stream->look()->test(Twig_Token::OPERATOR_TYPE, ':')) { // hash - $key = $this->parser->getStream()->next()->getValue(); - $this->parser->getStream()->next(); + $key = $stream->next()->getValue(); + $stream->next(); $elements[$key] = $this->parseExpression(); continue; } - $this->parser->getStream()->rewind(); + $stream->rewind(); } $elements[] = $this->parseExpression(); } + $stream->expect(Twig_Token::OPERATOR_TYPE, ']'); return new Twig_Node_Expression_Array($elements, $this->parser->getCurrentToken()->getLine()); }