From ac86c6368c8b1ed27adb436af54d0398b64cab1f Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 17 Feb 2013 20:37:38 +0100 Subject: [PATCH] changed an exception to be consistent with other ones (and also to better help debugging problems) --- lib/Twig/Lexer.php | 5 ++++- test/Twig/Tests/LexerTest.php | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletions(-) diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 3be7215..ff5c6ce 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -32,6 +32,7 @@ class Twig_Lexer implements Twig_LexerInterface protected $regexes; protected $position; protected $positions; + protected $currentVarBlockLine; const STATE_DATA = 0; const STATE_BLOCK = 1; @@ -186,12 +187,14 @@ class Twig_Lexer implements Twig_LexerInterface } else { $this->pushToken(Twig_Token::BLOCK_START_TYPE); $this->pushState(self::STATE_BLOCK); + $this->currentVarBlockLine = $this->lineno; } break; case $this->options['tag_variable'][0]: $this->pushToken(Twig_Token::VAR_START_TYPE); $this->pushState(self::STATE_VAR); + $this->currentVarBlockLine = $this->lineno; break; } } @@ -225,7 +228,7 @@ class Twig_Lexer implements Twig_LexerInterface $this->moveCursor($match[0]); if ($this->cursor >= $this->end) { - throw new Twig_Error_Syntax(sprintf('Unexpected end of file: Unclosed "%s"', $this->state === self::STATE_BLOCK ? 'block' : 'variable'), $this->lineno, $this->filename); + throw new Twig_Error_Syntax(sprintf('Unclosed "%s"', $this->state === self::STATE_BLOCK ? 'block' : 'variable'), $this->currentVarBlockLine, $this->filename); } } diff --git a/test/Twig/Tests/LexerTest.php b/test/Twig/Tests/LexerTest.php index cb1cc3c..34ed74f 100644 --- a/test/Twig/Tests/LexerTest.php +++ b/test/Twig/Tests/LexerTest.php @@ -260,4 +260,42 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase $stream->expect(Twig_Token::NUMBER_TYPE, 1); $stream->expect(Twig_Token::OPERATOR_TYPE, 'and'); } + + /** + * @expectedException Twig_Error_Syntax + * @expectedExceptionMessage Unclosed "variable" at line 3 + */ + public function testUnterminatedVariable() + { + $template = ' + +{{ + +bar + + +'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $stream = $lexer->tokenize($template); + } + + /** + * @expectedException Twig_Error_Syntax + * @expectedExceptionMessage Unclosed "block" at line 3 + */ + public function testUnterminatedBlock() + { + $template = ' + +{% + +bar + + +'; + + $lexer = new Twig_Lexer(new Twig_Environment()); + $stream = $lexer->tokenize($template); + } } -- 1.7.2.5