-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.go
More file actions
357 lines (321 loc) · 8.57 KB
/
parse.go
File metadata and controls
357 lines (321 loc) · 8.57 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
package replvar
import (
"fmt"
"io"
"unicode"
"github.com/KarpelesLab/typutil"
)
// parser holds the state for parsing variable expressions and strings.
// It operates on a buffer of runes and provides methods for tokenization
// and AST construction.
type parser struct {
buf []rune // input buffer of runes to be parsed
}
// escapedChars maps escape sequence characters to their actual values.
// These are recognized within double-quoted strings (e.g., "\n" becomes newline).
var escapedChars = map[rune]rune{
'r': '\r',
'n': '\n',
't': '\t',
'v': '\v',
'\\': '\\',
}
// ParseString parses a string that may contain embedded variable expressions.
// Variable expressions are delimited by {{ and }}. The mode parameter controls
// how nested variables are handled:
// - "text": variables are resolved to their string representation
// - "json": variables are automatically JSON-encoded when embedded
func ParseString(s string, mode string) (Var, error) {
p := newParser(s)
return p.parseString(-1, mode)
}
// ParseVariable parses a variable expression (the content typically found inside {{}}).
// This handles variable names, operators, and nested expressions directly.
func ParseVariable(s string) (Var, error) {
p := newParser(s)
return p.parse(false)
}
// newParser creates a new parser initialized with the given string.
func newParser(s string) *parser {
p := &parser{
buf: []rune(s),
}
return p
}
// parse parses a variable expression using a two-stage approach:
//
// Stage 1: Tokenization - reads tokens and converts them to Var objects.
// Operators are stored as varPendingToken placeholders.
//
// Stage 2: Operator association - processes pending tokens to build the
// final AST by associating operators with their operands.
//
// If varStart is true, parsing expects to end with }} (TokenVariableEnd).
// If varStart is false, }} will raise an error.
func (p *parser) parse(varStart bool) (Var, error) {
var res []Var
// Stage 1: Tokenization loop
mainloop:
for {
if p.empty() {
if varStart {
// unexpected
return nil, io.ErrUnexpectedEOF
}
// reached end of buffer
break
}
tok, dat := p.readToken()
switch tok {
case TokenVariableEnd:
if !varStart {
return nil, fmt.Errorf("unexpected token }}")
}
break mainloop
case TokenStringConstant:
sub, err := p.parseString(dat[0], "text")
if err != nil {
return nil, err
}
res = append(res, sub)
case TokenNumber:
v, ok := typutil.AsNumber(string(dat))
if !ok {
return nil, fmt.Errorf("invalid number: %s", string(dat))
}
res = append(res, &staticVar{v})
case TokenVariable:
res = append(res, varFetchFromCtx(string(dat)))
case TokenInvalid:
return nil, fmt.Errorf("invalid token found, value=%v", dat)
default:
// unknown token, defer to step 2
res = append(res, varPendingToken(tok))
}
}
if len(res) == 0 {
return varNull{}, nil
}
// Stage 2: Operator association
// Build the AST respecting operator precedence.
return associateOperators(res)
}
// associateOperators processes a slice of Var and pending tokens to build
// the final AST with proper operator precedence.
func associateOperators(res []Var) (Var, error) {
if len(res) == 0 {
return varNull{}, nil
}
if len(res) == 1 {
return res[0], nil
}
// Step 1: Handle leading unary operators (!, ~)
if tok, ok := res[0].(varPendingToken); ok {
t := Token(tok)
if t.IsUnary() {
inner, err := associateOperators(res[1:])
if err != nil {
return nil, err
}
switch t {
case TokenNot:
return &varNot{inner}, nil
case TokenBitwiseNot:
return &varBitwiseNot{inner}, nil
}
}
return nil, fmt.Errorf("unexpected token at start: %v", tok)
}
// Step 2: First pass - handle all dot operators (highest precedence, left-to-right)
for i := 1; i < len(res)-1; i += 2 {
if tok, ok := res[i].(varPendingToken); ok && Token(tok) == TokenDot {
if v2, ok := res[i+1].(varFetchFromCtx); ok {
access := &varAccessOffset{res[i-1], string(v2)}
res = append(res[:i-1], append([]Var{access}, res[i+2:]...)...)
i -= 2 // reprocess from same position
if i < 1 {
i = -1
}
} else {
return nil, fmt.Errorf("invalid syntax: dot not followed by var")
}
}
}
if len(res) == 1 {
return res[0], nil
}
// Step 2b: Handle filter pipes (|) - when | is followed by a known filter name
for i := 1; i < len(res)-1; i += 2 {
if tok, ok := res[i].(varPendingToken); ok && Token(tok) == TokenOr {
if v2, ok := res[i+1].(varFetchFromCtx); ok {
if fn := LookupFilter(string(v2)); fn != nil {
filter := &varFilter{input: res[i-1], name: string(v2), fn: fn}
res = append(res[:i-1], append([]Var{filter}, res[i+2:]...)...)
i -= 2
if i < 1 {
i = -1
}
continue
}
}
}
}
if len(res) == 1 {
return res[0], nil
}
// Step 3: Find the lowest precedence operator (rightmost for left-associativity)
// Lower precedence number = binds tighter, so we want highest precedence number
lowestPrecIdx := -1
lowestPrec := -1
for i := 1; i < len(res)-1; i += 2 {
if tok, ok := res[i].(varPendingToken); ok {
prec := Token(tok).Precedence()
if prec >= lowestPrec {
lowestPrec = prec
lowestPrecIdx = i
}
}
}
if lowestPrecIdx == -1 {
return nil, fmt.Errorf("invalid syntax: no operator found")
}
tok := res[lowestPrecIdx].(varPendingToken)
t := Token(tok)
// Build left and right subtrees
left, err := associateOperators(res[:lowestPrecIdx])
if err != nil {
return nil, err
}
right, err := associateOperators(res[lowestPrecIdx+1:])
if err != nil {
return nil, err
}
if math := t.MathOp(); math != "" {
return &varMath{left, right, math}, nil
}
return nil, fmt.Errorf("unexpected operator: %v", tok)
}
// parseString parses a string literal or template string.
//
// The cut parameter specifies the closing delimiter:
// - -1: parse until end of input (for top-level strings)
// - '"', '\'', '`': parse until matching quote (for quoted strings)
//
// The mode parameter controls variable handling ("text" or "json").
// Supports escape sequences (in double-quoted strings) and nested {{}} expressions.
func (p *parser) parseString(cut rune, mode string) (Var, error) {
var str []rune // accumulator for literal characters
var res []Var // result Var objects (static strings and variables)
mainloop:
for {
c := p.take()
if c == cut {
// reached the end of the string
break
}
if c == -1 {
// unexpected end of string
return nil, io.ErrUnexpectedEOF
}
switch c {
case '\\':
// escape char
nc := p.cur()
if nc == cut && cut != -1 {
str = append(str, nc)
p.forward()
continue mainloop
}
if cut == '"' {
if n, ok := escapedChars[nc]; ok {
str = append(str, n)
p.forward()
continue mainloop
}
}
if nc == '\\' {
str = append(str, nc)
p.forward()
continue mainloop
}
// not a matching thing, just include the \ character to the output
case '{':
if p.cur() == '{' {
// we have a string, flush it
if len(str) > 0 {
res = append(res, &staticVar{string(str)})
str = nil
}
p.forward()
// parse subvar
sub, err := p.parse(true)
if err != nil {
return nil, err
}
if mode == "json" {
// if json mode, encode any subvar as json
sub = &varJsonMarshal{sub}
}
res = append(res, sub)
continue mainloop
}
}
// nothing happened
str = append(str, c)
}
if len(str) > 0 {
res = append(res, &staticVar{string(str)})
str = nil
}
if len(res) == 1 {
return res[0], nil
}
return varConcat(res), nil
}
// cur returns the current rune without consuming it, or -1 if at end.
func (p *parser) cur() rune {
if len(p.buf) == 0 {
return -1
}
return p.buf[0]
}
// empty returns true if the parser buffer is exhausted.
func (p *parser) empty() bool {
return len(p.buf) == 0
}
// forward advances the parser by one rune.
func (p *parser) forward() {
if len(p.buf) > 0 {
p.buf = p.buf[1:]
}
}
// forward2 advances the parser by two runes (used for two-character tokens like == or &&).
func (p *parser) forward2() {
if len(p.buf) > 1 {
p.buf = p.buf[2:]
} else {
p.buf = nil
}
}
// take returns the current rune and advances the parser, or -1 if at end.
func (p *parser) take() rune {
if len(p.buf) == 0 {
return -1
}
r := p.buf[0]
p.buf = p.buf[1:]
return r
}
// next returns the rune after the current one (lookahead), or -1 if unavailable.
func (p *parser) next() rune {
if len(p.buf) < 2 {
return -1
}
return p.buf[1]
}
// skipSpaces advances past any whitespace characters.
func (p *parser) skipSpaces() {
for unicode.IsSpace(p.cur()) {
p.forward()
}
}