Skip to content

Commit

Permalink
Add continue and break statement support in while and for loop for te…
Browse files Browse the repository at this point in the history
…mplate
  • Loading branch information
felinx authored and bdarnell committed Jun 25, 2012
1 parent 31c3f14 commit 3e508b3
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions tornado/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 3e508b3

Please sign in to comment.