-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
templelementparser.go
52 lines (42 loc) · 1.45 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
package parser
import (
"github.com/a-h/parse"
"github.com/a-h/templ/parser/v2/goexpression"
)
type templElementExpressionParser struct{}
func (p templElementExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.Rune('@').Parse(pi); err != nil || !ok {
return
}
var r TemplElementExpression
// Parse the Go expression.
if r.Expression, err = parseGo("templ element", pi, goexpression.TemplExpression); err != nil {
return r, false, err
}
// Once we've got a start expression, check to see if there's an open brace for children. {\n.
var hasOpenBrace bool
_, hasOpenBrace, err = openBraceWithOptionalPadding.Parse(pi)
if err != nil {
return
}
if !hasOpenBrace {
return r, true, nil
}
// Once we've had the start of an element's children, we must conclude the block.
// Node contents.
np := newTemplateNodeParser(closeBraceWithOptionalPadding, "templ element closing brace")
var nodes Nodes
if nodes, ok, err = np.Parse(pi); err != nil || !ok {
err = parse.Error("@"+r.Expression.Value+": expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("@"+r.Expression.Value+": missing end (expected '}')", pi.Position())
return
}
return r, true, nil
}
var templElementExpression templElementExpressionParser