-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
ifexpressionparser.go
108 lines (87 loc) · 3.07 KB
/
ifexpressionparser.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
// IfExpression.
func newIfExpressionParser() ifExpressionParser {
return ifExpressionParser{}
}
type ifExpressionParser struct {
}
var ifExpressionStartParser = createStartParser("if")
func (p ifExpressionParser) asChildren(parts []interface{}) (result interface{}, ok bool) {
if len(parts) == 0 {
return []Node{}, true
}
return parts[0].([]Node), true
}
func (p ifExpressionParser) Parse(pi parse.Input) parse.Result {
var r IfExpression
// Check the prefix first.
prefixResult := ifExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we've got a prefix, we must have the if expression, followed by a tagEnd.
from := NewPositionFromInput(pi)
pr := parse.StringUntil(parse.Or(expressionEnd, 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("ifExpressionParser", newParseError("if: unterminated (missing closing ' %}')", 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("ifExpressionParser", newParseError("if: unterminated (missing closing ' %}')", from, NewPositionFromInput(pi)))
}
// Once we've had the start of an if block, we must conclude the block.
// Eat optional newline.
if lb := newLine(pi); lb.Error != nil {
return lb
}
// Read the 'Then' nodes.
from = NewPositionFromInput(pi)
eep := newElseExpressionParser()
pr = newTemplateNodeParser(parse.Or(eep.Parse, endIfParser)).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("ifExpressionParser", newParseError("if: expected nodes, but none were found", from, NewPositionFromInput(pi)))
}
r.Then = pr.Item.([]Node)
// Read the optional 'Else' Nodes.
from = NewPositionFromInput(pi)
pr = parse.Optional(p.asChildren, eep.Parse)(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
r.Else = pr.Item.([]Node)
// Read the required "endif" statement.
if ie := endIfParser(pi); !ie.Success {
return parse.Failure("ifExpressionParser", newParseError("if: missing end (expected '{% endif %}')", from, NewPositionFromInput(pi)))
}
return parse.Success("if", r, nil)
}
func newElseExpressionParser() elseExpressionParser {
return elseExpressionParser{}
}
type elseExpressionParser struct {
}
func (p elseExpressionParser) asElseExpression(parts []interface{}) (result interface{}, ok bool) {
return parts[1].([]Node), true // the array of nodes from templateNodeParser
}
func (p elseExpressionParser) Parse(pi parse.Input) parse.Result {
return parse.All(p.asElseExpression,
endElseParser,
newTemplateNodeParser(endIfParser).Parse, // else contents
)(pi)
}
var endElseParser = createEndParser("else") // {% else %}
var endIfParser = createEndParser("endif") // {% endif %}