tomlc17.c

filter: Statementstmt 49.6% 843 / 1699 Branchbran 33.1% 394 / 1188 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 42.8% 1237 / 2887 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« easyxs/easyxs_numeric.h ↑ Summary XS.xs »
1 /* Copyright (c) 2024-2026, CK Tan.
2 * https://github.com/cktan/tomlc17/blob/main/LICENSE
3 */
4 #include "tomlc17.h"
5 #include <assert.h>
6 #include <ctype.h>
7 #include <errno.h>
8 #include <limits.h>
9 #include <math.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16  
17 const toml_datum_t DATUM_ZERO = {0};
18  
19 static toml_option_t toml_option = {0, realloc, free};
20  
21 #define MALLOC(n) toml_option.mem_realloc(0, n)
22 #define REALLOC(p, n) toml_option.mem_realloc(p, n)
23 #define FREE(p) toml_option.mem_free(p)
24  
25 #define DO(x) \
26 if (x) \
27 return -1; \
28 else \
29 (void)0
30  
31 // Copy string src to dst where dst is limited to dstsz that includes
32 // NUL. Return 0 on success, -1 otherwise (because src[] is longer than dst[]).
33 0 static inline int copystring(char *dst, int dstsz, const char *src) {
34 0 int srcsz = strlen(src) + 1;
35 0 ▶ if (srcsz > dstsz) {
Branch: 1 branch
BranchTrueFalseTotal
if (srcsz > dstsz) { 0 0 0
36 0 return -1;
37 }
38 0 memcpy(dst, src, srcsz);
39 0 return 0;
40 }
41  
42 /*
43 * Error buffer
44 */
45 typedef struct ebuf_t ebuf_t;
46 struct ebuf_t {
47 char *ptr;
48 int len;
49 };
50  
51 /*
52 * Format an error into ebuf[]. Always return -1.
53 */
54 3 static int SETERROR(ebuf_t ebuf, int lineno, const char *fmt, ...) {
55 va_list args;
56 3 va_start(args, fmt);
57 3 char *p = ebuf.ptr;
58 3 char *q = p + ebuf.len;
59 3 ▶ if (lineno) {
Branch: 1 branch
BranchTrueFalseTotal
if (lineno) { 3 0 3
60 3 ▶ snprintf(p, p < q ? q - p : 0, "(line %d) ", lineno);
Branch: 1 branch
BranchTrueFalseTotal
snprintf(p, p < q ? q - p : 0, "(line %d) ", lineno); 3 0 3
61 3 p += strlen(p);
62 }
63 3 ▶ vsnprintf(p, p < q ? q - p : 0, fmt, args);
Branch: 1 branch
BranchTrueFalseTotal
vsnprintf(p, p < q ? q - p : 0, fmt, args); 3 0 3
64 3 va_end(args);
65 3 return -1;
66 }
67  
68 /*
69 * Memory pool. A pool is a pair of page_t lists: small allocations
70 * (<= PAGE_LARGE_THRESHOLD bytes) are bump-allocated out of
71 * PAGE_SMALL_SIZE pages, handed out piecemeal until a page fills up
72 * and a fresh one is linked in front. Large allocations get their
73 * own exactly-sized page, used once and never shared. This avoids
74 * ever mallocing one giant block sized for the whole document.
75 */
76 #define PAGE_SMALL_SIZE 4096
77 #define PAGE_LARGE_THRESHOLD 1024
78 // Small allocations must always fit in a fresh small page.
79 static_assert(PAGE_SMALL_SIZE > PAGE_LARGE_THRESHOLD,
80 "page/threshold invariant");
81  
82 #define PAGE_MAGIC 0x50414745u // "PAGE"
83  
84 typedef struct page_t page_t;
85 struct page_t {
86 uint32_t magic; // PAGE_MAGIC, checked to catch corruption/use-after-free
87 int top; // offset of first free byte in data[]
88 int max; // size of data[]
89 page_t *next; // link to next page in list
90 char data[1]; // first byte starts here
91 };
92  
93 #define POOL_MAGIC 0x504f4f4cu // "POOL"
94  
95 typedef struct pool_t pool_t;
96 struct pool_t {
97 uint32_t magic; // POOL_MAGIC, checked to catch corruption/use-after-free
98 page_t *small; // most recent small page, bump-allocated piecemeal
99 page_t *large; // most recent large page, each used for one alloc only
100 };
101  
102 /**
103 * Create a page of `size` bytes. Return the page on success, or
104 * NULL if out of memory.
105 */
106 8 static page_t *page_create(int size) {
107 8 ▶ if (!(0 <= size && size <= (1 << 30))) { // [0..1GB]
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= size && size <= (1 << 30))) { // [0..1GB] 8 0 8
if (!(0 <= size && size <= (1 << 30))) { // [0..1GB] 0 8 8
108 0 return NULL;
109 }
110 8 size_t totalsz = (size_t) & ((page_t *)0)->data[size];
111 8 page_t *page = MALLOC(totalsz);
112 8 ▶ if (!page) {
Branch: 1 branch
BranchTrueFalseTotal
if (!page) { 0 8 8
113 0 return NULL;
114 }
115 8 page->magic = PAGE_MAGIC;
116 8 page->top = 0;
117 8 page->max = size;
118 8 page->next = NULL;
119 8 return page;
120 }
121  
122 /**
123 * Create an empty memory pool. Return the memory pool on success,
124 * or NULL if out of memory. pool->small is always non-NULL: a
125 * pool holds at least one small page for its whole lifetime.
126 */
127 8 static pool_t *pool_create(void) {
128 8 pool_t *pool = MALLOC(sizeof(pool_t));
129 8 ▶ if (!pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pool) { 0 8 8
130 0 return NULL;
131 }
132 8 pool->magic = POOL_MAGIC;
133 8 pool->small = page_create(PAGE_SMALL_SIZE);
134 8 ▶ if (!pool->small) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pool->small) { 0 8 8
135 0 FREE(pool);
136 0 return NULL;
137 }
138 8 pool->large = NULL;
139 8 return pool;
140 }
141  
142 /**
143 * Destroy a memory pool, freeing every page in it.
144 */
145 11 static void pool_destroy(pool_t *pool) {
146 11 ▶ if (!pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pool) { 3 8 11
147 3 return;
148 }
149 8 ▶ assert(pool->magic == POOL_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
assert(pool->magic == POOL_MAGIC); 0 8 8
150 8 pool->magic = 0; // catch use-after-free/double-destroy
151 16 ▶ for (page_t *p = pool->small; p;) {
Branch: 1 branch
BranchTrueFalseTotal
for (page_t *p = pool->small; p;) { 8 8 16
152 8 page_t *next = p->next;
153 8 ▶ assert(p->magic == PAGE_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
assert(p->magic == PAGE_MAGIC); 0 8 8
154 8 p->magic = 0; // catch use-after-free/double-destroy
155 8 FREE(p);
156 8 p = next;
157 }
158 8 ▶ for (page_t *p = pool->large; p;) {
Branch: 1 branch
BranchTrueFalseTotal
for (page_t *p = pool->large; p;) { 0 8 8
159 0 page_t *next = p->next;
160 0 ▶ assert(p->magic == PAGE_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
assert(p->magic == PAGE_MAGIC); 0 0 0
161 0 p->magic = 0; // catch use-after-free/double-destroy
162 0 FREE(p);
163 0 p = next;
164 }
165 8 FREE(pool);
166 }
167  
168 /**
169 * Allocate n bytes from pool. Return the memory allocated on
170 * success, or NULL if out of memory.
171 */
172 78 static char *pool_alloc(pool_t *pool, int n) {
173 78 ▶ assert(pool->magic == POOL_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
assert(pool->magic == POOL_MAGIC); 0 78 78
174 78 ▶ if (n > PAGE_LARGE_THRESHOLD) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > PAGE_LARGE_THRESHOLD) { 0 78 78
175 // Large alloc: own exactly-sized page, used once.
176 0 page_t *page = page_create(n);
177 0 ▶ if (!page) {
Branch: 1 branch
BranchTrueFalseTotal
if (!page) { 0 0 0
178 0 return NULL;
179 }
180 0 page->next = pool->large;
181 0 pool->large = page;
182 0 page->top = page->max;
183 0 return page->data;
184 }
185  
186 78 ▶ assert(pool->small->magic == PAGE_MAGIC);
Branch: 1 branch
BranchTrueFalseTotal
assert(pool->small->magic == PAGE_MAGIC); 0 78 78
187 78 ▶ if (pool->small->top + n > pool->small->max) {
Branch: 1 branch
BranchTrueFalseTotal
if (pool->small->top + n > pool->small->max) { 0 78 78
188 // Current small page doesn't have room: abandon its leftover
189 // space and start a fresh one.
190 0 page_t *page = page_create(PAGE_SMALL_SIZE);
191 0 ▶ if (!page) {
Branch: 1 branch
BranchTrueFalseTotal
if (!page) { 0 0 0
192 0 return NULL;
193 }
194 0 page->next = pool->small;
195 0 pool->small = page;
196 }
197 78 char *ret = pool->small->data + pool->small->top;
198 78 pool->small->top += n;
199 78 return ret;
200 }
201  
202 // Cell: expandable buffer that behaves like realloc
203 typedef struct cell_t cell_t;
204 struct cell_t {
205 uint32_t top, max;
206 // first byte of data starts here (i.e. &(*this)[1])
207 };
208  
209 192 static char *cell_realloc(char *p, int size) {
210 192 ▶ assert(size >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(size >= 0); 0 192 192
211 192 ▶ if (p == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == NULL) { 56 136 192
212 // first malloc
213 56 cell_t *tmp = REALLOC(NULL, size + sizeof(cell_t));
214 56 ▶ if (!tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { 0 56 56
215 0 return 0; // out of memory
216 }
217 56 cell_t *cp = tmp;
218 56 cp->max = size;
219 56 cp->top = size;
220 56 return (char *)&cp[1];
221 }
222  
223 // obtain a handle to cell info
224 136 cell_t *cp = (cell_t *)(p - sizeof(cell_t));
225 136 ▶ if ((uint32_t)size <= cp->max) {
Branch: 1 branch
BranchTrueFalseTotal
if ((uint32_t)size <= cp->max) { 86 50 136
226 // cell is big enough for the new size. DONE.
227 86 cp->top = size;
228 86 return p;
229 }
230  
231 // need to expand. add 30% margin.
232 50 int newmax = size * 1.3 + 100;
233 50 cell_t *tmp = REALLOC(cp, newmax + sizeof(cell_t));
234 50 ▶ if (!tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { 0 50 50
235 0 return 0; // out of memory
236 }
237 50 cp = tmp;
238 50 cp->max = newmax;
239 50 cp->top = size;
240 50 return (char *)&cp[1];
241 }
242  
243 78 static void cell_free(char *p) {
244 78 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 56 22 78
245 56 cell_t *cp = (cell_t *)(p - sizeof(cell_t));
246 56 FREE(cp);
247 }
248 78 }
249  
250 /* This is a string view. */
251 typedef struct span_t span_t;
252 struct span_t {
253 const char *ptr;
254 int len;
255 };
256  
257 /* Represents a multi-part key */
258 #define KEYPARTMAX 10
259 typedef struct keypart_t keypart_t;
260 struct keypart_t {
261 int nspan;
262 span_t span[KEYPARTMAX];
263 };
264  
265 static int utf8_to_ucs(const char *s, int len, uint32_t *ret);
266 static int ucs_to_utf8(uint32_t code, char buf[4]);
267  
268 // Maximum levels of brackets and braces to prevent
269 // stack overflow during recursive descent of the parser.
270 #define BRACKET_LEVEL_MAX 30
271 #define BRACE_LEVEL_MAX 30
272 #define TABLE_MAX (1 << 14) // 16k
273 #define ARRAY_MAX (1 << 14) // 16k
274  
275 enum toktyp_t {
276 TOK_DOT = 1,
277 TOK_EQUAL,
278 TOK_COMMA,
279 TOK_LBRACK, // [
280 TOK_LLBRACK, // [[
281 TOK_RBRACK, // ]
282 TOK_RRBRACK, // ]]
283 TOK_LBRACE, // {
284 TOK_RBRACE, // }
285 TOK_LIT,
286 TOK_STRING, // "string"
287 TOK_MLSTRING, // """multi-line-string"""
288 TOK_LITSTRING, // 'lit-string'
289 TOK_MLLITSTRING, // '''multi-line-lit-string'''
290 TOK_TIME,
291 TOK_DATE,
292 TOK_DATETIME,
293 TOK_DATETIMETZ,
294 TOK_INTEGER,
295 TOK_FLOAT,
296 TOK_BOOL,
297 TOK_ENDL,
298 TOK_FIN = -5000, // EOF
299 };
300 typedef enum toktyp_t toktyp_t;
301 typedef struct scanner_t scanner_t;
302  
303 /* Remember the current state of a scanner */
304 typedef struct scanner_state_t scanner_state_t;
305 struct scanner_state_t {
306 scanner_t *sp;
307 const char *cur; // points into scanner_t::src[]
308 int lineno; // current line number
309 const char *line_start;
310 };
311  
312 // A scan token
313 typedef struct token_t token_t;
314 struct token_t {
315 toktyp_t toktyp;
316 int lineno;
317 int colno;
318 span_t str;
319  
320 // values represented by str
321 union {
322 const char *escp; // point to an esc char in str
323 int64_t int64;
324 double fp64;
325 bool b1;
326 struct {
327 // validity depends on toktyp for TIME, DATE, DATETIME, DATETIMETZ
328 int year, month, day, hour, minute, sec, usec;
329 int tz; // +- minutes
330 } tsval;
331 } u;
332 };
333  
334 // Scanner object
335 struct scanner_t {
336 const char *src; // src[] is a NUL-terminated string
337 const char *endp; // end of src[]. always pointing at a NUL char.
338 const char *cur; // current char in src[]
339 int lineno; // line number of current char
340 const char *line_start;
341 char *errmsg; // set to ebuf.ptr if there was an error
342 ebuf_t ebuf; // buffer to store error message
343  
344 int bracket_level; // count depth of [ ]
345 int brace_level; // count depth of { }
346 };
347 static void scan_init(scanner_t *sp, const char *src, int len, char *errbuf,
348 int errbufsz);
349 static int scan_key(scanner_t *sp, token_t *tok);
350 static int scan_value(scanner_t *sp, token_t *tok);
351 // restore scanner to state before tok was returned
352 static scanner_state_t scan_mark(scanner_t *sp);
353 static void scan_restore(scanner_t *sp, scanner_state_t state);
354  
355 #ifndef min
356 54 static inline int min(int a, int b) { return a < b ? a : b; }
357 #endif
358  
359 // Copy up to dstsz - 1 chars from the current position of the scanner
360 // to dst, and always terminate dst[] with a NUL if dstsz > 0.
361 54 static void scan_copystr(scanner_t *sp, char *dst, int dstsz) {
362 54 ▶ assert(dstsz > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(dstsz > 0); 0 54 54
363 54 int len = min(sp->endp - sp->cur, dstsz - 1); // account for NUL
364 54 ▶ if (len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len > 0) { 54 0 54
365 54 memcpy(dst, sp->cur, len);
366 54 dst[len] = '\0';
367 }
368 54 }
369  
370 // Parser object
371 typedef struct parser_t parser_t;
372 struct parser_t {
373 scanner_t scanner;
374 toml_datum_t toptab; // top table
375 toml_datum_t *curtab; // current table
376 pool_t *pool; // memory pool for strings
377 const char *srcname; // source name copied into the pool, or NULL
378 ebuf_t ebuf; // buffer to store last error message
379 };
380  
381 // Find key in tab and return its index. If not found, return -1.
382 66 static int tab_find(toml_datum_t *tab, span_t key) {
383 66 ▶ assert(tab->type == TOML_TABLE);
Branch: 1 branch
BranchTrueFalseTotal
assert(tab->type == TOML_TABLE); 0 66 66
384 203 ▶ for (int i = 0, top = tab->u.tab.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = tab->u.tab.size; i < top; i++) { 139 64 203
385 139 ▶ if (tab->u.tab.len[i] == key.len &&
Branch: 1 branch
BranchTrueFalseTotal
if (tab->u.tab.len[i] == key.len && 11 128 139
386 11 ▶ 0 == memcmp(tab->u.tab.key[i], key.ptr, key.len)) {
Branch: 1 branch
BranchTrueFalseTotal
0 == memcmp(tab->u.tab.key[i], key.ptr, key.len)) { 2 9 11
387 2 return i;
388 }
389 }
390 64 return -1;
391 }
392  
393 // Put key into tab dictionary. Return a pointer to the datum for the key.
394 // If the key already exists, returns a pointer to its existing datum (the
395 // datum is NOT zeroed — callers must check datum->type to detect duplicates).
396 // If the key is new, appends it with a zero datum and returns a pointer to it.
397 // Returns NULL on allocation failure.
398 //
399 // Dual-use: tab_add() uses the non-zero type to detect and reject duplicates;
400 // datum_copy/datum_merge() use it to locate or create a slot for writing.
401 55 static toml_datum_t *tab_emplace(toml_datum_t *tab, span_t key,
402 const char **reason) {
403 55 ▶ assert(tab->type == TOML_TABLE);
Branch: 1 branch
BranchTrueFalseTotal
assert(tab->type == TOML_TABLE); 0 55 55
404 55 int i = tab_find(tab, key);
405 55 ▶ if (i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 0) { 0 55 55
406 0 return &tab->u.tab.value[i];
407 }
408  
409 // Expand pkey[], plen[] and value[].
410 55 int N = tab->u.tab.size;
411 55 ▶ if (N >= TABLE_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (N >= TABLE_MAX) { 0 55 55
412 0 *reason = "table too large";
413 0 return NULL;
414 }
415  
416 {
417 char **pkey =
418 55 (char **)cell_realloc((char *)tab->u.tab.key, sizeof(*pkey) * (N + 1));
419 int *plen =
420 55 (int *)cell_realloc((char *)tab->u.tab.len, sizeof(*plen) * (N + 1));
421 55 toml_datum_t *value = (toml_datum_t *)cell_realloc(
422 55 (char *)tab->u.tab.value, sizeof(*value) * (N + 1));
423  
424 // on success, must save new pointers in tab->u.tab because the
425 // old memory areas are gone.
426 55 ▶ if (pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkey) { 55 0 55
427 55 tab->u.tab.key = (const char **)pkey;
428 }
429 55 ▶ if (plen) {
Branch: 1 branch
BranchTrueFalseTotal
if (plen) { 55 0 55
430 55 tab->u.tab.len = plen;
431 }
432 55 ▶ if (value) {
Branch: 1 branch
BranchTrueFalseTotal
if (value) { 55 0 55
433 55 tab->u.tab.value = value;
434 }
435  
436 // if any fail, it is safe to bail out.
437 55 ▶ if (!pkey || !plen || !value) {
Branch: 3 branches
BranchTrueFalseTotal
if (!pkey || !plen || !value) { 55 0 55
if (!pkey || !plen || !value) { 55 0 55
if (!pkey || !plen || !value) { 0 55 55
438 0 *reason = "out of memory";
439 0 return NULL;
440 }
441 }
442  
443 // There is sufficient space in all the arrays for one more element.
444 // Append the new key. The value is set to DATUM_ZERO. Caller will
445 // overwrite with a valid datum.
446 55 tab->u.tab.size = N + 1;
447 55 tab->u.tab.key[N] = (char *)key.ptr;
448 55 tab->u.tab.len[N] = key.len;
449 55 tab->u.tab.value[N] = DATUM_ZERO;
450 55 return &tab->u.tab.value[N];
451 }
452  
453 // Add a new key in tab. Return 0 on success, -1 otherwise.
454 // On error, *reason will point to an error message.
455 55 static int tab_add(toml_datum_t *tab, span_t newkey, toml_datum_t newvalue,
456 const char **reason) {
457 55 ▶ assert(tab->type == TOML_TABLE);
Branch: 1 branch
BranchTrueFalseTotal
assert(tab->type == TOML_TABLE); 0 55 55
458 55 toml_datum_t *pvalue = tab_emplace(tab, newkey, reason);
459 55 ▶ if (!pvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pvalue) { 0 55 55
460 0 return -1;
461 }
462 55 ▶ if (pvalue->type) {
Branch: 1 branch
BranchTrueFalseTotal
if (pvalue->type) { 0 55 55
463 0 *reason = "duplicate key";
464 0 return -1;
465 }
466 55 *pvalue = newvalue;
467 55 return 0;
468 }
469  
470 // Add a new element into an array. Return 0 on success, -1 otherwise.
471 // On error, *reason will point to an error message.
472 27 static toml_datum_t *arr_emplace(toml_datum_t *arr, const char **reason) {
473 27 ▶ assert(arr->type == TOML_ARRAY);
Branch: 1 branch
BranchTrueFalseTotal
assert(arr->type == TOML_ARRAY); 0 27 27
474 27 int n = arr->u.arr.size;
475 27 ▶ if (n >= ARRAY_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (n >= ARRAY_MAX) { 0 27 27
476 0 *reason = "array too large";
477 0 return NULL;
478 }
479 27 toml_datum_t *elem = (toml_datum_t *)cell_realloc((char *)arr->u.arr.elem,
480 sizeof(*elem) * (n + 1));
481 27 ▶ if (!elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (!elem) { 0 27 27
482 0 *reason = "out of memory";
483 0 return NULL;
484 }
485 27 arr->u.arr.elem = elem;
486 27 arr->u.arr.size = n + 1;
487 27 elem[n] = DATUM_ZERO;
488 27 return &elem[n];
489 }
490  
491 // ------------------- parser section
492 static int parse_norm(parser_t *pp, token_t tok, span_t *ret_span);
493 static int parse_val(parser_t *pp, token_t tok, toml_datum_t *ret);
494 static int parse_keyvalue_expr(parser_t *pp, token_t tok);
495 static int parse_std_table_expr(parser_t *pp, token_t tok);
496 static int parse_array_table_expr(parser_t *pp, token_t tok);
497  
498 93 static toml_datum_t mkdatum(toml_type_t ty) {
499 93 toml_datum_t ret = {0};
500 93 ret.type = ty;
501 93 ▶ if (ty == TOML_DATE || ty == TOML_TIME || ty == TOML_DATETIME ||
Branch: 4 branches
BranchTrueFalseTotal
if (ty == TOML_DATE || ty == TOML_TIME || ty == TOML_DATETIME || 92 1 93
if (ty == TOML_DATE || ty == TOML_TIME || ty == TOML_DATETIME || 91 1 92
if (ty == TOML_DATE || ty == TOML_TIME || ty == TOML_DATETIME || 91 0 91
if (ty == TOML_DATE || ty == TOML_TIME || ty == TOML_DATETIME || 9 82 91
502 ty == TOML_DATETIMETZ) {
503 11 ret.u.ts.year = -1;
504 11 ret.u.ts.month = -1;
505 11 ret.u.ts.day = -1;
506 11 ret.u.ts.hour = -1;
507 11 ret.u.ts.minute = -1;
508 11 ret.u.ts.second = -1;
509 11 ret.u.ts.usec = -1;
510 11 ret.u.ts.tz = -1;
511 }
512 93 return ret;
513 }
514  
515 93 static toml_datum_t mkdatum_at(toml_type_t ty, int lineno, int colno) {
516 93 toml_datum_t ret = mkdatum(ty);
517 93 ret.lineno = lineno;
518 93 ret.colno = colno;
519 93 return ret;
520 }
521  
522 // Recursively free any dynamically allocated memory in the datum tree
523 96 static void datum_free(toml_datum_t *datum) {
524 96 ▶ if (datum->type == TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum->type == TOML_TABLE) { 22 74 96
525 77 ▶ for (int i = 0, top = datum->u.tab.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.tab.size; i < top; i++) { 55 22 77
526 55 datum_free(&datum->u.tab.value[i]);
527 }
528 22 cell_free((char *)(void *)datum->u.tab.key);
529 22 cell_free((char *)datum->u.tab.len);
530 22 cell_free((char *)datum->u.tab.value);
531 74 ▶ } else if (datum->type == TOML_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (datum->type == TOML_ARRAY) { 12 62 74
532 39 ▶ for (int i = 0, top = datum->u.arr.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.arr.size; i < top; i++) { 27 12 39
533 27 datum_free(&datum->u.arr.elem[i]);
534 }
535 12 cell_free((char *)datum->u.arr.elem);
536 }
537 // other types do not allocate memory
538 96 *datum = DATUM_ZERO;
539 96 }
540  
541 // Maps each distinct source-name pointer to a single copy in the destination
542 // pool (deduplicated), so the merged pool holds at most one copy of each source
543 // name.
544 typedef struct srcmap_t srcmap_t;
545 struct srcmap_t {
546 pool_t *pool;
547 const char **olds; // cell buffer of old pointers
548 const char **news; // cell buffer of new (copied) pointers
549 int n, cap;
550 };
551  
552 // Returns the deduplicated copy of src, or NULL for a NULL input or on
553 // out-of-memory. Callers distinguish the two: a non-NULL src that yields NULL
554 // is a hard failure.
555 0 static const char *dedup_source(srcmap_t *m, const char *src) {
556 0 ▶ if (!src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!src) { 0 0 0
557 0 return NULL;
558 }
559 0 ▶ for (int i = 0; i < m->n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < m->n; i++) { 0 0 0
560 0 ▶ if (m->olds[i] == src) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->olds[i] == src) { 0 0 0
561 0 return m->news[i];
562 }
563 }
564 // Grow the memo first, so an allocation failure aborts cleanly.
565 0 ▶ if (m->n == m->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (m->n == m->cap) { 0 0 0
566 0 ▶ int newcap = m->cap ? m->cap * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
int newcap = m->cap ? m->cap * 2 : 4; 0 0 0
567 const char **no =
568 0 (const char **)cell_realloc((char *)m->olds, sizeof(*no) * newcap);
569 0 ▶ if (!no) {
Branch: 1 branch
BranchTrueFalseTotal
if (!no) { 0 0 0
570 0 return NULL; // out of memory
571 }
572 0 m->olds = no;
573 const char **nn =
574 0 (const char **)cell_realloc((char *)m->news, sizeof(*nn) * newcap);
575 0 ▶ if (!nn) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nn) { 0 0 0
576 0 return NULL; // out of memory
577 }
578 0 m->news = nn;
579 0 m->cap = newcap;
580 }
581 0 int len = (int)strlen(src) + 1;
582 0 char *p = pool_alloc(m->pool, len);
583 0 ▶ if (!p) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { 0 0 0
584 0 return NULL; // out of memory
585 }
586 0 memcpy(p, src, len);
587 0 m->olds[m->n] = src;
588 0 m->news[m->n] = p;
589 0 m->n++;
590 0 return p;
591 }
592  
593 // Make a deep copy of src to dst.
594 // Return 0 on success, -1 otherwise.
595 0 static int datum_copy(toml_datum_t *dst, toml_datum_t src, srcmap_t *sm,
596 const char **reason) {
597 0 *dst = mkdatum_at(src.type, src.lineno, src.colno);
598 0 dst->flag = src.flag;
599 0 switch (src.type) {
600 0 case TOML_STRING:
601 0 dst->u.str.ptr = pool_alloc(sm->pool, src.u.str.len + 1);
602 0 ▶ if (!dst->u.str.ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (!dst->u.str.ptr) { 0 0 0
603 0 *reason = "out of memory";
604 0 goto bail;
605 }
606 0 dst->u.str.len = src.u.str.len;
607 0 memcpy((char *)dst->u.str.ptr, src.u.str.ptr, src.u.str.len + 1);
608 0 break;
609 0 case TOML_TABLE:
610 0 ▶ for (int i = 0; i < src.u.tab.size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < src.u.tab.size; i++) { 0 0 0
611 0 char *keycopy = pool_alloc(sm->pool, src.u.tab.len[i] + 1);
612 0 ▶ if (!keycopy) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keycopy) { 0 0 0
613 0 *reason = "out of memory";
614 0 goto bail;
615 }
616 0 memcpy(keycopy, src.u.tab.key[i], src.u.tab.len[i] + 1);
617 0 span_t newkey = {keycopy, src.u.tab.len[i]};
618 0 toml_datum_t *pvalue = tab_emplace(dst, newkey, reason);
619 0 ▶ if (!pvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pvalue) { 0 0 0
620 0 goto bail;
621 }
622 0 ▶ if (datum_copy(pvalue, src.u.tab.value[i], sm, reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum_copy(pvalue, src.u.tab.value[i], sm, reason)) { 0 0 0
623 0 goto bail;
624 }
625 }
626 0 break;
627 0 case TOML_ARRAY:
628 0 ▶ for (int i = 0; i < src.u.arr.size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < src.u.arr.size; i++) { 0 0 0
629 0 toml_datum_t *pelem = arr_emplace(dst, reason);
630 0 ▶ if (!pelem) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pelem) { 0 0 0
631 0 goto bail;
632 }
633 0 ▶ if (datum_copy(pelem, src.u.arr.elem[i], sm, reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum_copy(pelem, src.u.arr.elem[i], sm, reason)) { 0 0 0
634 0 goto bail;
635 }
636 }
637 0 break;
638 0 default:
639 0 *dst = src;
640 0 break;
641 }
642  
643 0 dst->source = dedup_source(sm, src.source);
644 0 ▶ if (src.source && !dst->source) {
Branch: 2 branches
BranchTrueFalseTotal
if (src.source && !dst->source) { 0 0 0
if (src.source && !dst->source) { 0 0 0
645 0 *reason = "out of memory";
646 0 goto bail;
647 }
648 0 return 0;
649  
650 0 bail:
651 0 datum_free(dst);
652 0 return -1;
653 }
654  
655 // Check if datum is an array of tables.
656 0 static inline bool is_array_of_tables(toml_datum_t datum) {
657 0 bool ret = (datum.type == TOML_ARRAY);
658 0 ▶ for (int i = 0; ret && i < datum.u.arr.size; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (int i = 0; ret && i < datum.u.arr.size; i++) { 0 0 0
for (int i = 0; ret && i < datum.u.arr.size; i++) { 0 0 0
659 0 ret = (datum.u.arr.elem[i].type == TOML_TABLE);
660 }
661 0 return ret;
662 }
663  
664 // Merge src into dst. Return 0 on success, -1 otherwise.
665 0 static int datum_merge(toml_datum_t *dst, toml_datum_t src, srcmap_t *sm,
666 const char **reason) {
667 0 ▶ if (dst->type != src.type) {
Branch: 1 branch
BranchTrueFalseTotal
if (dst->type != src.type) { 0 0 0
668 0 datum_free(dst);
669 0 return datum_copy(dst, src, sm, reason);
670 }
671 0 switch (src.type) {
672 0 case TOML_TABLE:
673 // for key-value in src:
674 // override key-value in dst.
675 0 ▶ for (int i = 0; i < src.u.tab.size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < src.u.tab.size; i++) { 0 0 0
676 0 char *keycopy = pool_alloc(sm->pool, src.u.tab.len[i] + 1);
677 0 ▶ if (!keycopy) {
Branch: 1 branch
BranchTrueFalseTotal
if (!keycopy) { 0 0 0
678 0 *reason = "out of memory";
679 0 return -1;
680 }
681 0 memcpy(keycopy, src.u.tab.key[i], src.u.tab.len[i] + 1);
682 span_t key;
683 0 key.ptr = keycopy;
684 0 key.len = src.u.tab.len[i];
685 0 toml_datum_t *pvalue = tab_emplace(dst, key, reason);
686 0 ▶ if (!pvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pvalue) { 0 0 0
687 0 return -1;
688 }
689 0 ▶ if (pvalue->type) {
Branch: 1 branch
BranchTrueFalseTotal
if (pvalue->type) { 0 0 0
690 0 ▶ DO(datum_merge(pvalue, src.u.tab.value[i], sm, reason));
Branch: 1 branch
BranchTrueFalseTotal
DO(datum_merge(pvalue, src.u.tab.value[i], sm, reason)); 0 0 0
691 } else {
692 0 datum_free(pvalue);
693 0 ▶ DO(datum_copy(pvalue, src.u.tab.value[i], sm, reason));
Branch: 1 branch
BranchTrueFalseTotal
DO(datum_copy(pvalue, src.u.tab.value[i], sm, reason)); 0 0 0
694 }
695 }
696 0 return 0;
697 0 case TOML_ARRAY:
698 0 ▶ if (is_array_of_tables(*dst) && is_array_of_tables(src)) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_array_of_tables(*dst) && is_array_of_tables(src)) { 0 0 0
if (is_array_of_tables(*dst) && is_array_of_tables(src)) { 0 0 0
699 // append src array to dst
700 0 ▶ for (int i = 0; i < src.u.arr.size; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < src.u.arr.size; i++) { 0 0 0
701 0 toml_datum_t *pelem = arr_emplace(dst, reason);
702 0 ▶ if (!pelem) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pelem) { 0 0 0
703 0 return -1;
704 }
705 0 ▶ DO(datum_copy(pelem, src.u.arr.elem[i], sm, reason));
Branch: 1 branch
BranchTrueFalseTotal
DO(datum_copy(pelem, src.u.arr.elem[i], sm, reason)); 0 0 0
706 }
707 0 return 0;
708 }
709 // fallthru
710 default:
711 0 break;
712 }
713 0 datum_free(dst);
714 0 return datum_copy(dst, src, sm, reason);
715 }
716  
717 // Compare the content of a and b.
718 0 static bool datum_equiv(toml_datum_t a, toml_datum_t b) {
719 0 ▶ if (a.type != b.type) {
Branch: 1 branch
BranchTrueFalseTotal
if (a.type != b.type) { 0 0 0
720 0 return false;
721 }
722 int N;
723 0 switch (a.type) {
724 0 case TOML_STRING:
725 0 ▶ return a.u.str.len == b.u.str.len &&
Branch: 1 branch
BranchTrueFalseTotal
return a.u.str.len == b.u.str.len && 0 0 0
726 0 ▶ 0 == memcmp(a.u.str.ptr, b.u.str.ptr, a.u.str.len);
Branch: 1 branch
BranchTrueFalseTotal
0 == memcmp(a.u.str.ptr, b.u.str.ptr, a.u.str.len); 0 0 0
727 0 case TOML_INT64:
728 0 return a.u.int64 == b.u.int64;
729 0 case TOML_FP64:
730 0 ▶ return a.u.fp64 == b.u.fp64 || (isnan(a.u.fp64) && isnan(b.u.fp64));
Branch: 3 branches
BranchTrueFalseTotal
return a.u.fp64 == b.u.fp64 || (isnan(a.u.fp64) && isnan(b.u.fp64)); 0 0 0
return a.u.fp64 == b.u.fp64 || (isnan(a.u.fp64) && isnan(b.u.fp64)); 0 0 0
return a.u.fp64 == b.u.fp64 || (isnan(a.u.fp64) && isnan(b.u.fp64)); 0 0 0
731 0 case TOML_BOOLEAN:
732 0 return !!a.u.boolean == !!b.u.boolean;
733 0 case TOML_DATE:
734 0 ▶ return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month &&
Branch: 2 branches
BranchTrueFalseTotal
return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month && 0 0 0
return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month && 0 0 0
735 0 ▶ a.u.ts.day == b.u.ts.day;
Branch: 1 branch
BranchTrueFalseTotal
a.u.ts.day == b.u.ts.day; 0 0 0
736 0 case TOML_TIME:
737 0 ▶ return a.u.ts.hour == b.u.ts.hour && a.u.ts.minute == b.u.ts.minute &&
Branch: 1 branch
BranchTrueFalseTotal
return a.u.ts.hour == b.u.ts.hour && a.u.ts.minute == b.u.ts.minute && 0 0 0
738 0 ▶ a.u.ts.second == b.u.ts.second && a.u.ts.usec == b.u.ts.usec;
Branch: 3 branches
BranchTrueFalseTotal
a.u.ts.second == b.u.ts.second && a.u.ts.usec == b.u.ts.usec; 0 0 0
a.u.ts.second == b.u.ts.second && a.u.ts.usec == b.u.ts.usec; 0 0 0
a.u.ts.second == b.u.ts.second && a.u.ts.usec == b.u.ts.usec; 0 0 0
739 0 case TOML_DATETIME:
740 0 ▶ return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month &&
Branch: 1 branch
BranchTrueFalseTotal
return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month && 0 0 0
741 0 ▶ a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour &&
Branch: 2 branches
BranchTrueFalseTotal
a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour && 0 0 0
a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour && 0 0 0
742 0 ▶ a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second &&
Branch: 3 branches
BranchTrueFalseTotal
a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second && 0 0 0
a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second && 0 0 0
a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second && 0 0 0
743 0 ▶ a.u.ts.usec == b.u.ts.usec;
Branch: 1 branch
BranchTrueFalseTotal
a.u.ts.usec == b.u.ts.usec; 0 0 0
744 0 case TOML_DATETIMETZ:
745 0 ▶ return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month &&
Branch: 1 branch
BranchTrueFalseTotal
return a.u.ts.year == b.u.ts.year && a.u.ts.month == b.u.ts.month && 0 0 0
746 0 ▶ a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour &&
Branch: 2 branches
BranchTrueFalseTotal
a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour && 0 0 0
a.u.ts.day == b.u.ts.day && a.u.ts.hour == b.u.ts.hour && 0 0 0
747 0 ▶ a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second &&
Branch: 2 branches
BranchTrueFalseTotal
a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second && 0 0 0
a.u.ts.minute == b.u.ts.minute && a.u.ts.second == b.u.ts.second && 0 0 0
748 0 ▶ a.u.ts.usec == b.u.ts.usec && a.u.ts.tz == b.u.ts.tz;
Branch: 3 branches
BranchTrueFalseTotal
a.u.ts.usec == b.u.ts.usec && a.u.ts.tz == b.u.ts.tz; 0 0 0
a.u.ts.usec == b.u.ts.usec && a.u.ts.tz == b.u.ts.tz; 0 0 0
a.u.ts.usec == b.u.ts.usec && a.u.ts.tz == b.u.ts.tz; 0 0 0
749 0 case TOML_ARRAY:
750 0 N = a.u.arr.size;
751 0 ▶ if (N != b.u.arr.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (N != b.u.arr.size) { 0 0 0
752 0 return false;
753 }
754 0 ▶ for (int i = 0; i < N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < N; i++) { 0 0 0
755 0 ▶ if (!datum_equiv(a.u.arr.elem[i], b.u.arr.elem[i])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!datum_equiv(a.u.arr.elem[i], b.u.arr.elem[i])) { 0 0 0
756 0 return false;
757 }
758 }
759 0 return true;
760 0 case TOML_TABLE:
761 // Tables are unordered maps: match each key of a against any key of b,
762 // not positionally. Equal sizes plus a full one-way key match implies a
763 // bijection because keys are unique within a table.
764 0 N = a.u.tab.size;
765 0 ▶ if (N != b.u.tab.size) {
Branch: 1 branch
BranchTrueFalseTotal
if (N != b.u.tab.size) { 0 0 0
766 0 return false;
767 }
768 0 ▶ for (int i = 0; i < N; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < N; i++) { 0 0 0
769 0 int len = a.u.tab.len[i];
770 0 int j = 0;
771 0 ▶ for (; j < N; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; j < N; j++) { 0 0 0
772 0 ▶ if (len == b.u.tab.len[j] &&
Branch: 1 branch
BranchTrueFalseTotal
if (len == b.u.tab.len[j] && 0 0 0
773 0 ▶ 0 == memcmp(a.u.tab.key[i], b.u.tab.key[j], len)) {
Branch: 1 branch
BranchTrueFalseTotal
0 == memcmp(a.u.tab.key[i], b.u.tab.key[j], len)) { 0 0 0
774 0 break;
775 }
776 }
777 0 ▶ if (j == N) {
Branch: 1 branch
BranchTrueFalseTotal
if (j == N) { 0 0 0
778 0 return false;
779 }
780 0 ▶ if (!datum_equiv(a.u.tab.value[i], b.u.tab.value[j])) {
Branch: 1 branch
BranchTrueFalseTotal
if (!datum_equiv(a.u.tab.value[i], b.u.tab.value[j])) { 0 0 0
781 0 return false;
782 }
783 }
784 0 return true;
785 0 default:
786 0 break;
787 }
788 0 return false;
789 }
790  
791 /**
792 * Override values in r1 using r2. Return a new result. All results
793 * (i.e., r1, r2 and the returned result) must be freed using toml_free()
794 * after use.
795 *
796 * LOGIC:
797 * ret = copy of r1
798 * for each item x in r2:
799 * if x is not in ret:
800 * override
801 * elif x in ret is NOT of the same type:
802 * override
803 * elif x is an array of tables:
804 * append r2.x to ret.x
805 * elif x is a table:
806 * merge r2.x to ret.x
807 * else:
808 * override
809 */
810 0 toml_result_t toml_merge(const toml_result_t *r1, const toml_result_t *r2) {
811 0 const char *reason = "";
812 0 toml_result_t ret = {0};
813 0 pool_t *pool = 0;
814 0 srcmap_t sm = {0};
815 0 ▶ if (!r1->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r1->ok) { 0 0 0
816 0 reason = "param error: r1 not ok";
817 0 goto bail;
818 }
819 0 ▶ if (!r2->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!r2->ok) { 0 0 0
820 0 reason = "param error: r2 not ok";
821 0 goto bail;
822 }
823 0 pool = pool_create();
824 0 ▶ if (!pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pool) { 0 0 0
825 0 reason = "out of memory";
826 0 goto bail;
827 }
828 0 sm.pool = pool;
829  
830 // Make a copy of r1
831 0 ▶ if (datum_copy(&ret.toptab, r1->toptab, &sm, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum_copy(&ret.toptab, r1->toptab, &sm, &reason)) { 0 0 0
832 0 goto bail;
833 }
834  
835 // Merge r2 into the result
836 0 ▶ if (datum_merge(&ret.toptab, r2->toptab, &sm, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum_merge(&ret.toptab, r2->toptab, &sm, &reason)) { 0 0 0
837 0 goto bail;
838 }
839  
840 0 cell_free((char *)(void *)sm.olds);
841 0 cell_free((char *)(void *)sm.news);
842 0 ret.ok = 1;
843 0 ret.__internal = pool;
844 0 return ret;
845  
846 0 bail:
847 0 cell_free((char *)(void *)sm.olds);
848 0 cell_free((char *)(void *)sm.news);
849 0 pool_destroy(pool);
850 0 snprintf(ret.errmsg, sizeof(ret.errmsg), "%s", reason);
851 0 return ret;
852 }
853  
854 0 bool toml_equiv(const toml_result_t *r1, const toml_result_t *r2) {
855 0 ▶ if (!(r1->ok && r2->ok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(r1->ok && r2->ok)) { 0 0 0
if (!(r1->ok && r2->ok)) { 0 0 0
856 0 return false;
857 }
858 0 return datum_equiv(r1->toptab, r2->toptab);
859 }
860  
861 /**
862 * Find a key in a toml_table. Return the value of the key if found,
863 * or a TOML_UNKNOWN otherwise.
864 */
865 71 toml_datum_t toml_get(toml_datum_t datum, const char *key) {
866 71 ▶ if (datum.type == TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (datum.type == TOML_TABLE) { 71 0 71
867 71 int n = datum.u.tab.size;
868 71 ▶ if (n > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n > 0) { 71 0 71
869 71 int key_len = strlen(key);
870 71 const char **pkey = datum.u.tab.key;
871 71 int *plen = datum.u.tab.len;
872 71 toml_datum_t *pvalue = datum.u.tab.value;
873 290 ▶ for (int i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < n; i++) { 289 1 290
874 289 ▶ if (plen[i] == key_len && 0 == memcmp(pkey[i], key, key_len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (plen[i] == key_len && 0 == memcmp(pkey[i], key, key_len)) { 72 217 289
if (plen[i] == key_len && 0 == memcmp(pkey[i], key, key_len)) { 70 2 72
875 70 return pvalue[i];
876 }
877 }
878 }
879 }
880 1 return DATUM_ZERO;
881 }
882  
883 /**
884 * Locate a value starting from a toml_table. Return the value of the key if
885 * found, or a TOML_UNKNOWN otherwise.
886 *
887 * Note: the multipart-key is separated by DOT, and must not have any escape
888 * chars.
889 */
890 0 toml_datum_t toml_seek(toml_datum_t table, const char *multipart_key) {
891 0 ▶ if (table.type != TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (table.type != TOML_TABLE) { 0 0 0
892 0 return DATUM_ZERO;
893 }
894  
895 // Make a mutable copy of the multipart_key for splitting
896 char buf[256];
897 0 ▶ if (copystring(buf, sizeof(buf), multipart_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (copystring(buf, sizeof(buf), multipart_key)) { 0 0 0
898 // if the multipart_key is longer than buffer, just
899 // signal a not-found.
900 0 return DATUM_ZERO;
901 }
902  
903 // Go through the multipart name part by part.
904 0 char *p = buf;
905 0 toml_datum_t datum = table;
906 0 ▶ while (datum.type == TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
while (datum.type == TOML_TABLE) { 0 0 0
907 0 char *q = strchr(p, '.');
908 0 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 0 0
909 // traverse to next key
910 0 *q = 0;
911 0 datum = toml_get(datum, p);
912 0 p = q + 1;
913 0 continue;
914 }
915  
916 // At end of last keypart.
917 // look up p in the final table
918 0 return toml_get(datum, p);
919 }
920  
921 0 return DATUM_ZERO;
922 }
923  
924 /**
925 * Return the default options.
926 */
927 0 toml_option_t toml_default_option(void) {
928 0 toml_option_t opt = {0, realloc, free};
929 0 return opt;
930 }
931  
932 /**
933 * Override the current options.
934 */
935 0 void toml_set_option(toml_option_t opt) { toml_option = opt; }
936  
937 /**
938 * Free the result returned by toml_parse().
939 */
940 8 void toml_free(toml_result_t result) {
941 8 datum_free(&result.toptab);
942 8 pool_destroy((pool_t *)result.__internal);
943 8 }
944  
945 /**
946 * Parse a toml document.
947 */
948 0 toml_result_t toml_parse_file_ex(const char *fname) {
949 0 toml_result_t result = {0};
950 0 FILE *fp = fopen(fname, "r");
951 0 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 0 0 0
952 0 snprintf(result.errmsg, sizeof(result.errmsg), "fopen %s: %s", fname,
953 0 strerror(errno));
954 0 return result;
955 }
956 0 result = toml_parse_file_named(fp, fname);
957 0 fclose(fp);
958 0 return result;
959 }
960  
961 0 toml_result_t toml_parse_file(FILE *fp) {
962 0 return toml_parse_file_named(fp, NULL);
963 }
964  
965 /**
966 * Parse a toml document from a file, tagging datums with name.
967 */
968 0 toml_result_t toml_parse_file_named(FILE *fp, const char *name) {
969 0 toml_result_t result = {0};
970 0 ▶ if (!fp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) { 0 0 0
971 0 snprintf(result.errmsg, sizeof(result.errmsg), "fp is NULL");
972 0 return result;
973 }
974 0 char *buf = 0;
975 0 int top = 0; // number of bytes read into buf[]
976 enum { CHUNKSZ = 8 * 1024 }; // bytes to read per iteration
977  
978 // Read file into memory. cell_realloc handles capacity growth, so we only
979 // need to ask for room for one more chunk (plus a terminating NUL) each pass.
980 // Drive the loop off fread's return value rather than feof(): feof() only
981 // reports true after a read has already hit EOF, and this also guarantees buf
982 // is allocated at least once before the NUL terminator below.
983 0 for (;;) {
984 0 ▶ if (top > INT_MAX - CHUNKSZ - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (top > INT_MAX - CHUNKSZ - 1) { 0 0 0
985 0 snprintf(result.errmsg, sizeof(result.errmsg), "file is too big");
986 0 break;
987 }
988 // add 1 to CHUNKSZ so we always have room for terminating NUL.
989 0 char *tmp = cell_realloc(buf, top + CHUNKSZ + 1);
990 0 ▶ if (!tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tmp) { 0 0 0
991 0 snprintf(result.errmsg, sizeof(result.errmsg), "out of memory");
992 0 break;
993 }
994 0 buf = tmp;
995  
996 0 errno = 0;
997 0 size_t n = fread(buf + top, 1, CHUNKSZ, fp);
998 0 top += n;
999 0 ▶ if (n < CHUNKSZ) { // short read => EOF or error
Branch: 1 branch
BranchTrueFalseTotal
if (n < CHUNKSZ) { // short read => EOF or error 0 0 0
1000 0 ▶ if (ferror(fp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ferror(fp)) { 0 0 0
1001 0 snprintf(result.errmsg, sizeof(result.errmsg), "%s",
1002 0 ▶ errno ? strerror(errno) : "Error reading file");
Branch: 1 branch
BranchTrueFalseTotal
errno ? strerror(errno) : "Error reading file"); 0 0 0
1003 0 break;
1004 }
1005 0 ▶ if (feof(fp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (feof(fp)) { 0 0 0
1006 0 break;
1007 }
1008 // small probability of short read due to signal, etc.
1009 }
1010 }
1011 // error?
1012 0 ▶ if (result.errmsg[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (result.errmsg[0]) { 0 0 0
1013 0 cell_free(buf);
1014 0 return result;
1015 }
1016 0 buf[top] = 0; // NUL terminator
1017  
1018 0 result = toml_parse_named(buf, top, name);
1019 0 cell_free(buf);
1020 0 return result;
1021 }
1022  
1023 87 static void set_source_recursive(toml_datum_t *datum, const char *source) {
1024 87 datum->source = source;
1025 87 switch (datum->type) {
1026 10 case TOML_ARRAY:
1027 37 ▶ for (int i = 0, top = datum->u.arr.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.arr.size; i < top; i++) { 27 10 37
1028 27 set_source_recursive(&datum->u.arr.elem[i], source);
1029 }
1030 10 break;
1031 18 case TOML_TABLE:
1032 73 ▶ for (int i = 0, top = datum->u.tab.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.tab.size; i < top; i++) { 55 18 73
1033 55 set_source_recursive(&datum->u.tab.value[i], source);
1034 }
1035 18 break;
1036 59 default:
1037 59 break;
1038 }
1039 87 }
1040  
1041 /**
1042 * Parse a toml document.
1043 */
1044 8 toml_result_t toml_parse(const char *src, int len) {
1045 8 return toml_parse_named(src, len, NULL);
1046 }
1047  
1048 8 toml_result_t toml_parse_named(const char *src, int len, const char *name) {
1049 8 toml_result_t result = {0};
1050 8 parser_t parser = {0};
1051 8 parser_t *pp = &parser;
1052  
1053 // Check that src is not NULL.
1054 8 ▶ if (!src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!src) { 0 8 8
1055 0 snprintf(result.errmsg, sizeof(result.errmsg), "src is NULL");
1056 0 goto bail;
1057 }
1058  
1059 // Check that src is NUL terminated.
1060 8 ▶ if (src[len]) {
Branch: 1 branch
BranchTrueFalseTotal
if (src[len]) { 0 8 8
1061 0 snprintf(result.errmsg, sizeof(result.errmsg),
1062 "src[] must be NUL terminated");
1063 0 goto bail;
1064 }
1065  
1066 // If user insists, check that src[] is a valid utf8 string.
1067 8 ▶ if (toml_option.check_utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if (toml_option.check_utf8) { 0 8 8
1068 0 int line = 1; // keeps track of line number
1069 0 ▶ for (int i = 0; i < len;) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < len;) { 0 0 0
1070 uint32_t ch;
1071 0 int n = utf8_to_ucs(src + i, len - i, &ch);
1072 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1073 0 snprintf(result.errmsg, sizeof(result.errmsg),
1074 "invalid UTF8 char on line %d", line);
1075 0 goto bail;
1076 }
1077 0 ▶ if (0xD800 <= ch && ch <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (0xD800 <= ch && ch <= 0xDFFF) { 0 0 0
if (0xD800 <= ch && ch <= 0xDFFF) { 0 0 0
1078 // explicitly prohibit surrogates (non-scalar unicode code point)
1079 0 snprintf(result.errmsg, sizeof(result.errmsg),
1080 "invalid UTF8 char \\u%04x on line %d", ch, line);
1081 0 goto bail;
1082 }
1083 0 line += (ch == '\n' ? 1 : 0);
1084 0 i += n;
1085 }
1086 }
1087  
1088 // Initialize parser
1089 8 pp->toptab = mkdatum_at(TOML_TABLE, 1, 1);
1090 8 pp->curtab = &pp->toptab;
1091 8 pp->ebuf.ptr = result.errmsg; // parse error will be printed into pp->ebuf
1092 8 pp->ebuf.len = sizeof(result.errmsg);
1093  
1094 // Alloc memory pool
1095 8 ▶ int namelen = name ? (int)strlen(name) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
int namelen = name ? (int)strlen(name) + 1 : 0; 0 8 8
1096 8 pp->pool = pool_create();
1097 8 ▶ if (!pp->pool) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pp->pool) { 0 8 8
1098 0 snprintf(result.errmsg, sizeof(result.errmsg), "out of memory");
1099 0 goto bail;
1100 }
1101 8 ▶ if (name) {
Branch: 1 branch
BranchTrueFalseTotal
if (name) { 0 8 8
1102 0 char *p = pool_alloc(pp->pool, namelen);
1103 0 ▶ if (!p) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { 0 0 0
1104 0 snprintf(result.errmsg, sizeof(result.errmsg), "out of memory");
1105 0 goto bail;
1106 }
1107 0 memcpy(p, name, namelen);
1108 0 pp->srcname = p;
1109 }
1110  
1111 // Initialize scanner. Scan error will be printed into pp->ebuf.
1112 8 scan_init(&pp->scanner, src, len, pp->ebuf.ptr, pp->ebuf.len);
1113  
1114 // Keep parsing until FIN
1115 70 for (;;) {
1116 token_t tok;
1117 78 ▶ if (scan_key(&pp->scanner, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scan_key(&pp->scanner, &tok)) { 0 78 78
1118 3 goto bail;
1119 }
1120 // break on FIN
1121 78 ▶ if (tok.toktyp == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_FIN) { 5 73 78
1122 5 break;
1123 }
1124 73 switch (tok.toktyp) {
1125 18 case TOK_ENDL: // skip blank lines
1126 70 continue;
1127 9 case TOK_LBRACK:
1128 9 ▶ if (parse_std_table_expr(pp, tok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_std_table_expr(pp, tok)) { 0 9 9
1129 0 goto bail;
1130 }
1131 9 break;
1132 0 case TOK_LLBRACK:
1133 0 ▶ if (parse_array_table_expr(pp, tok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_array_table_expr(pp, tok)) { 0 0 0
1134 0 goto bail;
1135 }
1136 0 break;
1137 46 default:
1138 // non-blank line: parse an expression
1139 46 ▶ if (parse_keyvalue_expr(pp, tok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_keyvalue_expr(pp, tok)) { 3 43 46
1140 3 goto bail;
1141 }
1142 43 break;
1143 }
1144 // each expression must be followed by newline
1145 52 ▶ if (scan_key(&pp->scanner, &tok)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scan_key(&pp->scanner, &tok)) { 0 52 52
1146 0 goto bail;
1147 }
1148 52 ▶ if (tok.toktyp == TOK_FIN || tok.toktyp == TOK_ENDL) {
Branch: 2 branches
BranchTrueFalseTotal
if (tok.toktyp == TOK_FIN || tok.toktyp == TOK_ENDL) { 52 0 52
if (tok.toktyp == TOK_FIN || tok.toktyp == TOK_ENDL) { 52 0 52
1149 52 continue;
1150 }
1151 0 SETERROR(pp->ebuf, tok.lineno, "ENDL expected");
1152 0 goto bail;
1153 }
1154  
1155 // return result
1156 5 result.ok = true;
1157 5 set_source_recursive(&pp->toptab, pp->srcname);
1158 5 result.toptab = pp->toptab;
1159 5 result.__internal = (void *)pp->pool;
1160 5 return result;
1161  
1162 3 bail:
1163 // return error
1164 3 datum_free(&pp->toptab);
1165 3 pool_destroy(pp->pool);
1166 3 result.ok = false;
1167 3 ▶ if (result.errmsg[0] == '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (result.errmsg[0] == '\0') { 0 3 3
1168 0 assert(0);
1169 snprintf(result.errmsg, sizeof(result.errmsg), "Error near line %d\n",
1170 pp->scanner.lineno);
1171 }
1172 3 return result;
1173 }
1174  
1175 // Convert a (LITSTRING, LIT, MLLITSTRING, MLSTRING, or STRING) token to a
1176 // datum.
1177 17 static int token_to_string(parser_t *pp, token_t tok, toml_datum_t *ret) {
1178 17 *ret = mkdatum_at(TOML_STRING, tok.lineno, tok.colno);
1179 span_t span;
1180 17 ▶ DO(parse_norm(pp, tok, &span));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_norm(pp, tok, &span)); 0 17 17
1181 17 ret->u.str.ptr = (char *)span.ptr;
1182 17 ret->u.str.len = span.len;
1183 17 return 0;
1184 }
1185  
1186 // Convert a TIME/DATE/DATETIME/DATETIMETZ to a datum
1187 11 static int token_to_timestamp(parser_t *pp, token_t tok, toml_datum_t *ret) {
1188 (void)pp;
1189 static const toml_type_t map[] = {[TOK_TIME] = TOML_TIME,
1190 [TOK_DATE] = TOML_DATE,
1191 [TOK_DATETIME] = TOML_DATETIME,
1192 [TOK_DATETIMETZ] = TOML_DATETIMETZ};
1193 11 ▶ switch (tok.toktyp) {
Branch: 1 branch
BranchTrueFalseTotal
switch (tok.toktyp) { 11 0 11
1194 11 case TOK_TIME:
1195 case TOK_DATE:
1196 case TOK_DATETIME:
1197 case TOK_DATETIMETZ:
1198 11 break;
1199 0 default:
1200 0 assert(0 && "unexpected token type");
1201 return -1;
1202 }
1203  
1204 11 *ret = mkdatum_at(map[tok.toktyp], tok.lineno, tok.colno);
1205 11 ret->u.ts.year = tok.u.tsval.year;
1206 11 ret->u.ts.month = tok.u.tsval.month;
1207 11 ret->u.ts.day = tok.u.tsval.day;
1208 11 ret->u.ts.hour = tok.u.tsval.hour;
1209 11 ret->u.ts.minute = tok.u.tsval.minute;
1210 11 ret->u.ts.second = tok.u.tsval.sec;
1211 11 ret->u.ts.usec = tok.u.tsval.usec;
1212 11 ret->u.ts.tz = tok.u.tsval.tz;
1213 11 return 0;
1214 }
1215  
1216 // Convert an int64 token to a datum.
1217 11 static int token_to_int64(parser_t *pp, token_t tok, toml_datum_t *ret) {
1218 (void)pp;
1219 11 ▶ assert(tok.toktyp == TOK_INTEGER);
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_INTEGER); 0 11 11
1220 11 *ret = mkdatum_at(TOML_INT64, tok.lineno, tok.colno);
1221 11 ret->u.int64 = tok.u.int64;
1222 11 return 0;
1223 }
1224  
1225 // Convert a fp64 token to a datum.
1226 10 static int token_to_fp64(parser_t *pp, token_t tok, toml_datum_t *ret) {
1227 (void)pp;
1228 10 ▶ assert(tok.toktyp == TOK_FLOAT);
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_FLOAT); 0 10 10
1229 10 *ret = mkdatum_at(TOML_FP64, tok.lineno, tok.colno);
1230 10 ret->u.fp64 = tok.u.fp64;
1231 10 return 0;
1232 }
1233  
1234 // Convert a boolean token to a datum.
1235 10 static int token_to_boolean(parser_t *pp, token_t tok, toml_datum_t *ret) {
1236 (void)pp;
1237 10 ▶ assert(tok.toktyp == TOK_BOOL);
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_BOOL); 0 10 10
1238 10 *ret = mkdatum_at(TOML_BOOLEAN, tok.lineno, tok.colno);
1239 10 ret->u.boolean = tok.u.b1;
1240 10 return 0;
1241 }
1242  
1243 // Parse a multipart key. Return 0 on success, -1 otherwise.
1244 59 static int parse_key(parser_t *pp, token_t tok, keypart_t *ret_keypart) {
1245 59 ret_keypart->nspan = 0;
1246 // key = simple-key | dotted_key
1247 // simple-key = STRING | LITSTRING | LIT
1248 // dotted-key = simple-key (DOT simple-key)+
1249 59 ▶ if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING &&
Branch: 2 branches
BranchTrueFalseTotal
if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING && 54 5 59
if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING && 54 0 54
1250 54 ▶ tok.toktyp != TOK_LIT) {
Branch: 1 branch
BranchTrueFalseTotal
tok.toktyp != TOK_LIT) { 0 54 54
1251 0 return SETERROR(pp->ebuf, tok.lineno, "missing key");
1252 }
1253  
1254 59 int n = 0;
1255 59 span_t *kpspan = ret_keypart->span;
1256  
1257 // Normalize the first keypart
1258 59 ▶ if (parse_norm(pp, tok, &kpspan[n])) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_norm(pp, tok, &kpspan[n])) { 0 59 59
1259 0 return SETERROR(pp->ebuf, tok.lineno,
1260 "unable to normalize string; probably a unicode issue");
1261 }
1262 59 n++;
1263  
1264 // Scan and normalize the second to last keypart
1265 2 while (1) {
1266 61 scanner_state_t mark = scan_mark(&pp->scanner);
1267  
1268 // Eat the dot if it is there
1269 61 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 61 61
1270  
1271 // If not a dot, we are done with keyparts.
1272 61 ▶ if (tok.toktyp != TOK_DOT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp != TOK_DOT) { 59 2 61
1273 59 scan_restore(&pp->scanner, mark);
1274 59 break;
1275 }
1276  
1277 // Scan the n-th key
1278 2 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 2 2
1279  
1280 2 ▶ if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING &&
Branch: 2 branches
BranchTrueFalseTotal
if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING && 2 0 2
if (tok.toktyp != TOK_STRING && tok.toktyp != TOK_LITSTRING && 2 0 2
1281 2 ▶ tok.toktyp != TOK_LIT) {
Branch: 1 branch
BranchTrueFalseTotal
tok.toktyp != TOK_LIT) { 0 2 2
1282 0 return SETERROR(pp->ebuf, tok.lineno, "expects a string in dotted-key");
1283 }
1284  
1285 2 ▶ if (n >= KEYPARTMAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (n >= KEYPARTMAX) { 0 2 2
1286 0 return SETERROR(pp->ebuf, tok.lineno, "too many key parts");
1287 }
1288  
1289 // Normalize the n-th key.
1290 2 ▶ DO(parse_norm(pp, tok, &kpspan[n]));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_norm(pp, tok, &kpspan[n])); 0 2 2
1291 2 n++;
1292 }
1293  
1294 // This key has n parts.
1295 59 ret_keypart->nspan = n;
1296 59 return 0;
1297 }
1298  
1299 // Starting at toptab, descend following keypart[]. If a key does not
1300 // exist in the current table, create a new table entry for the
1301 // key. Returns the final table represented by the key.
1302 13 static toml_datum_t *descend_keypart(parser_t *pp, int lineno, int colno,
1303 toml_datum_t *toptab, keypart_t *keypart,
1304 bool stdtabexpr) {
1305 13 toml_datum_t *tab = toptab; // current tab
1306  
1307 15 ▶ for (int i = 0; i < keypart->nspan; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < keypart->nspan; i++) { 2 13 15
1308 const char *reason;
1309 // Find the i-th keypart
1310 2 int j = tab_find(tab, keypart->span[i]);
1311 // Not found: add a new (key, tab) pair.
1312 2 ▶ if (j < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < 0) { 0 2 2
1313 0 toml_datum_t newtab = mkdatum_at(TOML_TABLE, lineno, colno);
1314 0 ▶ newtab.flag |= stdtabexpr ? TOML_FLAG_STDEXPR : 0;
Branch: 1 branch
BranchTrueFalseTotal
newtab.flag |= stdtabexpr ? TOML_FLAG_STDEXPR : 0; 0 0 0
1315 0 ▶ if (tab_add(tab, keypart->span[i], newtab, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, keypart->span[i], newtab, &reason)) { 0 0 0
1316 0 SETERROR(pp->ebuf, lineno, "%s", reason);
1317 0 return NULL;
1318 }
1319 0 tab = &tab->u.tab.value[tab->u.tab.size - 1]; // descend
1320 0 continue;
1321 }
1322  
1323 // Found: extract the value of the key.
1324 2 toml_datum_t *value = &tab->u.tab.value[j];
1325  
1326 // If the value is a table, descend.
1327 2 ▶ if (value->type == TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->type == TOML_TABLE) { 2 0 2
1328 2 tab = value; // descend
1329 2 continue;
1330 }
1331  
1332 // If the value is an array: locate the last entry and descend.
1333 0 ▶ if (value->type == TOML_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->type == TOML_ARRAY) { 0 0 0
1334 // If empty: error.
1335 0 ▶ if (value->u.arr.size <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->u.arr.size <= 0) { 0 0 0
1336 0 SETERROR(pp->ebuf, lineno, "array %s has no elements",
1337 keypart->span[i].ptr);
1338 0 return NULL;
1339 }
1340  
1341 // Extract the last element of the array.
1342 0 value = &value->u.arr.elem[value->u.arr.size - 1];
1343  
1344 // It must be a table!
1345 0 ▶ if (value->type != TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->type != TOML_TABLE) { 0 0 0
1346 0 SETERROR(pp->ebuf, lineno, "array %s must be array of tables",
1347 keypart->span[i].ptr);
1348 0 return NULL;
1349 }
1350 0 tab = value; // descend
1351 0 continue;
1352 }
1353  
1354 // key not found
1355 0 SETERROR(pp->ebuf, lineno, "cannot locate table at key %s",
1356 keypart->span[i].ptr);
1357 0 return NULL;
1358 }
1359  
1360 // Return the table corresponding to the keypart[].
1361 13 return tab;
1362 }
1363  
1364 // Recursively set flags on datum
1365 49 static void set_flag_recursive(toml_datum_t *datum, uint32_t flag) {
1366 49 datum->flag |= flag;
1367 49 switch (datum->type) {
1368 13 case TOML_ARRAY:
1369 44 ▶ for (int i = 0, top = datum->u.arr.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.arr.size; i < top; i++) { 31 13 44
1370 31 set_flag_recursive(&datum->u.arr.elem[i], flag);
1371 }
1372 13 break;
1373 7 case TOML_TABLE:
1374 11 ▶ for (int i = 0, top = datum->u.tab.size; i < top; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0, top = datum->u.tab.size; i < top; i++) { 4 7 11
1375 4 set_flag_recursive(&datum->u.tab.value[i], flag);
1376 }
1377 7 break;
1378 29 default:
1379 29 break;
1380 }
1381 49 }
1382  
1383 // Parse an inline array.
1384 12 static int parse_inline_array(parser_t *pp, token_t tok,
1385 toml_datum_t *ret_datum) {
1386 12 ▶ assert(tok.toktyp == TOK_LBRACK);
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_LBRACK); 0 12 12
1387 12 *ret_datum = mkdatum_at(TOML_ARRAY, tok.lineno, tok.colno);
1388 12 int need_comma = 0;
1389  
1390 // loop until RBRACK
1391 46 for (;;) {
1392 // skip ENDL
1393 do {
1394 59 ▶ DO(scan_value(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_value(&pp->scanner, &tok)); 1 57 58
1395 57 ▶ } while (tok.toktyp == TOK_ENDL);
Branch: 1 branch
BranchTrueFalseTotal
} while (tok.toktyp == TOK_ENDL); 0 57 57
1396  
1397 // If got an RBRACK: done!
1398 57 ▶ if (tok.toktyp == TOK_RBRACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_RBRACK) { 10 47 57
1399 10 break;
1400 }
1401  
1402 // If got a COMMA: check if it is expected.
1403 47 ▶ if (tok.toktyp == TOK_COMMA) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_COMMA) { 19 28 47
1404 19 ▶ if (need_comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_comma) { 19 0 19
1405 19 need_comma = 0;
1406 19 continue;
1407 }
1408 0 return SETERROR(pp->ebuf, tok.lineno,
1409 "syntax error while parsing array: unexpected comma");
1410 }
1411  
1412 // Not a comma, but need a comma: error!
1413 28 ▶ if (need_comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_comma) { 0 28 28
1414 0 return SETERROR(pp->ebuf, tok.lineno,
1415 "syntax error while parsing array: missing comma");
1416 }
1417  
1418 // This is a valid value! Obtain the value.
1419 28 toml_datum_t value = DATUM_ZERO;
1420 28 ▶ if (parse_val(pp, tok, &value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_val(pp, tok, &value)) { 1 27 28
1421 1 datum_free(&value);
1422 1 return -1;
1423 }
1424  
1425 // Add the value to the array.
1426 const char *reason;
1427 27 toml_datum_t *pelem = arr_emplace(ret_datum, &reason);
1428 27 ▶ if (!pelem) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pelem) { 0 27 27
1429 0 datum_free(&value);
1430 0 return SETERROR(pp->ebuf, tok.lineno, "while parsing array: %s", reason);
1431 }
1432 27 *pelem = value;
1433  
1434 // Need comma before the next value.
1435 27 need_comma = 1;
1436 }
1437  
1438 // Set the INLINE flag for all things in this array.
1439 10 set_flag_recursive(ret_datum, TOML_FLAG_INLINED);
1440 10 return 0;
1441 }
1442  
1443 // Parse an inline table.
1444 5 static int parse_inline_table(parser_t *pp, token_t tok,
1445 toml_datum_t *ret_datum) {
1446 5 ▶ assert(tok.toktyp == TOK_LBRACE);
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_LBRACE); 0 5 5
1447 5 *ret_datum = mkdatum_at(TOML_TABLE, tok.lineno, tok.colno);
1448 5 bool need_comma = 0;
1449 5 bool was_comma = 0;
1450  
1451 // loop until RBRACE
1452 4 for (;;) {
1453 10 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 9 9
1454  
1455 // Got an RBRACE: done!
1456 9 ▶ if (tok.toktyp == TOK_RBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_RBRACE) { 4 5 9
1457 if (was_comma) {
1458 /*
1459 return SETERROR(pp->ebuf, tok.lineno,
1460 "extra comma before closing brace");
1461 */
1462 // extra comma before RBRACE is allowed for v1.1
1463 (void)0;
1464 }
1465 4 break;
1466 }
1467  
1468 // Got a comma: check if it is expected.
1469 5 ▶ if (tok.toktyp == TOK_COMMA) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_COMMA) { 1 4 5
1470 1 ▶ if (need_comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_comma) { 1 0 1
1471 1 need_comma = 0, was_comma = 1;
1472 1 continue;
1473 }
1474 0 return SETERROR(pp->ebuf, tok.lineno, "unexpected comma");
1475 }
1476  
1477 // Newline not allowed in inline table.
1478 // newline is allowed in v1.1
1479 4 ▶ if (tok.toktyp == TOK_ENDL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_ENDL) { 0 4 4
1480 // return SETERROR(pp->ebuf, tok.lineno, "unexpected newline");
1481 0 continue;
1482 }
1483  
1484 // Not a comma, but need a comma: error!
1485 4 ▶ if (need_comma) {
Branch: 1 branch
BranchTrueFalseTotal
if (need_comma) { 0 4 4
1486 0 return SETERROR(pp->ebuf, tok.lineno, "missing comma");
1487 }
1488  
1489 // Get the keyparts
1490 4 keypart_t keypart = {0};
1491 4 int keylineno = tok.lineno;
1492 4 int keycolno = tok.colno;
1493 4 ▶ DO(parse_key(pp, tok, &keypart));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_key(pp, tok, &keypart)); 0 4 4
1494  
1495 // Descend to one keypart before last
1496 4 ▶ assert(keypart.nspan > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(keypart.nspan > 0); 0 4 4
1497 4 span_t lastkeypart = keypart.span[--keypart.nspan];
1498 toml_datum_t *tab =
1499 4 descend_keypart(pp, keylineno, keycolno, ret_datum, &keypart, false);
1500 4 ▶ if (!tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) { 0 4 4
1501 0 return -1;
1502 }
1503  
1504 // If tab is a previously declared inline table: error.
1505 4 ▶ if (tab->flag & TOML_FLAG_INLINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->flag & TOML_FLAG_INLINED) { 0 4 4
1506 0 return SETERROR(pp->ebuf, tok.lineno, "inline table cannot be extended");
1507 }
1508  
1509 // We are explicitly defining it now.
1510 4 tab->flag |= TOML_FLAG_EXPLICIT;
1511  
1512 // match EQUAL
1513 4 ▶ DO(scan_value(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_value(&pp->scanner, &tok)); 0 4 4
1514  
1515 4 ▶ if (tok.toktyp != TOK_EQUAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp != TOK_EQUAL) { 0 4 4
1516 0 ▶ if (tok.toktyp == TOK_ENDL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp == TOK_ENDL) { 0 0 0
1517 0 return SETERROR(pp->ebuf, tok.lineno, "unexpected newline");
1518 } else {
1519 0 return SETERROR(pp->ebuf, tok.lineno, "missing '='");
1520 }
1521 }
1522  
1523 // obtain the value
1524 4 ▶ DO(scan_value(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_value(&pp->scanner, &tok)); 0 4 4
1525 4 toml_datum_t value = DATUM_ZERO;
1526 4 ▶ if (parse_val(pp, tok, &value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_val(pp, tok, &value)) { 1 3 4
1527 1 datum_free(&value);
1528 1 return -1;
1529 }
1530  
1531 // Add the value to tab.
1532 const char *reason;
1533 3 ▶ if (tab_add(tab, lastkeypart, value, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, lastkeypart, value, &reason)) { 0 3 3
1534 0 datum_free(&value);
1535 0 return SETERROR(pp->ebuf, tok.lineno, "%s", reason);
1536 }
1537 3 need_comma = 1, was_comma = 0;
1538 }
1539  
1540 4 set_flag_recursive(ret_datum, TOML_FLAG_INLINED);
1541 4 return 0;
1542 }
1543  
1544 // Parse a value.
1545 76 static int parse_val(parser_t *pp, token_t tok, toml_datum_t *ret) {
1546 76 *ret = DATUM_ZERO; // initialize
1547  
1548 // val = string / boolean / array / inline-table / date-time / float / integer
1549 76 switch (tok.toktyp) {
1550 17 case TOK_STRING:
1551 case TOK_MLSTRING:
1552 case TOK_LITSTRING:
1553 case TOK_MLLITSTRING:
1554 17 return token_to_string(pp, tok, ret);
1555 11 case TOK_TIME:
1556 case TOK_DATE:
1557 case TOK_DATETIME:
1558 case TOK_DATETIMETZ:
1559 11 return token_to_timestamp(pp, tok, ret);
1560 11 case TOK_INTEGER:
1561 11 return token_to_int64(pp, tok, ret);
1562 10 case TOK_FLOAT:
1563 10 return token_to_fp64(pp, tok, ret);
1564 10 case TOK_BOOL:
1565 10 return token_to_boolean(pp, tok, ret);
1566 12 case TOK_LBRACK: // inline-array
1567 12 return parse_inline_array(pp, tok, ret);
1568 5 case TOK_LBRACE: // inline-table
1569 5 return parse_inline_table(pp, tok, ret);
1570 0 default:
1571 0 return SETERROR(pp->ebuf, tok.lineno, "missing value");
1572 }
1573 }
1574  
1575 // Parse a standard table expression, and set the curtab of the parser
1576 // to the table referenced. A standard table expression is a line
1577 // like [a.b.c.d].
1578 9 static int parse_std_table_expr(parser_t *pp, token_t tok) {
1579 // std-table = [ key ]
1580 // Eat the [
1581 9 ▶ assert(tok.toktyp == TOK_LBRACK); // [ ate by caller
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_LBRACK); // [ ate by caller 0 9 9
1582  
1583 // Read the first keypart
1584 9 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 9 9
1585  
1586 // Extract the keypart[]
1587 9 int keylineno = tok.lineno;
1588 9 int keycolno = tok.colno;
1589 keypart_t keypart;
1590 9 ▶ DO(parse_key(pp, tok, &keypart));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_key(pp, tok, &keypart)); 0 9 9
1591  
1592 // Eat the ]
1593 9 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 9 9
1594 9 ▶ if (tok.toktyp != TOK_RBRACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp != TOK_RBRACK) { 0 9 9
1595 0 return SETERROR(pp->ebuf, tok.lineno, "missing right-bracket");
1596 }
1597  
1598 // Descend to one keypart before last.
1599 9 span_t lastkeypart = keypart.span[--keypart.nspan];
1600  
1601 // Descend keypart from the toptab.
1602 toml_datum_t *tab =
1603 9 descend_keypart(pp, keylineno, keycolno, &pp->toptab, &keypart, true);
1604 9 ▶ if (!tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) { 0 9 9
1605 0 return -1;
1606 }
1607  
1608 // Look for the last keypart in the final tab
1609 9 int j = tab_find(tab, lastkeypart);
1610 9 ▶ if (j < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < 0) { 9 0 9
1611 // If not found: add it.
1612 9 ▶ if (tab->flag & TOML_FLAG_INLINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->flag & TOML_FLAG_INLINED) { 0 9 9
1613 0 return SETERROR(pp->ebuf, keylineno, "inline table cannot be extended");
1614 }
1615 const char *reason;
1616 9 toml_datum_t newtab = mkdatum_at(TOML_TABLE, keylineno, keycolno);
1617 9 newtab.flag |= TOML_FLAG_STDEXPR;
1618 9 ▶ if (tab_add(tab, lastkeypart, newtab, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, lastkeypart, newtab, &reason)) { 0 9 9
1619 0 return SETERROR(pp->ebuf, keylineno, "%s", reason);
1620 }
1621 // this is the new tab
1622 9 tab = &tab->u.tab.value[tab->u.tab.size - 1];
1623 } else {
1624 // Found: check for errors
1625 0 tab = &tab->u.tab.value[j];
1626 0 ▶ if (tab->flag & TOML_FLAG_EXPLICIT) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->flag & TOML_FLAG_EXPLICIT) { 0 0 0
1627 /*
1628 This is not OK:
1629 [x.y.z]
1630 [x.y.z]
1631  
1632 but this is OK:
1633 [x.y.z]
1634 [x]
1635 */
1636 0 return SETERROR(pp->ebuf, keylineno, "table defined more than once");
1637 }
1638 0 ▶ if (!(tab->flag & TOML_FLAG_STDEXPR)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(tab->flag & TOML_FLAG_STDEXPR)) { 0 0 0
1639 /*
1640 [t1] # OK
1641 t2.t3.v = 0 # OK
1642 [t1.t2] # should FAIL - t2 was non-explicit but was not
1643 created by std-table-expr
1644 */
1645 0 return SETERROR(pp->ebuf, keylineno, "table defined before");
1646 }
1647 }
1648  
1649 // Set explicit flag on tab
1650 9 tab->flag |= TOML_FLAG_EXPLICIT;
1651 9 tab->lineno = keylineno;
1652 9 tab->colno = keycolno;
1653  
1654 // Set tab as curtab of the parser
1655 9 pp->curtab = tab;
1656 9 return 0;
1657 }
1658  
1659 // Parse an array table expression, and set the curtab of the parser
1660 // to the table referenced. A standard array table expression is a line
1661 // like [[a.b.c.d]].
1662 0 static int parse_array_table_expr(parser_t *pp, token_t tok) {
1663 // array-table = [[ key ]]
1664 0 ▶ assert(tok.toktyp == TOK_LLBRACK); // [[ ate by caller
Branch: 1 branch
BranchTrueFalseTotal
assert(tok.toktyp == TOK_LLBRACK); // [[ ate by caller 0 0 0
1665  
1666 // Read the first keypart
1667 0 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 0 0
1668  
1669 0 int keylineno = tok.lineno;
1670 0 int keycolno = tok.colno;
1671 keypart_t keypart;
1672 0 ▶ DO(parse_key(pp, tok, &keypart));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_key(pp, tok, &keypart)); 0 0 0
1673  
1674 // eat the ]]
1675 token_t rrb;
1676 0 ▶ DO(scan_key(&pp->scanner, &rrb));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &rrb)); 0 0 0
1677 0 ▶ if (rrb.toktyp != TOK_RRBRACK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rrb.toktyp != TOK_RRBRACK) { 0 0 0
1678 0 return SETERROR(pp->ebuf, rrb.lineno, "missing ']]'");
1679 }
1680  
1681 // remove the last keypart from keypart[]
1682 0 ▶ assert(keypart.nspan > 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(keypart.nspan > 0); 0 0 0
1683 0 span_t lastkeypart = keypart.span[--keypart.nspan];
1684  
1685 // descend intermediate keys from toptab
1686 toml_datum_t *tab =
1687 0 descend_keypart(pp, keylineno, keycolno, &pp->toptab, &keypart, true);
1688 0 ▶ if (!tab)
Branch: 1 branch
BranchTrueFalseTotal
if (!tab) 0 0 0
1689 0 return -1;
1690  
1691 // For the final keypart, make sure entry at key is an array of tables
1692 const char *reason;
1693 0 int idx = tab_find(tab, lastkeypart);
1694 0 ▶ if (idx == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (idx == -1) { 0 0 0
1695 // If not found, add an array of table.
1696 0 ▶ if (tab->flag & TOML_FLAG_INLINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->flag & TOML_FLAG_INLINED) { 0 0 0
1697 0 return SETERROR(pp->ebuf, keylineno, "inline table cannot be extended");
1698 }
1699 0 toml_datum_t newarr = mkdatum_at(TOML_ARRAY, keylineno, keycolno);
1700 0 ▶ if (tab_add(tab, lastkeypart, newarr, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, lastkeypart, newarr, &reason)) { 0 0 0
1701 0 return SETERROR(pp->ebuf, keylineno, "%s", reason);
1702 }
1703 0 idx = tab_find(tab, lastkeypart);
1704 0 ▶ assert(idx >= 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(idx >= 0); 0 0 0
1705 }
1706 // Check that this is an array.
1707 0 ▶ if (tab->u.tab.value[idx].type != TOML_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->u.tab.value[idx].type != TOML_ARRAY) { 0 0 0
1708 0 return SETERROR(pp->ebuf, keylineno, "entry must be an array");
1709 }
1710 // Add an empty table to the array
1711 0 toml_datum_t *arr = &tab->u.tab.value[idx];
1712 0 ▶ if (arr->flag & TOML_FLAG_INLINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (arr->flag & TOML_FLAG_INLINED) { 0 0 0
1713 0 return SETERROR(pp->ebuf, keylineno, "cannot extend a static array");
1714 }
1715 0 toml_datum_t *pelem = arr_emplace(arr, &reason);
1716 0 ▶ if (!pelem) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pelem) { 0 0 0
1717 0 return SETERROR(pp->ebuf, keylineno, "%s", reason);
1718 }
1719 0 *pelem = mkdatum_at(TOML_TABLE, keylineno, keycolno);
1720  
1721 // Set the last element of this array as curtab of the parser
1722 0 pp->curtab = &arr->u.arr.elem[arr->u.arr.size - 1];
1723 0 ▶ assert(pp->curtab->type == TOML_TABLE);
Branch: 1 branch
BranchTrueFalseTotal
assert(pp->curtab->type == TOML_TABLE); 0 0 0
1724  
1725 0 return 0;
1726 }
1727  
1728 // Parse an expression. A toml doc is just a list of expressions.
1729 46 static int parse_keyvalue_expr(parser_t *pp, token_t tok) {
1730 // Obtain the key
1731 46 int keylineno = tok.lineno;
1732 46 int keycolno = tok.colno;
1733 keypart_t keypart;
1734 46 ▶ DO(parse_key(pp, tok, &keypart));
Branch: 1 branch
BranchTrueFalseTotal
DO(parse_key(pp, tok, &keypart)); 0 46 46
1735  
1736 // match the '='
1737 46 ▶ DO(scan_key(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_key(&pp->scanner, &tok)); 0 46 46
1738 46 ▶ if (tok.toktyp != TOK_EQUAL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tok.toktyp != TOK_EQUAL) { 1 45 46
1739 1 return SETERROR(pp->ebuf, tok.lineno, "expect '='");
1740 }
1741  
1742 // Locate the last table using keypart[]
1743 const char *reason;
1744 45 toml_datum_t *tab = pp->curtab;
1745 45 ▶ for (int i = 0; i < keypart.nspan - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; i < keypart.nspan - 1; i++) { 0 45 45
1746 0 int j = tab_find(tab, keypart.span[i]);
1747 0 ▶ if (j < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (j < 0) { 0 0 0
1748 0 ▶ if (i > 0 && (tab->flag & TOML_FLAG_EXPLICIT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (i > 0 && (tab->flag & TOML_FLAG_EXPLICIT)) { 0 0 0
if (i > 0 && (tab->flag & TOML_FLAG_EXPLICIT)) { 0 0 0
1749 0 return SETERROR(
1750 pp->ebuf, keylineno,
1751 "cannot extend a previously defined table using dotted expression");
1752 }
1753 0 toml_datum_t newtab = mkdatum_at(TOML_TABLE, keylineno, keycolno);
1754 0 ▶ if (tab_add(tab, keypart.span[i], newtab, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, keypart.span[i], newtab, &reason)) { 0 0 0
1755 0 return SETERROR(pp->ebuf, keylineno, "%s", reason);
1756 }
1757 0 tab = &tab->u.tab.value[tab->u.tab.size - 1];
1758 0 continue;
1759 }
1760 0 toml_datum_t *value = &tab->u.tab.value[j];
1761 0 ▶ if (value->type == TOML_TABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->type == TOML_TABLE) { 0 0 0
1762 0 tab = value;
1763 0 continue;
1764 }
1765 0 ▶ if (value->type == TOML_ARRAY) {
Branch: 1 branch
BranchTrueFalseTotal
if (value->type == TOML_ARRAY) { 0 0 0
1766 0 return SETERROR(pp->ebuf, keylineno,
1767 "encountered previously declared array '%s'",
1768 keypart.span[i].ptr);
1769 }
1770 0 return SETERROR(pp->ebuf, keylineno, "cannot locate table at '%s'",
1771 keypart.span[i].ptr);
1772 }
1773  
1774 // Check for disallowed situations.
1775 45 ▶ if (tab->flag & TOML_FLAG_INLINED) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab->flag & TOML_FLAG_INLINED) { 0 45 45
1776 0 return SETERROR(pp->ebuf, keylineno, "inline table cannot be extended");
1777 }
1778 45 ▶ if (keypart.nspan > 1 && (tab->flag & TOML_FLAG_EXPLICIT)) {
Branch: 2 branches
BranchTrueFalseTotal
if (keypart.nspan > 1 && (tab->flag & TOML_FLAG_EXPLICIT)) { 0 45 45
if (keypart.nspan > 1 && (tab->flag & TOML_FLAG_EXPLICIT)) { 0 0 0
1779 0 return SETERROR(
1780 pp->ebuf, keylineno,
1781 "cannot extend a previously defined table using dotted expression");
1782 }
1783  
1784 // Obtain the value
1785 45 ▶ DO(scan_value(&pp->scanner, &tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_value(&pp->scanner, &tok)); 1 44 45
1786 44 toml_datum_t newval = DATUM_ZERO;
1787 44 ▶ if (parse_val(pp, tok, &newval)) {
Branch: 1 branch
BranchTrueFalseTotal
if (parse_val(pp, tok, &newval)) { 1 43 44
1788 1 datum_free(&newval);
1789 1 return -1;
1790 }
1791  
1792 // Add a new key/value for tab.
1793 43 ▶ if (tab_add(tab, keypart.span[keypart.nspan - 1], newval, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (tab_add(tab, keypart.span[keypart.nspan - 1], newval, &reason)) { 0 43 43
1794 0 datum_free(&newval);
1795 0 return SETERROR(pp->ebuf, keylineno, "%s", reason);
1796 }
1797  
1798 43 return 0;
1799 }
1800  
1801 // Normalize a LIT/STRING/MLSTRING/LITSTRING/MLLITSTRING
1802 // -> unescape all escaped chars
1803 // The returned string is allocated out of pp->pool
1804 78 static int parse_norm(parser_t *pp, token_t tok, span_t *ret_span) {
1805 // Allocate a buffer to store the normalized string. Add one
1806 // extra-byte for terminating NUL.
1807 78 char *p = pool_alloc(pp->pool, tok.str.len + 1);
1808 78 ▶ if (!p) {
Branch: 1 branch
BranchTrueFalseTotal
if (!p) { 0 78 78
1809 0 return SETERROR(pp->ebuf, tok.lineno, "out of memory");
1810 }
1811  
1812 // Copy from token string into buffer
1813 78 memcpy(p, tok.str.ptr, tok.str.len);
1814 78 p[tok.str.len] = 0; // additional NUL term for safety
1815  
1816 78 ret_span->ptr = p;
1817 78 ret_span->len = tok.str.len;
1818  
1819 78 switch (tok.toktyp) {
1820 56 case TOK_LIT:
1821 case TOK_LITSTRING:
1822 case TOK_MLLITSTRING:
1823 // no need to handle escape chars
1824 56 return 0;
1825  
1826 22 case TOK_STRING:
1827 case TOK_MLSTRING:
1828 // need to handle escape chars
1829 22 break;
1830  
1831 0 default:
1832 0 return SETERROR(pp->ebuf, 0, "internal: arg must be a string");
1833 }
1834  
1835 // if there is no escape char, then done!
1836 22 ▶ if (!tok.u.escp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tok.u.escp) { 22 0 22
1837 22 return 0; // success
1838 }
1839  
1840 // p points to the backslash
1841 0 p += (tok.u.escp - tok.str.ptr);
1842 0 ▶ assert(p - ret_span->ptr == tok.u.escp - tok.str.ptr);
Branch: 1 branch
BranchTrueFalseTotal
assert(p - ret_span->ptr == tok.u.escp - tok.str.ptr); 0 0 0
1843 0 ▶ assert(*p == '\\');
Branch: 1 branch
BranchTrueFalseTotal
assert(*p == '\\'); 0 0 0
1844  
1845 // Normalize the escaped chars
1846 0 char *dst = p;
1847 0 ▶ while (*p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*p) { 0 0 0
1848 0 ▶ if (*p != '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '\\') { 0 0 0
1849 0 *dst++ = *p++;
1850 0 continue;
1851 }
1852 0 switch (p[1]) {
1853 0 case '"':
1854 case '\\':
1855 0 *dst++ = p[1];
1856 0 p += 2;
1857 0 continue;
1858 0 case 'b':
1859 0 *dst++ = '\b';
1860 0 p += 2;
1861 0 continue;
1862 0 case 't':
1863 0 *dst++ = '\t';
1864 0 p += 2;
1865 0 continue;
1866 0 case 'n':
1867 0 *dst++ = '\n';
1868 0 p += 2;
1869 0 continue;
1870 0 case 'f':
1871 0 *dst++ = '\f';
1872 0 p += 2;
1873 0 continue;
1874 0 case 'r':
1875 0 *dst++ = '\r';
1876 0 p += 2;
1877 0 continue;
1878 0 case 'e':
1879 0 *dst++ = '\033';
1880 0 p += 2;
1881 0 continue;
1882 0 case 'x': {
1883 char buf[3];
1884 0 memcpy(buf, p + 2, 2);
1885 0 buf[2] = 0;
1886 // There is no need to check for two hex digits here because
1887 // the scanner already checked it.
1888 0 int32_t ucs = strtol(buf, 0, 16);
1889 0 int n = ucs_to_utf8(ucs, dst);
1890 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1891 0 return SETERROR(pp->ebuf, tok.lineno, "error converting UCS %s to UTF8",
1892 buf);
1893 }
1894 0 dst += n;
1895 0 p += 2 + 2; // \xNN
1896 0 continue;
1897 }
1898 0 case 'u':
1899 0 case 'U': {
1900 char buf[9];
1901 0 ▶ int sz = (p[1] == 'u' ? 4 : 8);
Branch: 1 branch
BranchTrueFalseTotal
int sz = (p[1] == 'u' ? 4 : 8); 0 0 0
1902 0 memcpy(buf, p + 2, sz);
1903 0 buf[sz] = 0;
1904 // There is no need to check for 4 or 8 hex digits here because
1905 // the scanner already checked it.
1906 0 int32_t ucs = strtol(buf, 0, 16);
1907 0 ▶ if (0xD800 <= ucs && ucs <= 0xDFFF) {
Branch: 2 branches
BranchTrueFalseTotal
if (0xD800 <= ucs && ucs <= 0xDFFF) { 0 0 0
if (0xD800 <= ucs && ucs <= 0xDFFF) { 0 0 0
1908 // explicitly prohibit surrogates (non-scalar unicode code point)
1909 0 return SETERROR(pp->ebuf, tok.lineno, "invalid UTF8 char \\u%04x", ucs);
1910 }
1911 0 int n = ucs_to_utf8(ucs, dst);
1912 0 ▶ if (n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) { 0 0 0
1913 0 return SETERROR(pp->ebuf, tok.lineno, "error converting UCS %s to UTF8",
1914 buf);
1915 }
1916 0 dst += n;
1917 0 p += 2 + sz; // \uNNNN or \UNNNNNNNN
1918 0 continue;
1919 }
1920  
1921 0 case ' ':
1922 case '\t':
1923 case '\r':
1924 // line-ending backslash
1925 // --- allow for extra whitespace chars after backslash
1926 // --- skip until newline
1927 0 p++; // skip the escape char
1928 0 p += strspn(p, " \t\r"); // skip whitespaces
1929 0 ▶ if (*p != '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '\n') { 0 0 0
1930 0 return SETERROR(pp->ebuf, tok.lineno,
1931 "unexpected char after line-ending backslash");
1932 }
1933 // fallthru
1934 case '\n':
1935 // skip all whitespaces including newline
1936 0 p++;
1937 0 p += strspn(p, " \t\r\n");
1938 0 continue;
1939 0 default:
1940 0 return SETERROR(pp->ebuf, tok.lineno,
1941 0 "internal: unknown escape char \\%c", p[1]);
1942 }
1943 }
1944 0 *dst = 0;
1945 0 ret_span->len = dst - ret_span->ptr;
1946 0 return 0;
1947 }
1948  
1949 // ===================================================================
1950 // == SCANNER SECTION
1951 // ===================================================================
1952  
1953 // Get the next char
1954 921 static int scan_get(scanner_t *sp) {
1955 921 int ret = TOK_FIN;
1956 921 const char *p = sp->cur;
1957 921 ▶ if (p < sp->endp) {
Branch: 1 branch
BranchTrueFalseTotal
if (p < sp->endp) { 914 7 921
1958 914 ret = *p++;
1959 914 ▶ if (ret == '\r' && p < sp->endp && *p == '\n') {
Branch: 3 branches
BranchTrueFalseTotal
if (ret == '\r' && p < sp->endp && *p == '\n') { 0 914 914
if (ret == '\r' && p < sp->endp && *p == '\n') { 0 0 0
if (ret == '\r' && p < sp->endp && *p == '\n') { 0 0 0
1960 0 ret = *p++;
1961 }
1962 }
1963 921 sp->cur = p;
1964 921 ▶ if (ret == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (ret == '\n') { 70 851 921
1965 70 sp->lineno += 1;
1966 70 sp->line_start = p;
1967 }
1968 921 return ret;
1969 }
1970  
1971 // Check if the next char matches ch.
1972 410 static inline bool scan_match(scanner_t *sp, int ch) {
1973 410 const char *p = sp->cur;
1974 // exact match? done.
1975 410 ▶ if (p < sp->endp && *p == ch) {
Branch: 2 branches
BranchTrueFalseTotal
if (p < sp->endp && *p == ch) { 410 0 410
if (p < sp->endp && *p == ch) { 71 339 410
1976 71 return true;
1977 }
1978 // \n also matches \r\n
1979 339 ▶ if (ch == '\n' && p + 1 < sp->endp) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == '\n' && p + 1 < sp->endp) { 120 219 339
if (ch == '\n' && p + 1 < sp->endp) { 120 0 120
1980 120 ▶ return p[0] == '\r' && p[1] == '\n';
Branch: 2 branches
BranchTrueFalseTotal
return p[0] == '\r' && p[1] == '\n'; 0 120 120
return p[0] == '\r' && p[1] == '\n'; 0 0 0
1981 }
1982 // not a match
1983 219 return false;
1984 }
1985  
1986 // Check if the next char is in accept[].
1987 0 static bool scan_matchany(scanner_t *sp, const char *accept) {
1988 0 ▶ for (; *accept; accept++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; *accept; accept++) { 0 0 0
1989 0 ▶ if (scan_match(sp, *accept)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scan_match(sp, *accept)) { 0 0 0
1990 0 return true;
1991 }
1992 }
1993 0 return false;
1994 }
1995  
1996 // Check if the next n chars match ch.
1997 22 static inline bool scan_nmatch(scanner_t *sp, int ch, int n) {
1998 22 ▶ assert(ch != '\n'); // not handled
Branch: 1 branch
BranchTrueFalseTotal
assert(ch != '\n'); // not handled 0 22 22
1999 22 ▶ if (sp->cur + n > sp->endp) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp->cur + n > sp->endp) { 0 22 22
2000 0 return false;
2001 }
2002 22 const char *p = sp->cur;
2003 int i;
2004 44 ▶ for (i = 0; i < n && p[i] == ch; i++)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && p[i] == ch; i++) 44 0 44
for (i = 0; i < n && p[i] == ch; i++) 22 22 44
2005 ;
2006 22 return i == n;
2007 }
2008  
2009 // Initialize a token.
2010 695 static inline token_t mktoken(scanner_t *sp, toktyp_t typ) {
2011 695 token_t tok = {0};
2012 695 tok.toktyp = typ;
2013 695 tok.str.ptr = sp->cur;
2014 695 tok.lineno = sp->lineno;
2015 695 tok.colno = (int)(sp->cur - sp->line_start) + 1;
2016 695 return tok;
2017 }
2018  
2019 #define S_GET() scan_get(sp)
2020 #define S_MATCH(ch) scan_match(sp, (ch))
2021 #define S_MATCH3(ch) scan_nmatch(sp, (ch), 3)
2022 #define S_MATCH4(ch) scan_nmatch(sp, (ch), 4)
2023 #define S_MATCH6(ch) scan_nmatch(sp, (ch), 6)
2024  
2025 192 static inline bool is_valid_char(int ch) {
2026 // i.e. (0x20 <= ch && ch <= 0x7e) || (ch & 0x80);
2027 192 ▶ return isprint((unsigned char)ch) || (ch & 0x80);
Branch: 2 branches
BranchTrueFalseTotal
return isprint((unsigned char)ch) || (ch & 0x80); 22 170 192
return isprint((unsigned char)ch) || (ch & 0x80); 22 0 22
2028 }
2029  
2030 0 static inline bool is_hex_char(int ch) {
2031 0 ch = toupper((unsigned char)ch);
2032 0 ▶ return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F');
Branch: 4 branches
BranchTrueFalseTotal
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F'); 0 0 0
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F'); 0 0 0
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F'); 0 0 0
return ('0' <= ch && ch <= '9') || ('A' <= ch && ch <= 'F'); 0 0 0
2033 }
2034  
2035 // Initialize a scanner
2036 8 static void scan_init(scanner_t *sp, const char *src, int len, char *errbuf,
2037 int errbufsz) {
2038 8 memset(sp, 0, sizeof(*sp));
2039 8 sp->src = src;
2040 8 sp->endp = src + len;
2041 8 ▶ assert(*sp->endp == '\0');
Branch: 1 branch
BranchTrueFalseTotal
assert(*sp->endp == '\0'); 0 8 8
2042 8 sp->cur = src;
2043 8 sp->lineno = 1;
2044 8 sp->line_start = src;
2045 8 sp->ebuf.ptr = errbuf;
2046 8 sp->ebuf.len = errbufsz;
2047 8 }
2048  
2049 // Scan escape sequence characters after a backslash.
2050 // Return #char match on success, 0 if no match, -1 on error.
2051 0 static int scan_escape_chars(scanner_t *sp) {
2052 0 const char *p = sp->cur;
2053 0 ▶ if (p >= sp->endp) {
Branch: 1 branch
BranchTrueFalseTotal
if (p >= sp->endp) { 0 0 0
2054 0 return 0;
2055 }
2056 0 int ch = *p++;
2057 0 ▶ if (ch && strchr("btnfre\"\\", ch)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ch && strchr("btnfre\"\\", ch)) { 0 0 0
if (ch && strchr("btnfre\"\\", ch)) { 0 0 0
2058 0 return p - sp->cur;
2059 }
2060 0 ▶ int hex = (ch == 'x') ? 2 : (ch == 'u') ? 4 : (ch == 'U') ? 8 : 0;
Branch: 3 branches
BranchTrueFalseTotal
int hex = (ch == 'x') ? 2 : (ch == 'u') ? 4 : (ch == 'U') ? 8 : 0; 0 0 0
int hex = (ch == 'x') ? 2 : (ch == 'u') ? 4 : (ch == 'U') ? 8 : 0; 0 0 0
int hex = (ch == 'x') ? 2 : (ch == 'u') ? 4 : (ch == 'U') ? 8 : 0; 0 0 0
2061 0 ▶ if (hex) {
Branch: 1 branch
BranchTrueFalseTotal
if (hex) { 0 0 0
2062 0 int i = 0;
2063 0 ▶ for (; i < hex && p < sp->endp && is_hex_char(*p); i++, p++)
Branch: 3 branches
BranchTrueFalseTotal
for (; i < hex && p < sp->endp && is_hex_char(*p); i++, p++) 0 0 0
for (; i < hex && p < sp->endp && is_hex_char(*p); i++, p++) 0 0 0
for (; i < hex && p < sp->endp && is_hex_char(*p); i++, p++) 0 0 0
2064 ;
2065 0 ▶ if (i != hex) {
Branch: 1 branch
BranchTrueFalseTotal
if (i != hex) { 0 0 0
2066 0 return SETERROR(sp->ebuf, sp->lineno, "expect %d hex digits after \\%c",
2067 hex, ch);
2068 }
2069 0 return p - sp->cur;
2070 }
2071 0 return 0;
2072 }
2073  
2074 // Scan """ ... """
2075 0 static int scan_multiline_string(scanner_t *sp, token_t *tok) {
2076 0 ▶ assert(S_MATCH3('"'));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH3('"')); 0 0 0
2077 0 S_GET(), S_GET(), S_GET(); // skip opening """
2078  
2079 // According to spec: trim first newline after """
2080 0 ▶ if (S_MATCH('\n')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH('\n')) { 0 0 0
2081 0 S_GET();
2082 }
2083  
2084 0 *tok = mktoken(sp, TOK_MLSTRING);
2085 // scan until terminating """
2086 0 const char *escp = NULL;
2087 0 while (1) {
2088 0 ▶ if (S_MATCH3('"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH3('"')) { 0 0 0
2089 0 ▶ if (S_MATCH4('"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH4('"')) { 0 0 0
2090 // special case... """abcd """" -> (abcd ")
2091 // but sequences of 3 or more double quotes are not allowed
2092 0 ▶ if (S_MATCH6('"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH6('"')) { 0 0 0
2093 0 return SETERROR(sp->ebuf, sp->lineno,
2094 "detected sequences of 3 or more double quotes");
2095 } else {
2096 ; // no problem
2097 }
2098 } else {
2099 0 break; // found terminating """
2100 }
2101 }
2102 0 int ch = S_GET();
2103 0 ▶ if (ch == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) { 0 0 0
2104 0 return SETERROR(sp->ebuf, sp->lineno, "unterminated \"\"\"");
2105 }
2106 // If non-escaped char ...
2107 0 ▶ if (ch != '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != '\\') { 0 0 0
2108 0 ▶ if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
2109 0 return SETERROR(sp->ebuf, sp->lineno, "invalid char in string");
2110 }
2111 0 continue;
2112 }
2113 // ch is backslash
2114 0 ▶ if (!escp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!escp) { 0 0 0
2115 0 escp = sp->cur - 1; /* mark the first esc position */
2116 0 ▶ assert(*escp == '\\');
Branch: 1 branch
BranchTrueFalseTotal
assert(*escp == '\\'); 0 0 0
2117 }
2118  
2119 // handle escape char
2120 0 int cnt = scan_escape_chars(sp);
2121 0 ▶ if (cnt < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt < 0) { 0 0 0
2122 0 return -1;
2123 }
2124 0 ▶ if (cnt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt > 0) { 0 0 0
2125 0 sp->cur += cnt; // skip the escape sequence
2126 0 continue;
2127 }
2128 0 ▶ assert(cnt == 0);
Branch: 1 branch
BranchTrueFalseTotal
assert(cnt == 0); 0 0 0
2129  
2130 // handle line-ending backslash
2131 0 ch = S_GET();
2132 0 ▶ if (ch == ' ' || ch == '\t') {
Branch: 2 branches
BranchTrueFalseTotal
if (ch == ' ' || ch == '\t') { 0 0 0
if (ch == ' ' || ch == '\t') { 0 0 0
2133 // Although the spec does not allow for whitespace following a
2134 // line-ending backslash, some standard tests expect it.
2135 // Skip whitespace till EOL.
2136 0 ▶ while (ch != TOK_FIN && ch && strchr(" \t", ch)) {
Branch: 3 branches
BranchTrueFalseTotal
while (ch != TOK_FIN && ch && strchr(" \t", ch)) { 0 0 0
while (ch != TOK_FIN && ch && strchr(" \t", ch)) { 0 0 0
while (ch != TOK_FIN && ch && strchr(" \t", ch)) { 0 0 0
2137 0 ch = S_GET();
2138 }
2139 0 ▶ if (ch != '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != '\n') { 0 0 0
2140 // Got a backslash followed by whitespace, followed by some char
2141 // before newline
2142 0 return SETERROR(sp->ebuf, sp->lineno, "bad escape char in string");
2143 }
2144 // fallthru
2145 }
2146 0 ▶ if (ch == '\n') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == '\n') { 0 0 0
2147 // got a line-ending backslash
2148 // - skip all whitespaces
2149 0 ▶ while (scan_matchany(sp, " \t\n")) {
Branch: 1 branch
BranchTrueFalseTotal
while (scan_matchany(sp, " \t\n")) { 0 0 0
2150 0 S_GET();
2151 }
2152 0 continue;
2153 }
2154 0 return SETERROR(sp->ebuf, sp->lineno, "bad escape char in string");
2155 }
2156 0 tok->str.len = sp->cur - tok->str.ptr;
2157 0 tok->u.escp = escp;
2158  
2159 0 ▶ assert(S_MATCH3('"'));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH3('"')); 0 0 0
2160 0 S_GET(), S_GET(), S_GET();
2161 0 return 0;
2162 }
2163  
2164 // Scan " ... "
2165 22 static int scan_string(scanner_t *sp, token_t *tok) {
2166 22 ▶ assert(S_MATCH('"'));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH('"')); 0 22 22
2167 22 ▶ if (S_MATCH3('"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH3('"')) { 0 22 22
2168 0 return scan_multiline_string(sp, tok);
2169 }
2170 22 S_GET(); // skip opening "
2171  
2172 // scan until closing "
2173 22 *tok = mktoken(sp, TOK_STRING);
2174 22 const char *escp = NULL;
2175 214 ▶ while (!S_MATCH('"')) {
Branch: 1 branch
BranchTrueFalseTotal
while (!S_MATCH('"')) { 192 22 214
2176 192 int ch = S_GET();
2177 192 ▶ if (ch == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) { 0 192 192
2178 0 return SETERROR(sp->ebuf, sp->lineno, "unterminated string");
2179 }
2180 // If non-escaped char ...
2181 192 ▶ if (ch != '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if (ch != '\\') { 192 0 192
2182 192 ▶ if (!(is_valid_char(ch) || ch == ' ' || ch == '\t')) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(is_valid_char(ch) || ch == ' ' || ch == '\t')) { 0 192 192
if (!(is_valid_char(ch) || ch == ' ' || ch == '\t')) { 0 0 0
if (!(is_valid_char(ch) || ch == ' ' || ch == '\t')) { 0 0 0
2183 0 return SETERROR(sp->ebuf, sp->lineno, "invalid char in string");
2184 }
2185 192 continue;
2186 }
2187 // ch is backslash
2188 0 ▶ if (!escp) {
Branch: 1 branch
BranchTrueFalseTotal
if (!escp) { 0 0 0
2189 0 escp = sp->cur - 1;
2190 0 ▶ assert(*escp == '\\');
Branch: 1 branch
BranchTrueFalseTotal
assert(*escp == '\\'); 0 0 0
2191 }
2192  
2193 // handle escape char
2194 0 int cnt = scan_escape_chars(sp);
2195 0 ▶ if (cnt < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt < 0) { 0 0 0
2196 0 return -1;
2197 }
2198 0 ▶ if (cnt > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cnt > 0) { 0 0 0
2199 0 sp->cur += cnt;
2200 0 continue;
2201 }
2202 0 return SETERROR(sp->ebuf, sp->lineno, "bad escape char in string");
2203 }
2204 22 tok->str.len = sp->cur - tok->str.ptr;
2205 22 tok->u.escp = escp;
2206  
2207 22 ▶ assert(S_MATCH('"'));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH('"')); 0 22 22
2208 22 S_GET(); // skip the terminating "
2209 22 return 0;
2210 }
2211  
2212 // Scan ''' ... '''
2213 0 static int scan_multiline_litstring(scanner_t *sp, token_t *tok) {
2214 0 ▶ assert(S_MATCH3('\''));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH3('\'')); 0 0 0
2215 0 S_GET(), S_GET(), S_GET(); // skip opening '''
2216  
2217 // According to spec: trim first newline after '''
2218 0 ▶ if (S_MATCH('\n')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH('\n')) { 0 0 0
2219 0 S_GET();
2220 }
2221  
2222 // scan until terminating '''
2223 0 *tok = mktoken(sp, TOK_MLLITSTRING);
2224 0 while (1) {
2225 0 ▶ if (S_MATCH3('\'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH3('\'')) { 0 0 0
2226 0 ▶ if (S_MATCH4('\'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH4('\'')) { 0 0 0
2227 // special case... '''abcd '''' -> (abcd ')
2228 // but sequences of 3 or more single quotes are not allowed
2229 0 ▶ if (S_MATCH6('\'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH6('\'')) { 0 0 0
2230 0 return SETERROR(sp->ebuf, sp->lineno,
2231 "sequences of 3 or more single quotes");
2232 } else {
2233 ; // no problem
2234 }
2235 } else {
2236 0 break; // found terminating '''
2237 }
2238 }
2239 0 int ch = S_GET();
2240 0 ▶ if (ch == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) { 0 0 0
2241 0 return SETERROR(sp->ebuf, sp->lineno,
2242 "unterminated multiline lit string");
2243 }
2244 0 ▶ if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
if (!(is_valid_char(ch) || (ch && strchr(" \t\n", ch)))) { 0 0 0
2245 0 return SETERROR(sp->ebuf, sp->lineno, "invalid char in string");
2246 }
2247 }
2248 0 tok->str.len = sp->cur - tok->str.ptr;
2249  
2250 0 ▶ assert(S_MATCH3('\''));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH3('\'')); 0 0 0
2251 0 S_GET(), S_GET(), S_GET();
2252 0 return 0;
2253 }
2254  
2255 // Scan ' ... '
2256 0 static int scan_litstring(scanner_t *sp, token_t *tok) {
2257 0 ▶ assert(S_MATCH('\''));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH('\'')); 0 0 0
2258 0 ▶ if (S_MATCH3('\'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (S_MATCH3('\'')) { 0 0 0
2259 0 return scan_multiline_litstring(sp, tok);
2260 }
2261 0 S_GET(); // skip opening '
2262  
2263 // scan until closing '
2264 0 *tok = mktoken(sp, TOK_LITSTRING);
2265 0 ▶ while (!S_MATCH('\'')) {
Branch: 1 branch
BranchTrueFalseTotal
while (!S_MATCH('\'')) { 0 0 0
2266 0 int ch = S_GET();
2267 0 ▶ if (ch == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) { 0 0 0
2268 0 return SETERROR(sp->ebuf, sp->lineno, "unterminated string");
2269 }
2270 0 ▶ if (!(is_valid_char(ch) || ch == '\t')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(is_valid_char(ch) || ch == '\t')) { 0 0 0
if (!(is_valid_char(ch) || ch == '\t')) { 0 0 0
2271 0 return SETERROR(sp->ebuf, sp->lineno, "invalid char in string");
2272 }
2273 }
2274 0 tok->str.len = sp->cur - tok->str.ptr;
2275 0 ▶ assert(S_MATCH('\''));
Branch: 1 branch
BranchTrueFalseTotal
assert(S_MATCH('\'')); 0 0 0
2276 0 S_GET();
2277 0 return 0;
2278 }
2279  
2280 10 static bool is_valid_date(int year, int month, int day) {
2281 10 ▶ if (!(1 <= year)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(1 <= year)) { 0 10 10
2282 0 return false;
2283 }
2284 10 ▶ if (!(1 <= month && month <= 12)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(1 <= month && month <= 12)) { 10 0 10
if (!(1 <= month && month <= 12)) { 0 10 10
2285 0 return false;
2286 }
2287 10 ▶ int is_leap_year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Branch: 3 branches
BranchTrueFalseTotal
int is_leap_year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 10 10
int is_leap_year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 0 0
int is_leap_year = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 10 10
2288 10 int days_in_month[] = {
2289 10 31, 28 + is_leap_year, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
2290 10 ▶ return (1 <= day && day <= days_in_month[month - 1]);
Branch: 2 branches
BranchTrueFalseTotal
return (1 <= day && day <= days_in_month[month - 1]); 10 0 10
return (1 <= day && day <= days_in_month[month - 1]); 10 0 10
2291 }
2292  
2293 10 static bool is_valid_time(int hour, int minute, int sec, int usec) {
2294 10 ▶ if (!(0 <= hour && hour <= 23)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= hour && hour <= 23)) { 10 0 10
if (!(0 <= hour && hour <= 23)) { 0 10 10
2295 0 return false;
2296 }
2297 10 ▶ if (!(0 <= minute && minute <= 59)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= minute && minute <= 59)) { 10 0 10
if (!(0 <= minute && minute <= 59)) { 0 10 10
2298 0 return false;
2299 }
2300 10 ▶ if (!(0 <= sec && sec <= 59)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= sec && sec <= 59)) { 10 0 10
if (!(0 <= sec && sec <= 59)) { 0 10 10
2301 0 return false;
2302 }
2303 10 ▶ if (!(0 <= usec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(0 <= usec)) { 0 10 10
2304 0 return false;
2305 }
2306 10 return true;
2307 }
2308  
2309 9 static bool is_valid_timezone(int minute) {
2310 9 minute = (minute < 0 ? -minute : minute);
2311 9 int hour = minute / 60;
2312 9 minute = minute % 60;
2313 9 ▶ if (!(0 <= hour && hour <= 23)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= hour && hour <= 23)) { 9 0 9
if (!(0 <= hour && hour <= 23)) { 0 9 9
2314 0 return false;
2315 }
2316 9 ▶ if (!(0 <= minute && minute < 60)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= minute && minute < 60)) { 9 0 9
if (!(0 <= minute && minute < 60)) { 0 9 9
2317 0 return false;
2318 }
2319 9 return true;
2320 }
2321  
2322 // Read an int (without signs) from the string p.
2323 78 static int read_int(const char *p, int *ret) {
2324 78 const char *pp = p;
2325 78 int64_t val = 0;
2326 254 ▶ for (; isdigit((unsigned char)*p); p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; isdigit((unsigned char)*p); p++) { 176 78 254
2327 176 val = val * 10 + (*p - '0');
2328 176 ▶ if (val > INT_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (val > INT_MAX) { 0 176 176
2329 0 return 0; // overflowed
2330 }
2331 }
2332 78 *ret = (int)val;
2333 78 return p - pp;
2334 }
2335  
2336 // Read a date as YYYY-MM-DD from p[]. Return #bytes consumed.
2337 10 static int read_date(const char *p, int *year, int *month, int *day) {
2338 10 const char *pp = p;
2339 int n;
2340 10 n = read_int(p, year);
2341 10 ▶ if (n != 4 || p[4] != '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (n != 4 || p[4] != '-') { 10 0 10
if (n != 4 || p[4] != '-') { 0 10 10
2342 0 return 0;
2343 }
2344 10 n = read_int(p += n + 1, month);
2345 10 ▶ if (n != 2 || p[2] != '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (n != 2 || p[2] != '-') { 10 0 10
if (n != 2 || p[2] != '-') { 0 10 10
2346 0 return 0;
2347 }
2348 10 n = read_int(p += n + 1, day);
2349 10 ▶ if (n != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n != 2) { 0 10 10
2350 0 return 0;
2351 }
2352 10 p += 2;
2353 10 ▶ assert(p - pp == 10);
Branch: 1 branch
BranchTrueFalseTotal
assert(p - pp == 10); 0 10 10
2354 10 return p - pp;
2355 }
2356  
2357 // Read a time as HH:MM:SS.subsec from p[]. Return #bytes consumed.
2358 10 static int read_time(const char *p, int *hour, int *minute, int *second,
2359 int *usec) {
2360 10 const char *pp = p;
2361 int n;
2362 10 *hour = *minute = *second = *usec = 0;
2363 // scan hours
2364 10 n = read_int(p, hour);
2365 10 ▶ if (n != 2 || p[2] != ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (n != 2 || p[2] != ':') { 10 0 10
if (n != 2 || p[2] != ':') { 0 10 10
2366 0 return 0;
2367 }
2368 10 p += 3;
2369  
2370 // scan minutes
2371 10 n = read_int(p, minute);
2372 10 ▶ if (n != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n != 2) { 0 10 10
2373 0 return 0;
2374 }
2375 10 ▶ if (p[2] != ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (p[2] != ':') { 0 10 10
2376 // seconds are optional in v1.1
2377 0 p += 2;
2378 0 return p - pp;
2379 }
2380 10 p += 3;
2381  
2382 // scan seconds
2383 10 n = read_int(p, second);
2384 10 ▶ if (n != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n != 2) { 0 10 10
2385 0 return 0;
2386 }
2387 10 p += 2;
2388  
2389 10 ▶ if (*p != '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != '.') { 9 1 10
2390 9 return p - pp;
2391 }
2392 1 p++; // skip the period
2393 1 ▶ if (!isdigit((unsigned char)*p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!isdigit((unsigned char)*p)) { 0 1 1
2394 // trailing period
2395 0 return 0;
2396 }
2397 1 int micro_factor = 100000;
2398 4 ▶ while (isdigit((unsigned char)*p) && micro_factor) {
Branch: 2 branches
BranchTrueFalseTotal
while (isdigit((unsigned char)*p) && micro_factor) { 3 1 4
while (isdigit((unsigned char)*p) && micro_factor) { 3 0 3
2399 3 *usec += (*p - '0') * micro_factor;
2400 3 micro_factor /= 10;
2401 3 p++;
2402 }
2403 1 ▶ while (isdigit((unsigned char)*p))
Branch: 1 branch
BranchTrueFalseTotal
while (isdigit((unsigned char)*p)) 0 1 1
2404 0 p++; // consume extra sub-microsecond digits
2405 1 return p - pp;
2406 }
2407  
2408 // Reads a timezone from p[]. Return #bytes consumed.
2409 // tzhours and tzminutes restricted to 2-char integers only.
2410 9 static int read_tzone(const char *p, char *tzsign, int *tzhour, int *tzminute) {
2411 9 const char *pp = p;
2412  
2413 // Default values
2414 9 *tzhour = *tzminute = 0;
2415 9 *tzsign = '+';
2416  
2417 // Look for Zulu
2418 9 ▶ if (*p == 'Z' || *p == 'z') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == 'Z' || *p == 'z') { 9 0 9
if (*p == 'Z' || *p == 'z') { 0 9 9
2419 0 return 1; // done! tz is +00:00.
2420 }
2421  
2422 // Look for +/-
2423 9 *tzsign = *p++;
2424 9 ▶ if (!(*tzsign == '+' || *tzsign == '-')) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(*tzsign == '+' || *tzsign == '-')) { 9 0 9
if (!(*tzsign == '+' || *tzsign == '-')) { 0 9 9
2425 0 return 0;
2426 }
2427  
2428 // Look for HH:MM
2429 int n;
2430 9 n = read_int(p, tzhour);
2431 9 ▶ if (n != 2 || p[2] != ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (n != 2 || p[2] != ':') { 9 0 9
if (n != 2 || p[2] != ':') { 0 9 9
2432 0 return 0;
2433 }
2434 9 n = read_int(p += 3, tzminute);
2435 9 ▶ if (n != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (n != 2) { 0 9 9
2436 0 return 0;
2437 }
2438 9 p += 2;
2439 9 return p - pp;
2440 }
2441  
2442 // Scan hh:mm:ss.xxxxx
2443 1 static int scan_time(scanner_t *sp, token_t *tok) {
2444 1 int lineno = sp->lineno;
2445 char buffer[20];
2446 1 scan_copystr(sp, buffer, sizeof(buffer));
2447  
2448 1 char *p = buffer;
2449 int hour, minute, sec, usec;
2450 1 int len = read_time(p, &hour, &minute, &sec, &usec);
2451 1 ▶ if (len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == 0) { 0 1 1
2452 0 return SETERROR(sp->ebuf, lineno, "invalid time");
2453 }
2454 1 ▶ if (!is_valid_time(hour, minute, sec, usec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_valid_time(hour, minute, sec, usec)) { 0 1 1
2455 0 return SETERROR(sp->ebuf, lineno, "invalid time");
2456 }
2457  
2458 1 *tok = mktoken(sp, TOK_TIME);
2459 1 tok->str.len = len;
2460 1 sp->cur += len;
2461 1 tok->u.tsval.year = -1;
2462 1 tok->u.tsval.month = -1;
2463 1 tok->u.tsval.day = -1;
2464 1 tok->u.tsval.hour = hour;
2465 1 tok->u.tsval.minute = minute;
2466 1 tok->u.tsval.sec = sec;
2467 1 tok->u.tsval.usec = usec;
2468 1 tok->u.tsval.tz = -1;
2469 1 return 0;
2470 }
2471  
2472 // Scan a time, a date, a datetime, or a datatimetz
2473 10 static int scan_timestamp(scanner_t *sp, token_t *tok) {
2474 int year, month, day, hour, minute, sec, usec, tz;
2475 10 year = month = day = hour = minute = sec = usec = tz = -1;
2476  
2477 int n;
2478 // make a copy of sp->cur into buffer to ensure NUL terminated string
2479 char buffer[80];
2480 10 scan_copystr(sp, buffer, sizeof(buffer));
2481  
2482 10 toktyp_t toktyp = TOK_FIN;
2483 10 int lineno = sp->lineno;
2484  
2485 // See if this a TIME only
2486 10 const char *p = buffer;
2487 10 ▶ if (isdigit((unsigned char)p[0]) && isdigit((unsigned char)p[1]) &&
Branch: 2 branches
BranchTrueFalseTotal
if (isdigit((unsigned char)p[0]) && isdigit((unsigned char)p[1]) && 10 0 10
if (isdigit((unsigned char)p[0]) && isdigit((unsigned char)p[1]) && 10 0 10
2488 10 ▶ p[2] == ':') {
Branch: 1 branch
BranchTrueFalseTotal
p[2] == ':') { 0 10 10
2489 0 n = read_time(buffer, &hour, &minute, &sec, &usec);
2490 0 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 0 0 0
2491 0 return SETERROR(sp->ebuf, lineno, "invalid time");
2492 }
2493 0 toktyp = TOK_TIME;
2494 0 p += n;
2495 0 goto done;
2496 }
2497  
2498 // Try reading a DATE
2499 10 n = read_date(p, &year, &month, &day);
2500 10 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 0 10 10
2501 0 return SETERROR(sp->ebuf, lineno, "invalid date");
2502 }
2503 10 toktyp = TOK_DATE;
2504 10 p += n;
2505  
2506 // Check if there is no time component in addition
2507 10 ▶ if (!((p[0] == 'T' || p[0] == ' ' || p[0] == 't') &&
Branch: 3 branches
BranchTrueFalseTotal
if (!((p[0] == 'T' || p[0] == ' ' || p[0] == 't') && 1 9 10
if (!((p[0] == 'T' || p[0] == ' ' || p[0] == 't') && 0 1 1
if (!((p[0] == 'T' || p[0] == ' ' || p[0] == 't') && 0 0 0
2508 10 ▶ isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) &&
Branch: 2 branches
BranchTrueFalseTotal
isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) && 9 1 10
isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) && 9 0 9
2509 9 ▶ p[3] == ':')) {
Branch: 1 branch
BranchTrueFalseTotal
p[3] == ':')) { 0 9 9
2510 1 goto done; // no TIME component. we are done.
2511 }
2512  
2513 // Read the TIME
2514 9 n = read_time(p += 1, &hour, &minute, &sec, &usec);
2515 9 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 0 9 9
2516 0 return SETERROR(sp->ebuf, lineno, "invalid timestamp");
2517 }
2518 9 toktyp = TOK_DATETIME;
2519 9 p += n;
2520  
2521 // Read the (optional) timezone
2522 char tzsign;
2523 int tzhour, tzminute;
2524 9 n = read_tzone(p, &tzsign, &tzhour, &tzminute);
2525 9 ▶ if (n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) { 0 9 9
2526 0 goto done; // datetime only
2527 }
2528 9 toktyp = TOK_DATETIMETZ;
2529 9 p += n;
2530  
2531 // Check tzminute range. This must be done here instead of is_valid_timezone()
2532 // because we combine tzhour and tzminute into tz (by minutes only).
2533 9 ▶ if (!(0 <= tzminute && tzminute < 60)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(0 <= tzminute && tzminute < 60)) { 9 0 9
if (!(0 <= tzminute && tzminute < 60)) { 0 9 9
2534 0 return SETERROR(sp->ebuf, lineno, "invalid timezone");
2535 }
2536 9 ▶ tz = (tzhour * 60 + tzminute) * (tzsign == '-' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
tz = (tzhour * 60 + tzminute) * (tzsign == '-' ? -1 : 1); 9 0 9
2537 9 goto done; // datetimetz
2538  
2539 10 done:
2540 10 *tok = mktoken(sp, toktyp);
2541 10 n = p - buffer;
2542 10 tok->str.len = n;
2543 10 sp->cur += n;
2544  
2545 10 tok->u.tsval.year = year;
2546 10 tok->u.tsval.month = month;
2547 10 tok->u.tsval.day = day;
2548 10 tok->u.tsval.hour = hour;
2549 10 tok->u.tsval.minute = minute;
2550 10 tok->u.tsval.sec = sec;
2551 10 tok->u.tsval.usec = usec;
2552 10 tok->u.tsval.tz = tz;
2553  
2554 // Do some error checks based on type
2555 10 switch (tok->toktyp) {
2556 0 case TOK_TIME:
2557 0 ▶ if (!is_valid_time(hour, minute, sec, usec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_valid_time(hour, minute, sec, usec)) { 0 0 0
2558 0 return SETERROR(sp->ebuf, lineno, "invalid time");
2559 }
2560 0 break;
2561 1 case TOK_DATE:
2562 1 ▶ if (!is_valid_date(year, month, day)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_valid_date(year, month, day)) { 0 1 1
2563 0 return SETERROR(sp->ebuf, lineno, "invalid date");
2564 }
2565 1 break;
2566 9 case TOK_DATETIME:
2567 case TOK_DATETIMETZ:
2568 9 ▶ if (!is_valid_date(year, month, day)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_valid_date(year, month, day)) { 0 9 9
2569 0 return SETERROR(sp->ebuf, lineno, "invalid date");
2570 }
2571 9 ▶ if (!is_valid_time(hour, minute, sec, usec)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!is_valid_time(hour, minute, sec, usec)) { 0 9 9
2572 0 return SETERROR(sp->ebuf, lineno, "invalid time");
2573 }
2574 9 ▶ if (tok->toktyp == TOK_DATETIMETZ && !is_valid_timezone(tz)) {
Branch: 2 branches
BranchTrueFalseTotal
if (tok->toktyp == TOK_DATETIMETZ && !is_valid_timezone(tz)) { 9 0 9
if (tok->toktyp == TOK_DATETIMETZ && !is_valid_timezone(tz)) { 0 9 9
2575 0 return SETERROR(sp->ebuf, lineno, "invalid timezone");
2576 }
2577 9 break;
2578 0 default:
2579 0 assert(0);
2580 return SETERROR(sp->ebuf, lineno, "internal error");
2581 }
2582  
2583 10 return 0;
2584 }
2585  
2586 // Given a toml number (int and float) in buffer[]:
2587 // 1. squeeze out '_'
2588 // 2. check for syntax restrictions
2589 33 static int process_numstr(char *buffer, int base, const char **reason) {
2590 // squeeze out _
2591 33 char *q = strchr(buffer, '_');
2592 33 ▶ if (q) {
Branch: 1 branch
BranchTrueFalseTotal
if (q) { 0 33 33
2593 0 ▶ for (int i = q - buffer; buffer[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = q - buffer; buffer[i]; i++) { 0 0 0
2594 0 ▶ if (buffer[i] != '_') {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer[i] != '_') { 0 0 0
2595 0 *q++ = buffer[i];
2596 0 continue;
2597 }
2598 0 ▶ int left = (i == 0) ? 0 : (unsigned char)buffer[i - 1];
Branch: 1 branch
BranchTrueFalseTotal
int left = (i == 0) ? 0 : (unsigned char)buffer[i - 1]; 0 0 0
2599 0 int right = (unsigned char)buffer[i + 1];
2600 0 ▶ if (!isdigit(left) && !(base == 16 && is_hex_char(left))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!isdigit(left) && !(base == 16 && is_hex_char(left))) { 0 0 0
if (!isdigit(left) && !(base == 16 && is_hex_char(left))) { 0 0 0
if (!isdigit(left) && !(base == 16 && is_hex_char(left))) { 0 0 0
2601 0 *reason = "underscore only allowed between digits";
2602 0 return -1;
2603 }
2604 0 ▶ if (!isdigit(right) && !(base == 16 && is_hex_char(right))) {
Branch: 3 branches
BranchTrueFalseTotal
if (!isdigit(right) && !(base == 16 && is_hex_char(right))) { 0 0 0
if (!isdigit(right) && !(base == 16 && is_hex_char(right))) { 0 0 0
if (!isdigit(right) && !(base == 16 && is_hex_char(right))) { 0 0 0
2605 0 *reason = "underscore only allowed between digits";
2606 0 return -1;
2607 }
2608 }
2609 0 *q = 0;
2610 }
2611  
2612 // decimal points must be surrounded by digits. Also, convert to lowercase.
2613 159 ▶ for (int i = 0; buffer[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int i = 0; buffer[i]; i++) { 126 33 159
2614 126 ▶ if (buffer[i] == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (buffer[i] == '.') { 20 106 126
2615 20 ▶ if (i == 0 || !isdigit((unsigned char)buffer[i - 1]) ||
Branch: 2 branches
BranchTrueFalseTotal
if (i == 0 || !isdigit((unsigned char)buffer[i - 1]) || 20 0 20
if (i == 0 || !isdigit((unsigned char)buffer[i - 1]) || 20 0 20
2616 20 ▶ !isdigit((unsigned char)buffer[i + 1])) {
Branch: 1 branch
BranchTrueFalseTotal
!isdigit((unsigned char)buffer[i + 1])) { 0 20 20
2617 0 *reason = "decimal point must be surrounded by digits";
2618 0 return -1;
2619 }
2620 106 ▶ } else if ('A' <= buffer[i] && buffer[i] <= 'Z') {
Branch: 2 branches
BranchTrueFalseTotal
} else if ('A' <= buffer[i] && buffer[i] <= 'Z') { 0 106 106
} else if ('A' <= buffer[i] && buffer[i] <= 'Z') { 0 0 0
2621 0 buffer[i] = tolower((unsigned char)buffer[i]);
2622 }
2623 }
2624  
2625 33 ▶ if (base == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if (base == 10) { 33 0 33
2626 // check for leading 0: '+01' is an error!
2627 33 q = buffer;
2628 33 ▶ q += (*q == '+' || *q == '-') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
q += (*q == '+' || *q == '-') ? 1 : 0; 33 0 33
q += (*q == '+' || *q == '-') ? 1 : 0; 0 33 33
2629 33 ▶ if (q[0] == '0' && isdigit((unsigned char)q[1])) {
Branch: 2 branches
BranchTrueFalseTotal
if (q[0] == '0' && isdigit((unsigned char)q[1])) { 2 31 33
if (q[0] == '0' && isdigit((unsigned char)q[1])) { 2 0 2
2630 2 *reason = "leading 0 in numbers";
2631 2 return -1;
2632 }
2633 }
2634  
2635 31 return 0;
2636 }
2637  
2638 10 static int scan_float(scanner_t *sp, token_t *tok) {
2639 char buffer[50]; // need to accommodate "9_007_199_254_740_991.0"
2640 10 scan_copystr(sp, buffer, sizeof(buffer));
2641  
2642 10 int lineno = sp->lineno;
2643 10 char *p = buffer;
2644 10 ▶ p += (*p == '+' || *p == '-') ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
p += (*p == '+' || *p == '-') ? 1 : 0; 10 0 10
p += (*p == '+' || *p == '-') ? 1 : 0; 0 10 10
2645 10 ▶ if (0 == memcmp(p, "nan", 3) || (0 == memcmp(p, "inf", 3))) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == memcmp(p, "nan", 3) || (0 == memcmp(p, "inf", 3))) { 10 0 10
if (0 == memcmp(p, "nan", 3) || (0 == memcmp(p, "inf", 3))) { 0 10 10
2646 0 p += 3;
2647 } else {
2648 10 p += strspn(p, "_0123456789eE.+-");
2649 }
2650 10 int len = p - buffer;
2651 10 buffer[len] = 0;
2652  
2653 const char *reason;
2654 10 ▶ if (process_numstr(buffer, 10, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (process_numstr(buffer, 10, &reason)) { 0 10 10
2655 0 return SETERROR(sp->ebuf, lineno, "%s", reason);
2656 }
2657  
2658 10 errno = 0;
2659 char *q;
2660 10 double fp64 = strtod(buffer, &q);
2661 10 ▶ if (errno || *q || q == buffer) {
Branch: 3 branches
BranchTrueFalseTotal
if (errno || *q || q == buffer) { 10 0 10
if (errno || *q || q == buffer) { 10 0 10
if (errno || *q || q == buffer) { 0 10 10
2662 0 return SETERROR(sp->ebuf, lineno, "error parsing float");
2663 }
2664  
2665 10 *tok = mktoken(sp, TOK_FLOAT);
2666 10 tok->u.fp64 = fp64;
2667 10 tok->str.len = len;
2668 10 sp->cur += len;
2669 10 return 0;
2670 }
2671  
2672 23 static int scan_number(scanner_t *sp, token_t *tok) {
2673 const char *reason;
2674 char buffer[50]; // need to accommodate "9_007_199_254_740_991.0"
2675 23 scan_copystr(sp, buffer, sizeof(buffer));
2676  
2677 23 char *p = buffer;
2678 23 int lineno = sp->lineno;
2679 // process %0x, %0o or %0b integers
2680 23 ▶ if (p[0] == '0') {
Branch: 1 branch
BranchTrueFalseTotal
if (p[0] == '0') { 2 21 23
2681 2 const char *span = 0;
2682 2 int base = 0;
2683 2 switch (p[1]) {
2684 0 case 'x':
2685 0 base = 16;
2686 0 span = "_0123456789abcdefABCDEF";
2687 0 break;
2688 0 case 'o':
2689 0 base = 8;
2690 0 span = "_01234567";
2691 0 break;
2692 0 case 'b':
2693 0 base = 2;
2694 0 span = "_01";
2695 0 break;
2696 }
2697 2 ▶ if (base) {
Branch: 1 branch
BranchTrueFalseTotal
if (base) { 0 2 2
2698 0 p += 2;
2699 0 p += strspn(p, span);
2700 0 int len = p - buffer;
2701 0 buffer[len] = 0;
2702  
2703 0 ▶ if (process_numstr(buffer + 2, base, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (process_numstr(buffer + 2, base, &reason)) { 0 0 0
2704 0 return SETERROR(sp->ebuf, lineno, "%s", reason);
2705 }
2706  
2707 // use strtoll to obtain the value
2708 0 *tok = mktoken(sp, TOK_INTEGER);
2709 char *q;
2710 0 errno = 0;
2711 0 tok->u.int64 = strtoll(buffer + 2, &q, base);
2712 0 ▶ if (errno || *q || q == buffer + 2) {
Branch: 3 branches
BranchTrueFalseTotal
if (errno || *q || q == buffer + 2) { 0 0 0
if (errno || *q || q == buffer + 2) { 0 0 0
if (errno || *q || q == buffer + 2) { 0 0 0
2713 0 return SETERROR(sp->ebuf, lineno, "error parsing integer");
2714 }
2715 0 tok->str.len = len;
2716 0 sp->cur += len;
2717 0 return 0;
2718 }
2719 }
2720  
2721 // handle inf/nan
2722 23 ▶ if (*p == '+' || *p == '-') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '+' || *p == '-') { 23 0 23
if (*p == '+' || *p == '-') { 0 23 23
2723 0 p++;
2724 }
2725 23 ▶ if (*p == 'i' || *p == 'n') {
Branch: 2 branches
BranchTrueFalseTotal
if (*p == 'i' || *p == 'n') { 23 0 23
if (*p == 'i' || *p == 'n') { 0 23 23
2726 0 return scan_float(sp, tok);
2727 }
2728  
2729 // regular int or float
2730 23 p = buffer;
2731 23 p += strspn(p, "0123456789_+-.eE");
2732 23 int len = p - buffer;
2733 23 buffer[len] = 0;
2734  
2735 23 ▶ if (process_numstr(buffer, 10, &reason)) {
Branch: 1 branch
BranchTrueFalseTotal
if (process_numstr(buffer, 10, &reason)) { 2 21 23
2736 2 return SETERROR(sp->ebuf, lineno, "%s", reason);
2737 }
2738  
2739 21 *tok = mktoken(sp, TOK_INTEGER);
2740 char *q;
2741 21 errno = 0;
2742 21 tok->u.int64 = strtoll(buffer, &q, 10);
2743 21 ▶ if (errno || *q || q == buffer) {
Branch: 3 branches
BranchTrueFalseTotal
if (errno || *q || q == buffer) { 21 0 21
if (errno || *q || q == buffer) { 11 10 21
if (errno || *q || q == buffer) { 0 11 11
2744 10 ▶ if (*q && strchr(".eE", *q)) {
Branch: 2 branches
BranchTrueFalseTotal
if (*q && strchr(".eE", *q)) { 10 0 10
if (*q && strchr(".eE", *q)) { 10 0 10
2745 10 return scan_float(sp, tok); // try to fit a float
2746 }
2747 0 return SETERROR(sp->ebuf, lineno, "error parsing integer");
2748 }
2749  
2750 11 tok->str.len = len;
2751 11 sp->cur += len;
2752 11 return 0;
2753 }
2754  
2755 10 static int scan_bool(scanner_t *sp, token_t *tok) {
2756 char buffer[10];
2757 10 scan_copystr(sp, buffer, sizeof(buffer));
2758  
2759 10 int lineno = sp->lineno;
2760 10 bool val = false;
2761 10 const char *p = buffer;
2762 10 ▶ if (0 == strncmp(p, "true", 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == strncmp(p, "true", 4)) { 3 7 10
2763 3 val = true;
2764 3 p += 4;
2765 7 ▶ } else if (0 == strncmp(p, "false", 5)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (0 == strncmp(p, "false", 5)) { 7 0 7
2766 7 val = false;
2767 7 p += 5;
2768 } else {
2769 0 return SETERROR(sp->ebuf, lineno, "invalid boolean value");
2770 }
2771 10 ▶ if (*p && !strchr("# \r\n\t,}]", *p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (*p && !strchr("# \r\n\t,}]", *p)) { 10 0 10
if (*p && !strchr("# \r\n\t,}]", *p)) { 0 10 10
2772 0 return SETERROR(sp->ebuf, lineno, "invalid boolean value");
2773 }
2774  
2775 10 int len = p - buffer;
2776 10 *tok = mktoken(sp, TOK_BOOL);
2777 10 tok->u.b1 = val;
2778 10 tok->str.len = len;
2779 10 sp->cur += len;
2780 10 return 0;
2781 }
2782  
2783 // Check if the next token may be TIME
2784 44 static inline bool test_time(const char *p, const char *endp) {
2785 44 ▶ return &p[2] < endp && isdigit((unsigned char)p[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
return &p[2] < endp && isdigit((unsigned char)p[0]) && 34 10 44
2786 88 ▶ isdigit((unsigned char)p[1]) && p[2] == ':';
Branch: 3 branches
BranchTrueFalseTotal
isdigit((unsigned char)p[1]) && p[2] == ':'; 44 0 44
isdigit((unsigned char)p[1]) && p[2] == ':'; 33 1 34
isdigit((unsigned char)p[1]) && p[2] == ':'; 1 32 33
2787 }
2788  
2789 // Check if the next token may be DATE
2790 43 static inline bool test_date(const char *p, const char *endp) {
2791 43 ▶ return &p[4] < endp && isdigit((unsigned char)p[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
return &p[4] < endp && isdigit((unsigned char)p[0]) && 33 10 43
2792 33 ▶ isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) &&
Branch: 2 branches
BranchTrueFalseTotal
isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) && 32 1 33
isdigit((unsigned char)p[1]) && isdigit((unsigned char)p[2]) && 23 9 32
2793 86 ▶ isdigit((unsigned char)p[3]) && p[4] == '-';
Branch: 3 branches
BranchTrueFalseTotal
isdigit((unsigned char)p[3]) && p[4] == '-'; 43 0 43
isdigit((unsigned char)p[3]) && p[4] == '-'; 15 8 23
isdigit((unsigned char)p[3]) && p[4] == '-'; 10 5 15
2794 }
2795  
2796 // Check if the next token may be BOOL
2797 33 static inline bool test_bool(const char *p, const char *endp) {
2798 33 ▶ return &p[0] < endp && (*p == 't' || *p == 'f');
Branch: 3 branches
BranchTrueFalseTotal
return &p[0] < endp && (*p == 't' || *p == 'f'); 33 0 33
return &p[0] < endp && (*p == 't' || *p == 'f'); 30 3 33
return &p[0] < endp && (*p == 't' || *p == 'f'); 7 23 30
2799 }
2800  
2801 // Check if the next token may be NUMBER
2802 23 static bool test_number(const char *p, const char *endp) {
2803 23 ▶ if (&p[0] < endp && *p && strchr("0123456789+-._", *p)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&p[0] < endp && *p && strchr("0123456789+-._", *p)) { 23 0 23
if (&p[0] < endp && *p && strchr("0123456789+-._", *p)) { 23 0 23
if (&p[0] < endp && *p && strchr("0123456789+-._", *p)) { 23 0 23
2804 23 return true;
2805 }
2806 0 ▶ if (&p[2] < endp) {
Branch: 1 branch
BranchTrueFalseTotal
if (&p[2] < endp) { 0 0 0
2807 0 ▶ if (0 == memcmp(p, "nan", 3) || 0 == memcmp(p, "inf", 3)) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == memcmp(p, "nan", 3) || 0 == memcmp(p, "inf", 3)) { 0 0 0
if (0 == memcmp(p, "nan", 3) || 0 == memcmp(p, "inf", 3)) { 0 0 0
2808 0 return true;
2809 }
2810 }
2811 0 return false;
2812 }
2813  
2814 // Scan a literal that is not a string
2815 44 static int scan_nonstring_literal(scanner_t *sp, token_t *tok) {
2816 44 int lineno = sp->lineno;
2817 44 ▶ if (test_time(sp->cur, sp->endp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_time(sp->cur, sp->endp)) { 1 43 44
2818 1 return scan_time(sp, tok);
2819 }
2820  
2821 43 ▶ if (test_date(sp->cur, sp->endp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_date(sp->cur, sp->endp)) { 10 33 43
2822 10 return scan_timestamp(sp, tok);
2823 }
2824  
2825 33 ▶ if (test_bool(sp->cur, sp->endp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_bool(sp->cur, sp->endp)) { 10 23 33
2826 10 return scan_bool(sp, tok);
2827 }
2828  
2829 23 ▶ if (test_number(sp->cur, sp->endp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (test_number(sp->cur, sp->endp)) { 23 0 23
2830 23 return scan_number(sp, tok);
2831 }
2832 0 return SETERROR(sp->ebuf, lineno, "invalid value");
2833 }
2834  
2835 // Return true if Unicode codepoint is allowed in a TOML 1.1 bare key.
2836 // Ranges taken verbatim from the TOML 1.1 spec grammar for bare-key-char.
2837 0 static bool is_unicode_bare_key_char(uint32_t cp) {
2838 0 ▶ if (cp == 0xB2 || cp == 0xB3 || cp == 0xB9)
Branch: 3 branches
BranchTrueFalseTotal
if (cp == 0xB2 || cp == 0xB3 || cp == 0xB9) 0 0 0
if (cp == 0xB2 || cp == 0xB3 || cp == 0xB9) 0 0 0
if (cp == 0xB2 || cp == 0xB3 || cp == 0xB9) 0 0 0
2839 0 return true;
2840 0 ▶ if (0xBC <= cp && cp <= 0xBE)
Branch: 2 branches
BranchTrueFalseTotal
if (0xBC <= cp && cp <= 0xBE) 0 0 0
if (0xBC <= cp && cp <= 0xBE) 0 0 0
2841 0 return true;
2842 0 ▶ if (0xC0 <= cp && cp <= 0xD6)
Branch: 2 branches
BranchTrueFalseTotal
if (0xC0 <= cp && cp <= 0xD6) 0 0 0
if (0xC0 <= cp && cp <= 0xD6) 0 0 0
2843 0 return true;
2844 0 ▶ if (0xD8 <= cp && cp <= 0xF6)
Branch: 2 branches
BranchTrueFalseTotal
if (0xD8 <= cp && cp <= 0xF6) 0 0 0
if (0xD8 <= cp && cp <= 0xF6) 0 0 0
2845 0 return true;
2846 0 ▶ if (0xF8 <= cp && cp <= 0x37D)
Branch: 2 branches
BranchTrueFalseTotal
if (0xF8 <= cp && cp <= 0x37D) 0 0 0
if (0xF8 <= cp && cp <= 0x37D) 0 0 0
2847 0 return true;
2848 0 ▶ if (0x37F <= cp && cp <= 0x1FFF)
Branch: 2 branches
BranchTrueFalseTotal
if (0x37F <= cp && cp <= 0x1FFF) 0 0 0
if (0x37F <= cp && cp <= 0x1FFF) 0 0 0
2849 0 return true;
2850 0 ▶ if (cp == 0x200C || cp == 0x200D)
Branch: 2 branches
BranchTrueFalseTotal
if (cp == 0x200C || cp == 0x200D) 0 0 0
if (cp == 0x200C || cp == 0x200D) 0 0 0
2851 0 return true;
2852 0 ▶ if (0x203F <= cp && cp <= 0x2040)
Branch: 2 branches
BranchTrueFalseTotal
if (0x203F <= cp && cp <= 0x2040) 0 0 0
if (0x203F <= cp && cp <= 0x2040) 0 0 0
2853 0 return true;
2854 0 ▶ if (0x2070 <= cp && cp <= 0x218F)
Branch: 2 branches
BranchTrueFalseTotal
if (0x2070 <= cp && cp <= 0x218F) 0 0 0
if (0x2070 <= cp && cp <= 0x218F) 0 0 0
2855 0 return true;
2856 0 ▶ if (0x2460 <= cp && cp <= 0x24FF)
Branch: 2 branches
BranchTrueFalseTotal
if (0x2460 <= cp && cp <= 0x24FF) 0 0 0
if (0x2460 <= cp && cp <= 0x24FF) 0 0 0
2857 0 return true;
2858 0 ▶ if (0x2C00 <= cp && cp <= 0x2FEF)
Branch: 2 branches
BranchTrueFalseTotal
if (0x2C00 <= cp && cp <= 0x2FEF) 0 0 0
if (0x2C00 <= cp && cp <= 0x2FEF) 0 0 0
2859 0 return true;
2860 0 ▶ if (0x3001 <= cp && cp <= 0xD7FF)
Branch: 2 branches
BranchTrueFalseTotal
if (0x3001 <= cp && cp <= 0xD7FF) 0 0 0
if (0x3001 <= cp && cp <= 0xD7FF) 0 0 0
2861 0 return true;
2862 0 ▶ if (0xF900 <= cp && cp <= 0xFDCF)
Branch: 2 branches
BranchTrueFalseTotal
if (0xF900 <= cp && cp <= 0xFDCF) 0 0 0
if (0xF900 <= cp && cp <= 0xFDCF) 0 0 0
2863 0 return true;
2864 0 ▶ if (0xFDF0 <= cp && cp <= 0xFFFD)
Branch: 2 branches
BranchTrueFalseTotal
if (0xFDF0 <= cp && cp <= 0xFFFD) 0 0 0
if (0xFDF0 <= cp && cp <= 0xFFFD) 0 0 0
2865 0 return true;
2866 0 ▶ if (0x10000 <= cp && cp <= 0xEFFFF)
Branch: 2 branches
BranchTrueFalseTotal
if (0x10000 <= cp && cp <= 0xEFFFF) 0 0 0
if (0x10000 <= cp && cp <= 0xEFFFF) 0 0 0
2867 0 return true;
2868 0 return false;
2869 }
2870  
2871 // Scan a literal
2872 56 static int scan_literal(scanner_t *sp, token_t *tok) {
2873 56 *tok = mktoken(sp, TOK_LIT);
2874 56 const char *p = sp->cur;
2875 421 ▶ while (p < sp->endp) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < sp->endp) { 420 1 421
2876 420 ▶ if (isalnum((unsigned char)*p) || *p == '_' || *p == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (isalnum((unsigned char)*p) || *p == '_' || *p == '-') { 56 364 420
if (isalnum((unsigned char)*p) || *p == '_' || *p == '-') { 55 1 56
if (isalnum((unsigned char)*p) || *p == '_' || *p == '-') { 0 55 55
2877 365 p++;
2878 365 continue;
2879 }
2880 55 ▶ if ((unsigned char)*p >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned char)*p >= 0x80) { 0 55 55
2881 uint32_t cp;
2882 0 int n = utf8_to_ucs(p, sp->endp - p, &cp);
2883 0 ▶ if (n > 0 && is_unicode_bare_key_char(cp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (n > 0 && is_unicode_bare_key_char(cp)) { 0 0 0
if (n > 0 && is_unicode_bare_key_char(cp)) { 0 0 0
2884 0 p += n;
2885 0 continue;
2886 }
2887 }
2888 55 break;
2889 }
2890 56 tok->str.len = p - tok->str.ptr;
2891 56 sp->cur = p;
2892 56 return 0;
2893 }
2894  
2895 // Save the current state of the scanner
2896 61 static scanner_state_t scan_mark(scanner_t *sp) {
2897 scanner_state_t mark;
2898 61 mark.sp = sp;
2899 61 mark.cur = sp->cur;
2900 61 mark.lineno = sp->lineno;
2901 61 mark.line_start = sp->line_start;
2902 61 return mark;
2903 }
2904  
2905 // Restore the scanner state to a previously saved state
2906 59 static void scan_restore(scanner_t *sp, scanner_state_t mark) {
2907 59 ▶ assert(mark.sp == sp);
Branch: 1 branch
BranchTrueFalseTotal
assert(mark.sp == sp); 0 59 59
2908 59 sp->cur = mark.cur;
2909 59 sp->lineno = mark.lineno;
2910 59 sp->line_start = mark.line_start;
2911 59 }
2912  
2913 // Return the next token
2914 377 static int scan_next(scanner_t *sp, bool keymode, token_t *tok) {
2915 static const toktyp_t map[128] = {
2916 ['\n'] = TOK_ENDL, ['.'] = TOK_DOT, ['='] = TOK_EQUAL,
2917 [','] = TOK_COMMA, ['{'] = TOK_LBRACE, ['}'] = TOK_RBRACE};
2918 188 again:
2919 565 *tok = mktoken(sp, TOK_FIN);
2920  
2921 565 int ch = S_GET();
2922 565 ▶ if (ch == TOK_FIN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) { 7 558 565
2923 7 return 0;
2924 }
2925  
2926 558 tok->str.len = 1;
2927 558 ▶ if (0 <= ch && ch < 128 && map[ch]) {
Branch: 3 branches
BranchTrueFalseTotal
if (0 <= ch && ch < 128 && map[ch]) { 558 0 558
if (0 <= ch && ch < 128 && map[ch]) { 558 0 558
if (0 <= ch && ch < 128 && map[ch]) { 199 359 558
2928 // map simple char to token type and done
2929 199 tok->toktyp = map[ch];
2930 199 return 0;
2931 }
2932  
2933 // handle char that require logic
2934 359 switch (ch) {
2935 183 case ' ':
2936 case '\t':
2937 183 goto again; // skip whitespace
2938  
2939 5 case '#':
2940 // comment: skip until newline
2941 125 ▶ while (!S_MATCH('\n')) {
Branch: 1 branch
BranchTrueFalseTotal
while (!S_MATCH('\n')) { 120 5 125
2942 120 ch = S_GET();
2943 120 ▶ if (ch == TOK_FIN)
Branch: 1 branch
BranchTrueFalseTotal
if (ch == TOK_FIN) 0 120 120
2944 0 break;
2945 120 ▶ if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) ||
Branch: 5 branches
BranchTrueFalseTotal
if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) || 120 0 120
if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) || 120 0 120
if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) || 120 0 120
if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) || 120 0 120
if ((0 <= ch && ch <= 0x8) || (0x0a <= ch && ch <= 0x1f) || 0 120 120
2946 (ch == 0x7f)) {
2947 0 return SETERROR(sp->ebuf, sp->lineno, "bad control char in comment");
2948 }
2949 }
2950 5 goto again; // skip comment
2951  
2952 21 case '[':
2953 21 tok->toktyp = TOK_LBRACK;
2954 21 ▶ if (keymode && S_MATCH('[')) {
Branch: 2 branches
BranchTrueFalseTotal
if (keymode && S_MATCH('[')) { 9 12 21
if (keymode && S_MATCH('[')) { 0 9 9
2955 0 S_GET();
2956 0 tok->toktyp = TOK_LLBRACK;
2957 0 tok->str.len = 2;
2958 }
2959 21 break;
2960  
2961 28 case ']':
2962 28 tok->toktyp = TOK_RBRACK;
2963 28 ▶ if (keymode && S_MATCH(']')) {
Branch: 2 branches
BranchTrueFalseTotal
if (keymode && S_MATCH(']')) { 18 10 28
if (keymode && S_MATCH(']')) { 0 18 18
2964 0 S_GET();
2965 0 tok->toktyp = TOK_RRBRACK;
2966 0 tok->str.len = 2;
2967 }
2968 28 break;
2969  
2970 22 case '"':
2971 22 sp->cur--;
2972 22 ▶ DO(scan_string(sp, tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_string(sp, tok)); 0 22 22
2973 22 break;
2974  
2975 0 case '\'':
2976 0 sp->cur--;
2977 0 ▶ DO(scan_litstring(sp, tok));
Branch: 1 branch
BranchTrueFalseTotal
DO(scan_litstring(sp, tok)); 0 0 0
2978 0 break;
2979  
2980 100 default:
2981 100 sp->cur--;
2982 100 ▶ DO(keymode ? scan_literal(sp, tok) : scan_nonstring_literal(sp, tok));
Branch: 2 branches
BranchTrueFalseTotal
DO(keymode ? scan_literal(sp, tok) : scan_nonstring_literal(sp, tok)); 56 44 100
DO(keymode ? scan_literal(sp, tok) : scan_nonstring_literal(sp, tok)); 2 98 100
2983 98 break;
2984 }
2985  
2986 169 return 0;
2987 }
2988  
2989 // Check for stack overflow due to excessive number of brackets or braces
2990 375 static int check_overflow(scanner_t *sp, token_t *tok) {
2991 375 switch (tok->toktyp) {
2992 21 case TOK_LBRACK:
2993 21 sp->bracket_level++;
2994 21 ▶ if (sp->bracket_level > BRACKET_LEVEL_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp->bracket_level > BRACKET_LEVEL_MAX) { 0 21 21
2995 0 return SETERROR(sp->ebuf, sp->lineno, "stack overflow");
2996 }
2997 21 break;
2998 28 case TOK_RBRACK:
2999 28 sp->bracket_level--;
3000 28 break;
3001 5 case TOK_LBRACE:
3002 5 sp->brace_level++;
3003 5 ▶ if (sp->brace_level > BRACE_LEVEL_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp->brace_level > BRACE_LEVEL_MAX) { 0 5 5
3004 0 return SETERROR(sp->ebuf, sp->lineno, "stack overflow");
3005 }
3006 5 break;
3007 4 case TOK_RBRACE:
3008 4 sp->brace_level--;
3009 4 break;
3010 317 default:
3011 317 break;
3012 }
3013 375 return 0;
3014 }
3015  
3016 266 static int scan_key(scanner_t *sp, token_t *tok) {
3017 266 ▶ if (sp->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp->errmsg) { 0 266 266
3018 0 return -1;
3019 }
3020 266 ▶ if (scan_next(sp, true, tok) || check_overflow(sp, tok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (scan_next(sp, true, tok) || check_overflow(sp, tok)) { 266 0 266
if (scan_next(sp, true, tok) || check_overflow(sp, tok)) { 0 266 266
3021 0 sp->errmsg = sp->ebuf.ptr;
3022 0 return -1;
3023 }
3024 266 return 0;
3025 }
3026  
3027 111 static int scan_value(scanner_t *sp, token_t *tok) {
3028 111 ▶ if (sp->errmsg) {
Branch: 1 branch
BranchTrueFalseTotal
if (sp->errmsg) { 0 111 111
3029 0 return -1;
3030 }
3031 111 ▶ if (scan_next(sp, false, tok) || check_overflow(sp, tok)) {
Branch: 2 branches
BranchTrueFalseTotal
if (scan_next(sp, false, tok) || check_overflow(sp, tok)) { 109 2 111
if (scan_next(sp, false, tok) || check_overflow(sp, tok)) { 0 109 109
3032 2 sp->errmsg = sp->ebuf.ptr;
3033 2 return -1;
3034 }
3035 109 return 0;
3036 }
3037  
3038 /**
3039 * Convert a char in utf8 into UCS, and store it in *ret.
3040 * Return #bytes consumed or -1 on failure.
3041 */
3042 0 static int utf8_to_ucs(const char *orig, int len, uint32_t *ret) {
3043 0 const unsigned char *buf = (const unsigned char *)orig;
3044 0 unsigned i = *buf++;
3045 uint32_t v;
3046  
3047 /* 0x00000000 - 0x0000007F:
3048 0xxxxxxx
3049 */
3050 0 ▶ if (0 == (i >> 7)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == (i >> 7)) { 0 0 0
3051 0 ▶ if (len < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 1) 0 0 0
3052 0 return -1;
3053 0 v = i;
3054 0 return *ret = v, 1;
3055 }
3056 /* 0x00000080 - 0x000007FF:
3057 110xxxxx 10xxxxxx
3058 */
3059 0 ▶ if (0x6 == (i >> 5)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0x6 == (i >> 5)) { 0 0 0
3060 0 ▶ if (len < 2)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 2) 0 0 0
3061 0 return -1;
3062 0 v = i & 0x1f;
3063 0 ▶ for (int j = 0; j < 1; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < 1; j++) { 0 0 0
3064 0 i = *buf++;
3065 0 ▶ if (0x2 != (i >> 6))
Branch: 1 branch
BranchTrueFalseTotal
if (0x2 != (i >> 6)) 0 0 0
3066 0 return -1;
3067 0 v = (v << 6) | (i & 0x3f);
3068 }
3069 0 return *ret = v, (const char *)buf - orig;
3070 }
3071  
3072 /* 0x00000800 - 0x0000FFFF:
3073 1110xxxx 10xxxxxx 10xxxxxx
3074 */
3075 0 ▶ if (0xE == (i >> 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0xE == (i >> 4)) { 0 0 0
3076 0 ▶ if (len < 3)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 3) 0 0 0
3077 0 return -1;
3078 0 v = i & 0x0F;
3079 0 ▶ for (int j = 0; j < 2; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < 2; j++) { 0 0 0
3080 0 i = *buf++;
3081 0 ▶ if (0x2 != (i >> 6))
Branch: 1 branch
BranchTrueFalseTotal
if (0x2 != (i >> 6)) 0 0 0
3082 0 return -1;
3083 0 v = (v << 6) | (i & 0x3f);
3084 }
3085 0 return *ret = v, (const char *)buf - orig;
3086 }
3087  
3088 /* 0x00010000 - 0x001FFFFF:
3089 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
3090 */
3091 0 ▶ if (0x1E == (i >> 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0x1E == (i >> 3)) { 0 0 0
3092 0 ▶ if (len < 4)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 4) 0 0 0
3093 0 return -1;
3094 0 v = i & 0x07;
3095 0 ▶ for (int j = 0; j < 3; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (int j = 0; j < 3; j++) { 0 0 0
3096 0 i = *buf++;
3097 0 ▶ if (0x2 != (i >> 6))
Branch: 1 branch
BranchTrueFalseTotal
if (0x2 != (i >> 6)) 0 0 0
3098 0 return -1;
3099 0 v = (v << 6) | (i & 0x3f);
3100 }
3101 0 return *ret = v, (const char *)buf - orig;
3102 }
3103  
3104 if (0) {
3105 // NOTE: these code points taking more than 4 bytes are not supported
3106  
3107 /* 0x00200000 - 0x03FFFFFF:
3108 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
3109 */
3110 if (0x3E == (i >> 2)) {
3111 if (len < 5)
3112 return -1;
3113 v = i & 0x03;
3114 for (int j = 0; j < 4; j++) {
3115 i = *buf++;
3116 if (0x2 != (i >> 6))
3117 return -1;
3118 v = (v << 6) | (i & 0x3f);
3119 }
3120 return *ret = v, (const char *)buf - orig;
3121 }
3122  
3123 /* 0x04000000 - 0x7FFFFFFF:
3124 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
3125 */
3126 if (0x7e == (i >> 1)) {
3127 if (len < 6)
3128 return -1;
3129 v = i & 0x01;
3130 for (int j = 0; j < 5; j++) {
3131 i = *buf++;
3132 if (0x2 != (i >> 6))
3133 return -1;
3134 v = (v << 6) | (i & 0x3f);
3135 }
3136 return *ret = v, (const char *)buf - orig;
3137 }
3138 }
3139  
3140 0 return -1;
3141 }
3142  
3143 /**
3144 * Convert a UCS char to utf8 code, and return it in buf.
3145 * Return #bytes used in buf to encode the char, or
3146 * -1 on error.
3147 */
3148 0 static int ucs_to_utf8(uint32_t code, char buf[4]) {
3149 /* http://stackoverflow.com/questions/6240055/manually-converting-unicode-codepoints-into-utf-8-and-utf-16
3150 */
3151 /* The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well
3152 * as 0xfffe and 0xffff (UCS noncharacters) should not appear in
3153 * conforming UTF-8 streams.
3154 */
3155 /*
3156 * https://github.com/toml-lang/toml-test/issues/165
3157 * [0xd800, 0xdfff] and [0xfffe, 0xffff] are implicitly allowed by TOML, so
3158 * we disable the check.
3159 */
3160 if (0) {
3161 if (0xd800 <= code && code <= 0xdfff)
3162 return -1;
3163 if (0xfffe <= code && code <= 0xffff)
3164 return -1;
3165 }
3166  
3167 /* 0x00000000 - 0x0000007F:
3168 0xxxxxxx
3169 */
3170 0 ▶ if (code <= 0x7F) {
Branch: 1 branch
BranchTrueFalseTotal
if (code <= 0x7F) { 0 0 0
3171 0 buf[0] = (unsigned char)code;
3172 0 return 1;
3173 }
3174  
3175 /* 0x00000080 - 0x000007FF:
3176 110xxxxx 10xxxxxx
3177 */
3178 0 ▶ if (code <= 0x000007FF) {
Branch: 1 branch
BranchTrueFalseTotal
if (code <= 0x000007FF) { 0 0 0
3179 0 buf[0] = (unsigned char)(0xc0 | (code >> 6));
3180 0 buf[1] = (unsigned char)(0x80 | (code & 0x3f));
3181 0 return 2;
3182 }
3183  
3184 /* 0x00000800 - 0x0000FFFF:
3185 1110xxxx 10xxxxxx 10xxxxxx
3186 */
3187 0 ▶ if (code <= 0x0000FFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (code <= 0x0000FFFF) { 0 0 0
3188 0 buf[0] = (unsigned char)(0xe0 | (code >> 12));
3189 0 buf[1] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
3190 0 buf[2] = (unsigned char)(0x80 | (code & 0x3f));
3191 0 return 3;
3192 }
3193  
3194 /* 0x00010000 - 0x001FFFFF:
3195 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
3196 */
3197 0 ▶ if (code <= 0x001FFFFF) {
Branch: 1 branch
BranchTrueFalseTotal
if (code <= 0x001FFFFF) { 0 0 0
3198 0 buf[0] = (unsigned char)(0xf0 | (code >> 18));
3199 0 buf[1] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
3200 0 buf[2] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
3201 0 buf[3] = (unsigned char)(0x80 | (code & 0x3f));
3202 0 return 4;
3203 }
3204  
3205 #ifdef UNDEF
3206 if (0) {
3207 // NOTE: these code points taking more than 4 bytes are not supported
3208 /* 0x00200000 - 0x03FFFFFF:
3209 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
3210 */
3211 if (code <= 0x03FFFFFF) {
3212 buf[0] = (unsigned char)(0xf8 | (code >> 24));
3213 buf[1] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
3214 buf[2] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
3215 buf[3] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
3216 buf[4] = (unsigned char)(0x80 | (code & 0x3f));
3217 return 5;
3218 }
3219  
3220 /* 0x04000000 - 0x7FFFFFFF:
3221 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
3222 */
3223 if (code <= 0x7FFFFFFF) {
3224 buf[0] = (unsigned char)(0xfc | (code >> 30));
3225 buf[1] = (unsigned char)(0x80 | ((code >> 24) & 0x3f));
3226 buf[2] = (unsigned char)(0x80 | ((code >> 18) & 0x3f));
3227 buf[3] = (unsigned char)(0x80 | ((code >> 12) & 0x3f));
3228 buf[4] = (unsigned char)(0x80 | ((code >> 6) & 0x3f));
3229 buf[5] = (unsigned char)(0x80 | (code & 0x3f));
3230 return 6;
3231 }
3232 }
3233 #endif
3234  
3235 0 return -1;
3236 }
« easyxs/easyxs_numeric.h ↑ Summary XS.xs »