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 visitRangeLiteral(self, start, stop, stride, node): 

67 """Visit RangeLiteral node. 

68 

69 Parameters 

70 ---------- 

71 start : `int` 

72 Range starting value. 

73 stop : `int` 

74 Range final value. 

75 stride : `int` or `None` 

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

77 as 1). 

78 node : `Node` 

79 Corresponding tree node, mostly useful for diagnostics. 

80 """ 

81 

82 @abstractmethod 

83 def visitIdentifier(self, name, node): 

84 """Visit Identifier node. 

85 

86 Parameters 

87 ---------- 

88 name : `str` 

89 Identifier name. 

90 node : `Node` 

91 Corresponding tree node, mostly useful for diagnostics. 

92 """ 

93 

94 @abstractmethod 

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

96 """Visit UnaryOp node. 

97 

98 Parameters 

99 ---------- 

100 operator : `str` 

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

102 operand : `object` 

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

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

105 node : `Node` 

106 Corresponding tree node, mostly useful for diagnostics. 

107 """ 

108 

109 @abstractmethod 

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

111 """Visit BinaryOp node. 

112 

113 Parameters 

114 ---------- 

115 operator : `str` 

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

117 lhs : `object` 

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

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

120 tree node. 

121 rhs : `object` 

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

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

124 tree node. 

125 node : `Node` 

126 Corresponding tree node, mostly useful for diagnostics. 

127 """ 

128 

129 @abstractmethod 

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

131 """Visit IsIn node. 

132 

133 Parameters 

134 ---------- 

135 lhs : `object` 

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

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

138 tree node. 

139 values : `list` of `object` 

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

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

142 not_in : `bool` 

143 `True` for "NOT IN" expression. 

144 node : `Node` 

145 Corresponding tree node, mostly useful for diagnostics. 

146 """ 

147 

148 @abstractmethod 

149 def visitParens(self, expression, node): 

150 """Visit Parens node. 

151 

152 Parameters 

153 ---------- 

154 expression : `object` 

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

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

157 other tree node. 

158 node : `Node` 

159 Corresponding tree node, mostly useful for diagnostics. 

160 """