-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
token.go
357 lines (303 loc) · 6.65 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package token
import (
"fmt"
"github.com/zeromicro/go-zero/tools/goctl/pkg/parser/api/placeholder"
"github.com/zeromicro/go-zero/tools/goctl/util"
)
const (
Syntax = "syntax"
Info = "info"
Service = "service"
Returns = "returns"
Any = "any"
TypeKeyword = "type"
MapKeyword = "map"
ImportKeyword = "import"
)
// Type is the type of token.
type Type int
// EofToken is the end of file token.
var EofToken = Token{Type: EOF}
// ErrorToken is the error token.
var ErrorToken = Token{Type: error}
// Token is the token of a rune.
type Token struct {
Type Type
Text string
Position Position
}
// Fork forks token for a given Type.
func (t Token) Fork(tp Type) Token {
return Token{
Type: tp,
Text: t.Text,
Position: t.Position,
}
}
// IsEmptyString returns true if the token is empty string.
func (t Token) IsEmptyString() bool {
if t.Type != STRING && t.Type != RAW_STRING {
return false
}
text := util.TrimWhiteSpace(t.Text)
return text == `""` || text == "``"
}
// IsComment returns true if the token is comment.
func (t Token) IsComment() bool {
return t.IsType(COMMENT)
}
// IsDocument returns true if the token is document.
func (t Token) IsDocument() bool {
return t.IsType(DOCUMENT)
}
// IsType returns true if the token is the given type.
func (t Token) IsType(tp Type) bool {
return t.Type == tp
}
// Line returns the line number of the token.
func (t Token) Line() int {
return t.Position.Line
}
// String returns the string of the token.
func (t Token) String() string {
if t == ErrorToken {
return t.Type.String()
}
return fmt.Sprintf("%s %s %s", t.Position.String(), t.Type.String(), t.Text)
}
// Valid returns true if the token is valid.
func (t Token) Valid() bool {
return t.Type != token_bg
}
// IsKeyword returns true if the token is keyword.
func (t Token) IsKeyword() bool {
return golang_keyword_beg < t.Type && t.Type < golang_keyword_end
}
// IsBaseType returns true if the token is base type.
func (t Token) IsBaseType() bool {
_, ok := baseDataType[t.Text]
return ok
}
// IsHttpMethod returns true if the token is http method.
func (t Token) IsHttpMethod() bool {
_, ok := httpMethod[t.Text]
return ok
}
// Is returns true if the token text is one of the given list.
func (t Token) Is(text ...string) bool {
for _, v := range text {
if t.Text == v {
return true
}
}
return false
}
const (
token_bg Type = iota
error
ILLEGAL
EOF
COMMENT
DOCUMENT
literal_beg
IDENT // main
INT // 123
DURATION // 3s,3ms
STRING // "abc"
RAW_STRING // `abc`
PATH // `abc`
KEY // `abc:`
literal_end
operator_beg
SUB // -
MUL // *
QUO // /
ASSIGN // =
LPAREN // (
LBRACK // [
LBRACE // {
COMMA // ,
DOT // .
RPAREN // )
RBRACE // }
RBRACK // ]
SEMICOLON // ;
COLON // :
ELLIPSIS
operator_end
golang_keyword_beg
BREAK
CASE
CHAN
CONST
CONTINUE
DEFAULT
DEFER
ELSE
FALLTHROUGH
FOR
FUNC
GO
GOTO
IF
IMPORT
INTERFACE
MAP
PACKAGE
RANGE
RETURN
SELECT
STRUCT
SWITCH
TYPE
VAR
golang_keyword_end
api_keyword_bg
AT_DOC
AT_HANDLER
AT_SERVER
ANY
api_keyword_end
token_end
)
// String returns the string of the token type.
func (t Type) String() string {
if t >= token_bg && t < token_end {
return tokens[t]
}
return ""
}
var tokens = [...]string{
ILLEGAL: "ILLEGAL",
EOF: "EOF",
COMMENT: "COMMENT",
DOCUMENT: "DOCUMENT",
IDENT: "IDENT",
INT: "INT",
DURATION: "DURATION",
STRING: "STRING",
RAW_STRING: "RAW_STRING",
PATH: "PATH",
KEY: "KEY",
SUB: "-",
MUL: "*",
QUO: "/",
ASSIGN: "=",
LPAREN: "(",
LBRACK: "[",
LBRACE: "{",
COMMA: ",",
DOT: ".",
RPAREN: ")",
RBRACK: "]",
RBRACE: "}",
SEMICOLON: ";",
COLON: ":",
ELLIPSIS: "...",
BREAK: "break",
CASE: "case",
CHAN: "chan",
CONST: "const",
CONTINUE: "continue",
DEFAULT: "default",
DEFER: "defer",
ELSE: "else",
FALLTHROUGH: "fallthrough",
FOR: "for",
FUNC: "func",
GO: "go",
GOTO: "goto",
IF: "if",
IMPORT: "import",
INTERFACE: "interface",
MAP: "map",
PACKAGE: "package",
RANGE: "range",
RETURN: "return",
SELECT: "select",
STRUCT: "struct",
SWITCH: "switch",
TYPE: "type",
VAR: "var",
AT_DOC: "@doc",
AT_HANDLER: "@handler",
AT_SERVER: "@server",
ANY: "interface{}",
}
// HttpMethods returns the http methods.
var HttpMethods = []interface{}{"get", "head", "post", "put", "patch", "delete", "connect", "options", "trace"}
var httpMethod = map[string]placeholder.Type{
"get": placeholder.PlaceHolder,
"head": placeholder.PlaceHolder,
"post": placeholder.PlaceHolder,
"put": placeholder.PlaceHolder,
"patch": placeholder.PlaceHolder,
"delete": placeholder.PlaceHolder,
"connect": placeholder.PlaceHolder,
"options": placeholder.PlaceHolder,
"trace": placeholder.PlaceHolder,
}
var keywords = map[string]Type{
// golang_keyword_bg
"break": BREAK,
"case": CASE,
"chan": CHAN,
"const": CONST,
"continue": CONTINUE,
"default": DEFAULT,
"defer": DEFER,
"else": ELSE,
"fallthrough": FALLTHROUGH,
"for": FOR,
"func": FUNC,
"go": GO,
"goto": GOTO,
"if": IF,
"import": IMPORT,
"interface": INTERFACE,
"map": MAP,
"package": PACKAGE,
"range": RANGE,
"return": RETURN,
"select": SELECT,
"struct": STRUCT,
"switch": SWITCH,
"type": TYPE,
"var": VAR,
// golang_keyword_end
}
var baseDataType = map[string]placeholder.Type{
"bool": placeholder.PlaceHolder,
"uint8": placeholder.PlaceHolder,
"uint16": placeholder.PlaceHolder,
"uint32": placeholder.PlaceHolder,
"uint64": placeholder.PlaceHolder,
"int8": placeholder.PlaceHolder,
"int16": placeholder.PlaceHolder,
"int32": placeholder.PlaceHolder,
"int64": placeholder.PlaceHolder,
"float32": placeholder.PlaceHolder,
"float64": placeholder.PlaceHolder,
"complex64": placeholder.PlaceHolder,
"complex128": placeholder.PlaceHolder,
"string": placeholder.PlaceHolder,
"int": placeholder.PlaceHolder,
"uint": placeholder.PlaceHolder,
"uintptr": placeholder.PlaceHolder,
"byte": placeholder.PlaceHolder,
"rune": placeholder.PlaceHolder,
"any": placeholder.PlaceHolder,
}
// LookupKeyword returns the keyword type if the given ident is keyword.
func LookupKeyword(ident string) (Type, bool) {
tp, ok := keywords[ident]
return tp, ok
}
// NewIllegalToken returns a new illegal token.
func NewIllegalToken(b rune, pos Position) Token {
return Token{
Type: ILLEGAL,
Text: string(b),
Position: pos,
}
}