-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
444 lines (365 loc) · 11.6 KB
/
parser.c
File metadata and controls
444 lines (365 loc) · 11.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
#include "parser.h"
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <stdarg.h>
static Parser ctx = {0};
// Error handling
static void error(const char *fmt, ...) {
if (ctx.has_error) return;
ctx.has_error = 1;
va_list args;
va_start(args, fmt);
snprintf(ctx.error_msg, sizeof(ctx.error_msg), "L%d:C%d: ", ctx.line, ctx.col);
vsnprintf(ctx.error_msg + strlen(ctx.error_msg), sizeof(ctx.error_msg) - strlen(ctx.error_msg), fmt, args);
va_end(args);
}
static void update_pos() {
ctx.line = ctx.col = 1;
for (const char *p = ctx.src; p < ctx.pos; p++) {
if (*p == '\n') { ctx.line++; ctx.col = 1; }
else ctx.col++;
}
}
// Parsing utilities
static void skip_ws() {
while (*ctx.pos && isspace(*ctx.pos)) ctx.pos++;
update_pos();
}
static int is_identifier(char c) {
return isalnum(c) || c == '_';
}
static int expect(char c) {
skip_ws();
return *ctx.pos == c ? (ctx.pos++, update_pos(), 1) : 0;
}
static Node *expr(); // forward
// Parse string literal
static char *parse_string() {
skip_ws();
if (*ctx.pos != '\'' && *ctx.pos != '"') return NULL;
char q = *ctx.pos++;
const char *start = ctx.pos;
while (*ctx.pos && *ctx.pos != q) {
ctx.pos += (*ctx.pos == '\\' && ctx.pos[1]) ? 2 : 1;
}
if (!*ctx.pos) { error("unterminated string"); return NULL; }
size_t len = ctx.pos - start;
char *buf = malloc(len + 1), *w = buf;
if (!buf) { error("memory allocation failed"); return NULL; }
for (const char *r = start; r < ctx.pos; r++) {
if (*r == '\\' && r + 1 < ctx.pos) {
switch (*++r) {
case 'n': *w++ = '\n'; break;
case 't': *w++ = '\t'; break;
case 'r': *w++ = '\r'; break;
default: *w++ = *r; break;
}
} else *w++ = *r;
}
*w = 0;
ctx.pos++; // skip closing quote
update_pos();
return buf;
}
// Parse number
static char *parse_number() {
skip_ws();
const char *start = ctx.pos;
if (*ctx.pos == '-') ctx.pos++;
if (!isdigit(*ctx.pos)) return NULL;
while (isdigit(*ctx.pos)) ctx.pos++;
if (*ctx.pos == '.') {
ctx.pos++;
while (isdigit(*ctx.pos)) ctx.pos++;
}
size_t len = ctx.pos - start;
char *buf = malloc(len + 1);
if (!buf) { error("memory allocation failed"); return NULL; }
memcpy(buf, start, len);
buf[len] = 0;
update_pos();
return buf;
}
// Parse identifier
static char *parse_identifier() {
skip_ws();
if (!isalpha(*ctx.pos) && *ctx.pos != '_') return NULL;
const char *start = ctx.pos;
while (is_identifier(*ctx.pos)) ctx.pos++;
size_t len = ctx.pos - start;
char *buf = malloc(len + 1);
if (!buf) { error("memory allocation failed"); return NULL; }
memcpy(buf, start, len);
buf[len] = 0;
update_pos();
return buf;
}
// Parse array [item1, item2, ...]
static Node *parse_array() {
skip_ws();
if (!expect('[')) return NULL;
Node *arr = node_new(NODE_LIST, NULL);
if (!arr) { error("memory allocation failed"); return NULL; }
while (1) {
skip_ws();
if (expect(']')) break;
if (!*ctx.pos) { error("unterminated array"); goto fail; }
Node *item = expr();
if (item) {
node_add(arr, item);
} else if (ctx.has_error) {
goto fail;
} else {
// Skip invalid token
while (*ctx.pos && *ctx.pos != ',' && *ctx.pos != ']') ctx.pos++;
}
skip_ws();
if (expect(',')) continue;
if (expect(']')) break;
error("expected ',' or ']' in array");
goto fail;
}
return arr;
fail:
node_free(arr);
return NULL;
}
// Parse primary expression
static Node *primary() {
skip_ws();
if (!*ctx.pos) { error("unexpected EOF"); return NULL; }
// String literal
char *str = parse_string();
if (str) {
Node *n = node_new(NODE_STRING, str);
free(str);
return n;
}
// Number
char *num = parse_number();
if (num) {
Node *n = node_new(NODE_NUMBER, num);
free(num);
return n;
}
// Array
if (*ctx.pos == '[') return parse_array();
// Identifier/boolean/function
char *id = parse_identifier();
if (!id) { error("unexpected '%c'", *ctx.pos); return NULL; }
// Boolean check
if (!strcasecmp(id, "true") || !strcasecmp(id, "false")) {
Node *n = node_new(NODE_BOOL, id);
free(id);
return n;
}
skip_ws();
// Function call
if (expect('(')) {
Node *fn = node_new(NODE_FUNCTION, id);
free(id);
if (!fn) { error("memory allocation failed"); return NULL; }
skip_ws();
if (expect(')')) return fn;
while (1) {
Node *arg = expr();
if (arg) {
node_add(fn, arg);
} else if (ctx.has_error) {
node_free(fn);
return NULL;
}
skip_ws();
if (expect(',')) continue;
if (expect(')')) break;
error("expected ',' or ')' in function call");
node_free(fn);
return NULL;
}
return fn;
}
// Regular identifier
Node *n = node_new(NODE_IDENTIFIER, id);
free(id);
return n;
}
// Parse property access and multi-access
static Node *parse_access() {
Node *left = primary();
if (!left) return NULL;
skip_ws();
if (*ctx.pos != '.') return left;
// Create object chain
Node *obj = (left->kind == NODE_IDENTIFIER) ?
node_new(NODE_OBJECT, left->value) :
node_new(NODE_OBJECT, NULL);
if (!obj) {
error("memory allocation failed");
node_free(left);
return NULL;
}
if (left->kind == NODE_IDENTIFIER) {
node_free(left);
} else {
node_add(obj, left);
}
Node *cur = obj;
while (expect('.')) {
skip_ws();
// Multi-access: .[prop1, prop2, ...]
if (expect('[')) {
Node *multi_list = node_new(NODE_LIST, NULL);
if (!multi_list) { error("memory allocation failed"); node_free(obj); return NULL; }
while (1) {
skip_ws();
if (expect(']')) break;
if (!*ctx.pos) { error("unterminated multi-access"); node_free(multi_list); node_free(obj); return NULL; }
//char *prop_name = parse_identifier();
Node *item = expr();
if (!item) {
error("expected expression name in multi-access");
node_free(multi_list);
node_free(obj);
return NULL;
}
node_add(multi_list, item);
skip_ws();
if (expect(',')) continue;
if (expect(']')) break;
error("expected ',' or ']' in multi-access");
node_free(multi_list);
node_free(obj);
return NULL;
}
node_add(cur, multi_list);
return obj;
}
// Regular property access
Node *right = primary();
if (!right) {
if (!ctx.has_error) error("expected property after '.'");
node_free(obj);
return NULL;
}
if (right->kind == NODE_IDENTIFIER) {
Node *prop = node_new(NODE_PROPERTY, right->value);
node_free(right);
if (!prop) { error("memory allocation failed"); node_free(obj); return NULL; }
node_add(cur, prop);
cur = prop;
} else {
node_add(cur, right);
cur = right;
}
skip_ws();
}
return obj;
}
// Main expression parser
static Node *expr() {
return parse_access();
}
static void free_nodes(char **nodes) {
if (!nodes) return;
for (char **p = nodes; *p; p++) {
free(*p);
}
free(nodes);
}
// Extract and parse placeholders from text
PlaceholderResult parse_placeholders(const char *text) {
PlaceholderResult result = {
.nodes = NULL,
.error_code = 0
};
if (!text) {
result.error_code = -1;
return result;
}
size_t cap = 8;
size_t count = 0;
char **nodes = (char **)malloc(cap * sizeof(char *));
if (!nodes) {
result.error_code = -1;
return result;
}
nodes[0] = NULL; // Keep NULL for dynamic iteration
const char *p = text;
while (1) {
const char *start = strstr(p, "${");
if (!start) break;
const char *content_start = start + 2;
const char *end = content_start;
int depth = 0;
while (*end) {
if (*end == '{') depth++;
else if (*end == '}') {
if (depth == 0) break;
depth--;
}
end++;
}
if (!*end) {
result.error_code = -1;
free_nodes(nodes);
return result;
}
size_t content_len = end - content_start;
char *content = (char*)malloc(content_len + 1);
if (!content) {
result.error_code = -1;
free_nodes(nodes);
return result;
}
memcpy(content, content_start, content_len);
content[content_len] = '\0';
// parsing simulation of content
int parse_error = 0; // ctx.has_error
if (!parse_error) {
if (count + 1 >= cap) {
cap *= 2;
char **tmp = (char **)realloc(nodes, cap * sizeof(char *));
if (!tmp) {
result.error_code = -1;
free(content);
free_nodes(nodes);
return result;
}
nodes = tmp;
}
nodes[count++] = content;
nodes[count] = NULL; //Keep NULL for dynamic iteration
} else {
fprintf(stderr, "Parse error in placeholder '${%.*s}'\n", (int)content_len, content);
free(content);
}
p = end + 1;
}
result.nodes = nodes;
return result;
}
// Public API for single expression parsing
Node *parse_expression_str(const char *source) {
if (!source) return NULL;
ctx = (Parser){.src = source, .pos = source, .line = 1, .col = 1, .has_error = 0};
Node *result = expr();
if (result && !ctx.has_error) {
// Check for unexpected content after valid expression
skip_ws();
if (*ctx.pos) {
error("unexpected content after expression: '%c'", *ctx.pos);
node_free(result);
result = NULL;
}
}
if (ctx.has_error) {
fprintf(stderr, "%s\n", ctx.error_msg);
if (result) { node_free(result); result = NULL; }
}
return result;
}
const char *parser_get_last_error() {
return ctx.has_error ? ctx.error_msg : NULL;
}