-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.n
More file actions
508 lines (448 loc) · 13.1 KB
/
parser.n
File metadata and controls
508 lines (448 loc) · 13.1 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
import strings
import libc
// Comment character
const CHAR_COMMENT = 35 // '#'
// Quote prefixes
const PREFIX_SINGLE_QUOTE = 39 // single quote '
const PREFIX_DOUBLE_QUOTE = 34 // double quote "
// Export prefix
const EXPORT_PREFIX = 'export'
// parse_bytes parses .env content bytes into a map of key-value pairs.
// It modifies the provided map in place, adding found variables.
#local
fn parse_bytes(string src, {string:string} out):void! {
// normalize line endings: replace \r\n with \n
var normalized = replace_all(src, '\r\n', '\n')
var cutset = normalized
for cutset.len() > 0 {
// skip to next statement (skip whitespace and comments)
var start_result = get_statement_start(cutset)
if !start_result.found {
break
}
cutset = start_result.value
// locate and parse the key name
var key_result = locate_key_name(cutset)
if key_result.err.len() > 0 {
throw errorf(key_result.err)
}
var key = key_result.key
cutset = key_result.rest
// extract the variable value
var val_result = extract_var_value(cutset, out)
if val_result.err.len() > 0 {
throw errorf(val_result.err)
}
out[key] = val_result.value
cutset = val_result.rest
}
}
// Result type for string search operations
#local
type find_result_t = struct {
bool found
string value
}
// Result type for key name parsing
#local
type key_result_t = struct {
string key
string rest
string err
}
// Result type for value extraction
#local
type val_result_t = struct {
string value
string rest
string err
}
// get_statement_start skips whitespace and comment lines, returning the start
// of the next meaningful statement.
#local
fn get_statement_start(string src):find_result_t {
var pos = index_of_non_space_char(src)
if pos == -1 {
return find_result_t{found: false}
}
var trimmed = src[pos..]
if trimmed.len() == 0 {
return find_result_t{found: false}
}
if trimmed[0] != CHAR_COMMENT {
return find_result_t{found: true, value: trimmed}
}
// skip comment line - find the next newline
var nl = find_char_index(trimmed, '\n'[0])
if nl == -1 {
return find_result_t{found: false}
}
return get_statement_start(trimmed[nl..])
}
// locate_key_name locates and parses the key name from the beginning of a line.
// It supports optional "export " prefix and keys matching [A-Za-z0-9_.]
#local
fn locate_key_name(string src):key_result_t {
// trim leading spaces (not newlines)
var trimmed = trim_left_spaces(src)
// handle "export " prefix
if trimmed.len() >= EXPORT_PREFIX.len() {
if trimmed[..EXPORT_PREFIX.len()] == EXPORT_PREFIX {
var after_export = trimmed[EXPORT_PREFIX.len()..]
if after_export.len() > 0 && is_space(after_export[0]) {
trimmed = trim_left_spaces(after_export)
}
}
}
if trimmed.len() == 0 {
return key_result_t{err: 'zero length string'}
}
// locate key name end - scan for = or :
var key_end = -1
for int i = 0; i < trimmed.len(); i += 1 {
var c = trimmed[i]
if is_space(c) {
continue
}
if c == '='[0] || c == ':'[0] {
key_end = i
break
}
if c == '_'[0] || c == '.'[0] {
continue
}
if is_letter(c) || is_digit(c) {
continue
}
return key_result_t{err: 'unexpected character in variable name'}
}
if key_end == -1 {
return key_result_t{err: 'missing = or : in variable declaration'}
}
// trim trailing whitespace from key
var key = trim_right_spaces(trimmed[..key_end])
// trim leading whitespace from value part
var rest = trim_left_spaces(trimmed[key_end + 1..])
return key_result_t{key: key, rest: rest}
}
// extract_var_value extracts a variable value from the source string.
// It handles unquoted values, single-quoted values, and double-quoted values.
// For double-quoted values, escape sequences are processed and variables are expanded.
#local
fn extract_var_value(string src, {string:string} vars):val_result_t {
if src.len() == 0 {
return val_result_t{}
}
var first = src[0]
// Check for quoted value
if first == PREFIX_DOUBLE_QUOTE || first == PREFIX_SINGLE_QUOTE {
var quote = first
// lookup quoted string terminator
for int i = 1; i < src.len(); i += 1 {
if src[i] != quote {
continue
}
// skip escaped quote
if i > 0 && src[i - 1] == '\\'[0] {
continue
}
// extract value between quotes
var value = src[1..i]
if quote == PREFIX_DOUBLE_QUOTE {
// unescape and expand variables for double-quoted strings
value = expand_variables(expand_escapes(value), vars)
}
var rest = ''
if i + 1 < src.len() {
rest = src[i + 1..]
}
return val_result_t{value: value, rest: rest}
}
// unterminated quoted value
var nl = find_char_index(src, '\n'[0])
var end_idx = src.len()
if nl != -1 {
end_idx = nl
}
return val_result_t{err: 'unterminated quoted value ' + src[..end_idx]}
}
// unquoted value - read until end of line
var end_of_line = find_line_end(src)
if end_of_line == -1 {
// Hit EOF without trailing newline
end_of_line = src.len()
if end_of_line == 0 {
return val_result_t{}
}
}
var line = src[..end_of_line]
if line.len() == 0 {
var rest = ''
if end_of_line < src.len() {
rest = src[end_of_line..]
}
return val_result_t{rest: rest}
}
// Check for inline comments: find # preceded by whitespace
var end_of_var = line.len()
for int i = 1; i < line.len(); i += 1 {
if line[i] == CHAR_COMMENT && i > 0 && is_space(line[i - 1]) {
end_of_var = i
break
}
}
var trimmed = trim_spaces(line[..end_of_var])
var rest = ''
if end_of_line < src.len() {
rest = src[end_of_line..]
}
return val_result_t{value: expand_variables(trimmed, vars), rest: rest}
}
// expand_escapes processes escape sequences in double-quoted strings.
// Supports \n (newline) and \r (carriage return).
#local
fn expand_escapes(string str):string {
var result = ''
var i = 0
for i < str.len() {
if str[i] == '\\'[0] && i + 1 < str.len() {
var next = str[i + 1]
if next == 'n'[0] {
result += '\n'
i += 2
continue
}
if next == 'r'[0] {
result += '\r'
i += 2
continue
}
if next == '\\'[0] {
result += '\\'
i += 2
continue
}
if next == '"'[0] {
result += '"'
i += 2
continue
}
if next == '\''[0] {
result += '\''
i += 2
continue
}
if next == '$'[0] {
result += '\\$'
i += 2
continue
}
// unknown escape, keep as-is
var c_bytes = [str[i + 1]]
result += c_bytes as string
i += 2
continue
}
var c_bytes = [str[i]]
result += c_bytes as string
i += 1
}
return result
}
// expand_variables replaces ${VAR}, $VAR, and $(VAR) references in the string
// with values from the provided map or from the system environment.
#local
fn expand_variables(string v, {string:string} vars):string {
var result = ''
var i = 0
for i < v.len() {
// check for escaped dollar sign
if v[i] == '\\'[0] && i + 1 < v.len() && v[i + 1] == '$'[0] {
result += '$'
i += 2
continue
}
if v[i] != '$'[0] {
var c_bytes = [v[i]]
result += c_bytes as string
i += 1
continue
}
// found $ - parse variable reference
i += 1
if i >= v.len() {
result += '$'
break
}
var var_name = ''
var has_braces = false
var has_parens = false
if v[i] == '{'[0] {
has_braces = true
i += 1
// read until closing brace
var start = i
for i < v.len() && v[i] != '}'[0] {
i += 1
}
if i < v.len() {
var_name = v[start..i]
i += 1 // skip closing brace
}
} else if v[i] == '('[0] {
has_parens = true
i += 1
// read until closing paren
var start = i
for i < v.len() && v[i] != ')'[0] {
i += 1
}
if i < v.len() {
var_name = v[start..i]
i += 1 // skip closing paren
}
} else {
// bare $VAR - read alphanumeric and underscore
var start = i
for i < v.len() && is_var_name_char(v[i]) {
i += 1
}
var_name = v[start..i]
}
if var_name.len() == 0 {
result += '$'
if has_braces {
result += '{}'
} else if has_parens {
result += '()'
}
continue
}
// lookup variable: first in provided map, then in environment
if vars.contains(var_name) {
result += vars[var_name]
} else {
var env_val = libc.getenv(var_name.to_cstr())
if env_val as anyptr != 0 as anyptr {
result += env_val.to_string()
}
}
}
return result
}
// -- Helper functions --
// is_var_name_char returns true if the char can appear in a variable name (A-Z, a-z, 0-9, _)
#local
fn is_var_name_char(u8 c):bool {
return is_letter(c) || is_digit(c) || c == '_'[0]
}
// is_space returns true for whitespace chars that are NOT line breaks
#local
fn is_space(u8 c):bool {
return c == ' '[0] || c == '\t'[0] || c == '\r'[0] || c == 11 || c == 12
}
// is_line_end returns true for newline characters
#local
fn is_line_end(u8 c):bool {
return c == '\n'[0] || c == '\r'[0]
}
// is_letter returns true if c is A-Z or a-z
#local
fn is_letter(u8 c):bool {
return (c >= 'A'[0] && c <= 'Z'[0]) || (c >= 'a'[0] && c <= 'z'[0])
}
// is_digit returns true if c is 0-9
#local
fn is_digit(u8 c):bool {
return c >= '0'[0] && c <= '9'[0]
}
// find_char_index finds the first occurrence of char c in string s, returns -1 if not found
#local
fn find_char_index(string s, u8 c):int {
for int i = 0; i < s.len(); i += 1 {
if s[i] == c {
return i
}
}
return -1
}
// find_line_end finds the index of the first line-ending character (\n or \r)
#local
fn find_line_end(string s):int {
for int i = 0; i < s.len(); i += 1 {
if s[i] == '\n'[0] || s[i] == '\r'[0] {
return i
}
}
return -1
}
// index_of_non_space_char finds the index of the first non-whitespace character
#local
fn index_of_non_space_char(string s):int {
for int i = 0; i < s.len(); i += 1 {
var c = s[i]
if c != ' '[0] && c != '\t'[0] && c != '\n'[0] && c != '\r'[0] && c != 11 && c != 12 {
return i
}
}
return -1
}
// trim_left_spaces trims leading space characters (not newlines) from a string
#local
fn trim_left_spaces(string s):string {
var i = 0
for i < s.len() && is_space(s[i]) {
i += 1
}
if i == 0 {
return s
}
return s[i..]
}
// trim_right_spaces trims trailing space characters (including newlines) from a string
#local
fn trim_right_spaces(string s):string {
var i = s.len()
for i > 0 && (is_space(s[i - 1]) || s[i - 1] == '\n'[0]) {
i -= 1
}
return s[..i]
}
// trim_spaces trims whitespace from both ends of a string
#local
fn trim_spaces(string s):string {
return trim_right_spaces(trim_left_spaces(s))
}
// replace_all replaces all occurrences of old with new_ in s
#local
fn replace_all(string s, string old, string new_):string {
if old.len() == 0 {
return s
}
var result = ''
var i = 0
for i <= s.len() - old.len() {
var is_match = true
for int j = 0; j < old.len(); j += 1 {
if s[i + j] != old[j] {
is_match = false
break
}
}
if is_match {
result += new_
i += old.len()
} else {
var c_bytes = [s[i]]
result += c_bytes as string
i += 1
}
}
// append remaining characters
for i < s.len() {
var c_bytes = [s[i]]
result += c_bytes as string
i += 1
}
return result
}