One should only be able to assign to variables
authornikic <+@ni-po.com>
Mon, 20 Dec 2010 17:36:34 +0000 (18:36 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Mon, 20 Dec 2010 18:03:15 +0000 (19:03 +0100)
lib/Twig/ExpressionParser.php

index 21d1e42..fdcbbe0 100644 (file)
@@ -109,7 +109,7 @@ class Twig_ExpressionParser
         return $token->test(Twig_Token::OPERATOR_TYPE) && isset($this->binaryOperators[$token->getValue()]);
     }
 
-    public function parsePrimaryExpression($assignment = false)
+    public function parsePrimaryExpression()
     {
         $token = $this->parser->getCurrentToken();
         switch ($token->getType()) {
@@ -129,8 +129,7 @@ class Twig_ExpressionParser
                         break;
 
                     default:
-                        $cls = $assignment ? 'Twig_Node_Expression_AssignName' : 'Twig_Node_Expression_Name';
-                        $node = new $cls($token->getValue(), $token->getLine());
+                        $node = new Twig_Node_Expression_Name($token->getValue(), $token->getLine());
                 }
                 break;
 
@@ -150,11 +149,7 @@ class Twig_ExpressionParser
                 }
         }
 
-        if (!$assignment) {
-            $node = $this->parsePostfixExpression($node);
-        }
-
-        return $node;
+        return $this->parsePostfixExpression($node);
     }
 
     public function parseArrayExpression()
@@ -315,22 +310,25 @@ class Twig_ExpressionParser
 
     public function parseAssignmentExpression()
     {
-        $lineno = $this->parser->getCurrentToken()->getLine();
         $targets = array();
         while (true) {
-            if (!empty($targets)) {
-                $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Multiple assignments must be separated by a comma (,)');
-            }
             if ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ')') ||
                     $this->parser->getStream()->test(Twig_Token::VAR_END_TYPE) ||
                     $this->parser->getStream()->test(Twig_Token::BLOCK_END_TYPE))
             {
                 break;
             }
-            $targets[] = $this->parsePrimaryExpression(true);
+
+            $token = $this->parser->getStream()->expect(Twig_Token::NAME_TYPE, null, 'Only variables can be assigned to');
+            if (in_array($token->getValue(), array('true', 'false', 'none'))) {
+                throw new Twig_Error_Syntax($token->getValue() . ' cannot be assigned to');
+            }
+            $targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine());
+
             if (!$this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, ',')) {
                 break;
             }
+            $this->parser->getStream()->next();
         }
 
         return new Twig_Node($targets);