-
-
Notifications
You must be signed in to change notification settings - Fork 238
/
templelementparser.go
66 lines (51 loc) · 2.11 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
package parser
import (
"fmt"
"strings"
"github.com/a-h/parse"
)
var templBlockElementExpression = parse.Func(func(pi *parse.Input) (r TemplElementExpression, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.Rune('@').Parse(pi); err != nil || !ok {
return
}
// Once we've got a prefix, read until {\n.
endOfStatementExpression := ExpressionOf(parse.StringUntil(parse.All(openBraceWithOptionalPadding, parse.NewLine)))
if r.Expression, ok, err = endOfStatementExpression.Parse(pi); err != nil || !ok {
return
}
// Eat " {\n".
if _, ok, err = Must(parse.All(openBraceWithOptionalPadding, parse.NewLine), "templ element: unterminated (missing closing '{\n')").Parse(pi); err != nil || !ok {
return
}
// Once we've had the start of a for block, we must conclude the block.
// Node contents.
np := newTemplateNodeParser(closeBraceWithOptionalPadding, "templ element closing brace")
if r.Children, ok, err = Must[[]Node](np, fmt.Sprintf("@%s: expected nodes, but none were found", r.Expression.Value)).Parse(pi); err != nil || !ok {
return
}
// Read the required closing brace.
if _, ok, err = Must(closeBraceWithOptionalPadding, fmt.Sprintf("@%s: missing end (expected '}')", r.Expression.Value)).Parse(pi); err != nil || !ok {
return
}
return r, true, nil
})
var templSelfClosingElementExpression = parse.Func(func(pi *parse.Input) (e TemplElementExpression, ok bool, err error) {
start := pi.Index()
// Check the prefix first.
if _, ok, err = parse.Rune('@').Parse(pi); err != nil || !ok {
return
}
// Once we've got a prefix, read until \n.
endOfStatementExpression := ExpressionOf(parse.StringUntil(parse.NewLine))
if e.Expression, ok, err = Must(endOfStatementExpression, "templ element: unterminated (missing closing newline)").Parse(pi); err != nil || !ok {
return
}
// It isn't a self-closing expression if there's an opening brace.
if strings.HasSuffix(strings.TrimSpace(e.Expression.Value), "{") {
pi.Seek(start)
return e, false, nil
}
return e, true, nil
})
var templElementExpression = parse.Any(templSelfClosingElementExpression, templBlockElementExpression)