-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
elementparser.go
416 lines (349 loc) · 10.4 KB
/
elementparser.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
package parser
import (
"fmt"
"html"
"io"
"strings"
"github.com/a-h/lexical/input"
"github.com/a-h/lexical/parse"
)
// Element.
// Element open tag.
type elementOpenTag struct {
Name string
Attributes []Attribute
}
func newElementOpenTagParser() elementOpenTagParser {
return elementOpenTagParser{}
}
type elementOpenTagParser struct {
}
func asElementOpenTag(parts []interface{}) (result interface{}, ok bool) {
return elementOpenTag{
Name: parts[1].(string),
Attributes: parts[2].([]Attribute),
}, true
}
func (p elementOpenTagParser) Parse(pi parse.Input) parse.Result {
return parse.All(asElementOpenTag,
parse.Rune('<'),
elementNameParser,
newAttributesParser().Parse,
parse.Optional(parse.WithStringConcatCombiner, whitespaceParser),
parse.Rune('>'),
)(pi)
}
// Element close tag.
type elementCloseTag struct {
Name string
}
func asElementCloseTag(parts []interface{}) (result interface{}, ok bool) {
return elementCloseTag{
Name: parts[1].(string),
}, true
}
var elementCloseTagParser = parse.All(asElementCloseTag,
parse.String("</"),
elementNameParser,
parse.Rune('>'),
)
// Attribute name.
var attributeNameFirst = "abcdefghijklmnopqrstuvwxyz"
var attributeNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
var attributeNameParser = parse.Then(parse.WithStringConcatCombiner,
parse.RuneIn(attributeNameFirst),
parse.Many(parse.WithStringConcatCombiner, 0, 128, parse.RuneIn(attributeNameSubsequent)),
)
// Constant attribute.
var attributeConstantValueParser = parse.StringUntil(parse.Rune('"'))
func newConstantAttributeParser() constantAttributeParser {
return constantAttributeParser{}
}
type constantAttributeParser struct {
}
func (p constantAttributeParser) asConstantAttribute(parts []interface{}) (result interface{}, ok bool) {
return ConstantAttribute{
Name: parts[1].(string),
Value: html.UnescapeString(parts[4].(string)),
}, true
}
func (p constantAttributeParser) Parse(pi parse.Input) parse.Result {
return parse.All(p.asConstantAttribute,
whitespaceParser,
attributeNameParser,
parse.Rune('='),
parse.Rune('"'),
attributeConstantValueParser,
parse.Rune('"'),
)(pi)
}
// BoolConstantAttribute.
func newBoolConstantAttributeParser() boolConstantAttributeParser {
return boolConstantAttributeParser{}
}
type boolConstantAttributeParser struct {
}
func (p boolConstantAttributeParser) Parse(pi parse.Input) parse.Result {
var r BoolConstantAttribute
start := pi.Index()
pr := whitespaceParser(pi)
if !pr.Success {
return pr
}
pr = attributeNameParser(pi)
if !pr.Success {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return pr
}
r.Name = pr.Item.(string)
// We have a name, but if we have an equals sign, it's not a constant boolean attribute.
next, err := pi.Peek()
if err != nil {
return parse.Failure("boolConstantAttributeParser", fmt.Errorf("boolConstantAttributeParser: unexpected error reading after attribute name: %w", pr.Error))
}
if next == '=' || next == '?' {
// It's one of the other attribute types.
err := rewind(pi, start)
if err != nil && err != input.ErrStartOfFile {
return parse.Failure("failed to rewind reader", err)
}
return parse.Failure("boolConstantAttributeParser", nil)
}
if !(next == ' ' || next == '\n' || next == '/') {
return parse.Failure("boolConstantAttributeParser", fmt.Errorf("boolConstantAttributeParser: expected attribute name to end with space, newline or '/>', but got %q", string(next)))
}
return parse.Success("boolConstantAttributeParser", r, nil)
}
// BoolExpressionAttribute.
func newBoolExpressionAttributeParser() boolExpressionAttributeParser {
return boolExpressionAttributeParser{}
}
var boolExpressionStart = parse.Or(parse.String("?={ "), parse.String("?={"))
type boolExpressionAttributeParser struct {
}
func (p boolExpressionAttributeParser) Parse(pi parse.Input) parse.Result {
var r BoolExpressionAttribute
start := pi.Index()
pr := whitespaceParser(pi)
if !pr.Success {
return pr
}
pr = attributeNameParser(pi)
if !pr.Success {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return pr
}
r.Name = pr.Item.(string)
// Check whether this is a boolean expression attribute.
if pr = boolExpressionStart(pi); !pr.Success {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return pr
}
// Once we have a prefix, we must have an expression that returns a template.
from := NewPositionFromInput(pi)
pr = exp.Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
if !pr.Success {
return pr
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat the final brace.
if pr, ok := chompBrace(pi); !ok {
return pr
}
return parse.Success("boolExpressionAttributeParser", r, nil)
}
// ExpressionAttribute.
func newExpressionAttributeParser() expressionAttributeParser {
return expressionAttributeParser{}
}
type expressionAttributeParser struct {
}
func (p expressionAttributeParser) Parse(pi parse.Input) parse.Result {
var r ExpressionAttribute
start := pi.Index()
pr := whitespaceParser(pi)
if !pr.Success {
return pr
}
pr = attributeNameParser(pi)
if !pr.Success {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return pr
}
r.Name = pr.Item.(string)
if pr = parse.Or(parse.String("={ "), parse.String("={"))(pi); !pr.Success {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return pr
}
// Once we've seen a expression prefix, read until the tag end.
from := NewPositionFromInput(pi)
pr = exp.Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
if !pr.Success {
return pr
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat the final brace.
if pr, ok := chompBrace(pi); !ok {
return pr
}
return parse.Success("expressionAttributeParser", r, nil)
}
func rewind(pi parse.Input, to int64) error {
for i := pi.Index(); i > to; i-- {
if _, err := pi.Retreat(); err != nil {
return err
}
}
return nil
}
// Attributes.
func newAttributesParser() attributesParser {
return attributesParser{}
}
type attributesParser struct {
}
func (p attributesParser) asAttributeArray(parts []interface{}) (result interface{}, ok bool) {
op := make([]Attribute, len(parts))
for i := 0; i < len(parts); i++ {
switch v := parts[i].(type) {
case BoolConstantAttribute:
op[i] = v
case ConstantAttribute:
op[i] = v
case BoolExpressionAttribute:
op[i] = v
case ExpressionAttribute:
op[i] = v
}
}
return op, true
}
var attributeParser = parse.Any(
newBoolConstantAttributeParser().Parse,
newConstantAttributeParser().Parse,
newBoolExpressionAttributeParser().Parse,
newExpressionAttributeParser().Parse,
)
func (p attributesParser) Parse(pi parse.Input) parse.Result {
return parse.Many(p.asAttributeArray, 0, 255, attributeParser)(pi)
}
// Element name.
var elementNameFirst = "abcdefghijklmnopqrstuvwxyz"
var elementNameSubsequent = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"
var elementNameParser = parse.Then(parse.WithStringConcatCombiner,
parse.RuneIn(elementNameFirst),
parse.Many(parse.WithStringConcatCombiner, 0, 15, parse.RuneIn(elementNameSubsequent)),
)
// Element.
func newElementOpenCloseParser() elementOpenCloseParser {
return elementOpenCloseParser{}
}
type elementOpenCloseParser struct {
SourceRangeToItemLookup SourceMap
}
func (p elementOpenCloseParser) Parse(pi parse.Input) parse.Result {
var r Element
// Check the open tag.
otr := newElementOpenTagParser().Parse(pi)
if otr.Error != nil || !otr.Success {
return otr
}
ot := otr.Item.(elementOpenTag)
r.Name = ot.Name
r.Attributes = ot.Attributes
// Once we've got an open tag, the rest must be present.
from := NewPositionFromInput(pi)
tnpr := newTemplateNodeParser(nil, "").Parse(pi)
if !tnpr.Success {
if _, isParseError := tnpr.Error.(ParseError); isParseError {
return tnpr
}
return parse.Failure("elementOpenCloseParser", newParseError(fmt.Sprintf("<%s>: %v", r.Name, tnpr.Error), from, NewPositionFromInput(pi)))
}
if arr, isArray := tnpr.Item.([]Node); isArray {
r.Children = append(r.Children, arr...)
}
// Close tag.
ectpr := elementCloseTagParser(pi)
if !ectpr.Success {
return parse.Failure("elementOpenCloseParser", newParseError(fmt.Sprintf("<%s>: expected end tag not present or invalid tag contents", r.Name), from, NewPositionFromInput(pi)))
}
if ct := ectpr.Item.(elementCloseTag); ct.Name != r.Name {
return parse.Failure("elementOpenCloseParser", newParseError(fmt.Sprintf("<%s>: mismatched end tag, expected '</%s>', got '</%s>'", r.Name, r.Name, ct.Name), from, NewPositionFromInput(pi)))
}
return parse.Success("elementOpenCloseParser", r, nil)
}
// Element self-closing tag.
var selfClosingElement = elementSelfClosingParser{}
type elementSelfClosingParser struct {
SourceRangeToItemLookup SourceMap
}
func (p elementSelfClosingParser) asElement(parts []interface{}) (result interface{}, ok bool) {
return Element{
Name: parts[1].(string),
Attributes: parts[2].([]Attribute),
}, true
}
func (p elementSelfClosingParser) Parse(pi parse.Input) parse.Result {
return parse.All(p.asElement,
parse.Rune('<'),
elementNameParser,
newAttributesParser().Parse,
optionalWhitespaceParser,
parse.String("/>"),
)(pi)
}
// Element
var element elementParser
type elementParser struct {
}
func (p elementParser) Parse(pi parse.Input) parse.Result {
var r Element
// Self closing.
from := NewPositionFromInput(pi)
scr := selfClosingElement.Parse(pi)
if scr.Error != nil && scr.Error != io.EOF {
return scr
}
if scr.Success {
r = scr.Item.(Element)
if msgs, ok := r.Validate(); !ok {
return parse.Failure("elementParser", newParseError(fmt.Sprintf("<%s>: %s", r.Name, strings.Join(msgs, ", ")), from, NewPositionFromInput(pi)))
}
return parse.Success("elementParser", r, nil)
}
// Open/close pair.
ocr := newElementOpenCloseParser().Parse(pi)
if ocr.Error != nil && ocr.Error != io.EOF {
return ocr
}
if ocr.Success {
r = ocr.Item.(Element)
if msgs, ok := r.Validate(); !ok {
return parse.Failure("elementParser", newParseError(fmt.Sprintf("<%s>: %s", r.Name, strings.Join(msgs, ", ")), from, NewPositionFromInput(pi)))
}
return parse.Success("elementParser", r, nil)
}
return parse.Failure("elementParser", nil)
}