This repository has been archived by the owner on Jan 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
token.go
249 lines (227 loc) · 3.9 KB
/
token.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
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
package cypher
import "strings"
// Token is a lexical token of the Cypher language.
type Token int
const (
// ILLEGAL Token, EOF, WS are Special Cypher tokens.
ILLEGAL Token = iota
EOF
WS
COMMENT
literalBeg
// IDENT and the following are literal tokens.
IDENT // main
NUMBER // 12345.67
INTEGER // 12345
STRING // "abc"
BADSTRING // "abc
BADESCAPE // "\q
TRUE // true
FALSE // false
NULL // null
literalEnd
operatorBeg
PLUS // +
SUB // -
MUL // *
DIV // /
MOD // %
POW // ^
EQ // =
NEQ // <>
LT // <
LTE // <=
GT // >
GTE // >=
INC // +=
BAR // |
AND // AND
OR // OR
XOR // XOR
NOT // NOT
operatorEnd
LPAREN // (
RPAREN // )
LBRACE // {
RBRACE // }
LBRACKET // [
RBRACKET // ]
COMMA // ,
COLON // :
SEMICOLON // ;
DOT // .
DOUBLEDOT // ..
keywordBeg
// ALL and the following are Cypher Keywords
ADD
ALL
AS
ASC
ASCENDING
BY
CASE
CONSTRAINT
CONTAINS
CREATE
DELETE
DESC
DESCENDING
DETACH
DISTINCT
DO
DROP
ELSE
END
ENDS
EXISTS
FOR
IN
IS
LIMIT
MANDATORY
MATCH
MERGE
OF
ON
OPTIONAL
ORDER
REMOVE
REQUIRE
RETURN
SCALAR
SET
SKIP
STARTS
THEN
UNION
UNIQUE
UNWIND
WHEN
WHERE
WITH
keywordEnd
)
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
WS: "WS",
IDENT: "IDENT",
NUMBER: "NUMBER",
STRING: "STRING",
TRUE: "TRUE",
FALSE: "FALSE",
PLUS: "+",
SUB: "-",
MUL: "*",
DIV: "/",
MOD: "%",
POW: "^",
AND: "AND",
OR: "OR",
XOR: "XOR",
NOT: "NOT",
EQ: "=",
NEQ: "<>",
LT: "<",
LTE: "<=",
GT: ">",
GTE: ">=",
LPAREN: "(",
RPAREN: ")",
LBRACE: "{",
RBRACE: "}",
LBRACKET: "[",
RBRACKET: "]",
COMMA: ",",
COLON: ":",
SEMICOLON: ";",
DOT: ".",
ADD: "ADD",
ALL: "ALL",
AS: "AS",
ASC: "ASC",
ASCENDING: "ASCENDING",
BY: "BY",
CASE: "CASE",
CONSTRAINT: "CONSTRAINT",
CONTAINS: "CONTAINS",
CREATE: "CREATE",
DELETE: "DELETE",
DESC: "DESC",
DESCENDING: "DESCENDING",
DETACH: "DETACH",
DISTINCT: "DISTINCT",
DO: "DO",
DROP: "DROP",
ELSE: "ELSE",
END: "END",
ENDS: "ENDS",
EXISTS: "EXISTS",
FOR: "FOR",
IN: "IN",
IS: "IS",
LIMIT: "LIMIT",
MANDATORY: "MANDATORY",
MATCH: "MATCH",
MERGE: "MERGE",
OF: "OF",
ON: "ON",
OPTIONAL: "OPTIONAL",
ORDER: "ORDER",
REMOVE: "REMOVE",
REQUIRE: "REQUIRE",
RETURN: "RETURN",
SCALAR: "SCALAR",
SET: "SET",
SKIP: "SKIP",
STARTS: "STARTS",
THEN: "THEN",
UNION: "UNION",
UNIQUE: "UNIQUE",
UNWIND: "UNWIND",
WHEN: "WHEN",
WHERE: "WHERE",
WITH: "WITH",
}
var keywords map[string]Token
func init() {
keywords = make(map[string]Token)
for tok := keywordBeg + 1; tok < keywordEnd; tok++ {
keywords[strings.ToLower(tokens[tok])] = tok
}
for _, tok := range []Token{AND, OR, XOR, NOT} {
keywords[strings.ToLower(tokens[tok])] = tok
}
keywords["true"] = TRUE
keywords["false"] = FALSE
keywords["null"] = NULL
}
// isOperator returns true for operator tokens.
func (tok Token) isOperator() bool { return tok > operatorBeg && tok < operatorEnd }
// String returns the string representation of the token.
func (tok Token) String() string {
if tok >= 0 && tok < Token(len(tokens)) {
return tokens[tok]
}
return ""
}
// tokstr returns a literal if provided, otherwise returns the token string.
func tokstr(tok Token, lit string) string {
if lit != "" {
return lit
}
return tok.String()
}
// Lookup returns the token associated with a given string.
func Lookup(ident string) Token {
if tok, ok := keywords[strings.ToLower(ident)]; ok {
return tok
}
return IDENT
}
// Pos specifies the line and character position of a token.
// The Char and Line are both zero-based indexes.
type Pos struct {
Line int
Char int
}