-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.go
221 lines (197 loc) · 4.3 KB
/
tokenizer.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
package jsontokenizer
import (
"fmt"
"io"
)
// A TokType is an enum for JSON types.
type TokType int
// The following TokTypes are defined.
const (
TokNull TokType = iota
TokTrue
TokFalse
TokNumber
TokString
TokArrayOpen
TokArrayClose
TokObjectOpen
TokObjectClose
TokObjectColon
TokComma
defaultSize = 64
)
// Tokenizer reads and tokenizes JSON from an input stream.
type Tokenizer interface {
// Token returns next token. TokString and TokNumber tokens must be
// consumed by ReadString and ReadNumber respectively.
Token() (TokType, error)
// ReadNumber consumes number token by writing it into provided io.Writer.
ReadNumber(into io.Writer) (n int, err error)
// ReadString consumes string token by writing it into provided io.Writer.
ReadString(into io.Writer) (n int, err error)
// Reset resets state of Tokenizer so it can be re-used with another Reader.
Reset(in io.Reader)
}
var (
_ Tokenizer = &tokenizer{}
bnull = []byte("null")
btrue = []byte("true")
bfalse = []byte("false")
lookup = [256]byte{
'\t': 's', '\n': 's', '\r': 's', ' ': 's',
'+': '#', '-': '#', '.': '#', '0': '#', '1': '#',
'2': '#', '3': '#', '4': '#', '5': '#', '6': '#',
'7': '#', '8': '#', '9': '#', 'E': '#', 'e': '#',
}
toklookup = [256]TokType{
'{': TokObjectOpen, '}': TokObjectClose,
'[': TokArrayOpen, ']': TokArrayClose,
':': TokObjectColon, ',': TokComma,
'"': TokString, '-': TokNumber, '0': TokNumber,
'1': TokNumber, '2': TokNumber, '3': TokNumber,
'4': TokNumber, '5': TokNumber, '6': TokNumber,
'7': TokNumber, '8': TokNumber, '9': TokNumber,
}
)
type tokenizer struct {
in io.Reader
buf []byte
bufp int
bufe int
}
// New returns a new Tokenizer with default buffer size.
func New(in io.Reader) Tokenizer {
return NewWithSize(in, defaultSize)
}
// NewWithSize returns a new Tokenizer with custom buffer size.
func NewWithSize(in io.Reader, size int) Tokenizer {
return &tokenizer{in: in, buf: make([]byte, size)}
}
func (t *tokenizer) Token() (TokType, error) {
c, err := t.peek()
if err != nil {
return TokNull, err
}
switch toklookup[c] {
case 0:
switch c {
case 't':
return TokTrue, t.readWord(btrue)
case 'f':
return TokFalse, t.readWord(bfalse)
case 'n':
return TokNull, t.readWord(bnull)
default:
return TokNull, fmt.Errorf("invalid json %q", t.buf[t.bufp:t.bufe])
}
case TokObjectOpen, TokObjectClose, TokArrayOpen, TokArrayClose, TokObjectColon, TokComma:
t.bufp++
fallthrough
default:
return toklookup[c], nil
}
}
func (t *tokenizer) ReadNumber(into io.Writer) (n int, err error) {
for {
for i := t.bufp; i < t.bufe; i++ {
if lookup[t.buf[i]] == '#' {
continue
}
z, err := into.Write(t.buf[t.bufp:i])
n += z
if err != nil {
return n, err
}
t.bufp = i
return n, nil
}
z, err := into.Write(t.buf[t.bufp:t.bufe])
n += z
if err != nil {
return n, err
}
err = t.refill()
if err == io.EOF {
return n, nil
}
if err != nil {
return n, err
}
}
}
func (t *tokenizer) ReadString(into io.Writer) (n int, err error) {
var prev byte
t.bufp++
for {
for i, c := range t.buf[t.bufp:t.bufe] {
if c == '"' && prev != '\\' {
z, err := into.Write(t.buf[t.bufp : t.bufp+i])
n += z
t.bufp += i + 1
return n, err
}
if c == '\\' && prev == '\\' {
prev = 0
} else {
prev = c
}
}
z, err := into.Write(t.buf[t.bufp:t.bufe])
n += z
if err != nil {
return n, err
}
err = t.refill()
if err == io.EOF {
return n, nil
}
if err != nil {
return n, err
}
}
}
func (t *tokenizer) Reset(in io.Reader) {
t.bufp = 0
t.bufe = 0
t.in = in
}
func (t *tokenizer) readWord(w []byte) error {
for i, c := range w {
if t.bufe == t.bufp {
err := t.refill()
if err != nil {
if err == io.EOF {
return fmt.Errorf("expected %s got EOF", w)
}
return err
}
}
if t.buf[t.bufp] != c {
return fmt.Errorf("expected %s got %c at index %d", w, t.buf[t.bufp], i)
}
t.bufp++
}
return nil
}
func (t *tokenizer) peek() (byte, error) {
var err error
for {
for i := t.bufp; i < t.bufe; i++ {
c := t.buf[i]
if lookup[c] == 's' {
continue
}
t.bufp = i
return c, nil
}
if err != nil {
return 0, err
}
err = t.refill()
}
}
func (t *tokenizer) refill() (err error) {
t.bufp = 0
t.bufe, err = t.in.Read(t.buf)
return err
}