-
Notifications
You must be signed in to change notification settings - Fork 1
/
token.go
112 lines (104 loc) · 2.03 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
package aml
const (
NUMBER = iota + 4 // number
STRING // string
COMMA // ,
LBRACKET // [
RBRACKET // ]
LBRACE // {
RBRACE // }
LANGLE // <
RANGLE // >
LGROUP // (
RGROUP // )
AUTO // auto
NUM // num
STR // str
TYPE // type
REQUIRED // required
OPTIONAL // optional
DEPRECATE // deprecate
GET // get method
POST // post method
OPTION // option method
PUT // put method
DELETE // delete method
HEAD // head method
PATCH // patch method
BOOL // bool
FROM // from
IMPORT // import
COLON // :
IDENTIFIER // identifier
ASSIGNMENT // =
)
// 最小词语单元
type Token struct {
Kind int
Value string
}
// 判空
func (t *Token) NotNull() bool {
return t.Kind > 0
}
// 新建
func (t *Token) New(kind int, value string) {
t.Kind = kind
t.Value = value
}
// 设置
func (t *Token) Set(n *Token) *Token {
t.Kind = n.Kind
t.Value = n.Value
return t
}
// 清除
func (t *Token) Reset() {
t.Kind = -1
}
// 切换
func (t *Token) Shift(n *Token) {
t.Set(n)
n.Reset()
}
// 关键字
func GetKind(result string) int {
switch result {
case "from":
return FROM
case "import":
return IMPORT
case "auto":
return AUTO
case "num":
return NUM
case "str":
return STR
case "type":
return TYPE
case "bool":
return BOOL
case "GET":
return GET
case "POST":
return POST
case "OPTION":
return OPTION
case "PUT":
return PUT
case "DELETE":
return DELETE
case "HEAD":
return HEAD
case "PATCH":
return PATCH
case "required":
return REQUIRED
case "optional":
return OPTIONAL
case "deprecate":
return DEPRECATE
default:
return IDENTIFIER
}
}