include/eshu_yaml.h

filter: Statementstmt 92.3% 253 / 274 Branchbran 60.9% 240 / 394 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.8% 493 / 668 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/eshu_java.h ↑ Summary include/eshu_go.h »
1 /*
2 * eshu_yaml.h — YAML indentation normaliser
3 *
4 * YAML uses indentation-significant structure. This pass normalises
5 * the indent style (tabs→spaces or vice-versa, adjusting indent_width)
6 * while preserving logical depth inferred from existing leading whitespace.
7 *
8 * Depth model: same stack-based approach as Python. Each new indent
9 * level is pushed onto depth_stack; dedents pop until a match is found.
10 *
11 * Block scalars (| and >): after a line ending in | or >, subsequent
12 * deeper-indented lines belong to the scalar body. The body is
13 * re-emitted at (key_depth+1) plus any relative indentation within
14 * the block, so that the block remains valid YAML after normalisation.
15 *
16 * Flow collections ({ } and [ ]): content that spans lines inside a
17 * flow collection is emitted verbatim — flow collections carry their
18 * own comma-separated syntax and not structural indentation.
19 *
20 * Directives (%YAML, %TAG) and document markers (--- / ...) are
21 * emitted at depth 0; markers also reset the depth stack.
22 */
23  
24 #ifndef ESHU_YAML_H
25 #define ESHU_YAML_H
26  
27 #include "eshu.h"
28 #include <string.h>
29 #include <ctype.h>
30  
31 /* ══════════════════════════════════════════════════════════════════
32 * Context
33 * ══════════════════════════════════════════════════════════════════ */
34  
35 typedef struct {
36 int depth;
37 int depth_stack[64]; /* source leading-space count per level */
38 int depth_top;
39 int in_block_scalar; /* 0=no, 1=literal(|), 2=folded(>) */
40 int scalar_src_base; /* source leading spaces of first body line */
41 int scalar_norm_depth;/* logical depth for body = key depth + 1 */
42 int flow_depth; /* unclosed { or [ */
43 enum eshu_state state;
44 eshu_config_t cfg;
45 } eshu_yaml_ctx_t;
46  
47 81 static void eshu_yaml_ctx_init(eshu_yaml_ctx_t *ctx, const eshu_config_t *cfg) {
48 81 memset(ctx, 0, sizeof(*ctx));
49 81 ctx->depth_stack[0] = 0;
50 81 ctx->depth_top = 1;
51 81 ctx->scalar_src_base = -1;
52 81 ctx->state = ESHU_CODE;
53 81 ctx->cfg = *cfg;
54 81 }
55  
56 /* ══════════════════════════════════════════════════════════════════
57 * Measure leading whitespace (spaces; tabs treated as 1 column)
58 * ══════════════════════════════════════════════════════════════════ */
59  
60 561 static int eshu_yaml_measure_indent(const char *p, const char *eol) {
61 561 int col = 0;
62 2173 ▶ while (p < eol && (*p == ' ' || *p == '\t')) { col++; p++; }
Branch: 3 branches
BranchTrueFalseTotal
while (p < eol && (*p == ' ' || *p == '\t')) { col++; p++; } 2173 0 2173
while (p < eol && (*p == ' ' || *p == '\t')) { col++; p++; } 1612 561 2173
while (p < eol && (*p == ' ' || *p == '\t')) { col++; p++; } 0 561 561
63 561 return col;
64 }
65  
66 /* ══════════════════════════════════════════════════════════════════
67 * Resolve logical depth from leading-space count (depth stack)
68 * ══════════════════════════════════════════════════════════════════ */
69  
70 530 static int eshu_yaml_resolve_depth(eshu_yaml_ctx_t *ctx, int col) {
71 int top;
72 530 ▶ if (ctx->depth_top < 64 && col > ctx->depth_stack[ctx->depth_top - 1]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ctx->depth_top < 64 && col > ctx->depth_stack[ctx->depth_top - 1]) { 530 0 530
if (ctx->depth_top < 64 && col > ctx->depth_stack[ctx->depth_top - 1]) { 201 329 530
73 201 ctx->depth_stack[ctx->depth_top++] = col;
74 201 ctx->depth = ctx->depth_top - 1;
75 201 return ctx->depth;
76 }
77 329 top = ctx->depth_top;
78 418 ▶ while (top > 1 && col < ctx->depth_stack[top - 1])
Branch: 2 branches
BranchTrueFalseTotal
while (top > 1 && col < ctx->depth_stack[top - 1]) 276 142 418
while (top > 1 && col < ctx->depth_stack[top - 1]) 89 187 276
79 89 top--;
80 329 ctx->depth_top = top;
81 329 ctx->depth = top - 1;
82 329 return ctx->depth;
83 }
84  
85 /* ══════════════════════════════════════════════════════════════════
86 * Detect block scalar opener at end of line.
87 * Scans backwards, stripping trailing whitespace then
88 * chomp/indent indicators (-/+/1-9), then checks for | or >.
89 * The char before the indicator must be whitespace or ':'.
90 * Returns 1 (literal |), 2 (folded >), or 0.
91 * ══════════════════════════════════════════════════════════════════ */
92  
93 530 static int eshu_yaml_block_scalar_type(const char *content, const char *eol) {
94 530 const char *p = eol;
95 char last;
96 530 ▶ while (p > content && (*(p-1) == ' ' || *(p-1) == '\t')) p--;
Branch: 3 branches
BranchTrueFalseTotal
while (p > content && (*(p-1) == ' ' || *(p-1) == '\t')) p--; 530 0 530
while (p > content && (*(p-1) == ' ' || *(p-1) == '\t')) p--; 0 530 530
while (p > content && (*(p-1) == ' ' || *(p-1) == '\t')) p--; 0 530 530
97 643 ▶ while (p > content && (*(p-1) == '-' || *(p-1) == '+' ||
Branch: 3 branches
BranchTrueFalseTotal
while (p > content && (*(p-1) == '-' || *(p-1) == '+' || 643 0 643
while (p > content && (*(p-1) == '-' || *(p-1) == '+' || 2 641 643
while (p > content && (*(p-1) == '-' || *(p-1) == '+' || 0 641 641
98 643 ▶ (*(p-1) >= '1' && *(p-1) <= '9'))) p--;
Branch: 2 branches
BranchTrueFalseTotal
(*(p-1) >= '1' && *(p-1) <= '9'))) p--; 547 94 641
(*(p-1) >= '1' && *(p-1) <= '9'))) p--; 111 436 547
99 530 ▶ if (p <= content) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (p <= content) return 0; 0 530 530
100 530 last = *(p-1);
101 530 ▶ if (last != '|' && last != '>') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (last != '|' && last != '>') return 0; 525 5 530
if (last != '|' && last != '>') return 0; 522 3 525
102 8 ▶ if (p - 1 > content && *(p-2) != ' ' && *(p-2) != '\t' && *(p-2) != ':')
Branch: 4 branches
BranchTrueFalseTotal
if (p - 1 > content && *(p-2) != ' ' && *(p-2) != '\t' && *(p-2) != ':') 8 0 8
if (p - 1 > content && *(p-2) != ' ' && *(p-2) != '\t' && *(p-2) != ':') 0 8 8
if (p - 1 > content && *(p-2) != ' ' && *(p-2) != '\t' && *(p-2) != ':') 0 0 0
if (p - 1 > content && *(p-2) != ' ' && *(p-2) != '\t' && *(p-2) != ':') 0 0 0
103 0 return 0;
104 8 ▶ return (last == '|') ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
return (last == '|') ? 1 : 2; 5 3 8
105 }
106  
107 /* ══════════════════════════════════════════════════════════════════
108 * Scan a line for { [ ] } and string transitions (updates flow_depth
109 * and ctx->state). Comments (# after whitespace) stop the scan.
110 * ══════════════════════════════════════════════════════════════════ */
111  
112 524 static void eshu_yaml_scan_flow(eshu_yaml_ctx_t *ctx,
113 const char *p, const char *end)
114 {
115 524 char prev = ' ';
116 6323 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 5803 520 6323
117 5803 char c = *p;
118 5803 ▶ if (ctx->state == ESHU_YAML_STRING_DQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_YAML_STRING_DQ) { 12 5791 5803
119 12 ▶ if (c == '\\' && p+1 < end) { p++; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' && p+1 < end) { p++; } 1 11 12
if (c == '\\' && p+1 < end) { p++; } 1 0 1
120 11 ▶ else if (c == '"') ctx->state = ESHU_CODE;
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '"') ctx->state = ESHU_CODE; 1 10 11
121 12 prev = c; p++; continue;
122 }
123 5791 ▶ if (ctx->state == ESHU_YAML_STRING_SQ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->state == ESHU_YAML_STRING_SQ) { 145 5646 5791
124 145 ▶ if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; prev = '\''; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; prev = '\''; continue; } 23 122 145
if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; prev = '\''; continue; } 1 22 23
if (c == '\'' && p+1 < end && *(p+1) == '\'') { p += 2; prev = '\''; continue; } 1 0 1
125 144 ▶ if (c == '\'') ctx->state = ESHU_CODE;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\'') ctx->state = ESHU_CODE; 22 122 144
126 144 prev = c; p++; continue;
127 }
128 /* code */
129 5646 ▶ if (c == '#' && (prev == ' ' || prev == '\t' || p == end - (int)(end-end)))
Branch: 4 branches
BranchTrueFalseTotal
if (c == '#' && (prev == ' ' || prev == '\t' || p == end - (int)(end-end))) 4 5642 5646
if (c == '#' && (prev == ' ' || prev == '\t' || p == end - (int)(end-end))) 0 4 4
if (c == '#' && (prev == ' ' || prev == '\t' || p == end - (int)(end-end))) 0 0 0
if (c == '#' && (prev == ' ' || prev == '\t' || p == end - (int)(end-end))) 0 0 0
130 break; /* comment */
131 5642 ▶ if (c == '"') { ctx->state = ESHU_YAML_STRING_DQ; prev = c; p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { ctx->state = ESHU_YAML_STRING_DQ; prev = c; p++; continue; } 1 5641 5642
132 5641 ▶ if (c == '\'') { ctx->state = ESHU_YAML_STRING_SQ; prev = c; p++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\'') { ctx->state = ESHU_YAML_STRING_SQ; prev = c; p++; continue; } 22 5619 5641
133 5619 ▶ if (c == '{' || c == '[') ctx->flow_depth++;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' || c == '[') ctx->flow_depth++; 5612 7 5619
if (c == '{' || c == '[') ctx->flow_depth++; 7 5605 5612
134 5619 ▶ if (c == '}' || c == ']') { if (ctx->flow_depth > 0) ctx->flow_depth--; }
Branch: 3 branches
BranchTrueFalseTotal
if (c == '}' || c == ']') { if (ctx->flow_depth > 0) ctx->flow_depth--; } 5612 7 5619
if (c == '}' || c == ']') { if (ctx->flow_depth > 0) ctx->flow_depth--; } 7 5605 5612
if (c == '}' || c == ']') { if (ctx->flow_depth > 0) ctx->flow_depth--; } 14 0 14
135 5619 prev = c; p++;
136 }
137 524 }
138  
139 /* ══════════════════════════════════════════════════════════════════
140 * Process one line
141 * ══════════════════════════════════════════════════════════════════ */
142  
143 566 static void eshu_yaml_process_line(eshu_yaml_ctx_t *ctx, eshu_buf_t *out,
144 const char *line_start, const char *eol,
145 int lineno)
146 {
147 566 const char *p = line_start;
148 566 const char *end = eol;
149 566 const char *content = eshu_skip_leading_ws(p);
150 int src_leading;
151 int depth;
152 int bs_type;
153  
154 /* ── Empty line ── */
155 566 ▶ if (content >= end) {
Branch: 1 branch
BranchTrueFalseTotal
if (content >= end) { 5 561 566
156 /* blank lines inside a block scalar are part of the body */
157 5 eshu_buf_putc(out, '\n');
158 5 return;
159 }
160  
161 561 src_leading = eshu_yaml_measure_indent(line_start, eol);
162  
163 /* ── Inside block scalar body ── */
164 561 ▶ if (ctx->in_block_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->in_block_scalar) { 28 533 561
165 28 ▶ if (ctx->scalar_src_base < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->scalar_src_base < 0) { 8 20 28
166 /* First non-empty body line: establish base */
167 8 ctx->scalar_src_base = src_leading;
168 }
169 28 ▶ if (src_leading >= ctx->scalar_src_base) {
Branch: 1 branch
BranchTrueFalseTotal
if (src_leading >= ctx->scalar_src_base) { 23 5 28
170 /* Body line: re-emit at (scalar_norm_depth) + relative spaces */
171 23 int relative = src_leading - ctx->scalar_src_base;
172 int i;
173 23 ▶ if (eshu_in_range(&ctx->cfg, lineno)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(&ctx->cfg, lineno)) { 23 0 23
174 23 eshu_emit_indent(out, ctx->scalar_norm_depth, &ctx->cfg);
175 29 ▶ for (i = 0; i < relative; i++) eshu_buf_putc(out, ' ');
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < relative; i++) eshu_buf_putc(out, ' '); 6 23 29
176 23 eshu_buf_write_trimmed(out, content, (int)(end - content));
177 } else {
178 0 eshu_buf_write(out, line_start, (size_t)(eol - line_start));
179 }
180 23 eshu_buf_putc(out, '\n');
181 23 return;
182 }
183 /* Indentation less than scalar base → scalar ended */
184 5 ctx->in_block_scalar = 0;
185 5 ctx->scalar_src_base = -1;
186 }
187  
188 /* ── Directive line (%YAML, %TAG) ── */
189 538 ▶ if (*content == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (*content == '%') { 0 538 538
190 0 ▶ if (eshu_in_range(&ctx->cfg, lineno)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(&ctx->cfg, lineno)) { 0 0 0
191 0 eshu_buf_write_trimmed(out, content, (int)(end - content));
192 } else {
193 0 eshu_buf_write(out, line_start, (size_t)(end - line_start));
194 }
195 0 eshu_buf_putc(out, '\n');
196 0 return;
197 }
198  
199 /* ── Document markers (--- / ...) — reset depth stack ── */
200 538 ▶ if ((end - content) >= 3 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((end - content) >= 3 && 526 12 538
201 526 ▶ ((content[0] == '-' && content[1] == '-' && content[2] == '-') ||
Branch: 3 branches
BranchTrueFalseTotal
((content[0] == '-' && content[1] == '-' && content[2] == '-') || 106 420 526
((content[0] == '-' && content[1] == '-' && content[2] == '-') || 5 101 106
((content[0] == '-' && content[1] == '-' && content[2] == '-') || 0 5 5
202 521 ▶ (content[0] == '.' && content[1] == '.' && content[2] == '.'))) {
Branch: 3 branches
BranchTrueFalseTotal
(content[0] == '.' && content[1] == '.' && content[2] == '.'))) { 1 520 521
(content[0] == '.' && content[1] == '.' && content[2] == '.'))) { 1 0 1
(content[0] == '.' && content[1] == '.' && content[2] == '.'))) { 1 0 1
203 6 ctx->depth_top = 1;
204 6 ctx->depth_stack[0] = 0;
205 6 ctx->depth = 0;
206 6 ctx->in_block_scalar = 0;
207 6 ctx->scalar_src_base = -1;
208 6 ctx->flow_depth = 0;
209 6 ▶ if (eshu_in_range(&ctx->cfg, lineno)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(&ctx->cfg, lineno)) { 6 0 6
210 6 eshu_buf_write_trimmed(out, content, (int)(end - content));
211 } else {
212 0 eshu_buf_write(out, line_start, (size_t)(end - line_start));
213 }
214 6 eshu_buf_putc(out, '\n');
215 6 return;
216 }
217  
218 /* ── Inside flow collection: emit verbatim, update flow depth ── */
219 532 ▶ if (ctx->flow_depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx->flow_depth > 0) { 2 530 532
220 2 eshu_yaml_scan_flow(ctx, content, end);
221 2 eshu_buf_write(out, line_start, (size_t)(eol - line_start));
222 2 eshu_buf_putc(out, '\n');
223 2 return;
224 }
225  
226 /* ── Normal structural line: measure depth, re-emit ── */
227 530 depth = eshu_yaml_resolve_depth(ctx, src_leading);
228  
229 530 ▶ if (eshu_in_range(&ctx->cfg, lineno)) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_in_range(&ctx->cfg, lineno)) { 530 0 530
230 530 eshu_emit_indent(out, depth, &ctx->cfg);
231 530 eshu_buf_write_trimmed(out, content, (int)(end - content));
232 } else {
233 0 eshu_buf_write(out, line_start, (size_t)(eol - line_start));
234 }
235 530 eshu_buf_putc(out, '\n');
236  
237 /* Check for block scalar opener on this line */
238 530 bs_type = eshu_yaml_block_scalar_type(content, end);
239 530 ▶ if (bs_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (bs_type) { 8 522 530
240 8 ctx->in_block_scalar = bs_type;
241 8 ctx->scalar_src_base = -1; /* determined by first body line */
242 8 ctx->scalar_norm_depth = depth + 1;
243 } else {
244 522 eshu_yaml_scan_flow(ctx, content, end);
245 }
246 }
247  
248 /* ══════════════════════════════════════════════════════════════════
249 * Public API
250 * ══════════════════════════════════════════════════════════════════ */
251  
252 81 static char *eshu_indent_yaml(const char *src, size_t src_len,
253 const eshu_config_t *cfg, size_t *out_len)
254 {
255 eshu_yaml_ctx_t ctx;
256 eshu_buf_t out;
257 81 const char *p = src;
258 81 const char *end = src + src_len;
259 81 int lineno = 1;
260  
261 81 eshu_yaml_ctx_init(&ctx, cfg);
262 81 eshu_buf_init(&out, src_len + 256);
263  
264 647 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 566 81 647
265 566 const char *eol = eshu_find_eol(p);
266 566 eshu_yaml_process_line(&ctx, &out, p, eol, lineno);
267 566 p = eol;
268 566 ▶ if (*p == '\n') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\n') p++; 566 0 566
269 566 lineno++;
270 }
271  
272 81 eshu_buf_putc(&out, '\0');
273 81 out.len--;
274 81 *out_len = out.len;
275 81 return out.data;
276 }
277  
278  
279 #include "eshu_hl_util.h"
280  
281 /* ══════════════════════════════════════════════════════════════════
282 * YAML highlighter
283 *
284 * Token classes:
285 * esh-a mapping key (word before ':')
286 * esh-v anchor (&name) and alias (*name)
287 * esh-p tag (!tag, !!type) and directive (%YAML, %TAG)
288 * esh-k document markers (--- ...), booleans, null, sequence '-'
289 * esh-s quoted strings ("..." and '...') and bare scalar values
290 * esh-n numbers
291 * esh-h block scalar body (lines after | or >)
292 * esh-c comments (# to EOL)
293 * ══════════════════════════════════════════════════════════════════ */
294  
295 static const char * const eshu_hl_yaml_bool[] = {
296 "true", "false", "null", "yes", "no", "on", "off",
297 "True", "False", "Null", "Yes", "No", "On", "Off",
298 "TRUE", "FALSE", "NULL", "YES", "NO", "ON", "OFF",
299 NULL
300 };
301  
302 35 static char *eshu_highlight_yaml(const char *src, size_t src_len, size_t *out_len)
303 {
304 eshu_buf_t out;
305 35 const char *p = src;
306 35 const char *end = src + src_len;
307 35 const char *plain = p;
308 35 int in_block_scalar = 0; /* 1=literal, 2=folded */
309 35 int scalar_indent = -1;
310 35 int flow_depth = 0;
311  
312 35 eshu_buf_init(&out, src_len * 2 + 64);
313  
314 #define YAML_SPAN(cls, ts, te) do { \
315 eshu_hl_flush(&out, plain, (ts)); \
316 eshu_hl_span(&out, (cls), (ts), (te)); \
317 p = (te); plain = p; \
318 } while(0)
319  
320 75 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 40 35 75
321 /* find end of current line */
322 40 const char *ls = p;
323 40 const char *eol = p;
324 457 ▶ while (eol < end && *eol != '\n') eol++;
Branch: 2 branches
BranchTrueFalseTotal
while (eol < end && *eol != '\n') eol++; 457 0 457
while (eol < end && *eol != '\n') eol++; 417 40 457
325  
326 /* leading whitespace */
327 40 const char *content = p;
328 46 ▶ while (content < eol && (*content == ' ' || *content == '\t')) content++;
Branch: 3 branches
BranchTrueFalseTotal
while (content < eol && (*content == ' ' || *content == '\t')) content++; 46 0 46
while (content < eol && (*content == ' ' || *content == '\t')) content++; 6 40 46
while (content < eol && (*content == ' ' || *content == '\t')) content++; 0 40 40
329 40 int src_leading = (int)(content - p);
330  
331 /* ── Block scalar body ── */
332 40 ▶ if (in_block_scalar) {
Branch: 1 branch
BranchTrueFalseTotal
if (in_block_scalar) { 2 38 40
333 2 ▶ if (content >= eol) {
Branch: 1 branch
BranchTrueFalseTotal
if (content >= eol) { 0 2 2
334 /* blank line inside block scalar */
335 0 ▶ p = eol; if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
p = eol; if (p < end && *p == '\n') p++; 0 0 0
p = eol; if (p < end && *p == '\n') p++; 0 0 0
336 0 eshu_hl_flush(&out, plain, p); plain = p;
337 0 continue;
338 }
339 2 ▶ if (scalar_indent < 0) scalar_indent = src_leading;
Branch: 1 branch
BranchTrueFalseTotal
if (scalar_indent < 0) scalar_indent = src_leading; 2 0 2
340 2 ▶ if (src_leading >= scalar_indent) {
Branch: 1 branch
BranchTrueFalseTotal
if (src_leading >= scalar_indent) { 2 0 2
341 /* body line */
342 2 YAML_SPAN("esh-h", ls, eol);
343 2 ▶ if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '\n') p++; 2 0 2
if (p < end && *p == '\n') p++; 2 0 2
344 2 plain = p;
345 2 continue;
346 }
347 0 in_block_scalar = 0;
348 0 scalar_indent = -1;
349 }
350  
351 /* ── Empty line ── */
352 38 ▶ if (content >= eol) {
Branch: 1 branch
BranchTrueFalseTotal
if (content >= eol) { 0 38 38
353 0 ▶ p = eol; if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
p = eol; if (p < end && *p == '\n') p++; 0 0 0
p = eol; if (p < end && *p == '\n') p++; 0 0 0
354 0 continue;
355 }
356  
357 /* advance p past leading ws */
358 38 p = content;
359  
360 /* ── Directive: %YAML %TAG ── */
361 38 ▶ if (*p == '%') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '%') { 1 37 38
362 1 YAML_SPAN("esh-p", p, eol);
363 1 ▶ if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '\n') p++; 1 0 1
if (p < end && *p == '\n') p++; 1 0 1
364 1 plain = p;
365 1 continue;
366 }
367  
368 /* ── Document markers: --- / ... ── */
369 37 ▶ if ((eol - p) >= 3 &&
Branch: 1 branch
BranchTrueFalseTotal
if ((eol - p) >= 3 && 37 0 37
370 37 ▶ ((p[0]=='-'&&p[1]=='-'&&p[2]=='-') ||
Branch: 3 branches
BranchTrueFalseTotal
((p[0]=='-'&&p[1]=='-'&&p[2]=='-') || 2 35 37
((p[0]=='-'&&p[1]=='-'&&p[2]=='-') || 1 1 2
((p[0]=='-'&&p[1]=='-'&&p[2]=='-') || 0 1 1
371 36 ▶ (p[0]=='.'&&p[1]=='.'&&p[2]=='.'))) {
Branch: 3 branches
BranchTrueFalseTotal
(p[0]=='.'&&p[1]=='.'&&p[2]=='.'))) { 1 35 36
(p[0]=='.'&&p[1]=='.'&&p[2]=='.'))) { 1 0 1
(p[0]=='.'&&p[1]=='.'&&p[2]=='.'))) { 1 0 1
372 2 const char *te = p + 3;
373 2 YAML_SPAN("esh-k", p, te);
374 2 ▶ p = eol; if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
p = eol; if (p < end && *p == '\n') p++; 2 0 2
p = eol; if (p < end && *p == '\n') p++; 2 0 2
375 2 plain = p;
376 2 continue;
377 }
378  
379 /* ── Sequence indicator: '- ' at start of content ── */
380 35 ▶ if (*p == '-' && p+1 < eol && (*(p+1) == ' ' || *(p+1) == '\t')) {
Branch: 4 branches
BranchTrueFalseTotal
if (*p == '-' && p+1 < eol && (*(p+1) == ' ' || *(p+1) == '\t')) { 1 34 35
if (*p == '-' && p+1 < eol && (*(p+1) == ' ' || *(p+1) == '\t')) { 1 0 1
if (*p == '-' && p+1 < eol && (*(p+1) == ' ' || *(p+1) == '\t')) { 0 1 1
if (*p == '-' && p+1 < eol && (*(p+1) == ' ' || *(p+1) == '\t')) { 0 0 0
381 1 YAML_SPAN("esh-k", p, p+1);
382 1 p++; /* space after '-' is plain */
383 }
384  
385 /* ── Scan line content for inline tokens ── */
386 /* First, check if this line has a mapping key (word followed by ':') */
387 {
388 35 const char *kp = p;
389 /* A key is: optional '?' + word chars + ':' + (space or EOL or #) */
390 35 ▶ if (*kp == '?') kp++;
Branch: 1 branch
BranchTrueFalseTotal
if (*kp == '?') kp++; 0 35 35
391 35 const char *ks = kp;
392 159 ▶ while (kp < eol && *kp != ':' && *kp != '\n' && *kp != '#') kp++;
Branch: 4 branches
BranchTrueFalseTotal
while (kp < eol && *kp != ':' && *kp != '\n' && *kp != '#') kp++; 159 0 159
while (kp < eol && *kp != ':' && *kp != '\n' && *kp != '#') kp++; 126 33 159
while (kp < eol && *kp != ':' && *kp != '\n' && *kp != '#') kp++; 126 0 126
while (kp < eol && *kp != ':' && *kp != '\n' && *kp != '#') kp++; 124 2 126
393 35 ▶ if (kp < eol && *kp == ':' &&
Branch: 2 branches
BranchTrueFalseTotal
if (kp < eol && *kp == ':' && 35 0 35
if (kp < eol && *kp == ':' && 33 2 35
394 33 ▶ (kp+1 >= eol || *(kp+1) == ' ' || *(kp+1) == '\t' ||
Branch: 3 branches
BranchTrueFalseTotal
(kp+1 >= eol || *(kp+1) == ' ' || *(kp+1) == '\t' || 32 1 33
(kp+1 >= eol || *(kp+1) == ' ' || *(kp+1) == '\t' || 0 32 32
(kp+1 >= eol || *(kp+1) == ' ' || *(kp+1) == '\t' || 0 0 0
395 0 ▶ *(kp+1) == '\n' || *(kp+1) == '#')) {
Branch: 2 branches
BranchTrueFalseTotal
*(kp+1) == '\n' || *(kp+1) == '#')) { 0 0 0
*(kp+1) == '\n' || *(kp+1) == '#')) { 0 0 0
396 /* key found: ks..kp is the key word, kp is ':' */
397 33 ▶ if (ks < kp) {
Branch: 1 branch
BranchTrueFalseTotal
if (ks < kp) { 33 0 33
398 /* emit leading '?' if present */
399 33 ▶ if (p < ks) { eshu_hl_flush(&out, plain, p); plain = p; }
Branch: 1 branch
BranchTrueFalseTotal
if (p < ks) { eshu_hl_flush(&out, plain, p); plain = p; } 0 33 33
400 33 YAML_SPAN("esh-a", ks, kp);
401 /* ':' emitted as plain */
402 33 p = kp;
403 }
404 } else {
405 /* not a key line — reset scan */
406 2 kp = p;
407 }
408 }
409  
410 /* scan rest of line char by char */
411 138 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 105 33 138
412 105 char c = *p;
413  
414 /* comment */
415 105 ▶ if (c == '#' && (p == ls || *(p-1) == ' ' || *(p-1) == '\t')) {
Branch: 4 branches
BranchTrueFalseTotal
if (c == '#' && (p == ls || *(p-1) == ' ' || *(p-1) == '\t')) { 2 103 105
if (c == '#' && (p == ls || *(p-1) == ' ' || *(p-1) == '\t')) { 0 2 2
if (c == '#' && (p == ls || *(p-1) == ' ' || *(p-1) == '\t')) { 0 0 0
if (c == '#' && (p == ls || *(p-1) == ' ' || *(p-1) == '\t')) { 0 0 0
416 2 YAML_SPAN("esh-c", p, eol);
417 2 break;
418 }
419  
420 /* anchor &name */
421 103 ▶ if (c == '&') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '&') { 1 102 103
422 1 const char *ts = p++;
423 7 ▶ while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.'))
Branch: 5 branches
BranchTrueFalseTotal
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 6 1 7
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 6 0 6
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
424 6 p++;
425 1 YAML_SPAN("esh-v", ts, p);
426 1 continue;
427 }
428  
429 /* alias *name */
430 102 ▶ if (c == '*') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '*') { 1 101 102
431 1 const char *ts = p++;
432 7 ▶ while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.'))
Branch: 5 branches
BranchTrueFalseTotal
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 6 1 7
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 6 0 6
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_' || *p=='.')) 0 0 0
433 6 p++;
434 1 YAML_SPAN("esh-v", ts, p);
435 1 continue;
436 }
437  
438 /* tag !!type or !tag */
439 101 ▶ if (c == '!') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '!') { 3 98 101
440 3 const char *ts = p++;
441 3 ▶ if (p < eol && *p == '!') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < eol && *p == '!') p++; 3 0 3
if (p < eol && *p == '!') p++; 3 0 3
442 13 ▶ while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_'))
Branch: 4 branches
BranchTrueFalseTotal
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_')) 13 0 13
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_')) 10 3 13
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_')) 0 3 3
while (p < eol && (isalnum((unsigned char)*p) || *p=='-' || *p=='_')) 0 3 3
443 10 p++;
444 3 YAML_SPAN("esh-p", ts, p);
445 3 continue;
446 }
447  
448 /* double-quoted string */
449 98 ▶ if (c == '"') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '"') { 1 97 98
450 1 const char *ts = p++;
451 12 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 12 0 12
452 12 ▶ if (*p == '\\' && p+1 < eol) { p += 2; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (*p == '\\' && p+1 < eol) { p += 2; continue; } 0 12 12
if (*p == '\\' && p+1 < eol) { p += 2; continue; } 0 0 0
453 12 ▶ if (*p == '"') { p++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '"') { p++; break; } 1 11 12
454 11 p++;
455 }
456 1 YAML_SPAN("esh-s", ts, p);
457 1 continue;
458 }
459  
460 /* single-quoted string ('' escapes a single quote) */
461 97 ▶ if (c == '\'') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\'') { 1 96 97
462 1 const char *ts = p++;
463 12 ▶ while (p < eol) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < eol) { 12 0 12
464 12 ▶ if (*p == '\'' && p+1 < eol && *(p+1) == '\'') { p += 2; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (*p == '\'' && p+1 < eol && *(p+1) == '\'') { p += 2; continue; } 1 11 12
if (*p == '\'' && p+1 < eol && *(p+1) == '\'') { p += 2; continue; } 0 1 1
if (*p == '\'' && p+1 < eol && *(p+1) == '\'') { p += 2; continue; } 0 0 0
465 12 ▶ if (*p == '\'') { p++; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '\'') { p++; break; } 1 11 12
466 11 p++;
467 }
468 1 YAML_SPAN("esh-s", ts, p);
469 1 continue;
470 }
471  
472 /* number (integer, float, hex) */
473 96 ▶ if (isdigit((unsigned char)c) ||
Branch: 2 branches
BranchTrueFalseTotal
if (isdigit((unsigned char)c) || 92 4 96
if (isdigit((unsigned char)c) || 1 91 92
474 1 ▶ (c == '-' && p+1 < eol && isdigit((unsigned char)*(p+1)))) {
Branch: 2 branches
BranchTrueFalseTotal
(c == '-' && p+1 < eol && isdigit((unsigned char)*(p+1)))) { 1 0 1
(c == '-' && p+1 < eol && isdigit((unsigned char)*(p+1)))) { 1 0 1
475 5 const char *ts = p;
476 5 ▶ if (*p == '-') p++;
Branch: 1 branch
BranchTrueFalseTotal
if (*p == '-') p++; 1 4 5
477 21 ▶ while (p < eol && (isdigit((unsigned char)*p) || *p=='.' ||
Branch: 3 branches
BranchTrueFalseTotal
while (p < eol && (isdigit((unsigned char)*p) || *p=='.' || 16 5 21
while (p < eol && (isdigit((unsigned char)*p) || *p=='.' || 15 1 16
while (p < eol && (isdigit((unsigned char)*p) || *p=='.' || 1 0 1
478 0 ▶ *p=='_' || *p=='e' || *p=='E' ||
Branch: 3 branches
BranchTrueFalseTotal
*p=='_' || *p=='e' || *p=='E' || 0 0 0
*p=='_' || *p=='e' || *p=='E' || 0 0 0
*p=='_' || *p=='e' || *p=='E' || 0 0 0
479 0 ▶ *p=='+' || *p=='-' || *p=='x' ||
Branch: 3 branches
BranchTrueFalseTotal
*p=='+' || *p=='-' || *p=='x' || 0 0 0
*p=='+' || *p=='-' || *p=='x' || 0 0 0
*p=='+' || *p=='-' || *p=='x' || 0 0 0
480 0 ▶ (*p>='a'&&*p<='f') || (*p>='A'&&*p<='F')))
Branch: 4 branches
BranchTrueFalseTotal
(*p>='a'&&*p<='f') || (*p>='A'&&*p<='F'))) 0 0 0
(*p>='a'&&*p<='f') || (*p>='A'&&*p<='F'))) 0 0 0
(*p>='a'&&*p<='f') || (*p>='A'&&*p<='F'))) 0 0 0
(*p>='a'&&*p<='f') || (*p>='A'&&*p<='F'))) 0 0 0
481 16 p++;
482 5 YAML_SPAN("esh-n", ts, p);
483 5 continue;
484 }
485  
486 /* flow brackets { [ ] } */
487 91 ▶ if (c == '{' || c == '[') { flow_depth++; p++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c == '{' || c == '[') { flow_depth++; p++; continue; } 91 0 91
if (c == '{' || c == '[') { flow_depth++; p++; continue; } 0 91 91
488 91 ▶ if (c == '}' || c == ']') { if (flow_depth>0) flow_depth--; p++; continue; }
Branch: 3 branches
BranchTrueFalseTotal
if (c == '}' || c == ']') { if (flow_depth>0) flow_depth--; p++; continue; } 91 0 91
if (c == '}' || c == ']') { if (flow_depth>0) flow_depth--; p++; continue; } 0 91 91
if (c == '}' || c == ']') { if (flow_depth>0) flow_depth--; p++; continue; } 0 0 0
489  
490 /* identifier: check for boolean/null keyword */
491 91 ▶ if (isalpha((unsigned char)c) || c == '_') {
Branch: 2 branches
BranchTrueFalseTotal
if (isalpha((unsigned char)c) || c == '_') { 70 21 91
if (isalpha((unsigned char)c) || c == '_') { 0 70 70
492 21 const char *ts = p;
493 100 ▶ while (p < eol && (isalnum((unsigned char)*p) || *p=='_')) p++;
Branch: 3 branches
BranchTrueFalseTotal
while (p < eol && (isalnum((unsigned char)*p) || *p=='_')) p++; 79 21 100
while (p < eol && (isalnum((unsigned char)*p) || *p=='_')) p++; 79 0 79
while (p < eol && (isalnum((unsigned char)*p) || *p=='_')) p++; 0 0 0
494 21 size_t wlen = (size_t)(p - ts);
495 21 int is_bool = 0;
496 size_t bi;
497 261 ▶ for (bi = 0; eshu_hl_yaml_bool[bi]; bi++) {
Branch: 1 branch
BranchTrueFalseTotal
for (bi = 0; eshu_hl_yaml_bool[bi]; bi++) { 256 5 261
498 256 ▶ if (strlen(eshu_hl_yaml_bool[bi]) == wlen &&
Branch: 1 branch
BranchTrueFalseTotal
if (strlen(eshu_hl_yaml_bool[bi]) == wlen && 66 190 256
499 66 ▶ memcmp(ts, eshu_hl_yaml_bool[bi], wlen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
memcmp(ts, eshu_hl_yaml_bool[bi], wlen) == 0) { 16 50 66
500 16 is_bool = 1; break;
501 }
502 }
503 21 ▶ if (is_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_bool) { 16 5 21
504 16 YAML_SPAN("esh-k", ts, p);
505 } else {
506 /* plain text — flush and write html-escaped */
507 5 eshu_hl_flush(&out, plain, ts);
508 5 eshu_hl_write_html(&out, ts, wlen);
509 5 plain = p;
510 }
511 21 continue;
512 }
513  
514 70 p++;
515 }
516  
517 /* detect block scalar opener on this line */
518 {
519 35 const char *ep = eol;
520 35 ▶ while (ep > content && (*(ep-1) == ' ' || *(ep-1) == '\t')) ep--;
Branch: 3 branches
BranchTrueFalseTotal
while (ep > content && (*(ep-1) == ' ' || *(ep-1) == '\t')) ep--; 35 0 35
while (ep > content && (*(ep-1) == ' ' || *(ep-1) == '\t')) ep--; 0 35 35
while (ep > content && (*(ep-1) == ' ' || *(ep-1) == '\t')) ep--; 0 35 35
521 43 ▶ while (ep > content && (*(ep-1) == '-' || *(ep-1) == '+' ||
Branch: 3 branches
BranchTrueFalseTotal
while (ep > content && (*(ep-1) == '-' || *(ep-1) == '+' || 43 0 43
while (ep > content && (*(ep-1) == '-' || *(ep-1) == '+' || 1 42 43
while (ep > content && (*(ep-1) == '-' || *(ep-1) == '+' || 0 42 42
522 43 ▶ (*(ep-1) >= '1' && *(ep-1) <= '9'))) ep--;
Branch: 2 branches
BranchTrueFalseTotal
(*(ep-1) >= '1' && *(ep-1) <= '9'))) ep--; 35 7 42
(*(ep-1) >= '1' && *(ep-1) <= '9'))) ep--; 7 28 35
523 35 ▶ if (ep > content) {
Branch: 1 branch
BranchTrueFalseTotal
if (ep > content) { 35 0 35
524 35 char last = *(ep-1);
525 35 ▶ if ((last == '|' || last == '>') &&
Branch: 2 branches
BranchTrueFalseTotal
if ((last == '|' || last == '>') && 34 1 35
if ((last == '|' || last == '>') && 1 33 34
526 2 ▶ (ep-1 == content || *(ep-2) == ' ' || *(ep-2) == '\t' ||
Branch: 3 branches
BranchTrueFalseTotal
(ep-1 == content || *(ep-2) == ' ' || *(ep-2) == '\t' || 2 0 2
(ep-1 == content || *(ep-2) == ' ' || *(ep-2) == '\t' || 0 2 2
(ep-1 == content || *(ep-2) == ' ' || *(ep-2) == '\t' || 0 0 0
527 0 ▶ *(ep-2) == ':')) {
Branch: 1 branch
BranchTrueFalseTotal
*(ep-2) == ':')) { 0 0 0
528 2 ▶ in_block_scalar = (last == '|') ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
in_block_scalar = (last == '|') ? 1 : 2; 1 1 2
529 2 scalar_indent = -1;
530 }
531 }
532 }
533  
534 /* advance past EOL */
535 35 ▶ if (p < end && *p == '\n') p++;
Branch: 2 branches
BranchTrueFalseTotal
if (p < end && *p == '\n') p++; 35 0 35
if (p < end && *p == '\n') p++; 35 0 35
536 35 plain = p;
537 }
538  
539 35 eshu_hl_flush(&out, plain, end);
540 35 eshu_buf_putc(&out, '\0');
541 35 *out_len = out.len - 1;
542 35 return out.data;
543  
544 #undef YAML_SPAN
545 }
546  
547 #endif /* ESHU_YAML_H */
« include/eshu_java.h ↑ Summary include/eshu_go.h »