enhanced error messages when an unexpected token is parsed in an expression
authorFabien Potencier <fabien.potencier@gmail.com>
Tue, 14 Dec 2010 08:47:56 +0000 (09:47 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Tue, 14 Dec 2010 08:47:56 +0000 (09:47 +0100)
CHANGELOG
lib/Twig/ExpressionParser.php
lib/Twig/TokenStream.php

index e4b4d2d..f8ed2c8 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -15,6 +15,7 @@ Backward incompatibilities:
 
 Changes:
 
+ * enhanced error messages when an unexpected token is parsed in an expression
  * fixed filename not being added to syntax error messages
  * added the autoescape option to enable/disable autoescaping
  * removed the newline after a comment (mimicks PHP behavior)
index 51a87c9..966674c 100644 (file)
@@ -77,7 +77,7 @@ class Twig_ExpressionParser
         } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
             $this->parser->getStream()->next();
             $expr = $this->parseExpression();
-            $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ')');
+            $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
 
             return $this->parsePostfixExpression($expr);
         }
@@ -90,7 +90,7 @@ class Twig_ExpressionParser
         while ($this->parser->getStream()->test(Twig_Token::PUNCTUATION_TYPE, '?')) {
             $this->parser->getStream()->next();
             $expr2 = $this->parseExpression();
-            $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ':');
+            $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'The ternary operator must have a default value');
             $expr3 = $this->parseExpression();
 
             $expr = new Twig_Node_Expression_Conditional($expr, $expr2, $expr3, $this->parser->getCurrentToken()->getLine());
@@ -160,11 +160,11 @@ class Twig_ExpressionParser
     public function parseArrayExpression()
     {
         $stream = $this->parser->getStream();
-        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[');
+        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '[', 'An array element was expected');
         $elements = array();
         while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
             if (!empty($elements)) {
-                $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',');
+                $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'An array element must be followed by a comma (,)');
 
                 // trailing ,?
                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, ']')) {
@@ -174,7 +174,7 @@ class Twig_ExpressionParser
 
             $elements[] = $this->parseExpression();
         }
-        $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']');
+        $stream->expect(Twig_Token::PUNCTUATION_TYPE, ']', 'An opened array is not properly closed');
 
         return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
     }
@@ -182,11 +182,11 @@ class Twig_ExpressionParser
     public function parseHashExpression()
     {
         $stream = $this->parser->getStream();
-        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{');
+        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
         $elements = array();
         while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
             if (!empty($elements)) {
-                $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',');
+                $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma (,)');
 
                 // trailing ,?
                 if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
@@ -199,10 +199,10 @@ class Twig_ExpressionParser
             }
 
             $key = $stream->next()->getValue();
-            $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':');
+            $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
             $elements[$key] = $this->parseExpression();
         }
-        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}');
+        $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
 
         return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
     }
@@ -298,11 +298,11 @@ class Twig_ExpressionParser
         $args = array();
         while (!$parser->test(Twig_Token::PUNCTUATION_TYPE, ')')) {
             if (!empty($args)) {
-                $parser->expect(Twig_Token::PUNCTUATION_TYPE, ',');
+                $parser->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma (,)');
             }
             $args[] = $this->parseExpression();
         }
-        $parser->expect(Twig_Token::PUNCTUATION_TYPE, ')');
+        $parser->expect(Twig_Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
 
         return new Twig_Node($args);
     }
@@ -313,7 +313,7 @@ class Twig_ExpressionParser
         $targets = array();
         while (true) {
             if (!empty($targets)) {
-                $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ',');
+                $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) ||
@@ -337,7 +337,7 @@ class Twig_ExpressionParser
         $is_multitarget = false;
         while (true) {
             if (!empty($targets)) {
-                $this->parser->getStream()->expect(Twig_Token::PUNCTUATION_TYPE, ',');
+                $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) ||
index 65da651..80b5e32 100644 (file)
@@ -117,11 +117,12 @@ class Twig_TokenStream
     /**
      * Expects a token (like $token->test()) and returns it or throw a syntax error.
      */
-    public function expect($primary, $secondary = null)
+    public function expect($primary, $secondary = null, $message = null)
     {
         $token = $this->current;
         if (!$token->test($primary, $secondary)) {
-            throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s" ("%s" expected%s)',
+            throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)',
+                $message ? $message.'. ' : '',
                 Twig_Token::getTypeAsString($token->getType()), $token->getValue(),
                 Twig_Token::getTypeAsString($primary), $secondary ? sprintf(' with value "%s"', $secondary) : ''),
                 $this->current->getLine()