forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
231 lines (215 loc) · 5.18 KB
/
parser.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
package predicate
import (
"fmt"
"strings"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxql"
)
// a fixed buffer ring
type buffer [3]struct {
tok influxql.Token // last read token
pos influxql.Pos // last read pos
lit string // last read literal
}
// parser of the predicate will connvert
// such a statement `(a = "a" or b!="b") and c ! =~/efg/`
// to the predicate node
type parser struct {
sc *influxql.Scanner
i int // buffer index
n int // buffer size
openParen int
buf buffer
}
// scan returns the next token from the underlying scanner.
// If a token has been unscanned then read that instead.
func (p *parser) scan() (tok influxql.Token, pos influxql.Pos, lit string) {
// If we have a token on the buffer, then return it.
if p.n > 0 {
p.n--
return p.curr()
}
// Move buffer position forward and save the token.
p.i = (p.i + 1) % len(p.buf)
buf := &p.buf[p.i]
buf.tok, buf.pos, buf.lit = p.sc.Scan()
return p.curr()
}
func (p *parser) unscan() {
p.n++
}
// curr returns the last read token.
func (p *parser) curr() (tok influxql.Token, pos influxql.Pos, lit string) {
buf := &p.buf[(p.i-p.n+len(p.buf))%len(p.buf)]
return buf.tok, buf.pos, buf.lit
}
// scanIgnoreWhitespace scans the next non-whitespace token.
func (p *parser) scanIgnoreWhitespace() (tok influxql.Token, pos influxql.Pos, lit string) {
tok, pos, lit = p.scan()
if tok == influxql.WS {
tok, pos, lit = p.scan()
}
return
}
// Parse the predicate statement.
func Parse(sts string) (n Node, err error) {
if sts == "" {
return nil, nil
}
p := new(parser)
p.sc = influxql.NewScanner(strings.NewReader(sts))
return p.parseLogicalNode()
}
func (p *parser) parseLogicalNode() (Node, error) {
n := new(LogicalNode)
for {
tok, pos, _ := p.scanIgnoreWhitespace()
switch tok {
case influxql.NUMBER, influxql.INTEGER, influxql.NAME, influxql.IDENT:
p.unscan()
tr, err := p.parseTagRuleNode()
if err != nil {
return *n, err
}
if n.Children[0] == nil {
n.Children[0] = tr
} else {
n.Children[1] = tr
}
case influxql.AND:
n.Operator = LogicalAnd
if n.Children[1] == nil {
continue
}
var n1 Node
var err error
if tokNext := p.peekTok(); tokNext == influxql.LPAREN {
n1, err = p.parseLogicalNode()
} else {
n1, err = p.parseTagRuleNode()
}
if err != nil {
return *n, err
}
n = &LogicalNode{
Children: [2]Node{*n, n1},
Operator: LogicalAnd,
}
case influxql.OR:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("the logical operator OR is not supported yet at position %d", pos.Char),
}
case influxql.LPAREN:
p.openParen++
currParen := p.openParen
n1, err := p.parseLogicalNode()
if err != nil {
return *n, err
}
if p.openParen != currParen-1 {
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("extra ( seen"),
}
}
if n.Children[0] == nil {
n.Children[0] = n1
} else {
n.Children[1] = n1
}
case influxql.RPAREN:
p.openParen--
fallthrough
case influxql.EOF:
if p.openParen < 0 {
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("extra ) seen"),
}
}
if n.Children[1] == nil {
return n.Children[0], nil
}
return *n, nil
default:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bad logical expression, at position %d", pos.Char),
}
}
}
}
func (p *parser) parseTagRuleNode() (TagRuleNode, error) {
n := new(TagRuleNode)
// scan the key
tok, pos, lit := p.scanIgnoreWhitespace()
switch tok {
case influxql.IDENT:
n.Key = lit
case influxql.NAME:
n.Key = "name"
default:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bad tag key, at position %d", pos.Char),
}
}
tok, pos, _ = p.scanIgnoreWhitespace()
switch tok {
case influxql.EQ:
n.Operator = influxdb.Equal
goto scanRegularTagValue
case influxql.NEQ:
n.Operator = influxdb.NotEqual
goto scanRegularTagValue
case influxql.EQREGEX:
fallthrough
case influxql.NEQREGEX:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("operator: %q at position: %d is not supported yet", tok.String(), pos.Char),
}
default:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("invalid operator %q at position: %d", tok.String(), pos.Char),
}
}
// scan the value
scanRegularTagValue:
tok, pos, lit = p.scanIgnoreWhitespace()
switch tok {
case influxql.SUB:
n.Value = "-"
goto scanRegularTagValue
case influxql.IDENT:
fallthrough
case influxql.DURATIONVAL:
fallthrough
case influxql.NUMBER:
fallthrough
case influxql.INTEGER:
n.Value += lit
return *n, nil
case influxql.TRUE:
n.Value = "true"
return *n, nil
case influxql.FALSE:
n.Value = "false"
return *n, nil
default:
return *n, &influxdb.Error{
Code: influxdb.EInvalid,
Msg: fmt.Sprintf("bad tag value: %q, at position %d", lit, pos.Char),
}
}
}
// peekRune returns the next rune that would be read by the scanner.
func (p *parser) peekTok() influxql.Token {
tok, _, _ := p.scanIgnoreWhitespace()
if tok != influxql.EOF {
p.unscan()
}
return tok
}