forked from kaptinlin/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
569 lines (506 loc) · 16.6 KB
/
parser.go
File metadata and controls
569 lines (506 loc) · 16.6 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package template
import (
"regexp"
"strconv"
"strings"
)
// Regular expression to identify variables.
// var variableRegex = regexp.MustCompile(`{{\s*([\w\.]+)((?:\s*\|\s*[\w\:\,]+(?:\s*:\s*[^}]+)?)*)\s*}}`)
var variableRegex = regexp.MustCompile(`{{\s*(?:'[^']*'|"[\s\S]*?"|[\w\.]+)((?:\s*\|\s*[\w\:\,]+(?:\s*:\s*[^}]+)?)*)\s*}}`)
var forRegex = regexp.MustCompile(`{%\s*for\s+([\w\.]+)\s+in\s+([\w\.\[\]]+)\s*%}`)
var ifRegex = regexp.MustCompile(`{%\s*if\s+` + // if start
`([^%]+)` + // Capture any non-% characters (expression part)
`\s*%}`) // if end
var endforRegex = regexp.MustCompile(`{%\s*endfor\s*%}`)
var endifRegex = regexp.MustCompile(`{%\s*endif\s*%}`)
var elseRegex = regexp.MustCompile(`{%\s*else\s*%}`)
// Parser analyzes template syntax.
type Parser struct{}
// NewParser creates a Parser with a compiled regular expression for efficiency.
func NewParser() *Parser {
return &Parser{}
}
// Updated addVariableNode processes a variable token, parses out any filters, and adds it to the template.
func (p *Parser) addVariableNode(token string, tpl *Template, node *Node) {
if tpl != nil {
// Extract the inner content of the variable token.
innerContent := strings.TrimSpace(token[2 : len(token)-2])
// Split the variable name from any filters.
parts := strings.SplitN(innerContent, "|", 2)
varName := strings.TrimSpace(parts[0])
// Initialize filters slice.
var filters []Filter
// Check if there are filters to parse and use parseFilters if so.
if len(parts) > 1 {
filters = parseFilters(parts[1])
}
// Create a new variable node with the parsed variable name and filters.
node = &Node{
Type: "variable",
Variable: varName,
Filters: filters,
Text: token,
}
// Add the new node to the template.
tpl.Nodes = append(tpl.Nodes, node)
} else {
// Extract the inner content of the variable token.
innerContent := strings.TrimSpace(token[2 : len(token)-2])
// Split the variable name from any filters.
parts := strings.SplitN(innerContent, "|", 2)
varName := strings.TrimSpace(parts[0])
// Initialize filters slice.
var filters []Filter
// Check if there are filters to parse and use parseFilters if so.
if len(parts) > 1 {
filters = parseFilters(parts[1])
}
node.Children = append(node.Children, &Node{
Type: "variable",
Variable: varName,
Filters: filters,
Text: token,
})
}
}
func parseFilters(filterStr string) []Filter {
filters := make([]Filter, 0)
if filterStr == "" {
return filters
}
// Splitting the entire filter string into individual filters
filterParts := strings.Split(filterStr, "|")
for _, part := range filterParts {
partTrimmed := strings.TrimSpace(part)
if partTrimmed == "" {
continue
}
// Splitting the filter name from its arguments
nameArgs := strings.SplitN(partTrimmed, ":", 2)
filter := Filter{Name: strings.TrimSpace(nameArgs[0])}
// Handling arguments, if present
if len(nameArgs) == 2 {
filter.Args = splitArgsConsideringQuotes(nameArgs[1])
}
filters = append(filters, filter)
}
return filters
}
func splitArgsConsideringQuotes(argsStr string) []FilterArg {
var args []FilterArg
var currentArg strings.Builder
var inQuotes bool
var quoteChar rune
appendArg := func() {
arg := currentArg.String()
currentArg.Reset()
if arg == `""` || arg == "''" {
args = append(args, StringArg{val: ""})
} else if trimmedArg := strings.TrimSpace(arg); len(trimmedArg) > 0 {
if len(trimmedArg) >= 2 && (trimmedArg[0] == '"' || trimmedArg[0] == '\'') {
// String argument
args = append(args, StringArg{val: trimmedArg[1 : len(trimmedArg)-1]})
} else if number, err := strconv.ParseFloat(trimmedArg, 64); err == nil {
args = append(args, NumberArg{val: number})
} else {
// Treat as variable
args = append(args, VariableArg{name: trimmedArg})
}
}
}
for i, char := range argsStr {
switch {
case char == '"' || char == '\'':
if inQuotes && char == quoteChar {
currentArg.WriteRune(char) // Include the quote for simplicity
inQuotes = false
if i == len(argsStr)-1 || argsStr[i+1] == ',' {
appendArg()
}
} else if !inQuotes {
inQuotes = true
quoteChar = char
currentArg.WriteRune(char) // Include the quote for parsing
}
case char == ',' && !inQuotes:
appendArg()
default:
currentArg.WriteRune(char)
}
}
if currentArg.Len() > 0 || inQuotes {
appendArg()
}
return args
}
// addTextNode adds a text token to the template
func (p *Parser) addTextNode(text string, tpl *Template, node *Node) {
if text != "" && tpl != nil {
tpl.Nodes = append(tpl.Nodes, &Node{Type: "text", Text: text})
} else {
node.Children = append(node.Children, &Node{Type: "text", Text: text})
}
}
// addForNode adds a for node to the template
func (p *Parser) addForNode(text string, tpl *Template, node *Node) {
matched := endforRegex.MatchString(text)
if text != "" && tpl != nil {
tpl.Nodes = append(tpl.Nodes, &Node{Type: "text", Text: text})
} else if !matched {
node.Children = append(node.Children, &Node{Type: "text", Text: text})
}
if matched {
node.Type = "for"
node.EndText = text
analyzeForParameter(node.Text, node)
}
}
// analyzeForParameter analyzes the for parameter
func analyzeForParameter(text string, node *Node) {
n := len(text)
next := 0
for next < n {
if text[next:next+4] == " in " {
left := next
right := next + 4
for left > 0 {
if text[0:left] == "{% for " {
node.Variable = text[left:next]
}
left--
}
for right < n {
if text[right:n] == " %}" {
node.Collection = text[next+4 : right]
}
right++
}
break
}
next++
}
}
// addIfNode adds an if node to the template
func (p *Parser) addIfNode(text string, tpl *Template, node *Node) {
matched := endifRegex.MatchString(text)
if text != "" && tpl != nil {
tpl.Nodes = append(tpl.Nodes, &Node{Type: "text", Text: text})
} else if !matched {
node.Children = append(node.Children, &Node{Type: "text", Text: text})
}
if matched {
node.Type = "if"
node.EndText = text
}
}
// Parse converts a template string into a Template object.
// It recognizes the following syntax:
// - Text content
// - Variable expressions {{ variable }}
// - Control structures {% if/for ... %}{% endif/endfor %}
// Returns the parsed template and any error encountered.
func (p *Parser) Parse(src string) (*Template, error) {
n := len(src)
template := NewTemplate()
prev := 0
next := 0
for next < n {
switch {
case src[next] == '{' && next+1 < n && src[next+1] == '{':
// Handle variable
next, prev = p.handleVariable(src, next, prev, template, nil)
case next+1 < n && src[next] == '{' && src[next+1] == '%':
// Handle for and if
next, prev = p.parseControlBlock(src, next, prev, template)
default:
next++
}
}
// Handle any remaining text
p.parseRemainingText(src, prev, next, template)
return template, nil
}
// parseControlBlock processes control structures (if/for blocks) in the template.
// It handles the parsing of opening tags, their content, and closing tags,
// maintaining the proper nesting structure in the template tree.
// Returns updated next and prev positions in the source string.
func (p *Parser) parseControlBlock(src string, next, prev int, template *Template) (int, int) {
n := len(src)
// Try to match control structure opening tags and determine their type
matched, temp, typ := p.matchAppropriateStrings(src, n, next+2, "%}")
if matched && typ < 3 { //nolint:nestif
// Extract the complete control structure token
token := src[next : temp+1]
// Handle any text content before the control structure
if next > prev {
token := src[prev:next]
p.addTextNode(token, template, nil)
}
// Add appropriate node based on control structure type (for/if)
if typ == 1 {
p.addForNode(token, template, nil)
} else if typ == 2 {
p.addIfNode(token, template, nil)
}
// Get the last added node to process its children
node := template.Nodes[len(template.Nodes)-1]
// Parse the content between opening and closing tags
tempPrev, tempNext := p.parser(src, temp+1, typ, node)
tempToken := src[tempPrev : tempNext+1]
// Process the closing tag of the control structure
if typ == 1 {
p.addForNode(tempToken, nil, node)
} else if typ == 2 {
p.addIfNode(tempToken, nil, node)
}
// Update position markers to continue parsing after this block
next = tempNext + 1
prev = tempNext + 1
} else {
// If no match found, move to next character
next++
}
return next, prev
}
func (p *Parser) parseRemainingText(src string, prev, next int, template *Template) {
if next > prev {
token := src[prev:next]
p.addTextNode(token, template, nil)
}
}
// matchAppropriateStrings matches specific syntax structures in the template.
// It handles two main syntax formats:
// - Control structure tags {% ... %}
// - Variable expression tags {{ ... }}
// Returns:
// - bool: whether the match was successful
// - int: ending position of the match
// - int: syntax type (0:variable, 1:for, 2:if)
func (p *Parser) matchAppropriateStrings(src string, n int, next int, format string) (bool, int, int) {
// Store starting position (excluding the two opening characters)
temp := next - 2
switch format {
case "%}":
// Handle control structure cases
for next+1 < n {
// Look for "%}" ending tag
if src[next] == '%' && src[next+1] == '}' {
// Determine if it's a for or if statement
matched, typ := p.judgeBranchingStatements(src, temp, next+2)
if matched {
return true, next + 1, typ
} else {
return false, 0, 0
}
}
next++
}
case "}}":
// Handle variable expression cases
for next+1 < n {
// Look for "}}" ending tag
if src[next] == '}' && src[next+1] == '}' {
// Extract complete variable expression and validate format
token := src[temp : next+2]
matched := variableRegex.MatchString(token)
return matched, next + 1, 0
}
next++
}
default:
// Return failure for unsupported formats
return false, 0, 0
}
// Return failure if no match found
return false, 0, 0
}
// judgeBranchingStatements determines the type of control structure by matching against predefined regex patterns.
// It checks for the following control structures:
// - for/endfor statements (type 1/4)
// - if/endif statements (type 2/5)
// - else statements (type 3)
// Returns:
// - bool: whether a valid control structure was matched
// - int: the type of control structure (1-5) or 0 if no match
func (p *Parser) judgeBranchingStatements(src string, temp int, next int) (bool, int) {
token := src[temp:next]
switch {
case forRegex.MatchString(token):
return true, 1
case ifRegex.MatchString(token):
return true, 2
case elseRegex.MatchString(token):
return true, 3
case endforRegex.MatchString(token):
return true, 4
case endifRegex.MatchString(token):
return true, 5
default:
return false, 0
}
}
// parser processes the content within control structure blocks, including nested variables,
// control structures, and plain text. It recursively handles all nested structures until
// it encounters the corresponding end tag.
// Parameters:
// - src: source template string
// - prev: current starting position
// - typ: current control structure type
// - node: current node being processed
// Returns:
// - ending position and position of matched end tag
func (p *Parser) parser(src string, prev int, typ int, node *Node) (int, int) {
n := len(src)
next := prev
// Adjust type value to match end tags (e.g., 1->4 means for->endfor)
typ += 3
// Flag to track if we've entered an else branch
markEnterElse := false
// Store reference to if node for else branch handling
enterIfNode := node
for next < n {
switch {
case src[next] == '{' && next+1 < n && src[next+1] == '{':
// Handle variable expressions
next, prev = p.handleVariable(src, next, prev, nil, node)
case next+1 < n && src[next] == '{' && src[next+1] == '%':
// Handle control structures
matched, temp, tempType := p.matchAppropriateStrings(src, n, next+2, "%}")
switch {
case matched && tempType < 3:
next, prev = p.handleControlStructure(src, next, prev, temp, tempType, node)
case matched && tempType == 3 && typ == 5:
next, prev, markEnterElse, node = p.handleElseStructure(src, next, prev, temp, node)
case matched && tempType == typ:
if next > prev {
token := src[prev:next]
p.addTextNode(token, nil, node)
}
if markEnterElse {
node = enterIfNode //nolint:staticcheck,ineffassign
}
return next, temp
default:
next++
}
default:
next++
}
}
// No matching end tag found
return 0, 0
}
// handleControlStructure processes nested for/if control structures within a parent node.
// It handles both the opening and closing tags, and processes all content in between.
// Parameters:
// - src: source template string
// - next, prev: current positions in the source
// - temp: position of the end of opening tag
// - typ: type of control structure (1:for, 2:if)
// - node: parent node where this structure belongs
// Returns updated next and prev positions
func (p *Parser) handleControlStructure(src string, next, prev, temp, typ int, node *Node) (int, int) {
// Extract the complete opening tag
token := src[next : temp+1]
// Process any text content before this control structure
if next > prev {
token := src[prev:next]
p.addTextNode(token, nil, node)
}
// Create appropriate node based on control structure type
if typ == 1 {
p.addForNode(token, nil, node)
} else if typ == 2 {
p.addIfNode(token, nil, node)
}
// Get the newly created node to process its contents
node1 := node.Children[len(node.Children)-1]
// Recursively parse the content between opening and closing tags
tempPrev, tempNext := p.parser(src, temp+1, typ, node1)
// Extract and process the closing tag
tempToken := src[tempPrev : tempNext+1]
if typ == 1 {
p.addForNode(tempToken, nil, node1)
} else if typ == 2 {
p.addIfNode(tempToken, nil, node1)
}
// Update positions to continue parsing after this structure
next = tempNext + 1
prev = tempNext + 1
return next, prev
}
// handleElseStructure processes the else branch within an if control structure.
// It handles the else tag and prepares for processing the else branch content.
// Parameters:
// - src: source template string
// - next, prev: current positions in the source
// - temp: position of the end of else tag
// - node: current if node being processed
// Returns:
// - updated next and prev positions
// - markEnterElse flag to indicate else branch
// - updated node reference for else content
func (p *Parser) handleElseStructure(src string, next, prev, temp int, node *Node) (int, int, bool, *Node) {
// Extract the complete else tag
token := src[next : temp+1]
// Process any text content before the else tag
if next > prev {
token := src[prev:next]
p.addTextNode(token, nil, node)
}
// Add else node as a child of current if node
p.addIfNode(token, nil, node)
// Set flag to indicate we're in else branch
markEnterElse := true
// Get the newly created else node
tempNode := node.Children[len(node.Children)-1]
node = tempNode
// Update positions to continue parsing after else tag
next = temp + 1
prev = temp + 1
return next, prev, markEnterElse, node
}
// handleVariable processes variable expressions {{ ... }} in the template.
// It can handle variables both at the root level (using template) and
// nested within control structures (using node).
// Parameters:
// - src: source template string
// - next, prev: current positions in the source
// - template: root template (nil if processing nested variable)
// - node: parent node (nil if processing root level variable)
// Returns updated next and prev positions
func (p *Parser) handleVariable(src string, next, prev int, template *Template, node *Node) (int, int) {
n := len(src)
// Try to match variable expression ending with "}}"
matched, tempNext, _ := p.matchAppropriateStrings(src, n, next+2, "}}")
if matched { //nolint:nestif
// Extract the complete variable expression
token := src[next : tempNext+1]
// Process any text content before the variable
if next > prev {
token := src[prev:next]
if template != nil {
// Add text node to root template
p.addTextNode(token, template, nil)
} else {
// Add text node to parent control structure
p.addTextNode(token, nil, node)
}
}
// Add the variable node to appropriate parent
if template != nil {
// Add variable to root template
p.addVariableNode(token, template, nil)
} else {
// Add variable to parent control structure
p.addVariableNode(token, nil, node)
}
// Update positions to continue after variable
prev = tempNext + 1
next = tempNext + 1
} else {
// No valid variable match found, move forward
next++
}
return next, prev
}