From: Felinx Lee Date: Tue, 19 Jun 2012 03:25:51 +0000 (+0800) Subject: Add continue and break statement support in while and for loop for template X-Git-Tag: v2.4.0~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3e508b3b6a89a262518a677afa987756b6985d4d;p=thirdparty%2Ftornado.git Add continue and break statement support in while and for loop for template --- diff --git a/tornado/template.py b/tornado/template.py index 7072760be..64d8391ea 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -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)