forked from crestonbunch/godata
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
382 lines (328 loc) · 8.67 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
package godata
import (
"regexp"
"strings"
)
const (
OpAssociationLeft int = iota
OpAssociationRight
OpAssociationNone
)
const (
NodeTypeLiteral int = iota
NodeTypeOp
NodeTypeFunc
)
type Tokenizer struct {
TokenMatchers []*TokenMatcher
IgnoreMatchers []*TokenMatcher
}
type TokenMatcher struct {
Pattern string
Re *regexp.Regexp
Token int
}
type Token struct {
Value string
Type int
// Holds information about the semantic meaning of this token taken from the
// context of the GoDataService.
SemanticType int
SemanticReference interface{}
}
func (t *Tokenizer) Add(pattern string, token int) {
rxp := regexp.MustCompile(pattern)
matcher := &TokenMatcher{pattern, rxp, token}
t.TokenMatchers = append(t.TokenMatchers, matcher)
}
func (t *Tokenizer) Ignore(pattern string, token int) {
rxp := regexp.MustCompile(pattern)
matcher := &TokenMatcher{pattern, rxp, token}
t.IgnoreMatchers = append(t.IgnoreMatchers, matcher)
}
func (t *Tokenizer) TokenizeBytes(target []byte) ([]*Token, error) {
result := make([]*Token, 0)
match := true // false when no match is found
for len(target) > 0 && match {
match = false
for _, m := range t.TokenMatchers {
token := m.Re.Find(target)
if len(token) > 0 {
//If a filter is found but next char is not "(" than don't treat it as filter
if m.Token == FilterTokenFunc && string(target[len(token):len(token)+1]) != "(" {
continue
}
parsed := Token{Value: string(token), Type: m.Token}
result = append(result, &parsed)
target = target[len(token):] // remove the token from the input
match = true
break
}
}
for _, m := range t.IgnoreMatchers {
token := m.Re.Find(target)
if len(token) > 0 {
match = true
target = target[len(token):] // remove the token from the input
break
}
}
}
if len(target) > 0 && !match {
return result, BadRequestError("No matching token for " + string(target))
}
return result, nil
}
func (t *Tokenizer) Tokenize(target string) ([]*Token, error) {
return t.TokenizeBytes([]byte(target))
}
type Parser struct {
// Map from string inputs to operator types
Operators map[string]*Operator
// Map from string inputs to function types
Functions map[string]*Function
}
type Operator struct {
Token string
// Whether the operator is left/right/or not associative
Association int
// The number of operands this operator operates on
Operands int
// Rank of precedence
Precedence int
}
type Function struct {
Token string
// The number of parameters this function accepts
Params int
}
type ParseNode struct {
Token *Token
Parent *ParseNode
Children []*ParseNode
}
func EmptyParser() *Parser {
return &Parser{make(map[string]*Operator, 0), make(map[string]*Function)}
}
// Add an operator to the language. Provide the token, a precedence, and
// whether the operator is left, right, or not associative.
func (p *Parser) DefineOperator(token string, operands, assoc, precedence int) {
p.Operators[token] = &Operator{token, assoc, operands, precedence}
}
// Add a function to the language
func (p *Parser) DefineFunction(token string, params int) {
p.Functions[token] = &Function{token, params}
}
// Parse the input string of tokens using the given definitions of operators
// and functions. (Everything else is assumed to be a literal.) Uses the
// Shunting-Yard algorithm.
func (p *Parser) InfixToPostfix(tokens []*Token) (*tokenQueue, error) {
queue := tokenQueue{}
stack := tokenStack{}
for len(tokens) > 0 {
token := tokens[0]
tokens = tokens[1:]
if token.Type == FilterTokenFunc {
// push functions onto the stack if no tokens are left or the next token is a "("
stack.Push(token)
} else if token.Value == "," {
// function parameter separator, pop off stack until we see a "("
for !stack.Empty() && stack.Peek().Value != "(" {
queue.Enqueue(stack.Pop())
}
// there was an error parsing
if stack.Empty() {
return nil, BadRequestError("Parse error")
}
} else if o1, ok := p.Operators[token.Value]; ok {
// push operators onto stack according to precedence
if !stack.Empty() {
for o2, ok := p.Operators[stack.Peek().Value]; ok &&
(o1.Association == OpAssociationLeft && o1.Precedence <= o2.Precedence) ||
(o1.Association == OpAssociationRight && o1.Precedence < o2.Precedence); {
queue.Enqueue(stack.Pop())
if stack.Empty() {
break
}
o2, ok = p.Operators[stack.Peek().Value]
}
}
stack.Push(token)
} else if token.Value == "(" {
// push open parens onto the stack
stack.Push(token)
} else if token.Value == ")" {
// if we find a close paren, pop things off the stack
for !stack.Empty() && stack.Peek().Value != "(" {
queue.Enqueue(stack.Pop())
}
// there was an error parsing
if stack.Empty() {
return nil, BadRequestError("Parse error. Mismatched parenthesis.")
}
// pop off open paren
stack.Pop()
// if next token is a function, move it to the queue
if !stack.Empty() {
//if _, ok := p.Functions[stack.Peek().Value]; ok {
if stack.Peek().Type == FilterTokenFunc {
queue.Enqueue(stack.Pop())
}
}
} else {
// Token is a literal -- put it in the queue
queue.Enqueue(token)
}
}
// pop off the remaining operators onto the queue
for !stack.Empty() {
if stack.Peek().Value == "(" || stack.Peek().Value == ")" {
return nil, BadRequestError("parse error. Mismatched parenthesis.")
}
queue.Enqueue(stack.Pop())
}
return &queue, nil
}
// Convert a Postfix token queue to a parse tree
func (p *Parser) PostfixToTree(queue *tokenQueue) (*ParseNode, error) {
stack := &nodeStack{}
currNode := &ParseNode{}
t := queue.Head
for t != nil {
t = t.Next
}
for !queue.Empty() {
// push the token onto the stack as a tree node
currNode = &ParseNode{queue.Dequeue(), nil, make([]*ParseNode, 0)}
stack.Push(currNode)
if stack.Peek().Token.Type == FilterTokenFunc {
// if the top of the stack is a function
node := stack.Pop()
f := p.Functions[node.Token.Value]
// pop off function parameters
for i := 0; i < f.Params; i++ {
if stack.Empty() {
continue
}
// prepend children so they get added in the right order
node.Children = append([]*ParseNode{stack.Pop()}, node.Children...)
}
stack.Push(node)
} else if _, ok := p.Operators[stack.Peek().Token.Value]; ok {
// if the top of the stack is an operator
node := stack.Pop()
o := p.Operators[node.Token.Value]
// pop off operands
for i := 0; i < o.Operands; i++ {
if stack.Empty() {
continue
}
// prepend children so they get added in the right order
node.Children = append([]*ParseNode{stack.Pop()}, node.Children...)
}
stack.Push(node)
}
stack.Peek().Token.Value = strings.TrimSpace(stack.Peek().Token.Value)
}
return currNode, nil
}
type tokenStack struct {
Head *tokenStackNode
Size int
}
type tokenStackNode struct {
Token *Token
Prev *tokenStackNode
}
func (s *tokenStack) Push(t *Token) {
node := tokenStackNode{t, s.Head}
//fmt.Println("Pushed:", t.Value, "->", s.String())
s.Head = &node
s.Size++
}
func (s *tokenStack) Pop() *Token {
node := s.Head
s.Head = node.Prev
s.Size--
//fmt.Println("Popped:", node.Token.Value, "<-", s.String())
return node.Token
}
func (s *tokenStack) Peek() *Token {
return s.Head.Token
}
func (s *tokenStack) Empty() bool {
return s.Head == nil
}
func (s *tokenStack) String() string {
output := ""
currNode := s.Head
for currNode != nil {
output += " " + currNode.Token.Value
currNode = currNode.Prev
}
return output
}
type tokenQueue struct {
Head *tokenQueueNode
Tail *tokenQueueNode
}
type tokenQueueNode struct {
Token *Token
Prev *tokenQueueNode
Next *tokenQueueNode
}
func (q *tokenQueue) Enqueue(t *Token) {
node := tokenQueueNode{t, q.Tail, nil}
//fmt.Println(t.Value)
if q.Tail == nil {
q.Head = &node
} else {
q.Tail.Next = &node
}
q.Tail = &node
}
func (q *tokenQueue) Dequeue() *Token {
node := q.Head
if node.Next != nil {
node.Next.Prev = nil
}
q.Head = node.Next
if q.Head == nil {
q.Tail = nil
}
return node.Token
}
func (q *tokenQueue) Empty() bool {
return q.Head == nil && q.Tail == nil
}
func (q *tokenQueue) String() string {
result := ""
node := q.Head
for node != nil {
result += node.Token.Value
node = node.Next
}
return result
}
type nodeStack struct {
Head *nodeStackNode
}
type nodeStackNode struct {
ParseNode *ParseNode
Prev *nodeStackNode
}
func (s *nodeStack) Push(n *ParseNode) {
node := nodeStackNode{n, s.Head}
s.Head = &node
}
func (s *nodeStack) Pop() *ParseNode {
node := s.Head
s.Head = node.Prev
return node.ParseNode
}
func (s *nodeStack) Peek() *ParseNode {
return s.Head.ParseNode
}
func (s *nodeStack) Empty() bool {
return s.Head == nil
}