include/eshu_json.h

filter: Statementstmt 95.1% 157 / 165 Branchbran 75.7% 159 / 210 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 316 / 375 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_py.h ↑ Summary include/eshu_c.h »
1 /*
2 * eshu_json.h — JSON/JSONC token-based reformatter for Eshu
3 *
4 * Fully re-serialises JSON with correct indentation, handling both
5 * compact single-line input and already-formatted multi-line input.
6 * JSONC extensions (// and block comments) are tolerated and preserved.
7 *
8 * Pure C, no Perl dependencies, header-only.
9 */
10  
11 #ifndef ESHU_JSON_H
12 #define ESHU_JSON_H
13  
14 #include "eshu.h"
15  
16 /* ══════════════════════════════════════════════════════════════════
17 * Internal helpers
18 * ══════════════════════════════════════════════════════════════════ */
19  
20 /* Advance past a JSON string body starting at *pp (which already
21 * points past the opening "). Stops after the closing ". */
22 619 static void eshu_json_scan_string(const char **pp, const char *end) {
23 619 const char *p = *pp;
24 4794 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 4794 0 4794
25 4794 ▶ if (*p == '\\') { p += 2; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') { p += 2; continue; } 10 4784 4794
26 4784 ▶ if (*p == '"') { p++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { p++; break; } 619 4165 4784
27 4165 p++;
28 }
29 619 *pp = p;
30 619 }
31  
32 /* Skip whitespace including newlines. */
33 2401 static void eshu_json_skip_ws(const char **pp, const char *end) {
34 6592 ▶ while (*pp < end && (unsigned char)**pp <= ' ') (*pp)++;
Branch: 2 branches
BranchTrueFalseTotal
while (*pp < end && (unsigned char)**pp <= ' ') (*pp)++; 6536 56 6592
while (*pp < end && (unsigned char)**pp <= ' ') (*pp)++; 4191 2345 6536
35 2401 }
36  
37 /* Skip a // line comment. *pp must point at the first '/' of '//'.
38 * Advances to just past the newline (or end). */
39 4 static void eshu_json_skip_line_comment(const char **pp, const char *end) {
40 4 *pp += 2; /* skip '//' */
41 43 ▶ while (*pp < end && **pp != '\n') (*pp)++;
Branch: 2 branches
BranchTrueFalseTotal
while (*pp < end && **pp != '\n') (*pp)++; 43 0 43
while (*pp < end && **pp != '\n') (*pp)++; 39 4 43
42 4 ▶ if (*pp < end) (*pp)++; /* skip '\n' */
Branch: 1 branch
BranchTrueFalseTotal
if (*pp < end) (*pp)++; /* skip '\n' */ 4 0 4
43 4 }
44  
45 /* Skip a block comment. *pp must point at the first '/' of "/ *".
46 * Advances past the closing "* /". */
47 2 static void eshu_json_skip_block_comment(const char **pp, const char *end) {
48 2 *pp += 2; /* skip slash-star */
49 20 ▶ while (*pp + 1 < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (*pp + 1 < end) { 20 0 20
50 20 ▶ if (**pp == '*' && *(*pp + 1) == '/') { *pp += 2; return; }
Branch: 2 branches
BranchTrueFalseTotal
if (**pp == '*' && *(*pp + 1) == '/') { *pp += 2; return; } 2 18 20
if (**pp == '*' && *(*pp + 1) == '/') { *pp += 2; return; } 2 0 2
51 18 (*pp)++;
52 }
53 0 *pp = end;
54 }
55  
56 /* ══════════════════════════════════════════════════════════════════
57 * eshu_indent_json
58 * ══════════════════════════════════════════════════════════════════ */
59  
60 111 static char *eshu_indent_json(const char *src, size_t src_len,
61 const eshu_config_t *cfg, size_t *out_len)
62 {
63 eshu_buf_t out;
64 111 const char *p = src;
65 111 const char *end = src + src_len;
66 111 int depth = 0;
67  
68 /* skip UTF-8 BOM */
69 111 ▶ if (src_len >= 3 &&
Branch: 1 branch
BranchTrueFalseTotal
if (src_len >= 3 && 109 2 111
70 109 ▶ (unsigned char)p[0] == 0xEF &&
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[0] == 0xEF && 0 109 109
71 0 ▶ (unsigned char)p[1] == 0xBB &&
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[1] == 0xBB && 0 0 0
72 0 ▶ (unsigned char)p[2] == 0xBF)
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[2] == 0xBF) 0 0 0
73 0 p += 3;
74  
75 111 eshu_buf_init(&out, src_len + 512);
76  
77 2174 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2119 55 2174
78 /* skip inter-token whitespace */
79 2119 eshu_json_skip_ws(&p, end);
80 2119 ▶ if (p >= end) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p >= end) break; 56 2063 2119
81  
82 2063 char c = *p;
83  
84 /* JSONC // comment: skip silently (reformatter normalises layout) */
85 2063 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 6 2057 2063
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 6 0 6
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 4 2 6
86 4 eshu_json_skip_line_comment(&p, end);
87 4 continue;
88 }
89  
90 /* JSONC block comment: skip silently */
91 2059 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 2057 2059
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 0 2
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 0 2
92 2 eshu_json_skip_block_comment(&p, end);
93 2 continue;
94 }
95  
96 /* string */
97 2057 ▶ if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { 619 1438 2057
98 619 const char *ts = p;
99 619 p++; /* past opening " */
100 619 eshu_json_scan_string(&p, end);
101 619 eshu_buf_write(&out, ts, (size_t)(p - ts));
102 619 continue;
103 }
104  
105 /* number (including negative) */
106 1438 ▶ if (c == '-' || isdigit((unsigned char)c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '-' || isdigit((unsigned char)c)) { 1432 6 1438
if (c == '-' || isdigit((unsigned char)c)) { 137 1295 1432
107 143 const char *ts = p;
108 143 ▶ if (*p == '-') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') p++; 6 137 143
109 352 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 352 0 352
while (p < end && isdigit((unsigned char)*p)) p++; 209 143 352
110 143 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 143 0 143
if (p < end && *p == '.') { 12 131 143
111 12 p++;
112 47 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 47 0 47
while (p < end && isdigit((unsigned char)*p)) p++; 35 12 47
113 }
114 143 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'e' || *p == 'E')) { 143 0 143
if (p < end && (*p == 'e' || *p == 'E')) { 137 6 143
if (p < end && (*p == 'e' || *p == 'E')) { 1 136 137
115 7 p++;
116 7 ▶ if (p < end && (*p == '+' || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) p++; 7 0 7
if (p < end && (*p == '+' || *p == '-')) p++; 6 1 7
if (p < end && (*p == '+' || *p == '-')) p++; 2 4 6
117 16 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 16 0 16
while (p < end && isdigit((unsigned char)*p)) p++; 9 7 16
118 }
119 143 eshu_buf_write(&out, ts, (size_t)(p - ts));
120 143 continue;
121 }
122  
123 /* keywords: true / false / null */
124 1295 ▶ if (isalpha((unsigned char)c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isalpha((unsigned char)c)) { 39 1256 1295
125 39 const char *ts = p;
126 200 ▶ while (p < end && isalpha((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isalpha((unsigned char)*p)) p++; 200 0 200
while (p < end && isalpha((unsigned char)*p)) p++; 161 39 200
127 39 eshu_buf_write(&out, ts, (size_t)(p - ts));
128 39 continue;
129 }
130  
131 /* open container { or [ */
132 1256 ▶ if (c == '{' || c == '[') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' || c == '[') { 1046 210 1256
if (c == '{' || c == '[') { 72 974 1046
133 282 ▶ char close = (c == '{') ? '}' : ']';
Branch: 1 branch
BranchTrueFalseTotal
char close = (c == '{') ? '}' : ']'; 210 72 282
134 282 eshu_buf_putc(&out, c);
135 282 p++;
136 /* look ahead (skipping whitespace) for empty container */
137 282 const char *q = p;
138 282 eshu_json_skip_ws(&q, end);
139 282 ▶ if (q < end && *q == close) {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == close) { 282 0 282
if (q < end && *q == close) { 15 267 282
140 /* empty container: emit inline, advance past close */
141 15 eshu_buf_putc(&out, close);
142 15 p = q + 1;
143 } else {
144 267 depth++;
145 267 eshu_buf_putc(&out, '\n');
146 267 eshu_emit_indent(&out, depth, cfg);
147 }
148 282 continue;
149 }
150  
151 /* close container } or ] */
152 974 ▶ if (c == '}' || c == ']') {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '}' || c == ']') { 772 202 974
if (c == '}' || c == ']') { 65 707 772
153 267 depth--;
154 267 ▶ if (depth < 0) depth = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (depth < 0) depth = 0; 0 267 267
155 267 eshu_buf_putc(&out, '\n');
156 267 eshu_emit_indent(&out, depth, cfg);
157 267 eshu_buf_putc(&out, c);
158 267 p++;
159 267 continue;
160 }
161  
162 /* comma — separator between elements */
163 707 ▶ if (c == ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ',') { 299 408 707
164 299 eshu_buf_putc(&out, ',');
165 299 eshu_buf_putc(&out, '\n');
166 299 eshu_emit_indent(&out, depth, cfg);
167 299 p++;
168 299 continue;
169 }
170  
171 /* colon — key/value separator */
172 408 ▶ if (c == ':') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ':') { 408 0 408
173 408 eshu_buf_putc(&out, ':');
174 408 eshu_buf_putc(&out, ' ');
175 408 p++;
176 408 continue;
177 }
178  
179 /* trailing comma (JSON5/JSONC) or stray char: skip */
180 0 p++;
181 }
182  
183 111 eshu_buf_putc(&out, '\n');
184 111 eshu_buf_putc(&out, '\0');
185 111 *out_len = out.len - 1;
186 111 return out.data;
187 }
188  
189  
190 #include "eshu_hl_util.h"
191  
192 /* ══════════════════════════════════════════════════════════════════
193 * JSON / JSONC highlighter
194 * ══════════════════════════════════════════════════════════════════ */
195  
196 35 static char *eshu_highlight_json(const char *src, size_t src_len, size_t *out_len) {
197 eshu_buf_t out;
198 35 const char *p = src;
199 35 const char *end = src + src_len;
200 35 const char *plain = p;
201  
202 /* skip UTF-8 BOM */
203 35 ▶ if (src_len >= 3 &&
Branch: 1 branch
BranchTrueFalseTotal
if (src_len >= 3 && 33 2 35
204 33 ▶ (unsigned char)p[0] == 0xEF &&
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[0] == 0xEF && 0 33 33
205 0 ▶ (unsigned char)p[1] == 0xBB &&
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[1] == 0xBB && 0 0 0
206 0 ▶ (unsigned char)p[2] == 0xBF) {
Branch: 1 branch
BranchTrueFalseTotal
(unsigned char)p[2] == 0xBF) { 0 0 0
207 0 plain = p += 3;
208 }
209  
210 35 eshu_buf_init(&out, src_len * 2 + 64);
211  
212 #define JSON_FLUSH(upto) eshu_hl_flush(&out, plain, (upto))
213 #define JSON_SPAN(cls, s, e) do { JSON_FLUSH(s); eshu_hl_span(&out, (cls), (s), (e)); plain = (e); } while(0)
214  
215 239 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 204 35 239
216 204 char c = *p;
217  
218 /* JSONC // line comment */
219 204 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '/') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 4 200 204
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 4 0 4
if (c == '/' && p + 1 < end && *(p + 1) == '/') { 2 2 4
220 2 const char *ts = p;
221 2 p += 2;
222 29 ▶ while (p < end && *p != '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '\n') p++; 28 1 29
while (p < end && *p != '\n') p++; 27 1 28
223 2 JSON_SPAN("esh-c", ts, p);
224 2 continue;
225 }
226  
227 /* JSONC block comment */
228 202 ▶ if (c == '/' && p + 1 < end && *(p + 1) == '*') {
Branch: 3 branches
BranchTrueFalseTotal
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 200 202
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 0 2
if (c == '/' && p + 1 < end && *(p + 1) == '*') { 2 0 2
229 2 const char *ts = p;
230 2 p += 2;
231 29 ▶ while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 29 0 29
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 27 2 29
while (p + 1 < end && !(*p == '*' && *(p + 1) == '/')) p++; 0 2 2
232 2 ▶ if (p + 1 < end) p += 2;
Branch: 1 branch
BranchTrueFalseTotal
if (p + 1 < end) p += 2; 2 0 2
233 2 JSON_SPAN("esh-c", ts, p);
234 2 continue;
235 }
236  
237 /* string: key (esh-a) or value (esh-s) */
238 200 ▶ if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { 42 158 200
239 42 const char *ts = p;
240 42 p++; /* past opening " */
241 175 ▶ while (p < end && *p != '"') {
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && *p != '"') { 133 0 133
while (p < end && *p != '"') { 91 42 133
242 91 ▶ if (*p == '\\') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\\') p++; 2 89 91
243 91 ▶ if (p < end) p++;
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; 91 0 91
244 }
245 42 ▶ if (p < end && *p == '"') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '"') p++; 42 0 42
if (p < end && *p == '"') p++; 42 0 42
246 42 const char *te = p;
247 /* look ahead for ':' to determine key vs value */
248 42 const char *q = p;
249 42 ▶ while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++;
Branch: 5 branches
BranchTrueFalseTotal
while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++; 42 0 42
while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++; 0 42 42
while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++; 0 42 42
while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++; 0 42 42
while (q < end && (*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')) q++; 0 42 42
250 42 ▶ if (q < end && *q == ':') {
Branch: 2 branches
BranchTrueFalseTotal
if (q < end && *q == ':') { 42 0 42
if (q < end && *q == ':') { 33 9 42
251 33 JSON_SPAN("esh-a", ts, te);
252 } else {
253 9 JSON_SPAN("esh-s", ts, te);
254 }
255 42 continue;
256 }
257  
258 /* number */
259 158 ▶ if (c == '-' || isdigit((unsigned char)c)) {
Branch: 2 branches
BranchTrueFalseTotal
if (c == '-' || isdigit((unsigned char)c)) { 157 1 158
if (c == '-' || isdigit((unsigned char)c)) { 25 132 157
260 26 const char *ts = p;
261 26 ▶ if (*p == '-') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') p++; 1 25 26
262 54 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 54 0 54
while (p < end && isdigit((unsigned char)*p)) p++; 28 26 54
263 26 ▶ if (p < end && *p == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '.') { 26 0 26
if (p < end && *p == '.') { 3 23 26
264 3 p++;
265 7 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 7 0 7
while (p < end && isdigit((unsigned char)*p)) p++; 4 3 7
266 }
267 26 ▶ if (p < end && (*p == 'e' || *p == 'E')) {
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == 'e' || *p == 'E')) { 26 0 26
if (p < end && (*p == 'e' || *p == 'E')) { 24 2 26
if (p < end && (*p == 'e' || *p == 'E')) { 1 23 24
268 3 p++;
269 3 ▶ if (p < end && (*p == '+' || *p == '-')) p++;
Branch: 3 branches
BranchTrueFalseTotal
if (p < end && (*p == '+' || *p == '-')) p++; 3 0 3
if (p < end && (*p == '+' || *p == '-')) p++; 2 1 3
if (p < end && (*p == '+' || *p == '-')) p++; 1 1 2
270 8 ▶ while (p < end && isdigit((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isdigit((unsigned char)*p)) p++; 8 0 8
while (p < end && isdigit((unsigned char)*p)) p++; 5 3 8
271 }
272 26 JSON_SPAN("esh-n", ts, p);
273 26 continue;
274 }
275  
276 /* keywords: null / true / false */
277 132 ▶ if (isalpha((unsigned char)c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isalpha((unsigned char)c)) { 8 124 132
278 8 const char *ts = p;
279 42 ▶ while (p < end && isalpha((unsigned char)*p)) p++;
Branch: 2 branches
BranchTrueFalseTotal
while (p < end && isalpha((unsigned char)*p)) p++; 42 0 42
while (p < end && isalpha((unsigned char)*p)) p++; 34 8 42
280 8 JSON_SPAN("esh-k", ts, p);
281 8 continue;
282 }
283  
284 124 p++;
285 }
286  
287 35 JSON_FLUSH(end);
288 35 eshu_buf_putc(&out, '\0');
289 35 *out_len = out.len - 1;
290 35 return out.data;
291  
292 #undef JSON_FLUSH
293 #undef JSON_SPAN
294 }
295  
296 #endif /* ESHU_JSON_H */
« include/eshu_py.h ↑ Summary include/eshu_c.h »