From 05a12f08207368a437a55295d48cd7689aa42375 Mon Sep 17 00:00:00 2001 From: Martin Hason Date: Fri, 7 Jan 2011 17:12:54 +0100 Subject: [PATCH] fix line number in error exceptions --- lib/Twig/ExpressionParser.php | 7 ++++--- lib/Twig/Token.php | 10 +++++----- lib/Twig/TokenParser/AutoEscape.php | 2 +- lib/Twig/TokenStream.php | 7 ++++--- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 204f422..aa8be80 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -145,7 +145,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::typeToEnglish($token->getType()), $token->getValue()), $token->getLine()); + throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine()); } } @@ -190,7 +190,8 @@ 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::typeToEnglish($stream->getCurrent()->getType()), $stream->getCurrent()->getValue()), $stream->getCurrent()->getLine()); + $current = $stream->getCurrent(); + 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($current->getType(), $current->getLine()), $current->getValue()), $current->getLine()); } $key = $stream->next()->getValue(); @@ -344,7 +345,7 @@ class Twig_ExpressionParser while (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(sprintf('You cannot assign a value to "%s"', $token->getValue())); + throw new Twig_Error_Syntax(sprintf('You cannot assign a value to "%s"', $token->getValue()), $token->getLine()); } $targets[] = new Twig_Node_Expression_AssignName($token->getValue(), $token->getLine()); diff --git a/lib/Twig/Token.php b/lib/Twig/Token.php index 93156bb..f74453e 100644 --- a/lib/Twig/Token.php +++ b/lib/Twig/Token.php @@ -55,7 +55,7 @@ class Twig_Token */ public function __toString() { - return sprintf('%s(%s)', self::typeToString($this->type, true), $this->value); + return sprintf('%s(%s)', self::typeToString($this->type, true, $this->lineno), $this->value); } /** @@ -125,7 +125,7 @@ class Twig_Token * * @return string The string representation */ - static public function typeToString($type, $short = false) + static public function typeToString($type, $short = false, $line = -1) { switch ($type) { case self::EOF_TYPE: @@ -162,7 +162,7 @@ class Twig_Token $name = 'PUNCTUATION_TYPE'; break; default: - throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type)); + throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line); } return $short ? $name : 'Twig_Token::'.$name; @@ -176,7 +176,7 @@ class Twig_Token * * @return string The string representation */ - static public function typeToEnglish($type) + static public function typeToEnglish($type, $line = -1) { switch ($type) { case self::EOF_TYPE: @@ -202,7 +202,7 @@ class Twig_Token case self::PUNCTUATION_TYPE: return 'punctuation'; default: - throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type)); + throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line); } } } diff --git a/lib/Twig/TokenParser/AutoEscape.php b/lib/Twig/TokenParser/AutoEscape.php index 5146a95..324ee60 100644 --- a/lib/Twig/TokenParser/AutoEscape.php +++ b/lib/Twig/TokenParser/AutoEscape.php @@ -28,7 +28,7 @@ class Twig_TokenParser_AutoEscape extends Twig_TokenParser if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE)) { if (false === $value) { - throw new Twig_Error_Syntax(sprintf('Unexpected escaping strategy as you set autoescaping to false.', $lineno), -1); + throw new Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $lineno); } $value = $this->parser->getStream()->next()->getValue(); diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index 0a11e5b..68fe09a 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -68,11 +68,12 @@ class Twig_TokenStream { $token = $this->tokens[$this->current]; if (!$token->test($type, $value)) { + $line = $token->getLine(); throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message.'. ' : '', - Twig_Token::typeToEnglish($token->getType()), $token->getValue(), - Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''), - $token->getLine() + Twig_Token::typeToEnglish($token->getType(), $line), $token->getValue(), + Twig_Token::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''), + $line ); } $this->next(); -- 1.7.2.5