Line data Source code
1 : #ifndef HEADER_fd_src_ballet_toml_fd_toml_h 2 : #define HEADER_fd_src_ballet_toml_fd_toml_h 3 : 4 : /* fd_toml.h provides APIs for parsing TOML config files. 5 : 6 : Grammar: https://github.com/toml-lang/toml/blob/1.0.0/toml.abnf */ 7 : 8 : #include "../../util/pod/fd_pod.h" 9 : 10 : /* Error codes */ 11 : 12 12 : #define FD_TOML_SUCCESS ( 0) /* ok */ 13 0 : #define FD_TOML_ERR_POD (-1) /* ran out of output space */ 14 0 : #define FD_TOML_ERR_SCRATCH (-2) /* ran out of scratch space */ 15 0 : #define FD_TOML_ERR_KEY (-3) /* oversz key */ 16 0 : #define FD_TOML_ERR_DUP (-4) /* duplicate key */ 17 0 : #define FD_TOML_ERR_RANGE (-5) /* overflow */ 18 0 : #define FD_TOML_ERR_PARSE (-6) /* parse fail */ 19 : 20 : /* FD_TOML_PATH_MAX is the max supported pod path length. */ 21 : 22 : #define FD_TOML_PATH_MAX (512UL) 23 : 24 : /* fd_toml_err_info_t contains information about a TOML parse failure. */ 25 : 26 : struct fd_toml_err_info { 27 : ulong line; /* 1-indexed line number */ 28 : /* ... add more info here ... */ 29 : }; 30 : 31 : typedef struct fd_toml_err_info fd_toml_err_info_t; 32 : 33 : FD_PROTOTYPES_BEGIN 34 : 35 : /* fd_toml_parse deserializes a TOML document and inserts the document's 36 : object tree into an fd_pod. toml points to the first byte of the 37 : TOML. toml_sz is the byte length of the TOML. If toml_sz==0 then 38 : the toml pointer is ignored (may be invalid). pod is a local join to 39 : an fd_pod_t. [scratch,scratch+scratch_sz) is arbitrary unaligned 40 : scratch memory used during deserialization. scratch_sz>=4kB 41 : recommended. If scratch_sz is too small, may fail to deserialize 42 : long strings and sub tables. On success, returns FD_TOML_SUCCESS. 43 : On parse failure returns FD_TOML_ERR_*. If opt_err!=NULL, 44 : initializes *opt_err with error information (even if the return code 45 : was success). 46 : 47 : Note that toml is not interpreted as a cstr -- No terminating zero is 48 : fine and so are stray zeros in the middle of the file. 49 : 50 : fd_toml_parse is not hardened against untrusted input. fd_toml_parse 51 : is not optimized for performance. 52 : 53 : Mapping: 54 : 55 : TOML type | Example | fd_pod type 56 : ---------------|-------------|-------------------------------------- 57 : table | [key] | subpod 58 : array table | [[key]] | subpod (keys %d formatted cstrs) 59 : inline table | x={a=1,b=2} | subpod 60 : inline array | x=[1,2] | subpod (keys %d formatted cstrs) 61 : bool | true | int 62 : integer | -3 | long 63 : float | 3e-3 | float 64 : string | 'hello' | cstr 65 : 66 : Despite the name, TOML is neither "obvious" nor "minimal". fd_toml 67 : thus only supports a subset of the 'spec' and ignores some horrors. 68 : Known errata: 69 : 70 : - fd_toml allows duplicate tables and arrays whereas TOML has various 71 : complicated rules that forbid such. For example, the following 72 : is not allowed in toml: 73 : 74 : fruit = [] 75 : [[fruit]] # inline arrays are immutable 76 : 77 : - fd_toml allows mixing tables and arrays which is forbidden in TOML 78 : 79 : a = [1] 80 : a.b = 1 81 : 82 : - Missing validation for out-of-bounds Unicode escapes 83 : 84 : - Missing support for CRLF 85 : 86 : - Missing support for subtables of array tables. 87 : The following gets deserialized as {a={b={c={d="val0"}}}} instead 88 : of {a=[{b=[{c={d="val0"}}]}]} 89 : 90 : [[a]] 91 : [[a.b]] 92 : [a.b.c] 93 : d = "val0" 94 : 95 : - Missing support for dot-escapes. 96 : The tables ["a.b"] and [a.b] are the same in fd_toml. 97 : 98 : - Keys with embedded NUL characters are truncated whereas they are 99 : legal in TOML. 100 : 101 : - Infinite and NaN floats are rejected. 102 : 103 : - Missing support for date-time values. */ 104 : 105 : int 106 : fd_toml_parse( void const * toml, 107 : ulong toml_sz, 108 : uchar * pod, 109 : uchar * scratch, 110 : ulong scratch_sz, 111 : fd_toml_err_info_t * opt_err ); 112 : 113 : /* fd_toml_strerror returns a human-readable error string with static 114 : storage describing the given FD_TOML_ERR_* code. Works for negative 115 : return values in fd_toml_parse. */ 116 : 117 : FD_FN_CONST char const * 118 : fd_toml_strerror( int err ); 119 : 120 : FD_PROTOTYPES_END 121 : 122 : #endif /* HEADER_fd_src_ballet_toml_fd_toml_h */