-
Notifications
You must be signed in to change notification settings - Fork 2
/
token.go
49 lines (45 loc) · 921 Bytes
/
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
package jsonast
type token struct {
char string
}
func (t token) isDigit() bool {
r := t.char
return r == "0" ||
r == "1" ||
r == "2" ||
r == "3" ||
r == "4" ||
r == "5" ||
r == "6" ||
r == "7" ||
r == "8" ||
r == "9"
}
var commaToken = token{char: `,`}
var quoteToken = token{char: `"`}
var arrayStartToken = token{char: `[`}
var arrayEndToken = token{char: `]`}
var objectStartToken = token{char: `{`}
var objectEndToken = token{char: `}`}
func tokenize(str string, ch chan<- token) {
for _, char := range str {
var tkn token
if char == ',' {
tkn = commaToken
} else if char == '"' {
tkn = quoteToken
} else if char == '[' {
tkn = arrayStartToken
} else if char == ']' {
tkn = arrayEndToken
} else if char == '{' {
tkn = objectStartToken
} else if char == '}' {
tkn = objectEndToken
} else {
tkn = token{char: string(char)}
}
ch <- tkn
}
close(ch)
}