-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
forexpressionparser.go
76 lines (62 loc) · 2.15 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
// ForExpression.
func newForExpressionParser() forExpressionParser {
return forExpressionParser{}
}
type forExpressionParser struct {
}
func (p forExpressionParser) Parse(pi parse.Input) parse.Result {
var r ForExpression
// Check the prefix first.
blockPrefix := "for "
prefixResult := parse.String("{% " + blockPrefix)(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we've had "{% for ", we're expecting a loop Go expression, followed by a tagEnd.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.Or(tagEnd, newLine))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no match, there's no tagEnd or newLine, which is an error.
if !pr.Success {
return parse.Failure("forExpressionParser", newParseError("for: unterminated (missing closing ' %}')", from, NewPositionFromInput(pi)))
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat " %}".
from = NewPositionFromInput(pi)
if te := tagEnd(pi); !te.Success {
return parse.Failure("forExpressionParser", newParseError("for: unterminated (missing closing ' %}')", from, NewPositionFromInput(pi)))
}
// Eat required newline.
from = NewPositionFromInput(pi)
lb := newLine(pi)
if lb.Error != nil {
return lb
}
if !lb.Success {
return parse.Failure("forExpressionParser", newParseError("for: missing newline after closing ' %}'", from, NewPositionFromInput(pi)))
}
// Once we've had the start of a for block, we must conclude the block.
// Node contents.
from = NewPositionFromInput(pi)
pr = newTemplateNodeParser(endForParser).Parse(pi)
if pr.Error != nil {
return pr
}
if !lb.Success {
return parse.Failure("forExpressionParser", newParseError("for: contents not found", from, NewPositionFromInput(pi)))
}
r.Children = pr.Item.([]Node)
// Eat the required "endfor".
if pr = endForParser(pi); !pr.Success {
return parse.Failure("forExpressionParser", newParseError("for: missing end (expected '{% endfor %}')", from, NewPositionFromInput(pi)))
}
return parse.Success("for", r, nil)
}
var endForParser = parse.String("{% endfor %}")