-
Notifications
You must be signed in to change notification settings - Fork 1
/
expr.go
423 lines (377 loc) · 10.7 KB
/
expr.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package minidb
import (
"fmt"
"regexp"
)
type token struct {
sort QuerySort
content []rune
}
type stack struct {
data []token
// name string
}
type queue struct {
data []token
}
func newStack() *stack {
s := stack{data: []token{}}
return &s
}
func newQueue() *queue {
q := queue{data: []token{}}
return &q
}
func (s *stack) push(v token) {
// fmt.Printf("*** %s push %s: %s\n", s.name, QuerySortToStr(v.sort), string(v.content))
s.data = append(s.data, v)
}
func (s *stack) isEmpty() bool {
return len(s.data) == 0
}
func (s *stack) pop() token {
v := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1]
// fmt.Printf("*** %s pop %s: %s\n", s.name, QuerySortToStr(v.sort), string(v.content))
return v
}
func (s *stack) peek() token {
return s.data[len(s.data)-1]
}
func (s *stack) count() int {
return len(s.data)
}
func (s *stack) debugDump() string {
r := "Stack dump:\n"
for i := len(s.data) - 1; i >= 0; i-- {
r = r + fmt.Sprintf("Sort %s, Content '%s'\n", QuerySortToStr(s.data[i].sort), string(s.data[i].content))
}
return r
}
func (q *queue) add(v token) {
q.data = append(q.data, v)
}
func (q *queue) isEmpty() bool {
return len(q.data) == 0
}
func (q *queue) get() token {
v := q.data[0]
q.data = q.data[1:]
return v
}
func (q *queue) count() int {
return len(q.data)
}
type pstate struct {
pos int
in []rune
out *stack
ops *stack
}
func newPstate(s string) *pstate {
v := pstate{0, []rune(s), newStack(), newStack()}
return &v
}
// start the parsing (like a start rule)
func start(state *pstate) error {
return parseSearchClause(state)
}
// consume one literal token or just lookahead
func lookOrConsume1(state *pstate, consume bool) []rune {
var isTokenChar = regexp.MustCompile(`^[a-zA-Z_0-9]+$`).MatchString
i := state.pos
for i < len(state.in) && state.in[i] == ' ' {
i++
}
from := i
for i < len(state.in) && isTokenChar(string(state.in[i])) {
i++
}
if consume {
state.pos = i
}
return state.in[from:i]
}
func lookAhead1(state *pstate) []rune {
return lookOrConsume1(state, false)
}
func consume1(state *pstate) []rune {
return lookOrConsume1(state, true)
}
// parse parts like "Name=query" or "not every Name=John" or
// "no Data=hello%"
func parseSearch(state *pstate) error {
peek := lookAhead1(state)
if string(peek) == "not" {
state.ops.push(token{content: peek, sort: LogicalNot})
consume1(state)
peek = lookAhead1(state)
}
switch string(peek) {
case "every":
state.ops.push(token{content: peek, sort: EveryTerm})
consume1(state)
case "no":
state.ops.push(token{content: peek, sort: NoTerm})
consume1(state)
}
if err := parseField(state); err != nil {
return err
}
if err := parseInfixOP(state); err != nil {
return err
}
return parseSearchQuery(state)
}
func maybeParseParens(state *pstate) error {
maybeParen := true
for maybeParen && state.pos < len(state.in) {
skipWS(state)
nc := state.in[state.pos]
switch nc {
case '(':
state.ops.push(token{content: []rune{nc}, sort: LeftParen})
state.pos++
case ')':
/* while the operator at the top of the operator stack is not a left bracket:
pop the operator from the operator stack onto the output queue.
pop the left bracket from the stack. */
for !state.ops.isEmpty() && state.ops.peek().sort != LeftParen {
state.out.push(state.ops.pop())
}
if state.ops.isEmpty() {
return Fail(`pos=%d: mismatched right paren (no left paren)`, state.pos)
}
state.ops.pop()
state.pos++
maybeParseParens(state)
maybeParseConnective(state)
default:
maybeParen = false
}
}
return nil
}
func maybeParseConnective(state *pstate) error {
// peek for logical and, or
skipWS(state)
peek := lookAhead1(state)
switch string(peek) {
case "and", "or":
consume1(state)
/* if the token is an operator, then:
while ((there is a function at the top of the operator stack)
or (there is an operator at the top of the operator stack with greater precedence)
or (the operator at the top of the operator stack has equal precedence and is left associative))
and (the operator at the top of the operator stack is not a left bracket):
pop operators from the operator stack onto the output queue.
push it onto the operator stack. */
for !state.ops.isEmpty() && state.ops.peek().sort != LeftParen && state.ops.peek().sort != SearchClause {
state.out.push(state.ops.pop())
}
if string(peek) == "or" {
state.ops.push(token{sort: LogicalOr, content: peek})
} else {
state.ops.push(token{sort: LogicalAnd, content: peek})
}
err := parseComplexExpr(state)
if err != nil {
return Fail(`pos=%d: missing argument, expected a search clause like "Name=John" as second argument of "%s"; %s`, state.pos, string(peek), err)
}
}
return nil
}
func parseComplexExpr(state *pstate) error {
if err := maybeParseParens(state); err != nil {
return err
}
if err := parseSearch(state); err != nil {
return err
}
if err := maybeParseConnective(state); err != nil {
return err
}
if err := maybeParseParens(state); err != nil {
return err
}
return nil
}
// parse "Person Name=query" or "Person not every Name=Query"
func parseSearchClause(state *pstate) error {
state.ops.push(token{sort: SearchClause})
if err := parseTable(state); err != nil {
return err
}
parseComplexExpr(state)
return nil
}
// parse the name of a table
func parseTable(state *pstate) error {
var isTablename = regexp.MustCompile(`^[a-zA-Z_0-9]+$`).MatchString
skipWS(state)
start := state.pos
for state.pos < len(state.in) && isTablename(string(state.in[state.pos])) {
state.pos++
}
if start == state.pos {
return Fail(`pos=%d: malformed or missing table name`, start)
}
state.out.push(token{content: state.in[start:state.pos], sort: TableString})
return nil
}
// parse the name of a field
func parseField(state *pstate) error {
var isFieldname = regexp.MustCompile(`^[a-zA-Z_0-9]+$`).MatchString
skipWS(state)
start := state.pos
for state.pos < len(state.in) && isFieldname(string(state.in[state.pos])) {
state.pos++
}
if start == state.pos {
return Fail(`pos=%d: malformed or missing field name`, start)
}
state.out.push(token{content: state.in[start:state.pos], sort: FieldString})
return nil
}
// parse an infix operator such as "="
func parseInfixOP(state *pstate) error {
skipWS(state)
if state.pos >= len(state.in) {
return Fail(`pos=%d: unexpected end of line`, len(state.in))
}
c := state.in[state.pos]
switch c {
case '=':
state.pos++
state.ops.push(token{content: []rune{'='}, sort: InfixOP})
default:
return Fail(`pos=%d: expected an infix operator like "="`, state.pos)
}
return nil
}
// parse the query part which may be a string or unquoted, e.g.
// "%hello%" in the query "Person Name=%hello%"
func parseSearchQuery(state *pstate) error {
var noDelimiter = regexp.MustCompile(`\S`).MatchString
skipWS(state)
start := state.pos
if state.in[state.pos] == '"' {
return parseString(state)
}
for state.pos < len(state.in) && noDelimiter(string(state.in[state.pos])) && state.in[state.pos] != ')' {
state.pos++
}
state.out.push(token{content: state.in[start:state.pos], sort: QueryString})
return nil
}
// parse a string until the end of the string, adding the unquoted string as token
func parseString(state *pstate) error {
state.pos++
start := state.pos
if state.pos < len(state.in) && !(state.in[state.pos] == '"') {
state.pos++
}
state.out.push(token{content: state.in[start:state.pos], sort: SearchClause})
state.pos++
return nil
}
func skipWS(state *pstate) {
for state.pos < len(state.in) && state.in[state.pos] == ' ' {
state.pos++
}
}
// convert the operator stack and argument queue to a well-formed Query structure
// with nested arguments - it's a simple AST.
func convert(parse *pstate) (*Query, error) {
// if we're here, we're expecting something in the output
if parse.out.isEmpty() {
return nil, Fail(`syntax error, unexpected end of line`)
}
token := parse.out.pop()
switch token.sort {
case LogicalAnd, LogicalOr:
rarg, err := convert(parse)
if err != nil {
return nil, err
}
larg, err := convert(parse)
if err != nil {
return nil, err
}
query := Query{Sort: token.sort, Data: string(token.content), Children: []Query{*larg, *rarg}}
return &query, nil
case EveryTerm, NoTerm:
embeddedQuery, err := convert(parse)
if err != nil {
return nil, err
}
query := Query{Sort: token.sort, Data: string(token.content), Children: []Query{*embeddedQuery}}
return &query, nil
case LogicalNot:
embeddedQuery, err := convert(parse)
if err != nil {
return nil, err
}
query := Query{Sort: LogicalNot, Data: string(token.content), Children: []Query{*embeddedQuery}}
return &query, nil
case SearchClause:
subclause, err := convert(parse)
if err != nil {
return nil, Fail(`invalid or missing search expression: %s`, err)
}
table, err := convert(parse)
if err != nil {
return nil, Fail(`invalid or missing table name: %s`, err)
}
query := Query{Sort: SearchClause, Children: []Query{*subclause},
Data: table.Data}
return &query, nil
case InfixOP:
if parse.out.count() < 2 {
return nil, Fail(`syntax error, incomplete query, expected form fieldname=query`)
}
query1 := Query{Sort: QueryString, Data: string(parse.out.pop().content)}
query2 := Query{Sort: FieldString, Data: string(parse.out.pop().content)}
query3 := Query{Sort: InfixOP, Data: string(token.content), Children: []Query{query2, query1}}
return &query3, nil
case FieldString:
query := Query{Sort: FieldString, Data: string(token.content)}
return &query, nil
case QueryString:
query := Query{Sort: QueryString, Data: string(token.content)}
return &query, nil
case TableString:
query := Query{Sort: TableString, Data: string(token.content)}
return &query, nil
default:
return nil, Fail(`syntax error, unexpected operator type %s`, QuerySortToStr(token.sort))
}
}
// ParseQuery parses a string representing the part after the table name into a Query structure.
// Implements the classic Shunting Yard algorithm.
func ParseQuery(s string) (*Query, error) {
state := newPstate(s)
// fmt.Println()
// fmt.Println(s)
if err := start(state); err != nil {
return nil, err
}
// push all remaining operators to the output stack
for !state.ops.isEmpty() {
op := state.ops.pop()
if op.sort == LeftParen {
return nil, Fail(`syntax error, unmatched left parenthesis`)
}
state.out.push(op)
}
// fmt.Print(state.out.debugDump())
query, err := convert(state)
if err != nil {
return nil, err
}
// fmt.Print(query.DebugDump())
if !state.ops.isEmpty() || !state.out.isEmpty() {
return nil, Fail(`syntax error, unexpected input at end of the line\n++OutputStack=%s,\n++Operator stack=%s\n++Partial parse=%s`, state.out.debugDump(), state.ops.debugDump(), query.DebugDump())
}
return query, nil
}