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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
#define _POSIX_C_SOURCE 200809L
#include "parser.h"
#include "tests/tests.h"
#include "ast.h"
#include "slice.h"
#include <string.h>
enum value_type {
VALUE_IDENT,
VALUE_INT,
VALUE_BOOL,
VALUE_STRING,
};
struct value {
union {
struct slice ident;
struct slice string;
int64_t integer;
bool boolean;
};
enum value_type type;
};
static void
check_parser_errors(struct parser *parser, const char *file, int line,
const char *func)
{
if (parser->errors->len > 0) {
printf("\n");
size_t i;
sds val;
vector_foreach (parser->errors, i, val) {
printf("parser error %lu: %s\n", i, val);
}
printf(TBLD TRED "FAIL!\n" TRST);
printf("%s:%d: %s: ", file, line, func);
printf("parser encountered errors\n");
abort();
}
}
#define check_parser_errors(p) \
check_parser_errors(p, __FILE__, __LINE__, __func__)
static void
test_integer_literal(struct expression *expr, int64_t val)
{
char buf[128];
struct slice sval;
asserteq(expr->type, EXPRESSION_INT);
asserteq(expr->integer.value, val);
sprintf(buf, "%ld", val);
sval = slice_whole(buf);
asserteq(slice_cmp(&expr->token.literal, &sval), 0);
}
static void
test_identifier(struct expression *expr, struct slice *ident)
{
asserteq(expr->type, EXPRESSION_IDENT);
asserteq(slice_cmp(&expr->token.literal, ident), 0);
}
static void
test_boolean_literal(struct expression *expr, bool val)
{
char *str = val ? "true" : "false";
struct slice sval = slice_whole(str);
asserteq(expr->type, EXPRESSION_BOOL);
asserteq(expr->boolean.value, val);
asserteq(slice_cmp(&expr->token.literal, &sval), 0);
}
static void
test_string_literal(struct expression *expr, const struct slice *val)
{
asserteq(expr->type, EXPRESSION_STRING);
asserteq(slice_cmp(&expr->string.value, val), 0);
}
static inline void
test_expected(struct expression *expr, struct value v)
{
switch (v.type) {
case VALUE_IDENT:
test_identifier(expr, &v.ident);
break;
case VALUE_INT:
test_integer_literal(expr, v.integer);
break;
case VALUE_BOOL:
test_boolean_literal(expr, v.boolean);
break;
case VALUE_STRING:
test_string_literal(expr, &v.string);
break;
}
}
#define VIDENT(v) \
(struct value) \
{ \
.type = VALUE_IDENT, .ident = slice_whole(v) \
}
#define VINT(v) \
(struct value) \
{ \
.type = VALUE_INT, .integer = v \
}
#define VBOOL(v) \
(struct value) \
{ \
.type = VALUE_BOOL, .boolean = v \
}
#define VSTR(v) \
(struct value) \
{ \
.type = VALUE_STRING, .string = slice_whole(v) \
}
static inline void
test_infix(struct infix *expr, struct value lval, struct slice *op,
struct value rval)
{
test_expected(expr->left, lval);
asserteq(slice_cmp(&expr->operator, op), 0);
test_expected(expr->right, rval);
}
static inline void
test_literal_variables(void)
{
struct {
char *input;
struct value val;
} tests[] = {
{
"{{ foo }}",
VIDENT("foo"),
},
{
"{{ 20 }}",
VINT(20),
},
{
"{{ true }}",
VBOOL(true),
},
{
"{{ false }}",
VBOOL(false),
},
{0},
};
for (size_t i = 0; tests[i].input != NULL; i++) {
struct parser *parser = parser_new(strdup("test"), tests[i].input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
test_expected(blk->variable.expression, tests[i].val);
parser_destroy(parser);
template_destroy(tmpl);
}
}
static inline void
test_prefix_variables(void)
{
struct {
char *input;
struct slice operator;
struct value val;
} tests[] = {
{
"{{ !foo }}",
slice_whole("!"),
VIDENT("foo"),
},
{
"{{ -bar }}",
slice_whole("-"),
VIDENT("bar"),
},
{
"{{ -20 }}",
slice_whole("-"),
VINT(20),
},
{
"{{ !true }}",
slice_whole("!"),
VBOOL(true),
},
{
"{{ !false }}",
slice_whole("!"),
VBOOL(false),
},
{
"{{ not false }}",
slice_whole("not"),
VBOOL(false),
},
{0},
};
for (size_t i = 0; tests[i].input != NULL; i++) {
struct parser *parser = parser_new(strdup("test"), tests[i].input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
asserteq(blk->variable.expression->type, EXPRESSION_PREFIX);
struct expression *pref = blk->variable.expression;
asserteq(slice_cmp(&pref->prefix.operator, & tests[i].operator), 0);
test_expected(pref->prefix.right, tests[i].val);
parser_destroy(parser);
template_destroy(tmpl);
}
}
static inline void
test_infix_variables(void)
{
struct {
char *input;
struct value left;
struct slice operator;
struct value right;
} tests[] = {
{
"{{ foo + bar }}",
VIDENT("foo"),
slice_whole("+"),
VIDENT("bar"),
},
{
"{{ 6 - 9 }}",
VINT(6),
slice_whole("-"),
VINT(9),
},
{
"{{ 4 * 20 }}",
VINT(4),
slice_whole("*"),
VINT(20),
},
{
"{{ foo / 20 }}",
VIDENT("foo"),
slice_whole("/"),
VINT(20),
},
{
"{{ \"str\" == \"str\" }}",
VSTR("str"),
slice_whole("=="),
VSTR("str"),
},
{
"{{ true != false }}",
VBOOL(true),
slice_whole("!="),
VBOOL(false),
},
{
"{{ 4 < 20 }}",
VINT(4),
slice_whole("<"),
VINT(20),
},
{
"{{ 4 <= 20 }}",
VINT(4),
slice_whole("<="),
VINT(20),
},
{
"{{ 100 > 20 }}",
VINT(100),
slice_whole(">"),
VINT(20),
},
{
"{{ 100 >= 20 }}",
VINT(100),
slice_whole(">="),
VINT(20),
},
{
"{{ true and true }}",
VBOOL(true),
slice_whole("and"),
VBOOL(true),
},
{
"{{ true or false }}",
VBOOL(true),
slice_whole("or"),
VBOOL(false),
},
{0},
};
for (size_t i = 0; tests[i].input != NULL; i++) {
struct parser *parser = parser_new(strdup("test"), tests[i].input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
asserteq(blk->variable.expression->type, EXPRESSION_INFIX);
test_infix(&blk->variable.expression->infix,
tests[i].left, &tests[i].operator, tests[i].right);
parser_destroy(parser);
template_destroy(tmpl);
}
}
static inline void
test_map_variables(void)
{
char *input = "{{ map.key }}";
struct value left = VIDENT("map");
struct value key = VIDENT("key");
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
asserteq(blk->variable.expression->type, EXPRESSION_MAPKEY);
struct expression *map = blk->variable.expression;
test_expected(map->indexkey.left, left);
test_expected(map->indexkey.key, key);
parser_destroy(parser);
template_destroy(tmpl);
}
static inline void
test_index_variables(void)
{
char *input = "{{ arr[1 + 2] }}";
struct value left = VIDENT("arr");
struct value ileft = VINT(1);
struct slice iop = slice_whole("+");
struct value iright = VINT(2);
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
asserteq(blk->variable.expression->type, EXPRESSION_INDEX);
struct expression *map = blk->variable.expression;
test_expected(map->indexkey.left, left);
test_infix(&map->indexkey.key->infix, ileft, &iop, iright);
parser_destroy(parser);
template_destroy(tmpl);
}
static inline void
test_operator_precedence(void)
{
struct {
char *input;
char *expected;
} tests[] = {
{
"{{ -a * b }}",
"{{ ((-a) * b) }}",
},
{
"{{ 1 + 2 * 3 }}",
"{{ (1 + (2 * 3)) }}",
},
{
"{{ !-a }}",
"{{ (!(-a)) }}",
},
{
"{{ a + b + c }}",
"{{ ((a + b) + c) }}",
},
{
"{{ a + b - c }}",
"{{ ((a + b) - c) }}",
},
{
"{{ a * b * c }}",
"{{ ((a * b) * c) }}",
},
{
"{{ a * b / c }}",
"{{ ((a * b) / c) }}",
},
{
"{{ a + b / c }}",
"{{ (a + (b / c)) }}",
},
{
"{{ a + b * c + d / e - f }}",
"{{ (((a + (b * c)) + (d / e)) - f) }}",
},
{
"{{ 5 > 4 == 3 < 4 }}",
"{{ ((5 > 4) == (3 < 4)) }}",
},
{
"{{ 5 < 4 != 3 > 4 }}",
"{{ ((5 < 4) != (3 > 4)) }}",
},
{
"{{ 3 + 4 * 5 == 3 * 1 + 4 * 5 }}",
"{{ ((3 + (4 * 5)) == ((3 * 1) + (4 * 5))) }}",
},
{
"{{ (5 + 5) * 2 }}",
"{{ ((5 + 5) * 2) }}",
},
{
"{{ 2 / (5 + 5) }}",
"{{ (2 / (5 + 5)) }}",
},
{
"{{ (5 + 5) * 2 * (5 + 5) }}",
"{{ (((5 + 5) * 2) * (5 + 5)) }}",
},
{
"{{ -(5 + 5) }}",
"{{ (-(5 + 5)) }}",
},
{
"{{ foo[0] + bar[1 + 2] }}",
"{{ (foo[0] + bar[(1 + 2)]) }}",
},
{
"{{ foo.bar + foo.baz }}",
"{{ (foo.bar + foo.baz) }}",
},
{
"{{ foo.bar + bar[0].baz * foo.bar.baz }}",
"{{ (foo.bar + (bar[0].baz * foo.bar.baz)) }}",
},
{0},
};
for (size_t i = 0; tests[i].input != NULL; i++) {
struct parser *parser = parser_new(strdup("test"), tests[i].input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_VARIABLE);
sds output = block_string(blk, sdsempty());
asserteq(strcmp(output, tests[i].expected), 0);
sdsfree(output);
parser_destroy(parser);
template_destroy(tmpl);
}
}
static inline void
test_loop_tag(void)
{
char *input = "{% for v in seq %}"
"{% break %}"
"{% endfor %}"
"{{ foo }}";
struct slice item = slice_whole("v");
struct slice seq = slice_whole("seq");
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 2);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_TAG);
asserteq(blk->tag.type, TAG_FOR);
asserteq(slice_cmp(&blk->tag.loop.item.token.literal, &item), 0);
struct expression *seqexpr = blk->tag.loop.seq;
asserteq(seqexpr->type, EXPRESSION_IDENT);
asserteq(slice_cmp(&seqexpr->ident.token.literal, &seq), 0);
asserteq(blk->tag.loop.subblocks->len, 2);
struct block *sub1 = blk->tag.loop.subblocks->values[0];
struct block *sub2 = blk->tag.loop.subblocks->values[1];
asserteq(sub1->type, BLOCK_TAG);
asserteq(sub2->type, BLOCK_TAG);
asserteq(sub1->tag.type, TAG_BREAK);
asserteq(sub2->tag.type, TAG_CLOSE);
asserteq(sub2->tag.token.type, TOKEN_ENDFOR);
struct block *extra_blk = tmpl->blocks->values[1];
asserteq(extra_blk->type, BLOCK_VARIABLE);
parser_destroy(parser);
template_destroy(tmpl);
}
static inline void
test_cond_tag(void)
{
char *input = "{% if false %}"
"{{ foo }}"
"{% elif 1 > 2 %}"
"{{ bar }}"
"{% else %}"
"baz"
"{% endif %}";
struct value ifexp = VIDENT("foo");
struct value elifl = VINT(1);
struct slice elifop = slice_whole(">");
struct value elifr = VINT(2);
struct value elifexp = VIDENT("bar");
struct slice elsecont = slice_whole("baz");
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_TAG);
asserteq(blk->tag.type, TAG_IF);
struct branch *b1 = blk->tag.cond.root;
assertneq(b1->condition, NULL);
asserteq(b1->condition->type, EXPRESSION_BOOL);
asserteq(b1->condition->token.type, TOKEN_FALSE);
asserteq(b1->subblocks->len, 1);
struct block *b1sub1 = b1->subblocks->values[0];
asserteq(b1sub1->type, BLOCK_VARIABLE);
test_expected(b1sub1->variable.expression, ifexp);
struct branch *b2 = b1->next;
assertneq(b2->condition, NULL);
test_infix(&b2->condition->infix, elifl, &elifop, elifr);
asserteq(b2->subblocks->len, 1);
struct block *b2sub1 = b2->subblocks->values[0];
asserteq(b2sub1->type, BLOCK_VARIABLE);
test_expected(b2sub1->variable.expression, elifexp);
struct branch *b3 = b2->next;
asserteq(b3->condition, NULL);
asserteq(b3->subblocks->len, 2);
struct block *b3sub1 = b3->subblocks->values[0];
asserteq(b3sub1->type, BLOCK_CONTENT);
asserteq(slice_cmp(&b3sub1->content.token.literal, &elsecont), 0);
struct block *b3sub2 = b3->subblocks->values[1];
asserteq(b3sub2->tag.type, TAG_CLOSE);
asserteq(b3sub2->tag.token.type, TOKEN_ENDIF);
parser_destroy(parser);
template_destroy(tmpl);
}
static inline void
test_parent_tag(void)
{
char *input = "{% extends \"base.html\" %}";
struct slice name = slice_whole("base.html");
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_TAG);
asserteq(blk->tag.type, TAG_EXTENDS);
asserteq(slice_cmp(&blk->tag.parent.name->value, &name), 0);
parser_destroy(parser);
template_destroy(tmpl);
}
static inline void
test_tblock_tag(void)
{
char *input = "{% block cock %}"
"{% endblock %}";
struct slice name = slice_whole("cock");
struct parser *parser = parser_new(strdup("test"), input);
struct template *tmpl = parser_parse_template(parser);
check_parser_errors(parser);
assertneq(tmpl, NULL);
asserteq(tmpl->blocks->len, 1);
struct block *blk = tmpl->blocks->values[0];
asserteq(blk->type, BLOCK_TAG);
asserteq(blk->tag.type, TAG_BLOCK);
asserteq(slice_cmp(&blk->tag.tblock.name.token.literal, &name), 0);
asserteq(blk->tag.tblock.subblocks->len, 1);
struct block *sub1 = blk->tag.tblock.subblocks->values[0];
asserteq(sub1->type, BLOCK_TAG);
asserteq(sub1->tag.type, TAG_CLOSE);
asserteq(sub1->tag.token.type, TOKEN_ENDBLOCK);
blk = hmap_gets(tmpl->tblocks, &name);
assertneq(blk, NULL);
asserteq(blk->type, BLOCK_TAG);
asserteq(blk->tag.type, TAG_BLOCK);
asserteq(slice_cmp(&blk->tag.tblock.name.token.literal, &name), 0);
parser_destroy(parser);
template_destroy(tmpl);
}
static void
init(void)
{
parser_init();
}
static void
cleanup(void)
{
parser_deinit();
}
int
main(void)
{
init();
INIT_TESTS();
RUN_TEST(test_literal_variables);
RUN_TEST(test_prefix_variables);
RUN_TEST(test_infix_variables);
RUN_TEST(test_map_variables);
RUN_TEST(test_index_variables);
RUN_TEST(test_operator_precedence);
RUN_TEST(test_loop_tag);
RUN_TEST(test_cond_tag);
RUN_TEST(test_parent_tag);
RUN_TEST(test_tblock_tag);
cleanup();
}
|