changed an exception to be consistent with other ones (and also to better help debugg...
authorFabien Potencier <fabien.potencier@gmail.com>
Sun, 17 Feb 2013 19:37:38 +0000 (20:37 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Sun, 17 Feb 2013 19:51:08 +0000 (20:51 +0100)
lib/Twig/Lexer.php
test/Twig/Tests/LexerTest.php

index 3be7215..ff5c6ce 100644 (file)
@@ -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);
             }
         }
 
index cb1cc3c..34ed74f 100644 (file)
@@ -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);
+    }
 }