-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
templelementparser.go
134 lines (111 loc) · 4.14 KB
/
templelementparser.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
package parser
import (
"fmt"
"io"
"strings"
"github.com/a-h/lexical/parse"
)
var templBlockElementExpression templBlockElementExpressionParser
type templBlockElementExpressionParser struct{}
func (p templBlockElementExpressionParser) Parse(pi parse.Input) parse.Result {
var r TemplElementExpression
// Check the prefix first.
prefixResult := parse.Rune('@')(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we have a prefix, we must have an expression that returns a template.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.All(parse.WithStringConcatCombiner, openBraceWithOptionalPadding))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
if !pr.Success {
return parse.Failure("templElementParser", newParseError("templ element: unterminated (missing closing '{\n')", from, NewPositionFromInput(pi)))
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
from = NewPositionFromInput(pi)
if te := expressionEnd(pi); !te.Success {
return parse.Failure("templElementParser", newParseError("templ element: unterminated (missing closing '{')", from, NewPositionFromInput(pi)))
}
// Once we've had the start of a for block, we must conclude the block.
// Eat newline.
if lb := newLine(pi); lb.Error != nil {
return lb
}
// Node contents.
from = NewPositionFromInput(pi)
pr = newTemplateNodeParser(closeBraceWithOptionalPadding, "closing brace").Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no match, there's a problem in the template nodes.
if !pr.Success {
return parse.Failure("templElementParser", newParseError(fmt.Sprintf("@%s: expected nodes, but none were found", r.Expression.Value), from, NewPositionFromInput(pi)))
}
r.Children = pr.Item.([]Node)
// Read the required closing brace.
if ie := closeBraceWithOptionalPadding(pi); !ie.Success {
return parse.Failure("templElementParser", newParseError(fmt.Sprintf("@%s: missing end (expected '}')", r.Expression.Value), from, NewPositionFromInput(pi)))
}
return parse.Success("templElementParser", r, nil)
}
var templSelfClosingElementExpression templSelfClosingElementExpressionParser
type templSelfClosingElementExpressionParser struct{}
func (p templSelfClosingElementExpressionParser) asTemplElement(parts []interface{}) (result interface{}, ok bool) {
return TemplElementExpression{
Expression: parts[1].(Expression),
}, true
}
func (p templSelfClosingElementExpressionParser) parseExpression(pi parse.Input) parse.Result {
start := pi.Index()
var e Expression
from := NewPositionFromInput(pi)
pr := parse.StringUntil(newLine)(pi)
if pr.Error != nil && pr.Error != io.EOF {
return parse.Failure("templElementParser", nil)
}
if strings.HasSuffix(strings.TrimSpace(pr.Item.(string)), "{") {
err := rewind(pi, start)
if err != nil {
return parse.Failure("failed to rewind reader", err)
}
return parse.Failure("templElementParser", nil)
}
// If there's no match, there's no \n, which is an error.
if !pr.Success {
return parse.Failure("templElementParser", newParseError("templ element: unterminated (missing closing newline)", from, NewPositionFromInput(pi)))
}
e = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
return parse.Success("templElementParser", e, nil)
}
func (p templSelfClosingElementExpressionParser) Parse(pi parse.Input) parse.Result {
return parse.All(p.asTemplElement,
parse.Rune('@'),
p.parseExpression,
)(pi)
}
var templElementExpression templElementExpressionParser
type templElementExpressionParser struct{}
func (p templElementExpressionParser) Parse(pi parse.Input) parse.Result {
var r TemplElementExpression
// Self closing.
pr := templSelfClosingElementExpression.Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
if pr.Success {
r = pr.Item.(TemplElementExpression)
return parse.Success("templElementParser", r, nil)
}
// Block.
pr = templBlockElementExpression.Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
if pr.Success {
r = pr.Item.(TemplElementExpression)
return parse.Success("templElementParser", r, nil)
}
return parse.Failure("templElementParser", nil)
}