changed error messages to be more friendly
authorFabien Potencier <fabien.potencier@gmail.com>
Mon, 20 Dec 2010 13:48:07 +0000 (14:48 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Mon, 20 Dec 2010 13:48:07 +0000 (14:48 +0100)
lib/Twig/ExpressionParser.php
lib/Twig/Token.php
lib/Twig/TokenStream.php

index e0e323e..5f6b06c 100644 (file)
@@ -146,7 +146,7 @@ class Twig_ExpressionParser
                 } elseif ($token->test(Twig_Token::PUNCTUATION_TYPE, '{')) {
                     $node = $this->parseHashExpression();
                 } else {
-                    throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($token->getType()), $token->getValue()), $token->getLine());
+                    throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine());
                 }
         }
 
@@ -195,7 +195,7 @@ class Twig_ExpressionParser
             }
 
             if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) {
-                throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::getTypeAsString($stream->getCurrent()->getType()), $stream->getCurrent()->getValue()), $stream->getCurrent()->getLine());
+                throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($stream->getCurrent()->getType()), $stream->getCurrent()->getValue()), $stream->getCurrent()->getLine());
             }
 
             $key = $stream->next()->getValue();
index 8bbe8c0..b0c553b 100644 (file)
@@ -36,7 +36,7 @@ class Twig_Token
 
     public function __toString()
     {
-        return sprintf('%s(%s)', self::getTypeAsString($this->type, true), $this->value);
+        return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value);
     }
 
     /**
@@ -80,7 +80,7 @@ class Twig_Token
         $this->value = $value;
     }
 
-    static public function getTypeAsString($type, $short = false)
+    static public function typeToString($type, $short = false)
     {
         switch ($type) {
             case self::EOF_TYPE:
@@ -122,4 +122,34 @@ class Twig_Token
 
         return $short ? $name : 'Twig_Token::'.$name;
     }
+
+    static public function typeToEnglish($type)
+    {
+        switch ($type) {
+            case self::EOF_TYPE:
+                return 'end of template';
+            case self::TEXT_TYPE:
+                return 'text';
+            case self::BLOCK_START_TYPE:
+                return 'begin of statement block';
+            case self::VAR_START_TYPE:
+                return 'begin of print statement';
+            case self::BLOCK_END_TYPE:
+                return 'end of statement block';
+            case self::VAR_END_TYPE:
+                return 'end of print statement';
+            case self::NAME_TYPE:
+                return 'name';
+            case self::NUMBER_TYPE:
+                return 'number';
+            case self::STRING_TYPE:
+                return 'string';
+            case self::OPERATOR_TYPE:
+                return 'operator';
+            case self::PUNCTUATION_TYPE:
+                return 'punctuation';
+            default:
+                throw new Twig_Error_Syntax(sprintf('Token of type %s does not exist.', $type));
+        }
+    }
 }
index 80b5e32..5abbf29 100644 (file)
@@ -123,8 +123,8 @@ class Twig_TokenStream
         if (!$token->test($primary, $secondary)) {
             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) : ''),
+                Twig_Token::typeToEnglish($token->getType()), $token->getValue(),
+                Twig_Token::typeToEnglish($primary), $secondary ? sprintf(' with value "%s"', $secondary) : ''),
                 $this->current->getLine()
             );
         }