-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
ifexpressionparser.go
107 lines (86 loc) · 3.12 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
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
var ifExpression ifExpressionParser
type ifExpressionParser struct {
}
var ifExpressionStartParser = parse.String("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, 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("ifExpressionParser", newParseError("if: 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("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 required newline.
if lb := newLine(pi); lb.Error != nil {
return lb
}
// Read the 'Then' nodes.
from = NewPositionFromInput(pi)
pr = newTemplateNodeParser(parse.Or(elseExpression.Parse, closeBraceWithOptionalPadding), "else expression or closing brace").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, elseExpression.Parse)(pi)
if pr.Error != nil && pr.Error != io.EOF {
return pr
}
r.Else = pr.Item.([]Node)
// Read the required closing brace.
if ie := closeBraceWithOptionalPadding(pi); !ie.Success {
return parse.Failure("ifExpressionParser", newParseError("if: missing end (expected '}')", from, NewPositionFromInput(pi)))
}
return parse.Success("if", r, nil)
}
var elseExpression 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(closeBraceWithOptionalPadding, "closing brace").Parse, // else contents
)(pi)
}
var endElseParser = parse.All(parse.WithStringConcatCombiner,
parse.Rune('}'),
optionalWhitespaceParser,
parse.String("else"),
optionalWhitespaceParser,
parse.Rune('{'),
optionalWhitespaceParser)