-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
358 lines (306 loc) · 7.37 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
package main
import (
"bufio"
"fmt"
"log"
"strings"
)
/**
* Takes the HTML document as an input string.
* Instantiates a buffered string reader.
* Consumes characters from reader as part of a recursive descent parser.
* The grammar followed is defined in grammar.txt
* Read more: https://en.wikipedia.org/wiki/Recursive_descent_parser
*/
type Parser struct {
input string
position int
verbose bool
reader *bufio.Reader
}
func NewParser(input string, position int, verbose bool) *Parser {
sr := strings.NewReader(input)
var reader = bufio.NewReader(sr)
return &Parser{
input,
position,
verbose,
reader,
}
}
/**
* Accept the next byte if it's any of the provided check bytes.
*/
func (p *Parser) accept(check ...byte) bool {
var next, err = p.reader.Peek(1)
if HandleIOError(err) {
return false
}
for _, checkByte := range check {
if checkByte == next[0] {
_, err = p.reader.ReadByte()
if HandleIOError(err) {
return false
}
return true
}
}
return false
}
/**
* Asserts that the next len(check) bytes match the check string.
*/
func (p *Parser) assertString(check string) bool {
var next, err = p.reader.Peek(len(check))
if HandleIOError(err) {
return false
}
return string(next) == check
}
/**
* Peeks ahead and consumes a string if it's found.
*/
func (p *Parser) acceptString(check string) bool {
if p.assertString(check) {
p.reader.Discard(len(check))
return true
}
return false
}
/**
* Consumes byte provided test function returns true.
*/
func (p *Parser) acceptByteGivenTest(test func(val byte) bool) (byte, bool) {
var next, err = p.reader.Peek(1)
if HandleIOError(err) {
return 0, false
}
valid := test(next[0])
if valid {
_, err = p.reader.ReadByte()
if HandleIOError(err) {
return 0, false
}
return next[0], true
}
return 0, false
}
/**
* Consumes all the bytes until provided test function returns false.
*/
func (p *Parser) acceptBytesUntilTest(test func(val byte) bool) string {
var sb strings.Builder
var state = true
var val byte
for state {
val, state = p.acceptByteGivenTest(test)
if state {
sb.WriteByte(val)
}
}
if len(sb.String()) == 0 {
return ""
}
return sb.String()
}
// Consumes nothingness: carriage returns, newlines and spaces.
func (p *Parser) consumeWhitespace() {
state := true
for state == true {
state = p.accept('\r', '\n', ' ', '\t')
}
}
func isAlphanumericOrPunctuation(check byte) bool {
return check != '>' && check != '<' && check != '\r' && check != '\n'
}
func isAttributeSplit(check byte) bool {
return check != '=' && check != '>' && check != ' '
}
func isSingleQuote(check byte) bool {
return check != '\''
}
func isDoubleQuote(check byte) bool {
return check != '"'
}
/* Actual parsing starts here */
func (p *Parser) Parse() *DOMNode {
var rootNode = p.document()
return rootNode
}
func (p *Parser) document() *DOMNode {
p.consumeWhitespace()
p.acceptString("<!DOCTYPE html>")
var rootNode = p.node()
return rootNode
}
/**
* Parses a single node.
* TODO: Omit non rendered head tags from the final DOM tree.
*/
func (p *Parser) node() *DOMNode {
p.consumeWhitespace()
// accept html comment if it exists.
if c := p.comment(); c != nil {
return c
}
// accept html open tag if it exists
openTag, attributes, selfClosing := p.openTag()
// if this isn't a comment or an html tag, it must be a piece of plaintext
if openTag == "" {
return p.text()
}
node := &DOMNode{
tag: openTag,
attributes: attributes,
selfClosing: selfClosing,
}
// Self closing tags can't have children :(
if selfClosing {
return node
}
// recursively discover child nodes.
for n := p.node(); n != nil; {
node.children = append(node.children, n)
n = p.node()
}
// accept html close tag
// very permissive, we don't check if it exists or not.
p.closeTag()
return node
}
/**
* Ruleset for accepting a single html comment.
* TODO: Think of another way to represent comments in the DOM tree without the selfClosing hack.
*/
func (p *Parser) comment() *DOMNode {
if !p.acceptString("<!--") {
return nil
}
var sb strings.Builder
for !p.acceptString("-->") {
val, _ := p.reader.ReadByte()
sb.WriteByte(val)
}
if p.verbose {
fmt.Println("Comment: ", sb.String())
}
return &DOMNode{
text: sb.String(),
selfClosing: true,
}
}
/**
* Ruleset for accepting any text.
*/
func (p *Parser) text() *DOMNode {
var val = p.acceptBytesUntilTest(isAlphanumericOrPunctuation)
if len(val) > 0 {
node := DOMNode{
text: val,
}
if p.verbose {
fmt.Println("Consumed string: ", val)
}
return &node
}
return nil
}
/**
* Ruleset for accepting an opening tag.
* Return values: tag name, attributes, self-closing
*/
func (p *Parser) openTag() (string, map[string]string, bool) {
// if it's a close tag, bail out.
if p.assertString("</") {
return "", nil, false
}
if !p.accept('<') {
return "", nil, false
}
var tagName = p.tagName()
if p.verbose {
fmt.Println("Open tag: ", tagName)
}
p.consumeWhitespace()
// Exit early if we've reached the end of the tag.
// Return true if it's a self closing tag.
if p.accept('>') {
selfClosing := IsSelfClosing(tagName)
return tagName, nil, selfClosing
} else if p.acceptString("/>") {
return tagName, nil, true
}
// Tag isn't over yet, cover any attributes we can find.
var attributes = make(map[string]string)
var attrName, attrValue = p.attribute()
for attrName != "" {
p.consumeWhitespace()
attributes[attrName] = attrValue
// Quit the loop when we find the end of the tag.
if p.accept('>') {
selfClosing := IsSelfClosing(tagName)
return tagName, attributes, selfClosing
} else if p.acceptString("/>") {
return tagName, attributes, true
}
attrName, attrValue = p.attribute()
}
// We never found the end of the tag :(
return "", nil, false
}
/**
* Ruleset for accepting a closing tag.
*/
func (p *Parser) closeTag() string {
if !p.acceptString("</") {
return ""
}
var tagName = p.tagName()
if p.verbose {
fmt.Println("Close tag: ", tagName)
}
if p.accept('>') {
return tagName
}
return ""
}
/**
* Ruleset for accepting a tag name.
*/
func (p *Parser) tagName() string {
var tagName = p.acceptBytesUntilTest(func(val byte) bool {
return val != '>' && val != ' ' && val != '/'
})
// Now we've determined the tag name, consume any remaining whitespace.
p.consumeWhitespace()
return tagName
}
/**
* Ruleset for accepting a single html attribute.
*/
func (p *Parser) attribute() (string, string) {
var attributeName = p.acceptBytesUntilTest(isAttributeSplit)
// We have an attribute without a value
if !p.accept('=') {
if p.verbose {
log.Println("Attribute: ", "(", attributeName, ")")
}
return attributeName, ""
}
p.consumeWhitespace()
var attributeValue = ""
if p.accept('"') {
attributeValue = p.acceptBytesUntilTest(isDoubleQuote)
} else if p.accept('\'') {
attributeValue = p.acceptBytesUntilTest(isSingleQuote)
} else {
return "", ""
}
if !p.accept('"', '\'') {
return "", ""
}
if p.verbose {
log.Println("Attribute: ", "(", attributeName, "=", attributeValue, ")")
}
return attributeName, attributeValue
}