forked from dgraph-io/dgraph
/
state.go
139 lines (130 loc) · 3.19 KB
/
state.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
/*
* Copyright (C) 2017 Dgraph Labs, Inc. and Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package schema
import (
"gopkg.in/adibiarsotp/dgraph.v82/lex"
)
// Constants representing type of different graphql lexed items.
const (
itemText lex.ItemType = 5 + iota // plain text
itemLeftCurl // left curly bracket
itemRightCurl // right curly bracket
itemColon // colon
itemLeftRound // left round bracket
itemRightRound // right round bracket
itemAt
itemComma
itemNewLine
itemDot
itemUnderscore
itemLeftSquare
itemRightSquare
)
func lexText(l *lex.Lexer) lex.StateFn {
Loop:
for {
switch r := l.Next(); {
case r == lex.EOF:
break Loop
case isNameBegin(r):
l.Backup()
return lexWord
case isSpace(r):
l.Ignore()
case isEndOfLine(r):
l.Emit(itemNewLine)
case r == '.':
l.Emit(itemDot)
case r == ',':
l.Emit(itemComma)
case r == '<':
if err := lex.LexIRIRef(l, itemText); err != nil {
return l.Errorf("Invalid schema: %v", err)
}
case r == '{':
l.Emit(itemLeftCurl)
case r == '}':
l.Emit(itemRightCurl)
case r == '(':
l.Emit(itemLeftRound)
case r == ')':
l.Emit(itemRightRound)
case r == ':':
l.Emit(itemColon)
case r == '@':
l.Emit(itemAt)
case r == '[':
l.Emit(itemLeftSquare)
case r == ']':
l.Emit(itemRightSquare)
case r == '_':
// Predicates can start with _.
return lexWord
default:
return l.Errorf("Invalid schema. Unexpected %s", l.Input[l.Start:l.Pos])
}
}
if l.Pos > l.Start {
l.Emit(itemText)
}
l.Emit(lex.ItemEOF)
return nil
}
func lexWord(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(itemText)
break
}
return lexText
}
// isNameBegin returns true if the rune is an alphabet.
func isNameBegin(r rune) bool {
switch {
case r >= 'a' && r <= 'z':
return true
case r >= 'A' && r <= 'Z':
return true
default:
return false
}
}
func isNameSuffix(r rune) bool {
if isNameBegin(r) {
return true
}
if r >= '0' && r <= '9' {
return true
}
if r == '_' || r == '.' || r == '-' { // Use by freebase.
return true
}
return false
}
// isSpace returns true if the rune is a tab or space.
func isSpace(r rune) bool {
return r == '\u0009' || r == '\u0020'
}
// isEndOfLine returns true if the rune is a Linefeed or a Carriage return.
func isEndOfLine(r rune) bool {
return r == '\n' || r == '\u000D'
}