1###############################################################################
2#
3# Copyright (c) 2011 Ruslan Spivak
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22#
23###############################################################################
24
25__author__ = 'Ruslan Spivak <ruslan.spivak@gmail.com>'
26
27
28class ASTVisitor(object):
29    """Base class for custom AST node visitors.
30
31    Example:
32
33    >>> from slimit.parser import Parser
34    >>> from slimit.visitors.nodevisitor import ASTVisitor
35    >>>
36    >>> text = '''
37    ... var x = {
38    ...     "key1": "value1",
39    ...     "key2": "value2"
40    ... };
41    ... '''
42    >>>
43    >>> class MyVisitor(ASTVisitor):
44    ...     def visit_Object(self, node):
45    ...         '''Visit object literal.'''
46    ...         for prop in node:
47    ...             left, right = prop.left, prop.right
48    ...             print 'Property value: %s' % right.value
49    ...             # visit all children in turn
50    ...             self.visit(prop)
51    ...
52    >>>
53    >>> parser = Parser()
54    >>> tree = parser.parse(text)
55    >>> visitor = MyVisitor()
56    >>> visitor.visit(tree)
57    Property value: "value1"
58    Property value: "value2"
59
60    """
61
62    def visit(self, node):
63        method = 'visit_%s' % node.__class__.__name__
64        return getattr(self, method, self.generic_visit)(node)
65
66    def generic_visit(self, node):
67        for child in node:
68            self.visit(child)
69
70
71class NodeVisitor(object):
72    """Simple node visitor."""
73
74    def visit(self, node):
75        """Returns a generator that walks all children recursively."""
76        for child in node:
77            yield child
78            for subchild in self.visit(child):
79                yield subchild
80
81
82def visit(node):
83    visitor = NodeVisitor()
84    for child in visitor.visit(node):
85        yield child
86