-
Notifications
You must be signed in to change notification settings - Fork 48
/
parse_tree.go
236 lines (220 loc) · 7.4 KB
/
parse_tree.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
package influxql
import (
"fmt"
)
var Language = &ParseTree{}
type ParseTree struct {
Handlers map[Token]func(*Parser) (Statement, error)
Tokens map[Token]*ParseTree
Keys []string
}
// With passes the current parse tree to a function to allow nested functions.
func (t *ParseTree) With(fn func(*ParseTree)) {
fn(t)
}
// Group groups together a set of related handlers with a common token prefix.
func (t *ParseTree) Group(tokens ...Token) *ParseTree {
for _, tok := range tokens {
// Look for the parse tree for this token.
if subtree := t.Tokens[tok]; subtree != nil {
t = subtree
continue
}
// No subtree exists yet. Verify that we don't have a conflicting
// statement.
if _, conflict := t.Handlers[tok]; conflict {
panic(fmt.Sprintf("conflict for token %s", tok))
}
// Create the new parse tree and register it inside of this one for
// later reference.
newT := &ParseTree{}
if t.Tokens == nil {
t.Tokens = make(map[Token]*ParseTree)
}
t.Tokens[tok] = newT
t.Keys = append(t.Keys, tok.String())
t = newT
}
return t
}
// Handle registers a handler to be invoked when seeing the given token.
func (t *ParseTree) Handle(tok Token, fn func(*Parser) (Statement, error)) {
// Verify that there is no conflict for this token in this parse tree.
if _, conflict := t.Tokens[tok]; conflict {
panic(fmt.Sprintf("conflict for token %s", tok))
}
if _, conflict := t.Handlers[tok]; conflict {
panic(fmt.Sprintf("conflict for token %s", tok))
}
if t.Handlers == nil {
t.Handlers = make(map[Token]func(*Parser) (Statement, error))
}
t.Handlers[tok] = fn
t.Keys = append(t.Keys, tok.String())
}
// Parse parses a statement using the language defined in the parse tree.
func (t *ParseTree) Parse(p *Parser) (Statement, error) {
for {
tok, pos, lit := p.ScanIgnoreWhitespace()
if subtree := t.Tokens[tok]; subtree != nil {
t = subtree
continue
}
if stmt := t.Handlers[tok]; stmt != nil {
return stmt(p)
}
// There were no registered handlers. Return the valid tokens in the order they were added.
return nil, newParseError(tokstr(tok, lit), t.Keys, pos)
}
}
func (t *ParseTree) Clone() *ParseTree {
newT := &ParseTree{}
if t.Handlers != nil {
newT.Handlers = make(map[Token]func(*Parser) (Statement, error), len(t.Handlers))
for tok, handler := range t.Handlers {
newT.Handlers[tok] = handler
}
}
if t.Tokens != nil {
newT.Tokens = make(map[Token]*ParseTree, len(t.Tokens))
for tok, subtree := range t.Tokens {
newT.Tokens[tok] = subtree.Clone()
}
}
return newT
}
func init() {
Language.Handle(SELECT, func(p *Parser) (Statement, error) {
return p.parseSelectStatement(targetNotRequired)
})
Language.Handle(DELETE, func(p *Parser) (Statement, error) {
return p.parseDeleteStatement()
})
Language.Group(SHOW).With(func(show *ParseTree) {
show.Group(CONTINUOUS).Handle(QUERIES, func(p *Parser) (Statement, error) {
return p.parseShowContinuousQueriesStatement()
})
show.Handle(DATABASES, func(p *Parser) (Statement, error) {
return p.parseShowDatabasesStatement()
})
show.Handle(DIAGNOSTICS, func(p *Parser) (Statement, error) {
return p.parseShowDiagnosticsStatement()
})
show.Group(FIELD).With(func(field *ParseTree) {
field.Handle(KEY, func(p *Parser) (Statement, error) {
return p.parseShowFieldKeyCardinalityStatement()
})
field.Handle(KEYS, func(p *Parser) (Statement, error) {
return p.parseShowFieldKeysStatement()
})
})
show.Group(GRANTS).Handle(FOR, func(p *Parser) (Statement, error) {
return p.parseGrantsForUserStatement()
})
show.Group(MEASUREMENT).Handle(EXACT, func(p *Parser) (Statement, error) {
return p.parseShowMeasurementCardinalityStatement(true)
})
show.Group(MEASUREMENT).Handle(CARDINALITY, func(p *Parser) (Statement, error) {
return p.parseShowMeasurementCardinalityStatement(false)
})
show.Handle(MEASUREMENTS, func(p *Parser) (Statement, error) {
return p.parseShowMeasurementsStatement()
})
show.Handle(QUERIES, func(p *Parser) (Statement, error) {
return p.parseShowQueriesStatement()
})
show.Group(RETENTION).Handle(POLICIES, func(p *Parser) (Statement, error) {
return p.parseShowRetentionPoliciesStatement()
})
show.Handle(SERIES, func(p *Parser) (Statement, error) {
return p.parseShowSeriesStatement()
})
show.Group(SHARD).Handle(GROUPS, func(p *Parser) (Statement, error) {
return p.parseShowShardGroupsStatement()
})
show.Handle(SHARDS, func(p *Parser) (Statement, error) {
return p.parseShowShardsStatement()
})
show.Handle(STATS, func(p *Parser) (Statement, error) {
return p.parseShowStatsStatement()
})
show.Handle(SUBSCRIPTIONS, func(p *Parser) (Statement, error) {
return p.parseShowSubscriptionsStatement()
})
show.Group(TAG).With(func(tag *ParseTree) {
tag.Handle(KEY, func(p *Parser) (Statement, error) {
return p.parseShowTagKeyCardinalityStatement()
})
tag.Handle(KEYS, func(p *Parser) (Statement, error) {
return p.parseShowTagKeysStatement()
})
tag.Handle(VALUES, func(p *Parser) (Statement, error) {
return p.parseShowTagValuesStatement()
})
})
show.Handle(USERS, func(p *Parser) (Statement, error) {
return p.parseShowUsersStatement()
})
})
Language.Group(CREATE).With(func(create *ParseTree) {
create.Group(CONTINUOUS).Handle(QUERY, func(p *Parser) (Statement, error) {
return p.parseCreateContinuousQueryStatement()
})
create.Handle(DATABASE, func(p *Parser) (Statement, error) {
return p.parseCreateDatabaseStatement()
})
create.Handle(USER, func(p *Parser) (Statement, error) {
return p.parseCreateUserStatement()
})
create.Group(RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) {
return p.parseCreateRetentionPolicyStatement()
})
create.Handle(SUBSCRIPTION, func(p *Parser) (Statement, error) {
return p.parseCreateSubscriptionStatement()
})
})
Language.Group(DROP).With(func(drop *ParseTree) {
drop.Group(CONTINUOUS).Handle(QUERY, func(p *Parser) (Statement, error) {
return p.parseDropContinuousQueryStatement()
})
drop.Handle(DATABASE, func(p *Parser) (Statement, error) {
return p.parseDropDatabaseStatement()
})
drop.Handle(MEASUREMENT, func(p *Parser) (Statement, error) {
return p.parseDropMeasurementStatement()
})
drop.Group(RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) {
return p.parseDropRetentionPolicyStatement()
})
drop.Handle(SERIES, func(p *Parser) (Statement, error) {
return p.parseDropSeriesStatement()
})
drop.Handle(SHARD, func(p *Parser) (Statement, error) {
return p.parseDropShardStatement()
})
drop.Handle(SUBSCRIPTION, func(p *Parser) (Statement, error) {
return p.parseDropSubscriptionStatement()
})
drop.Handle(USER, func(p *Parser) (Statement, error) {
return p.parseDropUserStatement()
})
})
Language.Handle(EXPLAIN, func(p *Parser) (Statement, error) {
return p.parseExplainStatement()
})
Language.Handle(GRANT, func(p *Parser) (Statement, error) {
return p.parseGrantStatement()
})
Language.Handle(REVOKE, func(p *Parser) (Statement, error) {
return p.parseRevokeStatement()
})
Language.Group(ALTER, RETENTION).Handle(POLICY, func(p *Parser) (Statement, error) {
return p.parseAlterRetentionPolicyStatement()
})
Language.Group(SET, PASSWORD).Handle(FOR, func(p *Parser) (Statement, error) {
return p.parseSetPasswordUserStatement()
})
Language.Group(KILL).Handle(QUERY, func(p *Parser) (Statement, error) {
return p.parseKillQueryStatement()
})
}