-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_for.go
More file actions
143 lines (116 loc) · 4.7 KB
/
Copy pathparse_for.go
File metadata and controls
143 lines (116 loc) · 4.7 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package twig
import (
"fmt"
)
// parseFor parses a for loop construct in Twig templates
// Examples:
// {% for item in items %}...{% endfor %}
// {% for key, value in items %}...{% endfor %}
// {% for item in items %}...{% else %}...{% endfor %}
func (p *Parser) parseFor(parser *Parser) (Node, error) {
// Get the line number of the for token
forLine := parser.tokens[parser.tokenIndex-2].Line
// Parse the loop variable name(s)
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected variable name after for at line %d", forLine)
}
// Get value variable name
valueVar := parser.tokens[parser.tokenIndex].Value
parser.tokenIndex++
var keyVar string
// Check for key, value syntax
if parser.tokenIndex < len(parser.tokens) &&
parser.tokens[parser.tokenIndex].Type == TOKEN_PUNCTUATION &&
parser.tokens[parser.tokenIndex].Value == "," {
// Move past the comma
parser.tokenIndex++
// Now valueVar is actually the key, and we need to get the value
keyVar = valueVar
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected value variable name after comma at line %d", forLine)
}
valueVar = parser.tokens[parser.tokenIndex].Value
parser.tokenIndex++
}
// Expect 'in' keyword
if parser.tokenIndex >= len(parser.tokens) ||
parser.tokens[parser.tokenIndex].Type != TOKEN_NAME ||
parser.tokens[parser.tokenIndex].Value != "in" {
return nil, fmt.Errorf("expected 'in' keyword after variable name at line %d", forLine)
}
parser.tokenIndex++
// Parse the sequence expression
sequence, err := parser.parseExpression()
if err != nil {
return nil, err
}
// Check for filter operator (|) - needed for cases where filter detection might be missed
if IsDebugEnabled() {
LogDebug("For loop sequence expression type: %T", sequence)
}
// Expect the block end token (either regular or trim variant)
if parser.tokenIndex >= len(parser.tokens) || !isBlockEndToken(parser.tokens[parser.tokenIndex].Type) {
return nil, fmt.Errorf("expected block end after for statement at line %d", forLine)
}
parser.tokenIndex++
// Parse the for loop body
loopBody, err := parser.parseOuterTemplate()
if err != nil {
return nil, err
}
var elseBody []Node
// Check for else or endfor
if parser.tokenIndex < len(parser.tokens) && parser.tokens[parser.tokenIndex].Type == TOKEN_BLOCK_START {
parser.tokenIndex++
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected block name at line %d", parser.tokens[parser.tokenIndex-1].Line)
}
// Check if this is an else block
if parser.tokens[parser.tokenIndex].Value == "else" {
parser.tokenIndex++
// Expect the block end token
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END {
return nil, fmt.Errorf("expected block end after else at line %d", parser.tokens[parser.tokenIndex-1].Line)
}
parser.tokenIndex++
// Parse the else body
elseBody, err = parser.parseOuterTemplate()
if err != nil {
return nil, err
}
// Now expect the endfor
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START {
return nil, fmt.Errorf("expected endfor block at line %d", parser.tokens[parser.tokenIndex-1].Line)
}
parser.tokenIndex++
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected endfor at line %d", parser.tokens[parser.tokenIndex-1].Line)
}
if parser.tokens[parser.tokenIndex].Value != "endfor" {
return nil, fmt.Errorf("expected endfor, got %s at line %d", parser.tokens[parser.tokenIndex].Value, parser.tokens[parser.tokenIndex].Line)
}
parser.tokenIndex++
} else if parser.tokens[parser.tokenIndex].Value == "endfor" {
parser.tokenIndex++
} else {
return nil, fmt.Errorf("expected else or endfor, got %s at line %d", parser.tokens[parser.tokenIndex].Value, parser.tokens[parser.tokenIndex].Line)
}
// Expect the final block end token
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_END {
return nil, fmt.Errorf("expected block end after endfor at line %d", parser.tokens[parser.tokenIndex-1].Line)
}
parser.tokenIndex++
} else {
return nil, fmt.Errorf("unexpected end of template, expected endfor at line %d", forLine)
}
// Create the for node
forNode := &ForNode{
keyVar: keyVar,
valueVar: valueVar,
sequence: sequence,
body: loopBody,
elseBranch: elseBody,
line: forLine,
}
return forNode, nil
}