From 8e96487adace91ee362f86b7b94056d34ba25a4a Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 20 Dec 2010 14:48:07 +0100 Subject: [PATCH] changed error messages to be more friendly --- lib/Twig/ExpressionParser.php | 4 ++-- lib/Twig/Token.php | 34 ++++++++++++++++++++++++++++++++-- lib/Twig/TokenStream.php | 4 ++-- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index e0e323e..5f6b06c 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -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(); diff --git a/lib/Twig/Token.php b/lib/Twig/Token.php index 8bbe8c0..b0c553b 100644 --- a/lib/Twig/Token.php +++ b/lib/Twig/Token.php @@ -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)); + } + } } diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index 80b5e32..5abbf29 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -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() ); } -- 1.7.2.5