-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
templateparser.go
178 lines (154 loc) · 4.44 KB
/
templateparser.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
package parser
import (
"fmt"
"io"
"github.com/a-h/lexical/parse"
)
// TemplateExpression.
// TemplateExpression.
// templ Func(p Parameter) {
// templ (data Data) Func(p Parameter) {
// templ (data []string) Func(p Parameter) {
type templateExpression struct {
Expression Expression
}
func newTemplateExpressionParser() templateExpressionParser {
return templateExpressionParser{}
}
type templateExpressionParser struct {
}
var templateExpressionStartParser = parse.String("templ ")
func (p templateExpressionParser) Parse(pi parse.Input) parse.Result {
var r templateExpression
// Check the prefix first.
prefixResult := templateExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we have the prefix, everything to the brace at the end of the line is Go.
// e.g.
// templ (x []string) Test() {
// becomes:
// func (x []string) Test() templ.Component {
// Once we've got a prefix, read until {\n.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.All(parse.WithStringConcatCombiner, openBraceWithOptionalPadding, newLine))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no match, there's no {\n, which is an error.
if !pr.Success {
return parse.Failure("templateExpressionParser", newParseError("templ: unterminated (missing closing '{\n')", from, NewPositionFromInput(pi)))
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat " {".
from = NewPositionFromInput(pi)
if te := expressionEnd(pi); !te.Success {
return parse.Failure("templateExpressionParser", newParseError("templ: unterminated (missing closing '{')", from, NewPositionFromInput(pi)))
}
// Eat required newline.
if lb := newLine(pi); lb.Error != nil {
return lb
}
return parse.Success("templateExpressionParser", r, nil)
}
// Template node (element, call, if, switch, for, whitespace etc.)
func newTemplateNodeParser(until parse.Function, untilName string) templateNodeParser {
return templateNodeParser{
until: until,
untilName: untilName,
}
}
type templateNodeParser struct {
until parse.Function
untilName string
}
var rawElements = parse.Any(styleElement.Parse, scriptElement.Parse)
func (p templateNodeParser) Parse(pi parse.Input) parse.Result {
op := make([]Node, 0)
for {
var pr parse.Result
// Check if we've reached the end.
if p.until != nil {
start := pi.Index()
pr = p.until(pi)
if pr.Error != nil {
return pr
}
if pr.Success {
if err := rewind(pi, start); err != nil {
return parse.Failure("templateNodeParser", err)
}
return parse.Success("templateNodeParser", op, nil)
}
}
// Try for valid nodes.
pr = parse.Any(
// Try for a doctype.
// <!DOCTYPE html>
newDocTypeParser().Parse,
// Try for a raw <text>, <>, or <style> element (special behaviour - contents are not parsed).
rawElements,
// Try for an element.
// <a>, <br/> etc.
element.Parse,
// Try for an if expression.
// if {}
ifExpression.Parse,
// Try for a for expression.
// for {}
forExpression.Parse,
// Try for a switch expression.
// switch {}
switchExpression.Parse,
// Try for a call template expression.
// {! TemplateName(a, b, c) }
callTemplateExpression.Parse,
// Try for a templ element expression.
// <!TemplateName(a, b, c) />
templElementExpression.Parse,
// Try for a children element expression.
// { children... }
childrenExpression,
// Try for a string expression.
// { "abc" }
// { strings.ToUpper("abc") }
stringExpression.Parse,
)(pi)
if pr.Error != nil {
return pr
}
if pr.Success {
op = append(op, pr.Item.(Node))
continue
}
// Eat any whitespace.
pr = optionalWhitespaceParser(pi)
if pr.Error != nil {
return pr
}
if pr.Success && len(pr.Item.(Whitespace).Value) > 0 {
op = append(op, pr.Item.(Whitespace))
continue
}
// Try for text.
// anything & everything accepted...
pr = newTextParser().Parse(pi)
if pr.Error != nil {
return pr
}
if pr.Success && len(pr.Item.(Text).Value) > 0 {
op = append(op, pr.Item.(Text))
continue
}
if p.until == nil {
// In this case, we're just reading as many nodes as we can.
// The element parser checks the final node returned to make sure it's the expected close tag.
break
}
if p.until != nil {
return parse.Failure("templateNodeParser", fmt.Errorf("%v not found", p.untilName))
}
}
return parse.Success("templateNodeParser", op, nil)
}