-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathstate.go
More file actions
696 lines (656 loc) · 15 KB
/
state.go
File metadata and controls
696 lines (656 loc) · 15 KB
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
// Package dql is responsible for lexing and parsing a GraphQL query/mutation.
package dql
import (
"github.com/hypermodeinc/dgraph/v24/lex"
)
const (
leftCurl = '{'
rightCurl = '}'
leftRound = '('
rightRound = ')'
leftSquare = '['
rightSquare = ']'
period = '.'
comma = ','
slash = '/'
equal = '='
quote = '"'
at = '@'
colon = ':'
lsThan = '<'
star = '*'
)
// Constants representing type of different graphql lexed items.
const (
itemText lex.ItemType = 5 + iota // plain text
itemLeftCurl // left curly bracket
itemRightCurl // right curly bracket
itemEqual // equals to symbol
itemName // [9] names
itemOpType // operation type
itemLeftRound // left round bracket
itemRightRound // right round bracket
itemColon // Colon
itemAt // @
itemPeriod // .
itemDollar // $
itemRegex // /
itemMutationOp // mutation operation (set, delete)
itemMutationOpContent // mutation operation content
itemUpsertBlock // mutation upsert block
itemUpsertBlockOp // upsert block op (query, mutate)
itemUpsertBlockOpContent // upsert block operations' content
itemLeftSquare
itemRightSquare
itemComma
itemMathOp
itemStar
)
// lexUpsertBlock lexes the upsert block
func lexUpsertBlock(l *lex.Lexer) lex.StateFn {
l.Mode = lexUpsertBlock
for {
switch r := l.Next(); {
case r == rightCurl:
l.BlockDepth--
l.Emit(itemRightCurl)
if l.BlockDepth == 0 {
return lexTopLevel
}
case r == leftCurl:
l.BlockDepth++
l.Emit(itemLeftCurl)
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case isNameBegin(r):
return lexNameUpsertOp
case r == '#':
return lexComment
case r == lex.EOF:
return l.Errorf("Unclosed upsert block")
default:
return l.Errorf("Unrecognized character in upsert block: %#U", r)
}
}
}
// lexNameUpsertOp parses the operation names inside upsert block
func lexNameUpsertOp(l *lex.Lexer) lex.StateFn {
// The caller already checked isNameBegin, and absorbed one rune.
l.AcceptRun(isNameSuffix)
word := l.Input[l.Start:l.Pos]
switch word {
case "query":
l.Emit(itemUpsertBlockOp)
return lexBlockContent
case "mutation":
l.Emit(itemUpsertBlockOp)
return lexInsideMutation
case "fragment":
l.Emit(itemUpsertBlockOp)
return lexBlockContent
default:
return l.Errorf("Invalid operation type: %s", word)
}
}
// lexBlockContent lexes and absorbs the text inside a block (covered by braces).
func lexBlockContent(l *lex.Lexer) lex.StateFn {
return lexContent(l, leftCurl, rightCurl, lexUpsertBlock)
}
// lexIfContent lexes the whole of @if directive in a mutation block (covered by small brackets)
func lexIfContent(l *lex.Lexer) lex.StateFn {
if r := l.Next(); r != at {
return l.Errorf("Expected [@], found; [%#U]", r)
}
l.AcceptRun(isNameSuffix)
word := l.Input[l.Start:l.Pos]
if word != "@if" {
return l.Errorf("Expected @if, found [%v]", word)
}
return lexContent(l, '(', ')', lexInsideMutation)
}
func lexContent(l *lex.Lexer, leftRune, rightRune rune, returnTo lex.StateFn) lex.StateFn {
depth := 0
for {
switch l.Next() {
case lex.EOF:
return l.Errorf("Matching brackets not found")
case quote:
if err := l.LexQuotedString(); err != nil {
return l.Errorf(err.Error())
}
case leftRune:
depth++
case rightRune:
depth--
switch {
case depth < 0:
return l.Errorf("Unopened %c found", rightRune)
case depth == 0:
l.Emit(itemUpsertBlockOpContent)
return returnTo
}
}
}
}
func lexInsideMutation(l *lex.Lexer) lex.StateFn {
l.Mode = lexInsideMutation
for {
switch r := l.Next(); {
case r == at:
l.Backup()
return lexIfContent
case r == rightCurl:
l.Depth--
l.Emit(itemRightCurl)
if l.Depth == 0 {
return lexTopLevel
}
case r == leftCurl:
l.Depth++
l.Emit(itemLeftCurl)
if l.Depth >= 2 {
return lexTextMutation
}
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case isNameBegin(r):
return lexNameMutation
case r == '#':
return lexComment
case r == lex.EOF:
return l.Errorf("Unclosed mutation action")
default:
return l.Errorf("Unrecognized character inside mutation: %#U", r)
}
}
}
func lexInsideSchema(l *lex.Lexer) lex.StateFn {
l.Mode = lexInsideSchema
for {
switch r := l.Next(); {
case r == rightRound:
l.Emit(itemRightRound)
case r == leftRound:
l.Emit(itemLeftRound)
case r == rightCurl:
l.Depth--
l.Emit(itemRightCurl)
if l.Depth == 0 {
return lexTopLevel
}
case r == leftCurl:
l.Depth++
l.Emit(itemLeftCurl)
case r == leftSquare:
l.Emit(itemLeftSquare)
case r == rightSquare:
l.Emit(itemRightSquare)
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case r == lsThan:
return lexIRIRef
case isNameBegin(r):
return lexArgName
case r == '#':
return lexComment
case r == colon:
l.Emit(itemColon)
case r == comma:
l.Emit(itemComma)
case r == lex.EOF:
return l.Errorf("Unclosed schema action")
default:
return l.Errorf("Unrecognized character inside schema: %#U", r)
}
}
}
func lexFuncOrArg(l *lex.Lexer) lex.StateFn {
l.Mode = lexFuncOrArg
var empty bool
for {
switch r := l.Next(); {
case r == at:
l.Emit(itemAt)
return lexDirectiveOrLangList
case isNameBegin(r) || isNumber(r):
return lexArgName
case r == slash:
// if argument starts with '/' it's a regex, otherwise it's a division
if empty {
return lexRegex(l)
}
fallthrough
case isMathOp(r):
l.Emit(itemMathOp)
case isInequalityOp(r):
if r == equal {
if !isInequalityOp(l.Peek()) {
l.Emit(itemEqual)
continue
}
}
if r == lsThan {
if !isSpace(l.Peek()) && l.Peek() != '=' {
// as long as its not '=' or ' '
return lexIRIRef
}
}
if isInequalityOp(l.Peek()) {
l.Next()
}
l.Emit(itemMathOp)
case r == leftRound:
l.Emit(itemLeftRound)
l.ArgDepth++
case r == rightRound:
if l.ArgDepth == 0 {
return l.Errorf("Unexpected right round bracket")
}
l.ArgDepth--
l.Emit(itemRightRound)
if empty {
return l.Errorf("Empty Argument")
}
if l.ArgDepth == 0 {
return lexQuery // Filter directive is done.
}
case r == lex.EOF:
return l.Errorf("Unclosed Brackets")
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case r == comma:
if empty {
return l.Errorf("Consecutive commas not allowed.")
}
empty = true
l.Emit(itemComma)
case isDollar(r):
l.Emit(itemDollar)
case r == colon:
l.Emit(itemColon)
case r == quote:
{
empty = false
if err := l.LexQuotedString(); err != nil {
return l.Errorf(err.Error())
}
l.Emit(itemName)
}
case isEndLiteral(r):
{
empty = false
l.AcceptUntil(isEndLiteral) // This call will backup the ending ".
l.Next() // Consume the " .
l.Emit(itemName)
}
case r == leftSquare:
l.Emit(itemLeftSquare)
case r == rightSquare:
l.Emit(itemRightSquare)
case r == '#':
return lexComment
case r == '.':
l.Emit(itemPeriod)
default:
return l.Errorf("Unrecognized character inside a func: %#U", r)
}
}
}
func lexTopLevel(l *lex.Lexer) lex.StateFn {
// TODO(Aman): Find a way to identify different blocks in future. We only have
// Upsert block right now. BlockDepth tells us nesting of blocks. Currently, only
// the Upsert block has nested mutation/query/fragment blocks.
if l.BlockDepth != 0 {
return lexUpsertBlock
}
l.Mode = lexTopLevel
Loop:
for {
switch r := l.Next(); {
case r == leftCurl:
l.Depth++ // one level down.
l.Emit(itemLeftCurl)
return lexIdentifyMutationOrQuery
case r == rightCurl:
return l.Errorf("Too many right curl")
case r == lex.EOF:
break Loop
case r == '#':
return lexComment
case r == leftRound:
l.Backup()
l.Emit(itemText)
l.Next()
l.Emit(itemLeftRound)
l.ArgDepth++
return lexQuery
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case isNameBegin(r):
l.Backup()
return lexOperationType
}
}
if l.Pos > l.Start {
l.Emit(itemText)
}
l.Emit(lex.ItemEOF)
return nil
}
func lexIdentifyMutationOrQuery(l *lex.Lexer) lex.StateFn {
l.Mode = lexIdentifyMutationOrQuery
for {
switch r := l.Next(); {
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case isNameBegin(r):
l.AcceptRun(isNameSuffix)
op := l.Input[l.Start:l.Pos]
// If it is a mutation
if op == "set" || op == "delete" || op == "del" {
l.Emit(itemMutationOp)
return lexInsideMutation
}
// else it is a query
l.Emit(itemName)
return lexQuery
case r == '#':
return lexComment
case r == lex.EOF:
return l.Errorf("Invalid DQL")
default:
return l.Errorf("Unexpected character while lexing DQL: %#U", r)
}
}
}
// lexQuery lexes the input string and calls other lex functions.
func lexQuery(l *lex.Lexer) lex.StateFn {
l.Mode = lexQuery
for {
switch r := l.Next(); {
case r == period:
l.Emit(itemPeriod)
case r == rightCurl:
l.Depth--
l.Emit(itemRightCurl)
if l.Depth == 0 {
return lexTopLevel
}
case r == leftCurl:
l.Depth++
l.Emit(itemLeftCurl)
case r == lex.EOF:
return l.Errorf("Unclosed action")
case isSpace(r) || lex.IsEndOfLine(r):
l.Ignore()
case r == comma:
l.Emit(itemComma)
case isNameBegin(r):
return lexName
case r == '#':
return lexComment
case r == '-':
l.Emit(itemMathOp)
case r == leftRound:
l.Emit(itemLeftRound)
l.AcceptRun(isSpace)
l.Ignore()
l.ArgDepth++
return lexFuncOrArg
case r == colon:
l.Emit(itemColon)
case r == at:
l.Emit(itemAt)
return lexDirectiveOrLangList
case r == lsThan:
return lexIRIRef
case r == star:
l.Emit(itemStar)
default:
return l.Errorf("Unrecognized character in lexText: %#U", r)
}
}
}
func lexIRIRef(l *lex.Lexer) lex.StateFn {
if err := lex.IRIRef(l, itemName); err != nil {
return l.Errorf(err.Error())
}
return l.Mode
}
// lexDirectiveOrLangList is called right after we see a @.
func lexDirectiveOrLangList(l *lex.Lexer) lex.StateFn {
r := l.Next()
// Check first character.
if !isNameBegin(r) && r != period && r != star {
return l.Errorf("Unrecognized character in lexDirective: %#U", r)
}
l.Backup()
for {
r := l.Next()
if r == period {
l.Emit(itemName)
return l.Mode
}
if isLangOrDirective(r) {
continue
}
l.Backup()
l.Emit(itemName)
break
}
return l.Mode
}
func lexName(l *lex.Lexer) lex.StateFn {
l.AcceptRun(isNameSuffix)
l.Emit(itemName)
return l.Mode
}
// lexComment lexes a comment text.
func lexComment(l *lex.Lexer) lex.StateFn {
for {
r := l.Next()
if lex.IsEndOfLine(r) {
l.Ignore()
return l.Mode
}
if r == lex.EOF {
break
}
}
l.Ignore()
l.Emit(lex.ItemEOF)
return l.Mode
}
// lexNameMutation lexes the itemMutationOp, which could be set or delete.
func lexNameMutation(l *lex.Lexer) lex.StateFn {
for {
// The caller already checked isNameBegin, and absorbed one rune.
r := l.Next()
if isNameSuffix(r) {
continue
}
l.Backup()
l.Emit(itemMutationOp)
break
}
return l.Mode
}
// lexTextMutation lexes and absorbs the text inside a mutation operation block.
func lexTextMutation(l *lex.Lexer) lex.StateFn {
for {
r := l.Next()
if r == lex.EOF {
return l.Errorf("Unclosed mutation text")
}
if r == quote {
return lexMutationValue
}
if r == leftCurl {
return l.Errorf("Invalid character '{' inside mutation text")
}
if r != rightCurl {
// Absorb everything until we find '}'.
continue
}
l.Backup()
l.Emit(itemMutationOpContent)
break
}
return lexInsideMutation
}
// This function is used to absorb the object value.
func lexMutationValue(l *lex.Lexer) lex.StateFn {
LOOP:
for {
r := l.Next()
switch r {
case lex.EOF:
return l.Errorf("Unclosed mutation value")
case quote:
break LOOP
case '\\':
l.Next() // skip one.
}
}
return lexTextMutation
}
func lexRegex(l *lex.Lexer) lex.StateFn {
LOOP:
for {
r := l.Next()
switch r {
case lex.EOF:
return l.Errorf("Unclosed regexp")
case '\\':
l.Next()
case '/':
break LOOP
}
}
l.AcceptRun(isRegexFlag)
l.Emit(itemRegex)
return l.Mode
}
// lexOperationType lexes a query or mutation or schema operation type.
func lexOperationType(l *lex.Lexer) lex.StateFn {
l.AcceptRun(isNameSuffix)
// l.Pos would be index of the end of operation type + 1.
word := l.Input[l.Start:l.Pos]
switch word {
case "mutation":
l.Emit(itemOpType)
return lexInsideMutation
case "fragment":
l.Emit(itemOpType)
return lexQuery
case "query":
l.Emit(itemOpType)
return lexQuery
case "schema":
l.Emit(itemOpType)
return lexInsideSchema
case "upsert":
l.Emit(itemUpsertBlock)
return lexUpsertBlock
default:
return l.Errorf("Invalid operation type: %s", word)
}
}
// lexArgName lexes and emits the name part of an argument.
func lexArgName(l *lex.Lexer) lex.StateFn {
l.AcceptRun(isNameSuffix)
l.Emit(itemName)
return l.Mode
}
// isDollar returns true if the rune is a Dollar($).
func isDollar(r rune) bool {
return r == '$' || r == '\u0024'
}
// isSpace returns true if the rune is a tab or space.
func isSpace(r rune) bool {
return r == '\u0009' || r == '\u0020'
}
// isEndLiteral returns true if rune is quotation mark.
func isEndLiteral(r rune) bool {
return r == '"' || r == '\u000d' || r == '\u000a'
}
func isLangOrDirective(r rune) bool {
if isNameBegin(r) {
return true
}
if r == '-' {
return true
}
if r >= '0' && r <= '9' {
return true
}
if r == '*' {
return true
}
return false
}
// isNameBegin returns true if the rune is an alphabet or an '_' or '~'.
func isNameBegin(r rune) bool {
switch {
case r >= 'a' && r <= 'z':
return true
case r >= 'A' && r <= 'Z':
return true
case r == '_':
return true
case r == '~':
return true
default:
return false
}
}
func isMathOp(r rune) bool {
switch r {
case '+', '-', '*', '/', '%':
return true
default:
return false
}
}
func isInequalityOp(r rune) bool {
switch r {
case '<', '>', '=', '!':
return true
default:
return false
}
}
func isNumber(r rune) bool {
switch {
case (r >= '0' && r <= '9'):
return true
default:
return false
}
}
func isNameSuffix(r rune) bool {
if isMathOp(r) {
return false
}
if isNameBegin(r) || isNumber(r) {
return true
}
if r == '.' /*|| r == '!'*/ { // Use by freebase.
return true
}
return false
}
func isRegexFlag(r rune) bool {
switch {
case r >= 'a' && r <= 'z':
return true
case r >= 'A' && r <= 'Z':
return true
default:
return false
}
}