-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
217 lines (203 loc) · 5.69 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
package main
type Token struct {
Filename string
Value string
Type TokenType
Start int
End int
}
type TokenEmitter interface {
EmitToken(token *Token)
}
type TokenEmitterFunc func(token *Token)
func (f TokenEmitterFunc) EmitToken(token *Token) {
f(token)
}
type TokenIterator interface {
NextToken() *Token
}
type TokenEmitterIterator struct {
tokens chan *Token
end *Token
}
var _ TokenEmitter = (*TokenEmitterIterator)(nil)
var _ TokenIterator = (*TokenEmitterIterator)(nil)
func (e *TokenEmitterIterator) EmitToken(token *Token) {
e.tokens <- token
}
func (e *TokenEmitterIterator) NextToken() *Token {
tok, ok := <-e.tokens
if !ok {
return e.end
} else if tok.Type == TokEOF {
e.end = tok
close(e.tokens)
e.tokens = nil
}
return tok
}
func NewEmitterIterator() *TokenEmitterIterator {
return &TokenEmitterIterator{
tokens: make(chan *Token, 2),
}
}
type TokenType int
const (
TokError TokenType = -1 // Error token type which indicates error
TokEOF TokenType = iota // EOF token type which indicates end of input
TokIdentifier // identifier
TokConst // const
TokEnum // enum
TokModel // model
TokHttp // http
TokRpc // rpc
TokService // service
TokByte // byte
TokBool // bool
TokInt8 // int8
TokInt16 // int16
TokInt32 // int32
TokInt64 // int64
TokUint8 // uint8
TokUint16 // uint16
TokUint32 // uint32
TokUint64 // uint64
TokFloat32 // float32
TokFloat64 // float64
TokTimestamp // timestamp
TokString // string
TokMap // map
TokArray // array []
TokAny // any
TokFile // file
TokStream // stream
TokConstDuration // 1ns, 1us, 1ms, 1s, 1m, 1h
TokConstBytes // 1b, 1kb, 1mb, 1gb, 1tb, 1pb, 1eb
TokConstFloat // 1.0
TokConstInt // 1
TokConstStringSingleQuote // 'string'
TokConstStringDoubleQuote // "string"
TokConstStringBacktickQoute // `string`
TokConstBool // true, false
TokConstNull // null
TokReturn // =>
TokAssign // =
TokOptional // ?
TokColon // :
TokComma // ,
TokExtend // ...
TokOpenCurly // {
TokCloseCurly // }
TokOpenParen // (
TokCloseParen // )
TokOpenAngle // <
TokCloseAngle // >
TokComment // # comment
TokCustomError // error
)
func (tt TokenType) String() string {
switch tt {
case TokError:
return "Error"
case TokEOF:
return "EOF"
case TokIdentifier:
return "Identifier"
case TokConst:
return "Const"
case TokEnum:
return "Enum"
case TokModel:
return "Model"
case TokHttp:
return "Http"
case TokRpc:
return "Rpc"
case TokService:
return "Service"
case TokByte:
return "Byte"
case TokBool:
return "Bool"
case TokInt8:
return "Int8"
case TokInt16:
return "Int16"
case TokInt32:
return "Int32"
case TokInt64:
return "Int64"
case TokUint8:
return "Uint8"
case TokUint16:
return "Uint16"
case TokUint32:
return "Uint32"
case TokUint64:
return "Uint64"
case TokFloat32:
return "Float32"
case TokFloat64:
return "Float64"
case TokTimestamp:
return "Timestamp"
case TokString:
return "String"
case TokMap:
return "Map"
case TokArray:
return "Array"
case TokAny:
return "Any"
case TokFile:
return "File"
case TokStream:
return "Stream"
case TokConstDuration:
return "ConstDuration"
case TokConstBytes:
return "ConstBytes"
case TokConstFloat:
return "ConstFloat"
case TokConstInt:
return "ConstInt"
case TokConstStringSingleQuote:
return "ConstStringSingleQuote"
case TokConstStringDoubleQuote:
return "ConstStringDoubleQuote"
case TokConstStringBacktickQoute:
return "ConstStringBacktickQoute"
case TokConstBool:
return "ConstBool"
case TokConstNull:
return "ConstNull"
case TokReturn:
return "Return"
case TokAssign:
return "Assign"
case TokColon:
return "Colon"
case TokComma:
return "Comma"
case TokExtend:
return "Extend"
case TokOpenCurly:
return "OpenCurly"
case TokCloseCurly:
return "CloseCurly"
case TokOpenParen:
return "OpenParen"
case TokCloseParen:
return "CloseParen"
case TokOpenAngle:
return "OpenAngle"
case TokCloseAngle:
return "CloseAngle"
case TokComment:
return "Comment"
case TokCustomError:
return "CustomError"
default:
return "Unknown"
}
}