-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_macro.go
More file actions
244 lines (207 loc) · 8.22 KB
/
parse_macro.go
File metadata and controls
244 lines (207 loc) · 8.22 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package twig
import (
"fmt"
"strconv"
"strings"
)
func (p *Parser) parseMacro(parser *Parser) (Node, error) {
// Use debug logging if enabled
if IsDebugEnabled() && debugger.level >= DebugVerbose {
tokenIndex := parser.tokenIndex - 2
LogVerbose("Parsing macro, tokens available:")
for i := 0; i < 10 && tokenIndex+i < len(parser.tokens); i++ {
token := parser.tokens[tokenIndex+i]
LogVerbose(" Token %d: Type=%d, Value=%q, Line=%d", i, token.Type, token.Value, token.Line)
}
}
// Get the line number of the macro token
macroLine := parser.tokens[parser.tokenIndex-2].Line
// Get the macro name
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected macro name after macro keyword at line %d", macroLine)
}
// Special handling for incorrectly tokenized macro declarations
macroNameRaw := parser.tokens[parser.tokenIndex].Value
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Raw macro name: %s", macroNameRaw)
}
// Check if the name contains parentheses (incorrectly tokenized)
if strings.Contains(macroNameRaw, "(") {
// Extract the actual name before the parenthesis
parts := strings.SplitN(macroNameRaw, "(", 2)
if len(parts) == 2 {
macroName := parts[0]
paramStr := "(" + parts[1]
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Fixed macro name: %s", macroName)
LogVerbose("Parameter string: %s", paramStr)
}
// Parse parameters
var params []string
defaults := make(map[string]Node)
// Simple parameter parsing - split by comma
paramList := strings.TrimRight(paramStr[1:], ")")
if paramList != "" {
paramItems := strings.Split(paramList, ",")
for _, param := range paramItems {
param = strings.TrimSpace(param)
// Check for default value
if strings.Contains(param, "=") {
parts := strings.SplitN(param, "=", 2)
paramName := strings.TrimSpace(parts[0])
defaultValue := strings.TrimSpace(parts[1])
params = append(params, paramName)
// Handle quoted strings in default values
if (strings.HasPrefix(defaultValue, "'") && strings.HasSuffix(defaultValue, "'")) ||
(strings.HasPrefix(defaultValue, "\"") && strings.HasSuffix(defaultValue, "\"")) {
// Remove quotes
strValue := defaultValue[1 : len(defaultValue)-1]
defaults[paramName] = NewLiteralNode(strValue, macroLine)
} else if defaultValue == "true" {
defaults[paramName] = NewLiteralNode(true, macroLine)
} else if defaultValue == "false" {
defaults[paramName] = NewLiteralNode(false, macroLine)
} else if i, err := strconv.Atoi(defaultValue); err == nil {
defaults[paramName] = NewLiteralNode(i, macroLine)
} else {
// Fallback to string
defaults[paramName] = NewLiteralNode(defaultValue, macroLine)
}
} else {
params = append(params, param)
}
}
}
// Skip to the end of the token
parser.tokenIndex++
// Expect block end
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 macro declaration at line %d", macroLine)
}
parser.tokenIndex++
// Parse the macro body
bodyNodes, err := parser.parseOuterTemplate()
if err != nil {
return nil, err
}
// Expect endmacro tag
if parser.tokenIndex+1 >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START_TRIM) ||
parser.tokens[parser.tokenIndex+1].Type != TOKEN_NAME ||
parser.tokens[parser.tokenIndex+1].Value != "endmacro" {
return nil, fmt.Errorf("missing endmacro tag for macro '%s' at line %d",
macroName, macroLine)
}
// Skip {% endmacro %}
parser.tokenIndex += 2 // Skip {% endmacro
// Expect block end
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 endmacro at line %d", parser.tokens[parser.tokenIndex].Line)
}
parser.tokenIndex++
// Create the macro node
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Creating MacroNode with %d parameters and %d defaults", len(params), len(defaults))
}
return NewMacroNode(macroName, params, defaults, bodyNodes, macroLine), nil
}
}
// Regular parsing path
macroName := parser.tokens[parser.tokenIndex].Value
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Macro name: %s", macroName)
}
parser.tokenIndex++
// Expect opening parenthesis for parameters
if parser.tokenIndex >= len(parser.tokens) ||
parser.tokens[parser.tokenIndex].Type != TOKEN_PUNCTUATION ||
parser.tokens[parser.tokenIndex].Value != "(" {
return nil, fmt.Errorf("expected '(' after macro name at line %d", macroLine)
}
parser.tokenIndex++
// Parse parameters
var params []string
defaults := make(map[string]Node)
// If we don't have a closing parenthesis immediately, we have parameters
if parser.tokenIndex < len(parser.tokens) &&
(parser.tokens[parser.tokenIndex].Type != TOKEN_PUNCTUATION ||
parser.tokens[parser.tokenIndex].Value != ")") {
for {
// Get parameter name
if parser.tokenIndex >= len(parser.tokens) || parser.tokens[parser.tokenIndex].Type != TOKEN_NAME {
return nil, fmt.Errorf("expected parameter name at line %d", macroLine)
}
paramName := parser.tokens[parser.tokenIndex].Value
params = append(params, paramName)
parser.tokenIndex++
// Check for default value
if parser.tokenIndex < len(parser.tokens) &&
parser.tokens[parser.tokenIndex].Type == TOKEN_OPERATOR &&
parser.tokens[parser.tokenIndex].Value == "=" {
parser.tokenIndex++ // Skip =
// Parse default value expression
defaultExpr, err := parser.parseExpression()
if err != nil {
fmt.Println("DEBUG: Error parsing default value:", err)
return nil, err
}
defaults[paramName] = defaultExpr
}
// Check if we have more parameters
if parser.tokenIndex < len(parser.tokens) &&
parser.tokens[parser.tokenIndex].Type == TOKEN_PUNCTUATION &&
parser.tokens[parser.tokenIndex].Value == "," {
parser.tokenIndex++ // Skip comma
continue
}
break
}
}
// Expect closing parenthesis
if parser.tokenIndex >= len(parser.tokens) ||
parser.tokens[parser.tokenIndex].Type != TOKEN_PUNCTUATION ||
parser.tokens[parser.tokenIndex].Value != ")" {
return nil, fmt.Errorf("expected ')' after macro parameters at line %d", macroLine)
}
parser.tokenIndex++
// Expect block end
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 macro declaration at line %d", macroLine)
}
parser.tokenIndex++
// Parse the macro body
bodyNodes, err := parser.parseOuterTemplate()
if err != nil {
return nil, err
}
// Expect endmacro tag
if parser.tokenIndex+1 >= len(parser.tokens) ||
(parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START &&
parser.tokens[parser.tokenIndex].Type != TOKEN_BLOCK_START_TRIM) ||
parser.tokens[parser.tokenIndex+1].Type != TOKEN_NAME ||
parser.tokens[parser.tokenIndex+1].Value != "endmacro" {
return nil, fmt.Errorf("missing endmacro tag for macro '%s' at line %d",
macroName, macroLine)
}
// Skip {% endmacro %}
parser.tokenIndex += 2 // Skip {% endmacro
// Expect block end
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 endmacro at line %d", parser.tokens[parser.tokenIndex].Line)
}
parser.tokenIndex++
// Create the macro node
if IsDebugEnabled() && debugger.level >= DebugVerbose {
LogVerbose("Creating MacroNode with %d parameters and %d defaults", len(params), len(defaults))
}
return NewMacroNode(macroName, params, defaults, bodyNodes, macroLine), nil
}