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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

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

# for details of code ownership. 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

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

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

 

__all__ = ['TreeVisitor'] 

 

from abc import ABC, abstractmethod 

 

 

class TreeVisitor(ABC): 

"""Definition of interface for visitor classes. 

 

Visitors and tree node classes implement Visitor pattern for tree 

traversal. Typical use case is to generate different representation 

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

 

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

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

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

to other methods of the visitor. 

""" 

@abstractmethod 

def visitNumericLiteral(self, value, node): 

"""Visit NumericLiteral node. 

 

Parameters 

---------- 

value : `str` 

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

exactly as it appears in the original expression. Depending on 

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

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

def visitStringLiteral(self, value, node): 

"""Visit StringLiteral node. 

 

Parameters 

---------- 

value : `str` 

The value associated with the visited node. 

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

def visitIdentifier(self, name, node): 

"""Visit Identifier node. 

 

Parameters 

---------- 

name : `str` 

Identifier name. 

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

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

"""Visit UnaryOp node. 

 

Parameters 

---------- 

operator : `str` 

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

operand : `object` 

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

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

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

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

"""Visit BinaryOp node. 

 

Parameters 

---------- 

operator : `str` 

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

lhs : `object` 

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

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

tree node. 

rhs : `object` 

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

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

tree node. 

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

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

"""Visit IsIn node. 

 

Parameters 

---------- 

lhs : `object` 

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

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

tree node. 

values : `list` of `object` 

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

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

not_in : `bool` 

`True` for "NOT IN" expression. 

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

""" 

 

@abstractmethod 

def visitParens(self, expression, node): 

"""Visit Parens node. 

 

Parameters 

---------- 

expression : `object` 

Expression inside parentheses, this object is returned by one of 

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

other tree node. 

node : `Node` 

Corresponding tree node, mostly useful for diagnostics. 

"""