Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
dgraph
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
dgraph
Commits
5a7e209a
Commit
5a7e209a
authored
9 years ago
by
Manish R Jain
Browse files
Options
Downloads
Patches
Plain Diff
Modify state.go to accommodate Mutation instruction.
parent
0008cd87
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
gql/state.go
+87
-16
87 additions, 16 deletions
gql/state.go
gql/state_test.go
+52
-3
52 additions, 3 deletions
gql/state_test.go
lex/lexer.go
+1
-0
1 addition, 0 deletions
lex/lexer.go
with
140 additions
and
19 deletions
gql/state.go
+
87
−
16
View file @
5a7e209a
...
...
@@ -19,22 +19,26 @@ package gql
import
"github.com/dgraph-io/dgraph/lex"
const
(
leftCurl
=
'{'
rightCurl
=
'}'
leftCurl
=
'{'
rightCurl
=
'}'
queryMode
=
1
mutationMode
=
2
)
const
(
itemText
lex
.
ItemType
=
5
+
iota
// plain text
itemLeftCurl
// left curly bracket
itemRightCurl
// right curly bracket
itemComment
// comment
itemName
// [9] names
itemOpType
// operation type
itemString
// quoted string
itemLeftRound
// left round bracket
itemRightRound
// right round bracket
itemArgName
// argument name
itemArgVal
// argument val
itemText
lex
.
ItemType
=
5
+
iota
// plain text
itemLeftCurl
// left curly bracket
itemRightCurl
// right curly bracket
itemComment
// comment
itemName
// [9] names
itemOpType
// operation type
itemString
// quoted string
itemLeftRound
// left round bracket
itemRightRound
// right round bracket
itemArgName
// argument name
itemArgVal
// argument val
itemMutationOp
// mutation operation
itemMutationContent
// mutation content
)
func
lexText
(
l
*
lex
.
Lexer
)
lex
.
StateFn
{
...
...
@@ -47,7 +51,11 @@ Loop:
l
.
Next
()
// advance one to get back to where we saw leftCurl.
l
.
Depth
+=
1
// one level down.
l
.
Emit
(
itemLeftCurl
)
return
lexInside
// we're in.
if
l
.
Mode
==
mutationMode
{
return
lexInsideMutation
}
else
{
return
lexInside
}
case
r
==
rightCurl
:
return
l
.
Errorf
(
"Too many right characters"
)
...
...
@@ -79,7 +87,7 @@ func lexInside(l *lex.Lexer) lex.StateFn {
l
.
Depth
+=
1
l
.
Emit
(
itemLeftCurl
)
case
r
==
lex
.
EOF
:
return
l
.
Errorf
(
"
u
nclosed action"
)
return
l
.
Errorf
(
"
U
nclosed action"
)
case
isSpace
(
r
)
||
isEndOfLine
(
r
)
||
r
==
','
:
l
.
Ignore
()
case
isNameBegin
(
r
)
:
...
...
@@ -128,6 +136,64 @@ func lexComment(l *lex.Lexer) lex.StateFn {
return
nil
// Stop the run loop.
}
func
lexInsideMutation
(
l
*
lex
.
Lexer
)
lex
.
StateFn
{
for
{
switch
r
:=
l
.
Next
();
{
case
r
==
rightCurl
:
l
.
Depth
-=
1
l
.
Emit
(
itemRightCurl
)
if
l
.
Depth
==
0
{
return
lexText
}
case
r
==
leftCurl
:
l
.
Depth
+=
1
l
.
Emit
(
itemLeftCurl
)
if
l
.
Depth
>=
2
{
return
lexTextMutation
}
case
r
==
lex
.
EOF
:
return
l
.
Errorf
(
"Unclosed mutation action"
)
case
isSpace
(
r
)
||
isEndOfLine
(
r
)
:
l
.
Ignore
()
case
isNameBegin
(
r
)
:
return
lexNameMutation
default
:
return
l
.
Errorf
(
"Unrecognized character in lexInsideMutation: %#U"
,
r
)
}
}
}
func
lexNameMutation
(
l
*
lex
.
Lexer
)
lex
.
StateFn
{
for
{
// The caller already checked isNameBegin, and absorbed one rune.
r
:=
l
.
Next
()
if
isNameBegin
(
r
)
{
continue
}
l
.
Backup
()
l
.
Emit
(
itemMutationOp
)
break
}
return
lexInsideMutation
}
func
lexTextMutation
(
l
*
lex
.
Lexer
)
lex
.
StateFn
{
for
{
r
:=
l
.
Next
()
if
r
==
lex
.
EOF
{
return
l
.
Errorf
(
"Unclosed mutation text"
)
}
if
r
!=
rightCurl
{
// Absorb everything until we find '}'.
continue
}
l
.
Backup
()
l
.
Emit
(
itemMutationContent
)
break
}
return
lexInsideMutation
}
func
lexOperationType
(
l
*
lex
.
Lexer
)
lex
.
StateFn
{
for
{
r
:=
l
.
Next
()
...
...
@@ -136,8 +202,13 @@ func lexOperationType(l *lex.Lexer) lex.StateFn {
}
l
.
Backup
()
word
:=
l
.
Input
[
l
.
Start
:
l
.
Pos
]
if
word
==
"query"
||
word
==
"mutation"
{
if
word
==
"mutation"
{
l
.
Emit
(
itemOpType
)
l
.
Mode
=
mutationMode
}
else
if
word
==
"query"
{
l
.
Emit
(
itemOpType
)
l
.
Mode
=
queryMode
}
break
}
...
...
This diff is collapsed.
Click to expand it.
gql/state_test.go
+
52
−
3
View file @
5a7e209a
...
...
@@ -17,7 +17,6 @@
package
gql
import
(
"fmt"
"testing"
"github.com/dgraph-io/dgraph/lex"
...
...
@@ -25,7 +24,7 @@ import (
func
TestNewLexer
(
t
*
testing
.
T
)
{
input
:=
`
mutation
{
query
{
me( id: 10, xid: rick ) {
name0 # my name
_city, # 0what would fail lex.
...
...
@@ -38,6 +37,56 @@ func TestNewLexer(t *testing.T) {
l
:=
lex
.
NewLexer
(
input
)
go
run
(
l
)
for
item
:=
range
l
.
Items
{
fmt
.
Println
(
item
.
String
())
if
item
.
Typ
==
lex
.
ItemError
{
t
.
Error
(
item
.
String
())
}
t
.
Log
(
item
.
String
())
}
}
func
TestNewLexerMutation
(
t
*
testing
.
T
)
{
input
:=
`
mutation {
set {
What is <this> .
Why is this #!!?
How is this?
}
delete {
Why is this
}
}
query {
me(xid: rick) {
_city
}
}`
l
:=
lex
.
NewLexer
(
input
)
go
run
(
l
)
for
item
:=
range
l
.
Items
{
if
item
.
Typ
==
lex
.
ItemError
{
t
.
Error
(
item
.
String
())
}
t
.
Log
(
item
.
String
())
}
}
func
TestAbruptMutation
(
t
*
testing
.
T
)
{
input
:=
`
mutation {
set {
What is <this> .
Why is this #!!?
How is this?
}`
l
:=
lex
.
NewLexer
(
input
)
go
run
(
l
)
var
typ
lex
.
ItemType
for
item
:=
range
l
.
Items
{
t
.
Log
(
item
.
String
())
typ
=
item
.
Typ
}
if
typ
!=
lex
.
ItemError
{
t
.
Error
(
"This should fail."
)
}
}
This diff is collapsed.
Click to expand it.
lex/lexer.go
+
1
−
0
View file @
5a7e209a
...
...
@@ -63,6 +63,7 @@ type Lexer struct {
Width
int
// width of last rune read from input.
Items
chan
item
// channel of scanned items.
Depth
int
// nesting of {}
Mode
int
// mode based on information so far.
}
func
NewLexer
(
input
string
)
*
Lexer
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment