src/stencil_compile.c

filter: Statementstmt 91.6% 1501 / 1637 Branchbran 78.8% 987 / 1252 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.1% 2488 / 2889 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Stencil.xs ↑ Summary src/stencil_render.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 #define BLK_SWITCH 4
19  
20 #define STENCIL_MAX_DEPTH 64
21 #define STENCIL_MAX_SEGS 8
22 #define STENCIL_PATCH_END 0xFFFFFFFFu
23  
24 typedef struct stencil_blk {
25 uint8_t kind;
26 uint8_t has_else; /* IF/UNLESS: else seen; SWITCH: default seen */
27 uint32_t arms; /* SWITCH: case and default arms so far */
28 uint32_t arm_head, arm_tail; /* SWITCH: its case arms as a linked list
29 in c->armrec (nested switches interleave) */
30 uint32_t line, col; /* opener position for errors */
31 uint32_t cond_patch; /* pending TEST_JF/JT operand offset
32 (SWITCH: the last SOP_CASE's miss) */
33 uint32_t jump_chain; /* end-jump patch sites, chained through
34 the operand slots themselves */
35 uint32_t body_start; /* FOR: back-jump target */
36 uint32_t for_end_patch; /* FOR: end_target operand offset */
37 int32_t binds_at_entry;
38 } stencil_blk;
39  
40 typedef struct stencil_compiler {
41 const char *src, *p, *end;
42 const char *name; /* diagnostic template name */
43 uint32_t line;
44 const char *line_start;
45  
46 stencil_arena code, pool, names, paths, segs, incs, filts, lines;
47 stencil_arena pend; /* pending literal run */
48 stencil_arena cases, cvals, rxs; /* switch tables, made on first use */
49 stencil_arena ctabs, ctents; /* switch dispatch tables, likewise */
50 stencil_arena armrec; /* compile-time only: case arms */
51 stencil_intern lit_intern; /* long literals + strings, into pool */
52  
53 uint32_t n_names, n_paths, n_segs, n_incs, n_filts, n_lines;
54 uint32_t n_cases, n_cvals, n_ctabs, n_ctents, n_armrec;
55  
56 stencil_blk blk[STENCIL_MAX_DEPTH];
57 uint32_t n_blk;
58 uint32_t open_switches; /* switch blocks among blk[]: case and
59 default are keywords only when > 0 */
60  
61 int32_t cur_stack, max_stack;
62 int32_t cur_frames, max_frames;
63 int32_t cur_binds, max_binds;
64  
65 uint32_t flags;
66 int no_files; /* files => 0: include and content refused */
67 uint32_t last_line_rec;
68  
69 #ifdef PERL_IMPLICIT_CONTEXT
70 PerlInterpreter *perl; /* for PERL_HASH's seed access */
71 #endif
72  
73 /* first error wins */
74 int failed;
75 char errbuf[192];
76 uint32_t err_line, err_col;
77 } stencil_compiler;
78  
79 /* Reserved words: first-position statement keywords plus 'in', which
80 * cannot be a for/set binding name (is_keyword guards those). */
81 static const char *const kw_list[] = {
82 "if", "unless", "elsif", "else", "end", "for", "set",
83 "include", "raw", "content", "in", "switch", NULL
84 };
85 /* 'case' and 'default' are not in the list: they are keywords only inside
86 * a switch, so outside one they stay ordinary data names. */
87  
88 /* ---- error helpers ------------------------------------------------- */
89  
90 115998 static uint32_t ccol(const stencil_compiler *c, const char *at)
91 {
92 115998 return (uint32_t)(at - c->line_start) + 1;
93 }
94  
95 342 static int cerror_at(stencil_compiler *c, uint32_t line, uint32_t col,
96 const char *fmt, ...)
97 {
98 va_list ap;
99 342 ▶ if (c->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (c->failed) 0 342 342
100 0 return 0;
101 342 c->failed = 1;
102 342 c->err_line = line;
103 342 c->err_col = col;
104 342 va_start(ap, fmt);
105 342 vsnprintf(c->errbuf, sizeof c->errbuf, fmt, ap);
106 342 va_end(ap);
107 342 return 0;
108 }
109  
110 255 static int cerror(stencil_compiler *c, const char *at, const char *fmt, ...)
111 {
112 va_list ap;
113 255 ▶ if (c->failed)
Branch: 1 branch
BranchTrueFalseTotal
if (c->failed) 0 255 255
114 0 return 0;
115 255 c->failed = 1;
116 255 c->err_line = c->line;
117 255 c->err_col = ccol(c, at);
118 255 va_start(ap, fmt);
119 255 vsnprintf(c->errbuf, sizeof c->errbuf, fmt, ap);
120 255 va_end(ap);
121 255 return 0;
122 }
123  
124 0 static int coom(stencil_compiler *c)
125 {
126 0 return cerror_at(c, c->line, 1, "out of memory");
127 }
128  
129 /* ---- emit helpers --------------------------------------------------- */
130  
131 631402 static int emit_bytes(stencil_compiler *c, const void *p, size_t n)
132 {
133 631402 uint32_t off = stencil_arena_alloc(&c->code, n, 1);
134 631402 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 631402 631402
135 0 return coom(c);
136 631402 memcpy(c->code.base + off, p, n);
137 631402 return 1;
138 }
139  
140 399045 static int emit_u8(stencil_compiler *c, uint8_t v)
141 {
142 399045 return emit_bytes(c, &v, 1);
143 }
144  
145 125693 static int emit_u32(stencil_compiler *c, uint32_t v)
146 {
147 125693 return emit_bytes(c, &v, 4);
148 }
149  
150 30985 static void patch_u32(stencil_compiler *c, uint32_t at, uint32_t v)
151 {
152 30985 memcpy(c->code.base + at, &v, 4);
153 30985 }
154  
155 3794 static uint32_t read_code_u32(stencil_compiler *c, uint32_t at)
156 {
157 uint32_t v;
158 3794 memcpy(&v, c->code.base + at, 4);
159 3794 return v;
160 }
161  
162 135424 static uint32_t code_pos(const stencil_compiler *c)
163 {
164 135424 return (uint32_t)c->code.used;
165 }
166  
167 /* Record the source line for the op about to be emitted (delta table,
168 * appended only when the line changes). */
169 312637 static int record_line(stencil_compiler *c)
170 {
171 stencil_cline *e;
172 uint32_t off;
173 312637 ▶ if (c->line == c->last_line_rec)
Branch: 1 branch
BranchTrueFalseTotal
if (c->line == c->last_line_rec) 308778 3859 312637
174 308778 return 1;
175 3859 off = stencil_arena_alloc(&c->lines, sizeof(stencil_cline),
176 sizeof(uint32_t));
177 3859 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 3859 3859
178 0 return coom(c);
179 3859 e = (stencil_cline *)(c->lines.base + off);
180 3859 e->off = code_pos(c);
181 3859 e->line = c->line;
182 3859 c->n_lines++;
183 3859 c->last_line_rec = c->line;
184 3859 return 1;
185 }
186  
187 312637 static int emit_op(stencil_compiler *c, uint8_t op, int32_t stack_delta)
188 {
189 312637 ▶ if (!record_line(c) || !emit_u8(c, op))
Branch: 2 branches
BranchTrueFalseTotal
if (!record_line(c) || !emit_u8(c, op)) 312637 0 312637
if (!record_line(c) || !emit_u8(c, op)) 0 312637 312637
190 0 return 0;
191 312637 c->cur_stack += stack_delta;
192 312637 ▶ if (c->cur_stack > c->max_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_stack > c->max_stack) 3329 309308 312637
193 3329 c->max_stack = c->cur_stack;
194 312637 return 1;
195 }
196  
197 /* Emit a jump-family op whose operand joins a patch chain threaded
198 * through the operand slots themselves; returns via *chain. */
199 3637 static int emit_jump_chained(stencil_compiler *c, uint8_t op,
200 int32_t stack_delta, uint32_t *chain)
201 {
202 uint32_t site;
203 3637 ▶ if (!emit_op(c, op, stack_delta))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, op, stack_delta)) 0 3637 3637
204 0 return 0;
205 3637 site = code_pos(c);
206 3637 ▶ if (!emit_u32(c, *chain))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_u32(c, *chain)) 0 3637 3637
207 0 return 0;
208 3637 *chain = site;
209 3637 return 1;
210 }
211  
212 68303 static void patch_chain(stencil_compiler *c, uint32_t chain,
213 uint32_t target)
214 {
215 71937 ▶ while (chain != STENCIL_PATCH_END) {
Branch: 1 branch
BranchTrueFalseTotal
while (chain != STENCIL_PATCH_END) { 3634 68303 71937
216 3634 uint32_t next = read_code_u32(c, chain);
217 3634 patch_u32(c, chain, target);
218 3634 chain = next;
219 }
220 68303 }
221  
222 /* ---- pending-literal accumulation and flush ------------------------- */
223  
224 118966 static int lit_pend(stencil_compiler *c, const char *p, size_t n)
225 {
226 uint32_t off;
227 118966 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 32190 86776 118966
228 32190 return 1;
229 86776 off = stencil_arena_alloc(&c->pend, n, 1);
230 86776 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 86776 86776
231 0 return coom(c);
232 86776 memcpy(c->pend.base + off, p, n);
233 86776 return 1;
234 }
235  
236 118885 static int lit_flush(stencil_compiler *c)
237 {
238 118885 size_t n = c->pend.used;
239 118885 ▶ if (!n)
Branch: 1 branch
BranchTrueFalseTotal
if (!n) 32174 86711 118885
240 32174 return 1;
241 86711 ▶ if (n <= 31) {
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 31) { 86408 303 86711
242 172816 if (!emit_op(c, SOP_LITERAL_SHORT, 0) ||
243 172816 ▶ !emit_u8(c, (uint8_t)n) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_u8(c, (uint8_t)n) || 0 86408 86408
244 86408 !emit_bytes(c, c->pend.base, n))
245 0 return 0;
246 } else {
247 303 uint32_t off = stencil_intern_get(&c->lit_intern, &c->pool,
248 303 c->pend.base, (uint32_t)n);
249 303 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 303 303
250 0 return coom(c);
251 606 if (!emit_op(c, SOP_LITERAL_LONG, 0) ||
252 606 ▶ !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 303 303
253 0 return 0;
254 }
255 86711 c->pend.used = 0;
256 86711 return 1;
257 }
258  
259 /* ---- source advance with line tracking ------------------------------- */
260  
261 119390 static void advance_lines(stencil_compiler *c, const char *from,
262 const char *to)
263 {
264 119390 const char *nl = from;
265 for (;;) {
266 121620 nl = (const char *)memchr(nl, '\n', (size_t)(to - nl));
267 120505 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 119390 1115 120505
268 119390 break;
269 1115 c->line++;
270 1115 nl++;
271 1115 c->line_start = nl;
272 }
273 119390 }
274  
275 /* ---- token helpers --------------------------------------------------- */
276  
277 #define IS_WORD_START(ch) (isALPHA(ch) || (ch) == '_')
278 #define IS_WORD(ch) (isALNUM(ch) || (ch) == '_')
279  
280 440047 static void skip_ws(stencil_compiler *c)
281 {
282 744566 ▶ while (c->p < c->end && isSPACE(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isSPACE(*c->p)) { 744551 15 744566
while (c->p < c->end && isSPACE(*c->p)) { 304519 440032 744551
283 304519 ▶ if (*c->p == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '\n') { 6 304513 304519
284 6 c->line++;
285 6 c->line_start = c->p + 1;
286 }
287 304519 c->p++;
288 }
289 440047 }
290  
291 362013 static size_t word_len(const char *p, const char *end)
292 {
293 362013 const char *q = p;
294 362013 ▶ if (q >= end || !IS_WORD_START(*q))
Branch: 3 branches
BranchTrueFalseTotal
if (q >= end || !IS_WORD_START(*q)) 362006 7 362013
if (q >= end || !IS_WORD_START(*q)) 49248 312758 362006
if (q >= end || !IS_WORD_START(*q)) 49248 0 49248
295 49255 return 0;
296 312758 q++;
297 1203983 ▶ while (q < end && IS_WORD(*q))
Branch: 3 branches
BranchTrueFalseTotal
while (q < end && IS_WORD(*q)) 1203981 2 1203983
while (q < end && IS_WORD(*q)) 891225 312756 1203981
while (q < end && IS_WORD(*q)) 0 312756 312756
298 891225 q++;
299 312758 return (size_t)(q - p);
300 }
301  
302 1234086 static int word_is(const char *p, size_t n, const char *kw)
303 {
304 1234086 ▶ return strlen(kw) == n && memcmp(p, kw, n) == 0;
Branch: 2 branches
BranchTrueFalseTotal
return strlen(kw) == n && memcmp(p, kw, n) == 0; 203021 1031065 1234086
return strlen(kw) == n && memcmp(p, kw, n) == 0; 55939 147082 203021
305 }
306  
307 1933 static int is_keyword(const char *p, size_t n)
308 {
309 int i;
310 25118 ▶ for (i = 0; kw_list[i]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; kw_list[i]; i++) 23189 1929 25118
311 23189 ▶ if (word_is(p, n, kw_list[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, kw_list[i])) 4 23185 23189
312 4 return 1;
313 1929 return 0;
314 }
315  
316 115180 static int expect_close(stencil_compiler *c)
317 {
318 115180 skip_ws(c);
319 115180 ▶ 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] == '}') { 115173 7 115180
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 115121 52 115173
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 115116 5 115121
320 115116 c->p += 2;
321 115116 return 1;
322 }
323 64 return cerror(c, c->p, "expected '%%}'");
324 }
325  
326 /* ---- name / path interning ------------------------------------------ */
327  
328 148724 static uint32_t name_intern(stencil_compiler *c, const char *p, uint32_t n)
329 {
330 148724 stencil_cname *tab = (stencil_cname *)c->names.base;
331 uint32_t i, off, hash, noff;
332 stencil_cname *e;
333 #ifdef PERL_IMPLICIT_CONTEXT
334 dTHXa(c->perl);
335 #endif
336 4223623 ▶ for (i = 0; i < c->n_names; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_names; i++) 4193798 29825 4223623
337 4193798 ▶ if (tab[i].len == n
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].len == n 2628549 1565249 4193798
338 2628549 ▶ && memcmp(c->pool.base + tab[i].off, p, n) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(c->pool.base + tab[i].off, p, n) == 0) 118899 2509650 2628549
339 118899 return i;
340 29825 off = stencil_arena_alloc(&c->pool, n, 1);
341 29825 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 29825 29825
342 0 return STENCIL_ARENA_NULL;
343 29825 memcpy(c->pool.base + off, p, n);
344 29825 ▶ PERL_HASH(hash, p, n);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, p, n); 29825 0 29825
345 29825 noff = stencil_arena_alloc(&c->names, sizeof(stencil_cname),
346 sizeof(uint32_t));
347 29825 ▶ if (noff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (noff == STENCIL_ARENA_NULL) 0 29825 29825
348 0 return STENCIL_ARENA_NULL;
349 29825 e = (stencil_cname *)(c->names.base + noff);
350 29825 e->off = off;
351 29825 e->len = n;
352 29825 e->hash = hash;
353 29825 return c->n_names++;
354 }
355  
356 /* Parse a dotted/indexed path at c->p; returns path_id or
357 * STENCIL_ARENA_NULL after setting the error. */
358 86829 static uint32_t path_parse(stencil_compiler *c)
359 {
360 86829 const char *start = c->p;
361 stencil_seg segs[STENCIL_MAX_SEGS];
362 86829 uint32_t n_segs = 0;
363 uint32_t i, seg_base, full_off, poff;
364 size_t full_len;
365 stencil_cpath *tab, *e;
366  
367 for (;;) {
368 146847 ▶ if (n_segs >= STENCIL_MAX_SEGS) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_segs >= STENCIL_MAX_SEGS) { 1 146846 146847
369 1 cerror(c, c->p, "path has more than %d segments",
370 STENCIL_MAX_SEGS);
371 1 return STENCIL_ARENA_NULL;
372 }
373 146846 ▶ if (c->p < c->end && *c->p == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') { 146844 2 146846
if (c->p < c->end && *c->p == '[') { 18 146826 146844
374 18 const char *q = c->p + 1;
375 18 SSize_t ix = 0;
376 18 ▶ if (n_segs == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_segs == 0) { 0 18 18
377 0 cerror(c, c->p, "path cannot start with an index");
378 0 return STENCIL_ARENA_NULL;
379 }
380 18 ▶ if (q >= c->end || !isDIGIT(*q)) {
Branch: 2 branches
BranchTrueFalseTotal
if (q >= c->end || !isDIGIT(*q)) { 18 0 18
if (q >= c->end || !isDIGIT(*q)) { 1 17 18
381 1 cerror(c, c->p, "expected digits in [index]");
382 1 return STENCIL_ARENA_NULL;
383 }
384 35 ▶ while (q < c->end && isDIGIT(*q))
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && isDIGIT(*q)) 35 0 35
while (q < c->end && isDIGIT(*q)) 18 17 35
385 18 ix = ix * 10 + (*q++ - '0');
386 17 ▶ if (q >= c->end || *q != ']') {
Branch: 2 branches
BranchTrueFalseTotal
if (q >= c->end || *q != ']') { 17 0 17
if (q >= c->end || *q != ']') { 1 16 17
387 1 cerror(c, q, "expected ']'");
388 1 return STENCIL_ARENA_NULL;
389 }
390 16 segs[n_segs].name_id = 0;
391 16 segs[n_segs].is_index = 1;
392 16 segs[n_segs].index = ix;
393 16 n_segs++;
394 16 c->p = q + 1;
395 } else {
396 146828 size_t wl = word_len(c->p, c->end);
397 uint32_t id;
398 146828 ▶ if (!wl) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) { 17 146811 146828
399 17 ▶ cerror(c, c->p, n_segs ? "expected name after '.'"
Branch: 1 branch
BranchTrueFalseTotal
cerror(c, c->p, n_segs ? "expected name after '.'" 1 16 17
400 : "expected a name");
401 17 return STENCIL_ARENA_NULL;
402 }
403 146811 id = name_intern(c, c->p, (uint32_t)wl);
404 146811 ▶ if (id == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (id == STENCIL_ARENA_NULL) { 0 146811 146811
405 0 coom(c);
406 0 return STENCIL_ARENA_NULL;
407 }
408 146811 segs[n_segs].name_id = id;
409 146811 segs[n_segs].is_index = 0;
410 146811 segs[n_segs].index = 0;
411 146811 n_segs++;
412 146811 c->p += wl;
413 }
414 146827 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 146826 1 146827
if (c->p < c->end && *c->p == '.') { 60000 86826 146826
415 60000 c->p++;
416 60000 continue;
417 }
418 86827 ▶ if (c->p < c->end && *c->p == '[')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '[') 86826 1 86827
if (c->p < c->end && *c->p == '[') 18 86808 86826
419 18 continue;
420 86809 break;
421 }
422  
423 /* dedup by the normalised source slice */
424 86809 full_len = (size_t)(c->p - start);
425 86809 tab = (stencil_cpath *)c->paths.base;
426 4088675 ▶ for (i = 0; i < c->n_paths; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_paths; i++) 4060482 28193 4088675
427 4060482 ▶ if (tab[i].full_len == full_len
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].full_len == full_len 2505810 1554672 4060482
428 2505810 ▶ && 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) 58616 2447194 2505810
429 58616 return i;
430  
431 28193 full_off = stencil_intern_get(&c->lit_intern, &c->pool, start,
432 (uint32_t)full_len);
433 28193 ▶ if (full_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (full_off == STENCIL_ARENA_NULL) { 0 28193 28193
434 0 coom(c);
435 0 return STENCIL_ARENA_NULL;
436 }
437 28193 seg_base = c->n_segs;
438 58043 ▶ for (i = 0; i < n_segs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n_segs; i++) { 29850 28193 58043
439 29850 uint32_t soff = stencil_arena_alloc(&c->segs, sizeof(stencil_seg),
440 sizeof(SSize_t));
441 29850 ▶ if (soff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (soff == STENCIL_ARENA_NULL) { 0 29850 29850
442 0 coom(c);
443 0 return STENCIL_ARENA_NULL;
444 }
445 29850 memcpy(c->segs.base + soff, &segs[i], sizeof(stencil_seg));
446 29850 c->n_segs++;
447 }
448 28193 poff = stencil_arena_alloc(&c->paths, sizeof(stencil_cpath),
449 sizeof(uint32_t));
450 28193 ▶ if (poff == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (poff == STENCIL_ARENA_NULL) { 0 28193 28193
451 0 coom(c);
452 0 return STENCIL_ARENA_NULL;
453 }
454 28193 e = (stencil_cpath *)(c->paths.base + poff);
455 {
456 28193 const stencil_cname *names = (const stencil_cname *)c->names.base;
457 56386 e->loop_rooted = !segs[0].is_index
458 28193 ▶ && names[segs[0].name_id].len == 4
Branch: 1 branch
BranchTrueFalseTotal
&& names[segs[0].name_id].len == 4 16670 11523 28193
459 56386 ▶ && memcmp(c->pool.base + names[segs[0].name_id].off,
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(c->pool.base + names[segs[0].name_id].off, 28193 0 28193
&& memcmp(c->pool.base + names[segs[0].name_id].off, 137 16533 16670
460 "loop", 4) == 0;
461 }
462 28193 e->n_segs = (uint16_t)n_segs;
463 28193 e->seg_idx = seg_base;
464 28193 e->full_off = full_off;
465 28193 e->full_len = (uint32_t)full_len;
466 28193 return c->n_paths++;
467 }
468  
469 /* parse a path and emit its push */
470 86584 static int parse_path_push(stencil_compiler *c)
471 {
472 86584 uint32_t pid = path_parse(c);
473 86584 ▶ if (pid == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pid == STENCIL_ARENA_NULL) 20 86564 86584
474 20 return 0;
475 86564 ▶ 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); 86564 0 86564
return emit_op(c, SOP_PUSH_PATH, +1) && emit_u32(c, pid); 86564 0 86564
476 }
477  
478 /* ====================================================================
479 * Literal lexers (shared by expressions and, later, case patterns)
480 * ==================================================================== */
481  
482 /* A quoted string literal at c->p ('...' or "..."). The only escapes
483 * are \<quote> and \\; any other backslash is kept as written. The bytes
484 * land in the pool - interned (deduplicated) when there was nothing to
485 * unescape, copied otherwise - and *off_out, *len_out describe them; an empty
486 * string is (0, 0). Newlines inside the literal advance the line count.
487 * On return c->p is past the closing quote. */
488 2502 static int lex_string(stencil_compiler *c, uint32_t *off_out,
489 uint32_t *len_out)
490 {
491 2502 char quote = *c->p;
492 2502 const char *q = c->p + 1;
493 2502 int esc = 0;
494 uint32_t off, len;
495 6383 ▶ while (q < c->end && *q != quote) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && *q != quote) { 6381 2 6383
while (q < c->end && *q != quote) { 3881 2500 6381
496 3881 ▶ if (*q == '\\' && q + 1 < c->end
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\\' && q + 1 < c->end 6 3875 3881
if (*q == '\\' && q + 1 < c->end 6 0 6
497 6 ▶ && (q[1] == quote || q[1] == '\\')) {
Branch: 2 branches
BranchTrueFalseTotal
&& (q[1] == quote || q[1] == '\\')) { 2 4 6
&& (q[1] == quote || q[1] == '\\')) { 0 2 2
498 4 esc = 1;
499 4 q += 2;
500 } else {
501 3877 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 0 3877 3877
502 0 c->line++;
503 0 c->line_start = q + 1;
504 }
505 3877 q++;
506 }
507 }
508 2502 ▶ if (q >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= c->end) 2 2500 2502
509 2 return cerror(c, c->p, "unterminated string literal");
510 2500 ▶ if (!esc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!esc) { 2496 4 2500
511 2496 len = (uint32_t)(q - (c->p + 1));
512 2496 off = len ? stencil_intern_get(&c->lit_intern, &c->pool,
513 1618 c->p + 1, len)
514 2496 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 1618 878 2496
515 } else {
516 /* copy with unescaping straight into the pool (no dedup) */
517 4 const char *r = c->p + 1;
518 char *w;
519 4 off = stencil_arena_alloc(&c->pool, (size_t)(q - r), 1);
520 4 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 4 4
521 0 return coom(c);
522 4 w = c->pool.base + off;
523 19 ▶ while (r < q) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < q) { 15 4 19
524 15 ▶ if (*r == '\\' && (r[1] == quote || r[1] == '\\'))
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 4 11 15
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 4 4
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
525 4 r++;
526 15 *w++ = *r++;
527 }
528 4 len = (uint32_t)(w - (c->pool.base + off));
529 4 c->pool.used = (size_t)(w - c->pool.base);
530 }
531 2500 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 2500 2500
532 0 return coom(c);
533 2500 c->p = q + 1;
534 2500 *off_out = off;
535 2500 *len_out = len;
536 2500 return 1;
537 }
538  
539 /* A number literal at c->p, in one of two spellings.
540 * expression: -?digits(.digits)? The caller saw a digit, or '-' then a
541 * digit; a '.' must be followed by digits.
542 * case (LEXNUM_CASE, Switch::Declare's): also +5, .5 and 1e3 / 2.5E-3; a
543 * '.' with no digit after it ends the number, so [1..9] lexes
544 * as 1, '..', 9.
545 * The span is converted by perl's own my_atof, so a literal is bit-for-bit
546 * what perl makes of the same digits (summing digit * 0.1^k rounded
547 * differently for a fifth of all two-decimal values), at full NV width. An
548 * integer spelling that fits an IV or UV is also kept exactly. my_atof
549 * wants a NUL-terminated string, so the span is copied first: onto the
550 * stack when short, into a mortal otherwise, never reading past c->end. */
551 #define STENCIL_NUM_STACKBUF 64
552 #define LEXNUM_CASE 0x1
553  
554 typedef struct stencil_lexnum {
555 NV nv;
556 IV iv; /* is_int: the exact value (a UV when is_uv) */
557 int is_int;
558 int is_uv;
559 } stencil_lexnum;
560  
561 20904 static int lex_number(stencil_compiler *c, int flags, stencil_lexnum *out)
562 {
563 20904 const char *start = c->p, *q = c->p, *digits;
564 char buf[STENCIL_NUM_STACKBUF];
565 char *z;
566 size_t n;
567 20904 int int_spelling = 1; /* no '.', no exponent */
568 #ifdef PERL_IMPLICIT_CONTEXT
569 dTHXa(c->perl);
570 #endif
571 20904 ▶ if (q < c->end && (*q == '-' || *q == '+'))
Branch: 3 branches
BranchTrueFalseTotal
if (q < c->end && (*q == '-' || *q == '+')) 20904 0 20904
if (q < c->end && (*q == '-' || *q == '+')) 20888 16 20904
if (q < c->end && (*q == '-' || *q == '+')) 3 20885 20888
572 19 q++; /* '+' only reaches here in case spelling */
573 20904 digits = q;
574 71476 ▶ while (q < c->end && isDIGIT(*q))
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && isDIGIT(*q)) 71476 0 71476
while (q < c->end && isDIGIT(*q)) 50572 20904 71476
575 50572 q++;
576 20904 ▶ if (q < c->end && *q == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < c->end && *q == '.') { 20904 0 20904
if (q < c->end && *q == '.') { 20043 861 20904
577 20043 ▶ if (q + 1 < c->end && isDIGIT(q[1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (q + 1 < c->end && isDIGIT(q[1])) { 20043 0 20043
if (q + 1 < c->end && isDIGIT(q[1])) { 20040 3 20043
578 20040 q++;
579 60163 ▶ while (q < c->end && isDIGIT(*q))
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && isDIGIT(*q)) 60163 0 60163
while (q < c->end && isDIGIT(*q)) 40123 20040 60163
580 40123 q++;
581 20040 int_spelling = 0;
582 3 ▶ } else if (!(flags & LEXNUM_CASE)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!(flags & LEXNUM_CASE)) { 1 2 3
583 1 return cerror(c, q, "expected digits after '.'");
584 }
585 }
586 20903 ▶ if (q == digits)
Branch: 1 branch
BranchTrueFalseTotal
if (q == digits) 0 20903 20903
587 0 return cerror(c, c->p, "expected a number");
588 20903 ▶ if ((flags & LEXNUM_CASE) && q < c->end && (*q == 'e' || *q == 'E')) {
Branch: 4 branches
BranchTrueFalseTotal
if ((flags & LEXNUM_CASE) && q < c->end && (*q == 'e' || *q == 'E')) { 647 20256 20903
if ((flags & LEXNUM_CASE) && q < c->end && (*q == 'e' || *q == 'E')) { 647 0 647
if ((flags & LEXNUM_CASE) && q < c->end && (*q == 'e' || *q == 'E')) { 644 3 647
if ((flags & LEXNUM_CASE) && q < c->end && (*q == 'e' || *q == 'E')) { 1 643 644
589 4 const char *e = q + 1;
590 4 ▶ if (e < c->end && (*e == '+' || *e == '-'))
Branch: 3 branches
BranchTrueFalseTotal
if (e < c->end && (*e == '+' || *e == '-')) 4 0 4
if (e < c->end && (*e == '+' || *e == '-')) 4 0 4
if (e < c->end && (*e == '+' || *e == '-')) 1 3 4
591 1 e++;
592 4 ▶ if (e < c->end && isDIGIT(*e)) {
Branch: 2 branches
BranchTrueFalseTotal
if (e < c->end && isDIGIT(*e)) { 4 0 4
if (e < c->end && isDIGIT(*e)) { 4 0 4
593 8 ▶ while (e < c->end && isDIGIT(*e))
Branch: 2 branches
BranchTrueFalseTotal
while (e < c->end && isDIGIT(*e)) 8 0 8
while (e < c->end && isDIGIT(*e)) 4 4 8
594 4 e++;
595 4 q = e;
596 4 int_spelling = 0;
597 }
598 }
599 20903 n = (size_t)(q - start);
600 20903 ▶ if (n < STENCIL_NUM_STACKBUF) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < STENCIL_NUM_STACKBUF) { 20901 2 20903
601 20901 memcpy(buf, start, n);
602 20901 buf[n] = '\0';
603 20901 z = buf;
604 } else {
605 2 z = SvPVX(sv_2mortal(newSVpvn(start, n)));
606 }
607 20903 out->nv = my_atof(z);
608 20903 out->iv = 0;
609 20903 out->is_int = out->is_uv = 0;
610 20903 ▶ if (int_spelling) {
Branch: 1 branch
BranchTrueFalseTotal
if (int_spelling) { 860 20043 20903
611 860 UV uv = 0;
612 860 int g = grok_number(z, n, &uv);
613 860 ▶ if ((g & IS_NUMBER_IN_UV) && !(g & IS_NUMBER_GREATER_THAN_UV_MAX)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((g & IS_NUMBER_IN_UV) && !(g & IS_NUMBER_GREATER_THAN_UV_MAX)) { 859 1 860
if ((g & IS_NUMBER_IN_UV) && !(g & IS_NUMBER_GREATER_THAN_UV_MAX)) { 859 0 859
614 859 ▶ if (g & IS_NUMBER_NEG) {
Branch: 1 branch
BranchTrueFalseTotal
if (g & IS_NUMBER_NEG) { 10 849 859
615 10 ▶ if (uv <= (UV)IV_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (uv <= (UV)IV_MAX) { 8 2 10
616 8 out->iv = -(IV)uv;
617 8 out->is_int = 1;
618 2 ▶ } else if (uv == (UV)IV_MAX + 1) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (uv == (UV)IV_MAX + 1) { 2 0 2
619 2 out->iv = IV_MIN;
620 2 out->is_int = 1;
621 }
622 } else {
623 849 out->iv = (IV)uv;
624 849 out->is_int = 1;
625 849 out->is_uv = uv > (UV)IV_MAX;
626 }
627 }
628 }
629 20903 c->p = q;
630 20903 return 1;
631 }
632  
633 /* ====================================================================
634 * Expression parser
635 * precedence: or < and < not < comparison < primary
636 * ==================================================================== */
637  
638 static int parse_expr(stencil_compiler *c);
639  
640 43141 static int parse_primary(stencil_compiler *c)
641 {
642 43141 skip_ws(c);
643 43141 ▶ if (c->p >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p >= c->end) 2 43139 43141
644 2 return cerror(c, c->p, "expected an expression");
645  
646 43139 ▶ if (*c->p == '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '(') { 4 43135 43139
647 4 c->p++;
648 4 ▶ if (!parse_expr(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_expr(c)) 0 4 4
649 0 return 0;
650 4 skip_ws(c);
651 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
652 1 return cerror(c, c->p, "expected ')'");
653 3 c->p++;
654 3 return 1;
655 }
656  
657 43135 ▶ if (*c->p == '\'' || *c->p == '"') {
Branch: 2 branches
BranchTrueFalseTotal
if (*c->p == '\'' || *c->p == '"') { 43119 16 43135
if (*c->p == '\'' || *c->p == '"') { 28 43091 43119
658 44 uint32_t off = 0, len = 0;
659 44 ▶ if (!lex_string(c, &off, &len))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_string(c, &off, &len)) 1 43 44
660 1 return 0;
661 43 return emit_op(c, SOP_PUSH_LIT_STR, +1)
662 43 ▶ && emit_u32(c, off) && emit_u32(c, len);
Branch: 3 branches
BranchTrueFalseTotal
&& emit_u32(c, off) && emit_u32(c, len); 43 0 43
&& emit_u32(c, off) && emit_u32(c, len); 43 0 43
&& emit_u32(c, off) && emit_u32(c, len); 43 0 43
663 }
664  
665 43091 ▶ if (isDIGIT(*c->p)
Branch: 1 branch
BranchTrueFalseTotal
if (isDIGIT(*c->p) 22840 20251 43091
666 22840 ▶ || (*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]))) { 6 22834 22840
|| (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) { 6 0 6
|| (*c->p == '-' && c->p + 1 < c->end && isDIGIT(c->p[1]))) { 6 0 6
667 stencil_lexnum num;
668 20257 ▶ if (!lex_number(c, 0, &num))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_number(c, 0, &num)) 1 20256 20257
669 1 return 0;
670 20256 return emit_op(c, SOP_PUSH_LIT_NUM, +1)
671 20256 ▶ && emit_bytes(c, &num.nv, sizeof num.nv);
Branch: 2 branches
BranchTrueFalseTotal
&& emit_bytes(c, &num.nv, sizeof num.nv); 20256 0 20256
&& emit_bytes(c, &num.nv, sizeof num.nv); 20256 0 20256
672 }
673  
674 {
675 22834 size_t wl = word_len(c->p, c->end);
676 22834 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 42 22792 22834
677 42 return cerror(c, c->p, "expected an expression");
678 22792 ▶ if (word_is(c->p, wl, "undef")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "undef")) { 4 22788 22792
679 4 c->p += wl;
680 4 return emit_op(c, SOP_PUSH_UNDEF, +1);
681 }
682 22788 ▶ if (word_is(c->p, wl, "defined")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "defined")) { 8 22780 22788
683 8 const char *kw = c->p;
684 8 c->p += wl;
685 8 skip_ws(c);
686 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
687 1 return cerror(c, kw, "defined needs (path)");
688 7 c->p++;
689 7 skip_ws(c);
690 7 ▶ if (!parse_path_push(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_path_push(c)) 0 7 7
691 0 return 0;
692 7 skip_ws(c);
693 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 != ')') 0 7 7
694 0 return cerror(c, c->p, "expected ')'");
695 7 c->p++;
696 7 return emit_op(c, SOP_DEFINED, 0);
697 }
698 22780 return parse_path_push(c);
699 }
700 }
701  
702 /* returns the comparison opcode at c->p (consuming it) or 0 */
703 23004 static uint8_t scan_cmp_op(stencil_compiler *c)
704 {
705 23004 const char *p = c->p;
706 23004 size_t left = (size_t)(c->end - p);
707 size_t wl;
708 23004 ▶ if (left >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 2) { 23004 0 23004
709 23004 ▶ 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; } 20049 2955 23004
if (p[0] == '=' && p[1] == '=') { c->p += 2; return SOP_EQ_NUM; } 20048 1 20049
710 2956 ▶ 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 2954 2956
if (p[0] == '!' && p[1] == '=') { c->p += 2; return SOP_NE_NUM; } 2 0 2
711 2954 ▶ 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 2949 2954
if (p[0] == '<' && p[1] == '=') { c->p += 2; return SOP_LE_NUM; } 2 3 5
712 2952 ▶ 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 2945 2952
if (p[0] == '>' && p[1] == '=') { c->p += 2; return SOP_GE_NUM; } 4 3 7
713 }
714 2948 ▶ if (left >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (left >= 1) { 2948 0 2948
715 2948 ▶ 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 2945 2948
716 2945 ▶ 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 2942 2945
717 }
718 2942 wl = word_len(p, c->end);
719 2942 ▶ if (wl == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (wl == 2) { 34 2908 2942
720 34 ▶ 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; } 20 14 34
721 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
722 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
723 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
724 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
725 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
726 }
727 2910 return 0;
728 }
729  
730 23047 static int parse_cmp(stencil_compiler *c)
731 {
732 uint8_t op;
733 23047 ▶ if (!parse_primary(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_primary(c)) 43 23004 23047
734 43 return 0;
735 23004 skip_ws(c);
736 23004 op = scan_cmp_op(c);
737 23004 ▶ if (!op)
Branch: 1 branch
BranchTrueFalseTotal
if (!op) 2910 20094 23004
738 2910 return 1;
739 20094 ▶ if (!parse_primary(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_primary(c)) 5 20089 20094
740 5 return 0;
741 20089 return emit_op(c, op, -1);
742 }
743  
744 23047 static int parse_not(stencil_compiler *c)
745 {
746 23047 int negs = 0;
747 for (;;) {
748 23054 skip_ws(c);
749 23054 ▶ if (c->p < c->end && *c->p == '!'
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '!' 23052 2 23054
if (c->p < c->end && *c->p == '!' 3 23049 23052
750 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
751 3 c->p++;
752 3 negs++;
753 3 continue;
754 }
755 {
756 23051 size_t wl = word_len(c->p, c->end);
757 23051 ▶ if (wl && word_is(c->p, wl, "not")) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, "not")) { 22770 281 23051
if (wl && word_is(c->p, wl, "not")) { 4 22766 22770
758 4 c->p += wl;
759 4 negs++;
760 4 continue;
761 }
762 }
763 23047 break;
764 }
765 23047 ▶ if (!parse_cmp(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_cmp(c)) 48 22999 23047
766 48 return 0;
767 23006 ▶ while (negs--)
Branch: 1 branch
BranchTrueFalseTotal
while (negs--) 7 22999 23006
768 7 ▶ if (!emit_op(c, SOP_NOT, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_NOT, 0)) 0 7 7
769 0 return 0;
770 22999 return 1;
771 }
772  
773 /* and-op at c->p? consume and return true */
774 45985 static int scan_word_op(stencil_compiler *c, const char *sym,
775 const char *word)
776 {
777 size_t wl;
778 45985 ▶ 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] 45985 0 45985
if ((size_t)(c->end - c->p) >= 2 && c->p[0] == sym[0] 29 45956 45985
779 29 ▶ && c->p[1] == sym[1]) {
Branch: 1 branch
BranchTrueFalseTotal
&& c->p[1] == sym[1]) { 28 1 29
780 28 c->p += 2;
781 28 return 1;
782 }
783 45957 wl = word_len(c->p, c->end);
784 45957 ▶ if (wl && word_is(c->p, wl, word)) {
Branch: 2 branches
BranchTrueFalseTotal
if (wl && word_is(c->p, wl, word)) { 11 45946 45957
if (wl && word_is(c->p, wl, word)) { 3 8 11
785 3 c->p += wl;
786 3 return 1;
787 }
788 45954 return 0;
789 }
790  
791 23034 static int parse_and(stencil_compiler *c)
792 {
793 23034 uint32_t chain = STENCIL_PATCH_END;
794 23034 ▶ if (!parse_not(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_not(c)) 22986 48 23034
795 48 return 0;
796 for (;;) {
797 22999 skip_ws(c);
798 22999 ▶ if (!scan_word_op(c, "&&", "and"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "&&", "and")) 22986 13 22999
799 22986 break;
800 26 if (!emit_jump_chained(c, SOP_JF_KEEP, 0, &chain) ||
801 26 ▶ !emit_op(c, SOP_POP, -1) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_op(c, SOP_POP, -1) || 0 13 13
802 13 !parse_not(c))
803 0 return 0;
804 }
805 22986 patch_chain(c, chain, code_pos(c));
806 22986 return 1;
807 }
808  
809 23016 static int parse_or(stencil_compiler *c)
810 {
811 23016 uint32_t chain = STENCIL_PATCH_END;
812 23016 ▶ if (!parse_and(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_and(c)) 22969 47 23016
813 47 return 0;
814 for (;;) {
815 22986 skip_ws(c);
816 22986 ▶ if (!scan_word_op(c, "||", "or"))
Branch: 1 branch
BranchTrueFalseTotal
if (!scan_word_op(c, "||", "or")) 22968 18 22986
817 22968 break;
818 36 if (!emit_jump_chained(c, SOP_JT_KEEP, 0, &chain) ||
819 36 ▶ !emit_op(c, SOP_POP, -1) ||
Branch: 1 branch
BranchTrueFalseTotal
!emit_op(c, SOP_POP, -1) || 1 17 18
820 18 !parse_and(c))
821 1 return 0;
822 }
823 22968 patch_chain(c, chain, code_pos(c));
824 22968 return 1;
825 }
826  
827 23016 static int parse_expr(stencil_compiler *c)
828 {
829 23016 return parse_or(c);
830 }
831  
832 /* ====================================================================
833 * Filters
834 * ==================================================================== */
835  
836 212 static int32_t filter_builtin(const char *p, uint32_t n)
837 {
838 212 ▶ if (word_is(p, n, "upper")) return STENCIL_FILT_UPPER;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "upper")) return STENCIL_FILT_UPPER; 56 156 212
839 156 ▶ 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 147 156
840 147 ▶ 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 127 147
841 127 ▶ 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 117 127
842 117 ▶ if (word_is(p, n, "uri")) return STENCIL_FILT_URI;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "uri")) return STENCIL_FILT_URI; 11 106 117
843 106 ▶ if (word_is(p, n, "default")) return STENCIL_FILT_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(p, n, "default")) return STENCIL_FILT_DEFAULT; 22 84 106
844 84 ▶ 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 37 84
845 37 return STENCIL_FILT_USER;
846 }
847  
848 /* fmt's format string, checked here so the render can trust it blind:
849 * literal text plus exactly one %-conversion of a known-safe shape.
850 *
851 * %[-+ 0#]*[width][.precision]conv conv in [diouxXeEfgGs]
852 *
853 * %% is a literal. No '*', no length modifiers, no %n; width and
854 * precision are capped so the render-side output buffer cannot be
855 * outgrown, and the whole format is capped so the rewritten format
856 * (the render inserts perl's IVdf/UVxf length modifiers) fits its own
857 * stack buffer. Returns NULL when valid, else the complaint. */
858 #define STENCIL_FMT_MAXLEN 48
859 #define STENCIL_FMT_MAXWID 256
860 45 static const char *fmt_check(const char *p, uint32_t n)
861 {
862 45 const char *end = p + n;
863 45 int convs = 0;
864 45 ▶ if (n > STENCIL_FMT_MAXLEN)
Branch: 1 branch
BranchTrueFalseTotal
if (n > STENCIL_FMT_MAXLEN) 1 44 45
865 1 return "fmt: format is too long";
866 116 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 79 37 116
867 unsigned long w;
868 79 ▶ if (*p != '%') { p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '%') { p++; continue; } 34 45 79
869 45 p++;
870 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
871 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
872 45 ▶ || *p == '0' || *p == '#'))
Branch: 2 branches
BranchTrueFalseTotal
|| *p == '0' || *p == '#')) 1 44 45
|| *p == '0' || *p == '#')) 1 43 44
873 4 p++;
874 44 w = 0;
875 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
876 10 w = w * 10 + (unsigned long)(*p++ - '0');
877 44 ▶ if (w > STENCIL_FMT_MAXWID)
Branch: 1 branch
BranchTrueFalseTotal
if (w > STENCIL_FMT_MAXWID) 1 43 44
878 1 return "fmt: width is too large";
879 43 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 42 1 43
if (p < end && *p == '.') { 13 29 42
880 13 p++;
881 13 w = 0;
882 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
883 17 w = w * 10 + (unsigned long)(*p++ - '0');
884 13 ▶ if (w > STENCIL_FMT_MAXWID)
Branch: 1 branch
BranchTrueFalseTotal
if (w > STENCIL_FMT_MAXWID) 1 12 13
885 1 return "fmt: precision is too large";
886 }
887 42 ▶ if (p >= end)
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) 1 41 42
888 1 return "fmt: format ends inside a conversion";
889 41 ▶ if (*p == '*')
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '*') 1 40 41
890 1 return "fmt: '*' is not allowed";
891 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
892 3 return "fmt: conversion must be one of diouxXeEfgGs";
893 37 p++;
894 37 convs++;
895 }
896 37 ▶ if (convs != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (convs != 1) 2 35 37
897 return convs ? "fmt: only one conversion is allowed"
898 2 ▶ : "fmt: format needs one % conversion";
Branch: 1 branch
BranchTrueFalseTotal
: "fmt: format needs one % conversion"; 1 1 2
899 35 return NULL;
900 }
901  
902 /* parse `| name` / `| name(arg)` chains and emit SOP_FILTERs */
903 62060 static int parse_filters(stencil_compiler *c)
904 {
905 176 for (;;) {
906 const char *fname;
907 size_t fnlen;
908 stencil_cfilt f;
909 uint32_t foff;
910  
911 62236 skip_ws(c);
912 62236 ▶ if (c->p >= c->end || *c->p != '|')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '|') 62234 2 62236
if (c->p >= c->end || *c->p != '|') 62043 191 62234
913 62060 return 1;
914 191 c->p++;
915 191 skip_ws(c);
916 191 fnlen = word_len(c->p, c->end);
917 191 ▶ if (!fnlen)
Branch: 1 branch
BranchTrueFalseTotal
if (!fnlen) 1 190 191
918 1 return cerror(c, c->p, "expected a filter name after '|'");
919 190 fname = c->p;
920 190 c->p += fnlen;
921  
922 190 memset(&f, 0, sizeof f);
923 190 f.name_off = stencil_intern_get(&c->lit_intern, &c->pool, fname,
924 (uint32_t)fnlen);
925 190 ▶ if (f.name_off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (f.name_off == STENCIL_ARENA_NULL) 0 190 190
926 0 return coom(c);
927 190 f.name_len = (uint32_t)fnlen;
928 190 f.builtin_id = filter_builtin(fname, (uint32_t)fnlen);
929  
930 190 skip_ws(c);
931 190 ▶ if (c->p < c->end && *c->p == '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '(') { 190 0 190
if (c->p < c->end && *c->p == '(') { 72 118 190
932 72 c->p++;
933 72 skip_ws(c);
934 72 ▶ if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 72 0 72
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 22 50 72
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 15 7 22
935 65 char quote = *c->p;
936 65 const char *q = c->p + 1;
937 349 ▶ while (q < c->end && *q != quote) {
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && *q != quote) { 349 0 349
while (q < c->end && *q != quote) { 284 65 349
938 284 ▶ if (*q == '\\' && q + 1 < c->end
Branch: 2 branches
BranchTrueFalseTotal
if (*q == '\\' && q + 1 < c->end 0 284 284
if (*q == '\\' && q + 1 < c->end 0 0 0
939 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
940 0 q += 2;
941 else
942 284 q++;
943 }
944 65 ▶ if (q >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= c->end) 0 65 65
945 0 return cerror(c, c->p, "unterminated filter argument");
946 /* store raw (escapes are rare in filter args; unescape
947 * with the same rule as expression strings) */
948 {
949 65 const char *r = c->p + 1;
950 char *w;
951 65 uint32_t off = stencil_arena_alloc(&c->pool,
952 65 (size_t)(q - r), 1);
953 65 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 65 65
954 0 return coom(c);
955 65 w = c->pool.base + off;
956 349 ▶ while (r < q) {
Branch: 1 branch
BranchTrueFalseTotal
while (r < q) { 284 65 349
957 284 ▶ if (*r == '\\' && (r[1] == quote || r[1] == '\\'))
Branch: 3 branches
BranchTrueFalseTotal
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 284 284
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
if (*r == '\\' && (r[1] == quote || r[1] == '\\')) 0 0 0
958 0 r++;
959 284 *w++ = *r++;
960 }
961 65 f.str_off = off;
962 65 f.str_len = (uint32_t)(w - (c->pool.base + off));
963 65 c->pool.used = (size_t)(w - c->pool.base);
964 }
965 65 f.has_arg = 1;
966 65 c->p = q + 1;
967 7 ▶ } else if (c->p < c->end
Branch: 1 branch
BranchTrueFalseTotal
} else if (c->p < c->end 7 0 7
968 7 ▶ && (isDIGIT(*c->p)
Branch: 1 branch
BranchTrueFalseTotal
&& (isDIGIT(*c->p) 0 7 7
969 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
970 0 ▶ && isDIGIT(c->p[1])))) {
Branch: 1 branch
BranchTrueFalseTotal
&& isDIGIT(c->p[1])))) { 0 0 0
971 7 double val = 0.0, frac = 0.1;
972 7 int neg = 0;
973 7 ▶ if (*c->p == '-') { neg = 1; c->p++; }
Branch: 1 branch
BranchTrueFalseTotal
if (*c->p == '-') { neg = 1; c->p++; } 0 7 7
974 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
975 7 val = val * 10.0 + (*c->p++ - '0');
976 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
977 1 c->p++;
978 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
979 1 val += (*c->p++ - '0') * frac;
980 1 frac *= 0.1;
981 }
982 }
983 7 ▶ f.num_arg = neg ? -val : val;
Branch: 1 branch
BranchTrueFalseTotal
f.num_arg = neg ? -val : val; 0 7 7
984 7 f.arg_is_num = 1;
985 7 f.has_arg = 1;
986 } else {
987 0 return cerror(c, c->p,
988 "filter argument must be a quoted string or number");
989 }
990 72 skip_ws(c);
991 72 ▶ if (c->p >= c->end || *c->p != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ')') 72 0 72
if (c->p >= c->end || *c->p != ')') 0 72 72
992 0 return cerror(c, c->p, "expected ')'");
993 72 c->p++;
994 }
995  
996 /* arity for built-ins is known at compile time */
997 190 ▶ if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg)
Branch: 2 branches
BranchTrueFalseTotal
if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg) 22 168 190
if (f.builtin_id == STENCIL_FILT_DEFAULT && !f.has_arg) 1 21 22
998 1 return cerror(c, fname, "filter 'default' needs an argument");
999 189 ▶ if (f.builtin_id == STENCIL_FILT_FMT) {
Branch: 1 branch
BranchTrueFalseTotal
if (f.builtin_id == STENCIL_FILT_FMT) { 47 142 189
1000 const char *bad;
1001 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
1002 2 return cerror(c, fname,
1003 "filter 'fmt' needs a quoted format argument");
1004 45 bad = fmt_check(c->pool.base + f.str_off, f.str_len);
1005 45 ▶ if (bad)
Branch: 1 branch
BranchTrueFalseTotal
if (bad) 10 35 45
1006 10 return cerror(c, fname, "%s", bad);
1007 }
1008 177 ▶ 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 161 16 177
if (f.builtin_id >= 0 && f.builtin_id != STENCIL_FILT_DEFAULT 140 21 161
1009 140 ▶ && f.builtin_id != STENCIL_FILT_FMT && f.has_arg)
Branch: 2 branches
BranchTrueFalseTotal
&& f.builtin_id != STENCIL_FILT_FMT && f.has_arg) 105 35 140
&& f.builtin_id != STENCIL_FILT_FMT && f.has_arg) 1 104 105
1010 1 return cerror(c, fname, "filter '%.*s' takes no argument",
1011 (int)fnlen, fname);
1012  
1013 176 foff = stencil_arena_alloc(&c->filts, sizeof(stencil_cfilt),
1014 sizeof(double));
1015 176 ▶ if (foff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (foff == STENCIL_ARENA_NULL) 0 176 176
1016 0 return coom(c);
1017 176 memcpy(c->filts.base + foff, &f, sizeof f);
1018 176 ▶ 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)) 176 0 176
if (!emit_op(c, SOP_FILTER, 0) || !emit_u32(c, c->n_filts)) 0 176 176
1019 0 return 0;
1020 176 c->n_filts++;
1021 }
1022 }
1023  
1024 /* ====================================================================
1025 * Statements
1026 * ==================================================================== */
1027  
1028 24612 static stencil_blk *blk_open(stencil_compiler *c, uint8_t kind,
1029 uint32_t line, uint32_t col)
1030 {
1031 stencil_blk *b;
1032 24612 ▶ if (c->n_blk >= STENCIL_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk >= STENCIL_MAX_DEPTH) { 3 24609 24612
1033 3 cerror_at(c, line, col, "blocks nested deeper than %d",
1034 STENCIL_MAX_DEPTH);
1035 3 return NULL;
1036 }
1037 24609 b = &c->blk[c->n_blk++];
1038 24609 memset(b, 0, sizeof *b);
1039 24609 b->kind = kind;
1040 24609 b->line = line;
1041 24609 b->col = col;
1042 24609 b->cond_patch = STENCIL_PATCH_END;
1043 24609 b->jump_chain = STENCIL_PATCH_END;
1044 24609 b->arm_head = STENCIL_PATCH_END;
1045 24609 b->arm_tail = STENCIL_PATCH_END;
1046 24609 b->binds_at_entry = c->cur_binds;
1047 24609 return b;
1048 }
1049  
1050 13 static const char *blk_kind_name(uint8_t kind)
1051 {
1052 return kind == BLK_FOR ? "for"
1053 13 ▶ : kind == BLK_UNLESS ? "unless"
Branch: 3 branches
BranchTrueFalseTotal
: kind == BLK_UNLESS ? "unless" 11 2 13
: kind == BLK_UNLESS ? "unless" 10 1 11
: kind == BLK_UNLESS ? "unless" 4 6 10
1054 : kind == BLK_SWITCH ? "switch"
1055 : "if";
1056 }
1057  
1058 /* A branch's set-binds are popped at run time when the branch's code
1059 * path leaves the block; each branch pops only its own binds (they are
1060 * counted per-branch, not per-block). */
1061 27671 static int emit_pop_binds(stencil_compiler *c, stencil_blk *b)
1062 {
1063 27671 int32_t delta = c->cur_binds - b->binds_at_entry;
1064 27671 ▶ if (delta <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (delta <= 0) 27594 77 27671
1065 27594 return 1;
1066 77 c->cur_binds = b->binds_at_entry;
1067 77 ▶ 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); 77 0 77
return emit_op(c, SOP_POP_BINDS, 0) && emit_u32(c, (uint32_t)delta); 77 0 77
1068 }
1069  
1070 21920 static int stmt_if(stencil_compiler *c, uint8_t kind, uint32_t line,
1071 uint32_t col)
1072 {
1073 21920 stencil_blk *b = blk_open(c, kind, line, col);
1074 21920 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 2 21918 21920
1075 2 return 0;
1076 21918 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 21893 25 21918
if (!parse_expr(c) || !expect_close(c)) 9 21884 21893
1077 34 return 0;
1078 21884 ▶ 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)) 20009 1875 21884
if (!emit_op(c, kind == BLK_UNLESS ? SOP_TEST_JT : SOP_TEST_JF, -1)) 0 21884 21884
1079 0 return 0;
1080 21884 b->cond_patch = code_pos(c);
1081 21884 return emit_u32(c, STENCIL_PATCH_END);
1082 }
1083  
1084 36 static int stmt_elsif(stencil_compiler *c, uint32_t line, uint32_t col)
1085 {
1086 stencil_blk *b;
1087 36 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 24 12 36
1088 24 return cerror_at(c, line, col, "'elsif' with no open 'if'");
1089 12 b = &c->blk[c->n_blk - 1];
1090 12 ▶ if (b->kind != BLK_IF)
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind != BLK_IF) 3 9 12
1091 3 return cerror_at(c, line, col, "'elsif' inside '%s' block",
1092 3 blk_kind_name(b->kind));
1093 9 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 1 8 9
1094 1 return cerror_at(c, line, col, "'elsif' after 'else'");
1095 8 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 8 8
1096 0 return 0;
1097 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
1098 0 return 0;
1099 8 patch_u32(c, b->cond_patch, code_pos(c));
1100 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
1101 0 return 0;
1102 8 ▶ if (!emit_op(c, SOP_TEST_JF, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_TEST_JF, -1)) 0 8 8
1103 0 return 0;
1104 8 b->cond_patch = code_pos(c);
1105 8 return emit_u32(c, STENCIL_PATCH_END);
1106 }
1107  
1108 144 static int stmt_else(stencil_compiler *c, uint32_t line, uint32_t col)
1109 {
1110 stencil_blk *b;
1111 144 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 25 119 144
1112 25 return cerror_at(c, line, col, "'else' with no open block");
1113 119 b = &c->blk[c->n_blk - 1];
1114 119 ▶ if (b->kind == BLK_FOR)
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_FOR) 1 118 119
1115 1 return cerror_at(c, line, col, "'else' inside 'for' block");
1116 118 ▶ if (b->kind == BLK_SWITCH)
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_SWITCH) 2 116 118
1117 2 return cerror_at(c, line, col,
1118 "'else' inside 'switch' block (use 'default')");
1119 116 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 1 115 116
1120 1 return cerror_at(c, line, col, "duplicate 'else'");
1121 115 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 115 115
1122 0 return 0;
1123 115 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 115 115
1124 0 return 0;
1125 115 ▶ 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 115 115
1126 0 return 0;
1127 115 patch_u32(c, b->cond_patch, code_pos(c));
1128 115 b->cond_patch = STENCIL_PATCH_END;
1129 115 b->has_else = 1;
1130 115 return 1;
1131 }
1132  
1133 static int switch_tables(stencil_compiler *c, const stencil_blk *b);
1134  
1135 24083 static int stmt_end(stencil_compiler *c, uint32_t line, uint32_t col)
1136 {
1137 stencil_blk *b;
1138 24083 ▶ if (!c->n_blk)
Branch: 1 branch
BranchTrueFalseTotal
if (!c->n_blk) 18 24065 24083
1139 18 return cerror_at(c, line, col, "'end' with no open block");
1140 24065 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 24065 24065
1141 0 return 0;
1142 24065 b = &c->blk[--c->n_blk];
1143 24065 ▶ if (!emit_pop_binds(c, b))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b)) 0 24065 24065
1144 0 return 0;
1145 24065 ▶ if (b->kind == BLK_FOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_FOR) { 1713 22352 24065
1146 /* the pops sit before FOR_NEXT so every iteration gets a fresh
1147 * bind scope */
1148 1713 ▶ 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)) 1713 0 1713
if (!emit_op(c, SOP_FOR_NEXT, 0) || !emit_u32(c, b->body_start)) 0 1713 1713
1149 0 return 0;
1150 1713 patch_u32(c, b->for_end_patch, code_pos(c));
1151 1713 c->cur_frames--;
1152 } else {
1153 22352 ▶ if (b->kind == BLK_SWITCH && !b->arms)
Branch: 2 branches
BranchTrueFalseTotal
if (b->kind == BLK_SWITCH && !b->arms) 669 21683 22352
if (b->kind == BLK_SWITCH && !b->arms) 3 666 669
1154 3 return cerror_at(c, b->line, b->col, "empty switch body");
1155 22349 ▶ if (b->cond_patch != STENCIL_PATCH_END)
Branch: 1 branch
BranchTrueFalseTotal
if (b->cond_patch != STENCIL_PATCH_END) 21712 637 22349
1156 21712 patch_u32(c, b->cond_patch, code_pos(c));
1157 22349 patch_chain(c, b->jump_chain, code_pos(c));
1158 22349 ▶ if (b->kind == BLK_SWITCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->kind == BLK_SWITCH) { 666 21683 22349
1159 /* every arm's end jump and the last case's miss land here,
1160 * where the topic is released */
1161 666 c->open_switches--;
1162 666 ▶ if (!emit_op(c, SOP_POP, -1) || !switch_tables(c, b))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_POP, -1) || !switch_tables(c, b)) 666 0 666
if (!emit_op(c, SOP_POP, -1) || !switch_tables(c, b)) 0 666 666
1163 0 return 0;
1164 }
1165 }
1166 24062 return 1;
1167 }
1168  
1169 1742 static int stmt_for(stencil_compiler *c, uint32_t line, uint32_t col)
1170 {
1171 1742 const char *n1, *n2 = NULL;
1172 1742 size_t l1, l2 = 0;
1173 1742 uint32_t id1, id2 = 0;
1174 stencil_blk *b;
1175  
1176 1742 skip_ws(c);
1177 1742 l1 = word_len(c->p, c->end);
1178 1742 ▶ if (!l1 || is_keyword(c->p, l1))
Branch: 2 branches
BranchTrueFalseTotal
if (!l1 || is_keyword(c->p, l1)) 1724 18 1742
if (!l1 || is_keyword(c->p, l1)) 2 1722 1724
1179 20 return cerror(c, c->p, "expected a loop variable name");
1180 1722 n1 = c->p;
1181 1722 c->p += l1;
1182 1722 skip_ws(c);
1183 1722 ▶ if (c->p < c->end && *c->p == ',') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ',') { 1722 0 1722
if (c->p < c->end && *c->p == ',') { 84 1638 1722
1184 84 c->p++;
1185 84 skip_ws(c);
1186 84 l2 = word_len(c->p, c->end);
1187 84 ▶ if (!l2 || is_keyword(c->p, l2))
Branch: 2 branches
BranchTrueFalseTotal
if (!l2 || is_keyword(c->p, l2)) 84 0 84
if (!l2 || is_keyword(c->p, l2)) 0 84 84
1188 0 return cerror(c, c->p, "expected a value variable name");
1189 84 n2 = c->p;
1190 84 c->p += l2;
1191 84 skip_ws(c);
1192 }
1193 {
1194 1722 size_t wl = word_len(c->p, c->end);
1195 1722 ▶ if (!wl || !word_is(c->p, wl, "in"))
Branch: 2 branches
BranchTrueFalseTotal
if (!wl || !word_is(c->p, wl, "in")) 1719 3 1722
if (!wl || !word_is(c->p, wl, "in")) 1 1718 1719
1196 4 return cerror(c, c->p, "expected 'in'");
1197 1718 c->p += wl;
1198 }
1199 1718 skip_ws(c);
1200 1718 ▶ if (!parse_path_push(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!parse_path_push(c)) 1 1717 1718
1201 1 return 0;
1202 1717 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 1717 1717
1203 0 return 0;
1204  
1205 1717 id1 = name_intern(c, n1, (uint32_t)l1);
1206 1717 ▶ if (id1 == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id1 == STENCIL_ARENA_NULL) 0 1717 1717
1207 0 return coom(c);
1208 1717 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 84 1633 1717
1209 84 id2 = name_intern(c, n2, (uint32_t)l2);
1210 84 ▶ if (id2 == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id2 == STENCIL_ARENA_NULL) 0 84 84
1211 0 return coom(c);
1212 }
1213  
1214 1717 b = blk_open(c, BLK_FOR, line, col);
1215 1717 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 0 1717 1717
1216 0 return 0;
1217 1717 ▶ if (n2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n2) { 84 1633 1717
1218 168 if (!emit_op(c, SOP_FOR_HASH, -1) ||
1219 168 ▶ !emit_u32(c, id1) || !emit_u32(c, id2))
Branch: 1 branch
BranchTrueFalseTotal
!emit_u32(c, id1) || !emit_u32(c, id2)) 0 84 84
1220 0 return 0;
1221 } else {
1222 1633 ▶ 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)) 1633 0 1633
if (!emit_op(c, SOP_FOR_ARY, -1) || !emit_u32(c, id1)) 0 1633 1633
1223 0 return 0;
1224 }
1225 1717 b->for_end_patch = code_pos(c);
1226 1717 ▶ if (!emit_u32(c, STENCIL_PATCH_END))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_u32(c, STENCIL_PATCH_END)) 0 1717 1717
1227 0 return 0;
1228 1717 b->body_start = code_pos(c);
1229 1717 c->cur_frames++;
1230 1717 ▶ if (c->cur_frames > c->max_frames)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_frames > c->max_frames) 1630 87 1717
1231 1630 c->max_frames = c->cur_frames;
1232 1717 return 1;
1233 }
1234  
1235 141 static int stmt_set(stencil_compiler *c)
1236 {
1237 const char *n1;
1238 size_t l1;
1239 uint32_t id;
1240  
1241 141 skip_ws(c);
1242 141 l1 = word_len(c->p, c->end);
1243 141 ▶ if (!l1 || is_keyword(c->p, l1))
Branch: 2 branches
BranchTrueFalseTotal
if (!l1 || is_keyword(c->p, l1)) 125 16 141
if (!l1 || is_keyword(c->p, l1)) 2 123 125
1244 18 return cerror(c, c->p, "expected a variable name after 'set'");
1245 123 n1 = c->p;
1246 123 c->p += l1;
1247 123 ▶ if (c->p < c->end && (*c->p == '.' || *c->p == '['))
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 123 0 123
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 121 2 123
if (c->p < c->end && (*c->p == '.' || *c->p == '[')) 0 121 121
1248 2 return cerror(c, c->p, "'set' target must be a plain name");
1249 121 skip_ws(c);
1250 121 ▶ if (c->p >= c->end || *c->p != '='
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '=' 121 0 121
if (c->p >= c->end || *c->p != '=' 112 9 121
1251 112 ▶ || (c->p + 1 < c->end && c->p[1] == '='))
Branch: 2 branches
BranchTrueFalseTotal
|| (c->p + 1 < c->end && c->p[1] == '=')) 112 0 112
|| (c->p + 1 < c->end && c->p[1] == '=')) 0 112 112
1252 9 return cerror(c, c->p, "expected '=' after the 'set' name");
1253 112 c->p++;
1254 112 ▶ if (!parse_expr(c) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_expr(c) || !expect_close(c)) 112 0 112
if (!parse_expr(c) || !expect_close(c)) 0 112 112
1255 0 return 0;
1256 112 id = name_intern(c, n1, (uint32_t)l1);
1257 112 ▶ if (id == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (id == STENCIL_ARENA_NULL) 0 112 112
1258 0 return coom(c);
1259 112 ▶ 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)) 112 0 112
if (!emit_op(c, SOP_SET, -1) || !emit_u32(c, id)) 0 112 112
1260 0 return 0;
1261 112 c->cur_binds++;
1262 112 ▶ if (c->cur_binds > c->max_binds)
Branch: 1 branch
BranchTrueFalseTotal
if (c->cur_binds > c->max_binds) 109 3 112
1263 109 c->max_binds = c->cur_binds;
1264 112 return 1;
1265 }
1266  
1267 66 static int stmt_include(stencil_compiler *c, uint32_t line, uint32_t col)
1268 {
1269 const char *n;
1270 size_t len;
1271 uint32_t i, off, ioff;
1272 stencil_cinc *tab, *e;
1273  
1274 66 skip_ws(c);
1275 66 n = c->p;
1276 596 ▶ while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%')
Branch: 3 branches
BranchTrueFalseTotal
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 595 1 596
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 537 58 595
while (c->p < c->end && !isSPACE(*c->p) && *c->p != '%') 530 7 537
1277 530 c->p++;
1278 66 len = (size_t)(c->p - n);
1279 66 ▶ if (!len)
Branch: 1 branch
BranchTrueFalseTotal
if (!len) 1 65 66
1280 1 return cerror(c, n, "expected a template name after 'include'");
1281 65 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 21 44 65
1282 21 return 0;
1283  
1284 44 tab = (stencil_cinc *)c->incs.base;
1285 44 ▶ for (i = 0; i < c->n_incs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->n_incs; i++) 1 43 44
1286 1 ▶ if (tab[i].name_len == len
Branch: 1 branch
BranchTrueFalseTotal
if (tab[i].name_len == len 1 0 1
1287 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
1288 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
1289  
1290 43 off = stencil_intern_get(&c->lit_intern, &c->pool, n, (uint32_t)len);
1291 43 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 43 43
1292 0 return coom(c);
1293 43 ioff = stencil_arena_alloc(&c->incs, sizeof(stencil_cinc),
1294 sizeof(uint32_t));
1295 43 ▶ if (ioff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (ioff == STENCIL_ARENA_NULL) 0 43 43
1296 0 return coom(c);
1297 43 e = (stencil_cinc *)(c->incs.base + ioff);
1298 43 e->name_off = off;
1299 43 e->name_len = (uint32_t)len;
1300 43 e->line = line;
1301 43 e->col = col;
1302 43 ▶ 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)) 43 0 43
if (!emit_op(c, SOP_INCLUDE, 0) || !emit_u32(c, c->n_incs)) 0 43 43
1303 0 return 0;
1304 43 c->n_incs++;
1305 43 return 1;
1306 }
1307  
1308 /* ====================================================================
1309 * {% switch EXPR %} {% case PATTERN %} ... {% default %} ... {% end %}
1310 *
1311 * The topic is pushed once and stays on the value stack for the whole
1312 * block (every statement inside leaves the stack as it found it), so each
1313 * SOP_CASE tests stack[sp-1] without popping it. Arms chain like if/elsif:
1314 * a miss jumps to the next arm's test, an arm's end jumps past the block,
1315 * and {% end %} pops the topic where they all meet. The pattern grammar
1316 * and its semantics are Switch::Declare's.
1317 *
1318 * SOP_CASE u8 op, u32 case id, u32 miss target (9 bytes)
1319 * SOP_CASE_TABLE u8 op, u32 table id, u32 miss target (the same shape)
1320 * ==================================================================== */
1321  
1322 #define CASE_OP_ID_AT 1 /* operand offsets within a SOP_CASE */
1323 #define CASE_OP_MISS_AT 5
1324 #define CASE_OP_LEN 9
1325  
1326 /* the compile-time arenas for switch tables are made on first use, so a
1327 * template without a switch pays nothing for them */
1328 10893 static int arena_on_demand(stencil_arena *a)
1329 {
1330 10893 ▶ return a->base || stencil_arena_init(a, 64);
Branch: 2 branches
BranchTrueFalseTotal
return a->base || stencil_arena_init(a, 64); 2272 8621 10893
return a->base || stencil_arena_init(a, 64); 2272 0 2272
1331 }
1332  
1333 3634 static int case_store(stencil_compiler *c, const stencil_ccase *cs,
1334 uint32_t *id)
1335 {
1336 uint32_t off;
1337 3634 ▶ if (!arena_on_demand(&c->cases))
Branch: 1 branch
BranchTrueFalseTotal
if (!arena_on_demand(&c->cases)) 0 3634 3634
1338 0 return coom(c);
1339 3634 off = stencil_arena_alloc(&c->cases, sizeof *cs, sizeof(uint32_t));
1340 3634 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 3634 3634
1341 0 return coom(c);
1342 3634 memcpy(c->cases.base + off, cs, sizeof *cs);
1343 3634 *id = c->n_cases++;
1344 3634 return 1;
1345 }
1346  
1347 3099 static int cval_store(stencil_compiler *c, const stencil_cval *v)
1348 {
1349 uint32_t off;
1350 3099 ▶ if (!arena_on_demand(&c->cvals))
Branch: 1 branch
BranchTrueFalseTotal
if (!arena_on_demand(&c->cvals)) 0 3099 3099
1351 0 return coom(c);
1352 3099 off = stencil_arena_alloc(&c->cvals, sizeof *v, STENCIL_CVAL_ALIGN);
1353 3099 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 3099 3099
1354 0 return coom(c);
1355 3099 memcpy(c->cvals.base + off, v, sizeof *v);
1356 3099 c->n_cvals++;
1357 3099 return 1;
1358 }
1359  
1360 /* a number can start here, in case spelling: 5 -5 +5 .5 -.5 */
1361 1301 static int case_number_start(const stencil_compiler *c)
1362 {
1363 1301 const char *q = c->p;
1364 1301 ▶ if (q < c->end && (*q == '-' || *q == '+'))
Branch: 3 branches
BranchTrueFalseTotal
if (q < c->end && (*q == '-' || *q == '+')) 1301 0 1301
if (q < c->end && (*q == '-' || *q == '+')) 1285 16 1301
if (q < c->end && (*q == '-' || *q == '+')) 6 1279 1285
1365 22 q++;
1366 1301 ▶ if (q < c->end && *q == '.')
Branch: 2 branches
BranchTrueFalseTotal
if (q < c->end && *q == '.') 1301 0 1301
if (q < c->end && *q == '.') 6 1295 1301
1367 6 q++;
1368 1301 ▶ return q < c->end && isDIGIT(*q);
Branch: 2 branches
BranchTrueFalseTotal
return q < c->end && isDIGIT(*q); 1301 0 1301
return q < c->end && isDIGIT(*q); 976 325 1301
1369 }
1370  
1371 /* One case literal into *v: a number (compared with ==) or a quoted
1372 * string (compared with eq) - typed by its spelling, as in Switch::Declare.
1373 * Strings follow the template's own quoting rules (\' \" \\ only). */
1374 3102 static int case_literal(stencil_compiler *c, stencil_cval *v)
1375 {
1376 3102 memset(v, 0, sizeof *v);
1377 3102 ▶ if (c->p < c->end && (*c->p == '\'' || *c->p == '"'))
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) 3102 0 3102
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) 3099 3 3102
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) 2450 649 3099
1378 2453 return lex_string(c, &v->str_off, &v->str_len);
1379 649 ▶ if (case_number_start(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (case_number_start(c)) { 647 2 649
1380 stencil_lexnum num;
1381 647 ▶ if (!lex_number(c, LEXNUM_CASE, &num))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_number(c, LEXNUM_CASE, &num)) 0 647 647
1382 0 return 0;
1383 647 v->is_num = 1;
1384 647 v->is_int = (uint8_t)num.is_int;
1385 647 v->is_uv = (uint8_t)num.is_uv;
1386 647 v->iv = num.iv;
1387 647 v->nv = num.nv;
1388 647 return 1;
1389 }
1390 2 return cerror(c, c->p, "expected a number or a quoted string");
1391 }
1392  
1393 /* [LO .. HI] (inclusive range) or [a, b, c] (membership); c->p at '[' */
1394 179 static int case_bracket(stencil_compiler *c, stencil_ccase *cs)
1395 {
1396 stencil_cval v;
1397 179 c->p++;
1398 179 skip_ws(c);
1399 179 ▶ if (c->p < c->end && *c->p == ']')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ']') 179 0 179
if (c->p < c->end && *c->p == ']') 1 178 179
1400 1 return cerror(c, c->p, "empty list in case pattern");
1401 178 ▶ if (!case_literal(c, &v))
Branch: 1 branch
BranchTrueFalseTotal
if (!case_literal(c, &v)) 1 177 178
1402 1 return 0;
1403 177 cs->a = c->n_cvals;
1404 177 ▶ if (!cval_store(c, &v))
Branch: 1 branch
BranchTrueFalseTotal
if (!cval_store(c, &v)) 0 177 177
1405 0 return 0;
1406 177 skip_ws(c);
1407 177 ▶ if (c->p < c->end && *c->p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '.') { 177 0 177
if (c->p < c->end && *c->p == '.') { 89 88 177
1408 89 ▶ if (!(c->p + 1 < c->end && c->p[1] == '.'))
Branch: 2 branches
BranchTrueFalseTotal
if (!(c->p + 1 < c->end && c->p[1] == '.')) 89 0 89
if (!(c->p + 1 < c->end && c->p[1] == '.')) 1 88 89
1409 1 return cerror(c, c->p, "expected '..' in range case pattern");
1410 88 c->p += 2;
1411 88 skip_ws(c);
1412 88 ▶ if (!case_literal(c, &v) || !cval_store(c, &v))
Branch: 2 branches
BranchTrueFalseTotal
if (!case_literal(c, &v) || !cval_store(c, &v)) 88 0 88
if (!case_literal(c, &v) || !cval_store(c, &v)) 0 88 88
1413 0 return 0;
1414 88 skip_ws(c);
1415 88 ▶ if (c->p >= c->end || *c->p != ']')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ']') 88 0 88
if (c->p >= c->end || *c->p != ']') 1 87 88
1416 1 return cerror(c, c->p, "expected ']' to close the range");
1417 87 c->p++;
1418 87 cs->kind = STENCIL_CASE_RANGE;
1419 87 return 1;
1420 }
1421 88 cs->kind = STENCIL_CASE_LIST;
1422 88 cs->b = 1;
1423 for (;;) {
1424 181 skip_ws(c);
1425 181 ▶ if (c->p < c->end && *c->p == ']')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ']') 181 0 181
if (c->p < c->end && *c->p == ']') 84 97 181
1426 84 break;
1427 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 != ',') 1 96 97
1428 1 return cerror(c, c->p, "expected ',' or ']' in case list");
1429 96 c->p++;
1430 96 skip_ws(c);
1431 96 ▶ if (c->p < c->end && *c->p == ']')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == ']') 96 0 96
if (c->p < c->end && *c->p == ']') 2 94 96
1432 2 break; /* trailing comma */
1433 94 ▶ if (!case_literal(c, &v) || !cval_store(c, &v))
Branch: 2 branches
BranchTrueFalseTotal
if (!case_literal(c, &v) || !cval_store(c, &v)) 93 1 94
if (!case_literal(c, &v) || !cval_store(c, &v)) 0 93 93
1434 1 return 0;
1435 93 cs->b++;
1436 }
1437 86 c->p++;
1438 86 return 1;
1439 }
1440  
1441 /* Compile the pattern and copy the program into the rx region. Its own
1442 * function so the 19 KB work area lives only for the duration of the call. */
1443 427 static int case_regex_compile(stencil_compiler *c, stencil_ccase *cs,
1444 const char *pat, size_t plen, unsigned flags,
1445 uint32_t line0, const char *ls0)
1446 {
1447 stencil_rx_work w;
1448 stencil_rx_err err;
1449 const stencil_rx *rx;
1450 uint32_t off;
1451 427 rx = stencil_rx_compile(pat, plen,
1452 427 (c->flags & STENCIL_PROG_SRC_UTF8) ? 1 : 0,
1453 flags, &w, &err);
1454 427 ▶ if (!rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!rx) { 221 206 427
1455 /* point at the offending byte, which may be lines into the pattern */
1456 221 const char *at = pat + err.at, *q;
1457 221 uint32_t line = line0;
1458 221 const char *ls = ls0;
1459 1983 ▶ for (q = pat; q < at; q++)
Branch: 1 branch
BranchTrueFalseTotal
for (q = pat; q < at; q++) 1762 221 1983
1460 1762 ▶ if (*q == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*q == '\n') { 2 1760 1762
1461 2 line++;
1462 2 ls = q + 1;
1463 }
1464 221 return cerror_at(c, line, (uint32_t)(at - ls) + 1, "regex: %s",
1465 err.msg);
1466 }
1467 206 ▶ if (!arena_on_demand(&c->rxs))
Branch: 1 branch
BranchTrueFalseTotal
if (!arena_on_demand(&c->rxs)) 0 206 206
1468 0 return coom(c);
1469 206 off = stencil_arena_alloc(&c->rxs, rx->size, sizeof(uint32_t));
1470 206 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 206 206
1471 0 return coom(c);
1472 206 memcpy(c->rxs.base + off, rx, rx->size);
1473 206 cs->kind = STENCIL_CASE_RX;
1474 206 cs->a = off;
1475 206 cs->b = flags;
1476 204 cs->str_off = plen ? stencil_intern_get(&c->lit_intern, &c->pool, pat,
1477 (uint32_t)plen)
1478 206 ▶ : 0;
Branch: 1 branch
BranchTrueFalseTotal
: 0; 204 2 206
1479 206 ▶ if (cs->str_off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cs->str_off == STENCIL_ARENA_NULL) 0 206 206
1480 0 return coom(c);
1481 206 cs->str_len = (uint32_t)plen;
1482 206 return 1;
1483 }
1484  
1485 /* /PATTERN/flags; c->p at the opening '/'. The pattern runs to the next
1486 * unescaped '/' ('\/' for a literal slash, as in perl) and may span lines;
1487 * a "%}" inside it is pattern text, not the end of the tag. */
1488 438 static int case_regex(stencil_compiler *c, stencil_ccase *cs)
1489 {
1490 438 const char *open = c->p, *pat = c->p + 1, *q = c->p + 1;
1491 438 uint32_t line0 = c->line;
1492 438 const char *ls0 = c->line_start;
1493 438 unsigned flags = 0;
1494 32777 ▶ while (q < c->end && *q != '/')
Branch: 2 branches
BranchTrueFalseTotal
while (q < c->end && *q != '/') 32767 10 32777
while (q < c->end && *q != '/') 32339 428 32767
1495 32339 ▶ q += (*q == '\\' && q + 1 < c->end) ? 2 : 1;
Branch: 2 branches
BranchTrueFalseTotal
q += (*q == '\\' && q + 1 < c->end) ? 2 : 1; 733 31606 32339
q += (*q == '\\' && q + 1 < c->end) ? 2 : 1; 733 0 733
1496 438 ▶ if (q >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q >= c->end) 10 428 438
1497 10 return cerror(c, open, "unterminated regex in case pattern");
1498 428 advance_lines(c, pat, q);
1499 428 c->p = q + 1;
1500 450 ▶ while (c->p < c->end && isALPHA(*c->p)) {
Branch: 2 branches
BranchTrueFalseTotal
while (c->p < c->end && isALPHA(*c->p)) { 450 0 450
while (c->p < c->end && isALPHA(*c->p)) { 23 427 450
1501 23 switch (*c->p) {
1502 9 case 'i': flags |= STENCIL_RX_I; break;
1503 4 case 'm': flags |= STENCIL_RX_M; break;
1504 3 case 's': flags |= STENCIL_RX_S; break;
1505 6 case 'x': flags |= STENCIL_RX_X; break;
1506 1 default:
1507 1 return cerror(c, c->p,
1508 1 "unsupported regex flag '%c' (only i, m, s and x)", *c->p);
1509 }
1510 22 c->p++;
1511 }
1512 427 return case_regex_compile(c, cs, pat, (size_t)(q - pat), flags,
1513 line0, ls0);
1514 }
1515  
1516 /* the data path operand of == / eq / =~ */
1517 249 static int case_path_operand(stencil_compiler *c, stencil_ccase *cs,
1518 uint32_t kind, const char *op)
1519 {
1520 uint32_t pid;
1521 249 skip_ws(c);
1522 249 ▶ if (!word_len(c->p, c->end))
Branch: 1 branch
BranchTrueFalseTotal
if (!word_len(c->p, c->end)) 4 245 249
1523 4 return cerror(c, c->p, "expected a data path after '%s'", op);
1524 245 pid = path_parse(c);
1525 245 ▶ if (pid == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (pid == STENCIL_ARENA_NULL) 0 245 245
1526 0 return 0;
1527 245 cs->kind = kind;
1528 245 cs->a = pid;
1529 245 return 1;
1530 }
1531  
1532 /* The optional (TYPE) after ref / reftype, required (Class) after isa:
1533 * a bareword, package-qualified with '::', or a quoted string. Sets
1534 * cs->b = 1 and cs->str_* when present. */
1535 186 static int case_type_arg(stencil_compiler *c, stencil_ccase *cs,
1536 const char *kw, int required)
1537 {
1538 186 skip_ws(c);
1539 186 ▶ if (c->p >= c->end || *c->p != '(') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '(') { 186 0 186
if (c->p >= c->end || *c->p != '(') { 16 170 186
1540 16 ▶ if (required)
Branch: 1 branch
BranchTrueFalseTotal
if (required) 1 15 16
1541 1 return cerror(c, c->p, "'%s' needs a class: isa(My::Class)", kw);
1542 15 return 1;
1543 }
1544 170 c->p++;
1545 170 skip_ws(c);
1546 170 ▶ if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) {
Branch: 3 branches
BranchTrueFalseTotal
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 170 0 170
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 170 0 170
if (c->p < c->end && (*c->p == '\'' || *c->p == '"')) { 5 165 170
1547 5 ▶ if (!lex_string(c, &cs->str_off, &cs->str_len))
Branch: 1 branch
BranchTrueFalseTotal
if (!lex_string(c, &cs->str_off, &cs->str_len)) 0 5 5
1548 0 return 0;
1549 5 ▶ if (!cs->str_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!cs->str_len) 1 4 5
1550 1 return cerror(c, c->p, "empty name in '%s(...)'", kw);
1551 } else {
1552 165 const char *s = c->p;
1553 size_t wl;
1554 for (;;) {
1555 237 wl = word_len(c->p, c->end);
1556 237 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 1 236 237
1557 1 return cerror(c, c->p, "expected a name inside '%s(...)'",
1558 kw);
1559 236 c->p += wl;
1560 236 ▶ if (c->end - c->p >= 3 && c->p[0] == ':' && c->p[1] == ':'
Branch: 3 branches
BranchTrueFalseTotal
if (c->end - c->p >= 3 && c->p[0] == ':' && c->p[1] == ':' 236 0 236
if (c->end - c->p >= 3 && c->p[0] == ':' && c->p[1] == ':' 73 163 236
if (c->end - c->p >= 3 && c->p[0] == ':' && c->p[1] == ':' 73 0 73
1561 73 ▶ && IS_WORD_START(c->p[2])) {
Branch: 2 branches
BranchTrueFalseTotal
&& IS_WORD_START(c->p[2])) { 1 72 73
&& IS_WORD_START(c->p[2])) { 0 1 1
1562 72 c->p += 2;
1563 72 continue;
1564 }
1565 164 break;
1566 }
1567 164 cs->str_len = (uint32_t)(c->p - s);
1568 164 cs->str_off = stencil_intern_get(&c->lit_intern, &c->pool, s,
1569 cs->str_len);
1570 164 ▶ if (cs->str_off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (cs->str_off == STENCIL_ARENA_NULL) 0 164 164
1571 0 return coom(c);
1572 }
1573 168 skip_ws(c);
1574 168 ▶ if (c->p >= c->end || *c->p != ')')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != ')') 168 0 168
if (c->p >= c->end || *c->p != ')') 2 166 168
1575 2 return cerror(c, c->p, "expected ')' after the name in '%s(...)'",
1576 kw);
1577 166 c->p++;
1578 166 cs->b = 1;
1579 166 return 1;
1580 }
1581  
1582 /* \&name: a user filter registered on the engine, used as a predicate. It
1583 * goes in the filter table like any other user filter, so the engine's
1584 * validate_filters refuses an unregistered name at compile time. */
1585 24 static int case_predicate(stencil_compiler *c, stencil_ccase *cs)
1586 {
1587 const char *name;
1588 size_t nl;
1589 stencil_cfilt f;
1590 uint32_t foff;
1591 24 c->p++; /* '\' */
1592 24 ▶ if (c->p >= c->end || *c->p != '&')
Branch: 2 branches
BranchTrueFalseTotal
if (c->p >= c->end || *c->p != '&') 24 0 24
if (c->p >= c->end || *c->p != '&') 1 23 24
1593 1 return cerror(c, c->p, "expected '&' after '\\' in case predicate");
1594 23 c->p++;
1595 23 nl = word_len(c->p, c->end);
1596 23 ▶ if (!nl)
Branch: 1 branch
BranchTrueFalseTotal
if (!nl) 1 22 23
1597 1 return cerror(c, c->p, "expected a filter name after '\\&'");
1598 22 name = c->p;
1599 22 ▶ if (filter_builtin(name, (uint32_t)nl) != STENCIL_FILT_USER)
Branch: 1 branch
BranchTrueFalseTotal
if (filter_builtin(name, (uint32_t)nl) != STENCIL_FILT_USER) 1 21 22
1600 1 return cerror(c, name, "'%.*s' is a built-in filter; a case "
1601 "predicate must be a registered filter", (int)nl,
1602 name);
1603 21 c->p += nl;
1604 21 memset(&f, 0, sizeof f);
1605 21 f.name_off = stencil_intern_get(&c->lit_intern, &c->pool, name,
1606 (uint32_t)nl);
1607 21 ▶ if (f.name_off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (f.name_off == STENCIL_ARENA_NULL) 0 21 21
1608 0 return coom(c);
1609 21 f.name_len = (uint32_t)nl;
1610 21 f.builtin_id = STENCIL_FILT_USER;
1611 21 foff = stencil_arena_alloc(&c->filts, sizeof(stencil_cfilt),
1612 sizeof(double));
1613 21 ▶ if (foff == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (foff == STENCIL_ARENA_NULL) 0 21 21
1614 0 return coom(c);
1615 21 memcpy(c->filts.base + foff, &f, sizeof f);
1616 21 cs->kind = STENCIL_CASE_PRED;
1617 21 cs->a = c->n_filts++;
1618 21 return 1;
1619 }
1620  
1621 /* One case PATTERN at c->p into *cs. */
1622 3892 static int case_pattern(stencil_compiler *c, stencil_ccase *cs)
1623 {
1624 size_t wl;
1625 3892 memset(cs, 0, sizeof *cs);
1626 3892 skip_ws(c);
1627 3892 ▶ if (c->p >= c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p >= c->end) 0 3892 3892
1628 0 return cerror(c, c->p, "expected a case pattern");
1629 3892 switch (*c->p) {
1630 438 case '/':
1631 438 return case_regex(c, cs);
1632 179 case '[':
1633 179 return case_bracket(c, cs);
1634 24 case '\\':
1635 24 return case_predicate(c, cs);
1636 186 case '=':
1637 186 ▶ if (c->p + 1 < c->end && c->p[1] == '=') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p + 1 < c->end && c->p[1] == '=') { 186 0 186
if (c->p + 1 < c->end && c->p[1] == '=') { 73 113 186
1638 73 c->p += 2;
1639 73 return case_path_operand(c, cs, STENCIL_CASE_PATH_NUM, "==");
1640 }
1641 113 ▶ if (c->p + 1 < c->end && c->p[1] == '~') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p + 1 < c->end && c->p[1] == '~') { 113 0 113
if (c->p + 1 < c->end && c->p[1] == '~') { 112 1 113
1642 112 c->p += 2;
1643 112 return case_path_operand(c, cs, STENCIL_CASE_PATH_RX, "=~");
1644 }
1645 1 return cerror(c, c->p, "expected '==' or '=~' in case pattern");
1646 2413 case '\'': case '"': {
1647 stencil_cval v;
1648 2413 cs->kind = STENCIL_CASE_VALUE;
1649 2413 cs->a = c->n_cvals;
1650 2413 ▶ return case_literal(c, &v) && cval_store(c, &v);
Branch: 2 branches
BranchTrueFalseTotal
return case_literal(c, &v) && cval_store(c, &v); 2412 1 2413
return case_literal(c, &v) && cval_store(c, &v); 2412 0 2412
1651 }
1652 }
1653 652 ▶ if (case_number_start(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (case_number_start(c)) { 329 323 652
1654 stencil_cval v;
1655 329 cs->kind = STENCIL_CASE_VALUE;
1656 329 cs->a = c->n_cvals;
1657 329 ▶ return case_literal(c, &v) && cval_store(c, &v);
Branch: 2 branches
BranchTrueFalseTotal
return case_literal(c, &v) && cval_store(c, &v); 329 0 329
return case_literal(c, &v) && cval_store(c, &v); 329 0 329
1658 }
1659 323 wl = word_len(c->p, c->end);
1660 323 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 1 322 323
1661 1 return cerror(c, c->p, "expected a case pattern");
1662 322 ▶ if (word_is(c->p, wl, "undef")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "undef")) { 69 253 322
1663 69 c->p += wl;
1664 69 cs->kind = STENCIL_CASE_UNDEF;
1665 69 return 1;
1666 }
1667 253 ▶ if (word_is(c->p, wl, "eq")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "eq")) { 64 189 253
1668 64 c->p += wl;
1669 64 return case_path_operand(c, cs, STENCIL_CASE_PATH_STR, "eq");
1670 }
1671 189 ▶ if (word_is(c->p, wl, "ref")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "ref")) { 85 104 189
1672 85 c->p += wl;
1673 85 cs->kind = STENCIL_CASE_REF;
1674 85 return case_type_arg(c, cs, "ref", 0);
1675 }
1676 104 ▶ if (word_is(c->p, wl, "reftype")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "reftype")) { 14 90 104
1677 14 c->p += wl;
1678 14 cs->kind = STENCIL_CASE_REFTYPE;
1679 14 return case_type_arg(c, cs, "reftype", 0);
1680 }
1681 90 ▶ if (word_is(c->p, wl, "isa")) {
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "isa")) { 87 3 90
1682 87 c->p += wl;
1683 87 cs->kind = STENCIL_CASE_ISA;
1684 87 return case_type_arg(c, cs, "isa", 1);
1685 }
1686 3 ▶ if (word_is(c->p, wl, "sub"))
Branch: 1 branch
BranchTrueFalseTotal
if (word_is(c->p, wl, "sub")) 1 2 3
1687 1 return cerror(c, c->p, "a case cannot contain Perl code; register "
1688 "a filter and use \\&name");
1689 2 return cerror(c, c->p, "unexpected '%.*s' in case pattern (to compare "
1690 "with data, use == %.*s or eq %.*s)", (int)wl, c->p,
1691 (int)wl, c->p, (int)wl, c->p);
1692 }
1693  
1694 /* ---- dispatch tables for runs of string arms ------------------------ */
1695  
1696 /* A case arm, recorded for the dispatch-table pass at {% end %}. */
1697 typedef struct stencil_armrec {
1698 uint32_t op_at; /* offset of the arm's SOP_CASE */
1699 uint32_t case_id;
1700 uint32_t next; /* next arm of the same switch, or STENCIL_PATCH_END */
1701 } stencil_armrec;
1702  
1703 #define ARMREC(c, i) (((stencil_armrec *)(c)->armrec.base) + (i))
1704  
1705 /* Runs of at least this many consecutive string arms become a table. Below
1706 * it, a chain of direct comparisons is as fast (measured: bench/switch.pl). */
1707 #define STENCIL_CASE_TABLE_MIN 6
1708  
1709 3634 static int arm_record(stencil_compiler *c, stencil_blk *b, uint32_t op_at,
1710 uint32_t case_id)
1711 {
1712 uint32_t off;
1713 stencil_armrec *arm;
1714 3634 ▶ if (!arena_on_demand(&c->armrec))
Branch: 1 branch
BranchTrueFalseTotal
if (!arena_on_demand(&c->armrec)) 0 3634 3634
1715 0 return coom(c);
1716 3634 off = stencil_arena_alloc(&c->armrec, sizeof *arm, sizeof(uint32_t));
1717 3634 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 3634 3634
1718 0 return coom(c);
1719 3634 arm = (stencil_armrec *)(c->armrec.base + off);
1720 3634 arm->op_at = op_at;
1721 3634 arm->case_id = case_id;
1722 3634 arm->next = STENCIL_PATCH_END;
1723 3634 ▶ if (b->arm_tail != STENCIL_PATCH_END)
Branch: 1 branch
BranchTrueFalseTotal
if (b->arm_tail != STENCIL_PATCH_END) 2962 672 3634
1724 2962 ARMREC(c, b->arm_tail)->next = c->n_armrec;
1725 else
1726 672 b->arm_head = c->n_armrec;
1727 3634 b->arm_tail = c->n_armrec;
1728 3634 c->n_armrec++;
1729 3634 return 1;
1730 }
1731  
1732 /* the arm's string literal, if it is a `case "string"` arm (one that can
1733 * join a dispatch table), else NULL */
1734 5911 static const stencil_cval *arm_string_key(const stencil_compiler *c,
1735 const stencil_armrec *arm)
1736 {
1737 5911 const stencil_ccase *cs = ((const stencil_ccase *)c->cases.base)
1738 5911 + arm->case_id;
1739 const stencil_cval *v;
1740 5911 ▶ if (cs->kind != STENCIL_CASE_VALUE)
Branch: 1 branch
BranchTrueFalseTotal
if (cs->kind != STENCIL_CASE_VALUE) 895 5016 5911
1741 895 return NULL;
1742 5016 v = ((const stencil_cval *)c->cvals.base) + cs->a;
1743 5016 ▶ return v->is_num ? NULL : v;
Branch: 1 branch
BranchTrueFalseTotal
return v->is_num ? NULL : v; 4698 318 5016
1744 }
1745  
1746 /* qsort over (key, arm order): equal keys keep their first arm first */
1747 typedef struct stencil_tabkey {
1748 const char *pv;
1749 uint32_t len, off, order, target;
1750 } stencil_tabkey;
1751  
1752 6382 static int tabkey_cmp(const void *pa, const void *pb)
1753 {
1754 6382 const stencil_tabkey *a = (const stencil_tabkey *)pa;
1755 6382 const stencil_tabkey *b = (const stencil_tabkey *)pb;
1756 6382 uint32_t n = a->len < b->len ? a->len : b->len;
1757 6382 ▶ int r = n ? memcmp(a->pv, b->pv, n) : 0;
Branch: 1 branch
BranchTrueFalseTotal
int r = n ? memcmp(a->pv, b->pv, n) : 0; 3168 3214 6382
1758 6382 ▶ if (r)
Branch: 1 branch
BranchTrueFalseTotal
if (r) 2306 4076 6382
1759 2306 return r;
1760 4076 ▶ if (a->len != b->len)
Branch: 1 branch
BranchTrueFalseTotal
if (a->len != b->len) 2564 1512 4076
1761 2564 ▶ return a->len < b->len ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
return a->len < b->len ? -1 : 1; 672 1892 2564
1762 1512 ▶ return a->order < b->order ? -1 : a->order > b->order;
Branch: 1 branch
BranchTrueFalseTotal
return a->order < b->order ? -1 : a->order > b->order; 0 1512 1512
1763 }
1764  
1765 /* Build one table from the run of n string arms starting at arm `first`
1766 * and rewrite the run's first SOP_CASE into a SOP_CASE_TABLE that jumps
1767 * straight to the matching arm's body, or to where the run's last arm
1768 * missed to. The other arms' SOP_CASEs stay in place, unreached. Keys are
1769 * bytes in the template's one encoding, so memcmp order is code-point
1770 * order, the order the VM's binary search compares in. */
1771 160 static int table_from_run(stencil_compiler *c, uint32_t first, uint32_t n)
1772 {
1773 stencil_arena tmp; /* compile-time scratch, as everywhere */
1774 stencil_tabkey *keys;
1775 stencil_ctab tab;
1776 160 uint32_t i, arm, last = first, kept = 0, off, miss;
1777 160 ▶ if (!stencil_arena_init(&tmp, n * sizeof *keys)
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_arena_init(&tmp, n * sizeof *keys) 160 0 160
1778 160 ▶ || stencil_arena_alloc(&tmp, n * sizeof *keys, sizeof(void *))
Branch: 1 branch
BranchTrueFalseTotal
|| stencil_arena_alloc(&tmp, n * sizeof *keys, sizeof(void *)) 0 160 160
1779 == STENCIL_ARENA_NULL) {
1780 0 stencil_arena_free(&tmp);
1781 0 return coom(c);
1782 }
1783 160 keys = (stencil_tabkey *)tmp.base;
1784 2446 ▶ for (i = 0, arm = first; i < n; i++, arm = ARMREC(c, arm)->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0, arm = first; i < n; i++, arm = ARMREC(c, arm)->next) { 2286 160 2446
1785 2286 const stencil_cval *v = arm_string_key(c, ARMREC(c, arm));
1786 2286 keys[i].pv = c->pool.base + v->str_off;
1787 2286 keys[i].off = v->str_off;
1788 2286 keys[i].len = v->str_len;
1789 2286 keys[i].order = i;
1790 2286 keys[i].target = ARMREC(c, arm)->op_at + CASE_OP_LEN;
1791 2286 last = arm;
1792 }
1793 160 qsort(keys, n, sizeof *keys, tabkey_cmp);
1794 160 ▶ if (!arena_on_demand(&c->ctents) || !arena_on_demand(&c->ctabs)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!arena_on_demand(&c->ctents) || !arena_on_demand(&c->ctabs)) { 160 0 160
if (!arena_on_demand(&c->ctents) || !arena_on_demand(&c->ctabs)) { 0 160 160
1795 0 stencil_arena_free(&tmp);
1796 0 return coom(c);
1797 }
1798 160 tab.first = c->n_ctents;
1799 2446 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2286 160 2446
1800 stencil_ctent ent;
1801 2286 ▶ if (kept && keys[i].len == keys[i - 1].len
Branch: 2 branches
BranchTrueFalseTotal
if (kept && keys[i].len == keys[i - 1].len 2126 160 2286
if (kept && keys[i].len == keys[i - 1].len 1205 921 2126
1802 1205 ▶ && memcmp(keys[i].pv, keys[i - 1].pv, keys[i].len) == 0)
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(keys[i].pv, keys[i - 1].pv, keys[i].len) == 0) 999 206 1205
1803 999 continue; /* a duplicate key */
1804 1287 ent.str_off = keys[i].off;
1805 1287 ent.str_len = keys[i].len;
1806 1287 ent.target = keys[i].target;
1807 1287 off = stencil_arena_alloc(&c->ctents, sizeof ent, sizeof(uint32_t));
1808 1287 ▶ if (off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) { 0 1287 1287
1809 0 stencil_arena_free(&tmp);
1810 0 return coom(c);
1811 }
1812 1287 memcpy(c->ctents.base + off, &ent, sizeof ent);
1813 1287 c->n_ctents++;
1814 1287 kept++;
1815 }
1816 160 stencil_arena_free(&tmp);
1817 160 tab.n = kept;
1818 160 off = stencil_arena_alloc(&c->ctabs, sizeof tab, sizeof(uint32_t));
1819 160 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 160 160
1820 0 return coom(c);
1821 160 memcpy(c->ctabs.base + off, &tab, sizeof tab);
1822 /* the run's first op becomes the table, missing where its last arm did */
1823 160 miss = read_code_u32(c, ARMREC(c, last)->op_at + CASE_OP_MISS_AT);
1824 160 c->code.base[ARMREC(c, first)->op_at] = (char)SOP_CASE_TABLE;
1825 160 patch_u32(c, ARMREC(c, first)->op_at + CASE_OP_ID_AT, c->n_ctabs);
1826 160 patch_u32(c, ARMREC(c, first)->op_at + CASE_OP_MISS_AT, miss);
1827 160 c->n_ctabs++;
1828 160 return 1;
1829 }
1830  
1831 /* At {% end %} (all jumps patched): turn every maximal run of at least
1832 * STENCIL_CASE_TABLE_MIN consecutive string arms into a table. Only
1833 * consecutive arms, so first-match-wins across other kinds of case holds. */
1834 666 static int switch_tables(stencil_compiler *c, const stencil_blk *b)
1835 {
1836 666 uint32_t arm = b->arm_head, run_first = STENCIL_PATCH_END, run_len = 0;
1837 3625 for (;;) {
1838 4291 int in_run = arm != STENCIL_PATCH_END
1839 4291 ▶ && arm_string_key(c, ARMREC(c, arm)) != NULL;
Branch: 2 branches
BranchTrueFalseTotal
&& arm_string_key(c, ARMREC(c, arm)) != NULL; 3625 666 4291
&& arm_string_key(c, ARMREC(c, arm)) != NULL; 2412 1213 3625
1840 4291 ▶ if (in_run) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_run) { 2412 1879 4291
1841 2412 ▶ if (!run_len)
Branch: 1 branch
BranchTrueFalseTotal
if (!run_len) 261 2151 2412
1842 261 run_first = arm;
1843 2412 run_len++;
1844 } else {
1845 1879 ▶ if (run_len >= STENCIL_CASE_TABLE_MIN
Branch: 1 branch
BranchTrueFalseTotal
if (run_len >= STENCIL_CASE_TABLE_MIN 160 1719 1879
1846 160 ▶ && !table_from_run(c, run_first, run_len))
Branch: 1 branch
BranchTrueFalseTotal
&& !table_from_run(c, run_first, run_len)) 0 160 160
1847 0 return 0;
1848 1879 run_len = 0;
1849 1879 ▶ if (arm == STENCIL_PATCH_END)
Branch: 1 branch
BranchTrueFalseTotal
if (arm == STENCIL_PATCH_END) 666 1213 1879
1850 666 return 1;
1851 }
1852 3625 arm = ARMREC(c, arm)->next;
1853 }
1854 }
1855  
1856 975 static int stmt_switch(stencil_compiler *c, uint32_t line, uint32_t col)
1857 {
1858 975 stencil_blk *b = blk_open(c, BLK_SWITCH, line, col);
1859 975 ▶ if (!b)
Branch: 1 branch
BranchTrueFalseTotal
if (!b) 1 974 975
1860 1 return 0;
1861 974 c->open_switches++;
1862 /* the topic: evaluated once, and left on the stack for every case */
1863 974 ▶ return parse_expr(c) && expect_close(c);
Branch: 2 branches
BranchTrueFalseTotal
return parse_expr(c) && expect_close(c); 951 23 974
return parse_expr(c) && expect_close(c); 946 5 951
1864 }
1865  
1866 /* close the running arm (if any) and aim the last case's miss here */
1867 4162 static int switch_next_arm(stencil_compiler *c, stencil_blk *b)
1868 {
1869 4162 ▶ if (!b->arms)
Branch: 1 branch
BranchTrueFalseTotal
if (!b->arms) 679 3483 4162
1870 679 return 1;
1871 3483 ▶ if (!emit_pop_binds(c, b)
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_pop_binds(c, b) 3483 0 3483
1872 3483 ▶ || !emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain))
Branch: 1 branch
BranchTrueFalseTotal
|| !emit_jump_chained(c, SOP_JUMP, 0, &b->jump_chain)) 0 3483 3483
1873 0 return 0;
1874 3483 ▶ if (b->cond_patch != STENCIL_PATCH_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->cond_patch != STENCIL_PATCH_END) { 3483 0 3483
1875 3483 patch_u32(c, b->cond_patch, code_pos(c));
1876 3483 b->cond_patch = STENCIL_PATCH_END;
1877 }
1878 3483 return 1;
1879 }
1880  
1881 3894 static int stmt_case(stencil_compiler *c, stencil_blk *b, uint32_t line,
1882 uint32_t col)
1883 {
1884 stencil_ccase cs;
1885 3894 uint32_t id = 0;
1886 3894 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 2 3892 3894
1887 2 return cerror_at(c, line, col,
1888 "'case' after 'default' is not allowed");
1889 3892 ▶ if (!case_pattern(c, &cs) || !expect_close(c))
Branch: 2 branches
BranchTrueFalseTotal
if (!case_pattern(c, &cs) || !expect_close(c)) 3636 256 3892
if (!case_pattern(c, &cs) || !expect_close(c)) 2 3634 3636
1890 258 return 0;
1891 3634 ▶ if (!switch_next_arm(c, b) || !case_store(c, &cs, &id))
Branch: 2 branches
BranchTrueFalseTotal
if (!switch_next_arm(c, b) || !case_store(c, &cs, &id)) 3634 0 3634
if (!switch_next_arm(c, b) || !case_store(c, &cs, &id)) 0 3634 3634
1892 0 return 0;
1893 3634 ▶ if (!arm_record(c, b, code_pos(c), id))
Branch: 1 branch
BranchTrueFalseTotal
if (!arm_record(c, b, code_pos(c), id)) 0 3634 3634
1894 0 return 0;
1895 3634 ▶ if (!emit_op(c, SOP_CASE, 0) || !emit_u32(c, id))
Branch: 2 branches
BranchTrueFalseTotal
if (!emit_op(c, SOP_CASE, 0) || !emit_u32(c, id)) 3634 0 3634
if (!emit_op(c, SOP_CASE, 0) || !emit_u32(c, id)) 0 3634 3634
1896 0 return 0;
1897 3634 b->cond_patch = code_pos(c);
1898 3634 b->arms++;
1899 3634 return emit_u32(c, STENCIL_PATCH_END);
1900 }
1901  
1902 530 static int stmt_default(stencil_compiler *c, stencil_blk *b, uint32_t line,
1903 uint32_t col)
1904 {
1905 530 ▶ if (b->has_else)
Branch: 1 branch
BranchTrueFalseTotal
if (b->has_else) 2 528 530
1906 2 return cerror_at(c, line, col, "multiple 'default' in one switch");
1907 528 ▶ if (!expect_close(c) || !switch_next_arm(c, b))
Branch: 2 branches
BranchTrueFalseTotal
if (!expect_close(c) || !switch_next_arm(c, b)) 528 0 528
if (!expect_close(c) || !switch_next_arm(c, b)) 0 528 528
1908 0 return 0;
1909 528 b->has_else = 1;
1910 528 b->arms++;
1911 528 return 1;
1912 }
1913  
1914 /* a pending literal run of nothing but whitespace? */
1915 945 static int pend_is_blank(const stencil_compiler *c)
1916 {
1917 size_t i;
1918 972 ▶ for (i = 0; i < c->pend.used; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->pend.used; i++) 30 942 972
1919 30 ▶ if (!isSPACE(c->pend.base[i]))
Branch: 1 branch
BranchTrueFalseTotal
if (!isSPACE(c->pend.base[i])) 3 27 30
1920 3 return 0;
1921 942 return 1;
1922 }
1923  
1924 /* ====================================================================
1925 * Main scan loop
1926 * ==================================================================== */
1927  
1928 3791 static int scan_template(stencil_compiler *c)
1929 {
1930 115153 for (;;) {
1931 118944 const char *run = c->p;
1932 118944 const char *q = c->p;
1933  
1934 /* literal run up to the next real '{%' */
1935 for (;;) {
1936 119108 q = stencil_dispatch.scan(q, c->end);
1937 119108 ▶ if (q == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (q == c->end) 3201 115907 119108
1938 3201 break;
1939 115907 ▶ if (q + 1 < c->end && q[1] == '%')
Branch: 2 branches
BranchTrueFalseTotal
if (q + 1 < c->end && q[1] == '%') 115906 1 115907
if (q + 1 < c->end && q[1] == '%') 115743 163 115906
1940 115743 break;
1941 164 q++;
1942 }
1943 118944 ▶ if (!lit_pend(c, run, (size_t)(q - run)))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_pend(c, run, (size_t)(q - run))) 0 118944 118944
1944 0 return 0;
1945 118944 advance_lines(c, run, q);
1946 118944 c->p = q;
1947 118944 ▶ if (c->p == c->end)
Branch: 1 branch
BranchTrueFalseTotal
if (c->p == c->end) 3201 115743 118944
1948 3201 break;
1949  
1950 /* at '{%' */
1951 {
1952 115743 uint32_t tag_line = c->line;
1953 115743 uint32_t tag_col = ccol(c, c->p);
1954 115743 const char *tag_at = c->p;
1955 115743 c->p += 2;
1956  
1957 /* comment */
1958 115743 ▶ if (c->p < c->end && *c->p == '#') {
Branch: 2 branches
BranchTrueFalseTotal
if (c->p < c->end && *c->p == '#') { 115742 1 115743
if (c->p < c->end && *c->p == '#') { 32 115710 115742
1959 32 const char *r = c->p + 1;
1960 for (;;) {
1961 112 r = (const char *)memchr(r, '%',
1962 72 (size_t)(c->end - r));
1963 72 ▶ if (!r)
Branch: 1 branch
BranchTrueFalseTotal
if (!r) 14 58 72
1964 14 return cerror_at(c, tag_line, tag_col,
1965 "unterminated comment");
1966 58 ▶ if (r + 1 < c->end && r[1] == '}')
Branch: 2 branches
BranchTrueFalseTotal
if (r + 1 < c->end && r[1] == '}') 58 0 58
if (r + 1 < c->end && r[1] == '}') 18 40 58
1967 18 break;
1968 40 r++;
1969 }
1970 18 advance_lines(c, c->p, r);
1971 18 c->p = r + 2;
1972 18 continue;
1973 }
1974  
1975 115711 skip_ws(c);
1976  
1977 /* empty tag = literal '{%' escape */
1978 115711 ▶ 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] == '}') { 115710 1 115711
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 22 115688 115710
if (c->end - c->p >= 2 && c->p[0] == '%' && c->p[1] == '}') { 22 0 22
1979 22 c->p += 2;
1980 22 ▶ if (!lit_pend(c, "{%", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_pend(c, "{%", 2)) 0 22 22
1981 0 return 0;
1982 22 continue;
1983 }
1984  
1985 {
1986 115689 size_t wl = word_len(c->p, c->end);
1987 115689 ▶ int is_case = wl && word_is(c->p, wl, "case");
Branch: 2 branches
BranchTrueFalseTotal
int is_case = wl && word_is(c->p, wl, "case"); 115670 19 115689
int is_case = wl && word_is(c->p, wl, "case"); 3922 111748 115670
1988 115689 ▶ int is_dflt = wl && word_is(c->p, wl, "default");
Branch: 2 branches
BranchTrueFalseTotal
int is_dflt = wl && word_is(c->p, wl, "default"); 115670 19 115689
int is_dflt = wl && word_is(c->p, wl, "default"); 554 115116 115670
1989 115689 ▶ stencil_blk *inner = c->n_blk ? &c->blk[c->n_blk - 1] : NULL;
Branch: 1 branch
BranchTrueFalseTotal
stencil_blk *inner = c->n_blk ? &c->blk[c->n_blk - 1] : NULL; 32376 83313 115689
1990  
1991 /* between {% switch x %} and its first arm: whitespace
1992 * only, and it is dropped rather than rendered */
1993 115689 ▶ if (inner && inner->kind == BLK_SWITCH && !inner->arms) {
Branch: 3 branches
BranchTrueFalseTotal
if (inner && inner->kind == BLK_SWITCH && !inner->arms) { 32376 83313 115689
if (inner && inner->kind == BLK_SWITCH && !inner->arms) { 5169 27207 32376
if (inner && inner->kind == BLK_SWITCH && !inner->arms) { 945 4224 5169
1994 945 ▶ if (!pend_is_blank(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!pend_is_blank(c)) 3 942 945
1995 3 return cerror_at(c, inner->line, inner->col,
1996 "only whitespace may come between 'switch' and "
1997 "its first 'case'");
1998 942 c->pend.used = 0;
1999 942 ▶ if (!is_case && !is_dflt
Branch: 2 branches
BranchTrueFalseTotal
if (!is_case && !is_dflt 12 930 942
if (!is_case && !is_dflt 5 7 12
2000 5 ▶ && !(wl && word_is(c->p, wl, "end")))
Branch: 2 branches
BranchTrueFalseTotal
&& !(wl && word_is(c->p, wl, "end"))) 5 0 5
&& !(wl && word_is(c->p, wl, "end"))) 2 3 5
2001 2 return cerror_at(c, tag_line, tag_col,
2002 "expected 'case' or 'default' after 'switch'");
2003 }
2004  
2005 115684 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 115684 115684
2006 0 return 0;
2007 115684 ▶ if (!wl)
Branch: 1 branch
BranchTrueFalseTotal
if (!wl) 19 115665 115684
2008 19 return cerror(c, c->p, "expected a name or keyword");
2009  
2010 115665 ▶ if ((is_case || is_dflt) && c->open_switches) {
Branch: 3 branches
BranchTrueFalseTotal
if ((is_case || is_dflt) && c->open_switches) { 111746 3919 115665
if ((is_case || is_dflt) && c->open_switches) { 554 111192 111746
if ((is_case || is_dflt) && c->open_switches) { 4427 46 4473
2011 /* keywords only inside a switch; elsewhere they are
2012 * data names, as they always were */
2013 4427 ▶ if (inner->kind != BLK_SWITCH)
Branch: 1 branch
BranchTrueFalseTotal
if (inner->kind != BLK_SWITCH) 3 4424 4427
2014 3 ▶ return cerror_at(c, tag_line, tag_col,
Branch: 1 branch
BranchTrueFalseTotal
return cerror_at(c, tag_line, tag_col, 2 1 3
2015 "'%s' inside '%s' block",
2016 is_case ? "case" : "default",
2017 3 blk_kind_name(inner->kind));
2018 4424 c->p += wl;
2019 4954 ▶ if (!(is_case ? stmt_case(c, inner, tag_line, tag_col)
Branch: 2 branches
BranchTrueFalseTotal
if (!(is_case ? stmt_case(c, inner, tag_line, tag_col) 3894 530 4424
if (!(is_case ? stmt_case(c, inner, tag_line, tag_col) 262 4162 4424
2020 530 : stmt_default(c, inner, tag_line, tag_col)))
2021 262 return 0;
2022 111238 ▶ } else if (word_is(c->p, wl, "switch")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "switch")) { 975 110263 111238
2023 975 c->p += wl;
2024 975 ▶ if (!stmt_switch(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_switch(c, tag_line, tag_col)) 29 946 975
2025 29 return 0;
2026 110263 ▶ } else if (word_is(c->p, wl, "if")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "if")) { 1911 108352 110263
2027 1911 c->p += wl;
2028 1911 ▶ if (!stmt_if(c, BLK_IF, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_if(c, BLK_IF, tag_line, tag_col)) 36 1875 1911
2029 36 return 0;
2030 108352 ▶ } else if (word_is(c->p, wl, "unless")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "unless")) { 20009 88343 108352
2031 20009 c->p += wl;
2032 20009 ▶ 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 20009 20009
2033 0 return 0;
2034 88343 ▶ } else if (word_is(c->p, wl, "elsif")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "elsif")) { 36 88307 88343
2035 36 c->p += wl;
2036 36 ▶ if (!stmt_elsif(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_elsif(c, tag_line, tag_col)) 28 8 36
2037 28 return 0;
2038 88307 ▶ } else if (word_is(c->p, wl, "else")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "else")) { 144 88163 88307
2039 144 c->p += wl;
2040 144 ▶ if (!stmt_else(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_else(c, tag_line, tag_col)) 29 115 144
2041 29 return 0;
2042 88163 ▶ } else if (word_is(c->p, wl, "end")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "end")) { 24083 64080 88163
2043 24083 c->p += wl;
2044 24083 ▶ if (!stmt_end(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_end(c, tag_line, tag_col)) 21 24062 24083
2045 21 return 0;
2046 64080 ▶ } else if (word_is(c->p, wl, "for")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "for")) { 1742 62338 64080
2047 1742 c->p += wl;
2048 1742 ▶ if (!stmt_for(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_for(c, tag_line, tag_col)) 25 1717 1742
2049 25 return 0;
2050 62338 ▶ } else if (word_is(c->p, wl, "set")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "set")) { 141 62197 62338
2051 141 c->p += wl;
2052 141 ▶ if (!stmt_set(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_set(c)) 29 112 141
2053 29 return 0;
2054 62197 ▶ } else if (word_is(c->p, wl, "include")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "include")) { 72 62125 62197
2055 72 ▶ if (c->no_files)
Branch: 1 branch
BranchTrueFalseTotal
if (c->no_files) 6 66 72
2056 6 return cerror_at(c, tag_line, tag_col,
2057 "include is refused: this engine reads no files (files => 0)");
2058 66 c->p += wl;
2059 66 ▶ if (!stmt_include(c, tag_line, tag_col))
Branch: 1 branch
BranchTrueFalseTotal
if (!stmt_include(c, tag_line, tag_col)) 22 44 66
2060 22 return 0;
2061 62125 ▶ } else if (word_is(c->p, wl, "content")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "content")) { 46 62079 62125
2062 46 ▶ if (c->no_files)
Branch: 1 branch
BranchTrueFalseTotal
if (c->no_files) 1 45 46
2063 1 return cerror_at(c, tag_line, tag_col,
2064 "content is refused: this engine reads no files (files => 0)");
2065 45 c->p += wl;
2066 45 ▶ if (!expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!expect_close(c)) 0 45 45
2067 0 return 0;
2068 45 ▶ if (!emit_op(c, SOP_CONTENT, 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_CONTENT, 0)) 0 45 45
2069 0 return 0;
2070 45 c->flags |= STENCIL_PROG_IS_WRAPPER;
2071 62079 ▶ } else if (word_is(c->p, wl, "raw")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (word_is(c->p, wl, "raw")) { 33 62046 62079
2072 33 c->p += wl;
2073 33 skip_ws(c);
2074 33 ▶ if (!parse_path_push(c) || !parse_filters(c)
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_path_push(c) || !parse_filters(c) 18 15 33
if (!parse_path_push(c) || !parse_filters(c) 18 0 18
2075 18 ▶ || !expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
|| !expect_close(c)) 4 14 18
2076 19 return 0;
2077 14 ▶ if (!emit_op(c, SOP_PRINT_RAW, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_PRINT_RAW, -1)) 0 14 14
2078 0 return 0;
2079 } else {
2080 /* output tag */
2081 62046 ▶ if (!parse_path_push(c) || !parse_filters(c)
Branch: 2 branches
BranchTrueFalseTotal
if (!parse_path_push(c) || !parse_filters(c) 62042 4 62046
if (!parse_path_push(c) || !parse_filters(c) 62027 15 62042
2082 62027 ▶ || !expect_close(c))
Branch: 1 branch
BranchTrueFalseTotal
|| !expect_close(c)) 23 62004 62027
2083 42 return 0;
2084 62004 ▶ if (!emit_op(c, SOP_PRINT_ESC, -1))
Branch: 1 branch
BranchTrueFalseTotal
if (!emit_op(c, SOP_PRINT_ESC, -1)) 0 62004 62004
2085 0 return 0;
2086 }
2087 }
2088 (void)tag_at;
2089 }
2090 }
2091  
2092 3201 ▶ if (!lit_flush(c))
Branch: 1 branch
BranchTrueFalseTotal
if (!lit_flush(c)) 0 3201 3201
2093 0 return 0;
2094 3201 ▶ if (c->n_blk) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->n_blk) { 7 3194 3201
2095 7 stencil_blk *b = &c->blk[c->n_blk - 1];
2096 7 return cerror_at(c, b->line, b->col,
2097 7 "unclosed '%s' block", blk_kind_name(b->kind));
2098 }
2099 3194 return emit_op(c, SOP_END, 0);
2100 }
2101  
2102 /* ====================================================================
2103 * Compile driver: pack regions into the final arena
2104 * ==================================================================== */
2105  
2106 3791 static void compiler_free_temps(stencil_compiler *c)
2107 {
2108 3791 stencil_arena_free(&c->code);
2109 3791 stencil_arena_free(&c->pool);
2110 3791 stencil_arena_free(&c->names);
2111 3791 stencil_arena_free(&c->paths);
2112 3791 stencil_arena_free(&c->segs);
2113 3791 stencil_arena_free(&c->incs);
2114 3791 stencil_arena_free(&c->filts);
2115 3791 stencil_arena_free(&c->lines);
2116 3791 stencil_arena_free(&c->pend);
2117 3791 stencil_arena_free(&c->cases);
2118 3791 stencil_arena_free(&c->cvals);
2119 3791 stencil_arena_free(&c->rxs);
2120 3791 stencil_arena_free(&c->ctabs);
2121 3791 stencil_arena_free(&c->ctents);
2122 3791 stencil_arena_free(&c->armrec);
2123 3791 stencil_intern_free(&c->lit_intern);
2124 3791 }
2125  
2126 41522 static uint32_t pack_region(stencil_arena *dst, const stencil_arena *src,
2127 size_t align)
2128 {
2129 41522 ▶ 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, 19576 21946 41522
2130 align);
2131 41522 ▶ if (off == STENCIL_ARENA_NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (off == STENCIL_ARENA_NULL) 0 41522 41522
2132 0 return off;
2133 41522 ▶ if (src->used)
Branch: 1 branch
BranchTrueFalseTotal
if (src->used) 19576 21946 41522
2134 19576 memcpy(dst->base + off, src->base, src->used);
2135 41522 return off;
2136 }
2137  
2138 3791 stencil_program *stencil_compile(pTHX_ const char *src, STRLEN len,
2139 const char *name, uint32_t flags,
2140 SV **err)
2141 {
2142 stencil_compiler c;
2143 3791 stencil_program *prog = NULL;
2144 int ok;
2145  
2146 3791 memset(&c, 0, sizeof c);
2147 3791 c.flags = flags & STENCIL_PROG_SRC_UTF8;
2148 3791 c.no_files = (flags & STENCIL_COMPILE_NO_FILES) ? 1 : 0;
2149 3791 c.src = c.p = c.line_start = src;
2150 3791 c.end = src + len;
2151 3791 c.name = name;
2152 3791 c.line = 1;
2153 3791 c.last_line_rec = 0;
2154 #ifdef PERL_IMPLICIT_CONTEXT
2155 c.perl = aTHX;
2156 #endif
2157  
2158 7582 if (!stencil_arena_init(&c.code, 256) ||
2159 7582 ▶ !stencil_arena_init(&c.pool, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pool, 256) || 3791 0 3791
2160 7582 ▶ !stencil_arena_init(&c.names, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.names, 256) || 3791 0 3791
2161 7582 ▶ !stencil_arena_init(&c.paths, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.paths, 256) || 3791 0 3791
2162 7582 ▶ !stencil_arena_init(&c.segs, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.segs, 256) || 3791 0 3791
2163 7582 ▶ !stencil_arena_init(&c.incs, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.incs, 64) || 3791 0 3791
2164 7582 ▶ !stencil_arena_init(&c.filts, 64) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.filts, 64) || 3791 0 3791
2165 7582 ▶ !stencil_arena_init(&c.lines, 128) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.lines, 128) || 3791 0 3791
2166 7582 ▶ !stencil_arena_init(&c.pend, 256) ||
Branch: 1 branch
BranchTrueFalseTotal
!stencil_arena_init(&c.pend, 256) || 0 3791 3791
2167 3791 !stencil_intern_init(&c.lit_intern)) {
2168 0 compiler_free_temps(&c);
2169 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
2170 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
2171 0 return NULL;
2172 }
2173  
2174 3791 ok = scan_template(&c);
2175  
2176 3791 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3194 597 3791
2177 3194 prog = (stencil_program *)calloc(1, sizeof *prog);
2178 3194 ▶ if (!prog) {
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) { 0 3194 3194
2179 0 ok = 0;
2180 0 cerror_at(&c, c.line, 1, "out of memory");
2181 }
2182 }
2183 3791 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3194 597 3791
2184 3194 size_t total = c.code.used + c.pool.used + c.names.used
2185 3194 + c.paths.used + c.segs.used + c.incs.used
2186 3194 + c.filts.used + c.lines.used + c.cases.used
2187 3194 + c.cvals.used + c.rxs.used + c.ctabs.used
2188 3194 + c.ctents.used + 224;
2189 3194 ▶ if (!stencil_arena_init(&prog->arena, total)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!stencil_arena_init(&prog->arena, total)) { 0 3194 3194
2190 0 free(prog);
2191 0 prog = NULL;
2192 0 ok = 0;
2193 0 cerror_at(&c, c.line, 1, "out of memory");
2194 }
2195 }
2196 3791 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3194 597 3791
2197 3194 prog->code_off = pack_region(&prog->arena, &c.code, 1);
2198 3194 prog->pool_off = pack_region(&prog->arena, &c.pool, 1);
2199 3194 prog->names_off = pack_region(&prog->arena, &c.names,
2200 sizeof(uint32_t));
2201 3194 prog->paths_off = pack_region(&prog->arena, &c.paths,
2202 sizeof(uint32_t));
2203 3194 prog->segs_off = pack_region(&prog->arena, &c.segs,
2204 sizeof(SSize_t));
2205 3194 prog->incs_off = pack_region(&prog->arena, &c.incs,
2206 sizeof(uint32_t));
2207 3194 prog->filts_off = pack_region(&prog->arena, &c.filts,
2208 sizeof(double));
2209 3194 prog->lines_off = pack_region(&prog->arena, &c.lines,
2210 sizeof(uint32_t));
2211 3194 prog->cases_off = pack_region(&prog->arena, &c.cases,
2212 sizeof(uint32_t));
2213 3194 prog->cvals_off = pack_region(&prog->arena, &c.cvals,
2214 STENCIL_CVAL_ALIGN);
2215 3194 prog->rxs_off = pack_region(&prog->arena, &c.rxs,
2216 sizeof(uint32_t));
2217 3194 prog->ctabs_off = pack_region(&prog->arena, &c.ctabs,
2218 sizeof(uint32_t));
2219 3194 prog->ctents_off = pack_region(&prog->arena, &c.ctents,
2220 sizeof(uint32_t));
2221 3194 ▶ if (prog->code_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
if (prog->code_off == STENCIL_ARENA_NULL 3194 0 3194
2222 3194 ▶ || prog->pool_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->pool_off == STENCIL_ARENA_NULL 3194 0 3194
2223 3194 ▶ || prog->names_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->names_off == STENCIL_ARENA_NULL 3194 0 3194
2224 3194 ▶ || prog->paths_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->paths_off == STENCIL_ARENA_NULL 3194 0 3194
2225 3194 ▶ || prog->segs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->segs_off == STENCIL_ARENA_NULL 3194 0 3194
2226 3194 ▶ || prog->incs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->incs_off == STENCIL_ARENA_NULL 3194 0 3194
2227 3194 ▶ || prog->filts_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->filts_off == STENCIL_ARENA_NULL 3194 0 3194
2228 3194 ▶ || prog->lines_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->lines_off == STENCIL_ARENA_NULL 3194 0 3194
2229 3194 ▶ || prog->cases_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->cases_off == STENCIL_ARENA_NULL 3194 0 3194
2230 3194 ▶ || prog->cvals_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->cvals_off == STENCIL_ARENA_NULL 3194 0 3194
2231 3194 ▶ || prog->rxs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->rxs_off == STENCIL_ARENA_NULL 3194 0 3194
2232 3194 ▶ || prog->ctabs_off == STENCIL_ARENA_NULL
Branch: 1 branch
BranchTrueFalseTotal
|| prog->ctabs_off == STENCIL_ARENA_NULL 3194 0 3194
2233 3194 ▶ || prog->ctents_off == STENCIL_ARENA_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
|| prog->ctents_off == STENCIL_ARENA_NULL) { 0 3194 3194
2234 0 stencil_program_free(prog);
2235 0 prog = NULL;
2236 0 ok = 0;
2237 0 cerror_at(&c, c.line, 1, "out of memory");
2238 }
2239 }
2240 3791 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 3194 597 3791
2241 3194 prog->code_len = (uint32_t)c.code.used;
2242 3194 prog->pool_len = (uint32_t)c.pool.used;
2243 3194 prog->n_names = c.n_names;
2244 3194 prog->n_paths = c.n_paths;
2245 3194 prog->n_segs = c.n_segs;
2246 3194 prog->n_incs = c.n_incs;
2247 3194 prog->n_filts = c.n_filts;
2248 3194 prog->n_lines = c.n_lines;
2249 3194 prog->n_cases = c.n_cases;
2250 3194 prog->n_cvals = c.n_cvals;
2251 3194 prog->rxs_len = (uint32_t)c.rxs.used;
2252 3194 prog->n_ctabs = c.n_ctabs;
2253 3194 prog->n_ctents = c.n_ctents;
2254 3194 prog->max_stack = (uint32_t)c.max_stack;
2255 3194 prog->max_frames = (uint32_t)c.max_frames;
2256 3194 prog->max_binds = (uint32_t)c.max_binds;
2257 3194 prog->flags = c.flags;
2258 3194 stencil_arena_finalise(&prog->arena);
2259 }
2260  
2261 3791 compiler_free_temps(&c);
2262  
2263 3791 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 597 3194 3791
2264 597 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 597 0 597
2265 597 ▶ *err = sv_2mortal(newSVpvf("Template::Stencil: %s:%u:%u: %s",
Branch: 1 branch
BranchTrueFalseTotal
*err = sv_2mortal(newSVpvf("Template::Stencil: %s:%u:%u: %s", 597 0 597
2266 c.name ? c.name : "<string>",
2267 (unsigned)c.err_line,
2268 (unsigned)c.err_col, c.errbuf));
2269 597 return NULL;
2270 }
2271 3194 return prog;
2272 }
2273  
2274 3259 void stencil_program_free(stencil_program *prog)
2275 {
2276 3259 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 65 3194 3259
2277 65 return;
2278 3194 stencil_arena_free(&prog->arena);
2279 3194 free(prog);
2280 }
2281  
2282 /* ====================================================================
2283 * Inspect: decode the program for tests
2284 * ==================================================================== */
2285  
2286 static const char *const op_names[] = {
2287 #define STENCIL_X_NAME(n) #n,
2288 STENCIL_OPS(STENCIL_X_NAME)
2289 #undef STENCIL_X_NAME
2290 };
2291  
2292 #define hv_store_lit(hv, key, sv) \
2293 (void)hv_store((hv), "" key "", sizeof(key) - 1, (sv), 0)
2294  
2295 384 static SV *path_to_sv(pTHX_ const stencil_program *pr, uint32_t id)
2296 {
2297 384 const stencil_cpath *p = &stencil_prog_paths(pr)[id];
2298 384 return newSVpvn(stencil_prog_pool(pr) + p->full_off, p->full_len);
2299 }
2300  
2301 196 static SV *name_to_sv(pTHX_ const stencil_program *pr, uint32_t id)
2302 {
2303 196 const stencil_cname *n = &stencil_prog_names(pr)[id];
2304 196 return newSVpvn(stencil_prog_pool(pr) + n->off, n->len);
2305 }
2306  
2307 static const char *const case_kind_names[STENCIL_CASE__MAX] = {
2308 "value", "range", "list", "regex", "path_num", "path_str", "path_rx",
2309 "pred", "undef", "ref", "reftype", "isa"
2310 };
2311  
2312 100 static SV *pool_sv(pTHX_ const stencil_program *pr, uint32_t off,
2313 uint32_t len)
2314 {
2315 100 SV *sv = newSVpvn(stencil_prog_pool(pr) + off, len);
2316 100 ▶ if (pr->flags & STENCIL_PROG_SRC_UTF8)
Branch: 1 branch
BranchTrueFalseTotal
if (pr->flags & STENCIL_PROG_SRC_UTF8) 2 98 100
2317 2 SvUTF8_on(sv);
2318 100 return sv;
2319 }
2320  
2321 /* a case literal: { num => NV [, int => exact integer] } or { str => ... } */
2322 297 static SV *cval_to_sv(pTHX_ const stencil_program *pr, uint32_t idx)
2323 {
2324 297 const stencil_cval *v = &stencil_prog_cvals(pr)[idx];
2325 297 HV *h = newHV();
2326 297 ▶ if (v->is_num) {
Branch: 1 branch
BranchTrueFalseTotal
if (v->is_num) { 243 54 297
2327 243 hv_store_lit(h, "num", newSVnv(v->nv));
2328 243 ▶ if (v->is_int)
Branch: 1 branch
BranchTrueFalseTotal
if (v->is_int) 235 8 243
2329 235 ▶ hv_store_lit(h, "int", v->is_uv ? newSVuv((UV)v->iv)
Branch: 1 branch
BranchTrueFalseTotal
hv_store_lit(h, "int", v->is_uv ? newSVuv((UV)v->iv) 1 234 235
2330 : newSViv(v->iv));
2331 } else {
2332 54 hv_store_lit(h, "str", pool_sv(aTHX_ pr, v->str_off, v->str_len));
2333 }
2334 297 return newRV_noinc((SV *)h);
2335 }
2336  
2337 /* the decoded case descriptor, stored into the op's hash */
2338 315 static void case_to_hv(pTHX_ const stencil_program *pr, uint32_t id, HV *h)
2339 {
2340 315 const stencil_ccase *cs = &stencil_prog_cases(pr)[id];
2341 315 hv_store_lit(h, "case_id", newSVuv(id));
2342 315 ▶ hv_store_lit(h, "kind", newSVpv(cs->kind < STENCIL_CASE__MAX
Branch: 1 branch
BranchTrueFalseTotal
hv_store_lit(h, "kind", newSVpv(cs->kind < STENCIL_CASE__MAX 315 0 315
2343 ? case_kind_names[cs->kind] : "?", 0));
2344 315 switch (cs->kind) {
2345 290 case STENCIL_CASE_VALUE: case STENCIL_CASE_RANGE:
2346 case STENCIL_CASE_LIST: {
2347 290 AV *vals = newAV();
2348 580 uint32_t i, n = cs->kind == STENCIL_CASE_VALUE ? 1
2349 290 ▶ : cs->kind == STENCIL_CASE_RANGE ? 2 : cs->b;
Branch: 2 branches
BranchTrueFalseTotal
: cs->kind == STENCIL_CASE_RANGE ? 2 : cs->b; 7 283 290
: cs->kind == STENCIL_CASE_RANGE ? 2 : cs->b; 2 5 7
2350 587 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 297 290 587
2351 297 av_push(vals, cval_to_sv(aTHX_ pr, cs->a + i));
2352 290 hv_store_lit(h, "values", newRV_noinc((SV *)vals));
2353 290 break;
2354 }
2355 11 case STENCIL_CASE_RX: {
2356 11 const stencil_rx *rx = stencil_prog_rx(pr, cs->a);
2357 char fl[5];
2358 11 int n = 0;
2359 11 ▶ if (cs->b & STENCIL_RX_I) fl[n++] = 'i';
Branch: 1 branch
BranchTrueFalseTotal
if (cs->b & STENCIL_RX_I) fl[n++] = 'i'; 3 8 11
2360 11 ▶ if (cs->b & STENCIL_RX_M) fl[n++] = 'm';
Branch: 1 branch
BranchTrueFalseTotal
if (cs->b & STENCIL_RX_M) fl[n++] = 'm'; 1 10 11
2361 11 ▶ if (cs->b & STENCIL_RX_S) fl[n++] = 's';
Branch: 1 branch
BranchTrueFalseTotal
if (cs->b & STENCIL_RX_S) fl[n++] = 's'; 1 10 11
2362 11 ▶ if (cs->b & STENCIL_RX_X) fl[n++] = 'x';
Branch: 1 branch
BranchTrueFalseTotal
if (cs->b & STENCIL_RX_X) fl[n++] = 'x'; 2 9 11
2363 11 fl[n] = '\0';
2364 11 hv_store_lit(h, "pattern", pool_sv(aTHX_ pr, cs->str_off, cs->str_len));
2365 11 hv_store_lit(h, "flags", newSVpv(fl, 0));
2366 11 hv_store_lit(h, "states", newSVuv(rx->n_insts));
2367 11 hv_store_lit(h, "rx_bytes", newSVuv(rx->size));
2368 11 hv_store_lit(h, "anchored", newSViv(rx->anchored));
2369 11 break;
2370 }
2371 4 case STENCIL_CASE_PATH_NUM: case STENCIL_CASE_PATH_STR:
2372 case STENCIL_CASE_PATH_RX:
2373 4 hv_store_lit(h, "path", path_to_sv(aTHX_ pr, cs->a));
2374 4 break;
2375 1 case STENCIL_CASE_PRED: {
2376 1 const stencil_cfilt *f = &stencil_prog_filts(pr)[cs->a];
2377 1 hv_store_lit(h, "filter",
2378 newSVpvn(stencil_prog_pool(pr) + f->name_off, f->name_len));
2379 1 break;
2380 }
2381 8 case STENCIL_CASE_REF: case STENCIL_CASE_REFTYPE: case STENCIL_CASE_ISA:
2382 8 ▶ if (cs->b)
Branch: 1 branch
BranchTrueFalseTotal
if (cs->b) 5 3 8
2383 5 hv_store_lit(h, "type",
2384 pool_sv(aTHX_ pr, cs->str_off, cs->str_len));
2385 8 break;
2386 }
2387 315 }
2388  
2389 124 SV *stencil_program_inspect(pTHX_ const stencil_program *pr)
2390 {
2391 124 HV *top = newHV();
2392 124 AV *ops = newAV();
2393 124 const uint8_t *code = stencil_prog_code(pr);
2394 124 uint32_t pc = 0;
2395  
2396 1688 ▶ while (pc < pr->code_len) {
Branch: 1 branch
BranchTrueFalseTotal
while (pc < pr->code_len) { 1564 124 1688
2397 1564 uint8_t op = code[pc];
2398 1564 HV *h = newHV();
2399 1564 uint32_t at = pc;
2400 1564 pc++;
2401 1564 hv_store_lit(h, "at", newSVuv(at));
2402 1564 ▶ 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)); 1564 0 1564
2403 1564 switch (op) {
2404 372 case SOP_LITERAL_SHORT: {
2405 372 uint8_t l = code[pc++];
2406 372 hv_store_lit(h, "len", newSVuv(l));
2407 372 hv_store_lit(h, "bytes",
2408 newSVpvn((const char *)code + pc, l));
2409 372 pc += l;
2410 372 break;
2411 }
2412 6 case SOP_LITERAL_LONG: case SOP_PUSH_LIT_STR: {
2413 uint32_t off, l;
2414 6 memcpy(&off, code + pc, 4); pc += 4;
2415 6 memcpy(&l, code + pc, 4); pc += 4;
2416 6 hv_store_lit(h, "off", newSVuv(off));
2417 6 hv_store_lit(h, "len", newSVuv(l));
2418 6 hv_store_lit(h, "bytes",
2419 newSVpvn(stencil_prog_pool(pr) + off, l));
2420 6 break;
2421 }
2422 7 case SOP_PUSH_LIT_NUM: {
2423 NV d;
2424 7 memcpy(&d, code + pc, sizeof d); pc += sizeof d;
2425 7 hv_store_lit(h, "num", newSVnv(d));
2426 7 break;
2427 }
2428 221 case SOP_PUSH_PATH: {
2429 uint32_t id;
2430 221 memcpy(&id, code + pc, 4); pc += 4;
2431 221 hv_store_lit(h, "path_id", newSVuv(id));
2432 221 hv_store_lit(h, "path", path_to_sv(aTHX_ pr, id));
2433 221 break;
2434 }
2435 366 case SOP_TEST_JF: case SOP_TEST_JT: case SOP_JF_KEEP:
2436 case SOP_JT_KEEP: case SOP_JUMP: case SOP_FOR_NEXT: {
2437 uint32_t t;
2438 366 memcpy(&t, code + pc, 4); pc += 4;
2439 366 hv_store_lit(h, "target", newSVuv(t));
2440 366 break;
2441 }
2442 6 case SOP_FOR_ARY: {
2443 uint32_t id, t;
2444 6 memcpy(&id, code + pc, 4); pc += 4;
2445 6 memcpy(&t, code + pc, 4); pc += 4;
2446 6 hv_store_lit(h, "name", name_to_sv(aTHX_ pr, id));
2447 6 hv_store_lit(h, "target", newSVuv(t));
2448 6 break;
2449 }
2450 0 case SOP_FOR_HASH: {
2451 uint32_t k, v, t;
2452 0 memcpy(&k, code + pc, 4); pc += 4;
2453 0 memcpy(&v, code + pc, 4); pc += 4;
2454 0 memcpy(&t, code + pc, 4); pc += 4;
2455 0 hv_store_lit(h, "key", name_to_sv(aTHX_ pr, k));
2456 0 hv_store_lit(h, "val", name_to_sv(aTHX_ pr, v));
2457 0 hv_store_lit(h, "target", newSVuv(t));
2458 0 break;
2459 }
2460 7 case SOP_SET: {
2461 uint32_t id;
2462 7 memcpy(&id, code + pc, 4); pc += 4;
2463 7 hv_store_lit(h, "name", name_to_sv(aTHX_ pr, id));
2464 7 break;
2465 }
2466 5 case SOP_POP_BINDS: {
2467 uint32_t n;
2468 5 memcpy(&n, code + pc, 4); pc += 4;
2469 5 hv_store_lit(h, "count", newSVuv(n));
2470 5 break;
2471 }
2472 2 case SOP_INCLUDE: {
2473 uint32_t id;
2474 const stencil_cinc *inc;
2475 2 memcpy(&id, code + pc, 4); pc += 4;
2476 2 inc = &stencil_prog_incs(pr)[id];
2477 2 hv_store_lit(h, "file",
2478 newSVpvn(stencil_prog_pool(pr) + inc->name_off,
2479 inc->name_len));
2480 2 break;
2481 }
2482 315 case SOP_CASE: {
2483 uint32_t id, t;
2484 315 memcpy(&id, code + pc, 4); pc += 4;
2485 315 memcpy(&t, code + pc, 4); pc += 4;
2486 315 hv_store_lit(h, "target", newSVuv(t));
2487 315 case_to_hv(aTHX_ pr, id, h);
2488 315 break;
2489 }
2490 5 case SOP_CASE_TABLE: {
2491 uint32_t id, t, i;
2492 const stencil_ctab *tab;
2493 5 AV *keys = newAV();
2494 5 memcpy(&id, code + pc, 4); pc += 4;
2495 5 memcpy(&t, code + pc, 4); pc += 4;
2496 5 tab = &stencil_prog_ctabs(pr)[id];
2497 5 hv_store_lit(h, "target", newSVuv(t));
2498 5 hv_store_lit(h, "table_id", newSVuv(id));
2499 35 ▶ for (i = 0; i < tab->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tab->n; i++) { 30 5 35
2500 30 const stencil_ctent *e = &stencil_prog_ctents(pr)[tab->first + i];
2501 30 HV *eh = newHV();
2502 30 hv_store_lit(eh, "str", pool_sv(aTHX_ pr, e->str_off, e->str_len));
2503 30 hv_store_lit(eh, "target", newSVuv(e->target));
2504 30 av_push(keys, newRV_noinc((SV *)eh));
2505 }
2506 5 hv_store_lit(h, "keys", newRV_noinc((SV *)keys));
2507 5 break;
2508 }
2509 3 case SOP_FILTER: {
2510 uint32_t id;
2511 const stencil_cfilt *f;
2512 3 memcpy(&id, code + pc, 4); pc += 4;
2513 3 f = &stencil_prog_filts(pr)[id];
2514 3 hv_store_lit(h, "filter",
2515 newSVpvn(stencil_prog_pool(pr) + f->name_off,
2516 f->name_len));
2517 3 hv_store_lit(h, "builtin_id", newSViv(f->builtin_id));
2518 3 ▶ if (f->has_arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (f->has_arg) { 2 1 3
2519 2 ▶ if (f->arg_is_num)
Branch: 1 branch
BranchTrueFalseTotal
if (f->arg_is_num) 1 1 2
2520 1 hv_store_lit(h, "arg", newSVnv(f->num_arg));
2521 else
2522 1 hv_store_lit(h, "arg",
2523 newSVpvn(stencil_prog_pool(pr) + f->str_off,
2524 f->str_len));
2525 }
2526 3 break;
2527 }
2528 249 default:
2529 249 break;
2530 }
2531 1564 av_push(ops, newRV_noinc((SV *)h));
2532 }
2533 124 hv_store_lit(top, "ops", newRV_noinc((SV *)ops));
2534  
2535 {
2536 124 AV *paths = newAV();
2537 uint32_t i, s;
2538 283 ▶ for (i = 0; i < pr->n_paths; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pr->n_paths; i++) { 159 124 283
2539 159 const stencil_cpath *p = &stencil_prog_paths(pr)[i];
2540 159 HV *ph = newHV();
2541 159 AV *segs = newAV();
2542 159 hv_store_lit(ph, "str", path_to_sv(aTHX_ pr, i));
2543 159 hv_store_lit(ph, "loop_rooted", newSViv(p->loop_rooted));
2544 346 ▶ for (s = 0; s < p->n_segs; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = 0; s < p->n_segs; s++) { 187 159 346
2545 187 const stencil_seg *sg =
2546 187 &stencil_prog_segs(pr)[p->seg_idx + s];
2547 187 HV *sh = newHV();
2548 187 ▶ if (sg->is_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (sg->is_index) { 4 183 187
2549 4 hv_store_lit(sh, "index", newSViv((IV)sg->index));
2550 } else {
2551 183 const stencil_cname *nm =
2552 183 &stencil_prog_names(pr)[sg->name_id];
2553 183 hv_store_lit(sh, "name", name_to_sv(aTHX_ pr,
2554 sg->name_id));
2555 183 hv_store_lit(sh, "hash", newSVuv(nm->hash));
2556 }
2557 187 av_push(segs, newRV_noinc((SV *)sh));
2558 }
2559 159 hv_store_lit(ph, "segs", newRV_noinc((SV *)segs));
2560 159 av_push(paths, newRV_noinc((SV *)ph));
2561 }
2562 124 hv_store_lit(top, "paths", newRV_noinc((SV *)paths));
2563 }
2564 {
2565 124 AV *incs = newAV();
2566 uint32_t i;
2567 126 ▶ for (i = 0; i < pr->n_incs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pr->n_incs; i++) { 2 124 126
2568 2 const stencil_cinc *inc = &stencil_prog_incs(pr)[i];
2569 2 av_push(incs, newSVpvn(stencil_prog_pool(pr) + inc->name_off,
2570 inc->name_len));
2571 }
2572 124 hv_store_lit(top, "includes", newRV_noinc((SV *)incs));
2573 }
2574  
2575 124 hv_store_lit(top, "max_stack", newSVuv(pr->max_stack));
2576 124 hv_store_lit(top, "max_frames", newSVuv(pr->max_frames));
2577 124 hv_store_lit(top, "max_binds", newSVuv(pr->max_binds));
2578 124 hv_store_lit(top, "n_cases", newSVuv(pr->n_cases));
2579 124 hv_store_lit(top, "n_tables", newSVuv(pr->n_ctabs));
2580 124 hv_store_lit(top, "code_len", newSVuv(pr->code_len));
2581 124 hv_store_lit(top, "n_lines", newSVuv(pr->n_lines));
2582 124 hv_store_lit(top, "is_wrapper",
2583 newSViv(!!(pr->flags & STENCIL_PROG_IS_WRAPPER)));
2584  
2585 124 return newRV_noinc((SV *)top);
2586 }
« Stencil.xs ↑ Summary src/stencil_render.c »