@@ -55,43 +55,6 @@ const (
55
55
itemStar
56
56
)
57
57
58
- // lexIdentifyBlock identifies whether it is an upsert block
59
- // If the block begins with "{" => mutation block
60
- // Else if the block begins with "upsert" => upsert block
61
- func lexIdentifyBlock (l * lex.Lexer ) lex.StateFn {
62
- l .Mode = lexIdentifyBlock
63
- for {
64
- switch r := l .Next (); {
65
- case isSpace (r ) || lex .IsEndOfLine (r ):
66
- l .Ignore ()
67
- case isNameBegin (r ):
68
- return lexNameBlock
69
- case r == leftCurl :
70
- l .Backup ()
71
- return lexInsideMutation
72
- case r == '#' :
73
- return lexComment
74
- case r == lex .EOF :
75
- return l .Errorf ("Invalid mutation block" )
76
- default :
77
- return l .Errorf ("Unexpected character while identifying mutation block: %#U" , r )
78
- }
79
- }
80
- }
81
-
82
- // lexNameBlock lexes the blocks, for now, only upsert block
83
- func lexNameBlock (l * lex.Lexer ) lex.StateFn {
84
- // The caller already checked isNameBegin, and absorbed one rune.
85
- l .AcceptRun (isNameSuffix )
86
- switch word := l .Input [l .Start :l .Pos ]; word {
87
- case "upsert" :
88
- l .Emit (itemUpsertBlock )
89
- return lexUpsertBlock
90
- default :
91
- return l .Errorf ("Invalid block: [%s]" , word )
92
- }
93
- }
94
-
95
58
// lexUpsertBlock lexes the upsert block
96
59
func lexUpsertBlock (l * lex.Lexer ) lex.StateFn {
97
60
l .Mode = lexUpsertBlock
@@ -368,7 +331,7 @@ Loop:
368
331
case r == leftCurl :
369
332
l .Depth ++ // one level down.
370
333
l .Emit (itemLeftCurl )
371
- return lexQuery
334
+ return lexIdentifyMutationOrQuery
372
335
case r == rightCurl :
373
336
return l .Errorf ("Too many right curl" )
374
337
case r == lex .EOF :
@@ -396,6 +359,33 @@ Loop:
396
359
return nil
397
360
}
398
361
362
+ func lexIdentifyMutationOrQuery (l * lex.Lexer ) lex.StateFn {
363
+ l .Mode = lexIdentifyMutationOrQuery
364
+ for {
365
+ switch r := l .Next (); {
366
+ case isSpace (r ) || lex .IsEndOfLine (r ):
367
+ l .Ignore ()
368
+ case isNameBegin (r ):
369
+ l .AcceptRun (isNameSuffix )
370
+ op := l .Input [l .Start :l .Pos ]
371
+ // If it is a mutation
372
+ if op == "set" || op == "delete" || op == "del" {
373
+ l .Emit (itemMutationOp )
374
+ return lexInsideMutation
375
+ }
376
+ // else it is a query
377
+ l .Emit (itemName )
378
+ return lexQuery
379
+ case r == '#' :
380
+ return lexComment
381
+ case r == lex .EOF :
382
+ return l .Errorf ("Invalid DQL" )
383
+ default :
384
+ return l .Errorf ("Unexpected character while lexing DQL: %#U" , r )
385
+ }
386
+ }
387
+ }
388
+
399
389
// lexQuery lexes the input string and calls other lex functions.
400
390
func lexQuery (l * lex.Lexer ) lex.StateFn {
401
391
l .Mode = lexQuery
@@ -592,6 +582,9 @@ func lexOperationType(l *lex.Lexer) lex.StateFn {
592
582
case "schema" :
593
583
l .Emit (itemOpType )
594
584
return lexInsideSchema
585
+ case "upsert" :
586
+ l .Emit (itemUpsertBlock )
587
+ return lexUpsertBlock
595
588
default :
596
589
return l .Errorf ("Invalid operation type: %s" , word )
597
590
}
0 commit comments