blob: 72edeb62a73c5a9a46cb5c44db5eee472fbfe23d [file] [log] [blame]
Armin Ronacher07bc6842008-03-31 14:18:49 +02001# -*- coding: utf-8 -*-
2"""
3 jinja2.nodes
4 ~~~~~~~~~~~~
5
6 This module implements additional nodes derived from the ast base node.
7
8 It also provides some node tree helper functions like `in_lineno` and
9 `get_nodes` used by the parser and translator in order to normalize
10 python and jinja nodes.
11
12 :copyright: 2007 by Armin Ronacher.
13 :license: BSD, see LICENSE for more details.
14"""
15import operator
16from itertools import chain, izip
Armin Ronacher82b3f3d2008-03-31 20:01:08 +020017from collections import deque
Armin Ronacher07bc6842008-03-31 14:18:49 +020018from copy import copy
Armin Ronacher4dfc9752008-04-09 15:03:29 +020019from jinja2.runtime import Undefined, subscribe
Armin Ronacher07bc6842008-03-31 14:18:49 +020020
21
22_binop_to_func = {
23 '*': operator.mul,
24 '/': operator.truediv,
25 '//': operator.floordiv,
26 '**': operator.pow,
27 '%': operator.mod,
28 '+': operator.add,
29 '-': operator.sub
30}
31
32_uaop_to_func = {
33 'not': operator.not_,
34 '+': operator.pos,
35 '-': operator.neg
36}
37
38
39class Impossible(Exception):
Armin Ronacher8efc5222008-04-08 14:47:40 +020040 """Raised if the node could not perform a requested action."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020041
42
43class NodeType(type):
Armin Ronacher8efc5222008-04-08 14:47:40 +020044 """A metaclass for nodes that handles the field and attribute
45 inheritance. fields and attributes from the parent class are
46 automatically forwarded to the child."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020047
48 def __new__(cls, name, bases, d):
Armin Ronachere791c2a2008-04-07 18:39:54 +020049 for attr in 'fields', 'attributes':
Armin Ronacher07bc6842008-03-31 14:18:49 +020050 storage = []
51 for base in bases:
52 storage.extend(getattr(base, attr, ()))
53 storage.extend(d.get(attr, ()))
54 assert len(storage) == len(set(storage))
55 d[attr] = tuple(storage)
56 return type.__new__(cls, name, bases, d)
57
58
59class Node(object):
Armin Ronacher8efc5222008-04-08 14:47:40 +020060 """Baseclass for all Jinja nodes."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020061 __metaclass__ = NodeType
Armin Ronachere791c2a2008-04-07 18:39:54 +020062 fields = ()
Armin Ronacherd55ab532008-04-09 16:13:39 +020063 attributes = ('lineno', 'environment')
Armin Ronacher07bc6842008-03-31 14:18:49 +020064
65 def __init__(self, *args, **kw):
66 if args:
Armin Ronachere791c2a2008-04-07 18:39:54 +020067 if len(args) != len(self.fields):
68 if not self.fields:
Armin Ronacher07bc6842008-03-31 14:18:49 +020069 raise TypeError('%r takes 0 arguments' %
70 self.__class__.__name__)
71 raise TypeError('%r takes 0 or %d argument%s' % (
72 self.__class__.__name__,
Armin Ronachere791c2a2008-04-07 18:39:54 +020073 len(self.fields),
74 len(self.fields) != 1 and 's' or ''
Armin Ronacher07bc6842008-03-31 14:18:49 +020075 ))
Armin Ronachere791c2a2008-04-07 18:39:54 +020076 for name, arg in izip(self.fields, args):
Armin Ronacher07bc6842008-03-31 14:18:49 +020077 setattr(self, name, arg)
Armin Ronachere791c2a2008-04-07 18:39:54 +020078 for attr in self.attributes:
Armin Ronacher07bc6842008-03-31 14:18:49 +020079 setattr(self, attr, kw.pop(attr, None))
80 if kw:
81 raise TypeError('unknown keyword argument %r' %
82 iter(kw).next())
83
84 def iter_fields(self):
Armin Ronachere791c2a2008-04-07 18:39:54 +020085 """Iterate over all fields."""
86 for name in self.fields:
Armin Ronacher07bc6842008-03-31 14:18:49 +020087 try:
88 yield name, getattr(self, name)
89 except AttributeError:
90 pass
91
92 def iter_child_nodes(self):
Armin Ronachere791c2a2008-04-07 18:39:54 +020093 """Iterate over all child nodes."""
Armin Ronacher07bc6842008-03-31 14:18:49 +020094 for field, item in self.iter_fields():
95 if isinstance(item, list):
96 for n in item:
97 if isinstance(n, Node):
98 yield n
99 elif isinstance(item, Node):
100 yield item
101
Armin Ronachere791c2a2008-04-07 18:39:54 +0200102 def find(self, node_type):
103 """Find the first node of a given type."""
104 for result in self.find_all(node_type):
105 return result
106
107 def find_all(self, node_type):
108 """Find all the nodes of a given type."""
109 for child in self.iter_child_nodes():
110 if isinstance(child, node_type):
111 yield child
112 for result in child.find_all(node_type):
113 yield result
114
115 def set_ctx(self, ctx):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200116 """Reset the context of a node and all child nodes. Per default the
117 parser will all generate nodes that have a 'load' context as it's the
118 most common one. This method is used in the parser to set assignment
119 targets and other nodes to a store context.
Armin Ronachere791c2a2008-04-07 18:39:54 +0200120 """
121 todo = deque([self])
122 while todo:
123 node = todo.popleft()
124 if 'ctx' in node.fields:
125 node.ctx = ctx
126 todo.extend(node.iter_child_nodes())
127
Armin Ronacherd55ab532008-04-09 16:13:39 +0200128 def set_environment(self, environment):
129 """Set the environment for all nodes."""
130 todo = deque([self])
131 while todo:
132 node = todo.popleft()
133 node.environment = environment
134 todo.extend(node.iter_child_nodes())
135
Armin Ronacher07bc6842008-03-31 14:18:49 +0200136 def __repr__(self):
137 return '%s(%s)' % (
138 self.__class__.__name__,
139 ', '.join('%s=%r' % (arg, getattr(self, arg, None)) for
Armin Ronachere791c2a2008-04-07 18:39:54 +0200140 arg in self.fields)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200141 )
142
143
144class Stmt(Node):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200145 """Base node for all statements."""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200146
147
148class Helper(Node):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200149 """Nodes that exist in a specific context only."""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200150
151
152class Template(Node):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200153 """Node that represents a template."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200154 fields = ('body',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200155
156
157class Output(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200158 """A node that holds multiple expressions which are then printed out.
159 This is used both for the `print` statement and the regular template data.
Armin Ronacher07bc6842008-03-31 14:18:49 +0200160 """
Armin Ronachere791c2a2008-04-07 18:39:54 +0200161 fields = ('nodes',)
162
163 def optimized_nodes(self):
164 """Try to optimize the nodes."""
165 buffer = []
166 for node in self.nodes:
167 try:
168 const = unicode(node.as_const())
169 except:
170 buffer.append(node)
171 else:
172 if buffer and isinstance(buffer[-1], unicode):
173 buffer[-1] += const
174 else:
175 buffer.append(const)
176 return buffer
Armin Ronacher07bc6842008-03-31 14:18:49 +0200177
178
179class Extends(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200180 """Represents an extends statement."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200181 fields = ('template',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200182
183
184class For(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200185 """A node that represents a for loop"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200186 fields = ('target', 'iter', 'body', 'else_', 'recursive')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200187
188
189class If(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200190 """A node that represents an if condition."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200191 fields = ('test', 'body', 'else_')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200192
193
194class Macro(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200195 """A node that represents a macro."""
196 fields = ('name', 'args', 'defaults', 'body')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200197
198
199class CallBlock(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200200 """A node that represents am extended macro call."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200201 fields = ('call', 'body')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200202
203
204class Set(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200205 """Allows defining own variables."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200206 fields = ('name', 'expr')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200207
208
209class FilterBlock(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200210 """Node for filter sections."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200211 fields = ('body', 'filters')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200212
213
214class Block(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200215 """A node that represents a block."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200216 fields = ('name', 'body')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200217
218
219class Include(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200220 """A node that represents the include tag."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200221 fields = ('template', 'target')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200222
223
224class Trans(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200225 """A node for translatable sections."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200226 fields = ('singular', 'plural', 'indicator', 'replacements')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200227
228
229class ExprStmt(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200230 """A statement that evaluates an expression to None."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200231 fields = ('node',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200232
233
Armin Ronacher82b3f3d2008-03-31 20:01:08 +0200234class Assign(Stmt):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200235 """Assigns an expression to a target."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200236 fields = ('target', 'node')
Armin Ronacher82b3f3d2008-03-31 20:01:08 +0200237
238
Armin Ronacher07bc6842008-03-31 14:18:49 +0200239class Expr(Node):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200240 """Baseclass for all expressions."""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200241
242 def as_const(self):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200243 """Return the value of the expression as constant or raise
244 `Impossible` if this was not possible.
Armin Ronacher07bc6842008-03-31 14:18:49 +0200245 """
246 raise Impossible()
247
248 def can_assign(self):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200249 """Check if it's possible to assign something to this node."""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200250 return False
251
252
253class BinExpr(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200254 """Baseclass for all binary expressions."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200255 fields = ('left', 'right')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200256 operator = None
257
258 def as_const(self):
259 f = _binop_to_func[self.operator]
260 try:
261 return f(self.left.as_const(), self.right.as_const())
262 except:
Armin Ronacher07bc6842008-03-31 14:18:49 +0200263 raise Impossible()
264
265
266class UnaryExpr(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200267 """Baseclass for all unary expressions."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200268 fields = ('node',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200269 operator = None
270
271 def as_const(self):
272 f = _uaop_to_func[self.operator]
273 try:
274 return f(self.node.as_const())
275 except:
276 raise Impossible()
277
278
279class Name(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200280 """any name such as {{ foo }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200281 fields = ('name', 'ctx')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200282
283 def can_assign(self):
Armin Ronachere791c2a2008-04-07 18:39:54 +0200284 return self.name not in ('true', 'false', 'none')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200285
286
287class Literal(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200288 """Baseclass for literals."""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200289
290
291class Const(Literal):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200292 """any constat such as {{ "foo" }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200293 fields = ('value',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200294
295 def as_const(self):
296 return self.value
297
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200298 @classmethod
Armin Ronacherd55ab532008-04-09 16:13:39 +0200299 def from_untrusted(cls, value, lineno=None, environment=None):
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200300 """Return a const object if the value is representable as
301 constant value in the generated code, otherwise it will raise
302 an `Impossible` exception."""
303 from compiler import has_safe_repr
304 if not has_safe_repr(value):
305 if silent:
306 return
307 raise Impossible()
Armin Ronacherd55ab532008-04-09 16:13:39 +0200308 return cls(value, lineno=lineno, environment=environment)
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200309
Armin Ronacher07bc6842008-03-31 14:18:49 +0200310
311class Tuple(Literal):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200312 """For loop unpacking and some other things like multiple arguments
Armin Ronacher07bc6842008-03-31 14:18:49 +0200313 for subscripts.
314 """
Armin Ronachere791c2a2008-04-07 18:39:54 +0200315 fields = ('items', 'ctx')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200316
317 def as_const(self):
318 return tuple(x.as_const() for x in self.items)
319
320 def can_assign(self):
321 for item in self.items:
322 if not item.can_assign():
323 return False
324 return True
325
326
327class List(Literal):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200328 """any list literal such as {{ [1, 2, 3] }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200329 fields = ('items',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200330
331 def as_const(self):
332 return [x.as_const() for x in self.items]
333
334
335class Dict(Literal):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200336 """any dict literal such as {{ {1: 2, 3: 4} }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200337 fields = ('items',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200338
339 def as_const(self):
340 return dict(x.as_const() for x in self.items)
341
342
343class Pair(Helper):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200344 """A key, value pair for dicts."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200345 fields = ('key', 'value')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200346
347 def as_const(self):
348 return self.key.as_const(), self.value.as_const()
349
350
Armin Ronacher8efc5222008-04-08 14:47:40 +0200351class Keyword(Helper):
352 """A key, value pair for keyword arguments."""
353 fields = ('key', 'value')
354
355
Armin Ronacher07bc6842008-03-31 14:18:49 +0200356class CondExpr(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200357 """{{ foo if bar else baz }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200358 fields = ('test', 'expr1', 'expr2')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200359
360 def as_const(self):
361 if self.test.as_const():
362 return self.expr1.as_const()
363 return self.expr2.as_const()
364
365
366class Filter(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200367 """{{ foo|bar|baz }}"""
Armin Ronacherd55ab532008-04-09 16:13:39 +0200368 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200369
Armin Ronacherd55ab532008-04-09 16:13:39 +0200370 def as_const(self):
371 filter = self.environment.filters.get(self.name)
372 if filter is None or getattr(filter, 'contextfilter', False):
373 raise nodes.Impossible()
374 obj = self.node.as_const()
375 args = [x.as_const() for x in self.args]
376 kwargs = dict(x.as_const() for x in self.kwargs)
377 if self.dyn_args is not None:
378 try:
379 args.extend(self.dyn_args.as_const())
380 except:
381 raise Impossible()
382 if self.dyn_kwargs is not None:
383 try:
384 kwargs.update(self.dyn_kwargs.as_const())
385 except:
386 raise Impossible()
387 try:
388 return filter(obj, *args, **kwargs)
389 except:
390 raise nodes.Impossible()
Armin Ronacher82b3f3d2008-03-31 20:01:08 +0200391
392
Armin Ronacher07bc6842008-03-31 14:18:49 +0200393class Test(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200394 """{{ foo is lower }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200395 fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200396
397
398class Call(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200399 """{{ foo(bar) }}"""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200400 fields = ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200401
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200402 def as_const(self):
403 obj = self.node.as_const()
404 args = [x.as_const() for x in self.args]
405 kwargs = dict(x.as_const() for x in self.kwargs)
406 if self.dyn_args is not None:
407 try:
408 args.extend(self.dyn_args.as_const())
409 except:
410 raise Impossible()
411 if self.dyn_kwargs is not None:
412 try:
Armin Ronacherd55ab532008-04-09 16:13:39 +0200413 kwargs.update(self.dyn_kwargs.as_const())
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200414 except:
415 raise Impossible()
416 try:
417 return obj(*args, **kwargs)
418 except:
419 raise nodes.Impossible()
420
Armin Ronacher07bc6842008-03-31 14:18:49 +0200421
422class Subscript(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200423 """{{ foo.bar }} and {{ foo['bar'] }} etc."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200424 fields = ('node', 'arg', 'ctx')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200425
426 def as_const(self):
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200427 if self.ctx != 'load':
428 raise Impossible()
Armin Ronacher07bc6842008-03-31 14:18:49 +0200429 try:
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200430 return subscribe(self.node.as_const(), self.arg.as_const())
Armin Ronacher07bc6842008-03-31 14:18:49 +0200431 except:
432 raise Impossible()
433
434 def can_assign(self):
435 return True
436
437
438class Slice(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200439 """1:2:3 etc."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200440 fields = ('start', 'stop', 'step')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200441
Armin Ronacher4dfc9752008-04-09 15:03:29 +0200442 def as_const(self):
443 def const(obj):
444 if obj is None:
445 return obj
446 return obj.as_const()
447 return slice(const(self.start), const(self.stop), const(self.step))
448
Armin Ronacher07bc6842008-03-31 14:18:49 +0200449
450class Concat(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200451 """For {{ foo ~ bar }}. Concatenates strings."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200452 fields = ('nodes',)
Armin Ronacher07bc6842008-03-31 14:18:49 +0200453
454 def as_const(self):
455 return ''.join(unicode(x.as_const()) for x in self.nodes)
456
457
458class Compare(Expr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200459 """{{ foo == bar }}, {{ foo >= bar }} etc."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200460 fields = ('expr', 'ops')
Armin Ronacher07bc6842008-03-31 14:18:49 +0200461
462
Armin Ronacher82b3f3d2008-03-31 20:01:08 +0200463class Operand(Helper):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200464 """Operator + expression."""
Armin Ronachere791c2a2008-04-07 18:39:54 +0200465 fields = ('op', 'expr')
Armin Ronacher82b3f3d2008-03-31 20:01:08 +0200466
467
Armin Ronacher07bc6842008-03-31 14:18:49 +0200468class Mul(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200469 """{{ foo * bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200470 operator = '*'
471
472
473class Div(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200474 """{{ foo / bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200475 operator = '/'
476
477
478class FloorDiv(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200479 """{{ foo // bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200480 operator = '//'
481
482
483class Add(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200484 """{{ foo + bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200485 operator = '+'
486
487
488class Sub(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200489 """{{ foo - bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200490 operator = '-'
491
492
493class Mod(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200494 """{{ foo % bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200495 operator = '%'
496
497
498class Pow(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200499 """{{ foo ** bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200500 operator = '**'
501
502
503class And(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200504 """{{ foo and bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200505 operator = 'and'
506
507 def as_const(self):
508 return self.left.as_const() and self.right.as_const()
509
510
511class Or(BinExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200512 """{{ foo or bar }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200513 operator = 'or'
514
515 def as_const(self):
516 return self.left.as_const() or self.right.as_const()
517
518
519class Not(UnaryExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200520 """{{ not foo }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200521 operator = 'not'
522
523
Armin Ronachere791c2a2008-04-07 18:39:54 +0200524class Neg(UnaryExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200525 """{{ -foo }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200526 operator = '-'
527
528
Armin Ronachere791c2a2008-04-07 18:39:54 +0200529class Pos(UnaryExpr):
Armin Ronacher8efc5222008-04-08 14:47:40 +0200530 """{{ +foo }}"""
Armin Ronacher07bc6842008-03-31 14:18:49 +0200531 operator = '+'