This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ast.go
283 lines (221 loc) · 4.6 KB
/
ast.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package cypher
import (
"bytes"
"fmt"
)
// Query represents the Cypher query root element.
type Query struct {
Root *SingleQuery
}
func (q Query) String() string {
return q.Root.String()
}
// SingleQuery ...
type SingleQuery struct {
Reading []ReadingClause
Distinct bool
ReturnItems []Expr
Order []OrderBy
Skip *Expr
Limit *Expr
}
func (sq SingleQuery) String() string {
var buf bytes.Buffer
for _, r := range sq.Reading {
_, _ = buf.WriteString(r.String())
}
_, _ = buf.WriteString(" RETURN ")
if sq.Distinct {
_, _ = buf.WriteString("DISTINCT ")
}
for _, i := range sq.ReturnItems {
_, _ = buf.WriteString(i.String())
}
if len(sq.Order) > 0 {
_, _ = buf.WriteString(" ORDER BY ")
for _, o := range sq.Order {
_, _ = buf.WriteString(o.String())
}
}
return buf.String()
}
// ReadingClause ...
type ReadingClause struct {
OptionalMatch bool
Pattern []MatchPattern
Where *Expr
// Unwind
// Call
}
func (rc ReadingClause) String() string {
var buf bytes.Buffer
if rc.OptionalMatch {
_, _ = buf.WriteString(" OPTIONAL ")
}
for _, p := range rc.Pattern {
_, _ = buf.WriteString(p.String())
}
if w := rc.Where; w != nil {
_, _ = buf.WriteString((*w).String())
}
return buf.String()
}
// MatchPattern ...
type MatchPattern struct {
Variable *Variable
Elements []PatternElement
}
func (mp MatchPattern) String() string {
var buf bytes.Buffer
buf.WriteString("MATCH ")
if mp.Variable != nil {
_, _ = buf.WriteString((*mp.Variable).String())
_, _ = buf.WriteString(" = ")
}
for _, e := range mp.Elements {
_, _ = buf.WriteString(e.String())
}
return buf.String()
}
// PatternElement ...
type PatternElement interface {
patternElem()
String() string
}
func (np NodePattern) patternElem() {}
func (ep EdgePattern) patternElem() {}
// NodePattern ...
type NodePattern struct {
Variable *Variable
Labels []string
Properties map[string]Expr
}
func (np NodePattern) String() string {
var buf bytes.Buffer
_, _ = buf.WriteRune('(')
if np.Variable != nil {
_, _ = buf.WriteString((*np.Variable).String())
}
for _, l := range np.Labels {
_, _ = buf.WriteString(" :")
_, _ = buf.WriteString(l)
}
if len(np.Properties) > 0 {
_, _ = buf.WriteString(" {")
var next bool
for p, v := range np.Properties {
if next {
_, _ = buf.WriteRune(',')
}
_, _ = buf.WriteString(p)
_, _ = buf.WriteString(": ")
_, _ = buf.WriteString(v.String())
next = true
}
_, _ = buf.WriteRune('}')
}
_, _ = buf.WriteRune(')')
return buf.String()
}
// EdgePattern ...
type EdgePattern struct {
Variable *string
Labels []string
Properties map[string]Expr
MinHops *int
MaxHops *int
Direction EdgeDirection
}
// Var ...
func (ep EdgePattern) Var() *string {
return ep.Variable
}
func (ep EdgePattern) String() string {
var buf bytes.Buffer
switch ep.Direction {
case EdgeRight, EdgeUndefined:
_, _ = buf.WriteRune('-')
case EdgeLeft, EdgeOutgoing:
_, _ = buf.WriteString("<-")
}
_, _ = buf.WriteRune('[')
if ep.Variable != nil {
_, _ = buf.WriteString(*ep.Variable)
}
for i, l := range ep.Labels {
if i > 0 {
_, _ = buf.WriteString(" | ")
}
_, _ = buf.WriteRune(':')
_, _ = buf.WriteString(l)
}
if len(ep.Properties) > 0 {
_, _ = buf.WriteRune('{')
var next bool
for p, v := range ep.Properties {
if next {
_, _ = buf.WriteRune(',')
}
_, _ = buf.WriteString(p)
_, _ = buf.WriteRune(':')
_, _ = buf.WriteString(v.String())
next = true
}
_, _ = buf.WriteRune('}')
}
_, _ = buf.WriteRune(']')
switch ep.Direction {
case EdgeLeft, EdgeUndefined:
_, _ = buf.WriteRune('-')
case EdgeRight, EdgeOutgoing:
_, _ = buf.WriteString("->")
}
return buf.String()
}
// EdgeDirection ...
type EdgeDirection int
const (
EdgeUndefined EdgeDirection = iota
EdgeRight
EdgeLeft
EdgeOutgoing
)
// OrderDirection ...
type OrderDirection int
const (
// Ascending defines the ascending ordering.
Ascending OrderDirection = iota
// Descending defines the descending ordering.
Descending
)
// OrderBy ...
type OrderBy struct {
Dir OrderDirection
Item Expr
}
func (o OrderBy) String() string {
return ""
}
// Variable ...
type Variable string
func (v Variable) String() string {
return string(v)
}
// Symbol ...
type Symbol string
func (s Symbol) String() string {
return string(s)
}
// StrLiteral ...
type StrLiteral string
func (s StrLiteral) String() string {
return fmt.Sprintf("\"%s\"", string(s))
}
// Expr ...
type Expr interface {
exp()
String() string
}
func (v Variable) exp() {}
func (s Symbol) exp() {}
func (s StrLiteral) exp() {}