]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add continue and break statement support in while and for loop for template
authorFelinx Lee <felinx.lee@gmail.com>
Tue, 19 Jun 2012 03:25:51 +0000 (11:25 +0800)
committerBen Darnell <ben@bendarnell.com>
Mon, 25 Jun 2012 02:22:36 +0000 (19:22 -0700)
tornado/template.py

index 7072760be1630340b11440a81628d819a08e1ffb..64d8391eaabc6452173c42712f95e00dcb0fd5c6 100644 (file)
@@ -676,7 +676,7 @@ def _format_code(code):
     return "".join([format % (i + 1, line) for (i, line) in enumerate(lines)])
 
 
-def _parse(reader, template, in_block=None):
+def _parse(reader, template, in_block=None, in_loop=None):
     body = _ChunkList([])
     while True:
         # Find next template directive
@@ -815,7 +815,11 @@ def _parse(reader, template, in_block=None):
 
         elif operator in ("apply", "block", "try", "if", "for", "while"):
             # parse inner body recursively
-            block_body = _parse(reader, template, operator)
+            if operator in ("for", "while"):
+                block_body = _parse(reader, template, operator, operator)
+            else:
+                block_body = _parse(reader, template, operator, in_loop)
+
             if operator == "apply":
                 if not suffix:
                     raise ParseError("apply missing method name on line %d" % line)
@@ -829,5 +833,11 @@ def _parse(reader, template, in_block=None):
             body.chunks.append(block)
             continue
 
+        elif operator in ("break", "continue"):
+            if not in_loop:
+                raise ParseError("%s outside %s block" % (operator, set(["for", "while"])))
+            body.chunks.append(_Statement(contents, line))
+            continue
+
         else:
             raise ParseError("unknown operator: %r" % operator)