-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
176 lines (150 loc) · 4.5 KB
/
parser.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
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
package template
import (
"regexp"
"strconv"
"strings"
)
// Regular expression to identify variables.
var variableRegex = regexp.MustCompile(`{{\s*([\w\.]+)((?:\s*\|\s*[\w\:\,]+(?:\s*:\s*[^}]+)?)*)\s*}}`)
// Parser analyzes template syntax.
type Parser struct{}
// NewParser creates a Parser with a compiled regular expression for efficiency.
func NewParser() *Parser {
return &Parser{}
}
// Parse transforms a template string into a Template.
func (p *Parser) Parse(src string) (*Template, error) {
template := NewTemplate()
tokens := p.tokenize(src)
for _, token := range tokens {
if p.isVariable(token) {
p.addVariableNode(token, template)
} else {
p.addTextNode(token, template)
}
}
return template, nil
}
// tokenize divides the source string into tokens for easier parsing.
func (p *Parser) tokenize(src string) []string {
tokens := make([]string, 0)
matches := variableRegex.FindAllStringIndex(src, -1)
start := 0
for _, match := range matches {
// Add text between variables as tokens
if start < match[0] {
tokens = append(tokens, src[start:match[0]])
}
// Add variable token
tokens = append(tokens, src[match[0]:match[1]])
start = match[1]
}
// Add remaining text as a token
if start < len(src) {
tokens = append(tokens, src[start:])
}
return tokens
}
// isVariable checks if a token represents a variable.
func (p *Parser) isVariable(token string) bool {
return strings.HasPrefix(token, "{{") && strings.HasSuffix(token, "}}")
}
// Updated addVariableNode processes a variable token, parses out any filters, and adds it to the template.
func (p *Parser) addVariableNode(token string, tpl *Template) {
// 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)
}
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) {
if text != "" {
tpl.Nodes = append(tpl.Nodes, &Node{Type: "text", Text: text})
}
}