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 71380 static uint32_t ccol(const stencil_compiler *c, const char *at)
77 {
78 71380 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 408035 static int emit_bytes(stencil_compiler *c, const void *p, size_t n)
118 {
119 408035 uint32_t off = stencil_arena_alloc(&c->code, n, 1);
120 408035 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 408035 408035
121 0 return coom(c);
122 408035 memcpy(c->code.base + off, p, n);
123 408035 return 1;
124 }
125  
126 268522 static int emit_u8(stencil_compiler *c, uint8_t v)
127 {
128 268522 return emit_bytes(c, &v, 1);
129 }
130  
131 75094 static int emit_u32(stencil_compiler *c, uint32_t v)
132 {
133 75094 return emit_bytes(c, &v, 4);
134 }
135  
136 3314 static void patch_u32(stencil_compiler *c, uint32_t at, uint32_t v)
137 {
138 3314 memcpy(c->code.base + at, &v, 4);
139 3314 }
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 16092 static uint32_t code_pos(const stencil_compiler *c)
149 {
150 16092 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 204200 static int record_line(stencil_compiler *c)
156 {
157 stencil_cline *e;
158 uint32_t off;
159 204200 ▶ if (c->line == c->last_line_rec)
Branch: 1 branch
BranchTrueFalseTotal
if (c->line == c->last_line_rec) 201436 2764 204200
160 201436 return 1;
161 2764 off = stencil_arena_alloc(&c->lines, sizeof(stencil_cline),
162 sizeof(uint32_t));
163 2764 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 2764 2764
164 0 return coom(c);
165 2764 e = (stencil_cline *)(c->lines.base + off);
166 2764 e->off = code_pos(c);
167 2764 e->line = c->line;
168 2764 c->n_lines++;
169 2764 c->last_line_rec = c->line;
170 2764 return 1;
171 }
172  
173 204200 static int emit_op(stencil_compiler *c, uint8_t op, int32_t stack_delta)
174 {
175 204200 ▶ if (!record_line(c) || !emit_u8(c, op))
Branch: 2 branches
BranchTrueFalseTotal
if (!record_line(c) || !emit_u8(c, op)) 204200 0 204200
if (!record_line(c) || !emit_u8(c, op)) 0 204200 204200
176 0 return 0;
177 204200 c->cur_stack += stack_delta;
178 204200 ▶ if (c->cur_stack > c->max_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_stack > c->max_stack) 2128 202072 204200
179 2128 c->max_stack = c->cur_stack;
180 204200 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 5149 static void patch_chain(stencil_compiler *c, uint32_t chain,
199 uint32_t target)
200 {
201 5269 ▶ while (chain != STENCIL_PATCH_END) {
Branch: 1 branch
BranchTrueFalseTotal
while (chain != STENCIL_PATCH_END) { 120 5149 5269
202 120 uint32_t next = read_code_u32(c, chain);
203 120 patch_u32(c, chain, target);
204 120 chain = next;
205 }
206 5149 }
207  
208 /* ---- pending-literal accumulation and flush ------------------------- */
209  
210 73647 static int lit_pend(stencil_compiler *c, const char *p, size_t n)
211 {
212 uint32_t off;
213 73647 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 8982 64665 73647
214 8982 return 1;
215 64665 off = stencil_arena_alloc(&c->pend, n, 1);
216 64665 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 64665 64665
217 0 return coom(c);
218 64665 memcpy(c->pend.base + off, p, n);
219 64665 return 1;
220 }
221  
222 73545 static int lit_flush(stencil_compiler *c)
223 {
224 73545 size_t n = c->pend.used;
225 73545 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 8937 64608 73545
226 8937 return 1;
227 64608 ▶ if (n <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 31) { 64322 286 64608
228 128644 if (!emit_op(c, SOP_LITERAL_SHORT, 0) ||
229 128644 ▶ !emit_u8(c, (uint8_t)n) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_u8(c, (uint8_t)n) || 0 64322 64322
230 64322 !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 64608 c->pend.used = 0;
242 64608 return 1;
243 }
244  
245 /* ---- source advance with line tracking ------------------------------- */
246  
247 73640 static void advance_lines(stencil_compiler *c, const char *from,
248 const char *to)
249 {
250 73640 const char *nl = from;
251 for (;;) {
252 75346 nl = (const char *)memchr(nl, '\n', (size_t)(to - nl));
253 74493 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 73640 853 74493
254 73640 break;
255 853 c->line++;
256 853 nl++;
257 853 c->line_start = nl;
258 }
259 73640 }
260  
261 /* ---- token helpers --------------------------------------------------- */
262  
263 #define IS_WORD_START(ch) (isALPHA(ch) || (ch) == '_')
264 #define IS_WORD(ch) (isALNUM(ch) || (ch) == '_')
265  
266 221232 static void skip_ws(stencil_compiler *c)
267 {
268 370721 ▶ while (c->p < c->end && isSPACE(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isSPACE(*c->p)) { 370709 12 370721
while (c->p < c->end && isSPACE(*c->p)) { 149489 221220 370709
269 149489 ▶ if (*c->p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '\n') { 9 149480 149489
270 9 c->line++;
271 9 c->line_start = c->p + 1;
272 }
273 149489 c->p++;
274 }
275 221232 }
276  
277 213245 static size_t word_len(const char *p, const char *end)
278 {
279 213245 const char *q = p;
280 213245 ▶ if (q >= end || !IS_WORD_START(*q))
Branch: 3 branches
BranchTrueFalseTotal
if (q >= end || !IS_WORD_START(*q)) 213240 5 213245
if (q >= end || !IS_WORD_START(*q)) 5522 207718 213240
if (q >= end || !IS_WORD_START(*q)) 5522 0 5522
281 5527 return 0;
282 207718 q++;
283 789425 ▶ while (q < end && IS_WORD(*q))
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && IS_WORD(*q)) 789422 3 789425
while (q < end && IS_WORD(*q)) 581707 207715 789422
while (q < end && IS_WORD(*q)) 0 207715 207715
284 581707 q++;
285 207718 return (size_t)(q - p);
286 }
287  
288 698724 static int word_is(const char *p, size_t n, const char *kw)
289 {
290 698724 ▶ return strlen(kw) == n && memcmp(p, kw, n) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return strlen(kw) == n && memcmp(p, kw, n) == 0; 73108 625616 698724
return strlen(kw) == n && memcmp(p, kw, n) == 0; 8803 64305 73108
291 }
292  
293 1787 static int is_keyword(const char *p, size_t n)
294 {
295 int i;
296 21436 ▶ for (i = 0; kw_list[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; kw_list[i]; i++) 19652 1784 21436
297 19652 ▶ if (word_is(p, n, kw_list[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, kw_list[i])) 3 19649 19652
298 3 return 1;
299 1784 return 0;
300 }
301  
302 70864 static int expect_close(stencil_compiler *c)
303 {
304 70864 skip_ws(c);
305 70864 ▶ 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] == '}') { 70858 6 70864
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 70837 21 70858
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 70816 21 70837
306 70816 c->p += 2;
307 70816 return 1;
308 }
309 48 return cerror(c, c->p, "expected '%%}'");
310 }
311  
312 /* ---- name / path interning ------------------------------------------ */
313  
314 131353 static uint32_t name_intern(stencil_compiler *c, const char *p, uint32_t n)
315 {
316 131353 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 217318 ▶ for (i = 0; i < c->n_names; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_names; i++) 208934 8384 217318
323 208934 ▶ if (tab[i].len == n
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].len == n 189333 19601 208934
324 189333 ▶ && memcmp(c->pool.base + tab[i].off, p, n) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(c->pool.base + tab[i].off, p, n) == 0) 122969 66364 189333
325 122969 return i;
326 8384 off = stencil_arena_alloc(&c->pool, n, 1);
327 8384 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 8384 8384
328 0 return STENCIL_ARENA_NULL;
329 8384 memcpy(c->pool.base + off, p, n);
330 8384 ▶ PERL_HASH(hash, p, n);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, p, n); 8384 0 8384
331 8384 noff = stencil_arena_alloc(&c->names, sizeof(stencil_cname),
332 sizeof(uint32_t));
333 8384 ▶ if (noff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (noff == STENCIL_ARENA_NULL) 0 8384 8384
334 0 return STENCIL_ARENA_NULL;
335 8384 e = (stencil_cname *)(c->names.base + noff);
336 8384 e->off = off;
337 8384 e->len = n;
338 8384 e->hash = hash;
339 8384 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 67459 static uint32_t path_parse(stencil_compiler *c)
345 {
346 67459 const char *start = c->p;
347 stencil_seg segs[STENCIL_MAX_SEGS];
348 67459 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 129614 ▶ if (n_segs >= STENCIL_MAX_SEGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_segs >= STENCIL_MAX_SEGS) { 1 129613 129614
355 1 cerror(c, c->p, "path has more than %d segments",
356 STENCIL_MAX_SEGS);
357 1 return STENCIL_ARENA_NULL;
358 }
359 129613 ▶ if (c->p < c->end && *c->p == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') { 129612 1 129613
if (c->p < c->end && *c->p == '[') { 15 129597 129612
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 129598 size_t wl = word_len(c->p, c->end);
383 uint32_t id;
384 129598 ▶ if (!wl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) { 19 129579 129598
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 129579 id = name_intern(c, c->p, (uint32_t)wl);
390 129579 ▶ if (id == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (id == STENCIL_ARENA_NULL) { 0 129579 129579
391 0 coom(c);
392 0 return STENCIL_ARENA_NULL;
393 }
394 129579 segs[n_segs].name_id = id;
395 129579 segs[n_segs].is_index = 0;
396 129579 segs[n_segs].index = 0;
397 129579 n_segs++;
398 129579 c->p += wl;
399 }
400 129592 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 129590 2 129592
if (c->p < c->end && *c->p == '.') { 62140 67450 129590
401 62140 c->p++;
402 62140 continue;
403 }
404 67452 ▶ if (c->p < c->end && *c->p == '[')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') 67450 2 67452
if (c->p < c->end && *c->p == '[') 15 67435 67450
405 15 continue;
406 67437 break;
407 }
408  
409 /* dedup by the normalised source slice */
410 67437 full_len = (size_t)(c->p - start);
411 67437 tab = (stencil_cpath *)c->paths.base;
412 78416 ▶ for (i = 0; i < c->n_paths; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_paths; i++) 71638 6778 78416
413 71638 ▶ if (tab[i].full_len == full_len
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].full_len == full_len 62549 9089 71638
414 62549 ▶ && 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) 60659 1890 62549
415 60659 return i;
416  
417 6778 full_off = stencil_intern_get(&c->lit_intern, &c->pool, start,
418 (uint32_t)full_len);
419 6778 ▶ if (full_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (full_off == STENCIL_ARENA_NULL) { 0 6778 6778
420 0 coom(c);
421 0 return STENCIL_ARENA_NULL;
422 }
423 6778 seg_base = c->n_segs;
424 15188 ▶ for (i = 0; i < n_segs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_segs; i++) { 8410 6778 15188
425 8410 uint32_t soff = stencil_arena_alloc(&c->segs, sizeof(stencil_seg),
426 sizeof(SSize_t));
427 8410 ▶ if (soff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (soff == STENCIL_ARENA_NULL) { 0 8410 8410
428 0 coom(c);
429 0 return STENCIL_ARENA_NULL;
430 }
431 8410 memcpy(c->segs.base + soff, &segs[i], sizeof(stencil_seg));
432 8410 c->n_segs++;
433 }
434 6778 poff = stencil_arena_alloc(&c->paths, sizeof(stencil_cpath),
435 sizeof(uint32_t));
436 6778 ▶ if (poff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (poff == STENCIL_ARENA_NULL) { 0 6778 6778
437 0 coom(c);
438 0 return STENCIL_ARENA_NULL;
439 }
440 6778 e = (stencil_cpath *)(c->paths.base + poff);
441 {
442 6778 const stencil_cname *names = (const stencil_cname *)c->names.base;
443 13556 e->loop_rooted = !segs[0].is_index
444 6778 ▶ && names[segs[0].name_id].len == 4
Branch: 1 branch
BranchTrueFalseTotal
&& names[segs[0].name_id].len == 4 1549 5229 6778
445 13556 ▶ && memcmp(c->pool.base + names[segs[0].name_id].off,
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->pool.base + names[segs[0].name_id].off, 6778 0 6778
&& memcmp(c->pool.base + names[segs[0].name_id].off, 132 1417 1549
446 "loop", 4) == 0;
447 }
448 6778 e->n_segs = (uint16_t)n_segs;
449 6778 e->seg_idx = seg_base;
450 6778 e->full_off = full_off;
451 6778 e->full_len = (uint32_t)full_len;
452 6778 return c->n_paths++;
453 }
454  
455 /* parse a path and emit its push */
456 67459 static int parse_path_push(stencil_compiler *c)
457 {
458 67459 uint32_t pid = path_parse(c);
459 67459 ▶ if (pid == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pid == STENCIL_ARENA_NULL) 22 67437 67459
460 22 return 0;
461 67437 ▶ 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); 67437 0 67437
return emit_op(c, SOP_PUSH_PATH, +1) && emit_u32(c, pid); 67437 0 67437
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 1887 static int parse_primary(stencil_compiler *c)
472 {
473 1887 skip_ws(c);
474 1887 ▶ if (c->p >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p >= c->end) 1 1886 1887
475 1 return cerror(c, c->p, "expected an expression");
476  
477 1886 ▶ if (*c->p == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '(') { 4 1882 1886
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 1882 ▶ if (*c->p == '\'' || *c->p == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (*c->p == '\'' || *c->p == '"') { 1866 16 1882
if (*c->p == '\'' || *c->p == '"') { 13 1853 1866
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 1853 ▶ if (isDIGIT(*c->p)
Branch: 1 branch
BranchTrueFalseTotal
if (isDIGIT(*c->p) 1758 95 1853
537 1758 ▶ || (*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 1756 1758
|| (*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 1756 size_t wl = word_len(c->p, c->end);
563 1756 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 20 1736 1756
564 20 return cerror(c, c->p, "expected an expression");
565 1736 ▶ if (word_is(c->p, wl, "undef")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "undef")) { 5 1731 1736
566 5 c->p += wl;
567 5 return emit_op(c, SOP_PUSH_UNDEF, +1);
568 }
569 1731 ▶ if (word_is(c->p, wl, "defined")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "defined")) { 9 1722 1731
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 1722 return parse_path_push(c);
586 }
587 }
588  
589 /* returns the comparison opcode at c->p (consuming it) or 0 */
590 1802 static uint8_t scan_cmp_op(stencil_compiler *c)
591 {
592 1802 const char *p = c->p;
593 1802 size_t left = (size_t)(c->end - p);
594 size_t wl;
595 1802 ▶ if (left >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 2) { 1802 0 1802
596 1802 ▶ 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 1781 1802
if (p[0] == '=' && p[1] == '=') { c->p += 2; return SOP_EQ_NUM; } 21 0 21
597 1781 ▶ 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 1779 1781
if (p[0] == '!' && p[1] == '=') { c->p += 2; return SOP_NE_NUM; } 2 0 2
598 1779 ▶ 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 1774 1779
if (p[0] == '<' && p[1] == '=') { c->p += 2; return SOP_LE_NUM; } 2 3 5
599 1777 ▶ 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 1770 1777
if (p[0] == '>' && p[1] == '=') { c->p += 2; return SOP_GE_NUM; } 4 3 7
600 }
601 1773 ▶ if (left >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 1) { 1773 0 1773
602 1773 ▶ 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 1770 1773
603 1770 ▶ 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 1767 1770
604 }
605 1767 wl = word_len(p, c->end);
606 1767 ▶ if (wl == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (wl == 2) { 28 1739 1767
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 1741 return 0;
615 }
616  
617 1826 static int parse_cmp(stencil_compiler *c)
618 {
619 uint8_t op;
620 1826 ▶ if (!parse_primary(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_primary(c)) 24 1802 1826
621 24 return 0;
622 1802 skip_ws(c);
623 1802 op = scan_cmp_op(c);
624 1802 ▶ if (!op)
Branch: 1 branch
BranchTrueFalseTotal
if (!op) 1741 61 1802
625 1741 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 1826 static int parse_not(stencil_compiler *c)
632 {
633 1826 int negs = 0;
634 for (;;) {
635 1833 skip_ws(c);
636 1833 ▶ if (c->p < c->end && *c->p == '!'
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '!' 1832 1 1833
if (c->p < c->end && *c->p == '!' 3 1829 1832
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 1830 size_t wl = word_len(c->p, c->end);
644 1830 ▶ if (wl && word_is(c->p, wl, "not")) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, "not")) { 1717 113 1830
if (wl && word_is(c->p, wl, "not")) { 4 1713 1717
645 4 c->p += wl;
646 4 negs++;
647 4 continue;
648 }
649 }
650 1826 break;
651 }
652 1826 ▶ if (!parse_cmp(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_cmp(c)) 26 1800 1826
653 26 return 0;
654 1807 ▶ while (negs--)
Branch: 1 branch
BranchTrueFalseTotal
while (negs--) 7 1800 1807
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 1800 return 1;
658 }
659  
660 /* and-op at c->p? consume and return true */
661 3587 static int scan_word_op(stencil_compiler *c, const char *sym,
662 const char *word)
663 {
664 size_t wl;
665 3587 ▶ 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] 3587 0 3587
if ((size_t)(c->end - c->p) >= 2 && c->p[0] == sym[0] 24 3563 3587
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 3564 wl = word_len(c->p, c->end);
671 3564 ▶ if (wl && word_is(c->p, wl, word)) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, word)) { 5 3559 3564
if (wl && word_is(c->p, wl, word)) { 3 2 5
672 3 c->p += wl;
673 3 return 1;
674 }
675 3561 return 0;
676 }
677  
678 1813 static int parse_and(stencil_compiler *c)
679 {
680 1813 uint32_t chain = STENCIL_PATCH_END;
681 1813 ▶ if (!parse_not(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_not(c)) 1787 26 1813
682 26 return 0;
683 for (;;) {
684 1800 skip_ws(c);
685 1800 ▶ if (!scan_word_op(c, "&&", "and"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "&&", "and")) 1787 13 1800
686 1787 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 1787 patch_chain(c, chain, code_pos(c));
693 1787 return 1;
694 }
695  
696 1800 static int parse_or(stencil_compiler *c)
697 {
698 1800 uint32_t chain = STENCIL_PATCH_END;
699 1800 ▶ if (!parse_and(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_and(c)) 1774 26 1800
700 26 return 0;
701 for (;;) {
702 1787 skip_ws(c);
703 1787 ▶ if (!scan_word_op(c, "||", "or"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "||", "or")) 1774 13 1787
704 1774 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 1774 patch_chain(c, chain, code_pos(c));
711 1774 return 1;
712 }
713  
714 1800 static int parse_expr(stencil_compiler *c)
715 {
716 1800 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 64110 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 64276 skip_ws(c);
799 64276 ▶ if (c->p >= c->end || *c->p != '|')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '|') 64274 2 64276
if (c->p >= c->end || *c->p != '|') 64092 182 64274
800 64110 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 3292 static stencil_blk *blk_open(stencil_compiler *c, uint8_t kind,
916 uint32_t line, uint32_t col)
917 {
918 stencil_blk *b;
919 3292 ▶ if (c->n_blk >= STENCIL_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk >= STENCIL_MAX_DEPTH) { 1 3291 3292
920 1 cerror_at(c, line, col, "blocks nested deeper than %d",
921 STENCIL_MAX_DEPTH);
922 1 return NULL;
923 }
924 3291 b = &c->blk[c->n_blk++];
925 3291 memset(b, 0, sizeof *b);
926 3291 b->kind = kind;
927 3291 b->line = line;
928 3291 b->col = col;
929 3291 b->cond_patch = STENCIL_PATCH_END;
930 3291 b->jump_chain = STENCIL_PATCH_END;
931 3291 b->binds_at_entry = c->cur_binds;
932 3291 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 3280 static int emit_pop_binds(stencil_compiler *c, stencil_blk *b)
944 {
945 3280 int32_t delta = c->cur_binds - b->binds_at_entry;
946 3280 ▶ if (delta <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delta <= 0) 3209 71 3280
947 3209 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 1694 static int stmt_if(stencil_compiler *c, uint8_t kind, uint32_t line,
953 uint32_t col)
954 {
955 1694 stencil_blk *b = blk_open(c, kind, line, col);
956 1694 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 1 1693 1694
957 1 return 0;
958 1693 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 1667 26 1693
if (!parse_expr(c) || !expect_close(c)) 8 1659 1667
959 34 return 0;
960 1659 ▶ 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 1651 1659
if (!emit_op(c, kind == BLK_UNLESS ? SOP_TEST_JT : SOP_TEST_JF, -1)) 0 1659 1659
961 0 return 0;
962 1659 b->cond_patch = code_pos(c);
963 1659 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 3220 static int stmt_end(stencil_compiler *c, uint32_t line, uint32_t col)
1013 {
1014 stencil_blk *b;
1015 3220 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 36 3184 3220
1016 36 return cerror_at(c, line, col, "'end' with no open block");
1017 3184 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 3184 3184
1018 0 return 0;
1019 3184 b = &c->blk[--c->n_blk];
1020 3184 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 3184 3184
1021 0 return 0;
1022 3184 ▶ if (b->kind == BLK_FOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_FOR) { 1596 1588 3184
1023 /* the pops sit before FOR_NEXT so every iteration gets a fresh
1024 * bind scope */
1025 1596 ▶ 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)) 1596 0 1596
if (!emit_op(c, SOP_FOR_NEXT, 0) || !emit_u32(c, b->body_start)) 0 1596 1596
1026 0 return 0;
1027 1596 patch_u32(c, b->for_end_patch, code_pos(c));
1028 1596 c->cur_frames--;
1029 } else {
1030 1588 ▶ if (b->cond_patch != STENCIL_PATCH_END)
Branch: 1 branch
BranchTrueFalseTotal
if (b->cond_patch != STENCIL_PATCH_END) 1502 86 1588
1031 1502 patch_u32(c, b->cond_patch, code_pos(c));
1032 1588 patch_chain(c, b->jump_chain, code_pos(c));
1033 }
1034 3184 return 1;
1035 }
1036  
1037 1630 static int stmt_for(stencil_compiler *c, uint32_t line, uint32_t col)
1038 {
1039 1630 const char *n1, *n2 = NULL;
1040 1630 size_t l1, l2 = 0;
1041 1630 uint32_t id1, id2 = 0;
1042 stencil_blk *b;
1043  
1044 1630 skip_ws(c);
1045 1630 l1 = word_len(c->p, c->end);
1046 1630 ▶ if (!l1 || is_keyword(c->p, l1))
Branch: 2 branches
BranchTrueFalseTotal
if (!l1 || is_keyword(c->p, l1)) 1604 26 1630
if (!l1 || is_keyword(c->p, l1)) 1 1603 1604
1047 27 return cerror(c, c->p, "expected a loop variable name");
1048 1603 n1 = c->p;
1049 1603 c->p += l1;
1050 1603 skip_ws(c);
1051 1603 ▶ if (c->p < c->end && *c->p == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ',') { 1603 0 1603
if (c->p < c->end && *c->p == ',') { 81 1522 1603
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 1603 size_t wl = word_len(c->p, c->end);
1063 1603 ▶ if (!wl || !word_is(c->p, wl, "in"))
Branch: 2 branches
BranchTrueFalseTotal
if (!wl || !word_is(c->p, wl, "in")) 1599 4 1603
if (!wl || !word_is(c->p, wl, "in")) 1 1598 1599
1064 5 return cerror(c, c->p, "expected 'in'");
1065 1598 c->p += wl;
1066 }
1067 1598 skip_ws(c);
1068 1598 ▶ if (!parse_path_push(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_path_push(c)) 0 1598 1598
1069 0 return 0;
1070 1598 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 1598 1598
1071 0 return 0;
1072  
1073 1598 id1 = name_intern(c, n1, (uint32_t)l1);
1074 1598 ▶ if (id1 == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id1 == STENCIL_ARENA_NULL) 0 1598 1598
1075 0 return coom(c);
1076 1598 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 81 1517 1598
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 1598 b = blk_open(c, BLK_FOR, line, col);
1083 1598 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 0 1598 1598
1084 0 return 0;
1085 1598 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 81 1517 1598
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 1517 ▶ 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)) 1517 0 1517
if (!emit_op(c, SOP_FOR_ARY, -1) || !emit_u32(c, id1)) 0 1517 1517
1091 0 return 0;
1092 }
1093 1598 b->for_end_patch = code_pos(c);
1094 1598 ▶ if (!emit_u32(c, STENCIL_PATCH_END))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_u32(c, STENCIL_PATCH_END)) 0 1598 1598
1095 0 return 0;
1096 1598 b->body_start = code_pos(c);
1097 1598 c->cur_frames++;
1098 1598 ▶ if (c->cur_frames > c->max_frames)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_frames > c->max_frames) 1529 69 1598
1099 1529 c->max_frames = c->cur_frames;
1100 1598 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 2742 static int scan_template(stencil_compiler *c)
1181 {
1182 70873 for (;;) {
1183 73615 const char *run = c->p;
1184 73615 const char *q = c->p;
1185  
1186 /* literal run up to the next real '{%' */
1187 for (;;) {
1188 73719 q = stencil_dispatch.scan(q, c->end);
1189 73719 ▶ if (q == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q == c->end) 2432 71287 73719
1190 2432 break;
1191 71287 ▶ if (q + 1 < c->end && q[1] == '%')
Branch: 2 branches
BranchTrueFalseTotal
if (q + 1 < c->end && q[1] == '%') 71286 1 71287
if (q + 1 < c->end && q[1] == '%') 71183 103 71286
1192 71183 break;
1193 104 q++;
1194 }
1195 73615 ▶ if (!lit_pend(c, run, (size_t)(q - run)))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_pend(c, run, (size_t)(q - run))) 0 73615 73615
1196 0 return 0;
1197 73615 advance_lines(c, run, q);
1198 73615 c->p = q;
1199 73615 ▶ if (c->p == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p == c->end) 2432 71183 73615
1200 2432 break;
1201  
1202 /* at '{%' */
1203 {
1204 71183 uint32_t tag_line = c->line;
1205 71183 uint32_t tag_col = ccol(c, c->p);
1206 71183 const char *tag_at = c->p;
1207 71183 c->p += 2;
1208  
1209 /* comment */
1210 71183 ▶ if (c->p < c->end && *c->p == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '#') { 71182 1 71183
if (c->p < c->end && *c->p == '#') { 38 71144 71182
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 71145 skip_ws(c);
1228  
1229 /* empty tag = literal '{%' escape */
1230 71145 ▶ 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] == '}') { 71144 1 71145
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 32 71112 71144
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 71113 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 71113 71113
1238 0 return 0;
1239  
1240 {
1241 71113 size_t wl = word_len(c->p, c->end);
1242 71113 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 27 71086 71113
1243 27 return cerror(c, c->p, "expected a name or keyword");
1244  
1245 71086 ▶ if (word_is(c->p, wl, "if")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "if")) { 1686 69400 71086
1246 1686 c->p += wl;
1247 1686 ▶ 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 1651 1686
1248 35 return 0;
1249 69400 ▶ } else if (word_is(c->p, wl, "unless")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "unless")) { 8 69392 69400
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 69392 ▶ } else if (word_is(c->p, wl, "elsif")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "elsif")) { 40 69352 69392
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 69352 ▶ } else if (word_is(c->p, wl, "else")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "else")) { 119 69233 69352
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 69233 ▶ } else if (word_is(c->p, wl, "end")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "end")) { 3220 66013 69233
1262 3220 c->p += wl;
1263 3220 ▶ if (!stmt_end(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_end(c, tag_line, tag_col)) 36 3184 3220
1264 36 return 0;
1265 66013 ▶ } else if (word_is(c->p, wl, "for")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "for")) { 1630 64383 66013
1266 1630 c->p += wl;
1267 1630 ▶ if (!stmt_for(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_for(c, tag_line, tag_col)) 32 1598 1630
1268 32 return 0;
1269 64383 ▶ } else if (word_is(c->p, wl, "set")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "set")) { 121 64262 64383
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 64262 ▶ } else if (word_is(c->p, wl, "include")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "include")) { 64 64198 64262
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 64198 ▶ } else if (word_is(c->p, wl, "content")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "content")) { 67 64131 64198
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 64131 ▶ } else if (word_is(c->p, wl, "raw")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "raw")) { 36 64095 64131
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 64095 ▶ if (!parse_path_push(c) || !parse_filters(c)
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_path_push(c) || !parse_filters(c) 64091 4 64095
if (!parse_path_push(c) || !parse_filters(c) 64076 15 64091
1295 64076 ▶ || !expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
|| !expect_close(c)) 6 64070 64076
1296 25 return 0;
1297 64070 ▶ if (!emit_op(c, SOP_PRINT_ESC, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_PRINT_ESC, -1)) 0 64070 64070
1298 0 return 0;
1299 }
1300 }
1301 (void)tag_at;
1302 }
1303 }
1304  
1305 2432 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 2432 2432
1306 0 return 0;
1307 2432 ▶ if (c->n_blk) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk) { 5 2427 2432
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 2427 return emit_op(c, SOP_END, 0);
1313 }
1314  
1315 /* ====================================================================
1316 * Compile driver: pack regions into the final arena
1317 * ==================================================================== */
1318  
1319 2742 static void compiler_free_temps(stencil_compiler *c)
1320 {
1321 2742 stencil_arena_free(&c->code);
1322 2742 stencil_arena_free(&c->pool);
1323 2742 stencil_arena_free(&c->names);
1324 2742 stencil_arena_free(&c->paths);
1325 2742 stencil_arena_free(&c->segs);
1326 2742 stencil_arena_free(&c->incs);
1327 2742 stencil_arena_free(&c->filts);
1328 2742 stencil_arena_free(&c->lines);
1329 2742 stencil_arena_free(&c->pend);
1330 2742 stencil_intern_free(&c->lit_intern);
1331 2742 }
1332  
1333 19416 static uint32_t pack_region(stencil_arena *dst, const stencil_arena *src,
1334 size_t align)
1335 {
1336 19416 ▶ 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, 13420 5996 19416
1337 align);
1338 19416 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 19416 19416
1339 0 return off;
1340 19416 ▶ if (src->used)
Branch: 1 branch
BranchTrueFalseTotal
if (src->used) 13420 5996 19416
1341 13420 memcpy(dst->base + off, src->base, src->used);
1342 19416 return off;
1343 }
1344  
1345 2742 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 2742 stencil_program *prog = NULL;
1351 int ok;
1352  
1353 2742 memset(&c, 0, sizeof c);
1354 2742 c.flags = flags & STENCIL_PROG_SRC_UTF8;
1355 2742 c.src = c.p = c.line_start = src;
1356 2742 c.end = src + len;
1357 2742 c.name = name;
1358 2742 c.line = 1;
1359 2742 c.last_line_rec = 0;
1360 #ifdef PERL_IMPLICIT_CONTEXT
1361 c.perl = aTHX;
1362 #endif
1363  
1364 5484 if (!stencil_arena_init(&c.code, 256) ||
1365 5484 ▶ !stencil_arena_init(&c.pool, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pool, 256) || 2742 0 2742
1366 5484 ▶ !stencil_arena_init(&c.names, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.names, 256) || 2742 0 2742
1367 5484 ▶ !stencil_arena_init(&c.paths, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.paths, 256) || 2742 0 2742
1368 5484 ▶ !stencil_arena_init(&c.segs, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.segs, 256) || 2742 0 2742
1369 5484 ▶ !stencil_arena_init(&c.incs, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.incs, 64) || 2742 0 2742
1370 5484 ▶ !stencil_arena_init(&c.filts, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.filts, 64) || 2742 0 2742
1371 5484 ▶ !stencil_arena_init(&c.lines, 128) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.lines, 128) || 2742 0 2742
1372 5484 ▶ !stencil_arena_init(&c.pend, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pend, 256) || 0 2742 2742
1373 2742 !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 2742 ok = scan_template(&c);
1381  
1382 2742 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 2427 315 2742
1383 2427 prog = (stencil_program *)calloc(1, sizeof *prog);
1384 2427 ▶ if (!prog) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) { 0 2427 2427
1385 0 ok = 0;
1386 0 cerror_at(&c, c.line, 1, "out of memory");
1387 }
1388 }
1389 2742 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 2427 315 2742
1390 2427 size_t total = c.code.used + c.pool.used + c.names.used
1391 2427 + c.paths.used + c.segs.used + c.incs.used
1392 2427 + c.filts.used + c.lines.used + 128;
1393 2427 ▶ if (!stencil_arena_init(&prog->arena, total)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_arena_init(&prog->arena, total)) { 0 2427 2427
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 2742 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 2427 315 2742
1401 2427 prog->code_off = pack_region(&prog->arena, &c.code, 1);
1402 2427 prog->pool_off = pack_region(&prog->arena, &c.pool, 1);
1403 2427 prog->names_off = pack_region(&prog->arena, &c.names,
1404 sizeof(uint32_t));
1405 2427 prog->paths_off = pack_region(&prog->arena, &c.paths,
1406 sizeof(uint32_t));
1407 2427 prog->segs_off = pack_region(&prog->arena, &c.segs,
1408 sizeof(SSize_t));
1409 2427 prog->incs_off = pack_region(&prog->arena, &c.incs,
1410 sizeof(uint32_t));
1411 2427 prog->filts_off = pack_region(&prog->arena, &c.filts,
1412 sizeof(double));
1413 2427 prog->lines_off = pack_region(&prog->arena, &c.lines,
1414 sizeof(uint32_t));
1415 2427 ▶ if (prog->code_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
if (prog->code_off == STENCIL_ARENA_NULL 2427 0 2427
1416 2427 ▶ || prog->pool_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->pool_off == STENCIL_ARENA_NULL 2427 0 2427
1417 2427 ▶ || prog->names_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->names_off == STENCIL_ARENA_NULL 2427 0 2427
1418 2427 ▶ || prog->paths_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->paths_off == STENCIL_ARENA_NULL 2427 0 2427
1419 2427 ▶ || prog->segs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->segs_off == STENCIL_ARENA_NULL 2427 0 2427
1420 2427 ▶ || prog->incs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->incs_off == STENCIL_ARENA_NULL 2427 0 2427
1421 2427 ▶ || prog->filts_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->filts_off == STENCIL_ARENA_NULL 2427 0 2427
1422 2427 ▶ || prog->lines_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
|| prog->lines_off == STENCIL_ARENA_NULL) { 0 2427 2427
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 2742 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 2427 315 2742
1430 2427 prog->code_len = (uint32_t)c.code.used;
1431 2427 prog->pool_len = (uint32_t)c.pool.used;
1432 2427 prog->n_names = c.n_names;
1433 2427 prog->n_paths = c.n_paths;
1434 2427 prog->n_segs = c.n_segs;
1435 2427 prog->n_incs = c.n_incs;
1436 2427 prog->n_filts = c.n_filts;
1437 2427 prog->n_lines = c.n_lines;
1438 2427 prog->max_stack = (uint32_t)c.max_stack;
1439 2427 prog->max_frames = (uint32_t)c.max_frames;
1440 2427 prog->max_binds = (uint32_t)c.max_binds;
1441 2427 prog->flags = c.flags;
1442 2427 stencil_arena_finalise(&prog->arena);
1443 }
1444  
1445 2742 compiler_free_temps(&c);
1446  
1447 2742 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 315 2427 2742
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 2427 return prog;
1456 }
1457  
1458 2480 void stencil_program_free(stencil_program *prog)
1459 {
1460 2480 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 53 2427 2480
1461 53 return;
1462 2427 stencil_arena_free(&prog->arena);
1463 2427 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 »