[email protected] | 2ec654a | 2012-01-10 17:47:00 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """ Visitor Object for traversing AST """ |
| 6 | |
| 7 | # |
| 8 | # IDLVisitor |
| 9 | # |
| 10 | # The IDLVisitor class will traverse an AST truncating portions of the tree |
[email protected] | caff0c3 | 2011-07-22 16:34:13 | [diff] [blame] | 11 | # when 'VisitFilter' returns false. After the filter returns true, for each |
| 12 | # node, the visitor will call the 'Arrive' member passing in the node and |
| 13 | # and generic data object from the parent call. The returned value is then |
| 14 | # passed to all children who's results are aggregated into a list. The child |
| 15 | # results along with the original Arrive result are passed to the Depart |
| 16 | # function which returns the final result of the Visit. By default this is |
| 17 | # the exact value that was return from the original arrive. |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 18 | # |
| 19 | |
| 20 | class IDLVisitor(object): |
| 21 | def __init__(self): |
[email protected] | d4eca59 | 2013-11-27 19:05:05 | [diff] [blame] | 22 | pass |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 23 | |
| 24 | # Return TRUE if the node should be visited |
| 25 | def VisitFilter(self, node, data): |
| 26 | return True |
| 27 | |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 28 | def Visit(self, node, data): |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 29 | if not self.VisitFilter(node, data): return None |
| 30 | |
| 31 | childdata = [] |
| 32 | newdata = self.Arrive(node, data) |
| 33 | for child in node.GetChildren(): |
| 34 | ret = self.Visit(child, newdata) |
[email protected] | d4eca59 | 2013-11-27 19:05:05 | [diff] [blame] | 35 | if ret is not None: |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 36 | childdata.append(ret) |
[email protected] | d4eca59 | 2013-11-27 19:05:05 | [diff] [blame] | 37 | return self.Depart(node, newdata, childdata) |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 38 | |
| 39 | def Arrive(self, node, data): |
[email protected] | caff0c3 | 2011-07-22 16:34:13 | [diff] [blame] | 40 | __pychecker__ = 'unusednames=node' |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 41 | return data |
| 42 | |
| 43 | def Depart(self, node, data, childdata): |
[email protected] | caff0c3 | 2011-07-22 16:34:13 | [diff] [blame] | 44 | __pychecker__ = 'unusednames=node,childdata' |
[email protected] | 74f4b46 | 2011-07-19 16:32:59 | [diff] [blame] | 45 | return data |