-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathanalyze_expressions.go
237 lines (211 loc) · 5.81 KB
/
analyze_expressions.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
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
package template
import (
"fmt"
)
type TokenType int
const (
TokenIdentifier TokenType = iota // Variable (e.g., user.age)
TokenBool // Boolean value (e.g., true, false)
TokenNumber // Number (e.g., 18)
TokenString // String constant
TokenOperator // Operators (e.g., ==, !=, <, >, &&, ||)
TokenArithOp // Arithmetic operators
TokenNot // Not operator (!)
TokenLParen // Left parenthesis (()
TokenRParen // Right parenthesis ())
TokenPipe // Pipe operator (|)
TokenFilter // Filter (including name and args, e.g., upper, truncate:30)
TokenEOF // End of input marker
TokenDot // Dot operator (.)
)
// Token is a token in the template language.
// It represents a type and a value.
type Token struct {
Typ TokenType // Token type
Val string // Token value
}
type Lexer struct {
input string // Input expression
pos int // Current scanning position
start int // Start position of current token
tokens []Token // Generated tokens
}
// Lexer is a lexer for the template language.
// It tokenizes the input string into a list of tokens.
func (l *Lexer) Lex() ([]Token, error) {
for l.pos < len(l.input) {
ch := l.input[l.pos]
switch {
case ch == '.':
l.pos++
l.emit(TokenDot)
l.start = l.pos
case isSpace(ch):
l.ignore() // Ignore whitespace
case isDigit(ch):
l.lexNumber() // Parse number
case ch == '"' || ch == '\'':
if err := l.lexString(); err != nil { // Parse string
return nil, err
}
case isLetter(ch) || ch == '_':
l.lexIdentifierOrKeyword() // Parse identifier or keyword
case ch == '(':
l.pos++
l.emit(TokenLParen) // Left parenthesis
l.start = l.pos
case ch == ')':
l.pos++
l.emit(TokenRParen) // Right parenthesis
l.start = l.pos
case isArithOperator(ch):
l.lexArithOperator() // Parse arithmetic operator
case isOperatorChar(ch, l.pos, l.input):
l.lexOperator() // Parse operator
case ch == '!':
l.lexNot() // Parse not operator
case ch == '|':
l.lexPipeOrFilter() // Parse pipe or filter
default:
return nil, fmt.Errorf("%w: %c", ErrUnexpectedCharacter, ch)
}
}
l.emit(TokenEOF) // Add end marker
return l.tokens, nil
}
func (l *Lexer) lexNumber() {
for l.pos < len(l.input) && (isDigit(l.input[l.pos]) || l.input[l.pos] == '.') {
l.pos++
}
l.emit(TokenNumber)
}
func (l *Lexer) lexIdentifierOrKeyword() {
for l.pos < len(l.input) && (isLetter(l.input[l.pos]) || isDigit(l.input[l.pos]) || l.input[l.pos] == '.' || l.input[l.pos] == '_') {
l.pos++
}
val := l.input[l.start:l.pos]
if val == "true" || val == "false" {
l.emit(TokenBool) // Boolean value
} else {
l.emit(TokenIdentifier) // Identifier
}
}
func (l *Lexer) lexNot() {
l.pos++
l.emit(TokenNot)
}
func (l *Lexer) lexPipeOrFilter() {
l.pos++ // Skip '|'
l.emit(TokenPipe)
// Skip whitespace
for l.pos < len(l.input) && isSpace(l.input[l.pos]) {
l.pos++
}
l.start = l.pos
if l.pos < len(l.input) && isLetter(l.input[l.pos]) {
// Parse filter name and arguments (if any)
for l.pos < len(l.input) {
ch := l.input[l.pos]
// Allow letters, digits, colons, commas, quotes and spaces as part of filter
if isLetter(ch) || isDigit(ch) || ch == ':' || ch == ',' ||
ch == '"' || ch == '\'' || ch == ' ' || ch == '_' {
l.pos++
continue
}
// Stop at other characters
break
}
// Remove trailing spaces
end := l.pos
for end > l.start && isSpace(l.input[end-1]) {
end--
}
// Emit filter token with complete filter expression
l.tokens = append(l.tokens, Token{
Typ: TokenFilter,
Val: l.input[l.start:end],
})
l.start = l.pos
}
}
func (l *Lexer) lexOperator() {
// Support multi-character operators (e.g., ==, !=, &&, ||)
if l.pos+1 < len(l.input) {
doubleOp := l.input[l.pos : l.pos+2]
if isOperator(doubleOp) {
l.pos += 2
l.emit(TokenOperator)
return
}
}
// Single character operator
l.pos++
l.emit(TokenOperator)
}
func (l *Lexer) lexString() error {
quote := l.input[l.pos] // Store quote type
l.pos++ // Skip opening quote
l.start = l.pos // Start recording after quote
for l.pos < len(l.input) {
if l.input[l.pos] == '\\' && l.pos+1 < len(l.input) {
l.pos += 2 // Skip escape character
continue
}
if l.input[l.pos] == quote {
val := l.input[l.start:l.pos]
l.pos++ // Skip closing quote
l.tokens = append(l.tokens, Token{Typ: TokenString, Val: val})
l.start = l.pos
return nil
}
l.pos++
}
return ErrUnterminatedString
}
func (l *Lexer) lexArithOperator() {
l.pos++
l.emit(TokenArithOp)
}
func isSpace(ch byte) bool {
return ch == ' ' || ch == '\t' || ch == '\n'
}
func isDigit(ch byte) bool {
return ch >= '0' && ch <= '9'
}
func isLetter(ch byte) bool {
return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
}
func isOperatorChar(ch byte, pos int, input string) bool {
if ch == '|' && pos+1 < len(input) && input[pos+1] != '|' {
return false
}
if ch == '!' && pos+1 < len(input) && input[pos+1] != '=' {
return false
}
return ch == '=' || ch == '!' || ch == '<' || ch == '>' || ch == '&' || ch == '|'
}
func isOperator(op string) bool {
operators := []string{"==", "!=", "<=", ">=", "&&", "||"}
for _, o := range operators {
if op == o {
return true
}
}
return false
}
func isArithOperator(ch byte) bool {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%'
}
func (l *Lexer) emit(typ TokenType) {
if typ != TokenEOF {
val := l.input[l.start:l.pos]
l.tokens = append(l.tokens, Token{Typ: typ, Val: val})
} else {
l.tokens = append(l.tokens, Token{Typ: typ, Val: "EOF"})
}
l.start = l.pos
}
func (l *Lexer) ignore() {
l.pos++
l.start = l.pos
}