-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
forexpressionparser.go
70 lines (54 loc) · 2.03 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
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
var forExpression forExpressionParser
type forExpressionParser struct {
}
var forExpressionStartParser = parse.String("for ")
func (p forExpressionParser) Parse(pi parse.Input) parse.Result {
var r ForExpression
// Check the prefix first.
prefixResult := forExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we've got a prefix, read until {\n.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.All(parse.WithStringConcatCombiner, openBraceWithOptionalPadding, newLine))(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no match, there's no {\n, which is an error.
if !pr.Success {
return parse.Failure("forExpressionParser", newParseError("for: unterminated (missing closing '{\n')", from, NewPositionFromInput(pi)))
}
r.Expression = NewExpression(pr.Item.(string), from, NewPositionFromInput(pi))
// Eat " {".
from = NewPositionFromInput(pi)
if te := expressionEnd(pi); !te.Success {
return parse.Failure("forExpressionParser", newParseError("for: unterminated (missing closing '{')", from, NewPositionFromInput(pi)))
}
// Once we've had the start of a for block, we must conclude the block.
// Eat newline.
if lb := newLine(pi); lb.Error != nil {
return lb
}
// Node contents.
from = NewPositionFromInput(pi)
pr = newTemplateNodeParser(parse.Or(elseExpression.Parse, closeBraceWithOptionalPadding)).Parse(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
// If there's no match, there's a problem in the template nodes.
if !pr.Success {
return parse.Failure("forExpressionParser", newParseError("for: expected nodes, but none were found", from, NewPositionFromInput(pi)))
}
r.Children = pr.Item.([]Node)
// Read the required closing brace.
if ie := closeBraceWithOptionalPadding(pi); !ie.Success {
return parse.Failure("forExpressionParser", newParseError("for: missing end (expected '}')", from, NewPositionFromInput(pi)))
}
return parse.Success("for", r, nil)
}