-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_apply.go
55 lines (45 loc) · 1.82 KB
/
parse_apply.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
package twig
import (
"fmt"
)
func (p *Parser) parseApply(parser *Parser) (Node, error) {
// Get the line number of the apply token
applyLine := parser.tokens[parser.tokenIndex-2].Line
// Parse the filter name
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected filter name after apply tag at line %d", applyLine)
}
filterName := parser.tokens[parser.tokenIndex].Value
parser.tokenIndex++
// Expect the block end token
if parser.tokenIndex >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END_TRIM) {
return nil, fmt.Errorf("expected block end token after apply filter at line %d", applyLine)
}
parser.tokenIndex++
// Parse the apply body
applyBody, err := parser.parseOuterTemplate()
if err != nil {
return nil, err
}
// Expect endapply tag
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START {
return nil, fmt.Errorf("expected endapply tag at line %d", applyLine)
}
parser.tokenIndex++
// Expect 'endapply' token
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME || parser.tokens[parser.tokenIndex].Value != "endapply" {
return nil, fmt.Errorf("expected 'endapply' at line %d", applyLine)
}
parser.tokenIndex++
// Expect block end token
if parser.tokenIndex >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END_TRIM) {
return nil, fmt.Errorf("expected block end token after endapply at line %d", applyLine)
}
parser.tokenIndex++
// Create apply node (no arguments for now)
return NewApplyNode(applyBody, filterName, nil, applyLine), nil
}