xref: /aosp_15_r20/external/libchrome/third_party/jinja2/nodes.py (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker# -*- coding: utf-8 -*-
2*635a8641SAndroid Build Coastguard Worker"""
3*635a8641SAndroid Build Coastguard Worker    jinja2.nodes
4*635a8641SAndroid Build Coastguard Worker    ~~~~~~~~~~~~
5*635a8641SAndroid Build Coastguard Worker
6*635a8641SAndroid Build Coastguard Worker    This module implements additional nodes derived from the ast base node.
7*635a8641SAndroid Build Coastguard Worker
8*635a8641SAndroid Build Coastguard Worker    It also provides some node tree helper functions like `in_lineno` and
9*635a8641SAndroid Build Coastguard Worker    `get_nodes` used by the parser and translator in order to normalize
10*635a8641SAndroid Build Coastguard Worker    python and jinja nodes.
11*635a8641SAndroid Build Coastguard Worker
12*635a8641SAndroid Build Coastguard Worker    :copyright: (c) 2017 by the Jinja Team.
13*635a8641SAndroid Build Coastguard Worker    :license: BSD, see LICENSE for more details.
14*635a8641SAndroid Build Coastguard Worker"""
15*635a8641SAndroid Build Coastguard Workerimport types
16*635a8641SAndroid Build Coastguard Workerimport operator
17*635a8641SAndroid Build Coastguard Worker
18*635a8641SAndroid Build Coastguard Workerfrom collections import deque
19*635a8641SAndroid Build Coastguard Workerfrom jinja2.utils import Markup
20*635a8641SAndroid Build Coastguard Workerfrom jinja2._compat import izip, with_metaclass, text_type, PY2
21*635a8641SAndroid Build Coastguard Worker
22*635a8641SAndroid Build Coastguard Worker
23*635a8641SAndroid Build Coastguard Worker#: the types we support for context functions
24*635a8641SAndroid Build Coastguard Worker_context_function_types = (types.FunctionType, types.MethodType)
25*635a8641SAndroid Build Coastguard Worker
26*635a8641SAndroid Build Coastguard Worker
27*635a8641SAndroid Build Coastguard Worker_binop_to_func = {
28*635a8641SAndroid Build Coastguard Worker    '*':        operator.mul,
29*635a8641SAndroid Build Coastguard Worker    '/':        operator.truediv,
30*635a8641SAndroid Build Coastguard Worker    '//':       operator.floordiv,
31*635a8641SAndroid Build Coastguard Worker    '**':       operator.pow,
32*635a8641SAndroid Build Coastguard Worker    '%':        operator.mod,
33*635a8641SAndroid Build Coastguard Worker    '+':        operator.add,
34*635a8641SAndroid Build Coastguard Worker    '-':        operator.sub
35*635a8641SAndroid Build Coastguard Worker}
36*635a8641SAndroid Build Coastguard Worker
37*635a8641SAndroid Build Coastguard Worker_uaop_to_func = {
38*635a8641SAndroid Build Coastguard Worker    'not':      operator.not_,
39*635a8641SAndroid Build Coastguard Worker    '+':        operator.pos,
40*635a8641SAndroid Build Coastguard Worker    '-':        operator.neg
41*635a8641SAndroid Build Coastguard Worker}
42*635a8641SAndroid Build Coastguard Worker
43*635a8641SAndroid Build Coastguard Worker_cmpop_to_func = {
44*635a8641SAndroid Build Coastguard Worker    'eq':       operator.eq,
45*635a8641SAndroid Build Coastguard Worker    'ne':       operator.ne,
46*635a8641SAndroid Build Coastguard Worker    'gt':       operator.gt,
47*635a8641SAndroid Build Coastguard Worker    'gteq':     operator.ge,
48*635a8641SAndroid Build Coastguard Worker    'lt':       operator.lt,
49*635a8641SAndroid Build Coastguard Worker    'lteq':     operator.le,
50*635a8641SAndroid Build Coastguard Worker    'in':       lambda a, b: a in b,
51*635a8641SAndroid Build Coastguard Worker    'notin':    lambda a, b: a not in b
52*635a8641SAndroid Build Coastguard Worker}
53*635a8641SAndroid Build Coastguard Worker
54*635a8641SAndroid Build Coastguard Worker
55*635a8641SAndroid Build Coastguard Workerclass Impossible(Exception):
56*635a8641SAndroid Build Coastguard Worker    """Raised if the node could not perform a requested action."""
57*635a8641SAndroid Build Coastguard Worker
58*635a8641SAndroid Build Coastguard Worker
59*635a8641SAndroid Build Coastguard Workerclass NodeType(type):
60*635a8641SAndroid Build Coastguard Worker    """A metaclass for nodes that handles the field and attribute
61*635a8641SAndroid Build Coastguard Worker    inheritance.  fields and attributes from the parent class are
62*635a8641SAndroid Build Coastguard Worker    automatically forwarded to the child."""
63*635a8641SAndroid Build Coastguard Worker
64*635a8641SAndroid Build Coastguard Worker    def __new__(cls, name, bases, d):
65*635a8641SAndroid Build Coastguard Worker        for attr in 'fields', 'attributes':
66*635a8641SAndroid Build Coastguard Worker            storage = []
67*635a8641SAndroid Build Coastguard Worker            storage.extend(getattr(bases[0], attr, ()))
68*635a8641SAndroid Build Coastguard Worker            storage.extend(d.get(attr, ()))
69*635a8641SAndroid Build Coastguard Worker            assert len(bases) == 1, 'multiple inheritance not allowed'
70*635a8641SAndroid Build Coastguard Worker            assert len(storage) == len(set(storage)), 'layout conflict'
71*635a8641SAndroid Build Coastguard Worker            d[attr] = tuple(storage)
72*635a8641SAndroid Build Coastguard Worker        d.setdefault('abstract', False)
73*635a8641SAndroid Build Coastguard Worker        return type.__new__(cls, name, bases, d)
74*635a8641SAndroid Build Coastguard Worker
75*635a8641SAndroid Build Coastguard Worker
76*635a8641SAndroid Build Coastguard Workerclass EvalContext(object):
77*635a8641SAndroid Build Coastguard Worker    """Holds evaluation time information.  Custom attributes can be attached
78*635a8641SAndroid Build Coastguard Worker    to it in extensions.
79*635a8641SAndroid Build Coastguard Worker    """
80*635a8641SAndroid Build Coastguard Worker
81*635a8641SAndroid Build Coastguard Worker    def __init__(self, environment, template_name=None):
82*635a8641SAndroid Build Coastguard Worker        self.environment = environment
83*635a8641SAndroid Build Coastguard Worker        if callable(environment.autoescape):
84*635a8641SAndroid Build Coastguard Worker            self.autoescape = environment.autoescape(template_name)
85*635a8641SAndroid Build Coastguard Worker        else:
86*635a8641SAndroid Build Coastguard Worker            self.autoescape = environment.autoescape
87*635a8641SAndroid Build Coastguard Worker        self.volatile = False
88*635a8641SAndroid Build Coastguard Worker
89*635a8641SAndroid Build Coastguard Worker    def save(self):
90*635a8641SAndroid Build Coastguard Worker        return self.__dict__.copy()
91*635a8641SAndroid Build Coastguard Worker
92*635a8641SAndroid Build Coastguard Worker    def revert(self, old):
93*635a8641SAndroid Build Coastguard Worker        self.__dict__.clear()
94*635a8641SAndroid Build Coastguard Worker        self.__dict__.update(old)
95*635a8641SAndroid Build Coastguard Worker
96*635a8641SAndroid Build Coastguard Worker
97*635a8641SAndroid Build Coastguard Workerdef get_eval_context(node, ctx):
98*635a8641SAndroid Build Coastguard Worker    if ctx is None:
99*635a8641SAndroid Build Coastguard Worker        if node.environment is None:
100*635a8641SAndroid Build Coastguard Worker            raise RuntimeError('if no eval context is passed, the '
101*635a8641SAndroid Build Coastguard Worker                               'node must have an attached '
102*635a8641SAndroid Build Coastguard Worker                               'environment.')
103*635a8641SAndroid Build Coastguard Worker        return EvalContext(node.environment)
104*635a8641SAndroid Build Coastguard Worker    return ctx
105*635a8641SAndroid Build Coastguard Worker
106*635a8641SAndroid Build Coastguard Worker
107*635a8641SAndroid Build Coastguard Workerclass Node(with_metaclass(NodeType, object)):
108*635a8641SAndroid Build Coastguard Worker    """Baseclass for all Jinja2 nodes.  There are a number of nodes available
109*635a8641SAndroid Build Coastguard Worker    of different types.  There are four major types:
110*635a8641SAndroid Build Coastguard Worker
111*635a8641SAndroid Build Coastguard Worker    -   :class:`Stmt`: statements
112*635a8641SAndroid Build Coastguard Worker    -   :class:`Expr`: expressions
113*635a8641SAndroid Build Coastguard Worker    -   :class:`Helper`: helper nodes
114*635a8641SAndroid Build Coastguard Worker    -   :class:`Template`: the outermost wrapper node
115*635a8641SAndroid Build Coastguard Worker
116*635a8641SAndroid Build Coastguard Worker    All nodes have fields and attributes.  Fields may be other nodes, lists,
117*635a8641SAndroid Build Coastguard Worker    or arbitrary values.  Fields are passed to the constructor as regular
118*635a8641SAndroid Build Coastguard Worker    positional arguments, attributes as keyword arguments.  Each node has
119*635a8641SAndroid Build Coastguard Worker    two attributes: `lineno` (the line number of the node) and `environment`.
120*635a8641SAndroid Build Coastguard Worker    The `environment` attribute is set at the end of the parsing process for
121*635a8641SAndroid Build Coastguard Worker    all nodes automatically.
122*635a8641SAndroid Build Coastguard Worker    """
123*635a8641SAndroid Build Coastguard Worker    fields = ()
124*635a8641SAndroid Build Coastguard Worker    attributes = ('lineno', 'environment')
125*635a8641SAndroid Build Coastguard Worker    abstract = True
126*635a8641SAndroid Build Coastguard Worker
127*635a8641SAndroid Build Coastguard Worker    def __init__(self, *fields, **attributes):
128*635a8641SAndroid Build Coastguard Worker        if self.abstract:
129*635a8641SAndroid Build Coastguard Worker            raise TypeError('abstract nodes are not instanciable')
130*635a8641SAndroid Build Coastguard Worker        if fields:
131*635a8641SAndroid Build Coastguard Worker            if len(fields) != len(self.fields):
132*635a8641SAndroid Build Coastguard Worker                if not self.fields:
133*635a8641SAndroid Build Coastguard Worker                    raise TypeError('%r takes 0 arguments' %
134*635a8641SAndroid Build Coastguard Worker                                    self.__class__.__name__)
135*635a8641SAndroid Build Coastguard Worker                raise TypeError('%r takes 0 or %d argument%s' % (
136*635a8641SAndroid Build Coastguard Worker                    self.__class__.__name__,
137*635a8641SAndroid Build Coastguard Worker                    len(self.fields),
138*635a8641SAndroid Build Coastguard Worker                    len(self.fields) != 1 and 's' or ''
139*635a8641SAndroid Build Coastguard Worker                ))
140*635a8641SAndroid Build Coastguard Worker            for name, arg in izip(self.fields, fields):
141*635a8641SAndroid Build Coastguard Worker                setattr(self, name, arg)
142*635a8641SAndroid Build Coastguard Worker        for attr in self.attributes:
143*635a8641SAndroid Build Coastguard Worker            setattr(self, attr, attributes.pop(attr, None))
144*635a8641SAndroid Build Coastguard Worker        if attributes:
145*635a8641SAndroid Build Coastguard Worker            raise TypeError('unknown attribute %r' %
146*635a8641SAndroid Build Coastguard Worker                            next(iter(attributes)))
147*635a8641SAndroid Build Coastguard Worker
148*635a8641SAndroid Build Coastguard Worker    def iter_fields(self, exclude=None, only=None):
149*635a8641SAndroid Build Coastguard Worker        """This method iterates over all fields that are defined and yields
150*635a8641SAndroid Build Coastguard Worker        ``(key, value)`` tuples.  Per default all fields are returned, but
151*635a8641SAndroid Build Coastguard Worker        it's possible to limit that to some fields by providing the `only`
152*635a8641SAndroid Build Coastguard Worker        parameter or to exclude some using the `exclude` parameter.  Both
153*635a8641SAndroid Build Coastguard Worker        should be sets or tuples of field names.
154*635a8641SAndroid Build Coastguard Worker        """
155*635a8641SAndroid Build Coastguard Worker        for name in self.fields:
156*635a8641SAndroid Build Coastguard Worker            if (exclude is only is None) or \
157*635a8641SAndroid Build Coastguard Worker               (exclude is not None and name not in exclude) or \
158*635a8641SAndroid Build Coastguard Worker               (only is not None and name in only):
159*635a8641SAndroid Build Coastguard Worker                try:
160*635a8641SAndroid Build Coastguard Worker                    yield name, getattr(self, name)
161*635a8641SAndroid Build Coastguard Worker                except AttributeError:
162*635a8641SAndroid Build Coastguard Worker                    pass
163*635a8641SAndroid Build Coastguard Worker
164*635a8641SAndroid Build Coastguard Worker    def iter_child_nodes(self, exclude=None, only=None):
165*635a8641SAndroid Build Coastguard Worker        """Iterates over all direct child nodes of the node.  This iterates
166*635a8641SAndroid Build Coastguard Worker        over all fields and yields the values of they are nodes.  If the value
167*635a8641SAndroid Build Coastguard Worker        of a field is a list all the nodes in that list are returned.
168*635a8641SAndroid Build Coastguard Worker        """
169*635a8641SAndroid Build Coastguard Worker        for field, item in self.iter_fields(exclude, only):
170*635a8641SAndroid Build Coastguard Worker            if isinstance(item, list):
171*635a8641SAndroid Build Coastguard Worker                for n in item:
172*635a8641SAndroid Build Coastguard Worker                    if isinstance(n, Node):
173*635a8641SAndroid Build Coastguard Worker                        yield n
174*635a8641SAndroid Build Coastguard Worker            elif isinstance(item, Node):
175*635a8641SAndroid Build Coastguard Worker                yield item
176*635a8641SAndroid Build Coastguard Worker
177*635a8641SAndroid Build Coastguard Worker    def find(self, node_type):
178*635a8641SAndroid Build Coastguard Worker        """Find the first node of a given type.  If no such node exists the
179*635a8641SAndroid Build Coastguard Worker        return value is `None`.
180*635a8641SAndroid Build Coastguard Worker        """
181*635a8641SAndroid Build Coastguard Worker        for result in self.find_all(node_type):
182*635a8641SAndroid Build Coastguard Worker            return result
183*635a8641SAndroid Build Coastguard Worker
184*635a8641SAndroid Build Coastguard Worker    def find_all(self, node_type):
185*635a8641SAndroid Build Coastguard Worker        """Find all the nodes of a given type.  If the type is a tuple,
186*635a8641SAndroid Build Coastguard Worker        the check is performed for any of the tuple items.
187*635a8641SAndroid Build Coastguard Worker        """
188*635a8641SAndroid Build Coastguard Worker        for child in self.iter_child_nodes():
189*635a8641SAndroid Build Coastguard Worker            if isinstance(child, node_type):
190*635a8641SAndroid Build Coastguard Worker                yield child
191*635a8641SAndroid Build Coastguard Worker            for result in child.find_all(node_type):
192*635a8641SAndroid Build Coastguard Worker                yield result
193*635a8641SAndroid Build Coastguard Worker
194*635a8641SAndroid Build Coastguard Worker    def set_ctx(self, ctx):
195*635a8641SAndroid Build Coastguard Worker        """Reset the context of a node and all child nodes.  Per default the
196*635a8641SAndroid Build Coastguard Worker        parser will all generate nodes that have a 'load' context as it's the
197*635a8641SAndroid Build Coastguard Worker        most common one.  This method is used in the parser to set assignment
198*635a8641SAndroid Build Coastguard Worker        targets and other nodes to a store context.
199*635a8641SAndroid Build Coastguard Worker        """
200*635a8641SAndroid Build Coastguard Worker        todo = deque([self])
201*635a8641SAndroid Build Coastguard Worker        while todo:
202*635a8641SAndroid Build Coastguard Worker            node = todo.popleft()
203*635a8641SAndroid Build Coastguard Worker            if 'ctx' in node.fields:
204*635a8641SAndroid Build Coastguard Worker                node.ctx = ctx
205*635a8641SAndroid Build Coastguard Worker            todo.extend(node.iter_child_nodes())
206*635a8641SAndroid Build Coastguard Worker        return self
207*635a8641SAndroid Build Coastguard Worker
208*635a8641SAndroid Build Coastguard Worker    def set_lineno(self, lineno, override=False):
209*635a8641SAndroid Build Coastguard Worker        """Set the line numbers of the node and children."""
210*635a8641SAndroid Build Coastguard Worker        todo = deque([self])
211*635a8641SAndroid Build Coastguard Worker        while todo:
212*635a8641SAndroid Build Coastguard Worker            node = todo.popleft()
213*635a8641SAndroid Build Coastguard Worker            if 'lineno' in node.attributes:
214*635a8641SAndroid Build Coastguard Worker                if node.lineno is None or override:
215*635a8641SAndroid Build Coastguard Worker                    node.lineno = lineno
216*635a8641SAndroid Build Coastguard Worker            todo.extend(node.iter_child_nodes())
217*635a8641SAndroid Build Coastguard Worker        return self
218*635a8641SAndroid Build Coastguard Worker
219*635a8641SAndroid Build Coastguard Worker    def set_environment(self, environment):
220*635a8641SAndroid Build Coastguard Worker        """Set the environment for all nodes."""
221*635a8641SAndroid Build Coastguard Worker        todo = deque([self])
222*635a8641SAndroid Build Coastguard Worker        while todo:
223*635a8641SAndroid Build Coastguard Worker            node = todo.popleft()
224*635a8641SAndroid Build Coastguard Worker            node.environment = environment
225*635a8641SAndroid Build Coastguard Worker            todo.extend(node.iter_child_nodes())
226*635a8641SAndroid Build Coastguard Worker        return self
227*635a8641SAndroid Build Coastguard Worker
228*635a8641SAndroid Build Coastguard Worker    def __eq__(self, other):
229*635a8641SAndroid Build Coastguard Worker        return type(self) is type(other) and \
230*635a8641SAndroid Build Coastguard Worker               tuple(self.iter_fields()) == tuple(other.iter_fields())
231*635a8641SAndroid Build Coastguard Worker
232*635a8641SAndroid Build Coastguard Worker    def __ne__(self, other):
233*635a8641SAndroid Build Coastguard Worker        return not self.__eq__(other)
234*635a8641SAndroid Build Coastguard Worker
235*635a8641SAndroid Build Coastguard Worker    # Restore Python 2 hashing behavior on Python 3
236*635a8641SAndroid Build Coastguard Worker    __hash__ = object.__hash__
237*635a8641SAndroid Build Coastguard Worker
238*635a8641SAndroid Build Coastguard Worker    def __repr__(self):
239*635a8641SAndroid Build Coastguard Worker        return '%s(%s)' % (
240*635a8641SAndroid Build Coastguard Worker            self.__class__.__name__,
241*635a8641SAndroid Build Coastguard Worker            ', '.join('%s=%r' % (arg, getattr(self, arg, None)) for
242*635a8641SAndroid Build Coastguard Worker                      arg in self.fields)
243*635a8641SAndroid Build Coastguard Worker        )
244*635a8641SAndroid Build Coastguard Worker
245*635a8641SAndroid Build Coastguard Worker    def dump(self):
246*635a8641SAndroid Build Coastguard Worker        def _dump(node):
247*635a8641SAndroid Build Coastguard Worker            if not isinstance(node, Node):
248*635a8641SAndroid Build Coastguard Worker                buf.append(repr(node))
249*635a8641SAndroid Build Coastguard Worker                return
250*635a8641SAndroid Build Coastguard Worker
251*635a8641SAndroid Build Coastguard Worker            buf.append('nodes.%s(' % node.__class__.__name__)
252*635a8641SAndroid Build Coastguard Worker            if not node.fields:
253*635a8641SAndroid Build Coastguard Worker                buf.append(')')
254*635a8641SAndroid Build Coastguard Worker                return
255*635a8641SAndroid Build Coastguard Worker            for idx, field in enumerate(node.fields):
256*635a8641SAndroid Build Coastguard Worker                if idx:
257*635a8641SAndroid Build Coastguard Worker                    buf.append(', ')
258*635a8641SAndroid Build Coastguard Worker                value = getattr(node, field)
259*635a8641SAndroid Build Coastguard Worker                if isinstance(value, list):
260*635a8641SAndroid Build Coastguard Worker                    buf.append('[')
261*635a8641SAndroid Build Coastguard Worker                    for idx, item in enumerate(value):
262*635a8641SAndroid Build Coastguard Worker                        if idx:
263*635a8641SAndroid Build Coastguard Worker                            buf.append(', ')
264*635a8641SAndroid Build Coastguard Worker                        _dump(item)
265*635a8641SAndroid Build Coastguard Worker                    buf.append(']')
266*635a8641SAndroid Build Coastguard Worker                else:
267*635a8641SAndroid Build Coastguard Worker                    _dump(value)
268*635a8641SAndroid Build Coastguard Worker            buf.append(')')
269*635a8641SAndroid Build Coastguard Worker        buf = []
270*635a8641SAndroid Build Coastguard Worker        _dump(self)
271*635a8641SAndroid Build Coastguard Worker        return ''.join(buf)
272*635a8641SAndroid Build Coastguard Worker
273*635a8641SAndroid Build Coastguard Worker
274*635a8641SAndroid Build Coastguard Worker
275*635a8641SAndroid Build Coastguard Workerclass Stmt(Node):
276*635a8641SAndroid Build Coastguard Worker    """Base node for all statements."""
277*635a8641SAndroid Build Coastguard Worker    abstract = True
278*635a8641SAndroid Build Coastguard Worker
279*635a8641SAndroid Build Coastguard Worker
280*635a8641SAndroid Build Coastguard Workerclass Helper(Node):
281*635a8641SAndroid Build Coastguard Worker    """Nodes that exist in a specific context only."""
282*635a8641SAndroid Build Coastguard Worker    abstract = True
283*635a8641SAndroid Build Coastguard Worker
284*635a8641SAndroid Build Coastguard Worker
285*635a8641SAndroid Build Coastguard Workerclass Template(Node):
286*635a8641SAndroid Build Coastguard Worker    """Node that represents a template.  This must be the outermost node that
287*635a8641SAndroid Build Coastguard Worker    is passed to the compiler.
288*635a8641SAndroid Build Coastguard Worker    """
289*635a8641SAndroid Build Coastguard Worker    fields = ('body',)
290*635a8641SAndroid Build Coastguard Worker
291*635a8641SAndroid Build Coastguard Worker
292*635a8641SAndroid Build Coastguard Workerclass Output(Stmt):
293*635a8641SAndroid Build Coastguard Worker    """A node that holds multiple expressions which are then printed out.
294*635a8641SAndroid Build Coastguard Worker    This is used both for the `print` statement and the regular template data.
295*635a8641SAndroid Build Coastguard Worker    """
296*635a8641SAndroid Build Coastguard Worker    fields = ('nodes',)
297*635a8641SAndroid Build Coastguard Worker
298*635a8641SAndroid Build Coastguard Worker
299*635a8641SAndroid Build Coastguard Workerclass Extends(Stmt):
300*635a8641SAndroid Build Coastguard Worker    """Represents an extends statement."""
301*635a8641SAndroid Build Coastguard Worker    fields = ('template',)
302*635a8641SAndroid Build Coastguard Worker
303*635a8641SAndroid Build Coastguard Worker
304*635a8641SAndroid Build Coastguard Workerclass For(Stmt):
305*635a8641SAndroid Build Coastguard Worker    """The for loop.  `target` is the target for the iteration (usually a
306*635a8641SAndroid Build Coastguard Worker    :class:`Name` or :class:`Tuple`), `iter` the iterable.  `body` is a list
307*635a8641SAndroid Build Coastguard Worker    of nodes that are used as loop-body, and `else_` a list of nodes for the
308*635a8641SAndroid Build Coastguard Worker    `else` block.  If no else node exists it has to be an empty list.
309*635a8641SAndroid Build Coastguard Worker
310*635a8641SAndroid Build Coastguard Worker    For filtered nodes an expression can be stored as `test`, otherwise `None`.
311*635a8641SAndroid Build Coastguard Worker    """
312*635a8641SAndroid Build Coastguard Worker    fields = ('target', 'iter', 'body', 'else_', 'test', 'recursive')
313*635a8641SAndroid Build Coastguard Worker
314*635a8641SAndroid Build Coastguard Worker
315*635a8641SAndroid Build Coastguard Workerclass If(Stmt):
316*635a8641SAndroid Build Coastguard Worker    """If `test` is true, `body` is rendered, else `else_`."""
317*635a8641SAndroid Build Coastguard Worker    fields = ('test', 'body', 'elif_', 'else_')
318*635a8641SAndroid Build Coastguard Worker
319*635a8641SAndroid Build Coastguard Worker
320*635a8641SAndroid Build Coastguard Workerclass Macro(Stmt):
321*635a8641SAndroid Build Coastguard Worker    """A macro definition.  `name` is the name of the macro, `args` a list of
322*635a8641SAndroid Build Coastguard Worker    arguments and `defaults` a list of defaults if there are any.  `body` is
323*635a8641SAndroid Build Coastguard Worker    a list of nodes for the macro body.
324*635a8641SAndroid Build Coastguard Worker    """
325*635a8641SAndroid Build Coastguard Worker    fields = ('name', 'args', 'defaults', 'body')
326*635a8641SAndroid Build Coastguard Worker
327*635a8641SAndroid Build Coastguard Worker
328*635a8641SAndroid Build Coastguard Workerclass CallBlock(Stmt):
329*635a8641SAndroid Build Coastguard Worker    """Like a macro without a name but a call instead.  `call` is called with
330*635a8641SAndroid Build Coastguard Worker    the unnamed macro as `caller` argument this node holds.
331*635a8641SAndroid Build Coastguard Worker    """
332*635a8641SAndroid Build Coastguard Worker    fields = ('call', 'args', 'defaults', 'body')
333*635a8641SAndroid Build Coastguard Worker
334*635a8641SAndroid Build Coastguard Worker
335*635a8641SAndroid Build Coastguard Workerclass FilterBlock(Stmt):
336*635a8641SAndroid Build Coastguard Worker    """Node for filter sections."""
337*635a8641SAndroid Build Coastguard Worker    fields = ('body', 'filter')
338*635a8641SAndroid Build Coastguard Worker
339*635a8641SAndroid Build Coastguard Worker
340*635a8641SAndroid Build Coastguard Workerclass With(Stmt):
341*635a8641SAndroid Build Coastguard Worker    """Specific node for with statements.  In older versions of Jinja the
342*635a8641SAndroid Build Coastguard Worker    with statement was implemented on the base of the `Scope` node instead.
343*635a8641SAndroid Build Coastguard Worker
344*635a8641SAndroid Build Coastguard Worker    .. versionadded:: 2.9.3
345*635a8641SAndroid Build Coastguard Worker    """
346*635a8641SAndroid Build Coastguard Worker    fields = ('targets', 'values', 'body')
347*635a8641SAndroid Build Coastguard Worker
348*635a8641SAndroid Build Coastguard Worker
349*635a8641SAndroid Build Coastguard Workerclass Block(Stmt):
350*635a8641SAndroid Build Coastguard Worker    """A node that represents a block."""
351*635a8641SAndroid Build Coastguard Worker    fields = ('name', 'body', 'scoped')
352*635a8641SAndroid Build Coastguard Worker
353*635a8641SAndroid Build Coastguard Worker
354*635a8641SAndroid Build Coastguard Workerclass Include(Stmt):
355*635a8641SAndroid Build Coastguard Worker    """A node that represents the include tag."""
356*635a8641SAndroid Build Coastguard Worker    fields = ('template', 'with_context', 'ignore_missing')
357*635a8641SAndroid Build Coastguard Worker
358*635a8641SAndroid Build Coastguard Worker
359*635a8641SAndroid Build Coastguard Workerclass Import(Stmt):
360*635a8641SAndroid Build Coastguard Worker    """A node that represents the import tag."""
361*635a8641SAndroid Build Coastguard Worker    fields = ('template', 'target', 'with_context')
362*635a8641SAndroid Build Coastguard Worker
363*635a8641SAndroid Build Coastguard Worker
364*635a8641SAndroid Build Coastguard Workerclass FromImport(Stmt):
365*635a8641SAndroid Build Coastguard Worker    """A node that represents the from import tag.  It's important to not
366*635a8641SAndroid Build Coastguard Worker    pass unsafe names to the name attribute.  The compiler translates the
367*635a8641SAndroid Build Coastguard Worker    attribute lookups directly into getattr calls and does *not* use the
368*635a8641SAndroid Build Coastguard Worker    subscript callback of the interface.  As exported variables may not
369*635a8641SAndroid Build Coastguard Worker    start with double underscores (which the parser asserts) this is not a
370*635a8641SAndroid Build Coastguard Worker    problem for regular Jinja code, but if this node is used in an extension
371*635a8641SAndroid Build Coastguard Worker    extra care must be taken.
372*635a8641SAndroid Build Coastguard Worker
373*635a8641SAndroid Build Coastguard Worker    The list of names may contain tuples if aliases are wanted.
374*635a8641SAndroid Build Coastguard Worker    """
375*635a8641SAndroid Build Coastguard Worker    fields = ('template', 'names', 'with_context')
376*635a8641SAndroid Build Coastguard Worker
377*635a8641SAndroid Build Coastguard Worker
378*635a8641SAndroid Build Coastguard Workerclass ExprStmt(Stmt):
379*635a8641SAndroid Build Coastguard Worker    """A statement that evaluates an expression and discards the result."""
380*635a8641SAndroid Build Coastguard Worker    fields = ('node',)
381*635a8641SAndroid Build Coastguard Worker
382*635a8641SAndroid Build Coastguard Worker
383*635a8641SAndroid Build Coastguard Workerclass Assign(Stmt):
384*635a8641SAndroid Build Coastguard Worker    """Assigns an expression to a target."""
385*635a8641SAndroid Build Coastguard Worker    fields = ('target', 'node')
386*635a8641SAndroid Build Coastguard Worker
387*635a8641SAndroid Build Coastguard Worker
388*635a8641SAndroid Build Coastguard Workerclass AssignBlock(Stmt):
389*635a8641SAndroid Build Coastguard Worker    """Assigns a block to a target."""
390*635a8641SAndroid Build Coastguard Worker    fields = ('target', 'filter', 'body')
391*635a8641SAndroid Build Coastguard Worker
392*635a8641SAndroid Build Coastguard Worker
393*635a8641SAndroid Build Coastguard Workerclass Expr(Node):
394*635a8641SAndroid Build Coastguard Worker    """Baseclass for all expressions."""
395*635a8641SAndroid Build Coastguard Worker    abstract = True
396*635a8641SAndroid Build Coastguard Worker
397*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
398*635a8641SAndroid Build Coastguard Worker        """Return the value of the expression as constant or raise
399*635a8641SAndroid Build Coastguard Worker        :exc:`Impossible` if this was not possible.
400*635a8641SAndroid Build Coastguard Worker
401*635a8641SAndroid Build Coastguard Worker        An :class:`EvalContext` can be provided, if none is given
402*635a8641SAndroid Build Coastguard Worker        a default context is created which requires the nodes to have
403*635a8641SAndroid Build Coastguard Worker        an attached environment.
404*635a8641SAndroid Build Coastguard Worker
405*635a8641SAndroid Build Coastguard Worker        .. versionchanged:: 2.4
406*635a8641SAndroid Build Coastguard Worker           the `eval_ctx` parameter was added.
407*635a8641SAndroid Build Coastguard Worker        """
408*635a8641SAndroid Build Coastguard Worker        raise Impossible()
409*635a8641SAndroid Build Coastguard Worker
410*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
411*635a8641SAndroid Build Coastguard Worker        """Check if it's possible to assign something to this node."""
412*635a8641SAndroid Build Coastguard Worker        return False
413*635a8641SAndroid Build Coastguard Worker
414*635a8641SAndroid Build Coastguard Worker
415*635a8641SAndroid Build Coastguard Workerclass BinExpr(Expr):
416*635a8641SAndroid Build Coastguard Worker    """Baseclass for all binary expressions."""
417*635a8641SAndroid Build Coastguard Worker    fields = ('left', 'right')
418*635a8641SAndroid Build Coastguard Worker    operator = None
419*635a8641SAndroid Build Coastguard Worker    abstract = True
420*635a8641SAndroid Build Coastguard Worker
421*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
422*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
423*635a8641SAndroid Build Coastguard Worker        # intercepted operators cannot be folded at compile time
424*635a8641SAndroid Build Coastguard Worker        if self.environment.sandboxed and \
425*635a8641SAndroid Build Coastguard Worker           self.operator in self.environment.intercepted_binops:
426*635a8641SAndroid Build Coastguard Worker            raise Impossible()
427*635a8641SAndroid Build Coastguard Worker        f = _binop_to_func[self.operator]
428*635a8641SAndroid Build Coastguard Worker        try:
429*635a8641SAndroid Build Coastguard Worker            return f(self.left.as_const(eval_ctx), self.right.as_const(eval_ctx))
430*635a8641SAndroid Build Coastguard Worker        except Exception:
431*635a8641SAndroid Build Coastguard Worker            raise Impossible()
432*635a8641SAndroid Build Coastguard Worker
433*635a8641SAndroid Build Coastguard Worker
434*635a8641SAndroid Build Coastguard Workerclass UnaryExpr(Expr):
435*635a8641SAndroid Build Coastguard Worker    """Baseclass for all unary expressions."""
436*635a8641SAndroid Build Coastguard Worker    fields = ('node',)
437*635a8641SAndroid Build Coastguard Worker    operator = None
438*635a8641SAndroid Build Coastguard Worker    abstract = True
439*635a8641SAndroid Build Coastguard Worker
440*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
441*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
442*635a8641SAndroid Build Coastguard Worker        # intercepted operators cannot be folded at compile time
443*635a8641SAndroid Build Coastguard Worker        if self.environment.sandboxed and \
444*635a8641SAndroid Build Coastguard Worker           self.operator in self.environment.intercepted_unops:
445*635a8641SAndroid Build Coastguard Worker            raise Impossible()
446*635a8641SAndroid Build Coastguard Worker        f = _uaop_to_func[self.operator]
447*635a8641SAndroid Build Coastguard Worker        try:
448*635a8641SAndroid Build Coastguard Worker            return f(self.node.as_const(eval_ctx))
449*635a8641SAndroid Build Coastguard Worker        except Exception:
450*635a8641SAndroid Build Coastguard Worker            raise Impossible()
451*635a8641SAndroid Build Coastguard Worker
452*635a8641SAndroid Build Coastguard Worker
453*635a8641SAndroid Build Coastguard Workerclass Name(Expr):
454*635a8641SAndroid Build Coastguard Worker    """Looks up a name or stores a value in a name.
455*635a8641SAndroid Build Coastguard Worker    The `ctx` of the node can be one of the following values:
456*635a8641SAndroid Build Coastguard Worker
457*635a8641SAndroid Build Coastguard Worker    -   `store`: store a value in the name
458*635a8641SAndroid Build Coastguard Worker    -   `load`: load that name
459*635a8641SAndroid Build Coastguard Worker    -   `param`: like `store` but if the name was defined as function parameter.
460*635a8641SAndroid Build Coastguard Worker    """
461*635a8641SAndroid Build Coastguard Worker    fields = ('name', 'ctx')
462*635a8641SAndroid Build Coastguard Worker
463*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
464*635a8641SAndroid Build Coastguard Worker        return self.name not in ('true', 'false', 'none',
465*635a8641SAndroid Build Coastguard Worker                                 'True', 'False', 'None')
466*635a8641SAndroid Build Coastguard Worker
467*635a8641SAndroid Build Coastguard Worker
468*635a8641SAndroid Build Coastguard Workerclass NSRef(Expr):
469*635a8641SAndroid Build Coastguard Worker    """Reference to a namespace value assignment"""
470*635a8641SAndroid Build Coastguard Worker    fields = ('name', 'attr')
471*635a8641SAndroid Build Coastguard Worker
472*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
473*635a8641SAndroid Build Coastguard Worker        # We don't need any special checks here; NSRef assignments have a
474*635a8641SAndroid Build Coastguard Worker        # runtime check to ensure the target is a namespace object which will
475*635a8641SAndroid Build Coastguard Worker        # have been checked already as it is created using a normal assignment
476*635a8641SAndroid Build Coastguard Worker        # which goes through a `Name` node.
477*635a8641SAndroid Build Coastguard Worker        return True
478*635a8641SAndroid Build Coastguard Worker
479*635a8641SAndroid Build Coastguard Worker
480*635a8641SAndroid Build Coastguard Workerclass Literal(Expr):
481*635a8641SAndroid Build Coastguard Worker    """Baseclass for literals."""
482*635a8641SAndroid Build Coastguard Worker    abstract = True
483*635a8641SAndroid Build Coastguard Worker
484*635a8641SAndroid Build Coastguard Worker
485*635a8641SAndroid Build Coastguard Workerclass Const(Literal):
486*635a8641SAndroid Build Coastguard Worker    """All constant values.  The parser will return this node for simple
487*635a8641SAndroid Build Coastguard Worker    constants such as ``42`` or ``"foo"`` but it can be used to store more
488*635a8641SAndroid Build Coastguard Worker    complex values such as lists too.  Only constants with a safe
489*635a8641SAndroid Build Coastguard Worker    representation (objects where ``eval(repr(x)) == x`` is true).
490*635a8641SAndroid Build Coastguard Worker    """
491*635a8641SAndroid Build Coastguard Worker    fields = ('value',)
492*635a8641SAndroid Build Coastguard Worker
493*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
494*635a8641SAndroid Build Coastguard Worker        rv = self.value
495*635a8641SAndroid Build Coastguard Worker        if PY2 and type(rv) is text_type and \
496*635a8641SAndroid Build Coastguard Worker           self.environment.policies['compiler.ascii_str']:
497*635a8641SAndroid Build Coastguard Worker            try:
498*635a8641SAndroid Build Coastguard Worker                rv = rv.encode('ascii')
499*635a8641SAndroid Build Coastguard Worker            except UnicodeError:
500*635a8641SAndroid Build Coastguard Worker                pass
501*635a8641SAndroid Build Coastguard Worker        return rv
502*635a8641SAndroid Build Coastguard Worker
503*635a8641SAndroid Build Coastguard Worker    @classmethod
504*635a8641SAndroid Build Coastguard Worker    def from_untrusted(cls, value, lineno=None, environment=None):
505*635a8641SAndroid Build Coastguard Worker        """Return a const object if the value is representable as
506*635a8641SAndroid Build Coastguard Worker        constant value in the generated code, otherwise it will raise
507*635a8641SAndroid Build Coastguard Worker        an `Impossible` exception.
508*635a8641SAndroid Build Coastguard Worker        """
509*635a8641SAndroid Build Coastguard Worker        from .compiler import has_safe_repr
510*635a8641SAndroid Build Coastguard Worker        if not has_safe_repr(value):
511*635a8641SAndroid Build Coastguard Worker            raise Impossible()
512*635a8641SAndroid Build Coastguard Worker        return cls(value, lineno=lineno, environment=environment)
513*635a8641SAndroid Build Coastguard Worker
514*635a8641SAndroid Build Coastguard Worker
515*635a8641SAndroid Build Coastguard Workerclass TemplateData(Literal):
516*635a8641SAndroid Build Coastguard Worker    """A constant template string."""
517*635a8641SAndroid Build Coastguard Worker    fields = ('data',)
518*635a8641SAndroid Build Coastguard Worker
519*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
520*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
521*635a8641SAndroid Build Coastguard Worker        if eval_ctx.volatile:
522*635a8641SAndroid Build Coastguard Worker            raise Impossible()
523*635a8641SAndroid Build Coastguard Worker        if eval_ctx.autoescape:
524*635a8641SAndroid Build Coastguard Worker            return Markup(self.data)
525*635a8641SAndroid Build Coastguard Worker        return self.data
526*635a8641SAndroid Build Coastguard Worker
527*635a8641SAndroid Build Coastguard Worker
528*635a8641SAndroid Build Coastguard Workerclass Tuple(Literal):
529*635a8641SAndroid Build Coastguard Worker    """For loop unpacking and some other things like multiple arguments
530*635a8641SAndroid Build Coastguard Worker    for subscripts.  Like for :class:`Name` `ctx` specifies if the tuple
531*635a8641SAndroid Build Coastguard Worker    is used for loading the names or storing.
532*635a8641SAndroid Build Coastguard Worker    """
533*635a8641SAndroid Build Coastguard Worker    fields = ('items', 'ctx')
534*635a8641SAndroid Build Coastguard Worker
535*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
536*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
537*635a8641SAndroid Build Coastguard Worker        return tuple(x.as_const(eval_ctx) for x in self.items)
538*635a8641SAndroid Build Coastguard Worker
539*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
540*635a8641SAndroid Build Coastguard Worker        for item in self.items:
541*635a8641SAndroid Build Coastguard Worker            if not item.can_assign():
542*635a8641SAndroid Build Coastguard Worker                return False
543*635a8641SAndroid Build Coastguard Worker        return True
544*635a8641SAndroid Build Coastguard Worker
545*635a8641SAndroid Build Coastguard Worker
546*635a8641SAndroid Build Coastguard Workerclass List(Literal):
547*635a8641SAndroid Build Coastguard Worker    """Any list literal such as ``[1, 2, 3]``"""
548*635a8641SAndroid Build Coastguard Worker    fields = ('items',)
549*635a8641SAndroid Build Coastguard Worker
550*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
551*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
552*635a8641SAndroid Build Coastguard Worker        return [x.as_const(eval_ctx) for x in self.items]
553*635a8641SAndroid Build Coastguard Worker
554*635a8641SAndroid Build Coastguard Worker
555*635a8641SAndroid Build Coastguard Workerclass Dict(Literal):
556*635a8641SAndroid Build Coastguard Worker    """Any dict literal such as ``{1: 2, 3: 4}``.  The items must be a list of
557*635a8641SAndroid Build Coastguard Worker    :class:`Pair` nodes.
558*635a8641SAndroid Build Coastguard Worker    """
559*635a8641SAndroid Build Coastguard Worker    fields = ('items',)
560*635a8641SAndroid Build Coastguard Worker
561*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
562*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
563*635a8641SAndroid Build Coastguard Worker        return dict(x.as_const(eval_ctx) for x in self.items)
564*635a8641SAndroid Build Coastguard Worker
565*635a8641SAndroid Build Coastguard Worker
566*635a8641SAndroid Build Coastguard Workerclass Pair(Helper):
567*635a8641SAndroid Build Coastguard Worker    """A key, value pair for dicts."""
568*635a8641SAndroid Build Coastguard Worker    fields = ('key', 'value')
569*635a8641SAndroid Build Coastguard Worker
570*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
571*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
572*635a8641SAndroid Build Coastguard Worker        return self.key.as_const(eval_ctx), self.value.as_const(eval_ctx)
573*635a8641SAndroid Build Coastguard Worker
574*635a8641SAndroid Build Coastguard Worker
575*635a8641SAndroid Build Coastguard Workerclass Keyword(Helper):
576*635a8641SAndroid Build Coastguard Worker    """A key, value pair for keyword arguments where key is a string."""
577*635a8641SAndroid Build Coastguard Worker    fields = ('key', 'value')
578*635a8641SAndroid Build Coastguard Worker
579*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
580*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
581*635a8641SAndroid Build Coastguard Worker        return self.key, self.value.as_const(eval_ctx)
582*635a8641SAndroid Build Coastguard Worker
583*635a8641SAndroid Build Coastguard Worker
584*635a8641SAndroid Build Coastguard Workerclass CondExpr(Expr):
585*635a8641SAndroid Build Coastguard Worker    """A conditional expression (inline if expression).  (``{{
586*635a8641SAndroid Build Coastguard Worker    foo if bar else baz }}``)
587*635a8641SAndroid Build Coastguard Worker    """
588*635a8641SAndroid Build Coastguard Worker    fields = ('test', 'expr1', 'expr2')
589*635a8641SAndroid Build Coastguard Worker
590*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
591*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
592*635a8641SAndroid Build Coastguard Worker        if self.test.as_const(eval_ctx):
593*635a8641SAndroid Build Coastguard Worker            return self.expr1.as_const(eval_ctx)
594*635a8641SAndroid Build Coastguard Worker
595*635a8641SAndroid Build Coastguard Worker        # if we evaluate to an undefined object, we better do that at runtime
596*635a8641SAndroid Build Coastguard Worker        if self.expr2 is None:
597*635a8641SAndroid Build Coastguard Worker            raise Impossible()
598*635a8641SAndroid Build Coastguard Worker
599*635a8641SAndroid Build Coastguard Worker        return self.expr2.as_const(eval_ctx)
600*635a8641SAndroid Build Coastguard Worker
601*635a8641SAndroid Build Coastguard Worker
602*635a8641SAndroid Build Coastguard Workerdef args_as_const(node, eval_ctx):
603*635a8641SAndroid Build Coastguard Worker    args = [x.as_const(eval_ctx) for x in node.args]
604*635a8641SAndroid Build Coastguard Worker    kwargs = dict(x.as_const(eval_ctx) for x in node.kwargs)
605*635a8641SAndroid Build Coastguard Worker
606*635a8641SAndroid Build Coastguard Worker    if node.dyn_args is not None:
607*635a8641SAndroid Build Coastguard Worker        try:
608*635a8641SAndroid Build Coastguard Worker            args.extend(node.dyn_args.as_const(eval_ctx))
609*635a8641SAndroid Build Coastguard Worker        except Exception:
610*635a8641SAndroid Build Coastguard Worker            raise Impossible()
611*635a8641SAndroid Build Coastguard Worker
612*635a8641SAndroid Build Coastguard Worker    if node.dyn_kwargs is not None:
613*635a8641SAndroid Build Coastguard Worker        try:
614*635a8641SAndroid Build Coastguard Worker            kwargs.update(node.dyn_kwargs.as_const(eval_ctx))
615*635a8641SAndroid Build Coastguard Worker        except Exception:
616*635a8641SAndroid Build Coastguard Worker            raise Impossible()
617*635a8641SAndroid Build Coastguard Worker
618*635a8641SAndroid Build Coastguard Worker    return args, kwargs
619*635a8641SAndroid Build Coastguard Worker
620*635a8641SAndroid Build Coastguard Worker
621*635a8641SAndroid Build Coastguard Workerclass Filter(Expr):
622*635a8641SAndroid Build Coastguard Worker    """This node applies a filter on an expression.  `name` is the name of
623*635a8641SAndroid Build Coastguard Worker    the filter, the rest of the fields are the same as for :class:`Call`.
624*635a8641SAndroid Build Coastguard Worker
625*635a8641SAndroid Build Coastguard Worker    If the `node` of a filter is `None` the contents of the last buffer are
626*635a8641SAndroid Build Coastguard Worker    filtered.  Buffers are created by macros and filter blocks.
627*635a8641SAndroid Build Coastguard Worker    """
628*635a8641SAndroid Build Coastguard Worker
629*635a8641SAndroid Build Coastguard Worker    fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
630*635a8641SAndroid Build Coastguard Worker
631*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
632*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
633*635a8641SAndroid Build Coastguard Worker
634*635a8641SAndroid Build Coastguard Worker        if eval_ctx.volatile or self.node is None:
635*635a8641SAndroid Build Coastguard Worker            raise Impossible()
636*635a8641SAndroid Build Coastguard Worker
637*635a8641SAndroid Build Coastguard Worker        # we have to be careful here because we call filter_ below.
638*635a8641SAndroid Build Coastguard Worker        # if this variable would be called filter, 2to3 would wrap the
639*635a8641SAndroid Build Coastguard Worker        # call in a list beause it is assuming we are talking about the
640*635a8641SAndroid Build Coastguard Worker        # builtin filter function here which no longer returns a list in
641*635a8641SAndroid Build Coastguard Worker        # python 3.  because of that, do not rename filter_ to filter!
642*635a8641SAndroid Build Coastguard Worker        filter_ = self.environment.filters.get(self.name)
643*635a8641SAndroid Build Coastguard Worker
644*635a8641SAndroid Build Coastguard Worker        if filter_ is None or getattr(filter_, 'contextfilter', False):
645*635a8641SAndroid Build Coastguard Worker            raise Impossible()
646*635a8641SAndroid Build Coastguard Worker
647*635a8641SAndroid Build Coastguard Worker        # We cannot constant handle async filters, so we need to make sure
648*635a8641SAndroid Build Coastguard Worker        # to not go down this path.
649*635a8641SAndroid Build Coastguard Worker        if (
650*635a8641SAndroid Build Coastguard Worker            eval_ctx.environment.is_async
651*635a8641SAndroid Build Coastguard Worker            and getattr(filter_, 'asyncfiltervariant', False)
652*635a8641SAndroid Build Coastguard Worker        ):
653*635a8641SAndroid Build Coastguard Worker            raise Impossible()
654*635a8641SAndroid Build Coastguard Worker
655*635a8641SAndroid Build Coastguard Worker        args, kwargs = args_as_const(self, eval_ctx)
656*635a8641SAndroid Build Coastguard Worker        args.insert(0, self.node.as_const(eval_ctx))
657*635a8641SAndroid Build Coastguard Worker
658*635a8641SAndroid Build Coastguard Worker        if getattr(filter_, 'evalcontextfilter', False):
659*635a8641SAndroid Build Coastguard Worker            args.insert(0, eval_ctx)
660*635a8641SAndroid Build Coastguard Worker        elif getattr(filter_, 'environmentfilter', False):
661*635a8641SAndroid Build Coastguard Worker            args.insert(0, self.environment)
662*635a8641SAndroid Build Coastguard Worker
663*635a8641SAndroid Build Coastguard Worker        try:
664*635a8641SAndroid Build Coastguard Worker            return filter_(*args, **kwargs)
665*635a8641SAndroid Build Coastguard Worker        except Exception:
666*635a8641SAndroid Build Coastguard Worker            raise Impossible()
667*635a8641SAndroid Build Coastguard Worker
668*635a8641SAndroid Build Coastguard Worker
669*635a8641SAndroid Build Coastguard Workerclass Test(Expr):
670*635a8641SAndroid Build Coastguard Worker    """Applies a test on an expression.  `name` is the name of the test, the
671*635a8641SAndroid Build Coastguard Worker    rest of the fields are the same as for :class:`Call`.
672*635a8641SAndroid Build Coastguard Worker    """
673*635a8641SAndroid Build Coastguard Worker
674*635a8641SAndroid Build Coastguard Worker    fields = ('node', 'name', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
675*635a8641SAndroid Build Coastguard Worker
676*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
677*635a8641SAndroid Build Coastguard Worker        test = self.environment.tests.get(self.name)
678*635a8641SAndroid Build Coastguard Worker
679*635a8641SAndroid Build Coastguard Worker        if test is None:
680*635a8641SAndroid Build Coastguard Worker            raise Impossible()
681*635a8641SAndroid Build Coastguard Worker
682*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
683*635a8641SAndroid Build Coastguard Worker        args, kwargs = args_as_const(self, eval_ctx)
684*635a8641SAndroid Build Coastguard Worker        args.insert(0, self.node.as_const(eval_ctx))
685*635a8641SAndroid Build Coastguard Worker
686*635a8641SAndroid Build Coastguard Worker        try:
687*635a8641SAndroid Build Coastguard Worker            return test(*args, **kwargs)
688*635a8641SAndroid Build Coastguard Worker        except Exception:
689*635a8641SAndroid Build Coastguard Worker            raise Impossible()
690*635a8641SAndroid Build Coastguard Worker
691*635a8641SAndroid Build Coastguard Worker
692*635a8641SAndroid Build Coastguard Workerclass Call(Expr):
693*635a8641SAndroid Build Coastguard Worker    """Calls an expression.  `args` is a list of arguments, `kwargs` a list
694*635a8641SAndroid Build Coastguard Worker    of keyword arguments (list of :class:`Keyword` nodes), and `dyn_args`
695*635a8641SAndroid Build Coastguard Worker    and `dyn_kwargs` has to be either `None` or a node that is used as
696*635a8641SAndroid Build Coastguard Worker    node for dynamic positional (``*args``) or keyword (``**kwargs``)
697*635a8641SAndroid Build Coastguard Worker    arguments.
698*635a8641SAndroid Build Coastguard Worker    """
699*635a8641SAndroid Build Coastguard Worker    fields = ('node', 'args', 'kwargs', 'dyn_args', 'dyn_kwargs')
700*635a8641SAndroid Build Coastguard Worker
701*635a8641SAndroid Build Coastguard Worker
702*635a8641SAndroid Build Coastguard Workerclass Getitem(Expr):
703*635a8641SAndroid Build Coastguard Worker    """Get an attribute or item from an expression and prefer the item."""
704*635a8641SAndroid Build Coastguard Worker    fields = ('node', 'arg', 'ctx')
705*635a8641SAndroid Build Coastguard Worker
706*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
707*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
708*635a8641SAndroid Build Coastguard Worker        if self.ctx != 'load':
709*635a8641SAndroid Build Coastguard Worker            raise Impossible()
710*635a8641SAndroid Build Coastguard Worker        try:
711*635a8641SAndroid Build Coastguard Worker            return self.environment.getitem(self.node.as_const(eval_ctx),
712*635a8641SAndroid Build Coastguard Worker                                            self.arg.as_const(eval_ctx))
713*635a8641SAndroid Build Coastguard Worker        except Exception:
714*635a8641SAndroid Build Coastguard Worker            raise Impossible()
715*635a8641SAndroid Build Coastguard Worker
716*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
717*635a8641SAndroid Build Coastguard Worker        return False
718*635a8641SAndroid Build Coastguard Worker
719*635a8641SAndroid Build Coastguard Worker
720*635a8641SAndroid Build Coastguard Workerclass Getattr(Expr):
721*635a8641SAndroid Build Coastguard Worker    """Get an attribute or item from an expression that is a ascii-only
722*635a8641SAndroid Build Coastguard Worker    bytestring and prefer the attribute.
723*635a8641SAndroid Build Coastguard Worker    """
724*635a8641SAndroid Build Coastguard Worker    fields = ('node', 'attr', 'ctx')
725*635a8641SAndroid Build Coastguard Worker
726*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
727*635a8641SAndroid Build Coastguard Worker        if self.ctx != 'load':
728*635a8641SAndroid Build Coastguard Worker            raise Impossible()
729*635a8641SAndroid Build Coastguard Worker        try:
730*635a8641SAndroid Build Coastguard Worker            eval_ctx = get_eval_context(self, eval_ctx)
731*635a8641SAndroid Build Coastguard Worker            return self.environment.getattr(self.node.as_const(eval_ctx),
732*635a8641SAndroid Build Coastguard Worker                                            self.attr)
733*635a8641SAndroid Build Coastguard Worker        except Exception:
734*635a8641SAndroid Build Coastguard Worker            raise Impossible()
735*635a8641SAndroid Build Coastguard Worker
736*635a8641SAndroid Build Coastguard Worker    def can_assign(self):
737*635a8641SAndroid Build Coastguard Worker        return False
738*635a8641SAndroid Build Coastguard Worker
739*635a8641SAndroid Build Coastguard Worker
740*635a8641SAndroid Build Coastguard Workerclass Slice(Expr):
741*635a8641SAndroid Build Coastguard Worker    """Represents a slice object.  This must only be used as argument for
742*635a8641SAndroid Build Coastguard Worker    :class:`Subscript`.
743*635a8641SAndroid Build Coastguard Worker    """
744*635a8641SAndroid Build Coastguard Worker    fields = ('start', 'stop', 'step')
745*635a8641SAndroid Build Coastguard Worker
746*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
747*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
748*635a8641SAndroid Build Coastguard Worker        def const(obj):
749*635a8641SAndroid Build Coastguard Worker            if obj is None:
750*635a8641SAndroid Build Coastguard Worker                return None
751*635a8641SAndroid Build Coastguard Worker            return obj.as_const(eval_ctx)
752*635a8641SAndroid Build Coastguard Worker        return slice(const(self.start), const(self.stop), const(self.step))
753*635a8641SAndroid Build Coastguard Worker
754*635a8641SAndroid Build Coastguard Worker
755*635a8641SAndroid Build Coastguard Workerclass Concat(Expr):
756*635a8641SAndroid Build Coastguard Worker    """Concatenates the list of expressions provided after converting them to
757*635a8641SAndroid Build Coastguard Worker    unicode.
758*635a8641SAndroid Build Coastguard Worker    """
759*635a8641SAndroid Build Coastguard Worker    fields = ('nodes',)
760*635a8641SAndroid Build Coastguard Worker
761*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
762*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
763*635a8641SAndroid Build Coastguard Worker        return ''.join(text_type(x.as_const(eval_ctx)) for x in self.nodes)
764*635a8641SAndroid Build Coastguard Worker
765*635a8641SAndroid Build Coastguard Worker
766*635a8641SAndroid Build Coastguard Workerclass Compare(Expr):
767*635a8641SAndroid Build Coastguard Worker    """Compares an expression with some other expressions.  `ops` must be a
768*635a8641SAndroid Build Coastguard Worker    list of :class:`Operand`\\s.
769*635a8641SAndroid Build Coastguard Worker    """
770*635a8641SAndroid Build Coastguard Worker    fields = ('expr', 'ops')
771*635a8641SAndroid Build Coastguard Worker
772*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
773*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
774*635a8641SAndroid Build Coastguard Worker        result = value = self.expr.as_const(eval_ctx)
775*635a8641SAndroid Build Coastguard Worker        try:
776*635a8641SAndroid Build Coastguard Worker            for op in self.ops:
777*635a8641SAndroid Build Coastguard Worker                new_value = op.expr.as_const(eval_ctx)
778*635a8641SAndroid Build Coastguard Worker                result = _cmpop_to_func[op.op](value, new_value)
779*635a8641SAndroid Build Coastguard Worker                value = new_value
780*635a8641SAndroid Build Coastguard Worker        except Exception:
781*635a8641SAndroid Build Coastguard Worker            raise Impossible()
782*635a8641SAndroid Build Coastguard Worker        return result
783*635a8641SAndroid Build Coastguard Worker
784*635a8641SAndroid Build Coastguard Worker
785*635a8641SAndroid Build Coastguard Workerclass Operand(Helper):
786*635a8641SAndroid Build Coastguard Worker    """Holds an operator and an expression."""
787*635a8641SAndroid Build Coastguard Worker    fields = ('op', 'expr')
788*635a8641SAndroid Build Coastguard Worker
789*635a8641SAndroid Build Coastguard Workerif __debug__:
790*635a8641SAndroid Build Coastguard Worker    Operand.__doc__ += '\nThe following operators are available: ' + \
791*635a8641SAndroid Build Coastguard Worker        ', '.join(sorted('``%s``' % x for x in set(_binop_to_func) |
792*635a8641SAndroid Build Coastguard Worker                  set(_uaop_to_func) | set(_cmpop_to_func)))
793*635a8641SAndroid Build Coastguard Worker
794*635a8641SAndroid Build Coastguard Worker
795*635a8641SAndroid Build Coastguard Workerclass Mul(BinExpr):
796*635a8641SAndroid Build Coastguard Worker    """Multiplies the left with the right node."""
797*635a8641SAndroid Build Coastguard Worker    operator = '*'
798*635a8641SAndroid Build Coastguard Worker
799*635a8641SAndroid Build Coastguard Worker
800*635a8641SAndroid Build Coastguard Workerclass Div(BinExpr):
801*635a8641SAndroid Build Coastguard Worker    """Divides the left by the right node."""
802*635a8641SAndroid Build Coastguard Worker    operator = '/'
803*635a8641SAndroid Build Coastguard Worker
804*635a8641SAndroid Build Coastguard Worker
805*635a8641SAndroid Build Coastguard Workerclass FloorDiv(BinExpr):
806*635a8641SAndroid Build Coastguard Worker    """Divides the left by the right node and truncates conver the
807*635a8641SAndroid Build Coastguard Worker    result into an integer by truncating.
808*635a8641SAndroid Build Coastguard Worker    """
809*635a8641SAndroid Build Coastguard Worker    operator = '//'
810*635a8641SAndroid Build Coastguard Worker
811*635a8641SAndroid Build Coastguard Worker
812*635a8641SAndroid Build Coastguard Workerclass Add(BinExpr):
813*635a8641SAndroid Build Coastguard Worker    """Add the left to the right node."""
814*635a8641SAndroid Build Coastguard Worker    operator = '+'
815*635a8641SAndroid Build Coastguard Worker
816*635a8641SAndroid Build Coastguard Worker
817*635a8641SAndroid Build Coastguard Workerclass Sub(BinExpr):
818*635a8641SAndroid Build Coastguard Worker    """Subtract the right from the left node."""
819*635a8641SAndroid Build Coastguard Worker    operator = '-'
820*635a8641SAndroid Build Coastguard Worker
821*635a8641SAndroid Build Coastguard Worker
822*635a8641SAndroid Build Coastguard Workerclass Mod(BinExpr):
823*635a8641SAndroid Build Coastguard Worker    """Left modulo right."""
824*635a8641SAndroid Build Coastguard Worker    operator = '%'
825*635a8641SAndroid Build Coastguard Worker
826*635a8641SAndroid Build Coastguard Worker
827*635a8641SAndroid Build Coastguard Workerclass Pow(BinExpr):
828*635a8641SAndroid Build Coastguard Worker    """Left to the power of right."""
829*635a8641SAndroid Build Coastguard Worker    operator = '**'
830*635a8641SAndroid Build Coastguard Worker
831*635a8641SAndroid Build Coastguard Worker
832*635a8641SAndroid Build Coastguard Workerclass And(BinExpr):
833*635a8641SAndroid Build Coastguard Worker    """Short circuited AND."""
834*635a8641SAndroid Build Coastguard Worker    operator = 'and'
835*635a8641SAndroid Build Coastguard Worker
836*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
837*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
838*635a8641SAndroid Build Coastguard Worker        return self.left.as_const(eval_ctx) and self.right.as_const(eval_ctx)
839*635a8641SAndroid Build Coastguard Worker
840*635a8641SAndroid Build Coastguard Worker
841*635a8641SAndroid Build Coastguard Workerclass Or(BinExpr):
842*635a8641SAndroid Build Coastguard Worker    """Short circuited OR."""
843*635a8641SAndroid Build Coastguard Worker    operator = 'or'
844*635a8641SAndroid Build Coastguard Worker
845*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
846*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
847*635a8641SAndroid Build Coastguard Worker        return self.left.as_const(eval_ctx) or self.right.as_const(eval_ctx)
848*635a8641SAndroid Build Coastguard Worker
849*635a8641SAndroid Build Coastguard Worker
850*635a8641SAndroid Build Coastguard Workerclass Not(UnaryExpr):
851*635a8641SAndroid Build Coastguard Worker    """Negate the expression."""
852*635a8641SAndroid Build Coastguard Worker    operator = 'not'
853*635a8641SAndroid Build Coastguard Worker
854*635a8641SAndroid Build Coastguard Worker
855*635a8641SAndroid Build Coastguard Workerclass Neg(UnaryExpr):
856*635a8641SAndroid Build Coastguard Worker    """Make the expression negative."""
857*635a8641SAndroid Build Coastguard Worker    operator = '-'
858*635a8641SAndroid Build Coastguard Worker
859*635a8641SAndroid Build Coastguard Worker
860*635a8641SAndroid Build Coastguard Workerclass Pos(UnaryExpr):
861*635a8641SAndroid Build Coastguard Worker    """Make the expression positive (noop for most expressions)"""
862*635a8641SAndroid Build Coastguard Worker    operator = '+'
863*635a8641SAndroid Build Coastguard Worker
864*635a8641SAndroid Build Coastguard Worker
865*635a8641SAndroid Build Coastguard Worker# Helpers for extensions
866*635a8641SAndroid Build Coastguard Worker
867*635a8641SAndroid Build Coastguard Worker
868*635a8641SAndroid Build Coastguard Workerclass EnvironmentAttribute(Expr):
869*635a8641SAndroid Build Coastguard Worker    """Loads an attribute from the environment object.  This is useful for
870*635a8641SAndroid Build Coastguard Worker    extensions that want to call a callback stored on the environment.
871*635a8641SAndroid Build Coastguard Worker    """
872*635a8641SAndroid Build Coastguard Worker    fields = ('name',)
873*635a8641SAndroid Build Coastguard Worker
874*635a8641SAndroid Build Coastguard Worker
875*635a8641SAndroid Build Coastguard Workerclass ExtensionAttribute(Expr):
876*635a8641SAndroid Build Coastguard Worker    """Returns the attribute of an extension bound to the environment.
877*635a8641SAndroid Build Coastguard Worker    The identifier is the identifier of the :class:`Extension`.
878*635a8641SAndroid Build Coastguard Worker
879*635a8641SAndroid Build Coastguard Worker    This node is usually constructed by calling the
880*635a8641SAndroid Build Coastguard Worker    :meth:`~jinja2.ext.Extension.attr` method on an extension.
881*635a8641SAndroid Build Coastguard Worker    """
882*635a8641SAndroid Build Coastguard Worker    fields = ('identifier', 'name')
883*635a8641SAndroid Build Coastguard Worker
884*635a8641SAndroid Build Coastguard Worker
885*635a8641SAndroid Build Coastguard Workerclass ImportedName(Expr):
886*635a8641SAndroid Build Coastguard Worker    """If created with an import name the import name is returned on node
887*635a8641SAndroid Build Coastguard Worker    access.  For example ``ImportedName('cgi.escape')`` returns the `escape`
888*635a8641SAndroid Build Coastguard Worker    function from the cgi module on evaluation.  Imports are optimized by the
889*635a8641SAndroid Build Coastguard Worker    compiler so there is no need to assign them to local variables.
890*635a8641SAndroid Build Coastguard Worker    """
891*635a8641SAndroid Build Coastguard Worker    fields = ('importname',)
892*635a8641SAndroid Build Coastguard Worker
893*635a8641SAndroid Build Coastguard Worker
894*635a8641SAndroid Build Coastguard Workerclass InternalName(Expr):
895*635a8641SAndroid Build Coastguard Worker    """An internal name in the compiler.  You cannot create these nodes
896*635a8641SAndroid Build Coastguard Worker    yourself but the parser provides a
897*635a8641SAndroid Build Coastguard Worker    :meth:`~jinja2.parser.Parser.free_identifier` method that creates
898*635a8641SAndroid Build Coastguard Worker    a new identifier for you.  This identifier is not available from the
899*635a8641SAndroid Build Coastguard Worker    template and is not threated specially by the compiler.
900*635a8641SAndroid Build Coastguard Worker    """
901*635a8641SAndroid Build Coastguard Worker    fields = ('name',)
902*635a8641SAndroid Build Coastguard Worker
903*635a8641SAndroid Build Coastguard Worker    def __init__(self):
904*635a8641SAndroid Build Coastguard Worker        raise TypeError('Can\'t create internal names.  Use the '
905*635a8641SAndroid Build Coastguard Worker                        '`free_identifier` method on a parser.')
906*635a8641SAndroid Build Coastguard Worker
907*635a8641SAndroid Build Coastguard Worker
908*635a8641SAndroid Build Coastguard Workerclass MarkSafe(Expr):
909*635a8641SAndroid Build Coastguard Worker    """Mark the wrapped expression as safe (wrap it as `Markup`)."""
910*635a8641SAndroid Build Coastguard Worker    fields = ('expr',)
911*635a8641SAndroid Build Coastguard Worker
912*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
913*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
914*635a8641SAndroid Build Coastguard Worker        return Markup(self.expr.as_const(eval_ctx))
915*635a8641SAndroid Build Coastguard Worker
916*635a8641SAndroid Build Coastguard Worker
917*635a8641SAndroid Build Coastguard Workerclass MarkSafeIfAutoescape(Expr):
918*635a8641SAndroid Build Coastguard Worker    """Mark the wrapped expression as safe (wrap it as `Markup`) but
919*635a8641SAndroid Build Coastguard Worker    only if autoescaping is active.
920*635a8641SAndroid Build Coastguard Worker
921*635a8641SAndroid Build Coastguard Worker    .. versionadded:: 2.5
922*635a8641SAndroid Build Coastguard Worker    """
923*635a8641SAndroid Build Coastguard Worker    fields = ('expr',)
924*635a8641SAndroid Build Coastguard Worker
925*635a8641SAndroid Build Coastguard Worker    def as_const(self, eval_ctx=None):
926*635a8641SAndroid Build Coastguard Worker        eval_ctx = get_eval_context(self, eval_ctx)
927*635a8641SAndroid Build Coastguard Worker        if eval_ctx.volatile:
928*635a8641SAndroid Build Coastguard Worker            raise Impossible()
929*635a8641SAndroid Build Coastguard Worker        expr = self.expr.as_const(eval_ctx)
930*635a8641SAndroid Build Coastguard Worker        if eval_ctx.autoescape:
931*635a8641SAndroid Build Coastguard Worker            return Markup(expr)
932*635a8641SAndroid Build Coastguard Worker        return expr
933*635a8641SAndroid Build Coastguard Worker
934*635a8641SAndroid Build Coastguard Worker
935*635a8641SAndroid Build Coastguard Workerclass ContextReference(Expr):
936*635a8641SAndroid Build Coastguard Worker    """Returns the current template context.  It can be used like a
937*635a8641SAndroid Build Coastguard Worker    :class:`Name` node, with a ``'load'`` ctx and will return the
938*635a8641SAndroid Build Coastguard Worker    current :class:`~jinja2.runtime.Context` object.
939*635a8641SAndroid Build Coastguard Worker
940*635a8641SAndroid Build Coastguard Worker    Here an example that assigns the current template name to a
941*635a8641SAndroid Build Coastguard Worker    variable named `foo`::
942*635a8641SAndroid Build Coastguard Worker
943*635a8641SAndroid Build Coastguard Worker        Assign(Name('foo', ctx='store'),
944*635a8641SAndroid Build Coastguard Worker               Getattr(ContextReference(), 'name'))
945*635a8641SAndroid Build Coastguard Worker    """
946*635a8641SAndroid Build Coastguard Worker
947*635a8641SAndroid Build Coastguard Worker
948*635a8641SAndroid Build Coastguard Workerclass Continue(Stmt):
949*635a8641SAndroid Build Coastguard Worker    """Continue a loop."""
950*635a8641SAndroid Build Coastguard Worker
951*635a8641SAndroid Build Coastguard Worker
952*635a8641SAndroid Build Coastguard Workerclass Break(Stmt):
953*635a8641SAndroid Build Coastguard Worker    """Break a loop."""
954*635a8641SAndroid Build Coastguard Worker
955*635a8641SAndroid Build Coastguard Worker
956*635a8641SAndroid Build Coastguard Workerclass Scope(Stmt):
957*635a8641SAndroid Build Coastguard Worker    """An artificial scope."""
958*635a8641SAndroid Build Coastguard Worker    fields = ('body',)
959*635a8641SAndroid Build Coastguard Worker
960*635a8641SAndroid Build Coastguard Worker
961*635a8641SAndroid Build Coastguard Workerclass OverlayScope(Stmt):
962*635a8641SAndroid Build Coastguard Worker    """An overlay scope for extensions.  This is a largely unoptimized scope
963*635a8641SAndroid Build Coastguard Worker    that however can be used to introduce completely arbitrary variables into
964*635a8641SAndroid Build Coastguard Worker    a sub scope from a dictionary or dictionary like object.  The `context`
965*635a8641SAndroid Build Coastguard Worker    field has to evaluate to a dictionary object.
966*635a8641SAndroid Build Coastguard Worker
967*635a8641SAndroid Build Coastguard Worker    Example usage::
968*635a8641SAndroid Build Coastguard Worker
969*635a8641SAndroid Build Coastguard Worker        OverlayScope(context=self.call_method('get_context'),
970*635a8641SAndroid Build Coastguard Worker                     body=[...])
971*635a8641SAndroid Build Coastguard Worker
972*635a8641SAndroid Build Coastguard Worker    .. versionadded:: 2.10
973*635a8641SAndroid Build Coastguard Worker    """
974*635a8641SAndroid Build Coastguard Worker    fields = ('context', 'body')
975*635a8641SAndroid Build Coastguard Worker
976*635a8641SAndroid Build Coastguard Worker
977*635a8641SAndroid Build Coastguard Workerclass EvalContextModifier(Stmt):
978*635a8641SAndroid Build Coastguard Worker    """Modifies the eval context.  For each option that should be modified,
979*635a8641SAndroid Build Coastguard Worker    a :class:`Keyword` has to be added to the :attr:`options` list.
980*635a8641SAndroid Build Coastguard Worker
981*635a8641SAndroid Build Coastguard Worker    Example to change the `autoescape` setting::
982*635a8641SAndroid Build Coastguard Worker
983*635a8641SAndroid Build Coastguard Worker        EvalContextModifier(options=[Keyword('autoescape', Const(True))])
984*635a8641SAndroid Build Coastguard Worker    """
985*635a8641SAndroid Build Coastguard Worker    fields = ('options',)
986*635a8641SAndroid Build Coastguard Worker
987*635a8641SAndroid Build Coastguard Worker
988*635a8641SAndroid Build Coastguard Workerclass ScopedEvalContextModifier(EvalContextModifier):
989*635a8641SAndroid Build Coastguard Worker    """Modifies the eval context and reverts it later.  Works exactly like
990*635a8641SAndroid Build Coastguard Worker    :class:`EvalContextModifier` but will only modify the
991*635a8641SAndroid Build Coastguard Worker    :class:`~jinja2.nodes.EvalContext` for nodes in the :attr:`body`.
992*635a8641SAndroid Build Coastguard Worker    """
993*635a8641SAndroid Build Coastguard Worker    fields = ('body',)
994*635a8641SAndroid Build Coastguard Worker
995*635a8641SAndroid Build Coastguard Worker
996*635a8641SAndroid Build Coastguard Worker# make sure nobody creates custom nodes
997*635a8641SAndroid Build Coastguard Workerdef _failing_new(*args, **kwargs):
998*635a8641SAndroid Build Coastguard Worker    raise TypeError('can\'t create custom node types')
999*635a8641SAndroid Build Coastguard WorkerNodeType.__new__ = staticmethod(_failing_new); del _failing_new
1000