This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
394 lines (315 loc) · 10.6 KB
/
parser.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
package fastxml
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"unicode/utf8"
"unsafe"
)
var _ = xml.Header
var (
ErrNotAValidTag = errors.New("not a valid tag")
ErrInvalidClosingElement = errors.New("invalid closing tag")
)
var (
docTypePrefix = []byte("<!DOCTYPE")
elementPrefix = []byte("<!ELEMENT")
attListPrefix = []byte("<!ATTLIST")
)
// TokenDecoderFunc if no token can be decoded - error MUST be returned.
type TokenDecoderFunc func([]byte) (xml.Token, error)
// Parser currently guarantees to supports only ASCII, UTF8 might chars/sequences be broken.
type Parser struct {
// buf holds full data to parse.
buf []byte
// lastTagName is the last found open tag name.
// This is necessary for self closing tags. For them there will be two events:
// startElement and then endElement with the same name.
lastTagName string
// innerData holds all available types that will be returned to the caller.
innerData struct {
charData CharData // "text between tags"
comment Comment // <!-- comment -->
directive Directive // <!directive>
startElement StartToken // <some_tag>
endElement EndElement // </some_tag>
procInst ProcInst // <?xmxl encoding="UTF-8" ?>
}
// currentPointer ALWAYS points to next byte that needs to be processed.
currentPointer uint32
}
// NewParser will create a parser from input bytes.
//
// Parser MUST own provided buffer, so if input buffer must be modified outside of the parer -
// set `mustCopy` to true and parser will copy full buffer to new slice and will use that.
func NewParser(buf []byte, mustCopy bool) *Parser {
if mustCopy {
newBuf := append([]byte(nil), buf...)
buf = newBuf
}
p := Parser{
buf: buf,
}
return &p
}
// Peek can be used to fetch next token without actually advancing parser.
//
// Basically it is wrapper for Parser.Next with state restoration.
func (p *Parser) Peek() (xml.Token, error) {
lastPos, lastTagName := p.currentPointer, p.lastTagName
defer func() {
p.currentPointer, p.lastTagName = lastPos, lastTagName
}()
return p.Next()
}
// Next will return next token and error, if any.
//
// Returned token will always be a pointer type.
//
// Caller MUST NOT hold onto returned tokens. Instead, it may store data from them, but don't hold onto pointers.
func (p *Parser) Next() (xml.Token, error) {
if p.lastTagName != "" {
token := p.sendSelfClosingEnd()
p.lastTagName = ""
return token, nil
}
if p.currentPointer >= uint32(len(p.buf)) {
return nil, io.EOF
}
tokenBytes, err := FetchNextToken(p.buf[p.currentPointer:])
if err != nil {
return nil, fmt.Errorf("fetch next token: %w", err)
}
p.currentPointer += uint32(len(tokenBytes))
token, err := p.decodeToken(tokenBytes)
if err != nil {
return nil, fmt.Errorf("decode token: index position %d: %w", p.currentPointer, err)
}
return token, nil
}
// decodeToken receives a buffer for next token and tries to decode it.
//
// Returned token cannot be copied or modified.
// It is valid to copy data from the token.
func (p *Parser) decodeToken(buf []byte) (xml.Token, error) { //nolint:gocyclo,cyclop // Performance matters
if len(buf) == 0 {
return nil, io.ErrUnexpectedEOF
}
if len(buf) < 3 && buf[0] == '<' {
return nil, ErrNotAValidTag
}
switch {
case buf[0] != '<':
return p.decodeString(buf)
case buf[0] == '<' && buf[1] == '/':
return p.decodeClosingTag(buf)
case buf[0] == '<' && buf[1] == '!' && buf[2] == '-' && buf[3] == '-':
return p.decodeComment(buf)
case len(buf) >= 11 && buf[0] == '<' && buf[1] == '!' && buf[2] == '[':
return p.decodeCdata(buf)
case buf[0] == '<' && buf[1] == '?':
return nil, nil // No implementation is available currently.
case buf[0] == '<' && buf[1] == '!':
return p.decodeDeclaration(buf) // Some sort of declaration(ignore, element, attrlist, etc).
default: // This will be our "catch-all" start tag decoder.
return p.decodeSimpleTag(buf)
}
}
func (p *Parser) sendSelfClosingEnd() xml.Token {
p.innerData.endElement.Name.Local = p.lastTagName
return &p.innerData.endElement
}
// decodeClosingTag is used to decode closing tag.
func (p *Parser) decodeClosingTag(buf []byte) (xml.Token, error) {
if len(buf) < 4 || buf[2] == '>' {
return nil, ErrInvalidClosingElement
}
buf = buf[2:]
nameEndIdx := scanTillWordEnd(buf)
if nameEndIdx == 0 {
return nil, ErrInvalidClosingElement
}
_ = buf[nameEndIdx] // Remove boundary check
p.innerData.endElement.Name.Local = unsafeByteToString(buf[:nameEndIdx])
return &p.innerData.endElement, nil
}
func (p *Parser) decodeComment(buf []byte) (xml.Token, error) {
commentEndIdx := bytes.Index(buf, []byte{'-', '-', '>'})
if commentEndIdx == -1 || (buf[commentEndIdx-1] == '-' && len(buf) < 7) {
return nil, errors.New("comment is not properly formatted")
}
p.innerData.comment = buf[4:commentEndIdx]
return &p.innerData.comment, nil
}
func (p *Parser) decodeCdata(buf []byte) (xml.Token, error) {
p.innerData.charData = buf[cdataPrefLen : len(buf)-cdataSufLen]
return &p.innerData.charData, nil
}
func (p *Parser) decodeString(buf []byte) (xml.Token, error) {
p.innerData.charData = buf
return &p.innerData.charData, nil
}
func (p *Parser) decodeSimpleTag(buf []byte) (xml.Token, error) {
tagNameIdx := scanTillWordEnd(buf[1:])
tagName := unsafeByteToString(buf[1 : tagNameIdx+1])
if buf[len(buf)-2] == '/' {
p.lastTagName = tagName
}
p.innerData.startElement.Name = tagName
p.innerData.startElement.attrBuf = nil
buf = buf[tagNameIdx+1:]
// Skip byte if it is space
var skipIdx int
for ; skipIdx < len(buf) && IsHTMLSpaceChar(rune(buf[skipIdx])); skipIdx++ {
}
buf = buf[skipIdx:]
if buf[0] != '>' && buf[0] != '/' {
p.innerData.startElement.attrBuf = buf
}
// Currently we are not supporting attributes.
// Plan is to have some sort of a function that will parse attributes on demand.
return &p.innerData.startElement, nil
}
func (p *Parser) decodeDeclaration(buf []byte) (xml.Token, error) {
switch {
case bytes.HasPrefix(buf, docTypePrefix),
bytes.HasPrefix(buf, elementPrefix),
bytes.HasPrefix(buf, attListPrefix):
return nil, nil
default:
return nil, fmt.Errorf("unknown declaration: %s", buf[:NextNonSpaceIndex(buf)])
}
}
func decodeTagAttribute(buf []byte) (string, string, int, error) {
if len(buf) == 0 || buf[0] == '>' {
return "", "", -1, nil
}
if bytes.IndexByte(buf, '=') == -1 {
return "", "", 0, errors.New("no equal sign in attributes")
}
nonSpaceIdx := NextNonSpaceIndex(buf)
if buf[nonSpaceIdx] == '>' {
return "", "", -1, nil
}
// Fetch attribute name and position where it ends.
attrName, endAttrNameIdx, err := NextWord(buf)
if err != nil {
return "", "", 0, err
}
// Now we need to find equal sign and pass over it.
equalIdx := nextTokenStartIndex(buf[endAttrNameIdx-1:], '=')
attrValue, endAttrValueIdx, err := NextQuotedWord(buf[endAttrNameIdx+equalIdx:])
if err != nil {
return "", "", 0, err
}
// 1 is added to skip index to go over the last quotation mark.
return attrName, attrValue, endAttrNameIdx + endAttrValueIdx + equalIdx + 1, nil
}
// CopyString will return copy of the input string.
//
// Call this function if you would like to get a copy of a string provided in a Token.
//
// Strings in the returned tokens are only pointers to input buffer.
// As such if data changes in input buffer - values of strings will also change.
func CopyString(s string) string {
return string([]byte(s))
}
// NextWord will return next word that possibly was located after some spaces.
func NextWord(buf []byte) (word string, endIdx int, err error) {
var startIdx int
startIdx, endIdx, err = NextWordIndex(buf)
if err != nil {
return "", 0, err
}
return unsafeByteToString(buf[startIdx:endIdx]), endIdx, nil
}
// NextQuotedWord will return next quoted word that possibly was located after some spaces.
func NextQuotedWord(buf []byte) (word string, endIdx int, err error) {
var startIdx int
startIdx, endIdx, err = NextQuotedWordIndex(buf)
if err != nil {
return "", 0, err
}
return unsafeByteToString(buf[startIdx+1 : endIdx]), endIdx, nil
}
// NextWordIndex returns two offsets: for start and the end of the word.
// Word is a sequence of alphabetic characters separated by underscore.
//
// On error `start` will hold starting index of the rune that is invalid, `end` will be always 0.
func NextWordIndex(buf []byte) (start, end int, err error) {
start = NextNonSpaceIndex(buf)
currPtr := start
decodedRune, size := utf8.DecodeRune(buf[currPtr:])
if !isNameStartChar(decodedRune) {
return currPtr, 0, fmt.Errorf("rune is not valid start of name: '%c'", decodedRune)
}
for {
currPtr += size
if currPtr >= len(buf) { // whole buf is proper chars.
return start, currPtr, nil
}
decodedRune, size = utf8.DecodeRune(buf[currPtr:])
// Check if name is finished
if IsHTMLSpaceChar(decodedRune) || decodedRune == '=' {
return start, currPtr, nil
}
if !isNameChar(decodedRune) {
return currPtr, 0, fmt.Errorf("rune is not valid name part: '%c'", decodedRune)
}
}
}
// NextQuotedWordIndex returns two offsets: for start and the end of the quotes.
// This means that caller MUST do something like `buf[start+1:start+1+end-1]` to get actual word.
//
// Word is a sequence of alphabetic characters separated by underscore.
//
// On error `start` will hold starting index of the rune that is invalid, `end` will be always 0.
//
// Returned indexes will not include quotation mark itself.
//
// Note: current implementation differs from NextWordIndex in a way that
// this function does not validate runes inside of found word.
func NextQuotedWordIndex(buf []byte) (start, end int, err error) {
start = NextNonSpaceIndex(buf)
quote := buf[start]
if quote != '\'' && quote != '"' {
return 0, 0, errors.New("no quotation mark on the beginning of the word")
}
end = bytes.IndexByte(buf[start+1:], quote)
if end == -1 {
return 0, 0, errors.New("word is not properly quoted")
}
return start, start + end + 1, nil
}
// NextNonSpaceIndex will return index on which next rune will be non-space.
func NextNonSpaceIndex(buf []byte) (idx int) {
for {
rn, size := utf8.DecodeRune(buf[idx:])
if !IsHTMLSpaceChar(rn) {
return
}
idx += size
}
}
func IsHTMLSpaceChar(rn rune) bool {
switch rn {
case ' ', '\t', '\r', '\n':
return true
default:
return false
}
}
func isNameStartChar(rn rune) bool {
return (rn >= 'a' && rn <= 'z') ||
(rn >= 'A' && rn <= 'Z') ||
rn == ':' || rn == '_'
}
func isNameChar(rn rune) bool {
return isNameStartChar(rn) || rn == '-' || rn == '.' ||
(rn >= '0' && rn <= '9')
}
func unsafeByteToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b)) // nolint:gosec // This is valid and simple conversion.
}