-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.cpp
More file actions
456 lines (401 loc) · 13 KB
/
Copy pathtemplates.cpp
File metadata and controls
456 lines (401 loc) · 13 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
#include <assert.h>
#include <stdio.h>
#include "templates.h"
enum TokenType {
TT_None = 0,
TT_Str,
TT_Var,
TT_If,
TT_Else,
TT_End,
};
struct LinkedToken {
TokenType type = TT_None;
String value;
u32 start;
u32 end;
LinkedToken* next;
};
inline static void
linked_token_free(LinkedToken* token)
{
while (token) {
LinkedToken* dead = token;
token = token->next;
if (dead->value) {
string_free(dead->value);
}
free(dead);
}
}
static void
print_error(const char* message, u32 start, u32 end, String action_template)
{
// Print a message like:
//
// Error: error message here
// some template error somewhere
// ^^^^^
//
fprintf(stdout, "Error: %s.\n%s\n", message, action_template);
u32 i = 0;
while (i++ < start)
printf(" ");
while (i++ <= end)
printf("^");
}
static void
add_token_and_reset(TokenType type, String value, u32 offset, LinkedToken** head, LinkedToken** end)
{
LinkedToken* new_token = ALLOC(LinkedToken, 1);
new_token->type
= type;
// We expect this function to be called once the entire token has been scanned,
// and offset pointing to the first character of the next token.
new_token->start = offset - string_len(value);
new_token->end = offset;
new_token->value = string_new(value);
// Set the first token in the list if it's not already set
if (!*head)
*head = new_token;
// If we have an end token set, link it with the new current before making
// the new token the new end.
if (*end)
(*end)->next = new_token;
*end = new_token;
string_clear(value);
}
static LinkedToken*
tokenize_template(String action_template)
{
enum TokenizerMode {
Literal,
VarBlock,
};
// Flag set whenever any error has ocurred during parsing
bool error = false;
// Flag set whenever a variable has been seen inside a var block
bool seen_variable = false;
// Flag set when a $ character has been seen, expecting the next character to be either $ or {
bool escape_mode = false;
// Flag set when the next pass through the loop should consume any existing whitespace
bool skip_next_whitespace = false;
TokenizerMode mode = Literal;
String curlit = string_new();
u32 offset = 0;
LinkedToken* tokens = 0;
LinkedToken* end = 0;
while (offset < string_len(action_template) && !error) {
if (skip_next_whitespace) {
while (action_template[offset] == ' ') {
offset++;
}
skip_next_whitespace = false;
}
char c = action_template[offset];
switch (mode) {
case Literal:
if (c == '$') {
if (escape_mode) {
escape_mode = false;
curlit = string_append(curlit, "$");
} else {
escape_mode = true;
}
} else if (c == '{') {
if (escape_mode) {
if (string_len(curlit)) {
add_token_and_reset(TT_Str, curlit, offset, &tokens, &end);
}
mode = VarBlock;
skip_next_whitespace = true;
} else {
curlit = string_append(curlit, c);
}
escape_mode = false;
} else {
if (escape_mode) {
print_error("Unexpected character (use $$ to output a literal $)", offset, offset + 1, action_template);
error = true;
break;
} else {
curlit = string_append(curlit, c);
}
}
break;
case VarBlock:
if (c == '}') {
if (string_len(curlit)) {
TokenType type = TT_Var;
if (string_eq(curlit, "else")) {
type = TT_Else;
} else if (string_eq(curlit, "end")) {
type = TT_End;
}
add_token_and_reset(type, curlit, offset, &tokens, &end);
}
seen_variable = false;
mode = Literal;
} else if (is_identifier_char(c)) {
if (seen_variable) {
print_error("Only a single variable allowed per block", offset, offset + 1, action_template);
error = true;
break;
}
curlit = string_append(curlit, c);
} else if (c == '?') {
if (string_len(curlit) > 0) {
add_token_and_reset(TT_If, curlit, offset, &tokens, &end);
skip_next_whitespace = true;
} else {
print_error("Missing variable", offset, offset + 1, action_template);
error = true;
break;
}
seen_variable = true;
} else if (c == ' ') {
add_token_and_reset(TT_Var, curlit, offset, &tokens, &end);
seen_variable = true;
skip_next_whitespace = true;
} else {
print_error("Unexpected character", offset, offset + 1, action_template);
error = true;
break;
}
break;
}
offset++;
}
if (mode == Literal) {
add_token_and_reset(TT_Str, curlit, offset, &tokens, &end);
} else if (!error) {
print_error("Unfinished variable block", string_len(action_template) - 1, string_len(action_template), action_template);
error = true;
}
string_free(curlit);
if (error) {
linked_token_free(tokens);
return 0;
}
return tokens;
}
static String
get_truthy_value(VarList* vars, const char* name)
{
String value = template_get(vars, name);
return value && !string_eq(value, "") ? value : 0;
}
static bool
_process_conditional(
LinkedToken** tokens,
VarList* vars,
String action_template,
bool skip_all,
String* result_out)
{
LinkedToken* token = (*tokens);
bool error = false;
bool seen_else = false;
bool seen_end = false;
assert(token->type == TT_If);
bool skip_block = get_truthy_value(vars, token->value) == 0;
while ((token = token->next)) {
skip_block = skip_all || skip_block;
if (token->type == TT_Str) {
if (skip_block)
continue;
*result_out = string_append(*result_out, token->value);
} else if (token->type == TT_Var) {
if (skip_block)
continue;
String value = get_truthy_value(vars, token->value);
if (value) {
*result_out = string_append(*result_out, value);
}
} else if (token->type == TT_If) {
if (!_process_conditional(&token, vars, action_template, skip_block, result_out)) {
error = true;
break;
}
} else if (token->type == TT_Else) {
if (seen_else) {
print_error("Too many ${else} blocks", token->start, token->end, action_template);
error = true;
break;
}
seen_else = true;
skip_block = !skip_block;
} else if (token->type == TT_End) {
seen_end = true;
break;
}
}
// Write the token to the caller to continue from
*tokens = token;
// The missing end is a special error case that we can't detect in the
// loop. Instead we detect it by exiting the loop without any other error,
// but without ever seening an END block.
if (!error && !seen_end) {
print_error("Missing ${end}", string_len(action_template) - 1, string_len(action_template), action_template);
return false;
}
return !error;
}
VarList*
template_set(VarList* head, const char* varname, const char* varvalue)
{
VarList* node = head;
VarList* end = 0;
while (node && !string_eq(node->name, varname)) {
end = node;
node = node->next;
}
if (node) {
// Overwrite the value of the existing variable with the same name
node->value = string_copy(node->value, varvalue);
} else {
// No existing node found, create a new one and append it to the end
node = ALLOC(VarList, 1);
node->name = string_new(varname);
node->value = string_new(varvalue);
if (end) {
end->next = node;
}
}
return head ? head : node;
}
VarList*
template_merge(VarList* base, VarList* extended)
{
VarList* result = 0;
VarList* varptr = base;
while (varptr) {
result = template_set(result, varptr->name, varptr->value);
varptr = varptr->next;
}
varptr = extended;
while (varptr) {
result = template_set(result, varptr->name, varptr->value);
varptr = varptr->next;
}
return result;
}
void template_free(VarList* node)
{
while (node) {
string_free(node->name);
string_free(node->value);
VarList* dead = node;
node = node->next;
free(dead);
}
}
String
template_get(VarList* node, const char* name)
{
while (node) {
if (string_eq(node->name, name)) {
return node->value;
}
node = node->next;
}
return 0;
}
String
template_generate_usage(String action_template, const char* action_name)
{
LinkedToken* tokens = tokenize_template(action_template);
if (!tokens) {
return 0;
}
u8 seen_pos[10] = { 0 };
bool has_pos_args = false;
String named_arg_desc = string_new();
StringList* seen_names = 0;
bool has_named_vars = false;
LinkedToken* tok = tokens;
while (tok) {
if ((tok->type == TT_If) || (tok->type == TT_Var)) {
if (string_len(tok->value) == 1 && (is_digit(*tok->value))) {
// Collect all of the seen positional arguments and loop over them in
// position order afterward. They can appear in any order in the template,
// but the order is (obviously) fixed on the command line.
seen_pos[*tok->value - '0'] = 1;
has_pos_args = true;
} else {
if (!string_list_contains(seen_names, tok->value)) {
named_arg_desc = string_append(named_arg_desc, " [--");
named_arg_desc = string_append(named_arg_desc, tok->value);
named_arg_desc = string_append(named_arg_desc, " <value>]");
seen_names = string_list_add_front_dup(seen_names, tok->value);
has_named_vars = true;
}
}
}
tok = tok->next;
}
string_list_free(seen_names);
linked_token_free(tokens);
String result = string_new("Usage: ");
result = string_append(result, action_name);
if (has_pos_args) {
String pos_arg_desc = string_new();
for (u8 i = 0; i < 10; i++) {
if (seen_pos[i]) {
pos_arg_desc = string_append(pos_arg_desc, " $");
pos_arg_desc = string_append(pos_arg_desc, (char)('0' + i));
}
}
result = string_append(result, pos_arg_desc);
string_free(pos_arg_desc);
}
if (has_named_vars) {
result = string_append(result, named_arg_desc);
string_free(named_arg_desc);
}
result = string_append(result, '\n');
return result;
}
String
template_render(String action_template, VarList* vars)
{
LinkedToken* tokens = tokenize_template(action_template);
if (!tokens) {
// Failed to tokenize the template string
return 0;
}
String result = string_new();
bool error = false;
LinkedToken* curtok = tokens;
while (curtok) {
// printf("Token main loop: %d [%s]\n", curtok->type, curtok->value);
if (curtok->type == TT_If) {
if (!_process_conditional(&curtok, vars, action_template, false, &result)) {
error = true;
break;
}
} else if (curtok->type == TT_Str) {
result = string_append(result, curtok->value);
} else if (curtok->type == TT_Else) {
print_error("Unexpected ${else} block", curtok->start, curtok->end, action_template);
error = true;
break;
} else if (curtok->type == TT_End) {
print_error("Unexpected ${end} block", curtok->start, curtok->end, action_template);
error = true;
break;
} else if (curtok->type == TT_Var) {
String value = get_truthy_value(vars, curtok->value);
if (value) {
result = string_append(result, value);
}
}
curtok = curtok->next;
}
if (error) {
string_free(result);
result = 0;
}
linked_token_free(tokens);
return result;
}