Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | jinja2.compiler |
| 4 | ~~~~~~~~~~~~~~~ |
| 5 | |
| 6 | Compiles nodes into python code. |
| 7 | |
| 8 | :copyright: Copyright 2008 by Armin Ronacher. |
| 9 | :license: GNU GPL. |
| 10 | """ |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 11 | from copy import copy |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 12 | from random import randrange |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 13 | from cStringIO import StringIO |
| 14 | from jinja2 import nodes |
| 15 | from jinja2.visitor import NodeVisitor, NodeTransformer |
| 16 | from jinja2.exceptions import TemplateAssertionError |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 17 | from jinja2.runtime import StaticLoopContext |
Armin Ronacher | 5236d8c | 2008-04-17 11:23:16 +0200 | [diff] [blame] | 18 | from jinja2.utils import Markup |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 19 | |
| 20 | |
| 21 | operators = { |
| 22 | 'eq': '==', |
| 23 | 'ne': '!=', |
| 24 | 'gt': '>', |
| 25 | 'gteq': '>=', |
| 26 | 'lt': '<', |
| 27 | 'lteq': '<=', |
| 28 | 'in': 'in', |
| 29 | 'notin': 'not in' |
| 30 | } |
| 31 | |
| 32 | |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 33 | try: |
| 34 | exec '(0 if 0 else 0)' |
| 35 | except SyntaxError: |
| 36 | have_condexpr = False |
| 37 | else: |
| 38 | have_condexpr = True |
| 39 | |
| 40 | |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 41 | def generate(node, environment, name, filename, stream=None): |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 42 | """Generate the python source for a node tree.""" |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 43 | generator = CodeGenerator(environment, name, filename, stream) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 44 | generator.visit(node) |
| 45 | if stream is None: |
| 46 | return generator.stream.getvalue() |
| 47 | |
| 48 | |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 49 | def has_safe_repr(value): |
| 50 | """Does the node have a safe representation?""" |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 51 | if value is None or value is NotImplemented or value is Ellipsis: |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 52 | return True |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 53 | if isinstance(value, (bool, int, long, float, complex, basestring, |
Armin Ronacher | 5236d8c | 2008-04-17 11:23:16 +0200 | [diff] [blame] | 54 | xrange, StaticLoopContext, Markup)): |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 55 | return True |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 56 | if isinstance(value, (tuple, list, set, frozenset)): |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 57 | for item in value: |
| 58 | if not has_safe_repr(item): |
| 59 | return False |
| 60 | return True |
| 61 | elif isinstance(value, dict): |
| 62 | for key, value in value.iteritems(): |
| 63 | if not has_safe_repr(key): |
| 64 | return False |
| 65 | if not has_safe_repr(value): |
| 66 | return False |
| 67 | return True |
| 68 | return False |
| 69 | |
| 70 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 71 | class Identifiers(object): |
| 72 | """Tracks the status of identifiers in frames.""" |
| 73 | |
| 74 | def __init__(self): |
| 75 | # variables that are known to be declared (probably from outer |
| 76 | # frames or because they are special for the frame) |
| 77 | self.declared = set() |
| 78 | |
| 79 | # names that are accessed without being explicitly declared by |
| 80 | # this one or any of the outer scopes. Names can appear both in |
| 81 | # declared and undeclared. |
| 82 | self.undeclared = set() |
| 83 | |
| 84 | # names that are declared locally |
| 85 | self.declared_locally = set() |
| 86 | |
| 87 | # names that are declared by parameters |
| 88 | self.declared_parameter = set() |
| 89 | |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 90 | # filters/tests that are referenced |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 91 | self.filters = set() |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 92 | self.tests = set() |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 93 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 94 | def add_special(self, name): |
| 95 | """Register a special name like `loop`.""" |
| 96 | self.undeclared.discard(name) |
| 97 | self.declared.add(name) |
| 98 | |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 99 | def is_declared(self, name, local_only=False): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 100 | """Check if a name is declared in this or an outer scope.""" |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 101 | if name in self.declared_locally or name in self.declared_parameter: |
| 102 | return True |
| 103 | if local_only: |
| 104 | return False |
| 105 | return name in self.declared |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 106 | |
| 107 | def find_shadowed(self): |
| 108 | """Find all the shadowed names.""" |
| 109 | return self.declared & (self.declared_locally | self.declared_parameter) |
| 110 | |
| 111 | |
| 112 | class Frame(object): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 113 | """Holds compile time information for us.""" |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 114 | |
| 115 | def __init__(self, parent=None): |
| 116 | self.identifiers = Identifiers() |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 117 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 118 | # a toplevel frame is the root + soft frames such as if conditions. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 119 | self.toplevel = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 120 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 121 | # the root frame is basically just the outermost frame, so no if |
| 122 | # conditions. This information is used to optimize inheritance |
| 123 | # situations. |
| 124 | self.rootlevel = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 125 | |
| 126 | # inside some tags we are using a buffer rather than yield statements. |
| 127 | # this for example affects {% filter %} or {% macro %}. If a frame |
| 128 | # is buffered this variable points to the name of the list used as |
| 129 | # buffer. |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 130 | self.buffer = None |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 131 | |
| 132 | # if a frame has name_overrides, all read access to a name in this |
| 133 | # dict is redirected to a string expression. |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 134 | self.name_overrides = {} |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 135 | |
| 136 | # the name of the block we're in, otherwise None. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 137 | self.block = parent and parent.block or None |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 138 | |
| 139 | # the parent of this frame |
| 140 | self.parent = parent |
| 141 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 142 | if parent is not None: |
| 143 | self.identifiers.declared.update( |
| 144 | parent.identifiers.declared | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 145 | parent.identifiers.declared_locally | |
| 146 | parent.identifiers.declared_parameter |
| 147 | ) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 148 | self.buffer = parent.buffer |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 149 | self.name_overrides = parent.name_overrides.copy() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 150 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 151 | def copy(self): |
| 152 | """Create a copy of the current one.""" |
| 153 | rv = copy(self) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 154 | rv.identifiers = copy(self.identifiers) |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 155 | rv.name_overrides = self.name_overrides.copy() |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 156 | return rv |
| 157 | |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 158 | def inspect(self, nodes, hard_scope=False): |
| 159 | """Walk the node and check for identifiers. If the scope |
| 160 | is hard (eg: enforce on a python level) overrides from outer |
| 161 | scopes are tracked differently. |
| 162 | """ |
| 163 | visitor = FrameIdentifierVisitor(self.identifiers, hard_scope) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 164 | for node in nodes: |
| 165 | visitor.visit(node) |
| 166 | |
| 167 | def inner(self): |
| 168 | """Return an inner frame.""" |
| 169 | return Frame(self) |
| 170 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 171 | def soft(self): |
| 172 | """Return a soft frame. A soft frame may not be modified as |
| 173 | standalone thing as it shares the resources with the frame it |
| 174 | was created of, but it's not a rootlevel frame any longer. |
| 175 | """ |
| 176 | rv = copy(self) |
| 177 | rv.rootlevel = False |
| 178 | return rv |
| 179 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 180 | |
| 181 | class FrameIdentifierVisitor(NodeVisitor): |
| 182 | """A visitor for `Frame.inspect`.""" |
| 183 | |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 184 | def __init__(self, identifiers, hard_scope): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 185 | self.identifiers = identifiers |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 186 | self.hard_scope = hard_scope |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 187 | |
| 188 | def visit_Name(self, node): |
| 189 | """All assignments to names go through this function.""" |
| 190 | if node.ctx in ('store', 'param'): |
| 191 | self.identifiers.declared_locally.add(node.name) |
| 192 | elif node.ctx == 'load': |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 193 | if not self.identifiers.is_declared(node.name, self.hard_scope): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 194 | self.identifiers.undeclared.add(node.name) |
| 195 | |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 196 | def visit_Filter(self, node): |
Armin Ronacher | 449167d | 2008-04-11 17:55:05 +0200 | [diff] [blame] | 197 | self.generic_visit(node) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 198 | self.identifiers.filters.add(node.name) |
| 199 | |
| 200 | def visit_Test(self, node): |
| 201 | self.generic_visit(node) |
| 202 | self.identifiers.tests.add(node.name) |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 203 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 204 | def visit_Macro(self, node): |
| 205 | """Macros set local.""" |
| 206 | self.identifiers.declared_locally.add(node.name) |
| 207 | |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 208 | def visit_Include(self, node): |
| 209 | """Some includes set local.""" |
| 210 | self.generic_visit(node) |
| 211 | if node.target is not None: |
| 212 | self.identifiers.declared_locally.add(node.target) |
| 213 | |
Armin Ronacher | ebe55aa | 2008-04-10 20:51:23 +0200 | [diff] [blame] | 214 | def visit_Assign(self, node): |
| 215 | """Visit assignments in the correct order.""" |
| 216 | self.visit(node.node) |
| 217 | self.visit(node.target) |
| 218 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 219 | # stop traversing at instructions that have their own scope. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 220 | visit_Block = visit_CallBlock = visit_FilterBlock = \ |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 221 | visit_For = lambda s, n: None |
| 222 | |
| 223 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 224 | class CompilerExit(Exception): |
| 225 | """Raised if the compiler encountered a situation where it just |
| 226 | doesn't make sense to further process the code. Any block that |
| 227 | raises such an exception is not further processed.""" |
| 228 | |
| 229 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 230 | class CodeGenerator(NodeVisitor): |
| 231 | |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 232 | def __init__(self, environment, name, filename, stream=None): |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 233 | if stream is None: |
| 234 | stream = StringIO() |
Christoph Hack | 65642a5 | 2008-04-08 14:46:56 +0200 | [diff] [blame] | 235 | self.environment = environment |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 236 | self.name = name |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 237 | self.filename = filename |
| 238 | self.stream = stream |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 239 | |
| 240 | # a registry for all blocks. Because blocks are moved out |
| 241 | # into the global python scope they are registered here |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 242 | self.blocks = {} |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 243 | |
| 244 | # the number of extends statements so far |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 245 | self.extends_so_far = 0 |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 246 | |
| 247 | # some templates have a rootlevel extends. In this case we |
| 248 | # can safely assume that we're a child template and do some |
| 249 | # more optimizations. |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 250 | self.has_known_extends = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 251 | |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 252 | # the current line number |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 253 | self.code_lineno = 1 |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 254 | |
| 255 | # the debug information |
| 256 | self.debug_info = [] |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 257 | self._write_debug_info = None |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 258 | |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 259 | # the number of new lines before the next write() |
| 260 | self._new_lines = 0 |
| 261 | |
| 262 | # the line number of the last written statement |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 263 | self._last_line = 0 |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 264 | |
| 265 | # true if nothing was written so far. |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 266 | self._first_write = True |
| 267 | |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 268 | # used by the `temporary_identifier` method to get new |
| 269 | # unique, temporary identifier |
| 270 | self._last_identifier = 0 |
| 271 | |
| 272 | # the current indentation |
| 273 | self._indentation = 0 |
| 274 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 275 | def temporary_identifier(self): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 276 | """Get a new unique identifier.""" |
| 277 | self._last_identifier += 1 |
| 278 | return 't%d' % self._last_identifier |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 279 | |
| 280 | def indent(self): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 281 | """Indent by one.""" |
| 282 | self._indentation += 1 |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 283 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 284 | def outdent(self, step=1): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 285 | """Outdent by step.""" |
| 286 | self._indentation -= step |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 287 | |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 288 | def blockvisit(self, nodes, frame, indent=True, force_generator=True): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 289 | """Visit a list of nodes as block in a frame. Per default the |
| 290 | code is indented, but this can be disabled by setting the indent |
| 291 | parameter to False. If the current frame is no buffer a dummy |
| 292 | ``if 0: yield None`` is written automatically unless the |
| 293 | force_generator parameter is set to False. |
| 294 | """ |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 295 | if indent: |
| 296 | self.indent() |
| 297 | if frame.buffer is None and force_generator: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 298 | self.writeline('if 0: yield None') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 299 | try: |
| 300 | for node in nodes: |
| 301 | self.visit(node, frame) |
| 302 | except CompilerExit: |
| 303 | pass |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 304 | if indent: |
| 305 | self.outdent() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 306 | |
| 307 | def write(self, x): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 308 | """Write a string into the output stream.""" |
| 309 | if self._new_lines: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 310 | if not self._first_write: |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 311 | self.stream.write('\n' * self._new_lines) |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 312 | self.code_lineno += self._new_lines |
| 313 | if self._write_debug_info is not None: |
| 314 | self.debug_info.append((self._write_debug_info, |
| 315 | self.code_lineno)) |
| 316 | self._write_debug_info = None |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 317 | self._first_write = False |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 318 | self.stream.write(' ' * self._indentation) |
| 319 | self._new_lines = 0 |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 320 | self.stream.write(x) |
| 321 | |
| 322 | def writeline(self, x, node=None, extra=0): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 323 | """Combination of newline and write.""" |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 324 | self.newline(node, extra) |
| 325 | self.write(x) |
| 326 | |
| 327 | def newline(self, node=None, extra=0): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 328 | """Add one or more newlines before the next write.""" |
| 329 | self._new_lines = max(self._new_lines, 1 + extra) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 330 | if node is not None and node.lineno != self._last_line: |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 331 | self._write_debug_info = node.lineno |
| 332 | self._last_line = node.lineno |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 333 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 334 | def signature(self, node, frame, have_comma=True, extra_kwargs=None): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 335 | """Writes a function call to the stream for the current node. |
| 336 | Per default it will write a leading comma but this can be |
| 337 | disabled by setting have_comma to False. If extra_kwargs is |
| 338 | given it must be a string that represents a single keyword |
| 339 | argument call that is inserted at the end of the regular |
| 340 | keyword argument calls. |
| 341 | """ |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 342 | have_comma = have_comma and [True] or [] |
| 343 | def touch_comma(): |
| 344 | if have_comma: |
| 345 | self.write(', ') |
| 346 | else: |
| 347 | have_comma.append(True) |
| 348 | |
| 349 | for arg in node.args: |
| 350 | touch_comma() |
| 351 | self.visit(arg, frame) |
| 352 | for kwarg in node.kwargs: |
| 353 | touch_comma() |
| 354 | self.visit(kwarg, frame) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 355 | if extra_kwargs is not None: |
| 356 | touch_comma() |
| 357 | self.write(extra_kwargs) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 358 | if node.dyn_args: |
| 359 | touch_comma() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 360 | self.write('*') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 361 | self.visit(node.dyn_args, frame) |
| 362 | if node.dyn_kwargs: |
| 363 | touch_comma() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 364 | self.write('**') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 365 | self.visit(node.dyn_kwargs, frame) |
| 366 | |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 367 | def pull_locals(self, frame, indent=True): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 368 | """Pull all the references identifiers into the local scope. |
| 369 | This affects regular names, filters and tests. If indent is |
| 370 | set to False, no automatic indentation will take place. |
| 371 | """ |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 372 | if indent: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 373 | self.indent() |
| 374 | for name in frame.identifiers.undeclared: |
| 375 | self.writeline('l_%s = context[%r]' % (name, name)) |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 376 | for name in frame.identifiers.filters: |
| 377 | self.writeline('f_%s = environment.filters[%r]' % (name, name)) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 378 | for name in frame.identifiers.tests: |
| 379 | self.writeline('t_%s = environment.tests[%r]' % (name, name)) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 380 | if indent: |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 381 | self.outdent() |
| 382 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 383 | def collect_shadowed(self, frame): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 384 | """This function returns all the shadowed variables in a dict |
| 385 | in the form name: alias and will write the required assignments |
| 386 | into the current scope. No indentation takes place. |
| 387 | """ |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 388 | # make sure we "backup" overridden, local identifiers |
| 389 | # TODO: we should probably optimize this and check if the |
| 390 | # identifier is in use afterwards. |
| 391 | aliases = {} |
| 392 | for name in frame.identifiers.find_shadowed(): |
| 393 | aliases[name] = ident = self.temporary_identifier() |
| 394 | self.writeline('%s = l_%s' % (ident, name)) |
| 395 | return aliases |
| 396 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 397 | def function_scoping(self, node, frame): |
Armin Ronacher | fed44b5 | 2008-04-13 19:42:53 +0200 | [diff] [blame] | 398 | """In Jinja a few statements require the help of anonymous |
| 399 | functions. Those are currently macros and call blocks and in |
| 400 | the future also recursive loops. As there is currently |
| 401 | technical limitation that doesn't allow reading and writing a |
| 402 | variable in a scope where the initial value is coming from an |
| 403 | outer scope, this function tries to fall back with a common |
| 404 | error message. Additionally the frame passed is modified so |
| 405 | that the argumetns are collected and callers are looked up. |
| 406 | |
| 407 | This will return the modified frame. |
| 408 | """ |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 409 | func_frame = frame.inner() |
| 410 | func_frame.inspect(node.iter_child_nodes(), hard_scope=True) |
| 411 | |
| 412 | # variables that are undeclared (accessed before declaration) and |
| 413 | # declared locally *and* part of an outside scope raise a template |
| 414 | # assertion error. Reason: we can't generate reasonable code from |
| 415 | # it without aliasing all the variables. XXX: alias them ^^ |
| 416 | overriden_closure_vars = ( |
| 417 | func_frame.identifiers.undeclared & |
| 418 | func_frame.identifiers.declared & |
| 419 | (func_frame.identifiers.declared_locally | |
| 420 | func_frame.identifiers.declared_parameter) |
| 421 | ) |
| 422 | if overriden_closure_vars: |
| 423 | vars = ', '.join(sorted(overriden_closure_vars)) |
| 424 | raise TemplateAssertionError('It\'s not possible to set and ' |
| 425 | 'access variables derived from ' |
| 426 | 'an outer scope! (affects: %s' % |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 427 | vars, node.lineno, self.name) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 428 | |
| 429 | # remove variables from a closure from the frame's undeclared |
| 430 | # identifiers. |
| 431 | func_frame.identifiers.undeclared -= ( |
| 432 | func_frame.identifiers.undeclared & |
| 433 | func_frame.identifiers.declared |
| 434 | ) |
| 435 | |
| 436 | func_frame.accesses_arguments = False |
| 437 | func_frame.accesses_caller = False |
| 438 | func_frame.arguments = args = ['l_' + x.name for x in node.args] |
| 439 | |
| 440 | if 'arguments' in func_frame.identifiers.undeclared: |
| 441 | func_frame.accesses_arguments = True |
| 442 | func_frame.identifiers.add_special('arguments') |
| 443 | args.append('l_arguments') |
| 444 | if 'caller' in func_frame.identifiers.undeclared: |
| 445 | func_frame.accesses_caller = True |
| 446 | func_frame.identifiers.add_special('caller') |
| 447 | args.append('l_caller') |
| 448 | return func_frame |
| 449 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 450 | # -- Visitors |
| 451 | |
| 452 | def visit_Template(self, node, frame=None): |
| 453 | assert frame is None, 'no root frame allowed' |
| 454 | self.writeline('from jinja2.runtime import *') |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 455 | self.writeline('name = %r' % self.name) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 456 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 457 | # do we have an extends tag at all? If not, we can save some |
| 458 | # overhead by just not processing any inheritance code. |
| 459 | have_extends = node.find(nodes.Extends) is not None |
| 460 | |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 461 | # find all blocks |
| 462 | for block in node.find_all(nodes.Block): |
| 463 | if block.name in self.blocks: |
| 464 | raise TemplateAssertionError('block %r defined twice' % |
| 465 | block.name, block.lineno, |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 466 | self.name) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 467 | self.blocks[block.name] = block |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 468 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 469 | # generate the root render function. |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 470 | self.writeline('def root(globals, environment=environment' |
| 471 | ', standalone=False):', extra=1) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 472 | self.indent() |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 473 | self.writeline('context = TemplateContext(environment, globals, %r, ' |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 474 | 'blocks, standalone)' % self.name) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 475 | if have_extends: |
| 476 | self.writeline('parent_root = None') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 477 | self.outdent() |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 478 | |
| 479 | # process the root |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 480 | frame = Frame() |
| 481 | frame.inspect(node.body) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 482 | frame.toplevel = frame.rootlevel = True |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 483 | self.indent() |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 484 | self.pull_locals(frame, indent=False) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 485 | self.writeline('yield context') |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 486 | self.blockvisit(node.body, frame, indent=False) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 487 | self.outdent() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 488 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 489 | # make sure that the parent root is called. |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 490 | if have_extends: |
| 491 | if not self.has_known_extends: |
| 492 | self.indent() |
| 493 | self.writeline('if parent_root is not None:') |
| 494 | self.indent() |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 495 | self.writeline('stream = parent_root(context)') |
| 496 | self.writeline('stream.next()') |
| 497 | self.writeline('for event in stream:') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 498 | self.indent() |
| 499 | self.writeline('yield event') |
Armin Ronacher | 7a52df8 | 2008-04-11 13:58:22 +0200 | [diff] [blame] | 500 | self.outdent(1 + self.has_known_extends) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 501 | |
| 502 | # at this point we now have the blocks collected and can visit them too. |
| 503 | for name, block in self.blocks.iteritems(): |
| 504 | block_frame = Frame() |
| 505 | block_frame.inspect(block.body) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 506 | block_frame.block = name |
Armin Ronacher | 62f8a29 | 2008-04-13 23:18:05 +0200 | [diff] [blame] | 507 | block_frame.identifiers.add_special('super') |
| 508 | block_frame.name_overrides['super'] = 'context.super(%r)' % name |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 509 | self.writeline('def block_%s(context, environment=environment):' |
| 510 | % name, block, 1) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 511 | self.pull_locals(block_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 512 | self.blockvisit(block.body, block_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 513 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 514 | self.writeline('blocks = {%s}' % ', '.join('%r: block_%s' % (x, x) |
Armin Ronacher | ba3757b | 2008-04-16 19:43:16 +0200 | [diff] [blame] | 515 | for x in self.blocks), |
| 516 | extra=1) |
| 517 | |
| 518 | # add a function that returns the debug info |
| 519 | self.writeline('def get_debug_info():', extra=1) |
| 520 | self.indent() |
| 521 | self.writeline('return %r' % self.debug_info) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 522 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 523 | def visit_Block(self, node, frame): |
| 524 | """Call a block and register it for the template.""" |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 525 | level = 1 |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 526 | if frame.toplevel: |
Armin Ronacher | bcb7c53 | 2008-04-11 16:30:34 +0200 | [diff] [blame] | 527 | # if we know that we are a child template, there is no need to |
| 528 | # check if we are one |
| 529 | if self.has_known_extends: |
| 530 | return |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 531 | if self.extends_so_far > 0: |
| 532 | self.writeline('if parent_root is None:') |
| 533 | self.indent() |
| 534 | level += 1 |
| 535 | self.writeline('for event in context.blocks[%r][-1](context):' % node.name) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 536 | self.indent() |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 537 | if frame.buffer is None: |
| 538 | self.writeline('yield event') |
| 539 | else: |
| 540 | self.writeline('%s.append(event)' % frame.buffer) |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 541 | self.outdent(level) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 542 | |
| 543 | def visit_Extends(self, node, frame): |
| 544 | """Calls the extender.""" |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 545 | if not frame.toplevel: |
| 546 | raise TemplateAssertionError('cannot use extend from a non ' |
| 547 | 'top-level scope', node.lineno, |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 548 | self.name) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 549 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 550 | # if the number of extends statements in general is zero so |
| 551 | # far, we don't have to add a check if something extended |
| 552 | # the template before this one. |
| 553 | if self.extends_so_far > 0: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 554 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 555 | # if we have a known extends we just add a template runtime |
| 556 | # error into the generated code. We could catch that at compile |
| 557 | # time too, but i welcome it not to confuse users by throwing the |
| 558 | # same error at different times just "because we can". |
| 559 | if not self.has_known_extends: |
| 560 | self.writeline('if parent_root is not None:') |
| 561 | self.indent() |
| 562 | self.writeline('raise TemplateRuntimeError(%r)' % |
| 563 | 'extended multiple times') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 564 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 565 | # if we have a known extends already we don't need that code here |
| 566 | # as we know that the template execution will end here. |
| 567 | if self.has_known_extends: |
| 568 | raise CompilerExit() |
| 569 | self.outdent() |
| 570 | |
Armin Ronacher | 41ef36f | 2008-04-11 19:55:08 +0200 | [diff] [blame] | 571 | self.writeline('parent_root = environment.get_template(', node, 1) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 572 | self.visit(node.template, frame) |
Armin Ronacher | 68f7767 | 2008-04-17 11:50:39 +0200 | [diff] [blame] | 573 | self.write(', %r).root_render_func' % self.name) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 574 | |
| 575 | # if this extends statement was in the root level we can take |
| 576 | # advantage of that information and simplify the generated code |
| 577 | # in the top level from this point onwards |
| 578 | self.has_known_extends = True |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 579 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 580 | # and now we have one more |
| 581 | self.extends_so_far += 1 |
| 582 | |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 583 | def visit_Include(self, node, frame): |
| 584 | """Handles includes.""" |
| 585 | # simpled include is include into a variable. This kind of |
| 586 | # include works the same on every level, so we handle it first. |
| 587 | if node.target is not None: |
| 588 | self.writeline('l_%s = ' % node.target, node) |
| 589 | if frame.toplevel: |
| 590 | self.write('context[%r] = ' % node.target) |
| 591 | self.write('IncludedTemplate(environment, context, ') |
| 592 | self.visit(node.template, frame) |
| 593 | self.write(')') |
| 594 | return |
| 595 | |
| 596 | self.writeline('included_stream = environment.get_template(', node) |
| 597 | self.visit(node.template, frame) |
| 598 | self.write(').root_render_func(context, standalone=True)') |
| 599 | self.writeline('included_context = included_stream.next()') |
| 600 | self.writeline('for event in included_stream:') |
| 601 | self.indent() |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 602 | if frame.buffer is None: |
| 603 | self.writeline('yield event') |
| 604 | else: |
| 605 | self.writeline('%s.append(event)' % frame.buffer) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 606 | self.outdent() |
| 607 | |
| 608 | # if we have a toplevel include the exported variables are copied |
| 609 | # into the current context without exporting them. context.udpate |
| 610 | # does *not* mark the variables as exported |
| 611 | if frame.toplevel: |
| 612 | self.writeline('context.update(included_context.get_exported())') |
| 613 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 614 | def visit_For(self, node, frame): |
| 615 | loop_frame = frame.inner() |
| 616 | loop_frame.inspect(node.iter_child_nodes()) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 617 | extended_loop = bool(node.else_) or \ |
| 618 | 'loop' in loop_frame.identifiers.undeclared |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 619 | if extended_loop: |
| 620 | loop_frame.identifiers.add_special('loop') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 621 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 622 | aliases = self.collect_shadowed(loop_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 623 | self.pull_locals(loop_frame, indent=False) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 624 | if node.else_: |
| 625 | self.writeline('l_loop = None') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 626 | |
| 627 | self.newline(node) |
| 628 | self.writeline('for ') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 629 | self.visit(node.target, loop_frame) |
Armin Ronacher | 180a1bd | 2008-04-09 12:14:24 +0200 | [diff] [blame] | 630 | self.write(extended_loop and ', l_loop in LoopContext(' or ' in ') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 631 | |
| 632 | # the expression pointing to the parent loop. We make the |
| 633 | # undefined a bit more debug friendly at the same time. |
| 634 | parent_loop = 'loop' in aliases and aliases['loop'] \ |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame^] | 635 | or "environment.undefined(%r)" % "'loop' is undefined. " \ |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 636 | 'the filter section of a loop as well as the ' \ |
| 637 | 'else block doesn\'t have access to the special ' \ |
| 638 | "'loop' variable of the current loop. Because " \ |
| 639 | 'there is no parent loop it\'s undefined.' |
| 640 | |
| 641 | # if we have an extened loop and a node test, we filter in the |
| 642 | # "outer frame". |
| 643 | if extended_loop and node.test is not None: |
| 644 | self.write('(') |
| 645 | self.visit(node.target, loop_frame) |
| 646 | self.write(' for ') |
| 647 | self.visit(node.target, loop_frame) |
| 648 | self.write(' in ') |
| 649 | self.visit(node.iter, loop_frame) |
| 650 | self.write(' if (') |
| 651 | test_frame = loop_frame.copy() |
| 652 | test_frame.name_overrides['loop'] = parent_loop |
| 653 | self.visit(node.test, test_frame) |
| 654 | self.write('))') |
| 655 | |
| 656 | else: |
| 657 | self.visit(node.iter, loop_frame) |
| 658 | |
Armin Ronacher | 180a1bd | 2008-04-09 12:14:24 +0200 | [diff] [blame] | 659 | if 'loop' in aliases: |
| 660 | self.write(', ' + aliases['loop']) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 661 | self.write(extended_loop and '):' or ':') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 662 | |
| 663 | # tests in not extended loops become a continue |
| 664 | if not extended_loop and node.test is not None: |
| 665 | self.indent() |
| 666 | self.writeline('if ') |
| 667 | self.visit(node.test) |
| 668 | self.write(':') |
| 669 | self.indent() |
| 670 | self.writeline('continue') |
| 671 | self.outdent(2) |
| 672 | |
Armin Ronacher | be4ae24 | 2008-04-18 09:49:08 +0200 | [diff] [blame^] | 673 | self.blockvisit(node.body, loop_frame, force_generator=True) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 674 | |
| 675 | if node.else_: |
| 676 | self.writeline('if l_loop is None:') |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 677 | self.indent() |
| 678 | self.writeline('l_loop = ' + parent_loop) |
| 679 | self.outdent() |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 680 | self.blockvisit(node.else_, loop_frame, force_generator=False) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 681 | |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 682 | # reset the aliases if there are any. |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 683 | for name, alias in aliases.iteritems(): |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 684 | self.writeline('l_%s = %s' % (name, alias)) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 685 | |
| 686 | def visit_If(self, node, frame): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 687 | if_frame = frame.soft() |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 688 | self.writeline('if ', node) |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 689 | self.visit(node.test, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 690 | self.write(':') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 691 | self.blockvisit(node.body, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 692 | if node.else_: |
| 693 | self.writeline('else:') |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 694 | self.blockvisit(node.else_, if_frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 695 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 696 | def visit_Macro(self, node, frame): |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 697 | macro_frame = self.function_scoping(node, frame) |
| 698 | args = macro_frame.arguments |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 699 | self.writeline('def macro(%s):' % ', '.join(args), node) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 700 | macro_frame.buffer = buf = self.temporary_identifier() |
| 701 | self.indent() |
| 702 | self.pull_locals(macro_frame, indent=False) |
| 703 | self.writeline('%s = []' % buf) |
| 704 | self.blockvisit(node.body, macro_frame, indent=False) |
Armin Ronacher | 5236d8c | 2008-04-17 11:23:16 +0200 | [diff] [blame] | 705 | self.writeline("return Markup(u''.join(%s))" % buf) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 706 | self.outdent() |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 707 | self.newline() |
| 708 | if frame.toplevel: |
| 709 | self.write('context[%r] = ' % node.name) |
| 710 | arg_tuple = ', '.join(repr(x.name) for x in node.args) |
| 711 | if len(node.args) == 1: |
| 712 | arg_tuple += ',' |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 713 | self.write('l_%s = Macro(environment, macro, %r, (%s), (' % |
| 714 | (node.name, node.name, arg_tuple)) |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 715 | for arg in node.defaults: |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 716 | self.visit(arg, macro_frame) |
Armin Ronacher | 4f62a9f | 2008-04-08 18:09:13 +0200 | [diff] [blame] | 717 | self.write(', ') |
Armin Ronacher | 00d5d21 | 2008-04-13 01:10:18 +0200 | [diff] [blame] | 718 | self.write('), %s, %s)' % ( |
| 719 | macro_frame.accesses_arguments and '1' or '0', |
| 720 | macro_frame.accesses_caller and '1' or '0' |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 721 | )) |
| 722 | |
| 723 | def visit_CallBlock(self, node, frame): |
| 724 | call_frame = self.function_scoping(node, frame) |
| 725 | args = call_frame.arguments |
| 726 | self.writeline('def call(%s):' % ', '.join(args), node) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 727 | call_frame.buffer = buf = self.temporary_identifier() |
| 728 | self.indent() |
| 729 | self.pull_locals(call_frame, indent=False) |
| 730 | self.writeline('%s = []' % buf) |
| 731 | self.blockvisit(node.body, call_frame, indent=False) |
Armin Ronacher | 5236d8c | 2008-04-17 11:23:16 +0200 | [diff] [blame] | 732 | self.writeline("return Markup(u''.join(%s))" % buf) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 733 | self.outdent() |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 734 | arg_tuple = ', '.join(repr(x.name) for x in node.args) |
| 735 | if len(node.args) == 1: |
| 736 | arg_tuple += ',' |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 737 | self.writeline('caller = Macro(environment, call, None, (%s), (' % |
| 738 | arg_tuple) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 739 | for arg in node.defaults: |
| 740 | self.visit(arg) |
| 741 | self.write(', ') |
Armin Ronacher | 00d5d21 | 2008-04-13 01:10:18 +0200 | [diff] [blame] | 742 | self.write('), %s, 0)' % (call_frame.accesses_arguments and '1' or '0')) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 743 | if frame.buffer is None: |
| 744 | self.writeline('yield ', node) |
| 745 | else: |
| 746 | self.writeline('%s.append(' % frame.buffer, node) |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 747 | self.visit_Call(node.call, call_frame, extra_kwargs='caller=caller') |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 748 | if frame.buffer is not None: |
| 749 | self.write(')') |
| 750 | |
| 751 | def visit_FilterBlock(self, node, frame): |
| 752 | filter_frame = frame.inner() |
| 753 | filter_frame.inspect(node.iter_child_nodes()) |
| 754 | |
| 755 | aliases = self.collect_shadowed(filter_frame) |
Armin Ronacher | 625215e | 2008-04-13 16:31:08 +0200 | [diff] [blame] | 756 | self.pull_locals(filter_frame, indent=False) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 757 | filter_frame.buffer = buf = self.temporary_identifier() |
| 758 | |
| 759 | self.writeline('%s = []' % buf, node) |
| 760 | for child in node.body: |
| 761 | self.visit(child, filter_frame) |
| 762 | |
| 763 | if frame.buffer is None: |
| 764 | self.writeline('yield ', node) |
| 765 | else: |
| 766 | self.writeline('%s.append(' % frame.buffer, node) |
| 767 | self.visit_Filter(node.filter, filter_frame, "u''.join(%s)" % buf) |
| 768 | if frame.buffer is not None: |
| 769 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 770 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 771 | def visit_ExprStmt(self, node, frame): |
| 772 | self.newline(node) |
| 773 | self.visit(node, frame) |
| 774 | |
| 775 | def visit_Output(self, node, frame): |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 776 | # if we have a known extends statement, we don't output anything |
Armin Ronacher | 7a52df8 | 2008-04-11 13:58:22 +0200 | [diff] [blame] | 777 | if self.has_known_extends and frame.toplevel: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 778 | return |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 779 | |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 780 | self.newline(node) |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 781 | if self.environment.finalize is unicode: |
| 782 | finalizer = 'unicode' |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 783 | have_finalizer = False |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 784 | else: |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 785 | finalizer = 'environment.finalize' |
| 786 | have_finalizer = True |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 787 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 788 | # if we are in the toplevel scope and there was already an extends |
| 789 | # statement we have to add a check that disables our yield(s) here |
| 790 | # so that they don't appear in the output. |
| 791 | outdent_later = False |
| 792 | if frame.toplevel and self.extends_so_far != 0: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 793 | self.writeline('if parent_root is None:') |
| 794 | self.indent() |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 795 | outdent_later = True |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 796 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 797 | # try to evaluate as many chunks as possible into a static |
| 798 | # string at compile time. |
| 799 | body = [] |
| 800 | for child in node.nodes: |
| 801 | try: |
| 802 | const = unicode(child.as_const()) |
| 803 | except: |
| 804 | body.append(child) |
| 805 | continue |
| 806 | if body and isinstance(body[-1], list): |
| 807 | body[-1].append(const) |
| 808 | else: |
| 809 | body.append([const]) |
| 810 | |
| 811 | # if we have less than 3 nodes we just yield them |
| 812 | if len(body) < 3: |
| 813 | for item in body: |
| 814 | if isinstance(item, list): |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 815 | val = repr(u''.join(item)) |
| 816 | if frame.buffer is None: |
| 817 | self.writeline('yield ' + val) |
| 818 | else: |
| 819 | self.writeline('%s.append(%s)' % (frame.buffer, val)) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 820 | else: |
| 821 | self.newline(item) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 822 | if frame.buffer is None: |
| 823 | self.write('yield ') |
| 824 | else: |
| 825 | self.write('%s.append(' % frame.buffer) |
| 826 | self.write(finalizer + '(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 827 | self.visit(item, frame) |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 828 | self.write(')' * (1 + (frame.buffer is not None))) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 829 | |
| 830 | # otherwise we create a format string as this is faster in that case |
| 831 | else: |
| 832 | format = [] |
| 833 | arguments = [] |
| 834 | for item in body: |
| 835 | if isinstance(item, list): |
| 836 | format.append(u''.join(item).replace('%', '%%')) |
| 837 | else: |
| 838 | format.append('%s') |
| 839 | arguments.append(item) |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 840 | if frame.buffer is None: |
| 841 | self.writeline('yield ') |
| 842 | else: |
| 843 | self.writeline('%s.append(' % frame.buffer) |
| 844 | self.write(repr(u''.join(format)) + ' % (') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 845 | idx = -1 |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 846 | self.indent() |
| 847 | for argument in arguments: |
| 848 | self.newline(argument) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 849 | if have_finalizer: |
Armin Ronacher | 18c6ca0 | 2008-04-17 10:03:29 +0200 | [diff] [blame] | 850 | self.write(finalizer + '(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 851 | self.visit(argument, frame) |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 852 | if have_finalizer: |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 853 | self.write(')') |
Armin Ronacher | 8e8d071 | 2008-04-16 23:10:49 +0200 | [diff] [blame] | 854 | self.write(',') |
| 855 | self.outdent() |
| 856 | self.writeline(')') |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 857 | if frame.buffer is not None: |
| 858 | self.write(')') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 859 | |
Armin Ronacher | 7fb3897 | 2008-04-11 13:54:28 +0200 | [diff] [blame] | 860 | if outdent_later: |
Armin Ronacher | 75cfb86 | 2008-04-11 13:47:22 +0200 | [diff] [blame] | 861 | self.outdent() |
| 862 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 863 | def visit_Assign(self, node, frame): |
| 864 | self.newline(node) |
| 865 | # toplevel assignments however go into the local namespace and |
| 866 | # the current template's context. We create a copy of the frame |
| 867 | # here and add a set so that the Name visitor can add the assigned |
| 868 | # names here. |
| 869 | if frame.toplevel: |
| 870 | assignment_frame = frame.copy() |
| 871 | assignment_frame.assigned_names = set() |
| 872 | else: |
| 873 | assignment_frame = frame |
| 874 | self.visit(node.target, assignment_frame) |
| 875 | self.write(' = ') |
| 876 | self.visit(node.node, frame) |
Armin Ronacher | 9706fab | 2008-04-08 18:49:56 +0200 | [diff] [blame] | 877 | |
| 878 | # make sure toplevel assignments are added to the context. |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 879 | if frame.toplevel: |
| 880 | for name in assignment_frame.assigned_names: |
| 881 | self.writeline('context[%r] = l_%s' % (name, name)) |
| 882 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 883 | def visit_Name(self, node, frame): |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 884 | if node.ctx == 'store': |
| 885 | if frame.toplevel: |
| 886 | frame.assigned_names.add(node.name) |
| 887 | frame.name_overrides.pop(node.name, None) |
| 888 | elif node.ctx == 'load': |
| 889 | if node.name in frame.name_overrides: |
| 890 | self.write(frame.name_overrides[node.name]) |
| 891 | return |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 892 | self.write('l_' + node.name) |
| 893 | |
| 894 | def visit_Const(self, node, frame): |
| 895 | val = node.value |
| 896 | if isinstance(val, float): |
| 897 | # XXX: add checks for infinity and nan |
| 898 | self.write(str(val)) |
| 899 | else: |
| 900 | self.write(repr(val)) |
| 901 | |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 902 | def visit_Tuple(self, node, frame): |
| 903 | self.write('(') |
| 904 | idx = -1 |
| 905 | for idx, item in enumerate(node.items): |
| 906 | if idx: |
| 907 | self.write(', ') |
| 908 | self.visit(item, frame) |
| 909 | self.write(idx == 0 and ',)' or ')') |
| 910 | |
Armin Ronacher | 8edbe49 | 2008-04-10 20:43:43 +0200 | [diff] [blame] | 911 | def visit_List(self, node, frame): |
| 912 | self.write('[') |
| 913 | for idx, item in enumerate(node.items): |
| 914 | if idx: |
| 915 | self.write(', ') |
| 916 | self.visit(item, frame) |
| 917 | self.write(']') |
| 918 | |
| 919 | def visit_Dict(self, node, frame): |
| 920 | self.write('{') |
| 921 | for idx, item in enumerate(node.items): |
| 922 | if idx: |
| 923 | self.write(', ') |
| 924 | self.visit(item.key, frame) |
| 925 | self.write(': ') |
| 926 | self.visit(item.value, frame) |
| 927 | self.write('}') |
| 928 | |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 929 | def binop(operator): |
| 930 | def visitor(self, node, frame): |
| 931 | self.write('(') |
| 932 | self.visit(node.left, frame) |
| 933 | self.write(' %s ' % operator) |
| 934 | self.visit(node.right, frame) |
| 935 | self.write(')') |
| 936 | return visitor |
| 937 | |
| 938 | def uaop(operator): |
| 939 | def visitor(self, node, frame): |
| 940 | self.write('(' + operator) |
Armin Ronacher | 9a82205 | 2008-04-17 18:44:07 +0200 | [diff] [blame] | 941 | self.visit(node.node, frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 942 | self.write(')') |
| 943 | return visitor |
| 944 | |
| 945 | visit_Add = binop('+') |
| 946 | visit_Sub = binop('-') |
| 947 | visit_Mul = binop('*') |
| 948 | visit_Div = binop('/') |
| 949 | visit_FloorDiv = binop('//') |
| 950 | visit_Pow = binop('**') |
| 951 | visit_Mod = binop('%') |
| 952 | visit_And = binop('and') |
| 953 | visit_Or = binop('or') |
| 954 | visit_Pos = uaop('+') |
| 955 | visit_Neg = uaop('-') |
| 956 | visit_Not = uaop('not ') |
| 957 | del binop, uaop |
| 958 | |
| 959 | def visit_Compare(self, node, frame): |
| 960 | self.visit(node.expr, frame) |
| 961 | for op in node.ops: |
| 962 | self.visit(op, frame) |
| 963 | |
| 964 | def visit_Operand(self, node, frame): |
| 965 | self.write(' %s ' % operators[node.op]) |
| 966 | self.visit(node.expr, frame) |
| 967 | |
| 968 | def visit_Subscript(self, node, frame): |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 969 | if isinstance(node.arg, nodes.Slice): |
| 970 | self.visit(node.node, frame) |
| 971 | self.write('[') |
| 972 | self.visit(node.arg, frame) |
| 973 | self.write(']') |
| 974 | return |
| 975 | try: |
| 976 | const = node.arg.as_const() |
| 977 | have_const = True |
| 978 | except nodes.Impossible: |
| 979 | have_const = False |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 980 | self.write('environment.subscribe(') |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 981 | self.visit(node.node, frame) |
| 982 | self.write(', ') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 983 | if have_const: |
| 984 | self.write(repr(const)) |
| 985 | else: |
| 986 | self.visit(node.arg, frame) |
Armin Ronacher | 4dfc975 | 2008-04-09 15:03:29 +0200 | [diff] [blame] | 987 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 988 | |
| 989 | def visit_Slice(self, node, frame): |
| 990 | if node.start is not None: |
| 991 | self.visit(node.start, frame) |
| 992 | self.write(':') |
| 993 | if node.stop is not None: |
| 994 | self.visit(node.stop, frame) |
| 995 | if node.step is not None: |
| 996 | self.write(':') |
| 997 | self.visit(node.step, frame) |
| 998 | |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 999 | def visit_Filter(self, node, frame, initial=None): |
Armin Ronacher | d4c64f7 | 2008-04-11 17:15:29 +0200 | [diff] [blame] | 1000 | self.write('f_%s(' % node.name) |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 1001 | func = self.environment.filters.get(node.name) |
| 1002 | if getattr(func, 'contextfilter', False): |
| 1003 | self.write('context, ') |
Armin Ronacher | 9a027f4 | 2008-04-17 11:13:40 +0200 | [diff] [blame] | 1004 | elif getattr(func, 'environmentfilter', False): |
| 1005 | self.write('environment, ') |
Christoph Hack | 8090986 | 2008-04-14 01:35:10 +0200 | [diff] [blame] | 1006 | if isinstance(node.node, nodes.Filter): |
| 1007 | self.visit_Filter(node.node, frame, initial) |
| 1008 | elif node.node is None: |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 1009 | self.write(initial) |
| 1010 | else: |
Armin Ronacher | fa865fb | 2008-04-12 22:11:53 +0200 | [diff] [blame] | 1011 | self.visit(node.node, frame) |
Armin Ronacher | d55ab53 | 2008-04-09 16:13:39 +0200 | [diff] [blame] | 1012 | self.signature(node, frame) |
| 1013 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1014 | |
| 1015 | def visit_Test(self, node, frame): |
Armin Ronacher | f059ec1 | 2008-04-11 22:21:00 +0200 | [diff] [blame] | 1016 | self.write('t_%s(' % node.name) |
| 1017 | func = self.environment.tests.get(node.name) |
| 1018 | if getattr(func, 'contexttest', False): |
| 1019 | self.write('context, ') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1020 | self.visit(node.node, frame) |
| 1021 | self.signature(node, frame) |
Armin Ronacher | e791c2a | 2008-04-07 18:39:54 +0200 | [diff] [blame] | 1022 | self.write(')') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1023 | |
Armin Ronacher | 3d8b784 | 2008-04-13 13:16:50 +0200 | [diff] [blame] | 1024 | def visit_CondExpr(self, node, frame): |
| 1025 | if not have_condexpr: |
| 1026 | self.write('((') |
| 1027 | self.visit(node.test, frame) |
| 1028 | self.write(') and (') |
| 1029 | self.visit(node.expr1, frame) |
| 1030 | self.write(',) or (') |
| 1031 | self.visit(node.expr2, frame) |
| 1032 | self.write(',))[0]') |
| 1033 | else: |
| 1034 | self.write('(') |
| 1035 | self.visit(node.expr1, frame) |
| 1036 | self.write(' if ') |
| 1037 | self.visit(node.test, frame) |
| 1038 | self.write(' else ') |
| 1039 | self.visit(node.expr2, frame) |
| 1040 | self.write(')') |
| 1041 | |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 1042 | def visit_Call(self, node, frame, extra_kwargs=None): |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1043 | if self.environment.sandboxed: |
| 1044 | self.write('environment.call(') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1045 | self.visit(node.node, frame) |
Armin Ronacher | c63243e | 2008-04-14 22:53:58 +0200 | [diff] [blame] | 1046 | self.write(self.environment.sandboxed and ', ' or '(') |
Armin Ronacher | 7108207 | 2008-04-12 14:19:36 +0200 | [diff] [blame] | 1047 | self.signature(node, frame, False, extra_kwargs) |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1048 | self.write(')') |
| 1049 | |
| 1050 | def visit_Keyword(self, node, frame): |
Armin Ronacher | 2e9396b | 2008-04-16 14:21:57 +0200 | [diff] [blame] | 1051 | self.write(node.key + '=') |
Armin Ronacher | 8efc522 | 2008-04-08 14:47:40 +0200 | [diff] [blame] | 1052 | self.visit(node.value, frame) |