From 6080a5238f776812a60a5b0cb3bf9493e7115968 Mon Sep 17 00:00:00 2001
From: Mike Bayer <mike_mp@zzzcomputing.com>
Date: Tue, 20 Feb 2007 01:15:54 +0000
Subject: [PATCH] - added lexer error for unclosed control-line (%) line

---
 CHANGES           |  1 +
 lib/mako/lexer.py |  2 ++
 test/lexer.py     | 14 +++++++++++++-
 3 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/CHANGES b/CHANGES
index 38e9246..711b2a9 100644
--- a/CHANGES
+++ b/CHANGES
@@ -8,6 +8,7 @@ either one "#" sign or two for now; two is preferred going forward, i.e.
 - new multiline comment form: "<%doc> a comment </%doc>"
 - UNDEFINED evaluates to False
 - improvement to scoping of "caller" variable when using <%call> tag
+- added lexer error for unclosed control-line (%) line
 
 0.1.2
 - fix to parsing of code/expression blocks to insure that non-ascii
diff --git a/lib/mako/lexer.py b/lib/mako/lexer.py
index 0d4838d..0d9081d 100644
--- a/lib/mako/lexer.py
+++ b/lib/mako/lexer.py
@@ -139,6 +139,8 @@ class Lexer(object):
             
         if len(self.tag):
             raise exceptions.SyntaxException("Unclosed tag: <%%%s>" % self.tag[-1].keyword, self.matched_lineno, self.matched_charpos, self.filename)
+        if len(self.control_line):
+            raise exceptions.SyntaxException("Unterminated control keyword: '%s'" % self.control_line[-1].keyword, self.control_line[-1].lineno, self.control_line[-1].pos, self.filename)
         return self.template
 
     def match_encoding(self):
diff --git a/test/lexer.py b/test/lexer.py
index 94e41f0..8c76202 100644
--- a/test/lexer.py
+++ b/test/lexer.py
@@ -284,6 +284,19 @@ text text la la
     def test_unmatched_control_2(self):
         template = """
 
+        % if foo:
+            % for x in range(1,5):
+            % endfor
+"""
+        try:
+            nodes = Lexer(template).parse()
+            assert False
+        except exceptions.SyntaxException, e:
+            assert str(e) == "Unterminated control keyword: 'if' at line: 3 char: 1"
+
+    def test_unmatched_control_3(self):
+        template = """
+
         % if foo:
             % for x in range(1,5):
             % endlala
@@ -333,7 +346,6 @@ text text la la
 </table>
 """
         nodes = Lexer(template).parse()
-        print repr(nodes)
         assert repr(nodes) == r"""TemplateNode({}, [NamespaceTag(u'namespace', {u'name': u'foo', u'file': u'somefile.html'}, (1, 1), []), Text(u'\n', (1, 46)), Comment(u'inherit from foobar.html', (2, 1)), InheritTag(u'inherit', {u'file': u'foobar.html'}, (3, 1), []), Text(u'\n\n', (3, 31)), DefTag(u'def', {u'name': u'header()'}, (5, 1), ["Text(u'\\n     <div>header</div>\\n', (5, 23))"]), Text(u'\n', (7, 8)), DefTag(u'def', {u'name': u'footer()'}, (8, 1), ["Text(u'\\n    <div> footer</div>\\n', (8, 23))"]), Text(u'\n\n<table>\n', (10, 8)), ControlLine(u'for', u'for j in data():', False, (13, 1)), Text(u'    <tr>\n', (14, 1)), ControlLine(u'for', u'for x in j:', False, (15, 1)), Text(u'            <td>Hello ', (16, 1)), Expression(u'x', ['h'], (16, 23)), Text(u'</td>\n', (16, 30)), ControlLine(u'for', u'endfor', True, (17, 1)), Text(u'    </tr>\n', (18, 1)), ControlLine(u'for', u'endfor', True, (19, 1)), Text(u'</table>\n', (20, 1))])"""
 
     def test_crlf(self):
-- 
GitLab