Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22__all__ = ['TreeVisitor'] 

23 

24from abc import ABC, abstractmethod 

25 

26 

27class TreeVisitor(ABC): 

28 """Definition of interface for visitor classes. 

29 

30 Visitors and tree node classes implement Visitor pattern for tree 

31 traversal. Typical use case is to generate different representation 

32 of the tree, e.g. transforming parsed tree into SQLAlchemy clause. 

33 

34 All methods of the class can (and most likely should) return the 

35 "transformed" value of the visited node. This value will be returned 

36 from the `Node.visit` method and it will also be passed as an argument 

37 to other methods of the visitor. 

38 """ 

39 @abstractmethod 

40 def visitNumericLiteral(self, value, node): 

41 """Visit NumericLiteral node. 

42 

43 Parameters 

44 ---------- 

45 value : `str` 

46 The value associated with the visited node, the value is string, 

47 exactly as it appears in the original expression. Depending on 

48 use case it may need to be converted to `int` or `float`. 

49 node : `Node` 

50 Corresponding tree node, mostly useful for diagnostics. 

51 """ 

52 

53 @abstractmethod 

54 def visitStringLiteral(self, value, node): 

55 """Visit StringLiteral node. 

56 

57 Parameters 

58 ---------- 

59 value : `str` 

60 The value associated with the visited node. 

61 node : `Node` 

62 Corresponding tree node, mostly useful for diagnostics. 

63 """ 

64 

65 @abstractmethod 

66 def visitTimeLiteral(self, value, node): 

67 """Visit TimeLiteral node. 

68 

69 Parameters 

70 ---------- 

71 value : `TimeLiteral` 

72 The value associated with the visited node. 

73 node : `Node` 

74 Corresponding tree node, mostly useful for diagnostics. 

75 """ 

76 

77 @abstractmethod 

78 def visitRangeLiteral(self, start, stop, stride, node): 

79 """Visit RangeLiteral node. 

80 

81 Parameters 

82 ---------- 

83 start : `int` 

84 Range starting value. 

85 stop : `int` 

86 Range final value. 

87 stride : `int` or `None` 

88 Stride, can be `None` if not specified (should be treated same 

89 as 1). 

90 node : `Node` 

91 Corresponding tree node, mostly useful for diagnostics. 

92 """ 

93 

94 @abstractmethod 

95 def visitIdentifier(self, name, node): 

96 """Visit Identifier node. 

97 

98 Parameters 

99 ---------- 

100 name : `str` 

101 Identifier name. 

102 node : `Node` 

103 Corresponding tree node, mostly useful for diagnostics. 

104 """ 

105 

106 @abstractmethod 

107 def visitUnaryOp(self, operator, operand, node): 

108 """Visit UnaryOp node. 

109 

110 Parameters 

111 ---------- 

112 operator : `str` 

113 Operator name, e.g. "NOT" or "+". 

114 operand : `object` 

115 Operand, this object is returned by one of the methods of this 

116 class as a result of transformation of some other tree node. 

117 node : `Node` 

118 Corresponding tree node, mostly useful for diagnostics. 

119 """ 

120 

121 @abstractmethod 

122 def visitBinaryOp(self, operator, lhs, rhs, node): 

123 """Visit BinaryOp node. 

124 

125 Parameters 

126 ---------- 

127 operator : `str` 

128 Operator name, e.g. "NOT" or "+". 

129 lhs : `object` 

130 Left hand side operand, this object is returned by one of the 

131 methods of this class as a result of transformation of some other 

132 tree node. 

133 rhs : `object` 

134 Right hand side operand, this object is returned by one of the 

135 methods of this class as a result of transformation of some other 

136 tree node. 

137 node : `Node` 

138 Corresponding tree node, mostly useful for diagnostics. 

139 """ 

140 

141 @abstractmethod 

142 def visitIsIn(self, lhs, values, not_in, node): 

143 """Visit IsIn node. 

144 

145 Parameters 

146 ---------- 

147 lhs : `object` 

148 Left hand side operand, this object is returned by one of the 

149 methods of this class as a result of transformation of some other 

150 tree node. 

151 values : `list` of `object` 

152 Right hand side operand, list of objects returned by methods of 

153 this class as a result of transformation of some other tree nodes. 

154 not_in : `bool` 

155 `True` for "NOT IN" expression. 

156 node : `Node` 

157 Corresponding tree node, mostly useful for diagnostics. 

158 """ 

159 

160 @abstractmethod 

161 def visitParens(self, expression, node): 

162 """Visit Parens node. 

163 

164 Parameters 

165 ---------- 

166 expression : `object` 

167 Expression inside parentheses, this object is returned by one of 

168 the methods of this class as a result of transformation of some 

169 other tree node. 

170 node : `Node` 

171 Corresponding tree node, mostly useful for diagnostics. 

172 """