(tokens, name, context=())
| 765 | |
| 766 | |
| 767 | def parse_expr(tokens, name, context=()): |
| 768 | if isinstance(tokens[0], basestring_): |
| 769 | return tokens[0], tokens[1:] |
| 770 | expr, pos = tokens[0] |
| 771 | expr = expr.strip() |
| 772 | if expr.startswith("py:"): |
| 773 | expr = expr[3:].lstrip(" \t") |
| 774 | if expr.startswith(("\n", "\r")): |
| 775 | expr = expr.lstrip("\r\n") |
| 776 | if "\r" in expr: |
| 777 | expr = expr.replace("\r\n", "\n") |
| 778 | expr = expr.replace("\r", "") |
| 779 | expr += "\n" |
| 780 | elif "\n" in expr: |
| 781 | raise TemplateError( |
| 782 | "Multi-line py blocks must start with a newline", |
| 783 | position=pos, |
| 784 | name=name, |
| 785 | ) |
| 786 | return ("py", pos, expr), tokens[1:] |
| 787 | elif expr in ("continue", "break"): |
| 788 | if "for" not in context: |
| 789 | raise TemplateError("continue outside of for loop", position=pos, name=name) |
| 790 | return (expr, pos), tokens[1:] |
| 791 | elif expr.startswith("if "): |
| 792 | return parse_cond(tokens, name, context) |
| 793 | elif expr.startswith("elif ") or expr == "else": |
| 794 | raise TemplateError( |
| 795 | f"{expr.split()[0]} outside of an if block", position=pos, name=name |
| 796 | ) |
| 797 | elif expr in ("if", "elif", "for"): |
| 798 | raise TemplateError(f"{expr} with no expression", position=pos, name=name) |
| 799 | elif expr in ("endif", "endfor", "enddef"): |
| 800 | raise TemplateError(f"Unexpected {expr}", position=pos, name=name) |
| 801 | elif expr.startswith("for "): |
| 802 | return parse_for(tokens, name, context) |
| 803 | elif expr.startswith("default "): |
| 804 | return parse_default(tokens, name, context) |
| 805 | elif expr.startswith("inherit "): |
| 806 | return parse_inherit(tokens, name, context) |
| 807 | elif expr.startswith("def "): |
| 808 | return parse_def(tokens, name, context) |
| 809 | elif expr.startswith("#"): |
| 810 | return ("comment", pos, tokens[0][0]), tokens[1:] |
| 811 | return ("expr", pos, tokens[0][0]), tokens[1:] |
| 812 | |
| 813 | |
| 814 | def parse_cond(tokens, name, context): |
no test coverage detected
searching dependent graphs…