made parseArrayExpression autonomous
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 8 Jun 2010 09:23:12 +0000 (11:23 +0200)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 8 Jun 2010 09:23:30 +0000 (11:23 +0200)
lib/Twig/ExpressionParser.php

index af6eb84..b9d6723 100644 (file)
@@ -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());
     }