-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
parser.go
47 lines (39 loc) · 1.15 KB
/
parser.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
package parser
import (
"github.com/a-h/parse"
)
// ) {
var expressionFuncEnd = parse.All(parse.Rune(')'), openBraceWithOptionalPadding)
// Template
var template = parse.Func(func(pi *parse.Input) (r HTMLTemplate, ok bool, err error) {
// templ FuncName(p Person, other Other) {
var te templateExpression
if te, ok, err = templateExpressionParser.Parse(pi); err != nil || !ok {
return
}
r.Expression = te.Expression
// Once we're in a template, we should expect some template whitespace, if/switch/for,
// or node string expressions etc.
var nodes Nodes
nodes, ok, err = newTemplateNodeParser(closeBraceWithOptionalPadding, "template closing brace").Parse(pi)
if err != nil {
return
}
if !ok {
err = parse.Error("templ: expected nodes in templ body, but found none", pi.Position())
return
}
r.Children = nodes.Nodes
r.Diagnostics = nodes.Diagnostics
// Eat any whitespace.
_, _, err = parse.OptionalWhitespace.Parse(pi)
if err != nil {
return
}
// Try for }
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("template: missing closing brace", pi.Position())
return
}
return r, true, nil
})