forked from square/grange
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnodes.go
144 lines (115 loc) · 2.31 KB
/
nodes.go
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
144
package grange
import (
"fmt"
"strings"
)
type operatorType int
const (
operatorIntersect operatorType = iota
operatorSubtract
operatorUnion
)
type parserNode interface {
String() string
}
type nodeNull struct{}
// Transient marker node to delineate the start of a braces capture. This is
// kind of weird. This node should never be present one parsing is complete.
type nodeBraceStart struct{}
type nodeText struct {
val string
}
type nodeConstant struct {
val string
}
type nodeRegexp struct {
val string
}
type nodeLocalClusterLookup struct {
key string
}
type nodeGroupQuery struct {
node parserNode
}
type nodeClusterLookup struct {
node parserNode
key parserNode
}
type nodeOperator struct {
op operatorType
left parserNode
right parserNode
}
type nodeBraces struct {
node parserNode
left parserNode
right parserNode
}
type nodeFunction struct {
name string
params []parserNode
}
func (n nodeFunction) String() string {
result := []string{}
for _, param := range n.params {
result = append(result, param.String())
}
return fmt.Sprintf("%s(%s)", n.name, strings.Join(result, ";"))
}
func (n nodeText) String() string {
return n.val
}
func (n nodeConstant) String() string {
return n.val
}
func (n nodeRegexp) String() string {
return fmt.Sprintf("/%s/", n.val)
}
func (n nodeClusterLookup) String() string {
switch n.key.(type) {
case nodeText:
if n.key.(nodeText).val == "CLUSTER" {
return fmt.Sprintf("%%{%s}", n.node)
}
}
return fmt.Sprintf("%%{%s}:%s", n.node, n.key)
}
func (n nodeGroupQuery) String() string {
return fmt.Sprintf("?%s", n.node)
}
func (n nodeLocalClusterLookup) String() string {
return fmt.Sprintf("$%s", n.key)
}
func (n nodeBraces) String() string {
return fmt.Sprintf("%s{%s}%s", n.node, n.left, n.right)
}
func (n nodeNull) String() string {
return ""
}
func (n nodeBraceStart) String() string {
return ""
}
func (n nodeOperator) String() string {
var op string
switch n.op {
case operatorIntersect:
op = "&"
case operatorSubtract:
op = "-"
case operatorUnion:
op = ","
}
return fmt.Sprintf("%s %s %s", n.left, op, n.right)
}
func (t operatorType) String() string {
switch t {
case operatorIntersect:
return "&"
case operatorSubtract:
return "-"
case operatorUnion:
return ","
default:
panic("Unknown operatorType")
}
}