From: nikic <+@ni-po.com> Date: Mon, 20 Dec 2010 17:36:34 +0000 (+0100) Subject: One should only be able to assign to variables X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=ad8375ac4b91f89cece172a0402a72f0a6a907c4;p=web%2Fkonrad%2Ftwig.git One should only be able to assign to variables --- diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 21d1e42..fdcbbe0 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -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);