-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.go
433 lines (409 loc) · 9.73 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// Copyright 2022 The Mellium Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.
package xml
import (
"bufio"
"encoding/xml"
"errors"
"fmt"
"io"
"strings"
)
const xmlURL = "http://www.w3.org/XML/1998/namespace"
// Various types re-used from the standard library XML package, aliased here so
// that both packages don't need to be imported.
type (
Attr = xml.Attr
CharData = xml.CharData
Comment = xml.Comment
Directive = xml.Directive
EndElement = xml.EndElement
Name = xml.Name
ProcInst = xml.ProcInst
StartElement = xml.StartElement
TokenReader = xml.TokenReader
Token = xml.Token
Marshaler = xml.Marshaler
Encoder = xml.Encoder
TagPathError = xml.TagPathError
Decoder = xml.Decoder
Unmarshaler = xml.Unmarshaler
UnmarshalerAttr = xml.UnmarshalerAttr
MarshalerAttr = xml.MarshalerAttr
UnsupportedTypeError = xml.UnsupportedTypeError
SyntaxError = xml.SyntaxError
)
var (
MarshalIndent = xml.MarshalIndent
Marshal = xml.Marshal
Unmarshal = xml.Unmarshal
NewEncoder = xml.NewEncoder
NewTokenDecoder = xml.NewTokenDecoder
CopyToken = xml.CopyToken
EscapeText = xml.EscapeText
HTMLAutoClose = xml.HTMLAutoClose
HTMLEntity = xml.HTMLEntity
)
var errEarlyEOF = &SyntaxError{Msg: "early EOF"}
// NewDecoder creates a new XML parser reading from r.
// If r does not implement io.ByteReader, NewDecoder will do its own buffering.
func NewDecoder(r io.Reader) *Decoder {
return NewTokenDecoder(NewTokenizer(r))
}
// Tokenizer splits a reader into XML tokens without performing any verification
// or namespace resolution on those tokens.
type Tokenizer struct {
r io.ByteReader
foundStart bool
selfClose *xml.Name
prefixes []map[string]string
spaces []string
}
// NewTokenizer creates a new XML parser reading from r.
// If r does not implement io.ByteReader, NewDecoder will do its own buffering.
func NewTokenizer(r io.Reader) *Tokenizer {
t := &Tokenizer{}
if br, ok := r.(io.ByteReader); ok {
t.r = br
} else {
t.r = bufio.NewReader(r)
}
return t
}
// Token returns the next XML token in the input stream.
// At the end of the input stream, Token returns nil, io.EOF.
func (t *Tokenizer) Token() (Token, error) {
if t.selfClose != nil {
name := *t.selfClose
t.selfClose = nil
return xml.EndElement{Name: name}, nil
}
var b byte
var err error
if t.foundStart {
b = '<'
t.foundStart = false
} else {
b, err = t.r.ReadByte()
if err != nil {
return nil, err
}
}
// We found a CharData. Read until we consume another '<'.
if b != '<' {
// TODO: reuse buf
buf := []byte{b}
return decodeCharData(t, buf)
}
// We found a '<', figure out what it is.
b, err = t.r.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
return nil, errEarlyEOF
}
return nil, err
}
switch b {
case '!':
// Directive or comment
// TODO: reuse buffer
var buf []byte
b, err := t.r.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
return nil, errEarlyEOF
}
return nil, err
}
buf = append(buf, b)
if b == '-' {
b, err = t.r.ReadByte()
if err != nil {
if errors.Is(err, io.EOF) {
return nil, errEarlyEOF
}
return nil, err
}
buf = append(buf, b)
if b == '-' {
buf = buf[:0]
return decodeComment(t, buf)
} else {
return nil, &SyntaxError{Msg: "invalid sequence <!- not part of <!--"}
}
}
return decodeDirective(t, buf)
case '?':
// ProcInst <?target inst?>
// TODO: reuse buffer
tok, err := decodeProcInst(t, nil)
if err != nil {
return nil, err
}
return tok, nil
case '/':
return decodeEndElement(t)
}
// StartElement, or self-closing
return decodeStartElement(t, b)
}
func decodeStartElement(t *Tokenizer, b byte) (StartElement, error) {
t.spaces = append(t.spaces, "")
// TODO: defer make until we actually find a prefix?
t.prefixes = append(t.prefixes, make(map[string]string))
// TODO: check for space as sep?
name, sep, def, err := decodeName(t, b, false)
if err != nil {
return StartElement{}, err
}
// We use an empty array instead of nil to match the behavior of encoding/xml.
attr := []Attr{}
for {
// If we reach the end, don't decode any more attributes.
switch sep {
case 0x20, 0x9, 0xD, 0xA:
// Consume any spaces between the name and attributes.
sep, err = t.r.ReadByte()
if err != nil {
return StartElement{}, err
}
continue
case '/':
t.selfClose = &name
sep, err = t.r.ReadByte()
if err != nil {
return StartElement{}, err
}
if sep != '>' {
return StartElement{}, fmt.Errorf("xml: expected > to end the element, got %q", string(sep))
}
fallthrough
case '>':
return StartElement{Name: name, Attr: attr}, nil
}
// Decode the attribute we found.
a, err := decodeAttr(t, sep)
if err != nil {
return StartElement{}, err
}
sep, err = t.r.ReadByte()
if err != nil {
return StartElement{}, err
}
if a.Name.Local != "" {
attr = append(attr, a)
}
switch {
case a.Name.Space == "" && a.Name.Local == "xmlns":
name.Space = a.Value
def = true
t.spaces[len(t.spaces)-1] = a.Value
case a.Name.Space == "xmlns":
t.prefixes[len(t.prefixes)-1][a.Name.Local] = a.Value
if !def && name.Space != "" && name.Space == a.Name.Local {
name.Space = a.Value
}
}
}
return StartElement{Name: name, Attr: attr}, nil
}
func decodeEndElement(t *Tokenizer) (EndElement, error) {
defer func() {
if len(t.prefixes) > 0 {
t.prefixes = t.prefixes[:len(t.prefixes)-1]
}
if len(t.spaces) > 0 {
t.spaces = t.spaces[:len(t.spaces)-1]
}
}()
// TODO: check for space as sep?
name, _, _, err := decodeName(t, 0, false)
if err != nil {
return EndElement{}, err
}
return EndElement{Name: name}, nil
}
func decodeName(t *Tokenizer, b byte, attr bool) (Name, byte, bool, error) {
// Set to the previous default namespace. If we find a new namespace this will
// be overwritten later.
var space string
// Attributes don't get the default namespace.
if !attr {
for i := len(t.spaces) - 1; i >= 0; i-- {
space = t.spaces[i]
if space != "" {
break
}
}
}
// TODO: reuse builder
var foundSep bool
var rawFirst, rawSecond strings.Builder
if b != 0 {
/* #nosec */
rawFirst.WriteByte(b)
}
for {
b, err := t.r.ReadByte()
if err != nil {
return Name{}, 0, false, err
}
if !isNameByte(b) {
if foundSep {
space = rawFirst.String()
// Go backwards up the stack looking for a prefix definition. If we find
// one, replace the namespace with it
for i := len(t.prefixes) - 1; i >= 0; i-- {
if resolvedSpace := t.prefixes[i][space]; resolvedSpace != "" {
space = resolvedSpace
break
}
}
return Name{Space: space, Local: rawSecond.String()}, b, false, nil
} else {
return Name{Space: space, Local: rawFirst.String()}, b, space != "", nil
}
}
if b == ':' {
foundSep = true
continue
}
if foundSep {
/* #nosec */
rawSecond.WriteByte(b)
} else {
/* #nosec */
rawFirst.WriteByte(b)
}
}
}
func decodeAttr(t *Tokenizer, b byte) (Attr, error) {
name, sep, _, err := decodeName(t, b, true)
if err != nil {
return Attr{}, err
}
if sep != '=' {
return Attr{}, fmt.Errorf("xml: bad attribute separator %q", string(sep))
}
b, err = t.r.ReadByte()
if err != nil {
return Attr{}, err
}
if b != '\'' && b != '"' {
return Attr{}, fmt.Errorf("xml: expected quoted attribute value")
}
quote := b
// Get the value
// TODO: reuse builder
var raw strings.Builder
for {
b, err = t.r.ReadByte()
if err != nil {
return Attr{}, err
}
// TODO: what characters are valid in a name?
if b == quote {
return Attr{
Name: name,
Value: raw.String(),
}, nil
}
raw.WriteByte(b)
}
}
func decodeDirective(t *Tokenizer, dir []byte) (Directive, error) {
for {
b, err := t.r.ReadByte()
if err != nil {
return nil, err
}
if b == '>' {
return Directive(dir), nil
}
dir = append(dir, b)
}
}
func decodeComment(t *Tokenizer, comment []byte) (Comment, error) {
var found uint8
for {
b, err := t.r.ReadByte()
if err != nil {
return nil, err
}
switch {
case b == '-':
found++
continue
case b == '>' && found > 1:
return Comment(comment), nil
default:
for i := uint8(0); i < found; i++ {
comment = append(comment, '-')
}
found = 0
}
comment = append(comment, b)
}
}
func decodeProcInst(t *Tokenizer, inst []byte) (ProcInst, error) {
var (
foundSpace bool
foundEnd bool
target strings.Builder
)
for {
b, err := t.r.ReadByte()
if err != nil {
return ProcInst{}, err
}
switch b {
case '>':
// If we found ?>, this is the end and we can return the token.
if foundEnd {
return ProcInst{Target: target.String(), Inst: inst}, nil
}
foundSpace = true
case '?':
foundEnd = true
foundSpace = true
continue
case 0x20, 0x9, 0xD, 0xA:
if !foundSpace {
foundSpace = true
continue
}
}
if foundSpace {
if target.Len() == 0 {
return ProcInst{}, &SyntaxError{Msg: "xml: expected target name after <?"}
}
if foundEnd {
inst = append(inst, '?')
foundEnd = false
}
inst = append(inst, b)
} else {
/* #nosec */
target.WriteByte(b)
}
}
}
func decodeCharData(t *Tokenizer, buf []byte) (CharData, error) {
for {
b, err := t.r.ReadByte()
if err != nil {
return nil, err
}
if b == '<' {
t.foundStart = true
break
}
buf = append(buf, b)
}
// TODO: unescape bytes, or leave them and will the decoder do it?
return CharData(buf), nil
}
func isSpace(b byte) bool {
return b == 0x20 || b == 0x9 || b == 0xD || b == 0xA
}