-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
forexpressionparser.go
52 lines (43 loc) · 1.46 KB
/
forexpressionparser.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"
)
var forExpression parse.Parser[Node] = forExpressionParser{}
type forExpressionParser struct{}
func (_ forExpressionParser) Parse(pi *parse.Input) (n Node, ok bool, err error) {
// Check the prefix first.
if _, ok, err = parse.String("for ").Parse(pi); err != nil || !ok {
return
}
// Once we've got a prefix, read until {\n.
// If there's no match, there's no {\n, which is an error.
from := pi.Position()
var r ForExpression
until := parse.All(openBraceWithOptionalPadding, parse.NewLine)
var fexp string
if fexp, ok, err = parse.StringUntil(until).Parse(pi); err != nil || !ok {
err = parse.Error("for: "+unterminatedMissingCurly, pi.Position())
return
}
r.Expression = NewExpression(fexp, from, pi.Position())
// Eat " {".
if _, ok, err = until.Parse(pi); err != nil || !ok {
err = parse.Error("for: "+unterminatedMissingCurly, pi.Position())
return
}
// Node contents.
tnp := newTemplateNodeParser(closeBraceWithOptionalPadding, "for expression closing brace")
var nodes Nodes
if nodes, ok, err = tnp.Parse(pi); err != nil || !ok {
err = parse.Error("for: expected nodes, but none were found", pi.Position())
return
}
r.Children = nodes.Nodes
r.Diagnostics = nodes.Diagnostics
// Read the required closing brace.
if _, ok, err = closeBraceWithOptionalPadding.Parse(pi); err != nil || !ok {
err = parse.Error("for: "+unterminatedMissingEnd, pi.Position())
return
}
return r, true, nil
}