Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 89 lines (75 sloc) 3.335 kb
e791c2a @mitsuhiko added first working pieces of compiler
authored
1 # -*- coding: utf-8 -*-
2 """
3 jinja2.visitor
4 ~~~~~~~~~~~~~~
5
6 This module implements a visitor for the nodes.
7
8 :copyright: Copyright 2008 by Armin Ronacher.
9 :license: GNU GPL.
10 """
11 from jinja2.nodes import Node
12
13
14 class NodeVisitor(object):
26c0f51 @mitsuhiko updated a few docstrings and removed contrib
authored
15 """Walks the abstract syntax tree and call visitor functions for every
16 node found. The visitor functions may return values which will be
17 forwarded by the `visit` method.
e791c2a @mitsuhiko added first working pieces of compiler
authored
18
19 Per default the visitor functions for the nodes are ``'visit_'`` +
20 class name of the node. So a `TryFinally` node visit function would
21 be `visit_TryFinally`. This behavior can be changed by overriding
22 the `get_visitor` function. If no visitor function exists for a node
23 (return value `None`) the `generic_visit` visitor is used instead.
24 """
25
26 def get_visitor(self, node):
27 """
28 Return the visitor function for this node or `None` if no visitor
29 exists for this node. In that case the generic visit function is
30 used instead.
31 """
32 method = 'visit_' + node.__class__.__name__
33 return getattr(self, method, None)
34
35 def visit(self, node, *args, **kwargs):
36 """Visit a node."""
37 f = self.get_visitor(node)
38 if f is not None:
39 return f(node, *args, **kwargs)
40 return self.generic_visit(node, *args, **kwargs)
41
42 def generic_visit(self, node, *args, **kwargs):
43 """Called if no explicit visitor function exists for a node."""
44 for node in node.iter_child_nodes():
45 self.visit(node, *args, **kwargs)
46
47
48 class NodeTransformer(NodeVisitor):
26c0f51 @mitsuhiko updated a few docstrings and removed contrib
authored
49 """Walks the abstract syntax tree and allows modifications of nodes.
e791c2a @mitsuhiko added first working pieces of compiler
authored
50
51 The `NodeTransformer` will walk the AST and use the return value of the
52 visitor functions to replace or remove the old node. If the return
53 value of the visitor function is `None` the node will be removed
54 from the previous location otherwise it's replaced with the return
55 value. The return value may be the original node in which case no
56 replacement takes place.
57 """
58
59 def generic_visit(self, node, *args, **kwargs):
60 for field, old_value in node.iter_fields():
61 if isinstance(old_value, list):
62 new_values = []
63 for value in old_value:
64 if isinstance(value, Node):
65 value = self.visit(value, *args, **kwargs)
66 if value is None:
67 continue
68 elif not isinstance(value, Node):
69 new_values.extend(value)
70 continue
71 new_values.append(value)
72 old_value[:] = new_values
73 elif isinstance(old_value, Node):
74 new_node = self.visit(old_value, *args, **kwargs)
75 if new_node is None:
76 delattr(node, field)
77 else:
78 setattr(node, field, new_node)
79 return node
4f7d2d5 @mitsuhiko fixed more unittests
authored
80
81 def visit_list(self, node, *args, **kwargs):
82 """As transformers may return lists in some places this method
83 can be used to enforce a list as return value.
84 """
85 rv = self.visit(node, *args, **kwargs)
86 if not isinstance(rv, list):
87 rv = [rv]
88 return rv
Something went wrong with that request. Please try again.