@@ -55,43 +55,6 @@ const (
5555 itemStar
5656)
5757
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-
9558// lexUpsertBlock lexes the upsert block
9659func lexUpsertBlock (l * lex.Lexer ) lex.StateFn {
9760 l .Mode = lexUpsertBlock
@@ -368,7 +331,7 @@ Loop:
368331 case r == leftCurl :
369332 l .Depth ++ // one level down.
370333 l .Emit (itemLeftCurl )
371- return lexQuery
334+ return lexIdentifyMutationOrQuery
372335 case r == rightCurl :
373336 return l .Errorf ("Too many right curl" )
374337 case r == lex .EOF :
@@ -396,6 +359,33 @@ Loop:
396359 return nil
397360}
398361
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+
399389// lexQuery lexes the input string and calls other lex functions.
400390func lexQuery (l * lex.Lexer ) lex.StateFn {
401391 l .Mode = lexQuery
@@ -592,6 +582,9 @@ func lexOperationType(l *lex.Lexer) lex.StateFn {
592582 case "schema" :
593583 l .Emit (itemOpType )
594584 return lexInsideSchema
585+ case "upsert" :
586+ l .Emit (itemUpsertBlock )
587+ return lexUpsertBlock
595588 default :
596589 return l .Errorf ("Invalid operation type: %s" , word )
597590 }
0 commit comments