-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
385 lines (334 loc) · 5.73 KB
/
scanner.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package thriftfile
import (
"fmt"
gotoken "go/token"
"unicode"
"unicode/utf8"
)
const (
bom = 0xFEFF
eof = -1
)
type scanner struct {
file *gotoken.File
src []byte
err func(pos gotoken.Position, msg string)
ch rune
offset int
rdOffset int
lineOffset int
tok token
pos gotoken.Pos
}
func (s *scanner) init(file *gotoken.File, src []byte, err func(pos gotoken.Position, msg string)) {
s.file = file
s.src = src
s.err = err
s.ch = ' '
s.offset = 0
s.rdOffset = 0
s.lineOffset = 0
s.tok = _ILLEGAL
s.pos = gotoken.NoPos
s.nextch()
if s.ch == bom {
s.nextch()
}
}
func (s *scanner) lit() string {
return string(s.src[s.file.Offset(s.pos):s.offset])
}
func (s *scanner) error(offs int, msg string) {
if s.err != nil {
s.err(s.file.Position(s.file.Pos(offs)), msg)
}
}
func (s *scanner) errorf(offs int, format string, args ...any) {
s.error(offs, fmt.Sprintf(format, args...))
}
func (s *scanner) nextch() {
if s.rdOffset < len(s.src) {
s.offset = s.rdOffset
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
r, w := rune(s.src[s.rdOffset]), 1
switch {
case r == 0:
s.error(s.offset, "illegal character NUL")
case r >= utf8.RuneSelf:
// not ASCII
r, w = utf8.DecodeRune(s.src[s.rdOffset:])
if r == utf8.RuneError && w == 1 {
s.error(s.offset, "illegal UTF-8 encoding")
} else if r == bom && s.offset > 0 {
s.error(s.offset, "illegal byte order mark")
}
}
s.rdOffset += w
s.ch = r
} else {
s.offset = len(s.src)
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
s.ch = eof
}
}
func (s *scanner) next() {
redo:
for s.ch == '\n' || s.ch == '\r' || s.ch == '\t' || s.ch == ' ' {
s.nextch()
}
s.pos = s.file.Pos(s.offset)
for isLetter(s.ch) || s.ch >= utf8.RuneSelf && s.atIdent(true) {
s.nextch()
s.ident()
return
}
switch s.ch {
case eof:
s.tok = _EOF
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
s.number(false)
case '"', '\'':
s.string()
case '+':
s.nextch()
if isDecimal(s.ch) {
s.number(false)
} else {
s.tok = _ADD
}
case '-':
s.nextch()
if isDecimal(s.ch) {
s.number(false)
} else {
s.tok = _SUB
}
case '*':
s.nextch()
s.tok = _MUL
case '/':
s.nextch()
if s.ch == '/' {
s.nextch()
s.lineComment()
return
}
switch s.ch {
case '/':
s.nextch()
s.lineComment()
case '*':
s.nextch()
s.fullComment()
default:
s.tok = _QUO
}
case '%':
s.nextch()
s.tok = _REM
case '&':
s.nextch()
s.tok = _AND
case '|':
s.nextch()
s.tok = _OR
case '^':
s.nextch()
s.tok = _XOR
case '=':
s.nextch()
if s.ch == '=' {
s.nextch()
s.tok = _EQL
} else {
s.tok = _ASSIGN
}
case '<':
s.nextch()
s.tok = _LSS
case '>':
s.nextch()
s.tok = _GTR
case '!':
s.nextch()
s.tok = _NOT
case '(':
s.nextch()
s.tok = _LPAREN
case '[':
s.nextch()
s.tok = _LBRACK
case '{':
s.nextch()
s.tok = _LBRACE
case ',':
s.nextch()
s.tok = _COMMA
case '.':
s.nextch()
s.tok = _PERIOD
case ')':
s.nextch()
s.tok = _RPAREN
case ']':
s.nextch()
s.tok = _RBRACK
case '}':
s.nextch()
s.tok = _RBRACE
case ';':
s.nextch()
s.tok = _SEMICOLON
case ':':
s.nextch()
s.tok = _COLON
case '#':
s.nextch()
s.lineComment()
default:
s.errorf(s.offset, "invalid character %#U", s.ch)
s.nextch()
goto redo
}
}
func (s *scanner) ident() {
for isLetter(s.ch) || isDecimal(s.ch) || s.ch == '.' {
s.nextch()
}
if s.ch >= utf8.RuneSelf {
for s.atIdent(false) {
s.nextch()
}
}
if lit := s.lit(); len(lit) > 2 {
if tok, ok := keywords[lit]; ok {
s.tok = tok
return
}
}
s.tok = _IDENT
}
func (s *scanner) atIdent(first bool) bool {
switch {
case unicode.IsLetter(s.ch) || s.ch == '_':
return true
case unicode.IsDigit(s.ch) || s.ch == '.':
if first {
s.errorf(s.offset, "identifier cannot begin with %#U", s.ch)
}
default:
return false
}
return true
}
func (s *scanner) number(seenPoint bool) {
tok := _INT
if !seenPoint {
if s.ch == '0' {
s.nextch()
switch lower(s.ch) {
case 'x':
s.nextch()
for isHex(s.ch) {
s.nextch()
}
s.tok = _INT
return
}
}
for isDecimal(s.ch) {
s.nextch()
}
if s.ch == '.' {
s.nextch()
seenPoint = true
}
}
if seenPoint {
tok = _FLOAT
for isDecimal(s.ch) {
s.nextch()
}
}
if lower(s.ch) == 'e' {
s.nextch()
tok = _FLOAT
if s.ch == '+' || s.ch == '-' {
s.nextch()
}
for isDecimal(s.ch) {
s.nextch()
}
}
s.tok = tok
}
func (s *scanner) string() {
quote := s.ch
s.nextch()
for {
if s.ch == quote {
s.nextch()
break
}
if s.ch == '\\' {
s.nextch()
if !s.escape(quote) {
return
}
continue
}
if s.ch == '\n' {
s.error(s.offset, "newline in string")
return
}
if s.ch < 0 {
s.error(s.offset, "string not terminated")
break
}
s.nextch()
}
s.tok = _STRING
}
func (s *scanner) escape(quote rune) bool {
switch s.ch {
case quote, '\\', 'n', 'r', 't':
s.nextch()
return true
default:
if s.ch < 0 {
return true
}
s.error(s.offset, "unknown escape")
return false
}
}
func (s *scanner) lineComment() {
s.tok = _COMMENT
for s.ch >= 0 && s.ch != '\n' {
s.nextch()
}
}
func (s *scanner) fullComment() {
s.tok = _COMMENT
for s.ch >= 0 {
for s.ch == '*' {
s.nextch()
if s.ch == '/' {
s.nextch()
return
}
}
s.nextch()
}
s.error(s.offset, "comment not terminated")
}
func lower(ch rune) rune { return ('a' - 'A') | ch }
func isLetter(ch rune) bool { return 'a' <= lower(ch) && lower(ch) <= 'z' || ch == '_' }
func isDecimal(ch rune) bool { return '0' <= ch && ch <= '9' }
func isHex(ch rune) bool { return isDecimal(ch) || 'a' <= lower(ch) && lower(ch) <= 'f' }