-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
calltemplateparser.go
46 lines (36 loc) · 1.47 KB
/
calltemplateparser.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
package parser
import (
"io"
"github.com/a-h/lexical/parse"
)
// newCallTemplateExpressionParser creates a new callTemplateExpressionParser.
func newCallTemplateExpressionParser() callTemplateExpressionParser {
return callTemplateExpressionParser{}
}
var callTemplateExpressionStartParser = parse.Or(parse.String("{%! "), parse.String("{%!"))
type callTemplateExpressionParser struct{}
func (p callTemplateExpressionParser) Parse(pi parse.Input) parse.Result {
var r CallTemplateExpression
// Check the prefix first.
prefixResult := callTemplateExpressionStartParser(pi)
if !prefixResult.Success {
return prefixResult
}
// Once we have a prefix, we must have an expression that returns a template, 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("callTemplateExpressionParser", newParseError("call: 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("callTemplateExpressionParser", newParseError("call: unterminated (missing closing ' %}')", from, NewPositionFromInput(pi)))
}
return parse.Success("callTemplate", r, nil)
}