From d2b44781e38b2e5be588608d141edae5ac3279c6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 19 Feb 2010 18:19:11 +0100 Subject: [PATCH] added an exception when the parser want to move after the EOF, and added a better error when a if tag is not closed properly (closes #12) --- lib/Twig/TokenParser/If.php | 38 ++++++++++++++++++++++++-------------- lib/Twig/TokenStream.php | 5 +++++ 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lib/Twig/TokenParser/If.php b/lib/Twig/TokenParser/If.php index adf8f7b..9bd4400 100644 --- a/lib/Twig/TokenParser/If.php +++ b/lib/Twig/TokenParser/If.php @@ -23,23 +23,33 @@ class Twig_TokenParser_If extends Twig_TokenParser $end = false; while (!$end) { - switch ($this->parser->getStream()->next()->getValue()) + try { - case 'else': - $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - $else = $this->parser->subparse(array($this, 'decideIfEnd')); - break; + switch ($this->parser->getStream()->next()->getValue()) + { + case 'else': + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); + $else = $this->parser->subparse(array($this, 'decideIfEnd')); + break; - case 'elseif': - $expr = $this->parser->getExpressionParser()->parseExpression(); - $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); - $body = $this->parser->subparse(array($this, 'decideIfFork')); - $tests[] = array($expr, $body); - break; + case 'elseif': + $expr = $this->parser->getExpressionParser()->parseExpression(); + $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE); + $body = $this->parser->subparse(array($this, 'decideIfFork')); + $tests[] = array($expr, $body); + break; - case 'endif': - $end = true; - break; + case 'endif': + $end = true; + break; + + default: + throw new Twig_SyntaxError('', -1); + } + } + catch (Twig_SyntaxError $e) + { + throw new Twig_SyntaxError(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d)', $lineno), -1); } } diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index a696d3c..74e1537 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -63,6 +63,11 @@ class Twig_TokenStream $token = array_shift($this->tokens); } + if (null === $token) + { + throw new Twig_SyntaxError('Unexpected end of template', -1); + } + // trim blocks if ($this->trimBlocks && $this->current && -- 1.7.2.5