src/stencil_compile.c

filter: Statementstmt 90.1% 961 / 1066 Branchbran 77.4% 637 / 822 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.6% 1598 / 1888 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_render.c ↑ Summary src/stencil_filters.c »
1 #include "stencil.h"
2  
3 #include <stdarg.h>
4  
5 /* ====================================================================
6 * Compiler
7 *
8 * During compile each region (bytecode, byte pool, tables) grows in
9 * its own bump arena so offsets never move; finalise packs them all
10 * into the program's single arena and frees the temporaries. All
11 * compile-time allocation goes through stencil_arena / stencil_intern
12 * - no stray mallocs.
13 * ==================================================================== */
14  
15 #define BLK_IF 1
16 #define BLK_UNLESS 2
17 #define BLK_FOR 3
18  
19 #define STENCIL_MAX_DEPTH 64
20 #define STENCIL_MAX_SEGS 8
21 #define STENCIL_PATCH_END 0xFFFFFFFFu
22  
23 typedef struct stencil_blk {
24 uint8_t kind;
25 uint8_t has_else;
26 uint32_t line, col; /* opener position for errors */
27 uint32_t cond_patch; /* pending TEST_JF/JT operand offset */
28 uint32_t jump_chain; /* end-jump patch sites, chained through
29 the operand slots themselves */
30 uint32_t body_start; /* FOR: back-jump target */
31 uint32_t for_end_patch; /* FOR: end_target operand offset */
32 int32_t binds_at_entry;
33 } stencil_blk;
34  
35 typedef struct stencil_compiler {
36 const char *src, *p, *end;
37 const char *name; /* diagnostic template name */
38 uint32_t line;
39 const char *line_start;
40  
41 stencil_arena code, pool, names, paths, segs, incs, filts, lines;
42 stencil_arena pend; /* pending literal run */
43 stencil_intern lit_intern; /* long literals + strings, into pool */
44  
45 uint32_t n_names, n_paths, n_segs, n_incs, n_filts, n_lines;
46  
47 stencil_blk blk[STENCIL_MAX_DEPTH];
48 uint32_t n_blk;
49  
50 int32_t cur_stack, max_stack;
51 int32_t cur_frames, max_frames;
52 int32_t cur_binds, max_binds;
53  
54 uint32_t flags;
55 uint32_t last_line_rec;
56  
57 #ifdef PERL_IMPLICIT_CONTEXT
58 PerlInterpreter *perl; /* for PERL_HASH's seed access */
59 #endif
60  
61 /* first error wins */
62 int failed;
63 char errbuf[192];
64 uint32_t err_line, err_col;
65 } stencil_compiler;
66  
67 /* Reserved words: first-position statement keywords plus 'in', which
68 * cannot be a for/set binding name (is_keyword guards those). */
69 static const char *const kw_list[] = {
70 "if", "unless", "elsif", "else", "end", "for", "set",
71 "include", "raw", "content", "in", NULL
72 };
73  
74 /* ---- error helpers ------------------------------------------------- */
75  
76 457970 static uint32_t ccol(const stencil_compiler *c, const char *at)
77 {
78 457970 return (uint32_t)(at - c->line_start) + 1;
79 }
80  
81 118 static int cerror_at(stencil_compiler *c, uint32_t line, uint32_t col,
82 const char *fmt, ...)
83 {
84 va_list ap;
85 118 ▶ if (c->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (c->failed) 0 118 118
86 0 return 0;
87 118 c->failed = 1;
88 118 c->err_line = line;
89 118 c->err_col = col;
90 118 va_start(ap, fmt);
91 118 vsnprintf(c->errbuf, sizeof c->errbuf, fmt, ap);
92 118 va_end(ap);
93 118 return 0;
94 }
95  
96 197 static int cerror(stencil_compiler *c, const char *at, const char *fmt, ...)
97 {
98 va_list ap;
99 197 ▶ if (c->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (c->failed) 0 197 197
100 0 return 0;
101 197 c->failed = 1;
102 197 c->err_line = c->line;
103 197 c->err_col = ccol(c, at);
104 197 va_start(ap, fmt);
105 197 vsnprintf(c->errbuf, sizeof c->errbuf, fmt, ap);
106 197 va_end(ap);
107 197 return 0;
108 }
109  
110 0 static int coom(stencil_compiler *c)
111 {
112 0 return cerror_at(c, c->line, 1, "out of memory");
113 }
114  
115 /* ---- emit helpers --------------------------------------------------- */
116  
117 2611391 static int emit_bytes(stencil_compiler *c, const void *p, size_t n)
118 {
119 2611391 uint32_t off = stencil_arena_alloc(&c->code, n, 1);
120 2611391 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 2611391 2611391
121 0 return coom(c);
122 2611391 memcpy(c->code.base + off, p, n);
123 2611391 return 1;
124 }
125  
126 1718062 static int emit_u8(stencil_compiler *c, uint8_t v)
127 {
128 1718062 return emit_bytes(c, &v, 1);
129 }
130  
131 481048 static int emit_u32(stencil_compiler *c, uint32_t v)
132 {
133 481048 return emit_bytes(c, &v, 4);
134 }
135  
136 22678 static void patch_u32(stencil_compiler *c, uint32_t at, uint32_t v)
137 {
138 22678 memcpy(c->code.base + at, &v, 4);
139 22678 }
140  
141 120 static uint32_t read_code_u32(stencil_compiler *c, uint32_t at)
142 {
143 uint32_t v;
144 120 memcpy(&v, c->code.base + at, 4);
145 120 return v;
146 }
147  
148 103230 static uint32_t code_pos(const stencil_compiler *c)
149 {
150 103230 return (uint32_t)c->code.used;
151 }
152  
153 /* Record the source line for the op about to be emitted (delta table,
154 * appended only when the line changes). */
155 1305878 static int record_line(stencil_compiler *c)
156 {
157 stencil_cline *e;
158 uint32_t off;
159 1305878 ▶ if (c->line == c->last_line_rec)
Branch: 1 branch
BranchTrueFalseTotal
if (c->line == c->last_line_rec) 1293432 12446 1305878
160 1293432 return 1;
161 12446 off = stencil_arena_alloc(&c->lines, sizeof(stencil_cline),
162 sizeof(uint32_t));
163 12446 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 12446 12446
164 0 return coom(c);
165 12446 e = (stencil_cline *)(c->lines.base + off);
166 12446 e->off = code_pos(c);
167 12446 e->line = c->line;
168 12446 c->n_lines++;
169 12446 c->last_line_rec = c->line;
170 12446 return 1;
171 }
172  
173 1305878 static int emit_op(stencil_compiler *c, uint8_t op, int32_t stack_delta)
174 {
175 1305878 ▶ if (!record_line(c) || !emit_u8(c, op))
Branch: 2 branches
BranchTrueFalseTotal
if (!record_line(c) || !emit_u8(c, op)) 1305878 0 1305878
if (!record_line(c) || !emit_u8(c, op)) 0 1305878 1305878
176 0 return 0;
177 1305878 c->cur_stack += stack_delta;
178 1305878 ▶ if (c->cur_stack > c->max_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_stack > c->max_stack) 11810 1294068 1305878
179 11810 c->max_stack = c->cur_stack;
180 1305878 return 1;
181 }
182  
183 /* Emit a jump-family op whose operand joins a patch chain threaded
184 * through the operand slots themselves; returns via *chain. */
185 122 static int emit_jump_chained(stencil_compiler *c, uint8_t op,
186 int32_t stack_delta, uint32_t *chain)
187 {
188 uint32_t site;
189 122 ▶ if (!emit_op(c, op, stack_delta))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, op, stack_delta)) 0 122 122
190 0 return 0;
191 122 site = code_pos(c);
192 122 ▶ if (!emit_u32(c, *chain))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_u32(c, *chain)) 0 122 122
193 0 return 0;
194 122 *chain = site;
195 122 return 1;
196 }
197  
198 34195 static void patch_chain(stencil_compiler *c, uint32_t chain,
199 uint32_t target)
200 {
201 34315 ▶ while (chain != STENCIL_PATCH_END) {
Branch: 1 branch
BranchTrueFalseTotal
while (chain != STENCIL_PATCH_END) { 120 34195 34315
202 120 uint32_t next = read_code_u32(c, chain);
203 120 patch_u32(c, chain, target);
204 120 chain = next;
205 }
206 34195 }
207  
208 /* ---- pending-literal accumulation and flush ------------------------- */
209  
210 469919 static int lit_pend(stencil_compiler *c, const char *p, size_t n)
211 {
212 uint32_t off;
213 469919 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 57392 412527 469919
214 57392 return 1;
215 412527 off = stencil_arena_alloc(&c->pend, n, 1);
216 412527 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 412527 412527
217 0 return coom(c);
218 412527 memcpy(c->pend.base + off, p, n);
219 412527 return 1;
220 }
221  
222 469817 static int lit_flush(stencil_compiler *c)
223 {
224 469817 size_t n = c->pend.used;
225 469817 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 57347 412470 469817
226 57347 return 1;
227 412470 ▶ if (n <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 31) { 412184 286 412470
228 824368 if (!emit_op(c, SOP_LITERAL_SHORT, 0) ||
229 824368 ▶ !emit_u8(c, (uint8_t)n) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_u8(c, (uint8_t)n) || 0 412184 412184
230 412184 !emit_bytes(c, c->pend.base, n))
231 0 return 0;
232 } else {
233 286 uint32_t off = stencil_intern_get(&c->lit_intern, &c->pool,
234 286 c->pend.base, (uint32_t)n);
235 286 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 286 286
236 0 return coom(c);
237 572 if (!emit_op(c, SOP_LITERAL_LONG, 0) ||
238 572 ▶ !emit_u32(c, off) || !emit_u32(c, (uint32_t)n))
Branch: 1 branch
BranchTrueFalseTotal
!emit_u32(c, off) || !emit_u32(c, (uint32_t)n)) 0 286 286
239 0 return 0;
240 }
241 412470 c->pend.used = 0;
242 412470 return 1;
243 }
244  
245 /* ---- source advance with line tracking ------------------------------- */
246  
247 469912 static void advance_lines(stencil_compiler *c, const char *from,
248 const char *to)
249 {
250 469912 const char *nl = from;
251 for (;;) {
252 471618 nl = (const char *)memchr(nl, '\n', (size_t)(to - nl));
253 470765 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 469912 853 470765
254 469912 break;
255 853 c->line++;
256 853 nl++;
257 853 c->line_start = nl;
258 }
259 469912 }
260  
261 /* ---- token helpers --------------------------------------------------- */
262  
263 #define IS_WORD_START(ch) (isALPHA(ch) || (ch) == '_')
264 #define IS_WORD(ch) (isALNUM(ch) || (ch) == '_')
265  
266 1419730 static void skip_ws(stencil_compiler *c)
267 {
268 2381127 ▶ while (c->p < c->end && isSPACE(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isSPACE(*c->p)) { 2381115 12 2381127
while (c->p < c->end && isSPACE(*c->p)) { 961397 1419718 2381115
269 961397 ▶ if (*c->p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '\n') { 9 961388 961397
270 9 c->line++;
271 9 c->line_start = c->p + 1;
272 }
273 961397 c->p++;
274 }
275 1419730 }
276  
277 1373015 static size_t word_len(const char *p, const char *end)
278 {
279 1373015 const char *q = p;
280 1373015 ▶ if (q >= end || !IS_WORD_START(*q))
Branch: 3 branches
BranchTrueFalseTotal
if (q >= end || !IS_WORD_START(*q)) 1373010 5 1373015
if (q >= end || !IS_WORD_START(*q)) 34568 1338442 1373010
if (q >= end || !IS_WORD_START(*q)) 34568 0 34568
281 34573 return 0;
282 1338442 q++;
283 5079953 ▶ while (q < end && IS_WORD(*q))
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && IS_WORD(*q)) 5079950 3 5079953
while (q < end && IS_WORD(*q)) 3741511 1338439 5079950
while (q < end && IS_WORD(*q)) 0 1338439 1338439
284 3741511 q++;
285 1338442 return (size_t)(q - p);
286 }
287  
288 4487168 static int word_is(const char *p, size_t n, const char *kw)
289 {
290 4487168 ▶ return strlen(kw) == n && memcmp(p, kw, n) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return strlen(kw) == n && memcmp(p, kw, n) == 0; 469380 4017788 4487168
return strlen(kw) == n && memcmp(p, kw, n) == 0; 57213 412167 469380
291 }
292  
293 11469 static int is_keyword(const char *p, size_t n)
294 {
295 int i;
296 137620 ▶ for (i = 0; kw_list[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; kw_list[i]; i++) 126154 11466 137620
297 126154 ▶ if (word_is(p, n, kw_list[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, kw_list[i])) 3 126151 126154
298 3 return 1;
299 11466 return 0;
300 }
301  
302 457454 static int expect_close(stencil_compiler *c)
303 {
304 457454 skip_ws(c);
305 457454 ▶ if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') {
Branch: 3 branches
BranchTrueFalseTotal
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 457448 6 457454
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 457427 21 457448
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 457406 21 457427
306 457406 c->p += 2;
307 457406 return 1;
308 }
309 48 return cerror(c, c->p, "expected '%%}'");
310 }
311  
312 /* ---- name / path interning ------------------------------------------ */
313  
314 846441 static uint32_t name_intern(stencil_compiler *c, const char *p, uint32_t n)
315 {
316 846441 stencil_cname *tab = (stencil_cname *)c->names.base;
317 uint32_t i, off, hash, noff;
318 stencil_cname *e;
319 #ifdef PERL_IMPLICIT_CONTEXT
320 dTHXa(c->perl);
321 #endif
322 1396452 ▶ for (i = 0; i < c->n_names; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_names; i++) 1339658 56794 1396452
323 1339658 ▶ if (tab[i].len == n
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].len == n 1213555 126103 1339658
324 1213555 ▶ && memcmp(c->pool.base + tab[i].off, p, n) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(c->pool.base + tab[i].off, p, n) == 0) 789647 423908 1213555
325 789647 return i;
326 56794 off = stencil_arena_alloc(&c->pool, n, 1);
327 56794 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 56794 56794
328 0 return STENCIL_ARENA_NULL;
329 56794 memcpy(c->pool.base + off, p, n);
330 56794 ▶ PERL_HASH(hash, p, n);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, p, n); 56794 0 56794
331 56794 noff = stencil_arena_alloc(&c->names, sizeof(stencil_cname),
332 sizeof(uint32_t));
333 56794 ▶ if (noff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (noff == STENCIL_ARENA_NULL) 0 56794 56794
334 0 return STENCIL_ARENA_NULL;
335 56794 e = (stencil_cname *)(c->names.base + noff);
336 56794 e->off = off;
337 56794 e->len = n;
338 56794 e->hash = hash;
339 56794 return c->n_names++;
340 }
341  
342 /* Parse a dotted/indexed path at c->p; returns path_id or
343 * STENCIL_ARENA_NULL after setting the error. */
344 434685 static uint32_t path_parse(stencil_compiler *c)
345 {
346 434685 const char *start = c->p;
347 stencil_seg segs[STENCIL_MAX_SEGS];
348 434685 uint32_t n_segs = 0;
349 uint32_t i, seg_base, full_off, poff;
350 size_t full_len;
351 stencil_cpath *tab, *e;
352  
353 for (;;) {
354 835020 ▶ if (n_segs >= STENCIL_MAX_SEGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_segs >= STENCIL_MAX_SEGS) { 1 835019 835020
355 1 cerror(c, c->p, "path has more than %d segments",
356 STENCIL_MAX_SEGS);
357 1 return STENCIL_ARENA_NULL;
358 }
359 835019 ▶ if (c->p < c->end && *c->p == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') { 835018 1 835019
if (c->p < c->end && *c->p == '[') { 15 835003 835018
360 15 const char *q = c->p + 1;
361 15 SSize_t ix = 0;
362 15 ▶ if (n_segs == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_segs == 0) { 0 15 15
363 0 cerror(c, c->p, "path cannot start with an index");
364 0 return STENCIL_ARENA_NULL;
365 }
366 15 ▶ if (q >= c->end || !isDIGIT(*q)) {
Branch: 2 branches
BranchTrueFalseTotal
if (q >= c->end || !isDIGIT(*q)) { 15 0 15
if (q >= c->end || !isDIGIT(*q)) { 1 14 15
367 1 cerror(c, c->p, "expected digits in [index]");
368 1 return STENCIL_ARENA_NULL;
369 }
370 29 ▶ while (q < c->end && isDIGIT(*q))
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && isDIGIT(*q)) 29 0 29
while (q < c->end && isDIGIT(*q)) 15 14 29
371 15 ix = ix * 10 + (*q++ - '0');
372 14 ▶ if (q >= c->end || *q != ']') {
Branch: 2 branches
BranchTrueFalseTotal
if (q >= c->end || *q != ']') { 14 0 14
if (q >= c->end || *q != ']') { 1 13 14
373 1 cerror(c, q, "expected ']'");
374 1 return STENCIL_ARENA_NULL;
375 }
376 13 segs[n_segs].name_id = 0;
377 13 segs[n_segs].is_index = 1;
378 13 segs[n_segs].index = ix;
379 13 n_segs++;
380 13 c->p = q + 1;
381 } else {
382 835004 size_t wl = word_len(c->p, c->end);
383 uint32_t id;
384 835004 ▶ if (!wl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) { 19 834985 835004
385 19 ▶ cerror(c, c->p, n_segs ? "expected name after '.'"
Branch: 1 branch
BranchTrueFalseTotal
cerror(c, c->p, n_segs ? "expected name after '.'" 2 17 19
386 : "expected a name");
387 19 return STENCIL_ARENA_NULL;
388 }
389 834985 id = name_intern(c, c->p, (uint32_t)wl);
390 834985 ▶ if (id == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (id == STENCIL_ARENA_NULL) { 0 834985 834985
391 0 coom(c);
392 0 return STENCIL_ARENA_NULL;
393 }
394 834985 segs[n_segs].name_id = id;
395 834985 segs[n_segs].is_index = 0;
396 834985 segs[n_segs].index = 0;
397 834985 n_segs++;
398 834985 c->p += wl;
399 }
400 834998 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 834996 2 834998
if (c->p < c->end && *c->p == '.') { 400320 434676 834996
401 400320 c->p++;
402 400320 continue;
403 }
404 434678 ▶ if (c->p < c->end && *c->p == '[')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') 434676 2 434678
if (c->p < c->end && *c->p == '[') 15 434661 434676
405 15 continue;
406 434663 break;
407 }
408  
409 /* dedup by the normalised source slice */
410 434663 full_len = (size_t)(c->p - start);
411 434663 tab = (stencil_cpath *)c->paths.base;
412 503734 ▶ for (i = 0; i < c->n_paths; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_paths; i++) 458228 45506 503734
413 458228 ▶ if (tab[i].full_len == full_len
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].full_len == full_len 400729 57499 458228
414 400729 ▶ && memcmp(c->pool.base + tab[i].full_off, start, full_len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(c->pool.base + tab[i].full_off, start, full_len) == 0) 389157 11572 400729
415 389157 return i;
416  
417 45506 full_off = stencil_intern_get(&c->lit_intern, &c->pool, start,
418 (uint32_t)full_len);
419 45506 ▶ if (full_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (full_off == STENCIL_ARENA_NULL) { 0 45506 45506
420 0 coom(c);
421 0 return STENCIL_ARENA_NULL;
422 }
423 45506 seg_base = c->n_segs;
424 102326 ▶ for (i = 0; i < n_segs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_segs; i++) { 56820 45506 102326
425 56820 uint32_t soff = stencil_arena_alloc(&c->segs, sizeof(stencil_seg),
426 sizeof(SSize_t));
427 56820 ▶ if (soff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (soff == STENCIL_ARENA_NULL) { 0 56820 56820
428 0 coom(c);
429 0 return STENCIL_ARENA_NULL;
430 }
431 56820 memcpy(c->segs.base + soff, &segs[i], sizeof(stencil_seg));
432 56820 c->n_segs++;
433 }
434 45506 poff = stencil_arena_alloc(&c->paths, sizeof(stencil_cpath),
435 sizeof(uint32_t));
436 45506 ▶ if (poff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (poff == STENCIL_ARENA_NULL) { 0 45506 45506
437 0 coom(c);
438 0 return STENCIL_ARENA_NULL;
439 }
440 45506 e = (stencil_cpath *)(c->paths.base + poff);
441 {
442 45506 const stencil_cname *names = (const stencil_cname *)c->names.base;
443 91012 e->loop_rooted = !segs[0].is_index
444 45506 ▶ && names[segs[0].name_id].len == 4
Branch: 1 branch
BranchTrueFalseTotal
&& names[segs[0].name_id].len == 4 11231 34275 45506
445 91012 ▶ && memcmp(c->pool.base + names[segs[0].name_id].off,
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->pool.base + names[segs[0].name_id].off, 45506 0 45506
&& memcmp(c->pool.base + names[segs[0].name_id].off, 132 11099 11231
446 "loop", 4) == 0;
447 }
448 45506 e->n_segs = (uint16_t)n_segs;
449 45506 e->seg_idx = seg_base;
450 45506 e->full_off = full_off;
451 45506 e->full_len = (uint32_t)full_len;
452 45506 return c->n_paths++;
453 }
454  
455 /* parse a path and emit its push */
456 434685 static int parse_path_push(stencil_compiler *c)
457 {
458 434685 uint32_t pid = path_parse(c);
459 434685 ▶ if (pid == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pid == STENCIL_ARENA_NULL) 22 434663 434685
460 22 return 0;
461 434663 ▶ return emit_op(c, SOP_PUSH_PATH, +1) && emit_u32(c, pid);
Branch: 2 branches
BranchTrueFalseTotal
return emit_op(c, SOP_PUSH_PATH, +1) && emit_u32(c, pid); 434663 0 434663
return emit_op(c, SOP_PUSH_PATH, +1) && emit_u32(c, pid); 434663 0 434663
462 }
463  
464 /* ====================================================================
465 * Expression parser
466 * precedence: or < and < not < comparison < primary
467 * ==================================================================== */
468  
469 static int parse_expr(stencil_compiler *c);
470  
471 11569 static int parse_primary(stencil_compiler *c)
472 {
473 11569 skip_ws(c);
474 11569 ▶ if (c->p >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p >= c->end) 1 11568 11569
475 1 return cerror(c, c->p, "expected an expression");
476  
477 11568 ▶ if (*c->p == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '(') { 4 11564 11568
478 4 c->p++;
479 4 ▶ if (!parse_expr(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_expr(c)) 0 4 4
480 0 return 0;
481 4 skip_ws(c);
482 4 ▶ if (c->p >= c->end || *c->p != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ')') 4 0 4
if (c->p >= c->end || *c->p != ')') 1 3 4
483 1 return cerror(c, c->p, "expected ')'");
484 3 c->p++;
485 3 return 1;
486 }
487  
488 11564 ▶ if (*c->p == '\'' || *c->p == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (*c->p == '\'' || *c->p == '"') { 11548 16 11564
if (*c->p == '\'' || *c->p == '"') { 13 11535 11548
489 29 char quote = *c->p;
490 29 const char *q = c->p + 1;
491 29 int esc = 0;
492 uint32_t off, len;
493 113 ▶ while (q < c->end && *q != quote) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && *q != quote) { 112 1 113
while (q < c->end && *q != quote) { 84 28 112
494 84 ▶ if (*q == '\\' && q + 1 < c->end
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\\' && q + 1 < c->end 2 82 84
if (*q == '\\' && q + 1 < c->end 2 0 2
495 2 ▶ && (q[1] == quote || q[1] == '\\')) {
Branch: 2 branches
BranchTrueFalseTotal
&& (q[1] == quote || q[1] == '\\')) { 0 2 2
&& (q[1] == quote || q[1] == '\\')) { 0 0 0
496 2 esc = 1;
497 2 q += 2;
498 } else {
499 82 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 0 82 82
500 0 c->line++;
501 0 c->line_start = q + 1;
502 }
503 82 q++;
504 }
505 }
506 29 ▶ if (q >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= c->end) 1 28 29
507 1 return cerror(c, c->p, "unterminated string literal");
508 28 ▶ if (!esc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!esc) { 26 2 28
509 26 len = (uint32_t)(q - (c->p + 1));
510 26 off = len ? stencil_intern_get(&c->lit_intern, &c->pool,
511 26 c->p + 1, len)
512 26 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 26 0 26
513 } else {
514 /* copy with unescaping straight into the pool (no dedup) */
515 2 const char *r = c->p + 1;
516 char *w;
517 2 off = stencil_arena_alloc(&c->pool, (size_t)(q - r), 1);
518 2 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 2 2
519 0 return coom(c);
520 2 w = c->pool.base + off;
521 9 ▶ while (r < q) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < q) { 7 2 9
522 7 ▶ if (*r == '\\' && (r[1] == quote || r[1] == '\\'))
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 2 5 7
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 2 2
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
523 2 r++;
524 7 *w++ = *r++;
525 }
526 2 len = (uint32_t)(w - (c->pool.base + off));
527 2 c->pool.used = (size_t)(w - c->pool.base);
528 }
529 28 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 28 28
530 0 return coom(c);
531 28 c->p = q + 1;
532 28 return emit_op(c, SOP_PUSH_LIT_STR, +1)
533 28 ▶ && emit_u32(c, off) && emit_u32(c, len);
Branch: 3 branches
BranchTrueFalseTotal
&& emit_u32(c, off) && emit_u32(c, len); 28 0 28
&& emit_u32(c, off) && emit_u32(c, len); 28 0 28
&& emit_u32(c, off) && emit_u32(c, len); 28 0 28
534 }
535  
536 11535 ▶ if (isDIGIT(*c->p)
Branch: 1 branch
BranchTrueFalseTotal
if (isDIGIT(*c->p) 11440 95 11535
537 11440 ▶ || (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) {
Branch: 3 branches
BranchTrueFalseTotal
|| (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) { 2 11438 11440
|| (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) { 2 0 2
|| (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) { 2 0 2
538 97 double val = 0.0, frac = 0.1;
539 97 int neg = 0;
540 97 ▶ if (*c->p == '-') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '-') { 2 95 97
541 2 neg = 1;
542 2 c->p++;
543 }
544 197 ▶ while (c->p < c->end && isDIGIT(*c->p))
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isDIGIT(*c->p)) 197 0 197
while (c->p < c->end && isDIGIT(*c->p)) 100 97 197
545 100 val = val * 10.0 + (*c->p++ - '0');
546 97 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 97 0 97
if (c->p < c->end && *c->p == '.') { 7 90 97
547 7 ▶ if (c->p + 1 >= c->end || !isDIGIT(c->p[1]))
Branch: 2 branches
BranchTrueFalseTotal
if (c->p + 1 >= c->end || !isDIGIT(c->p[1])) 7 0 7
if (c->p + 1 >= c->end || !isDIGIT(c->p[1])) 0 7 7
548 0 return cerror(c, c->p, "expected digits after '.'");
549 7 c->p++;
550 14 ▶ while (c->p < c->end && isDIGIT(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isDIGIT(*c->p)) { 14 0 14
while (c->p < c->end && isDIGIT(*c->p)) { 7 7 14
551 7 val += (*c->p++ - '0') * frac;
552 7 frac *= 0.1;
553 }
554 }
555 97 ▶ if (neg)
Branch: 1 branch
BranchTrueFalseTotal
if (neg) 2 95 97
556 2 val = -val;
557 97 return emit_op(c, SOP_PUSH_LIT_NUM, +1)
558 97 ▶ && emit_bytes(c, &val, sizeof val);
Branch: 2 branches
BranchTrueFalseTotal
&& emit_bytes(c, &val, sizeof val); 97 0 97
&& emit_bytes(c, &val, sizeof val); 97 0 97
559 }
560  
561 {
562 11438 size_t wl = word_len(c->p, c->end);
563 11438 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 20 11418 11438
564 20 return cerror(c, c->p, "expected an expression");
565 11418 ▶ if (word_is(c->p, wl, "undef")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "undef")) { 5 11413 11418
566 5 c->p += wl;
567 5 return emit_op(c, SOP_PUSH_UNDEF, +1);
568 }
569 11413 ▶ if (word_is(c->p, wl, "defined")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "defined")) { 9 11404 11413
570 9 const char *kw = c->p;
571 9 c->p += wl;
572 9 skip_ws(c);
573 9 ▶ if (c->p >= c->end || *c->p != '(')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '(') 9 0 9
if (c->p >= c->end || *c->p != '(') 1 8 9
574 1 return cerror(c, kw, "defined needs (path)");
575 8 c->p++;
576 8 skip_ws(c);
577 8 ▶ if (!parse_path_push(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_path_push(c)) 0 8 8
578 0 return 0;
579 8 skip_ws(c);
580 8 ▶ if (c->p >= c->end || *c->p != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ')') 8 0 8
if (c->p >= c->end || *c->p != ')') 1 7 8
581 1 return cerror(c, c->p, "expected ')'");
582 7 c->p++;
583 7 return emit_op(c, SOP_DEFINED, 0);
584 }
585 11404 return parse_path_push(c);
586 }
587 }
588  
589 /* returns the comparison opcode at c->p (consuming it) or 0 */
590 11484 static uint8_t scan_cmp_op(stencil_compiler *c)
591 {
592 11484 const char *p = c->p;
593 11484 size_t left = (size_t)(c->end - p);
594 size_t wl;
595 11484 ▶ if (left >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 2) { 11484 0 11484
596 11484 ▶ if (p[0] == '=' && p[1] == '=') { c->p += 2; return SOP_EQ_NUM; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '=' && p[1] == '=') { c->p += 2; return SOP_EQ_NUM; } 21 11463 11484
if (p[0] == '=' && p[1] == '=') { c->p += 2; return SOP_EQ_NUM; } 21 0 21
597 11463 ▶ if (p[0] == '!' && p[1] == '=') { c->p += 2; return SOP_NE_NUM; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '!' && p[1] == '=') { c->p += 2; return SOP_NE_NUM; } 2 11461 11463
if (p[0] == '!' && p[1] == '=') { c->p += 2; return SOP_NE_NUM; } 2 0 2
598 11461 ▶ if (p[0] == '<' && p[1] == '=') { c->p += 2; return SOP_LE_NUM; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '<' && p[1] == '=') { c->p += 2; return SOP_LE_NUM; } 5 11456 11461
if (p[0] == '<' && p[1] == '=') { c->p += 2; return SOP_LE_NUM; } 2 3 5
599 11459 ▶ if (p[0] == '>' && p[1] == '=') { c->p += 2; return SOP_GE_NUM; }
Branch: 2 branches
BranchTrueFalseTotal
if (p[0] == '>' && p[1] == '=') { c->p += 2; return SOP_GE_NUM; } 7 11452 11459
if (p[0] == '>' && p[1] == '=') { c->p += 2; return SOP_GE_NUM; } 4 3 7
600 }
601 11455 ▶ if (left >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 1) { 11455 0 11455
602 11455 ▶ if (p[0] == '<') { c->p += 1; return SOP_LT_NUM; }
Branch: 1 branch
BranchTrueFalseTotal
if (p[0] == '<') { c->p += 1; return SOP_LT_NUM; } 3 11452 11455
603 11452 ▶ if (p[0] == '>') { c->p += 1; return SOP_GT_NUM; }
Branch: 1 branch
BranchTrueFalseTotal
if (p[0] == '>') { c->p += 1; return SOP_GT_NUM; } 3 11449 11452
604 }
605 11449 wl = word_len(p, c->end);
606 11449 ▶ if (wl == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (wl == 2) { 28 11421 11449
607 28 ▶ if (word_is(p, 2, "eq")) { c->p += 2; return SOP_EQ_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "eq")) { c->p += 2; return SOP_EQ_STR; } 14 14 28
608 14 ▶ if (word_is(p, 2, "ne")) { c->p += 2; return SOP_NE_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "ne")) { c->p += 2; return SOP_NE_STR; } 3 11 14
609 11 ▶ if (word_is(p, 2, "lt")) { c->p += 2; return SOP_LT_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "lt")) { c->p += 2; return SOP_LT_STR; } 3 8 11
610 8 ▶ if (word_is(p, 2, "gt")) { c->p += 2; return SOP_GT_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "gt")) { c->p += 2; return SOP_GT_STR; } 2 6 8
611 6 ▶ if (word_is(p, 2, "le")) { c->p += 2; return SOP_LE_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "le")) { c->p += 2; return SOP_LE_STR; } 2 4 6
612 4 ▶ if (word_is(p, 2, "ge")) { c->p += 2; return SOP_GE_STR; }
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, 2, "ge")) { c->p += 2; return SOP_GE_STR; } 2 2 4
613 }
614 11423 return 0;
615 }
616  
617 11508 static int parse_cmp(stencil_compiler *c)
618 {
619 uint8_t op;
620 11508 ▶ if (!parse_primary(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_primary(c)) 24 11484 11508
621 24 return 0;
622 11484 skip_ws(c);
623 11484 op = scan_cmp_op(c);
624 11484 ▶ if (!op)
Branch: 1 branch
BranchTrueFalseTotal
if (!op) 11423 61 11484
625 11423 return 1;
626 61 ▶ if (!parse_primary(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_primary(c)) 2 59 61
627 2 return 0;
628 59 return emit_op(c, op, -1);
629 }
630  
631 11508 static int parse_not(stencil_compiler *c)
632 {
633 11508 int negs = 0;
634 for (;;) {
635 11515 skip_ws(c);
636 11515 ▶ if (c->p < c->end && *c->p == '!'
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '!' 11514 1 11515
if (c->p < c->end && *c->p == '!' 3 11511 11514
637 3 ▶ && !(c->p + 1 < c->end && c->p[1] == '=')) {
Branch: 2 branches
BranchTrueFalseTotal
&& !(c->p + 1 < c->end && c->p[1] == '=')) { 3 0 3
&& !(c->p + 1 < c->end && c->p[1] == '=')) { 3 0 3
638 3 c->p++;
639 3 negs++;
640 3 continue;
641 }
642 {
643 11512 size_t wl = word_len(c->p, c->end);
644 11512 ▶ if (wl && word_is(c->p, wl, "not")) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, "not")) { 11399 113 11512
if (wl && word_is(c->p, wl, "not")) { 4 11395 11399
645 4 c->p += wl;
646 4 negs++;
647 4 continue;
648 }
649 }
650 11508 break;
651 }
652 11508 ▶ if (!parse_cmp(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_cmp(c)) 26 11482 11508
653 26 return 0;
654 11489 ▶ while (negs--)
Branch: 1 branch
BranchTrueFalseTotal
while (negs--) 7 11482 11489
655 7 ▶ if (!emit_op(c, SOP_NOT, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_NOT, 0)) 0 7 7
656 0 return 0;
657 11482 return 1;
658 }
659  
660 /* and-op at c->p? consume and return true */
661 22951 static int scan_word_op(stencil_compiler *c, const char *sym,
662 const char *word)
663 {
664 size_t wl;
665 22951 ▶ if ((size_t)(c->end - c->p) >= 2 && c->p[0] == sym[0]
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)(c->end - c->p) >= 2 && c->p[0] == sym[0] 22951 0 22951
if ((size_t)(c->end - c->p) >= 2 && c->p[0] == sym[0] 24 22927 22951
666 24 ▶ && c->p[1] == sym[1]) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->p[1] == sym[1]) { 23 1 24
667 23 c->p += 2;
668 23 return 1;
669 }
670 22928 wl = word_len(c->p, c->end);
671 22928 ▶ if (wl && word_is(c->p, wl, word)) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, word)) { 5 22923 22928
if (wl && word_is(c->p, wl, word)) { 3 2 5
672 3 c->p += wl;
673 3 return 1;
674 }
675 22925 return 0;
676 }
677  
678 11495 static int parse_and(stencil_compiler *c)
679 {
680 11495 uint32_t chain = STENCIL_PATCH_END;
681 11495 ▶ if (!parse_not(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_not(c)) 11469 26 11495
682 26 return 0;
683 for (;;) {
684 11482 skip_ws(c);
685 11482 ▶ if (!scan_word_op(c, "&&", "and"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "&&", "and")) 11469 13 11482
686 11469 break;
687 26 if (!emit_jump_chained(c, SOP_JF_KEEP, 0, &chain) ||
688 26 ▶ !emit_op(c, SOP_POP, -1) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_op(c, SOP_POP, -1) || 0 13 13
689 13 !parse_not(c))
690 0 return 0;
691 }
692 11469 patch_chain(c, chain, code_pos(c));
693 11469 return 1;
694 }
695  
696 11482 static int parse_or(stencil_compiler *c)
697 {
698 11482 uint32_t chain = STENCIL_PATCH_END;
699 11482 ▶ if (!parse_and(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_and(c)) 11456 26 11482
700 26 return 0;
701 for (;;) {
702 11469 skip_ws(c);
703 11469 ▶ if (!scan_word_op(c, "||", "or"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "||", "or")) 11456 13 11469
704 11456 break;
705 26 if (!emit_jump_chained(c, SOP_JT_KEEP, 0, &chain) ||
706 26 ▶ !emit_op(c, SOP_POP, -1) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_op(c, SOP_POP, -1) || 0 13 13
707 13 !parse_and(c))
708 0 return 0;
709 }
710 11456 patch_chain(c, chain, code_pos(c));
711 11456 return 1;
712 }
713  
714 11482 static int parse_expr(stencil_compiler *c)
715 {
716 11482 return parse_or(c);
717 }
718  
719 /* ====================================================================
720 * Filters
721 * ==================================================================== */
722  
723 180 static int32_t filter_builtin(const char *p, uint32_t n)
724 {
725 180 ▶ if (word_is(p, n, "upper")) return STENCIL_FILT_UPPER;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "upper")) return STENCIL_FILT_UPPER; 51 129 180
726 129 ▶ if (word_is(p, n, "lower")) return STENCIL_FILT_LOWER;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "lower")) return STENCIL_FILT_LOWER; 9 120 129
727 120 ▶ if (word_is(p, n, "trim")) return STENCIL_FILT_TRIM;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "trim")) return STENCIL_FILT_TRIM; 20 100 120
728 100 ▶ if (word_is(p, n, "html")) return STENCIL_FILT_HTML;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "html")) return STENCIL_FILT_HTML; 10 90 100
729 90 ▶ if (word_is(p, n, "uri")) return STENCIL_FILT_URI;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "uri")) return STENCIL_FILT_URI; 9 81 90
730 81 ▶ if (word_is(p, n, "default")) return STENCIL_FILT_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "default")) return STENCIL_FILT_DEFAULT; 18 63 81
731 63 ▶ if (word_is(p, n, "fmt")) return STENCIL_FILT_FMT;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "fmt")) return STENCIL_FILT_FMT; 47 16 63
732 16 return STENCIL_FILT_USER;
733 }
734  
735 /* fmt's format string, checked here so the render can trust it blind:
736 * literal text plus exactly one %-conversion of a known-safe shape.
737 *
738 * %[-+ 0#]*[width][.precision]conv conv in [diouxXeEfgGs]
739 *
740 * %% is a literal. No '*', no length modifiers, no %n; width and
741 * precision are capped so the render-side output buffer cannot be
742 * outgrown, and the whole format is capped so the rewritten format
743 * (the render inserts perl's IVdf/UVxf length modifiers) fits its own
744 * stack buffer. Returns NULL when valid, else the complaint. */
745 #define STENCIL_FMT_MAXLEN 48
746 #define STENCIL_FMT_MAXWID 256
747 45 static const char *fmt_check(const char *p, uint32_t n)
748 {
749 45 const char *end = p + n;
750 45 int convs = 0;
751 45 ▶ if (n > STENCIL_FMT_MAXLEN)
Branch: 1 branch
BranchTrueFalseTotal
if (n > STENCIL_FMT_MAXLEN) 1 44 45
752 1 return "fmt: format is too long";
753 116 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 79 37 116
754 unsigned long w;
755 79 ▶ if (*p != '%') { p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '%') { p++; continue; } 34 45 79
756 45 p++;
757 45 ▶ if (p < end && *p == '%') { p++; continue; } /* literal %% */
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '%') { p++; continue; } /* literal %% */ 44 1 45
if (p < end && *p == '%') { p++; continue; } /* literal %% */ 1 43 44
758 48 ▶ while (p < end && (*p == '-' || *p == '+' || *p == ' '
Branch: 4 branches
BranchTrueFalseTotal
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 47 1 48
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 1 46 47
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 1 45 46
while (p < end && (*p == '-' || *p == '+' || *p == ' ' 0 45 45
759 45 ▶ || *p == '0' || *p == '#'))
Branch: 2 branches
BranchTrueFalseTotal
|| *p == '0' || *p == '#')) 1 44 45
|| *p == '0' || *p == '#')) 1 43 44
760 4 p++;
761 44 w = 0;
762 54 ▶ while (p < end && *p >= '0' && *p <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') 53 1 54
while (p < end && *p >= '0' && *p <= '9') 39 14 53
while (p < end && *p >= '0' && *p <= '9') 10 29 39
763 10 w = w * 10 + (unsigned long)(*p++ - '0');
764 44 ▶ if (w > STENCIL_FMT_MAXWID)
Branch: 1 branch
BranchTrueFalseTotal
if (w > STENCIL_FMT_MAXWID) 1 43 44
765 1 return "fmt: width is too large";
766 43 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 42 1 43
if (p < end && *p == '.') { 13 29 42
767 13 p++;
768 13 w = 0;
769 30 ▶ while (p < end && *p >= '0' && *p <= '9')
Branch: 3 branches
BranchTrueFalseTotal
while (p < end && *p >= '0' && *p <= '9') 30 0 30
while (p < end && *p >= '0' && *p <= '9') 30 0 30
while (p < end && *p >= '0' && *p <= '9') 17 13 30
770 17 w = w * 10 + (unsigned long)(*p++ - '0');
771 13 ▶ if (w > STENCIL_FMT_MAXWID)
Branch: 1 branch
BranchTrueFalseTotal
if (w > STENCIL_FMT_MAXWID) 1 12 13
772 1 return "fmt: precision is too large";
773 }
774 42 ▶ if (p >= end)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) 1 41 42
775 1 return "fmt: format ends inside a conversion";
776 41 ▶ if (*p == '*')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '*') 1 40 41
777 1 return "fmt: '*' is not allowed";
778 40 ▶ if (*p == '\0' || strchr("diouxXeEfgGs", *p) == NULL)
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\0' || strchr("diouxXeEfgGs", *p) == NULL) 40 0 40
if (*p == '\0' || strchr("diouxXeEfgGs", *p) == NULL) 3 37 40
779 3 return "fmt: conversion must be one of diouxXeEfgGs";
780 37 p++;
781 37 convs++;
782 }
783 37 ▶ if (convs != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (convs != 1) 2 35 37
784 return convs ? "fmt: only one conversion is allowed"
785 2 ▶ : "fmt: format needs one % conversion";
Branch: 1 branch
BranchTrueFalseTotal
: "fmt: format needs one % conversion"; 1 1 2
786 35 return NULL;
787 }
788  
789 /* parse `| name` / `| name(arg)` chains and emit SOP_FILTERs */
790 411972 static int parse_filters(stencil_compiler *c)
791 {
792 166 for (;;) {
793 const char *fname;
794 size_t fnlen;
795 stencil_cfilt f;
796 uint32_t foff;
797  
798 412138 skip_ws(c);
799 412138 ▶ if (c->p >= c->end || *c->p != '|')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '|') 412136 2 412138
if (c->p >= c->end || *c->p != '|') 411954 182 412136
800 411972 return 1;
801 182 c->p++;
802 182 skip_ws(c);
803 182 fnlen = word_len(c->p, c->end);
804 182 ▶ if (!fnlen)
Branch: 1 branch
BranchTrueFalseTotal
if (!fnlen) 2 180 182
805 2 return cerror(c, c->p, "expected a filter name after '|'");
806 180 fname = c->p;
807 180 c->p += fnlen;
808  
809 180 memset(&f, 0, sizeof f);
810 180 f.name_off = stencil_intern_get(&c->lit_intern, &c->pool, fname,
811 (uint32_t)fnlen);
812 180 ▶ if (f.name_off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (f.name_off == STENCIL_ARENA_NULL) 0 180 180
813 0 return coom(c);
814 180 f.name_len = (uint32_t)fnlen;
815 180 f.builtin_id = filter_builtin(fname, (uint32_t)fnlen);
816  
817 180 skip_ws(c);
818 180 ▶ if (c->p < c->end && *c->p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '(') { 180 0 180
if (c->p < c->end && *c->p == '(') { 68 112 180
819 68 c->p++;
820 68 skip_ws(c);
821 68 ▶ if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 68 0 68
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 18 50 68
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 11 7 18
822 61 char quote = *c->p;
823 61 const char *q = c->p + 1;
824 341 ▶ while (q < c->end && *q != quote) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && *q != quote) { 341 0 341
while (q < c->end && *q != quote) { 280 61 341
825 280 ▶ if (*q == '\\' && q + 1 < c->end
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\\' && q + 1 < c->end 0 280 280
if (*q == '\\' && q + 1 < c->end 0 0 0
826 0 ▶ && (q[1] == quote || q[1] == '\\'))
Branch: 2 branches
BranchTrueFalseTotal
&& (q[1] == quote || q[1] == '\\')) 0 0 0
&& (q[1] == quote || q[1] == '\\')) 0 0 0
827 0 q += 2;
828 else
829 280 q++;
830 }
831 61 ▶ if (q >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= c->end) 0 61 61
832 0 return cerror(c, c->p, "unterminated filter argument");
833 /* store raw (escapes are rare in filter args; unescape
834 * with the same rule as expression strings) */
835 {
836 61 const char *r = c->p + 1;
837 char *w;
838 61 uint32_t off = stencil_arena_alloc(&c->pool,
839 61 (size_t)(q - r), 1);
840 61 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 61 61
841 0 return coom(c);
842 61 w = c->pool.base + off;
843 341 ▶ while (r < q) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < q) { 280 61 341
844 280 ▶ if (*r == '\\' && (r[1] == quote || r[1] == '\\'))
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 280 280
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
845 0 r++;
846 280 *w++ = *r++;
847 }
848 61 f.str_off = off;
849 61 f.str_len = (uint32_t)(w - (c->pool.base + off));
850 61 c->pool.used = (size_t)(w - c->pool.base);
851 }
852 61 f.has_arg = 1;
853 61 c->p = q + 1;
854 7 ▶ } else if (c->p < c->end
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->p < c->end 7 0 7
855 7 ▶ && (isDIGIT(*c->p)
Branch: 1 branch
BranchTrueFalseTotal
&& (isDIGIT(*c->p) 0 7 7
856 0 ▶ || (*c->p == '-' && c->p + 1 < c->end
Branch: 2 branches
BranchTrueFalseTotal
|| (*c->p == '-' && c->p + 1 < c->end 0 0 0
|| (*c->p == '-' && c->p + 1 < c->end 0 0 0
857 0 ▶ && isDIGIT(c->p[1])))) {
Branch: 1 branch
BranchTrueFalseTotal
&& isDIGIT(c->p[1])))) { 0 0 0
858 7 double val = 0.0, frac = 0.1;
859 7 int neg = 0;
860 7 ▶ if (*c->p == '-') { neg = 1; c->p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '-') { neg = 1; c->p++; } 0 7 7
861 14 ▶ while (c->p < c->end && isDIGIT(*c->p))
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isDIGIT(*c->p)) 14 0 14
while (c->p < c->end && isDIGIT(*c->p)) 7 7 14
862 7 val = val * 10.0 + (*c->p++ - '0');
863 7 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 7 0 7
if (c->p < c->end && *c->p == '.') { 1 6 7
864 1 c->p++;
865 2 ▶ while (c->p < c->end && isDIGIT(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isDIGIT(*c->p)) { 2 0 2
while (c->p < c->end && isDIGIT(*c->p)) { 1 1 2
866 1 val += (*c->p++ - '0') * frac;
867 1 frac *= 0.1;
868 }
869 }
870 7 ▶ f.num_arg = neg ? -val : val;
Branch: 1 branch
BranchTrueFalseTotal
f.num_arg = neg ? -val : val; 0 7 7
871 7 f.arg_is_num = 1;
872 7 f.has_arg = 1;
873 } else {
874 0 return cerror(c, c->p,
875 "filter argument must be a quoted string or number");
876 }
877 68 skip_ws(c);
878 68 ▶ if (c->p >= c->end || *c->p != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ')') 68 0 68
if (c->p >= c->end || *c->p != ')') 0 68 68
879 0 return cerror(c, c->p, "expected ')'");
880 68 c->p++;
881 }
882  
883 /* arity for built-ins is known at compile time */
884 180 ▶ if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg)
Branch: 2 branches
BranchTrueFalseTotal
if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg) 18 162 180
if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg) 1 17 18
885 1 return cerror(c, fname, "filter 'default' needs an argument");
886 179 ▶ if (f.builtin_id == STENCIL_FILT_FMT) {
Branch: 1 branch
BranchTrueFalseTotal
if (f.builtin_id == STENCIL_FILT_FMT) { 47 132 179
887 const char *bad;
888 47 ▶ if (!f.has_arg || f.arg_is_num)
Branch: 2 branches
BranchTrueFalseTotal
if (!f.has_arg || f.arg_is_num) 46 1 47
if (!f.has_arg || f.arg_is_num) 1 45 46
889 2 return cerror(c, fname,
890 "filter 'fmt' needs a quoted format argument");
891 45 bad = fmt_check(c->pool.base + f.str_off, f.str_len);
892 45 ▶ if (bad)
Branch: 1 branch
BranchTrueFalseTotal
if (bad) 10 35 45
893 10 return cerror(c, fname, "%s", bad);
894 }
895 167 ▶ if (f.builtin_id >= 0 && f.builtin_id != STENCIL_FILT_DEFAULT
Branch: 2 branches
BranchTrueFalseTotal
if (f.builtin_id >= 0 && f.builtin_id != STENCIL_FILT_DEFAULT 151 16 167
if (f.builtin_id >= 0 && f.builtin_id != STENCIL_FILT_DEFAULT 134 17 151
896 134 ▶ && f.builtin_id != STENCIL_FILT_FMT && f.has_arg)
Branch: 2 branches
BranchTrueFalseTotal
&& f.builtin_id != STENCIL_FILT_FMT && f.has_arg) 99 35 134
&& f.builtin_id != STENCIL_FILT_FMT && f.has_arg) 1 98 99
897 1 return cerror(c, fname, "filter '%.*s' takes no argument",
898 (int)fnlen, fname);
899  
900 166 foff = stencil_arena_alloc(&c->filts, sizeof(stencil_cfilt),
901 sizeof(double));
902 166 ▶ if (foff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (foff == STENCIL_ARENA_NULL) 0 166 166
903 0 return coom(c);
904 166 memcpy(c->filts.base + foff, &f, sizeof f);
905 166 ▶ if (!emit_op(c, SOP_FILTER, 0) || !emit_u32(c, c->n_filts))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_FILTER, 0) || !emit_u32(c, c->n_filts)) 166 0 166
if (!emit_op(c, SOP_FILTER, 0) || !emit_u32(c, c->n_filts)) 0 166 166
906 0 return 0;
907 166 c->n_filts++;
908 }
909 }
910  
911 /* ====================================================================
912 * Statements
913 * ==================================================================== */
914  
915 22656 static stencil_blk *blk_open(stencil_compiler *c, uint8_t kind,
916 uint32_t line, uint32_t col)
917 {
918 stencil_blk *b;
919 22656 ▶ if (c->n_blk >= STENCIL_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk >= STENCIL_MAX_DEPTH) { 1 22655 22656
920 1 cerror_at(c, line, col, "blocks nested deeper than %d",
921 STENCIL_MAX_DEPTH);
922 1 return NULL;
923 }
924 22655 b = &c->blk[c->n_blk++];
925 22655 memset(b, 0, sizeof *b);
926 22655 b->kind = kind;
927 22655 b->line = line;
928 22655 b->col = col;
929 22655 b->cond_patch = STENCIL_PATCH_END;
930 22655 b->jump_chain = STENCIL_PATCH_END;
931 22655 b->binds_at_entry = c->cur_binds;
932 22655 return b;
933 }
934  
935 6 static const char *blk_kind_name(uint8_t kind)
936 {
937 6 ▶ return kind == BLK_FOR ? "for" : kind == BLK_UNLESS ? "unless" : "if";
Branch: 2 branches
BranchTrueFalseTotal
return kind == BLK_FOR ? "for" : kind == BLK_UNLESS ? "unless" : "if"; 5 1 6
return kind == BLK_FOR ? "for" : kind == BLK_UNLESS ? "unless" : "if"; 1 4 5
938 }
939  
940 /* A branch's set-binds are popped at run time when the branch's code
941 * path leaves the block; each branch pops only its own binds (they are
942 * counted per-branch, not per-block). */
943 22644 static int emit_pop_binds(stencil_compiler *c, stencil_blk *b)
944 {
945 22644 int32_t delta = c->cur_binds - b->binds_at_entry;
946 22644 ▶ if (delta <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delta <= 0) 22573 71 22644
947 22573 return 1;
948 71 c->cur_binds = b->binds_at_entry;
949 71 ▶ return emit_op(c, SOP_POP_BINDS, 0) && emit_u32(c, (uint32_t)delta);
Branch: 2 branches
BranchTrueFalseTotal
return emit_op(c, SOP_POP_BINDS, 0) && emit_u32(c, (uint32_t)delta); 71 0 71
return emit_op(c, SOP_POP_BINDS, 0) && emit_u32(c, (uint32_t)delta); 71 0 71
950 }
951  
952 11376 static int stmt_if(stencil_compiler *c, uint8_t kind, uint32_t line,
953 uint32_t col)
954 {
955 11376 stencil_blk *b = blk_open(c, kind, line, col);
956 11376 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 1 11375 11376
957 1 return 0;
958 11375 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 11349 26 11375
if (!parse_expr(c) || !expect_close(c)) 8 11341 11349
959 34 return 0;
960 11341 ▶ if (!emit_op(c, kind == BLK_UNLESS ? SOP_TEST_JT : SOP_TEST_JF, -1))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, kind == BLK_UNLESS ? SOP_TEST_JT : SOP_TEST_JF, -1)) 8 11333 11341
if (!emit_op(c, kind == BLK_UNLESS ? SOP_TEST_JT : SOP_TEST_JF, -1)) 0 11341 11341
961 0 return 0;
962 11341 b->cond_patch = code_pos(c);
963 11341 return emit_u32(c, STENCIL_PATCH_END);
964 }
965  
966 40 static int stmt_elsif(stencil_compiler *c, uint32_t line, uint32_t col)
967 {
968 stencil_blk *b;
969 40 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 30 10 40
970 30 return cerror_at(c, line, col, "'elsif' with no open 'if'");
971 10 b = &c->blk[c->n_blk - 1];
972 10 ▶ if (b->kind != BLK_IF)
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind != BLK_IF) 1 9 10
973 1 return cerror_at(c, line, col, "'elsif' inside '%s' block",
974 1 blk_kind_name(b->kind));
975 9 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 1 8 9
976 1 return cerror_at(c, line, col, "'elsif' after 'else'");
977 8 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 8 8
978 0 return 0;
979 8 ▶ if (!emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain)) 0 8 8
980 0 return 0;
981 8 patch_u32(c, b->cond_patch, code_pos(c));
982 8 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 8 0 8
if (!parse_expr(c) || !expect_close(c)) 0 8 8
983 0 return 0;
984 8 ▶ if (!emit_op(c, SOP_TEST_JF, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_TEST_JF, -1)) 0 8 8
985 0 return 0;
986 8 b->cond_patch = code_pos(c);
987 8 return emit_u32(c, STENCIL_PATCH_END);
988 }
989  
990 119 static int stmt_else(stencil_compiler *c, uint32_t line, uint32_t col)
991 {
992 stencil_blk *b;
993 119 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 29 90 119
994 29 return cerror_at(c, line, col, "'else' with no open block");
995 90 b = &c->blk[c->n_blk - 1];
996 90 ▶ if (b->kind == BLK_FOR)
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_FOR) 1 89 90
997 1 return cerror_at(c, line, col, "'else' inside 'for' block");
998 89 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 1 88 89
999 1 return cerror_at(c, line, col, "duplicate 'else'");
1000 88 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 88 88
1001 0 return 0;
1002 88 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 88 88
1003 0 return 0;
1004 88 ▶ if (!emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain)) 0 88 88
1005 0 return 0;
1006 88 patch_u32(c, b->cond_patch, code_pos(c));
1007 88 b->cond_patch = STENCIL_PATCH_END;
1008 88 b->has_else = 1;
1009 88 return 1;
1010 }
1011  
1012 22584 static int stmt_end(stencil_compiler *c, uint32_t line, uint32_t col)
1013 {
1014 stencil_blk *b;
1015 22584 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 36 22548 22584
1016 36 return cerror_at(c, line, col, "'end' with no open block");
1017 22548 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 22548 22548
1018 0 return 0;
1019 22548 b = &c->blk[--c->n_blk];
1020 22548 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 22548 22548
1021 0 return 0;
1022 22548 ▶ if (b->kind == BLK_FOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_FOR) { 11278 11270 22548
1023 /* the pops sit before FOR_NEXT so every iteration gets a fresh
1024 * bind scope */
1025 11278 ▶ if (!emit_op(c, SOP_FOR_NEXT, 0) || !emit_u32(c, b->body_start))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_FOR_NEXT, 0) || !emit_u32(c, b->body_start)) 11278 0 11278
if (!emit_op(c, SOP_FOR_NEXT, 0) || !emit_u32(c, b->body_start)) 0 11278 11278
1026 0 return 0;
1027 11278 patch_u32(c, b->for_end_patch, code_pos(c));
1028 11278 c->cur_frames--;
1029 } else {
1030 11270 ▶ if (b->cond_patch != STENCIL_PATCH_END)
Branch: 1 branch
BranchTrueFalseTotal
if (b->cond_patch != STENCIL_PATCH_END) 11184 86 11270
1031 11184 patch_u32(c, b->cond_patch, code_pos(c));
1032 11270 patch_chain(c, b->jump_chain, code_pos(c));
1033 }
1034 22548 return 1;
1035 }
1036  
1037 11312 static int stmt_for(stencil_compiler *c, uint32_t line, uint32_t col)
1038 {
1039 11312 const char *n1, *n2 = NULL;
1040 11312 size_t l1, l2 = 0;
1041 11312 uint32_t id1, id2 = 0;
1042 stencil_blk *b;
1043  
1044 11312 skip_ws(c);
1045 11312 l1 = word_len(c->p, c->end);
1046 11312 ▶ if (!l1 || is_keyword(c->p, l1))
Branch: 2 branches
BranchTrueFalseTotal
if (!l1 || is_keyword(c->p, l1)) 11286 26 11312
if (!l1 || is_keyword(c->p, l1)) 1 11285 11286
1047 27 return cerror(c, c->p, "expected a loop variable name");
1048 11285 n1 = c->p;
1049 11285 c->p += l1;
1050 11285 skip_ws(c);
1051 11285 ▶ if (c->p < c->end && *c->p == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ',') { 11285 0 11285
if (c->p < c->end && *c->p == ',') { 81 11204 11285
1052 81 c->p++;
1053 81 skip_ws(c);
1054 81 l2 = word_len(c->p, c->end);
1055 81 ▶ if (!l2 || is_keyword(c->p, l2))
Branch: 2 branches
BranchTrueFalseTotal
if (!l2 || is_keyword(c->p, l2)) 81 0 81
if (!l2 || is_keyword(c->p, l2)) 0 81 81
1056 0 return cerror(c, c->p, "expected a value variable name");
1057 81 n2 = c->p;
1058 81 c->p += l2;
1059 81 skip_ws(c);
1060 }
1061 {
1062 11285 size_t wl = word_len(c->p, c->end);
1063 11285 ▶ if (!wl || !word_is(c->p, wl, "in"))
Branch: 2 branches
BranchTrueFalseTotal
if (!wl || !word_is(c->p, wl, "in")) 11281 4 11285
if (!wl || !word_is(c->p, wl, "in")) 1 11280 11281
1064 5 return cerror(c, c->p, "expected 'in'");
1065 11280 c->p += wl;
1066 }
1067 11280 skip_ws(c);
1068 11280 ▶ if (!parse_path_push(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_path_push(c)) 0 11280 11280
1069 0 return 0;
1070 11280 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 11280 11280
1071 0 return 0;
1072  
1073 11280 id1 = name_intern(c, n1, (uint32_t)l1);
1074 11280 ▶ if (id1 == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id1 == STENCIL_ARENA_NULL) 0 11280 11280
1075 0 return coom(c);
1076 11280 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 81 11199 11280
1077 81 id2 = name_intern(c, n2, (uint32_t)l2);
1078 81 ▶ if (id2 == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id2 == STENCIL_ARENA_NULL) 0 81 81
1079 0 return coom(c);
1080 }
1081  
1082 11280 b = blk_open(c, BLK_FOR, line, col);
1083 11280 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 0 11280 11280
1084 0 return 0;
1085 11280 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 81 11199 11280
1086 162 if (!emit_op(c, SOP_FOR_HASH, -1) ||
1087 162 ▶ !emit_u32(c, id1) || !emit_u32(c, id2))
Branch: 1 branch
BranchTrueFalseTotal
!emit_u32(c, id1) || !emit_u32(c, id2)) 0 81 81
1088 0 return 0;
1089 } else {
1090 11199 ▶ if (!emit_op(c, SOP_FOR_ARY, -1) || !emit_u32(c, id1))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_FOR_ARY, -1) || !emit_u32(c, id1)) 11199 0 11199
if (!emit_op(c, SOP_FOR_ARY, -1) || !emit_u32(c, id1)) 0 11199 11199
1091 0 return 0;
1092 }
1093 11280 b->for_end_patch = code_pos(c);
1094 11280 ▶ if (!emit_u32(c, STENCIL_PATCH_END))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_u32(c, STENCIL_PATCH_END)) 0 11280 11280
1095 0 return 0;
1096 11280 b->body_start = code_pos(c);
1097 11280 c->cur_frames++;
1098 11280 ▶ if (c->cur_frames > c->max_frames)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_frames > c->max_frames) 11211 69 11280
1099 11211 c->max_frames = c->cur_frames;
1100 11280 return 1;
1101 }
1102  
1103 121 static int stmt_set(stencil_compiler *c)
1104 {
1105 const char *n1;
1106 size_t l1;
1107 uint32_t id;
1108  
1109 121 skip_ws(c);
1110 121 l1 = word_len(c->p, c->end);
1111 121 ▶ if (!l1 || is_keyword(c->p, l1))
Branch: 2 branches
BranchTrueFalseTotal
if (!l1 || is_keyword(c->p, l1)) 102 19 121
if (!l1 || is_keyword(c->p, l1)) 2 100 102
1112 21 return cerror(c, c->p, "expected a variable name after 'set'");
1113 100 n1 = c->p;
1114 100 c->p += l1;
1115 100 ▶ if (c->p < c->end && (*c->p == '.' || *c->p == '['))
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 100 0 100
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 97 3 100
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 0 97 97
1116 3 return cerror(c, c->p, "'set' target must be a plain name");
1117 97 skip_ws(c);
1118 97 ▶ if (c->p >= c->end || *c->p != '='
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '=' 97 0 97
if (c->p >= c->end || *c->p != '=' 95 2 97
1119 95 ▶ || (c->p + 1 < c->end && c->p[1] == '='))
Branch: 2 branches
BranchTrueFalseTotal
|| (c->p + 1 < c->end && c->p[1] == '=')) 95 0 95
|| (c->p + 1 < c->end && c->p[1] == '=')) 0 95 95
1120 2 return cerror(c, c->p, "expected '=' after the 'set' name");
1121 95 c->p++;
1122 95 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 95 0 95
if (!parse_expr(c) || !expect_close(c)) 0 95 95
1123 0 return 0;
1124 95 id = name_intern(c, n1, (uint32_t)l1);
1125 95 ▶ if (id == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id == STENCIL_ARENA_NULL) 0 95 95
1126 0 return coom(c);
1127 95 ▶ if (!emit_op(c, SOP_SET, -1) || !emit_u32(c, id))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_SET, -1) || !emit_u32(c, id)) 95 0 95
if (!emit_op(c, SOP_SET, -1) || !emit_u32(c, id)) 0 95 95
1128 0 return 0;
1129 95 c->cur_binds++;
1130 95 ▶ if (c->cur_binds > c->max_binds)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_binds > c->max_binds) 94 1 95
1131 94 c->max_binds = c->cur_binds;
1132 95 return 1;
1133 }
1134  
1135 64 static int stmt_include(stencil_compiler *c, uint32_t line, uint32_t col)
1136 {
1137 const char *n;
1138 size_t len;
1139 uint32_t i, off, ioff;
1140 stencil_cinc *tab, *e;
1141  
1142 64 skip_ws(c);
1143 64 n = c->p;
1144 485 ▶ while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%')
Branch: 3 branches
BranchTrueFalseTotal
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 482 3 485
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 443 39 482
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 421 22 443
1145 421 c->p++;
1146 64 len = (size_t)(c->p - n);
1147 64 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 1 63 64
1148 1 return cerror(c, n, "expected a template name after 'include'");
1149 63 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 28 35 63
1150 28 return 0;
1151  
1152 35 tab = (stencil_cinc *)c->incs.base;
1153 35 ▶ for (i = 0; i < c->n_incs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_incs; i++) 1 34 35
1154 1 ▶ if (tab[i].name_len == len
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].name_len == len 1 0 1
1155 1 ▶ && memcmp(c->pool.base + tab[i].name_off, n, len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(c->pool.base + tab[i].name_off, n, len) == 0) 1 0 1
1156 1 ▶ return emit_op(c, SOP_INCLUDE, 0) && emit_u32(c, i);
Branch: 2 branches
BranchTrueFalseTotal
return emit_op(c, SOP_INCLUDE, 0) && emit_u32(c, i); 1 0 1
return emit_op(c, SOP_INCLUDE, 0) && emit_u32(c, i); 1 0 1
1157  
1158 34 off = stencil_intern_get(&c->lit_intern, &c->pool, n, (uint32_t)len);
1159 34 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 34 34
1160 0 return coom(c);
1161 34 ioff = stencil_arena_alloc(&c->incs, sizeof(stencil_cinc),
1162 sizeof(uint32_t));
1163 34 ▶ if (ioff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ioff == STENCIL_ARENA_NULL) 0 34 34
1164 0 return coom(c);
1165 34 e = (stencil_cinc *)(c->incs.base + ioff);
1166 34 e->name_off = off;
1167 34 e->name_len = (uint32_t)len;
1168 34 e->line = line;
1169 34 e->col = col;
1170 34 ▶ if (!emit_op(c, SOP_INCLUDE, 0) || !emit_u32(c, c->n_incs))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_INCLUDE, 0) || !emit_u32(c, c->n_incs)) 34 0 34
if (!emit_op(c, SOP_INCLUDE, 0) || !emit_u32(c, c->n_incs)) 0 34 34
1171 0 return 0;
1172 34 c->n_incs++;
1173 34 return 1;
1174 }
1175  
1176 /* ====================================================================
1177 * Main scan loop
1178 * ==================================================================== */
1179  
1180 12424 static int scan_template(stencil_compiler *c)
1181 {
1182 457463 for (;;) {
1183 469887 const char *run = c->p;
1184 469887 const char *q = c->p;
1185  
1186 /* literal run up to the next real '{%' */
1187 for (;;) {
1188 469991 q = stencil_dispatch.scan(q, c->end);
1189 469991 ▶ if (q == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q == c->end) 12114 457877 469991
1190 12114 break;
1191 457877 ▶ if (q + 1 < c->end && q[1] == '%')
Branch: 2 branches
BranchTrueFalseTotal
if (q + 1 < c->end && q[1] == '%') 457876 1 457877
if (q + 1 < c->end && q[1] == '%') 457773 103 457876
1192 457773 break;
1193 104 q++;
1194 }
1195 469887 ▶ if (!lit_pend(c, run, (size_t)(q - run)))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_pend(c, run, (size_t)(q - run))) 0 469887 469887
1196 0 return 0;
1197 469887 advance_lines(c, run, q);
1198 469887 c->p = q;
1199 469887 ▶ if (c->p == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p == c->end) 12114 457773 469887
1200 12114 break;
1201  
1202 /* at '{%' */
1203 {
1204 457773 uint32_t tag_line = c->line;
1205 457773 uint32_t tag_col = ccol(c, c->p);
1206 457773 const char *tag_at = c->p;
1207 457773 c->p += 2;
1208  
1209 /* comment */
1210 457773 ▶ if (c->p < c->end && *c->p == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '#') { 457772 1 457773
if (c->p < c->end && *c->p == '#') { 38 457734 457772
1211 38 const char *r = c->p + 1;
1212 for (;;) {
1213 112 r = (const char *)memchr(r, '%',
1214 75 (size_t)(c->end - r));
1215 75 ▶ if (!r)
Branch: 1 branch
BranchTrueFalseTotal
if (!r) 13 62 75
1216 13 return cerror_at(c, tag_line, tag_col,
1217 "unterminated comment");
1218 62 ▶ if (r + 1 < c->end && r[1] == '}')
Branch: 2 branches
BranchTrueFalseTotal
if (r + 1 < c->end && r[1] == '}') 61 1 62
if (r + 1 < c->end && r[1] == '}') 25 36 61
1219 25 break;
1220 37 r++;
1221 }
1222 25 advance_lines(c, c->p, r);
1223 25 c->p = r + 2;
1224 25 continue;
1225 }
1226  
1227 457735 skip_ws(c);
1228  
1229 /* empty tag = literal '{%' escape */
1230 457735 ▶ if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') {
Branch: 3 branches
BranchTrueFalseTotal
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 457734 1 457735
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 32 457702 457734
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 32 0 32
1231 32 c->p += 2;
1232 32 ▶ if (!lit_pend(c, "{%", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_pend(c, "{%", 2)) 0 32 32
1233 0 return 0;
1234 32 continue;
1235 }
1236  
1237 457703 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 457703 457703
1238 0 return 0;
1239  
1240 {
1241 457703 size_t wl = word_len(c->p, c->end);
1242 457703 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 27 457676 457703
1243 27 return cerror(c, c->p, "expected a name or keyword");
1244  
1245 457676 ▶ if (word_is(c->p, wl, "if")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "if")) { 11368 446308 457676
1246 11368 c->p += wl;
1247 11368 ▶ if (!stmt_if(c, BLK_IF, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_if(c, BLK_IF, tag_line, tag_col)) 35 11333 11368
1248 35 return 0;
1249 446308 ▶ } else if (word_is(c->p, wl, "unless")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "unless")) { 8 446300 446308
1250 8 c->p += wl;
1251 8 ▶ if (!stmt_if(c, BLK_UNLESS, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_if(c, BLK_UNLESS, tag_line, tag_col)) 0 8 8
1252 0 return 0;
1253 446300 ▶ } else if (word_is(c->p, wl, "elsif")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "elsif")) { 40 446260 446300
1254 40 c->p += wl;
1255 40 ▶ if (!stmt_elsif(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_elsif(c, tag_line, tag_col)) 32 8 40
1256 32 return 0;
1257 446260 ▶ } else if (word_is(c->p, wl, "else")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "else")) { 119 446141 446260
1258 119 c->p += wl;
1259 119 ▶ if (!stmt_else(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_else(c, tag_line, tag_col)) 31 88 119
1260 31 return 0;
1261 446141 ▶ } else if (word_is(c->p, wl, "end")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "end")) { 22584 423557 446141
1262 22584 c->p += wl;
1263 22584 ▶ if (!stmt_end(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_end(c, tag_line, tag_col)) 36 22548 22584
1264 36 return 0;
1265 423557 ▶ } else if (word_is(c->p, wl, "for")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "for")) { 11312 412245 423557
1266 11312 c->p += wl;
1267 11312 ▶ if (!stmt_for(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_for(c, tag_line, tag_col)) 32 11280 11312
1268 32 return 0;
1269 412245 ▶ } else if (word_is(c->p, wl, "set")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "set")) { 121 412124 412245
1270 121 c->p += wl;
1271 121 ▶ if (!stmt_set(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_set(c)) 26 95 121
1272 26 return 0;
1273 412124 ▶ } else if (word_is(c->p, wl, "include")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "include")) { 64 412060 412124
1274 64 c->p += wl;
1275 64 ▶ if (!stmt_include(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_include(c, tag_line, tag_col)) 29 35 64
1276 29 return 0;
1277 412060 ▶ } else if (word_is(c->p, wl, "content")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "content")) { 67 411993 412060
1278 67 c->p += wl;
1279 67 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 67 67
1280 0 return 0;
1281 67 ▶ if (!emit_op(c, SOP_CONTENT, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_CONTENT, 0)) 0 67 67
1282 0 return 0;
1283 67 c->flags |= STENCIL_PROG_IS_WRAPPER;
1284 411993 ▶ } else if (word_is(c->p, wl, "raw")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "raw")) { 36 411957 411993
1285 36 c->p += wl;
1286 36 skip_ws(c);
1287 36 ▶ if (!parse_path_push(c) || !parse_filters(c)
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_path_push(c) || !parse_filters(c) 19 17 36
if (!parse_path_push(c) || !parse_filters(c) 18 1 19
1288 18 ▶ || !expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
|| !expect_close(c)) 6 12 18
1289 24 return 0;
1290 12 ▶ if (!emit_op(c, SOP_PRINT_RAW, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_PRINT_RAW, -1)) 0 12 12
1291 0 return 0;
1292 } else {
1293 /* output tag */
1294 411957 ▶ if (!parse_path_push(c) || !parse_filters(c)
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_path_push(c) || !parse_filters(c) 411953 4 411957
if (!parse_path_push(c) || !parse_filters(c) 411938 15 411953
1295 411938 ▶ || !expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
|| !expect_close(c)) 6 411932 411938
1296 25 return 0;
1297 411932 ▶ if (!emit_op(c, SOP_PRINT_ESC, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_PRINT_ESC, -1)) 0 411932 411932
1298 0 return 0;
1299 }
1300 }
1301 (void)tag_at;
1302 }
1303 }
1304  
1305 12114 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 12114 12114
1306 0 return 0;
1307 12114 ▶ if (c->n_blk) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk) { 5 12109 12114
1308 5 stencil_blk *b = &c->blk[c->n_blk - 1];
1309 5 return cerror_at(c, b->line, b->col,
1310 5 "unclosed '%s' block", blk_kind_name(b->kind));
1311 }
1312 12109 return emit_op(c, SOP_END, 0);
1313 }
1314  
1315 /* ====================================================================
1316 * Compile driver: pack regions into the final arena
1317 * ==================================================================== */
1318  
1319 12424 static void compiler_free_temps(stencil_compiler *c)
1320 {
1321 12424 stencil_arena_free(&c->code);
1322 12424 stencil_arena_free(&c->pool);
1323 12424 stencil_arena_free(&c->names);
1324 12424 stencil_arena_free(&c->paths);
1325 12424 stencil_arena_free(&c->segs);
1326 12424 stencil_arena_free(&c->incs);
1327 12424 stencil_arena_free(&c->filts);
1328 12424 stencil_arena_free(&c->lines);
1329 12424 stencil_arena_free(&c->pend);
1330 12424 stencil_intern_free(&c->lit_intern);
1331 12424 }
1332  
1333 96872 static uint32_t pack_region(stencil_arena *dst, const stencil_arena *src,
1334 size_t align)
1335 {
1336 96872 ▶ uint32_t off = stencil_arena_alloc(dst, src->used ? src->used : 1,
Branch: 1 branch
BranchTrueFalseTotal
uint32_t off = stencil_arena_alloc(dst, src->used ? src->used : 1, 71512 25360 96872
1337 align);
1338 96872 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 96872 96872
1339 0 return off;
1340 96872 ▶ if (src->used)
Branch: 1 branch
BranchTrueFalseTotal
if (src->used) 71512 25360 96872
1341 71512 memcpy(dst->base + off, src->base, src->used);
1342 96872 return off;
1343 }
1344  
1345 12424 stencil_program *stencil_compile(pTHX_ const char *src, STRLEN len,
1346 const char *name, uint32_t flags,
1347 SV **err)
1348 {
1349 stencil_compiler c;
1350 12424 stencil_program *prog = NULL;
1351 int ok;
1352  
1353 12424 memset(&c, 0, sizeof c);
1354 12424 c.flags = flags & STENCIL_PROG_SRC_UTF8;
1355 12424 c.src = c.p = c.line_start = src;
1356 12424 c.end = src + len;
1357 12424 c.name = name;
1358 12424 c.line = 1;
1359 12424 c.last_line_rec = 0;
1360 #ifdef PERL_IMPLICIT_CONTEXT
1361 c.perl = aTHX;
1362 #endif
1363  
1364 24848 if (!stencil_arena_init(&c.code, 256) ||
1365 24848 ▶ !stencil_arena_init(&c.pool, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pool, 256) || 12424 0 12424
1366 24848 ▶ !stencil_arena_init(&c.names, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.names, 256) || 12424 0 12424
1367 24848 ▶ !stencil_arena_init(&c.paths, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.paths, 256) || 12424 0 12424
1368 24848 ▶ !stencil_arena_init(&c.segs, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.segs, 256) || 12424 0 12424
1369 24848 ▶ !stencil_arena_init(&c.incs, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.incs, 64) || 12424 0 12424
1370 24848 ▶ !stencil_arena_init(&c.filts, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.filts, 64) || 12424 0 12424
1371 24848 ▶ !stencil_arena_init(&c.lines, 128) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.lines, 128) || 12424 0 12424
1372 24848 ▶ !stencil_arena_init(&c.pend, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pend, 256) || 0 12424 12424
1373 12424 !stencil_intern_init(&c.lit_intern)) {
1374 0 compiler_free_temps(&c);
1375 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
1376 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
1377 0 return NULL;
1378 }
1379  
1380 12424 ok = scan_template(&c);
1381  
1382 12424 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 12109 315 12424
1383 12109 prog = (stencil_program *)calloc(1, sizeof *prog);
1384 12109 ▶ if (!prog) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) { 0 12109 12109
1385 0 ok = 0;
1386 0 cerror_at(&c, c.line, 1, "out of memory");
1387 }
1388 }
1389 12424 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 12109 315 12424
1390 12109 size_t total = c.code.used + c.pool.used + c.names.used
1391 12109 + c.paths.used + c.segs.used + c.incs.used
1392 12109 + c.filts.used + c.lines.used + 128;
1393 12109 ▶ if (!stencil_arena_init(&prog->arena, total)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_arena_init(&prog->arena, total)) { 0 12109 12109
1394 0 free(prog);
1395 0 prog = NULL;
1396 0 ok = 0;
1397 0 cerror_at(&c, c.line, 1, "out of memory");
1398 }
1399 }
1400 12424 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 12109 315 12424
1401 12109 prog->code_off = pack_region(&prog->arena, &c.code, 1);
1402 12109 prog->pool_off = pack_region(&prog->arena, &c.pool, 1);
1403 12109 prog->names_off = pack_region(&prog->arena, &c.names,
1404 sizeof(uint32_t));
1405 12109 prog->paths_off = pack_region(&prog->arena, &c.paths,
1406 sizeof(uint32_t));
1407 12109 prog->segs_off = pack_region(&prog->arena, &c.segs,
1408 sizeof(SSize_t));
1409 12109 prog->incs_off = pack_region(&prog->arena, &c.incs,
1410 sizeof(uint32_t));
1411 12109 prog->filts_off = pack_region(&prog->arena, &c.filts,
1412 sizeof(double));
1413 12109 prog->lines_off = pack_region(&prog->arena, &c.lines,
1414 sizeof(uint32_t));
1415 12109 ▶ if (prog->code_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
if (prog->code_off == STENCIL_ARENA_NULL 12109 0 12109
1416 12109 ▶ || prog->pool_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->pool_off == STENCIL_ARENA_NULL 12109 0 12109
1417 12109 ▶ || prog->names_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->names_off == STENCIL_ARENA_NULL 12109 0 12109
1418 12109 ▶ || prog->paths_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->paths_off == STENCIL_ARENA_NULL 12109 0 12109
1419 12109 ▶ || prog->segs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->segs_off == STENCIL_ARENA_NULL 12109 0 12109
1420 12109 ▶ || prog->incs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->incs_off == STENCIL_ARENA_NULL 12109 0 12109
1421 12109 ▶ || prog->filts_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->filts_off == STENCIL_ARENA_NULL 12109 0 12109
1422 12109 ▶ || prog->lines_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
|| prog->lines_off == STENCIL_ARENA_NULL) { 0 12109 12109
1423 0 stencil_program_free(prog);
1424 0 prog = NULL;
1425 0 ok = 0;
1426 0 cerror_at(&c, c.line, 1, "out of memory");
1427 }
1428 }
1429 12424 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 12109 315 12424
1430 12109 prog->code_len = (uint32_t)c.code.used;
1431 12109 prog->pool_len = (uint32_t)c.pool.used;
1432 12109 prog->n_names = c.n_names;
1433 12109 prog->n_paths = c.n_paths;
1434 12109 prog->n_segs = c.n_segs;
1435 12109 prog->n_incs = c.n_incs;
1436 12109 prog->n_filts = c.n_filts;
1437 12109 prog->n_lines = c.n_lines;
1438 12109 prog->max_stack = (uint32_t)c.max_stack;
1439 12109 prog->max_frames = (uint32_t)c.max_frames;
1440 12109 prog->max_binds = (uint32_t)c.max_binds;
1441 12109 prog->flags = c.flags;
1442 12109 stencil_arena_finalise(&prog->arena);
1443 }
1444  
1445 12424 compiler_free_temps(&c);
1446  
1447 12424 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 315 12109 12424
1448 315 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 315 0 315
1449 315 ▶ *err = sv_2mortal(newSVpvf("Template::Stencil: %s:%u:%u: %s",
Branch: 1 branch
BranchTrueFalseTotal
*err = sv_2mortal(newSVpvf("Template::Stencil: %s:%u:%u: %s", 315 0 315
1450 c.name ? c.name : "<string>",
1451 (unsigned)c.err_line,
1452 (unsigned)c.err_col, c.errbuf));
1453 315 return NULL;
1454 }
1455 12109 return prog;
1456 }
1457  
1458 12162 void stencil_program_free(stencil_program *prog)
1459 {
1460 12162 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 53 12109 12162
1461 53 return;
1462 12109 stencil_arena_free(&prog->arena);
1463 12109 free(prog);
1464 }
1465  
1466 /* ====================================================================
1467 * Inspect: decode the program for tests
1468 * ==================================================================== */
1469  
1470 static const char *const op_names[] = {
1471 #define STENCIL_X_NAME(n) #n,
1472 STENCIL_OPS(STENCIL_X_NAME)
1473 #undef STENCIL_X_NAME
1474 };
1475  
1476 #define hv_store_lit(hv, key, sv) \
1477 (void)hv_store((hv), "" key "", sizeof(key) - 1, (sv), 0)
1478  
1479 213 static SV *path_to_sv(pTHX_ const stencil_program *pr, uint32_t id)
1480 {
1481 213 const stencil_cpath *p = &stencil_prog_paths(pr)[id];
1482 213 return newSVpvn(stencil_prog_pool(pr) + p->full_off, p->full_len);
1483 }
1484  
1485 102 static SV *name_to_sv(pTHX_ const stencil_program *pr, uint32_t id)
1486 {
1487 102 const stencil_cname *n = &stencil_prog_names(pr)[id];
1488 102 return newSVpvn(stencil_prog_pool(pr) + n->off, n->len);
1489 }
1490  
1491 53 SV *stencil_program_inspect(pTHX_ const stencil_program *pr)
1492 {
1493 53 HV *top = newHV();
1494 53 AV *ops = newAV();
1495 53 const uint8_t *code = stencil_prog_code(pr);
1496 53 uint32_t pc = 0;
1497  
1498 473 ▶ while (pc < pr->code_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pc < pr->code_len) { 420 53 473
1499 420 uint8_t op = code[pc];
1500 420 HV *h = newHV();
1501 420 uint32_t at = pc;
1502 420 pc++;
1503 420 hv_store_lit(h, "at", newSVuv(at));
1504 420 ▶ hv_store_lit(h, "op", newSVpv(op < SOP__MAX ? op_names[op] : "?", 0));
Branch: 1 branch
BranchTrueFalseTotal
hv_store_lit(h, "op", newSVpv(op < SOP__MAX ? op_names[op] : "?", 0)); 420 0 420
1505 420 switch (op) {
1506 53 case SOP_LITERAL_SHORT: {
1507 53 uint8_t l = code[pc++];
1508 53 hv_store_lit(h, "len", newSVuv(l));
1509 53 hv_store_lit(h, "bytes",
1510 newSVpvn((const char *)code + pc, l));
1511 53 pc += l;
1512 53 break;
1513 }
1514 6 case SOP_LITERAL_LONG: case SOP_PUSH_LIT_STR: {
1515 uint32_t off, l;
1516 6 memcpy(&off, code + pc, 4); pc += 4;
1517 6 memcpy(&l, code + pc, 4); pc += 4;
1518 6 hv_store_lit(h, "off", newSVuv(off));
1519 6 hv_store_lit(h, "len", newSVuv(l));
1520 6 hv_store_lit(h, "bytes",
1521 newSVpvn(stencil_prog_pool(pr) + off, l));
1522 6 break;
1523 }
1524 2 case SOP_PUSH_LIT_NUM: {
1525 double d;
1526 2 memcpy(&d, code + pc, 8); pc += 8;
1527 2 hv_store_lit(h, "num", newSVnv(d));
1528 2 break;
1529 }
1530 139 case SOP_PUSH_PATH: {
1531 uint32_t id;
1532 139 memcpy(&id, code + pc, 4); pc += 4;
1533 139 hv_store_lit(h, "path_id", newSVuv(id));
1534 139 hv_store_lit(h, "path", path_to_sv(aTHX_ pr, id));
1535 139 break;
1536 }
1537 107 case SOP_TEST_JF: case SOP_TEST_JT: case SOP_JF_KEEP:
1538 case SOP_JT_KEEP: case SOP_JUMP: case SOP_FOR_NEXT: {
1539 uint32_t t;
1540 107 memcpy(&t, code + pc, 4); pc += 4;
1541 107 hv_store_lit(h, "target", newSVuv(t));
1542 107 break;
1543 }
1544 4 case SOP_FOR_ARY: {
1545 uint32_t id, t;
1546 4 memcpy(&id, code + pc, 4); pc += 4;
1547 4 memcpy(&t, code + pc, 4); pc += 4;
1548 4 hv_store_lit(h, "name", name_to_sv(aTHX_ pr, id));
1549 4 hv_store_lit(h, "target", newSVuv(t));
1550 4 break;
1551 }
1552 0 case SOP_FOR_HASH: {
1553 uint32_t k, v, t;
1554 0 memcpy(&k, code + pc, 4); pc += 4;
1555 0 memcpy(&v, code + pc, 4); pc += 4;
1556 0 memcpy(&t, code + pc, 4); pc += 4;
1557 0 hv_store_lit(h, "key", name_to_sv(aTHX_ pr, k));
1558 0 hv_store_lit(h, "val", name_to_sv(aTHX_ pr, v));
1559 0 hv_store_lit(h, "target", newSVuv(t));
1560 0 break;
1561 }
1562 3 case SOP_SET: {
1563 uint32_t id;
1564 3 memcpy(&id, code + pc, 4); pc += 4;
1565 3 hv_store_lit(h, "name", name_to_sv(aTHX_ pr, id));
1566 3 break;
1567 }
1568 2 case SOP_POP_BINDS: {
1569 uint32_t n;
1570 2 memcpy(&n, code + pc, 4); pc += 4;
1571 2 hv_store_lit(h, "count", newSVuv(n));
1572 2 break;
1573 }
1574 1 case SOP_INCLUDE: {
1575 uint32_t id;
1576 const stencil_cinc *inc;
1577 1 memcpy(&id, code + pc, 4); pc += 4;
1578 1 inc = &stencil_prog_incs(pr)[id];
1579 1 hv_store_lit(h, "file",
1580 newSVpvn(stencil_prog_pool(pr) + inc->name_off,
1581 inc->name_len));
1582 1 break;
1583 }
1584 3 case SOP_FILTER: {
1585 uint32_t id;
1586 const stencil_cfilt *f;
1587 3 memcpy(&id, code + pc, 4); pc += 4;
1588 3 f = &stencil_prog_filts(pr)[id];
1589 3 hv_store_lit(h, "filter",
1590 newSVpvn(stencil_prog_pool(pr) + f->name_off,
1591 f->name_len));
1592 3 hv_store_lit(h, "builtin_id", newSViv(f->builtin_id));
1593 3 ▶ if (f->has_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->has_arg) { 2 1 3
1594 2 ▶ if (f->arg_is_num)
Branch: 1 branch
BranchTrueFalseTotal
if (f->arg_is_num) 1 1 2
1595 1 hv_store_lit(h, "arg", newSVnv(f->num_arg));
1596 else
1597 1 hv_store_lit(h, "arg",
1598 newSVpvn(stencil_prog_pool(pr) + f->str_off,
1599 f->str_len));
1600 }
1601 3 break;
1602 }
1603 100 default:
1604 100 break;
1605 }
1606 420 av_push(ops, newRV_noinc((SV *)h));
1607 }
1608 53 hv_store_lit(top, "ops", newRV_noinc((SV *)ops));
1609  
1610 {
1611 53 AV *paths = newAV();
1612 uint32_t i, s;
1613 127 ▶ for (i = 0; i < pr->n_paths; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pr->n_paths; i++) { 74 53 127
1614 74 const stencil_cpath *p = &stencil_prog_paths(pr)[i];
1615 74 HV *ph = newHV();
1616 74 AV *segs = newAV();
1617 74 hv_store_lit(ph, "str", path_to_sv(aTHX_ pr, i));
1618 74 hv_store_lit(ph, "loop_rooted", newSViv(p->loop_rooted));
1619 172 ▶ for (s = 0; s < p->n_segs; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < p->n_segs; s++) { 98 74 172
1620 98 const stencil_seg *sg =
1621 98 &stencil_prog_segs(pr)[p->seg_idx + s];
1622 98 HV *sh = newHV();
1623 98 ▶ if (sg->is_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (sg->is_index) { 3 95 98
1624 3 hv_store_lit(sh, "index", newSViv((IV)sg->index));
1625 } else {
1626 95 const stencil_cname *nm =
1627 95 &stencil_prog_names(pr)[sg->name_id];
1628 95 hv_store_lit(sh, "name", name_to_sv(aTHX_ pr,
1629 sg->name_id));
1630 95 hv_store_lit(sh, "hash", newSVuv(nm->hash));
1631 }
1632 98 av_push(segs, newRV_noinc((SV *)sh));
1633 }
1634 74 hv_store_lit(ph, "segs", newRV_noinc((SV *)segs));
1635 74 av_push(paths, newRV_noinc((SV *)ph));
1636 }
1637 53 hv_store_lit(top, "paths", newRV_noinc((SV *)paths));
1638 }
1639 {
1640 53 AV *incs = newAV();
1641 uint32_t i;
1642 54 ▶ for (i = 0; i < pr->n_incs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pr->n_incs; i++) { 1 53 54
1643 1 const stencil_cinc *inc = &stencil_prog_incs(pr)[i];
1644 1 av_push(incs, newSVpvn(stencil_prog_pool(pr) + inc->name_off,
1645 inc->name_len));
1646 }
1647 53 hv_store_lit(top, "includes", newRV_noinc((SV *)incs));
1648 }
1649  
1650 53 hv_store_lit(top, "max_stack", newSVuv(pr->max_stack));
1651 53 hv_store_lit(top, "max_frames", newSVuv(pr->max_frames));
1652 53 hv_store_lit(top, "max_binds", newSVuv(pr->max_binds));
1653 53 hv_store_lit(top, "code_len", newSVuv(pr->code_len));
1654 53 hv_store_lit(top, "n_lines", newSVuv(pr->n_lines));
1655 53 hv_store_lit(top, "is_wrapper",
1656 newSViv(!!(pr->flags & STENCIL_PROG_IS_WRAPPER)));
1657  
1658 53 return newRV_noinc((SV *)top);
1659 }
« src/stencil_render.c ↑ Summary src/stencil_filters.c »