forked from araddon/qlbridge
/
parse.go
629 lines (580 loc) · 16 KB
/
parse.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package expr
import (
"fmt"
"runtime"
"strings"
u "github.com/araddon/gou"
"github.com/araddon/qlbridge/lex"
)
var _ = u.EMPTY
// We have a default Dialect, which is the "Language" or rule-set of ql
var DefaultDialect *lex.Dialect = lex.LogicalExpressionDialect
// TokenPager wraps a Lexer, and implements the Logic to determine what is
// the end of this particular clause. Lexer's are stateless, while
// tokenpager implements state ontop of pager and allows forward/back etc
//
type TokenPager interface {
Peek() lex.Token
Next() lex.Token
Cur() lex.Token
Last() lex.TokenType
Backup()
IsEnd() bool
ClauseEnd() bool
Lexer() *lex.Lexer
}
// SchemaInfo is interface for a Column type
//
type SchemaInfo interface {
Key() string
}
// TokenPager is responsible for determining end of
// current tree (column, etc)
type LexTokenPager struct {
done bool
tokens []lex.Token // list of all the tokens
cursor int
lex *lex.Lexer
end lex.TokenType
}
func NewLexTokenPager(lex *lex.Lexer) *LexTokenPager {
p := LexTokenPager{
lex: lex,
}
p.cursor = 0
p.lexNext()
return &p
}
// next returns the next token.
func (m *LexTokenPager) Next() lex.Token {
m.lexNext()
m.cursor++
if m.cursor+1 > len(m.tokens) {
//u.Warnf("Next() CRAP? increment cursor: %v of %v %v", m.cursor, len(m.tokens))
//panic("WTF, not enough tokens?")
}
//u.Debugf("Next(): %v of %v %v", m.cursor, len(m.tokens), m.tokens[m.cursor])
return m.tokens[m.cursor-1]
}
func (m *LexTokenPager) lexNext() {
if !m.done {
tok := m.lex.NextToken()
if tok.T == lex.TokenEOF {
m.done = true
}
m.tokens = append(m.tokens, tok)
//u.Infof("lexNext: %v of %v cur=%v", m.cursor, len(m.tokens), tok)
}
}
func (m *LexTokenPager) Cur() lex.Token {
//u.Debugf("Cur(): %v of %v %v", m.cursor, len(m.tokens), m.tokens[m.cursor])
if m.cursor+1 >= len(m.tokens) {
//panic("WTF, not enough tokens?")
//u.Warnf("Next() CRAP? increment cursor: %v of %v %v", m.cursor, len(m.tokens), m.cursor < len(m.tokens))
}
return m.tokens[m.cursor]
}
func (m *LexTokenPager) Last() lex.TokenType {
return m.end
}
func (m *LexTokenPager) IsEnd() bool {
return false
}
func (m *LexTokenPager) ClauseEnd() bool {
return false
}
func (m *LexTokenPager) Lexer() *lex.Lexer {
return m.lex
}
// backup backs the input stream up one token.
func (m *LexTokenPager) Backup() {
if m.cursor > 0 {
m.cursor--
//u.Warnf("Backup?: %v", m.cursor)
return
}
}
// peek returns but does not consume the next token.
func (m *LexTokenPager) Peek() lex.Token {
//u.Debugf("prepeek: %v of %v", m.cursor, len(m.tokens))
if len(m.tokens) <= m.cursor+1 && !m.done {
m.lexNext()
//u.Warnf("lexed cursor?: %v %p", m.cursor, &m.cursor)
}
if len(m.tokens) < 2 {
m.lexNext()
}
if len(m.tokens) == m.cursor+1 {
u.Infof("last one?: %v of %v %v", m.cursor, len(m.tokens), m.tokens[m.cursor])
return m.tokens[m.cursor]
}
if m.cursor == -1 {
return m.tokens[1]
}
//u.Infof("peek: %v of %v %v", m.cursor, len(m.tokens), m.tokens[m.cursor+1])
return m.tokens[m.cursor+1]
}
// Tree is the representation of a single parsed expression
type Tree struct {
runCheck bool
Root Node // top-level root node of the tree
TokenPager // pager for grabbing next tokens, backup(), recognizing end
}
func NewTree(pager TokenPager) *Tree {
t := Tree{TokenPager: pager}
return &t
}
// Parse a single Expression, returning a Tree
//
// ParseExpression("5 * toint(item_name)")
//
func ParseExpression(expressionText string) (*Tree, error) {
l := lex.NewLexer(expressionText, lex.LogicalExpressionDialect)
pager := NewLexTokenPager(l)
t := NewTree(pager)
pager.end = lex.TokenEOF
err := t.BuildTree(true)
return t, err
}
// Parsing.
// errorf formats the error and terminates processing.
func (t *Tree) errorf(format string, args ...interface{}) {
t.Root = nil
format = fmt.Sprintf("expr: %s", format)
msg := fmt.Errorf(format, args...)
u.LogTracef(u.WARN, "about to panic: %v", msg)
panic(msg)
}
// error terminates processing.
func (t *Tree) error(err error) {
t.errorf("%s", err)
}
// expect verifies the current token and guarantees it has the required type
func (t *Tree) expect(expected lex.TokenType, context string) lex.Token {
token := t.Cur()
//u.Debugf("checking expected? %v got?: %v", expected, token)
if token.T != expected {
u.Warnf("unexpeted token? %v want:%v", token, expected)
t.unexpected(token, context)
}
return token
}
// expectOneOf consumes the next token and guarantees it has one of the required types.
func (t *Tree) expectOneOf(expected1, expected2 lex.TokenType, context string) lex.Token {
token := t.Cur()
if token.T != expected1 && token.T != expected2 {
t.unexpected(token, context)
}
return token
}
// unexpected complains about the token and terminates processing.
func (t *Tree) unexpected(token lex.Token, context string) {
u.Errorf("unexpected? %v", token)
t.errorf("unexpected %s in %s", token, context)
}
// recover is the handler that turns panics into returns from the top level of Parse.
func (t *Tree) recover(errp *error) {
e := recover()
if e != nil {
u.Errorf("Recover(): %v", e)
if _, ok := e.(runtime.Error); ok {
panic(e)
}
*errp = e.(error)
}
return
}
// buildTree take the tokens and recursively build into expression tree node
// @runCheck Do we want to verify this tree? If being used as VM then yes.
func (t *Tree) BuildTree(runCheck bool) error {
//u.Debugf("parsing: %v", t.Cur())
t.runCheck = runCheck
//u.Debugf("parsing: %v", t.Cur())
t.Root = t.O(0)
//u.Debugf("after parse()")
if !t.ClauseEnd() {
//u.Warnf("Not End? last=%v", t.TokenPager.Last())
//t.expect(t.TokenPager.Last(), "input")
}
if runCheck {
if err := t.Root.Check(); err != nil {
u.Errorf("found error: %v", err)
t.error(err)
return err
}
}
return nil
}
/*
Operator Predence planner during parse phase:
when we parse and build our node-sub-node structures we need to plan
the precedence rules, we use a recursion tree to build this
http://dev.mysql.com/doc/refman/5.0/en/operator-precedence.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
http://www.postgresql.org/docs/9.4/static/sql-syntax-lexical.html#SQL-PRECEDENCE
TODO:
- implement new one for parens
- implement flags for commutative/
--------------------------------------
O -> A {( "||" | OR ) A}
A -> C {( "&&" | AND ) C}
C -> P {( "==" | "!=" | ">" | ">=" | "<" | "<=" | "LIKE" | "IN" ) P}
P -> M {( "+" | "-" ) M}
M -> F {( "*" | "/" ) F}
F -> v | "(" O ")" | "!" O | "-" O
v -> number | func(..)
Func -> name "(" param {"," param} ")"
param -> number | "string" | O
Recursion: We recurse so the LAST to evaluate is the highest (parent, then or)
ie the deepest we get in recursion tree is the first to be evaluated
1 Unary + - arithmetic operators, PRIOR operator
2 * / arithmetic operators
3 Binary + - arithmetic operators, || character operators
4 All comparison operators
5 NOT logical operator
6 AND logical operator
7 OR logical operator
8 Paren's
*/
// expr:
func (t *Tree) O(depth int) Node {
//u.Debugf("depth:%d t.O Cur(): %v", depth, t.Cur())
n := t.A(depth)
//u.Debugf("depth:%d t.O AFTER: n:%v cur:%v ", depth, n, t.Cur())
for {
tok := t.Cur()
//u.Debugf("tok: cur=%v peek=%v", t.Cur(), t.Peek())
switch tok.T {
case lex.TokenLogicOr, lex.TokenOr:
t.Next()
n = NewBinaryNode(tok, n, t.A(depth+1))
case lex.TokenCommentSingleLine:
// we consume the comment signifier "--"" as well as comment
//u.Debugf("tok: %v", t.Next())
//u.Debugf("tok: %v", t.Next())
t.Next()
t.Next()
case lex.TokenEOF, lex.TokenEOS, lex.TokenFrom, lex.TokenComma, lex.TokenIf,
lex.TokenAs, lex.TokenSelect, lex.TokenLimit:
// these are indicators of End of Current Clause, so we can return?
//u.Debugf("done, return: %v", tok)
return n
default:
//u.Debugf("root couldnt evaluate node? %v", tok)
return n
}
}
}
func (t *Tree) A(depth int) Node {
//u.Debugf("%d t.A: %v", depth, t.Cur())
n := t.C(depth)
//u.Debugf("%d t.A: AFTER %v", depth, t.Cur())
for {
//u.Debugf("tok: cur=%v peek=%v", t.Cur(), t.Peek())
switch tok := t.Cur(); tok.T {
case lex.TokenLogicAnd, lex.TokenAnd:
t.Next()
n = NewBinaryNode(tok, n, t.C(depth+1))
default:
return n
}
}
}
func (t *Tree) C(depth int) Node {
//u.Debugf("%d t.C: %v", depth, t.Cur())
n := t.P(depth)
//u.Debugf("%d t.C: %v", depth, t.Cur())
for {
//u.Debugf("tok: cur=%v peek=%v n=%v", t.Cur(), t.Peek(), n.StringAST())
switch cur := t.Cur(); cur.T {
case lex.TokenNegate:
//u.Infof("doing urnary node on negate: %v", cur)
t.Next()
return NewUnary(cur, t.cInner(n, depth+1))
case lex.TokenIs:
t.Next()
if t.Cur().T == lex.TokenNegate {
cur = t.Next()
ne := lex.Token{T: lex.TokenNE, V: "!=", Pos: cur.Pos}
return NewBinaryNode(ne, n, t.P(depth+1))
}
return NewUnary(cur, t.cInner(n, depth+1))
default:
return t.cInner(n, depth)
}
}
}
func (t *Tree) cInner(n Node, depth int) Node {
//u.Debugf("%d t.cInner: %v", depth, t.Cur())
for {
//u.Debugf("cInner: tok: cur=%v peek=%v n=%v", t.Cur(), t.Peek(), n.StringAST())
switch cur := t.Cur(); cur.T {
case lex.TokenEqual, lex.TokenEqualEqual, lex.TokenNE, lex.TokenGT, lex.TokenGE,
lex.TokenLE, lex.TokenLT, lex.TokenLike:
t.Next()
n = NewBinaryNode(cur, n, t.P(depth+1))
case lex.TokenBetween:
// weird syntax: BETWEEN x AND y AND is ignored essentially
t.Next()
n2 := t.P(depth)
t.expect(lex.TokenLogicAnd, "input")
t.Next()
n = NewTriNode(cur, n, n2, t.P(depth+1))
case lex.TokenIN:
t.Next()
// This isn't really a Binary? It is an array or
// other type of native data type?
//n = NewSet(cur, n, t.Set(depth+1))
return t.MultiArg(n, cur, depth)
case lex.TokenNull:
t.Next()
return NewNull(cur)
default:
return n
}
}
}
func (t *Tree) P(depth int) Node {
//u.Debugf("%d t.P: %v", depth, t.Cur())
n := t.M(depth)
//u.Debugf("%d t.P: AFTER %v", depth, t.Cur())
for {
switch cur := t.Cur(); cur.T {
case lex.TokenPlus, lex.TokenMinus:
t.Next()
n = NewBinaryNode(cur, n, t.M(depth+1))
default:
return n
}
}
}
func (t *Tree) M(depth int) Node {
//u.Debugf("%d t.M: %v", depth, t.Cur())
n := t.F(depth)
//u.Debugf("%d t.M after: %v %v", depth, t.Cur(), n)
for {
switch cur := t.Cur(); cur.T {
case lex.TokenStar, lex.TokenMultiply, lex.TokenDivide, lex.TokenModulus:
t.Next()
n = NewBinaryNode(cur, n, t.F(depth+1))
default:
return n
}
}
}
func (t *Tree) MultiArg(first Node, op lex.Token, depth int) Node {
//u.Debugf("%d t.MultiArg: %v", depth, t.Cur())
t.expect(lex.TokenLeftParenthesis, "input")
t.Next() // Consume Left Paren
//u.Debugf("%d t.MultiArg after: %v ", depth, t.Cur())
multiNode := NewMultiArgNode(op)
multiNode.Append(first)
for {
//u.Debugf("MultiArg iteration: %v", t.Cur())
switch cur := t.Cur(); cur.T {
case lex.TokenRightParenthesis:
t.Next() // Consume the Paren
return multiNode
case lex.TokenComma:
t.Next()
default:
n := t.O(depth)
if n != nil {
multiNode.Append(n)
} else {
u.Warnf("invalid? %v", t.Cur())
return multiNode
}
}
}
}
func (t *Tree) F(depth int) Node {
//u.Debugf("%d t.F: %v", depth, t.Cur())
switch cur := t.Cur(); cur.T {
case lex.TokenUdfExpr:
return t.v(depth)
case lex.TokenInteger, lex.TokenFloat:
return t.v(depth)
case lex.TokenIdentity:
return t.v(depth)
case lex.TokenValue:
return t.v(depth)
case lex.TokenNull:
return t.v(depth)
case lex.TokenStar:
// in special situations: count(*) ??
return t.v(depth)
case lex.TokenNegate, lex.TokenMinus:
//u.Infof("doing urnary node on negate: %v", cur)
t.Next()
return NewUnary(cur, t.F(depth+1))
case lex.TokenIs:
nxt := t.Next()
//u.Infof("doing urnary node on negate: %v nxt=%v", cur, nxt)
if nxt.T == lex.TokenNegate {
return NewUnary(cur, t.F(depth+1))
}
return NewUnary(cur, t.F(depth+1))
case lex.TokenLeftParenthesis:
// I don't think this is right, parens should be higher up
// in precedence stack, very top?
t.Next() // Consume the Paren
n := t.O(depth + 1)
if bn, ok := n.(*BinaryNode); ok {
bn.Paren = true
}
//u.Debugf("expects right paren? cur=%v p=%v", t.Cur(), t.Peek())
t.expect(lex.TokenRightParenthesis, "input")
t.Next()
return n
default:
u.Warnf("unexpected? %v", cur)
//t.unexpected(cur, "input")
panic(fmt.Sprintf("unexpected token %v ", cur))
}
return nil
}
func (t *Tree) v(depth int) Node {
//u.Debugf("depth:%d t.v: cur(): %v peek:%v", depth, t.Cur(), t.Peek())
switch cur := t.Cur(); cur.T {
case lex.TokenInteger, lex.TokenFloat:
n, err := NewNumber(Pos(cur.Pos), cur.V)
if err != nil {
t.error(err)
}
t.Next()
return n
case lex.TokenValue:
n := NewStringNode(Pos(cur.Pos), cur.V)
t.Next()
return n
case lex.TokenIdentity:
n := NewIdentityNode(&cur)
t.Next()
return n
case lex.TokenNull:
t.Next()
return NewNull(cur)
case lex.TokenStar:
n := NewStringNode(Pos(cur.Pos), cur.V)
t.Next()
return n
case lex.TokenUdfExpr:
//u.Debugf("depth:%v t.v calling Func()?: %v", depth, cur)
t.Next() // consume Function Name
//u.Debugf("func? %v", funcTok)
return t.Func(depth, cur)
case lex.TokenLeftParenthesis:
// I don't think this is right, it should be higher up
// in precedence stack, very top?
t.Next()
n := t.O(depth + 1)
if bn, ok := n.(*BinaryNode); ok {
bn.Paren = true
}
//u.Debugf("cur?%v n %v ", t.Cur(), n.StringAST())
t.Next()
t.expect(lex.TokenRightParenthesis, "input")
return n
default:
if t.ClauseEnd() {
return nil
}
//u.Warnf("Unexpected?: %v", cur)
t.unexpected(cur, "input")
}
t.Backup()
return nil
}
func (t *Tree) Func(depth int, funcTok lex.Token) (fn *FuncNode) {
//u.Debugf("Func tok: %v cur:%v peek:%v", funcTok.V, t.Cur().V, t.Peek().V)
if t.Cur().T != lex.TokenLeftParenthesis {
panic(fmt.Sprintf("must have left paren on function: %v", t.Peek()))
}
var node Node
var tok lex.Token
funcImpl, ok := t.getFunction(funcTok.V)
if !ok {
if t.runCheck {
//u.Warnf("non func? %v", funcTok.V)
t.errorf("non existent function %s", funcTok.V)
} else {
// if we aren't testing for validity, make a "fake" func
// we may not be using vm, just ast
//u.Warnf("non func? %v", funcTok.V)
funcImpl = Func{Name: funcTok.V}
}
}
fn = NewFuncNode(Pos(funcTok.Pos), funcTok.V, funcImpl)
//u.Debugf("%d t.Func()?: %v %v", depth, t.Cur(), t.Peek())
//t.Next() // step forward to hopefully left paren
t.expect(lex.TokenLeftParenthesis, "func")
for {
node = nil
t.Next() // Are we sure we consume?
//u.Infof("%d pre loop token?: cur=%v peek=%v", depth, t.Cur(), t.Peek())
switch firstToken := t.Cur(); firstToken.T {
case lex.TokenRightParenthesis:
t.Next()
if node != nil {
fn.append(node)
}
//u.Warnf(" right paren? ")
return
case lex.TokenEOF, lex.TokenEOS, lex.TokenFrom:
//u.Warnf("return: %v", t.Cur())
if node != nil {
fn.append(node)
}
return
default:
//u.Debugf("%v getting node? t.Func()?: %v", depth, firstToken)
node = t.O(depth + 1)
}
tok = t.Cur()
//u.Infof("%d Func() pt2 consumed token?: %v", depth, tok)
switch tok.T {
case lex.TokenComma:
if node != nil {
fn.append(node)
}
// continue
case lex.TokenRightParenthesis:
if node != nil {
fn.append(node)
}
t.Next()
//u.Warnf("found right paren %v", t.Cur())
return
case lex.TokenEOF, lex.TokenEOS, lex.TokenFrom, lex.TokenAs:
if node != nil {
fn.append(node)
}
t.Next()
//u.Debugf("return: %v", t.Cur())
return
case lex.TokenEqual, lex.TokenEqualEqual, lex.TokenNE, lex.TokenGT, lex.TokenGE,
lex.TokenLE, lex.TokenLT, lex.TokenStar, lex.TokenMultiply, lex.TokenDivide:
// this func arg is an expression
// toint(str_item * 5)
//t.Backup()
//u.Debugf("hmmmmm: %v cu=%v", tok, t.Cur())
node = t.O(depth + 1)
if node != nil {
fn.append(node)
}
default:
t.unexpected(tok, "func")
}
}
}
// get Function from Global
func (t *Tree) getFunction(name string) (v Func, ok bool) {
if v, ok = funcs[strings.ToLower(name)]; ok {
return
}
return
}
func (t *Tree) String() string {
return t.Root.String()
}