-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser.go
443 lines (422 loc) · 12.3 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
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
package mexpr
import (
"math"
"strconv"
)
// NodeType defines the type of the abstract syntax tree node.
type NodeType uint8
// Possible node types
const (
NodeUnknown NodeType = iota
NodeIdentifier
NodeLiteral
NodeAdd
NodeSubtract
NodeMultiply
NodeDivide
NodeModulus
NodePower
NodeEqual
NodeNotEqual
NodeLessThan
NodeLessThanEqual
NodeGreaterThan
NodeGreaterThanEqual
NodeAnd
NodeOr
NodeNot
NodeFieldSelect
NodeArrayIndex
NodeSlice
NodeSign
NodeIn
NodeContains
NodeStartsWith
NodeEndsWith
NodeBefore
NodeAfter
NodeWhere
)
// Node is a unit of the binary tree that makes up the abstract syntax tree.
type Node struct {
Type NodeType
Length uint8
Offset uint16
Left *Node
Right *Node
Value interface{}
}
// String converts the node to a string representation (basically the node name
// or the node's value for identifiers/literals).
func (n Node) String() string {
switch n.Type {
case NodeIdentifier, NodeLiteral:
return toString(n.Value)
case NodeAdd:
return "+"
case NodeSubtract:
return "-"
case NodeMultiply:
return "*"
case NodeDivide:
return "/"
case NodeModulus:
return "%"
case NodePower:
return "^"
case NodeEqual:
return "=="
case NodeNotEqual:
return "!="
case NodeLessThan:
return "<"
case NodeLessThanEqual:
return "<="
case NodeGreaterThan:
return ">"
case NodeGreaterThanEqual:
return ">="
case NodeAnd:
return "and"
case NodeOr:
return "or"
case NodeNot:
return "not"
case NodeFieldSelect:
return "."
case NodeArrayIndex:
return "[]"
case NodeSlice:
return ":"
case NodeIn:
return "in"
case NodeContains:
return "contains"
case NodeStartsWith:
return "startsWith"
case NodeEndsWith:
return "endsWith"
case NodeBefore:
return "before"
case NodeAfter:
return "after"
case NodeWhere:
return "where"
}
return ""
}
// Dot returns a graphviz-compatible dot output, which can be used to render
// the parse tree at e.g. https://dreampuf.github.io/GraphvizOnline/ or
// locally. You must wrap the output with `graph G {` and `}`.
func (n Node) Dot(prefix string) string {
value := "\"" + prefix + n.String() + "\" [label=\"" + n.String() + "\"];\n"
if n.Left != nil {
value += "\"" + prefix + n.String() + "\" -- \"" + prefix + "l" + n.Left.String() + "\"\n"
value += n.Left.Dot(prefix+"l") + "\n"
}
if n.Right != nil {
value += "\"" + prefix + n.String() + "\" -- \"" + prefix + "r" + n.Right.String() + "\"\n"
value += n.Right.Dot(prefix+"r") + "\n"
}
return value
}
// bindingPowers for different tokens. Not listed means zero. The higher the
// number, the higher the token is in the order of operations.
var bindingPowers = map[TokenType]int{
TokenOr: 1,
TokenAnd: 2,
TokenWhere: 3,
TokenStringCompare: 4,
TokenComparison: 5,
TokenSlice: 5,
TokenAddSub: 10,
TokenMulDiv: 15,
TokenNot: 40,
TokenDot: 45,
TokenPower: 50,
TokenLeftBracket: 60,
TokenLeftParen: 70,
}
// precomputeLiterals takes two `NodeLiteral` nodes and a math operation and
// generates a single literal node for the resutl. This prevents the interpreter
// from needing to re-compute the value each time.
func precomputeLiterals(offset uint16, nodeType NodeType, left, right *Node) (*Node, Error) {
leftValue, err := toNumber(left, left.Value)
if err != nil {
return nil, err
}
rightValue, err := toNumber(right, right.Value)
if err != nil {
return nil, err
}
l := left.Length + right.Length
switch nodeType {
case NodeAdd:
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: leftValue + rightValue}, nil
case NodeSubtract:
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: leftValue - rightValue}, nil
case NodeMultiply:
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: leftValue * rightValue}, nil
case NodeDivide:
if rightValue == 0 {
return nil, NewError(offset, 1, "cannot divide by zero")
}
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: leftValue / rightValue}, nil
case NodeModulus:
if int(rightValue) == 0 {
return nil, NewError(offset, 1, "cannot divide by zero")
}
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: float64(int(leftValue) % int(rightValue))}, nil
case NodePower:
return &Node{Type: NodeLiteral, Offset: offset, Length: l, Value: math.Pow(leftValue, rightValue)}, nil
}
return nil, NewError(offset, 1, "cannot precompute unknown operator")
}
// Parser takes a lexer and parses its tokens into an abstract syntax tree.
type Parser interface {
// Parse the expression and return the root node.
Parse() (*Node, Error)
}
// NewParser creates a new parser that uses the given lexer to get and process
// tokens into an abstract syntax tree.
func NewParser(lexer Lexer) Parser {
return &parser{
lexer: lexer,
}
}
// parser is an implementation of a Pratt or top-down operator precedence parser
type parser struct {
lexer Lexer
token *Token
}
func (p *parser) advance() Error {
t, err := p.lexer.Next()
if err != nil {
return err
}
p.token = t
return nil
}
func (p *parser) parse(bindingPower int) (*Node, Error) {
leftToken := *p.token
if err := p.advance(); err != nil {
return nil, err
}
leftNode, err := p.nud(&leftToken)
if err != nil {
return nil, err
}
currentToken := *p.token
for bindingPower < bindingPowers[currentToken.Type] {
if leftNode == nil {
return nil, nil
}
if err := p.advance(); err != nil {
return nil, err
}
leftNode, err = p.led(¤tToken, leftNode)
if err != nil {
return nil, err
}
currentToken = *p.token
}
return leftNode, nil
}
// ensure the current token is `typ`, returning the `result` unless `err` is
// set or some other error occurs. Advances past the expected token type.
func (p *parser) ensure(result *Node, err Error, typ TokenType) (*Node, Error) {
if err != nil {
return nil, err
}
if p.token.Type == typ {
if err := p.advance(); err != nil {
return nil, err
}
return result, nil
}
extra := ""
if typ == TokenEOF && p.token.Type == TokenIdentifier {
switch p.token.Value {
case "startswith", "beginswith", "beginsWith", "hasprefix", "hasPrefix":
extra = " (did you mean `startsWith`?)"
case "endswith", "hassuffix", "hasSuffix":
extra = " (did you mean `endsWith`?)"
case "contains":
extra = " (did you mean `in`?)"
}
}
return nil, NewError(p.token.Offset, p.token.Length, "expected %s but found %s%s", typ, p.token.Type, extra)
}
// nud: null denotation. These nodes have no left context and only
// consume to the right. Examples: identifiers, numbers, unary operators like
// minus.
func (p *parser) nud(t *Token) (*Node, Error) {
switch t.Type {
case TokenIdentifier:
return &Node{Type: NodeIdentifier, Value: t.Value, Offset: t.Offset, Length: t.Length}, nil
case TokenNumber:
f, err := strconv.ParseFloat(t.Value, 64)
if err != nil {
return nil, NewError(p.token.Offset, p.token.Length, err.Error())
}
return &Node{Type: NodeLiteral, Value: f, Offset: t.Offset, Length: t.Length}, nil
case TokenString:
return &Node{Type: NodeLiteral, Value: t.Value, Offset: t.Offset, Length: t.Length}, nil
case TokenLeftParen:
result, err := p.parse(0)
return p.ensure(result, err, TokenRightParen)
case TokenNot:
offset := t.Offset
result, err := p.parse(bindingPowers[t.Type])
if err != nil {
return nil, err
}
return &Node{Type: NodeNot, Offset: offset, Length: uint8(t.Offset + uint16(t.Length) - offset), Right: result}, nil
case TokenAddSub:
value := t.Value
offset := t.Offset
result, err := p.parse(bindingPowers[t.Type])
if err != nil {
return nil, err
}
return &Node{Type: NodeSign, Value: value, Offset: offset, Length: uint8(t.Offset + uint16(t.Length) - offset), Right: result}, nil
case TokenSlice:
offset := t.Offset
result, err := p.parse(bindingPowers[t.Type])
if err != nil {
return nil, err
}
// Create a dummy left node with value 0, the start of the slice. This also
// sets the parent node's value to a pre-allocated list of [0, 0] which is
// used later by the interpreter. It prevents additional allocations.
return &Node{Type: NodeSlice, Offset: offset, Length: uint8(t.Offset + uint16(t.Length) - offset), Left: &Node{Type: NodeLiteral, Value: 0.0, Offset: offset}, Right: result, Value: []interface{}{0.0, 0.0}}, nil
case TokenRightParen:
return nil, NewError(t.Offset, t.Length, "unexpected right-paren")
case TokenRightBracket:
return nil, NewError(t.Offset, t.Length, "unexpected right-bracket")
case TokenEOF:
return nil, NewError(t.Offset, t.Length, "incomplete expression, EOF found")
}
return nil, nil
}
// newNodeParseRight creates a new node with the right tree set to the
// output of recursively parsing until a lower binding power is encountered.
func (p *parser) newNodeParseRight(left *Node, t *Token, typ NodeType, bindingPower int) (*Node, Error) {
offset := t.Offset
right, err := p.parse(bindingPower)
if err != nil {
return nil, err
}
if right == nil {
return nil, NewError(t.Offset, t.Length, "missing right operand")
}
return &Node{Type: typ, Offset: offset, Length: uint8(p.token.Offset + uint16(p.token.Length) - offset), Left: left, Right: right}, nil
}
// led: left denotation. These tokens produce nodes that operate on two operands
// a left and a right. Examples: addition, multiplication, etc.
func (p *parser) led(t *Token, n *Node) (*Node, Error) {
switch t.Type {
case TokenAddSub, TokenMulDiv, TokenPower:
var nodeType NodeType
switch t.Value[0] {
case '+':
nodeType = NodeAdd
case '-':
nodeType = NodeSubtract
case '*':
nodeType = NodeMultiply
case '/':
nodeType = NodeDivide
case '%':
nodeType = NodeModulus
case '^':
nodeType = NodePower
}
offset := t.Offset
binding := bindingPowers[t.Type]
if t.Type == TokenPower {
// Power operations should be right-associative, so we lower the binding
// power slightly so it prefers going right.
binding--
}
right, err := p.parse(binding)
if err != nil {
return nil, err
}
if right == nil {
return nil, NewError(t.Offset, t.Length, "missing right operand")
}
if n.Type == NodeLiteral && right.Type == NodeLiteral {
if !(isString(n.Value) || isString(right.Value)) {
return precomputeLiterals(offset, nodeType, n, right)
}
}
return &Node{Type: nodeType, Offset: offset, Length: uint8(t.Offset + uint16(t.Length) - offset), Left: n, Right: right, Value: 0.0}, nil
case TokenComparison:
var nodeType NodeType
switch t.Value {
case "==":
nodeType = NodeEqual
case "!=":
nodeType = NodeNotEqual
case "<":
nodeType = NodeLessThan
case "<=":
nodeType = NodeLessThanEqual
case ">":
nodeType = NodeGreaterThan
case ">=":
nodeType = NodeGreaterThanEqual
}
return p.newNodeParseRight(n, t, nodeType, bindingPowers[t.Type])
case TokenAnd:
return p.newNodeParseRight(n, t, NodeAnd, bindingPowers[t.Type])
case TokenOr:
return p.newNodeParseRight(n, t, NodeOr, bindingPowers[t.Type])
case TokenStringCompare:
var nodeType NodeType
switch t.Value {
case "in":
nodeType = NodeIn
case "contains":
nodeType = NodeContains
case "startsWith":
nodeType = NodeStartsWith
case "endsWith":
nodeType = NodeEndsWith
case "before":
nodeType = NodeBefore
case "after":
nodeType = NodeAfter
}
return p.newNodeParseRight(n, t, nodeType, bindingPowers[t.Type])
case TokenWhere:
return p.newNodeParseRight(n, t, NodeWhere, bindingPowers[t.Type])
case TokenDot:
return p.newNodeParseRight(n, t, NodeFieldSelect, bindingPowers[t.Type])
case TokenLeftBracket:
n, err := p.newNodeParseRight(n, t, NodeArrayIndex, 0)
return p.ensure(n, err, TokenRightBracket)
case TokenSlice:
if p.token.Type == TokenRightBracket {
// This sets the parent node's value to a pre-allocated list of [0, 0]
// which is used later by the interpreter. It prevents additional
// allocations.
return &Node{Type: NodeSlice, Offset: t.Offset, Length: t.Length, Left: n, Right: &Node{Type: NodeLiteral, Offset: t.Offset, Value: -1.0}, Value: []interface{}{0.0, 0.0}}, nil
}
nn, err := p.newNodeParseRight(n, t, NodeSlice, bindingPowers[t.Type])
if err != nil {
return nil, err
}
nn.Value = []interface{}{0.0, 0.0}
return nn, nil
}
return nil, NewError(t.Offset, t.Length, "unexpected token %s", t.Type)
}
func (p *parser) Parse() (*Node, Error) {
if err := p.advance(); err != nil {
return nil, err
}
n, err := p.parse(0)
return p.ensure(n, err, TokenEOF)
}