-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
267 lines (224 loc) · 7.14 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
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
package skl
import (
"fmt"
"io"
"strconv"
"strings"
"github.com/eliquious/lexer"
)
const (
// DateFormat represents the format for date literals.
DateFormat = "2006-01-02"
// DateTimeFormat represents the format for date time literals.
DateTimeFormat = "2006-01-02 15:04:05.999999"
)
// Parser represents an InfluxQL parser.
type Parser struct {
s *bufScanner
}
// NewParser returns a new instance of Parser.
func NewParser(r io.Reader) *Parser {
return &Parser{s: newBufScanner(r)}
}
// ParseStatement parses a statement string and returns its AST representation.
func ParseStatement(s string) (Statement, error) {
return NewParser(strings.NewReader(s)).ParseStatement()
}
// ParseStatement parses a string and returns a Statement AST object.
func (p *Parser) ParseStatement() (Statement, error) {
// Inspect the first token.
tok, pos, lit := p.scanIgnoreWhitespace()
switch tok {
case USE:
return p.parseUseStatement()
case CREATE:
return p.parseCreateStatement()
case DROP:
return p.parseDropStatement()
case SHOW:
return p.parseShowStatement()
default:
return nil, newParseError(tokstr(tok, lit), []string{"USE", "CREATE", "SHOW", "DROP"}, pos)
}
}
// parseUseStatement parses a string and returns a UseStatement.
// This function assumes the "USE" token has already been consumed.
func (p *Parser) parseUseStatement() (*UseStatement, error) {
stmt := &UseStatement{}
// Parse the name of the namespace to be used
lit, err := p.parseNamespace()
if err != nil {
return nil, err
}
stmt.name = lit
return stmt, nil
}
// parseCreateStatement parses a string and returns a Statement AST object.
// This function assumes the "CREATE" token has already been consumed.
func (p *Parser) parseCreateStatement() (Statement, error) {
// Inspect the first token.
tok, pos, lit := p.scanIgnoreWhitespace()
switch tok {
case NAMESPACE:
return p.parseCreateNamespaceStatement()
default:
return nil, newParseError(tokstr(tok, lit), []string{"NAMESPACE"}, pos)
}
}
// parseCreateNamespaceStat5ement parses a string and returns a CreateNamespaceStatement.
// This function assumes the "CREATE" token has already been consumed.
func (p *Parser) parseCreateNamespaceStatement() (*CreateNamespaceStatement, error) {
stmt := &CreateNamespaceStatement{}
// Parse the name of the namespace to be used
lit, err := p.parseNamespace()
if err != nil {
return nil, err
}
stmt.name = lit
return stmt, nil
}
// parseDropStatement parses a string and returns a Statement AST object.
// This function assumes the "DROP" token has already been consumed.
func (p *Parser) parseDropStatement() (Statement, error) {
// Inspect the first token.
tok, pos, lit := p.scanIgnoreWhitespace()
switch tok {
case NAMESPACE:
return p.parseDropNamespaceStatement()
default:
return nil, newParseError(tokstr(tok, lit), []string{"NAMESPACE"}, pos)
}
}
// parseDropNamespaceStat5ement parses a string and returns a DropNamespaceStatement.
// This function assumes the "DROP" token has already been consumed.
func (p *Parser) parseDropNamespaceStatement() (*DropNamespaceStatement, error) {
stmt := &DropNamespaceStatement{}
// Parse the name of the namespace to be used
lit, err := p.parseNamespace()
if err != nil {
return nil, err
}
stmt.name = lit
return stmt, nil
}
// parseShowStatement parses a string and returns a Statement AST object.
// This function assumes the "SHOW" token has already been consumed.
func (p *Parser) parseShowStatement() (Statement, error) {
// Inspect the first token.
tok, pos, lit := p.scanIgnoreWhitespace()
switch tok {
case NAMESPACES:
return &ShowNamespacesStatement{}, nil
default:
return nil, newParseError(tokstr(tok, lit), []string{"NAMESPACES"}, pos)
}
}
// parseNamespace returns a namespace title or an error
func (p *Parser) parseNamespace() (string, error) {
var namespace string
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != lexer.IDENT {
return "", newParseError(tokstr(tok, lit), []string{"namespace"}, pos)
}
namespace = lit
// Scan entire namespace
// Namespaces are a period delimited list of identifiers
var endPeriod bool
for {
tok, pos, lit = p.scan()
if tok == lexer.DOT {
namespace += "."
endPeriod = true
} else if tok == lexer.IDENT {
namespace += lit
endPeriod = false
} else {
break
}
}
// remove last token
p.unscan()
// Namespaces can't end on a period
if endPeriod {
return "", newParseError(tokstr(tok, lit), []string{"identifier"}, pos)
}
return namespace, nil
}
// parserString parses a string.
func (p *Parser) parseString() (string, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != STRING {
return "", newParseError(tokstr(tok, lit), []string{"string"}, pos)
}
return lit, nil
}
// parseIdent parses an identifier.
func (p *Parser) parseIdent() (string, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != lexer.IDENT {
return "", newParseError(tokstr(tok, lit), []string{"identifier"}, pos)
}
return lit, nil
}
// parseInt parses a string and returns an integer literal.
func (p *Parser) parseInt(min, max int) (int, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != lexer.NUMBER {
return 0, newParseError(tokstr(tok, lit), []string{"number"}, pos)
}
// Return an error if the number has a fractional part.
if strings.Contains(lit, ".") {
return 0, &ParseError{Message: "number must be an integer", Pos: pos}
}
// Convert string to int.
n, err := strconv.Atoi(lit)
if err != nil {
return 0, &ParseError{Message: err.Error(), Pos: pos}
} else if min > n || n > max {
return 0, &ParseError{
Message: fmt.Sprintf("invalid value %d: must be %d <= n <= %d", n, min, max),
Pos: pos,
}
}
return n, nil
}
// parseUInt32 parses a string and returns a 32-bit unsigned integer literal.
func (p *Parser) parseUInt32() (uint32, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != lexer.NUMBER {
return 0, newParseError(tokstr(tok, lit), []string{"number"}, pos)
}
// Convert string to unsigned 32-bit integer
n, err := strconv.ParseUint(lit, 10, 32)
if err != nil {
return 0, &ParseError{Message: err.Error(), Pos: pos}
}
return uint32(n), nil
}
// parseUInt64 parses a string and returns a 64-bit unsigned integer literal.
func (p *Parser) parseUInt64() (uint64, error) {
tok, pos, lit := p.scanIgnoreWhitespace()
if tok != lexer.NUMBER {
return 0, newParseError(tokstr(tok, lit), []string{"number"}, pos)
}
// Convert string to unsigned 64-bit integer
n, err := strconv.ParseUint(lit, 10, 64)
if err != nil {
return 0, &ParseError{Message: err.Error(), Pos: pos}
}
return uint64(n), nil
}
// scan returns the next token from the underlying scanner.
func (p *Parser) scan() (tok lexer.Token, pos lexer.Pos, lit string) { return p.s.Scan() }
// unscan pushes the previously read token back onto the buffer.
func (p *Parser) unscan() { p.s.Unscan() }
// peekRune returns the next rune that would be read by the scanner.
func (p *Parser) peekRune() rune { return p.s.s.Peek() }
// scanIgnoreWhitespace scans the next non-whitespace token.
func (p *Parser) scanIgnoreWhitespace() (tok lexer.Token, pos lexer.Pos, lit string) {
tok, pos, lit = p.scan()
if tok == lexer.WS {
tok, pos, lit = p.scan()
}
return
}