yeptris 0.6.58.1-arm-linux-eabihf → 0.6.60.1-arm-linux-eabihf

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/ext/yeptris_native/version.h +2 -2
  3. data/lib/yeptris/3.4/native.so +0 -0
  4. data/lib/yeptris/document.rb +15 -0
  5. data/lib/yeptris/ffi.rb +112 -0
  6. data/lib/yeptris/psych/visitors.rb +16 -6
  7. data/lib/yeptris/psych.rb +11 -5
  8. data/lib/yeptris/validation.rb +320 -0
  9. data/lib/yeptris/yaml.rb +128 -6
  10. data/lib/yeptris.rb +2 -1
  11. data/libyeptris.so +0 -0
  12. data/vendor/libyeptris/CMakeLists.txt +1 -1
  13. data/vendor/libyeptris/src/CMakeLists.txt +8 -0
  14. data/vendor/libyeptris/src/include/yeptris/emit.h +30 -2
  15. data/vendor/libyeptris/src/include/yeptris/parse.h +52 -0
  16. data/vendor/libyeptris/src/include/yeptris/resolve.h +8 -0
  17. data/vendor/libyeptris/src/include/yeptris/validate.h +259 -15
  18. data/vendor/libyeptris/src/include/yeptris/values.h +8 -2
  19. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +10 -0
  20. data/vendor/libyeptris/src/yeptris/cbor/decode.c +5 -7
  21. data/vendor/libyeptris/src/yeptris/cbor/encode.c +9 -9
  22. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +1 -1
  23. data/vendor/libyeptris/src/yeptris/common/error.h +2 -0
  24. data/vendor/libyeptris/src/yeptris/doc.h +20 -0
  25. data/vendor/libyeptris/src/yeptris/emit/emit.c +85 -13
  26. data/vendor/libyeptris/src/yeptris/emit/writer.c +158 -642
  27. data/vendor/libyeptris/src/yeptris/emit/writer.h +15 -1
  28. data/vendor/libyeptris/src/yeptris/events/push.c +28 -3
  29. data/vendor/libyeptris/src/yeptris/events/values.c +24 -0
  30. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  31. data/vendor/libyeptris/src/yeptris/parse/engine.c +121 -40
  32. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +326 -0
  33. data/vendor/libyeptris/src/yeptris/parse.c +181 -46
  34. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +1 -1
  35. data/vendor/libyeptris/src/yeptris/resolve/core12.c +11 -3
  36. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  37. data/vendor/libyeptris/src/yeptris/resolve/json12.c +12 -6
  38. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +20 -0
  39. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  40. data/vendor/libyeptris/src/yeptris/validate/internal.h +255 -10
  41. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1025 -125
  42. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  43. data/vendor/libyeptris/src/yeptris/validate/regexp.c +791 -163
  44. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  45. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  46. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  47. data/vendor/libyeptris/src/yeptris/validate/validate.c +330 -226
  48. metadata +10 -1
@@ -0,0 +1,1155 @@
1
+ /* stream.c — the event-route validator (#902, TODO.validate.md's
2
+ * stream.c): the family lanes riding the EVENT walk itself.
3
+ *
4
+ * The engine's event stream feeds a streaming state machine — the same
5
+ * family checks the DOM walk runs (through internal.h's fact cores),
6
+ * the same diag machinery, the same report contract — and NO node tree
7
+ * is ever built. The state machine holds per-collection frames (path
8
+ * law, duplicate-key tables, canonical digests) and reproduces the
9
+ * DOM walk's exact observable order:
10
+ *
11
+ * - DIAGNOSTIC ORDER. The DOM walk emits a mapping's own tag check,
12
+ * then ALL of its duplicate-key misses, then the pairs' content —
13
+ * while events deliver duplicates last (a miss is only known when
14
+ * the equal key arrives). Each mapping therefore stages its
15
+ * diagnostics ([own tag][duplicates][pair content]) and flushes at
16
+ * MAP_END; nested collections flush into the parent's content slot
17
+ * at their own close, so chronological order inside a slot is the
18
+ * walk order, and a collection KEY's own law lands at the pair's
19
+ * head position (an insert at the mark the key started at). The
20
+ * report cap applies at the replay — the flush order IS the walk
21
+ * order, so the LIMIT sentinel rides the same slot the DOM walk
22
+ * would have written it in; nothing is dropped before the order is
23
+ * fixed (the staging holds O(violations), the DOM route's own
24
+ * input-bounded class).
25
+ *
26
+ * - STREAM ATTRIBUTION (#885 item 4). A multi-document stream
27
+ * addresses its documents as array elements ("/1/k"), but the
28
+ * qualification (more than one document) is known only when the
29
+ * SECOND DOCUMENT_START arrives — after document 0's diagnostics.
30
+ * Those carry bare pointers until then; the prefix is prepended
31
+ * retroactively (with the path slot's exact truncation), and every
32
+ * later document pushes its index live. Single-document streams
33
+ * keep the bare pointers, exactly like the DOM route.
34
+ *
35
+ * - POSITIONS. The DOM route positions a diagnostic at the node's
36
+ * INPUT-BORROWED value bytes (the DOM builder's region law)
37
+ * resolved through the line index; collections position at their
38
+ * first leaf, a mapping's own tag check included. The event facts
39
+ * carry `borrowed` and the value view, so the offset law is the
40
+ * same predicate the DOM builder applies — the offsets (and thus
41
+ * line/col/offset) agree bit for bit; pooled content (folded,
42
+ * escaped, empty) stays unpositioned (0/0). A collection's tag
43
+ * check defers until its first leaf arrives (or the collection
44
+ * closes empty).
45
+ *
46
+ * - DUPLICATE KEYS. Per-mapping open addressing over canonical
47
+ * digests (spec 3.2.1.3), the digest fold identical to the DOM
48
+ * hash's law (yep_validate_hash_mix + the seed constants). Scalar
49
+ * keys re-compare under the DOM's exact equality law over stored
50
+ * byte snapshots; collection keys compare digests (equal structures
51
+ * fold equally; unequal ones practically do — the DOM's structural
52
+ * equality remains the reference, this is the documented bound of
53
+ * the streaming twin). Alias keys resolve their target through an
54
+ * anchor table filled at subtree close (an alias to an OPEN
55
+ * collection — legal under the engine's compose law — has no digest
56
+ * yet; the fingerprint falls back to the unbound form, which the
57
+ * DOM's capped recursive fold cannot reproduce: documented corner).
58
+ *
59
+ * MECE: no validation logic lives here beyond the fact plumbing — the
60
+ * verdicts come from the fact cores in validate.c (a verdict change
61
+ * lands once and both routes move together); the parse rides
62
+ * parse.c's sink-entry seam (the DOM route's front-end and statuses,
63
+ * one home); the sink sets `compose` so the engine's undefined-alias
64
+ * law matches the DOM route's parse statuses. The compiled JSON
65
+ * Schema lane keeps the DOM route (its evaluator needs random access
66
+ * — uniqueItems, contains, required; a streaming twin waits for
67
+ * #901's annotation-model seam, exactly as the issue sequences it).
68
+ */
69
+
70
+ #include <stdio.h>
71
+ #include <stdlib.h>
72
+ #include <string.h>
73
+
74
+ #include <yeptris/validate.h>
75
+
76
+ #include "../common/error.h"
77
+ #include "../doc.h"
78
+ #include "../memory/allocator.h"
79
+ #include "../parse/events.h"
80
+ #include "../parse/numbers.h"
81
+ #include "../resolve/resolver.h"
82
+ #include "internal.h"
83
+
84
+ /* ---- the facts ---------------------------------------------------------- */
85
+
86
+ /* The five facts the family checks read, per node — the DOM walk
87
+ * extracts them from yep_dnode, this walk from the engine's events. */
88
+ typedef struct {
89
+ int kind; /* yep_dom_kind */
90
+ int has_tag; /* an explicit tag rode the event */
91
+ uint8_t tag_id; /* the resolver's verdict (emit_now's SSOT) */
92
+ int plainish; /* PLAIN style or the implicit flag */
93
+ yep_view value; /* scalar content / alias name ({NULL, 0} when empty) */
94
+ uint32_t off; /* the input-borrowed value offset; UINT32_MAX =
95
+ * unpositioned (the DOM route's node_off law) */
96
+ uint32_t anchor_id;
97
+ } yep_vs_facts;
98
+
99
+ /* A duplicate-table entry: the 32-bit probe hash gates, the canonical
100
+ * digest identifies; scalar keys keep a byte snapshot so the equality
101
+ * re-runs the DOM's exact law. Collection keys compare digests. */
102
+ typedef struct {
103
+ uint32_t h32;
104
+ uint64_t digest;
105
+ uint8_t used;
106
+ uint8_t is_scalar;
107
+ uint8_t tag;
108
+ uint32_t blen;
109
+ char* bytes; /* scalar snapshot (heap; freed with the table) */
110
+ uint32_t off;
111
+ } yep_vs_keyent;
112
+
113
+ /* One open collection. */
114
+ typedef struct {
115
+ uint8_t kind; /* YEP_DOM_SEQUENCE / YEP_DOM_MAPPING */
116
+ uint8_t role; /* 0 = value/root; 1 = the direct KEY of a
117
+ * mapping (transparent: no own tag check, no
118
+ * path segment — the DOM's walk_children law) */
119
+ uint8_t pair_slot; /* mapping: 0 = key expected, 1 = value expected
120
+ * (the key's path segment is on the scratch) */
121
+ uint32_t seq_index;
122
+ size_t path_mark;
123
+ size_t pair_tail_mark; /* role 1: the parent tail's length at the
124
+ * key's start (the pair-head insert point) */
125
+ /* deferred own-tag check: collections position at their first leaf */
126
+ int has_tag;
127
+ uint8_t tag_id;
128
+ int first_seen;
129
+ uint32_t first_off;
130
+ /* the emission stage ([own tag][duplicates][pair content]) */
131
+ yeptris_diagnostic* head;
132
+ uint32_t head_n;
133
+ size_t head_cap;
134
+ yeptris_diagnostic* dups;
135
+ uint32_t dups_n;
136
+ size_t dups_cap;
137
+ yeptris_diagnostic* tail;
138
+ uint32_t tail_n;
139
+ size_t tail_cap;
140
+ /* the duplicate table (opens on the second key — a one-key mapping
141
+ * can never miss, the DOM walk's pairs<2 law) */
142
+ int have_pending;
143
+ yep_vs_keyent pending;
144
+ yep_vs_keyent* tbl;
145
+ uint32_t tbl_slots; /* power of two; 0 = unopened */
146
+ uint32_t tbl_n;
147
+ /* the canonical digest (spec 3.2.1.3) — keys and anchor records */
148
+ uint64_t digest;
149
+ uint64_t pair_key_digest; /* valid while pair_slot == 1 */
150
+ uint32_t anchor_id;
151
+ } yep_vs_frame;
152
+
153
+ /* An anchor's target: opened when the anchored node starts (the kind),
154
+ * completed when its subtree closes (digest + the key-check facts).
155
+ * The value view borrows the run's bytes — read only during the run. */
156
+ typedef struct {
157
+ uint8_t kind;
158
+ uint8_t closed;
159
+ uint8_t has_tag;
160
+ uint8_t tag_id;
161
+ uint8_t style;
162
+ uint8_t implicit;
163
+ yep_view value;
164
+ uint32_t off; /* the node_off law: own value or first leaf */
165
+ uint64_t digest;
166
+ int digest_known;
167
+ } yep_vs_anchor;
168
+
169
+ typedef struct yep_vstream {
170
+ yep_vctx ctx;
171
+ const yep_resolver* fam;
172
+ int json_family;
173
+ const char* input_base; /* the run's borrowed-view base (dies with
174
+ * the run; nothing reads it after) */
175
+ yep_vs_frame* frames;
176
+ int depth, frames_cap;
177
+ int maps_open; /* the stage hook rides this (diagnostics stage
178
+ * exactly while a mapping is open) */
179
+ int depth_diag; /* the walk guard's one-shot */
180
+ uint32_t docs_seen;
181
+ int qualified; /* a second document arrived: prefixes ride live */
182
+ yep_vs_anchor* anchors;
183
+ uint32_t anchors_cap; /* slots; ordinal i lives at i-1 */
184
+ } yep_vstream;
185
+
186
+ /* The stage hooks. yep_validate_emit composes and then hands the
187
+ * diagnostic to ctx.stage_put; three writers exist:
188
+ * - vs_stage_put: the innermost open mapping's CONTENT slot
189
+ * (chronological = the walk order inside it);
190
+ * - vs_capture_*: a SPECIFIC frame's own-tag (head) or duplicates
191
+ * slot, for the deferred tag fire and dup misses.
192
+ * With no mapping open the hook is NULL and emit takes the report
193
+ * path — one home for the cap law (the DOM route's exact shape). */
194
+ /* The stage hooks' capture: a frame plus, for the pair-head insert,
195
+ * the parent tail mark the pair's key started at. */
196
+ typedef struct {
197
+ yep_vstream* v;
198
+ yep_vs_frame* f;
199
+ size_t mark;
200
+ } yep_vs_capture;
201
+
202
+ /* ---- stage arrays (alloc + copy + free; no realloc) ---------------------- */
203
+
204
+ static int vs_stage_put_array(yep_vctx* c, yeptris_diagnostic** arr, uint32_t n, size_t* cap,
205
+ const yeptris_diagnostic* d) {
206
+ if ((size_t)n + 1 > *cap) {
207
+ size_t ncap = *cap ? *cap * 2 : 8;
208
+ yeptris_diagnostic* na = yep_alloc(yep_system_allocator(), ncap * sizeof(*na));
209
+ if (na == NULL) {
210
+ c->oom = 1;
211
+ return -1;
212
+ }
213
+ if (*arr != NULL) {
214
+ memcpy(na, *arr, (size_t)n * sizeof(*na));
215
+ yep_free(yep_system_allocator(), *arr);
216
+ }
217
+ *arr = na;
218
+ *cap = ncap;
219
+ }
220
+ (*arr)[n] = *d;
221
+ return 0;
222
+ }
223
+
224
+ /* The default hook: the diagnostic joins the INNERMOST OPEN mapping's
225
+ * content slot (everything inside it, chronologically = walk order). */
226
+ static void vs_stage_put(void* stage, const yeptris_diagnostic* d) {
227
+ yep_vstream* v = stage;
228
+ yep_vs_frame* m = NULL;
229
+ for (int i = v->depth - 1; i >= 0; i--) {
230
+ if (v->frames[i].kind == YEP_DOM_MAPPING) {
231
+ m = &v->frames[i];
232
+ break;
233
+ }
234
+ }
235
+ if (m == NULL) {
236
+ return; /* unreachable: the hook is up only under a mapping */
237
+ }
238
+ if (vs_stage_put_array(&v->ctx, &m->tail, m->tail_n, &m->tail_cap, d) == 0) {
239
+ m->tail_n++;
240
+ }
241
+ }
242
+
243
+ static void vs_capture_head(void* stage, const yeptris_diagnostic* d) {
244
+ yep_vs_capture* cap = stage;
245
+ if (vs_stage_put_array(&cap->v->ctx, &cap->f->head, cap->f->head_n, &cap->f->head_cap, d) ==
246
+ 0) {
247
+ cap->f->head_n++;
248
+ }
249
+ }
250
+
251
+ static void vs_capture_dups(void* stage, const yeptris_diagnostic* d) {
252
+ yep_vs_capture* cap = stage;
253
+ if (vs_stage_put_array(&cap->v->ctx, &cap->f->dups, cap->f->dups_n, &cap->f->dups_cap, d) ==
254
+ 0) {
255
+ cap->f->dups_n++;
256
+ }
257
+ }
258
+
259
+ /* A collection KEY's own json12 law lands at the pair's HEAD position:
260
+ * the DOM walk emits it before the key subtree's walk, whose content
261
+ * already sits in the parent's tail — the insert re-creates that
262
+ * order. The mark rides the capture (the parent tail's length at the
263
+ * key's start). */
264
+ static void vs_capture_pairhead(void* stage, const yeptris_diagnostic* d) {
265
+ yep_vs_capture* cap = stage;
266
+ yep_vs_frame* m = cap->f;
267
+ size_t mark = cap->mark;
268
+ if (mark > m->tail_n) {
269
+ return; /* defensive: the anchor point vanished */
270
+ }
271
+ if ((size_t)m->tail_n + 1 > m->tail_cap) {
272
+ size_t ncap = m->tail_cap ? m->tail_cap * 2 : 8;
273
+ yeptris_diagnostic* na = yep_alloc(yep_system_allocator(), ncap * sizeof(*na));
274
+ if (na == NULL) {
275
+ cap->v->ctx.oom = 1;
276
+ return;
277
+ }
278
+ if (m->tail != NULL) {
279
+ memcpy(na, m->tail, (size_t)m->tail_n * sizeof(*na));
280
+ yep_free(yep_system_allocator(), m->tail);
281
+ }
282
+ m->tail = na;
283
+ m->tail_cap = ncap;
284
+ }
285
+ memmove(m->tail + mark + 1, m->tail + mark, (size_t)(m->tail_n - mark) * sizeof(*m->tail));
286
+ m->tail[mark] = *d;
287
+ m->tail_n++;
288
+ }
289
+
290
+ /* Replays one stage slot through the report path (the hook is down;
291
+ * the staged diagnostics are RESOLVED — they were composed at walk
292
+ * time — so they append verbatim through the cap-law writer). */
293
+ static void vs_replay(yep_vstream* v, const yeptris_diagnostic* arr, uint32_t n) {
294
+ v->ctx.stage_put = NULL;
295
+ for (uint32_t i = 0; i < n; i++) {
296
+ yep_validate_report_put(&v->ctx, &arr[i]);
297
+ }
298
+ v->ctx.stage_put = vs_stage_put;
299
+ }
300
+
301
+ /* Flushes the mapping's stage: into the ENCLOSING mapping's content
302
+ * slot (the flattened [own tag][duplicates][content] joins the parent
303
+ * tail at this close position — chronological = walk order), or to
304
+ * the report when no mapping encloses it. */
305
+ static void vs_flush_stage(yep_vstream* v, yep_vs_frame* f) {
306
+ yep_vs_frame* m = NULL;
307
+ for (int i = v->depth - 2; i >= 0; i--) {
308
+ if (v->frames[i].kind == YEP_DOM_MAPPING) {
309
+ m = &v->frames[i];
310
+ break;
311
+ }
312
+ }
313
+ if (m == NULL) {
314
+ vs_replay(v, f->head, f->head_n);
315
+ vs_replay(v, f->dups, f->dups_n);
316
+ vs_replay(v, f->tail, f->tail_n);
317
+ v->ctx.staged -= (size_t)f->head_n + f->dups_n + f->tail_n;
318
+ } else {
319
+ const yeptris_diagnostic* slots[3] = {f->head, f->dups, f->tail};
320
+ uint32_t ns[3] = {f->head_n, f->dups_n, f->tail_n};
321
+ for (int s = 0; s < 3; s++) {
322
+ for (uint32_t i = 0; i < ns[s]; i++) {
323
+ if (vs_stage_put_array(&v->ctx, &m->tail, m->tail_n, &m->tail_cap, &slots[s][i]) !=
324
+ 0) {
325
+ break;
326
+ }
327
+ m->tail_n++;
328
+ }
329
+ }
330
+ }
331
+ const yep_allocator* sys = yep_system_allocator();
332
+ yep_free(sys, f->head);
333
+ yep_free(sys, f->dups);
334
+ yep_free(sys, f->tail);
335
+ f->head = NULL;
336
+ f->dups = NULL;
337
+ f->tail = NULL;
338
+ f->head_n = f->dups_n = f->tail_n = 0;
339
+ f->head_cap = f->dups_cap = f->tail_cap = 0;
340
+ }
341
+
342
+ /* ---- frames -------------------------------------------------------------- */
343
+
344
+ static yep_vs_frame* vs_push(yep_vstream* v, uint8_t kind, uint8_t role, const yep_event* ev) {
345
+ if (v->depth == v->frames_cap) {
346
+ int ncap = v->frames_cap ? v->frames_cap * 2 : 32;
347
+ yep_vs_frame* nf = yep_alloc(yep_system_allocator(), (size_t)ncap * sizeof(*nf));
348
+ if (nf == NULL) {
349
+ v->ctx.oom = 1;
350
+ return NULL;
351
+ }
352
+ if (v->frames != NULL) {
353
+ memcpy(nf, v->frames, (size_t)v->depth * sizeof(*nf));
354
+ yep_free(yep_system_allocator(), v->frames);
355
+ }
356
+ v->frames = nf;
357
+ v->frames_cap = ncap;
358
+ }
359
+ if (v->depth >= YEP_DOM_MAX_DEPTH) {
360
+ /* the engine's depth cap fires first (same constant): the
361
+ * walk guard's defensive twin, one diagnostic like the DOM */
362
+ if (!v->depth_diag) {
363
+ v->depth_diag = 1;
364
+ yep_validate_emit(&v->ctx, YEPTRIS_DIAG_DEPTH, UINT32_MAX,
365
+ "validation walk exceeded the nesting limit");
366
+ }
367
+ return NULL;
368
+ }
369
+ yep_vs_frame* f = &v->frames[v->depth++];
370
+ memset(f, 0, sizeof(*f));
371
+ f->kind = kind;
372
+ f->role = role;
373
+ f->path_mark = v->ctx.path_len;
374
+ f->first_off = UINT32_MAX;
375
+ f->has_tag = role == 0 && ev->tag.len > 0; /* a KEY-root collection
376
+ * never checks its own
377
+ * tag (the DOM law) */
378
+ f->tag_id = ev->tag_id;
379
+ f->anchor_id = ev->anchor_id;
380
+ f->digest = kind == YEP_DOM_SEQUENCE ? YEP_VAL_DIGEST_SEQ_SEED : YEP_VAL_DIGEST_MAP_SEED;
381
+ if (kind == YEP_DOM_MAPPING) {
382
+ v->maps_open++;
383
+ }
384
+ return f;
385
+ }
386
+
387
+ static void vs_pop(yep_vstream* v) {
388
+ yep_vs_frame* f = &v->frames[--v->depth];
389
+ if (f->kind == YEP_DOM_MAPPING) {
390
+ v->maps_open--;
391
+ }
392
+ const yep_allocator* sys = yep_system_allocator();
393
+ yep_free(sys, f->head);
394
+ yep_free(sys, f->dups);
395
+ yep_free(sys, f->tail);
396
+ /* the entries live at their hash slots: sweep the whole table */
397
+ for (uint32_t i = 0; i < f->tbl_slots; i++) {
398
+ yep_free(sys, f->tbl[i].bytes);
399
+ }
400
+ yep_free(sys, f->pending.bytes);
401
+ yep_free(sys, f->tbl);
402
+ }
403
+
404
+ /* ---- anchors ------------------------------------------------------------- */
405
+
406
+ static yep_vs_anchor* vs_anchor(yep_vstream* v, uint32_t ordinal) {
407
+ if (ordinal == 0) {
408
+ return NULL;
409
+ }
410
+ if (ordinal > v->anchors_cap) {
411
+ /* more anchors than input bytes is a bug, not a document (the
412
+ * DOM table's law); the cap keeps a hostile stream bounded */
413
+ if ((size_t)ordinal > (size_t)v->ctx.input_len + 16) {
414
+ return NULL;
415
+ }
416
+ uint32_t ncap = v->anchors_cap ? v->anchors_cap * 2 : 64;
417
+ while (ncap < ordinal) {
418
+ ncap *= 2;
419
+ }
420
+ yep_vs_anchor* na = yep_alloc(yep_system_allocator(), (size_t)ncap * sizeof(*na));
421
+ if (na == NULL) {
422
+ v->ctx.oom = 1;
423
+ return NULL;
424
+ }
425
+ if (v->anchors != NULL) {
426
+ memcpy(na, v->anchors, (size_t)v->anchors_cap * sizeof(*na));
427
+ yep_free(yep_system_allocator(), v->anchors);
428
+ }
429
+ v->anchors = na;
430
+ v->anchors_cap = ncap;
431
+ }
432
+ yep_vs_anchor* a = &v->anchors[ordinal - 1];
433
+ memset(a, 0, sizeof(*a));
434
+ return a;
435
+ }
436
+
437
+ static yep_vs_anchor* vs_anchor_at(const yep_vstream* v, uint32_t ordinal) {
438
+ if (ordinal == 0 || ordinal > v->anchors_cap) {
439
+ return NULL;
440
+ }
441
+ return &v->anchors[ordinal - 1];
442
+ }
443
+
444
+ /* ---- facts --------------------------------------------------------------- */
445
+
446
+ /* The DOM route's region law (the DOM builder's dom_str_in predicate),
447
+ * applied to the event's value view: input-borrowed, non-empty, inside
448
+ * the run's base. The offset (and therefore line/col/offset) matches
449
+ * the DOM walk's bit for bit; pooled content stays unpositioned. */
450
+ static uint32_t vs_off_of(const yep_vstream* v, const yep_view* value) {
451
+ if (value->len > 0 && value->p != NULL && v->input_base != NULL &&
452
+ (const char*)value->p >= v->input_base) {
453
+ return (uint32_t)((const char*)value->p - v->input_base);
454
+ }
455
+ return UINT32_MAX;
456
+ }
457
+
458
+ static void vs_facts_of(const yep_vstream* v, const yep_event* ev, yep_vs_facts* f) {
459
+ memset(f, 0, sizeof(*f));
460
+ f->kind = ev->type == YEP_EV_SCALAR ? (int)YEP_DOM_SCALAR : (int)YEP_DOM_ALIAS;
461
+ f->has_tag = ev->tag.len > 0;
462
+ f->tag_id = ev->tag_id;
463
+ f->plainish = ev->implicit || ev->style == YEP_STYLE_PLAIN;
464
+ f->value.p = ev->value.len ? ev->value.p : NULL;
465
+ f->value.len = ev->value.len;
466
+ f->off = vs_off_of(v, &ev->value);
467
+ f->anchor_id = ev->anchor_id;
468
+ }
469
+
470
+ /* ---- the first-leaf law -------------------------------------------------- */
471
+
472
+ /* The DFS-first leaf of every open collection is this leaf (stream
473
+ * order is document order): mark the ancestors that have not seen a
474
+ * leaf yet and fire their pending tag checks, outermost first — the
475
+ * DOM walk's enter order. A mapping's fire lands in its own head slot
476
+ * (capture); a sequence's rides the current stage (chronological:
477
+ * nothing of any enclosing content precedes the shared first leaf). */
478
+ static void vs_first_leaf(yep_vstream* v, const yep_vs_facts* f) {
479
+ for (int i = 0; i < v->depth; i++) {
480
+ yep_vs_frame* fr = &v->frames[i];
481
+ if (fr->first_seen) {
482
+ continue;
483
+ }
484
+ fr->first_seen = 1;
485
+ fr->first_off = f->off;
486
+ if (!fr->has_tag) {
487
+ continue;
488
+ }
489
+ fr->has_tag = 0;
490
+ if (fr->kind == YEP_DOM_MAPPING) {
491
+ yep_vs_capture cap = {v, fr, 0};
492
+ void (*save_put)(void*, const yeptris_diagnostic*) = v->ctx.stage_put;
493
+ void* save_stage = v->ctx.stage;
494
+ v->ctx.stage_put = vs_capture_head;
495
+ v->ctx.stage = &cap;
496
+ yep_check_tag_facts(&v->ctx, (int)fr->kind, 1, fr->tag_id, (yep_view){NULL, 0}, f->off,
497
+ v->fam, v->json_family);
498
+ v->ctx.stage_put = save_put;
499
+ v->ctx.stage = save_stage;
500
+ } else {
501
+ yep_check_tag_facts(&v->ctx, (int)fr->kind, 1, fr->tag_id, (yep_view){NULL, 0}, f->off,
502
+ v->fam, v->json_family);
503
+ }
504
+ }
505
+ }
506
+
507
+ /* ---- checks --------------------------------------------------------------- */
508
+
509
+ static void vs_checks_scalar(yep_vstream* v, const yep_vs_facts* f) {
510
+ if (v->ctx.oom) {
511
+ return;
512
+ }
513
+ yep_check_tag_facts(&v->ctx, f->kind, f->has_tag, f->tag_id, f->value, f->off, v->fam,
514
+ v->json_family);
515
+ if (v->json_family) {
516
+ yep_check_json_scalar_facts(&v->ctx, f->has_tag, f->tag_id, f->plainish, f->value, f->off);
517
+ }
518
+ }
519
+
520
+ /* A scalar KEY's checks at the mapping's path (the DOM pair order:
521
+ * the json12 key law first, then the tag check). */
522
+ static void vs_checks_key_scalar(yep_vstream* v, const yep_vs_facts* f) {
523
+ if (v->ctx.oom) {
524
+ return;
525
+ }
526
+ if (v->json_family) {
527
+ yep_check_json_key_facts(&v->ctx, f->has_tag, f->tag_id, f->plainish, f->value, f->off);
528
+ }
529
+ yep_check_tag_facts(&v->ctx, (int)YEP_DOM_SCALAR, f->has_tag, f->tag_id, f->value, f->off,
530
+ v->fam, v->json_family);
531
+ }
532
+
533
+ /* ---- the canonical digests ----------------------------------------------- */
534
+
535
+ static uint64_t vs_digest_scalar(const yep_vs_facts* f) {
536
+ return yep_validate_scalar_digest(yep_validate_tag_of(f->tag_id, f->has_tag, f->plainish),
537
+ f->value);
538
+ }
539
+
540
+ /* ---- duplicate keys ------------------------------------------------------- */
541
+
542
+ /* The DOM walk's scalar equality law over stored facts (the number
543
+ * arms compare by value across the int/float tags; everything else
544
+ * wants the same tag and equal bytes). */
545
+ static int vs_key_equal(const yep_vs_keyent* e, uint8_t tag, const char* p, uint32_t len) {
546
+ int numa = e->tag == YEPTRIS_TAG_INT || e->tag == YEPTRIS_TAG_FLOAT;
547
+ int numb = tag == YEPTRIS_TAG_INT || tag == YEPTRIS_TAG_FLOAT;
548
+ if (numa != numb) {
549
+ return 0;
550
+ }
551
+ if (numa) {
552
+ int64_t ia, ib;
553
+ if (yep_num_i64(e->bytes, e->blen, &ia) == 0 && yep_num_i64(p, len, &ib) == 0) {
554
+ return ia == ib;
555
+ }
556
+ double fa, fb;
557
+ if (yep_num_f64(e->bytes, e->blen, &fa) == 0 && yep_num_f64(p, len, &fb) == 0) {
558
+ return fa == fb;
559
+ }
560
+ return 0;
561
+ }
562
+ if (e->tag != tag) {
563
+ return 0;
564
+ }
565
+ return e->blen == len && (len == 0 || memcmp(e->bytes, p, len) == 0);
566
+ }
567
+
568
+ /* The dup diagnostic rides the mapping's duplicates slot (the DOM walk
569
+ * emits the whole table before the pairs). */
570
+ static void vs_dup_emit(yep_vstream* v, yep_vs_frame* f, const yep_vs_facts* kf) {
571
+ yep_vs_capture cap = {v, f, 0};
572
+ void (*save_put)(void*, const yeptris_diagnostic*) = v->ctx.stage_put;
573
+ void* save_stage = v->ctx.stage;
574
+ v->ctx.stage_put = vs_capture_dups;
575
+ v->ctx.stage = &cap;
576
+ char key[40];
577
+ size_t kl = kf->value.len < sizeof(key) - 8 ? kf->value.len : sizeof(key) - 8;
578
+ if (kl > 0 && kf->value.p != NULL) {
579
+ memcpy(key, kf->value.p, kl);
580
+ }
581
+ key[kl] = '\0';
582
+ yep_validate_emit(&v->ctx, YEPTRIS_DIAG_DUPLICATE_KEY, kf->off,
583
+ "duplicate mapping key (\"%s\")", key);
584
+ v->ctx.stage_put = save_put;
585
+ v->ctx.stage = save_stage;
586
+ }
587
+
588
+ /* Opens the probe table on the second key (the pending first entry
589
+ * rides along); NULL on allocation failure (ctx.oom set). The pending
590
+ * entry's snapshot moves into the table (the frame's copy is zeroed —
591
+ * one owner per snapshot). */
592
+ static yep_vs_keyent* vs_tbl_open(yep_vstream* v, yep_vs_frame* f) {
593
+ f->tbl_slots = 8;
594
+ f->tbl = yep_alloc(yep_system_allocator(), f->tbl_slots * sizeof(*f->tbl));
595
+ if (f->tbl == NULL) {
596
+ v->ctx.oom = 1;
597
+ return NULL;
598
+ }
599
+ memset(f->tbl, 0, f->tbl_slots * sizeof(*f->tbl));
600
+ f->tbl[f->pending.h32 & (f->tbl_slots - 1)] = f->pending;
601
+ memset(&f->pending, 0, sizeof(f->pending));
602
+ f->tbl_n = 1;
603
+ f->have_pending = 0;
604
+ return f->tbl;
605
+ }
606
+
607
+ /* Probes (and inserts) one key entry. The first key parks in the
608
+ * frame slot; the second opens the table (the pending entry moves in)
609
+ * and the probe runs against it — a one-key mapping can never miss,
610
+ * and once the table exists every key probes it. */
611
+ static void vs_dup_probe(yep_vstream* v, yep_vs_frame* f, const yep_vs_keyent* e,
612
+ const yep_vs_facts* kf) {
613
+ if (f->tbl == NULL) {
614
+ if (!f->have_pending) {
615
+ f->pending = *e; /* the snapshot moves with the entry */
616
+ f->have_pending = 1;
617
+ return;
618
+ }
619
+ if (vs_tbl_open(v, f) == NULL) {
620
+ yep_free(yep_system_allocator(), e->bytes);
621
+ return;
622
+ }
623
+ }
624
+ uint32_t i = e->h32 & (f->tbl_slots - 1);
625
+ while (f->tbl[i].used) {
626
+ const yep_vs_keyent* s = &f->tbl[i];
627
+ int hit = 0;
628
+ if (s->h32 == e->h32) {
629
+ if (s->is_scalar && e->is_scalar) {
630
+ hit = vs_key_equal(s, e->tag, e->bytes, e->blen);
631
+ } else if (!s->is_scalar && !e->is_scalar) {
632
+ hit = s->digest == e->digest;
633
+ }
634
+ /* mixed kinds: the DOM's kind mismatch answers 0 */
635
+ }
636
+ if (hit) {
637
+ /* enumerate EVERY miss; the equal key does not insert */
638
+ vs_dup_emit(v, f, kf);
639
+ yep_free(yep_system_allocator(), e->bytes);
640
+ return;
641
+ }
642
+ i = (i + 1) & (f->tbl_slots - 1);
643
+ }
644
+ /* grow at 3/4 (the DOM walk's cap<4*pairs shape) */
645
+ if ((f->tbl_n + 1) * 4 > f->tbl_slots * 3) {
646
+ uint32_t nslots = f->tbl_slots * 2;
647
+ yep_vs_keyent* nt = yep_alloc(yep_system_allocator(), nslots * sizeof(*nt));
648
+ if (nt == NULL) {
649
+ yep_free(yep_system_allocator(), e->bytes);
650
+ v->ctx.oom = 1;
651
+ return;
652
+ }
653
+ memset(nt, 0, nslots * sizeof(*nt));
654
+ for (uint32_t j = 0; j < f->tbl_slots; j++) {
655
+ if (f->tbl[j].used) {
656
+ uint32_t k = f->tbl[j].h32 & (nslots - 1);
657
+ while (nt[k].used) {
658
+ k = (k + 1) & (nslots - 1);
659
+ }
660
+ nt[k] = f->tbl[j];
661
+ }
662
+ }
663
+ yep_free(yep_system_allocator(), f->tbl);
664
+ f->tbl = nt;
665
+ f->tbl_slots = nslots;
666
+ i = e->h32 & (nslots - 1);
667
+ while (f->tbl[i].used) {
668
+ i = (i + 1) & (f->tbl_slots - 1);
669
+ }
670
+ }
671
+ f->tbl[i] = *e;
672
+ f->tbl_n++;
673
+ }
674
+
675
+ /* Builds a scalar key's table entry from the facts. */
676
+ static void vs_dup_scalar(yep_vstream* v, yep_vs_frame* f, const yep_vs_facts* kf,
677
+ uint64_t digest) {
678
+ yep_vs_keyent e;
679
+ memset(&e, 0, sizeof(e));
680
+ e.h32 = (uint32_t)(digest ^ (digest >> 32));
681
+ e.digest = digest;
682
+ e.used = 1;
683
+ e.is_scalar = 1;
684
+ e.tag = yep_validate_tag_of(kf->tag_id, kf->has_tag, kf->plainish);
685
+ e.blen = kf->value.len;
686
+ e.off = kf->off;
687
+ if (kf->value.len > 0) {
688
+ e.bytes = yep_alloc(yep_system_allocator(), kf->value.len);
689
+ if (e.bytes == NULL) {
690
+ v->ctx.oom = 1;
691
+ return;
692
+ }
693
+ memcpy(e.bytes, kf->value.p, kf->value.len);
694
+ }
695
+ vs_dup_probe(v, f, &e, kf);
696
+ }
697
+
698
+ /* Alias keys resolve their target through the anchor table (the DOM
699
+ * hash follows the target node). An OPEN target has no digest yet —
700
+ * the documented corner (see the file comment). */
701
+ static void vs_dup_alias(yep_vstream* v, yep_vs_frame* f, const yep_vs_facts* kf) {
702
+ yep_vs_anchor* a = vs_anchor_at(v, kf->anchor_id);
703
+ uint64_t digest = a != NULL && a->digest_known
704
+ ? a->digest
705
+ : yep_validate_hash_mix(YEP_VAL_DIGEST_ALIAS, kf->value.len);
706
+ yep_vs_keyent e;
707
+ memset(&e, 0, sizeof(e));
708
+ e.h32 = (uint32_t)(digest ^ (digest >> 32));
709
+ e.digest = digest;
710
+ e.used = 1;
711
+ e.is_scalar = 0;
712
+ e.off = kf->off;
713
+ vs_dup_probe(v, f, &e, kf);
714
+ }
715
+
716
+ /* ---- path ----------------------------------------------------------------- */
717
+
718
+ /* Pushes the key's path segment (its raw value bytes; the alias's
719
+ * name; collections contribute the empty segment — the DOM's law). */
720
+ static int vs_push_key_segment(yep_vstream* v, const yep_vs_facts* kf) {
721
+ return yep_validate_path_push(&v->ctx, kf->value.p != NULL ? kf->value.p : "", kf->value.len);
722
+ }
723
+
724
+ /* ---- events ---------------------------------------------------------------- */
725
+
726
+ static void vs_event_scalar(yep_vstream* v, const yep_event* ev) {
727
+ yep_vs_facts f;
728
+ vs_facts_of(v, ev, &f);
729
+ vs_first_leaf(v, &f);
730
+ if (v->ctx.oom) {
731
+ return;
732
+ }
733
+ uint64_t digest = vs_digest_scalar(&f);
734
+ yep_vs_frame* top = v->depth ? &v->frames[v->depth - 1] : NULL;
735
+ if (top == NULL) {
736
+ /* document root */
737
+ vs_checks_scalar(v, &f);
738
+ } else if (top->kind == YEP_DOM_SEQUENCE) {
739
+ /* a KEY-root sequence's children walk flat (no index) */
740
+ int pushed = top->role == 0 && yep_validate_path_push_index(&v->ctx, top->seq_index++) == 0;
741
+ vs_checks_scalar(v, &f);
742
+ if (pushed) {
743
+ yep_validate_path_pop(&v->ctx, top->path_mark);
744
+ }
745
+ top->digest = yep_validate_hash_mix(top->digest, digest);
746
+ } else if (top->pair_slot == 0) {
747
+ /* the KEY. A KEY-root mapping (role 1) walks its children the
748
+ * flat walk_children way: the value-arm checks, no key law,
749
+ * no dup table (its map arm never runs in the DOM), no path
750
+ * segments, and the pair folds still account. */
751
+ if (top->role == 1) {
752
+ vs_checks_scalar(v, &f);
753
+ } else {
754
+ vs_checks_key_scalar(v, &f);
755
+ vs_dup_scalar(v, top, &f, digest);
756
+ if (vs_push_key_segment(v, &f) != 0) {
757
+ /* OOM flagged; the value still streams at the same depth */
758
+ }
759
+ }
760
+ top->pair_slot = 1;
761
+ top->pair_key_digest = digest;
762
+ } else {
763
+ /* the VALUE: checks at the key's path, then the pair folds */
764
+ vs_checks_scalar(v, &f);
765
+ uint64_t pair = yep_validate_hash_mix(YEP_VAL_DIGEST_PAIR_SEED, top->pair_key_digest);
766
+ pair = yep_validate_hash_mix(pair, digest);
767
+ top->digest ^= pair;
768
+ if (top->role == 0) {
769
+ yep_validate_path_pop(&v->ctx, top->path_mark);
770
+ }
771
+ top->pair_slot = 0;
772
+ }
773
+ if (ev->anchor_id != 0) {
774
+ yep_vs_anchor* a = vs_anchor(v, ev->anchor_id);
775
+ if (a != NULL) {
776
+ a->kind = YEP_DOM_SCALAR;
777
+ a->closed = 1;
778
+ a->has_tag = (uint8_t)f.has_tag;
779
+ a->tag_id = f.tag_id;
780
+ a->style = (uint8_t)ev->style;
781
+ a->implicit = (uint8_t)ev->implicit;
782
+ a->value = f.value;
783
+ a->off = f.off;
784
+ a->digest = digest;
785
+ a->digest_known = 1;
786
+ }
787
+ }
788
+ }
789
+
790
+ static void vs_event_alias(yep_vstream* v, const yep_event* ev) {
791
+ yep_vs_facts f;
792
+ vs_facts_of(v, ev, &f);
793
+ vs_first_leaf(v, &f);
794
+ if (v->ctx.oom) {
795
+ return;
796
+ }
797
+ yep_vs_anchor* a = vs_anchor_at(v, f.anchor_id);
798
+ uint64_t digest = a != NULL && a->digest_known
799
+ ? a->digest
800
+ : yep_validate_hash_mix(YEP_VAL_DIGEST_ALIAS, f.value.len);
801
+ yep_vs_frame* top = v->depth ? &v->frames[v->depth - 1] : NULL;
802
+ if (top == NULL) {
803
+ /* alias root: the DOM walk checks nothing at the alias */
804
+ } else if (top->kind == YEP_DOM_SEQUENCE) {
805
+ int pushed = top->role == 0 && yep_validate_path_push_index(&v->ctx, top->seq_index++) == 0;
806
+ if (pushed) {
807
+ yep_validate_path_pop(&v->ctx, top->path_mark);
808
+ }
809
+ top->digest = yep_validate_hash_mix(top->digest, digest);
810
+ } else if (top->pair_slot == 0) {
811
+ /* the KEY. Role 1 (inside a KEY-root collection) walks flat:
812
+ * the ALIAS arm checks nothing, no segment, folds still run. */
813
+ if (top->role == 0) {
814
+ /* the json12 law resolves the TARGET's facts (the DOM
815
+ * recursion lands on the target node); no tag check */
816
+ if (v->json_family && a != NULL) {
817
+ if (a->kind != YEP_DOM_SCALAR) {
818
+ yep_validate_emit(&v->ctx, YEPTRIS_DIAG_KEY_NOT_STRING, a->off,
819
+ "mapping key is a %s",
820
+ a->kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
821
+ } else if (a->closed) {
822
+ yep_check_json_key_facts(&v->ctx, a->has_tag, a->tag_id,
823
+ a->implicit || a->style == YEP_STYLE_PLAIN, a->value,
824
+ a->off);
825
+ }
826
+ }
827
+ vs_dup_alias(v, top, &f);
828
+ if (vs_push_key_segment(v, &f) != 0) {
829
+ /* OOM flagged; the value still streams */
830
+ }
831
+ }
832
+ top->pair_slot = 1;
833
+ top->pair_key_digest = digest;
834
+ } else {
835
+ /* the VALUE: nothing (the target is validated at its place) */
836
+ uint64_t pair = yep_validate_hash_mix(YEP_VAL_DIGEST_PAIR_SEED, top->pair_key_digest);
837
+ pair = yep_validate_hash_mix(pair, digest);
838
+ top->digest ^= pair;
839
+ if (top->role == 0) {
840
+ yep_validate_path_pop(&v->ctx, top->path_mark);
841
+ }
842
+ top->pair_slot = 0;
843
+ }
844
+ }
845
+
846
+ static void vs_event_collection_start(yep_vstream* v, const yep_event* ev, uint8_t kind) {
847
+ yep_vs_frame* top = v->depth ? &v->frames[v->depth - 1] : NULL;
848
+ uint8_t role = 0;
849
+ size_t pair_mark = 0;
850
+ if (top != NULL && top->kind == YEP_DOM_SEQUENCE) {
851
+ /* walk_children children (inside a KEY-root collection) walk
852
+ * flat: no index segments — the seq arm never runs for it */
853
+ if (top->role == 0 && yep_validate_path_push_index(&v->ctx, top->seq_index++) != 0) {
854
+ /* OOM flagged; the frame still opens (the walk stops) */
855
+ }
856
+ } else if (top != NULL && top->pair_slot == 0) {
857
+ role = 1; /* the direct KEY of a mapping: transparent */
858
+ pair_mark = top->tail_n; /* the pair-head insert point */
859
+ }
860
+ yep_vs_frame* f = vs_push(v, kind, role, ev);
861
+ if (f == NULL) {
862
+ return;
863
+ }
864
+ f->pair_tail_mark = pair_mark;
865
+ /* the anchor's OPEN record (the DOM binds at node creation) */
866
+ if (ev->anchor_id != 0) {
867
+ yep_vs_anchor* a = vs_anchor(v, ev->anchor_id);
868
+ if (a != NULL) {
869
+ a->kind = kind;
870
+ a->closed = 0;
871
+ a->off = UINT32_MAX;
872
+ }
873
+ }
874
+ }
875
+
876
+ static void vs_event_collection_end(yep_vstream* v, const yep_event* ev, uint8_t kind) {
877
+ (void)ev;
878
+ if (v->depth == 0) {
879
+ return;
880
+ }
881
+ yep_vs_frame* f = &v->frames[v->depth - 1];
882
+ if (f->kind != kind) {
883
+ return; /* defensive: the engine's streams are balanced */
884
+ }
885
+ /* the pending tag check of an EMPTY collection (no first leaf) */
886
+ if (!f->first_seen) {
887
+ f->first_seen = 1;
888
+ if (f->has_tag) {
889
+ f->has_tag = 0;
890
+ if (f->kind == YEP_DOM_MAPPING) {
891
+ yep_vs_capture cap = {v, f, 0};
892
+ void (*save_put)(void*, const yeptris_diagnostic*) = v->ctx.stage_put;
893
+ void* save_stage = v->ctx.stage;
894
+ v->ctx.stage_put = vs_capture_head;
895
+ v->ctx.stage = &cap;
896
+ yep_check_tag_facts(&v->ctx, (int)f->kind, 1, f->tag_id, (yep_view){NULL, 0},
897
+ UINT32_MAX, v->fam, v->json_family);
898
+ v->ctx.stage_put = save_put;
899
+ v->ctx.stage = save_stage;
900
+ } else {
901
+ yep_check_tag_facts(&v->ctx, (int)f->kind, 1, f->tag_id, (yep_view){NULL, 0},
902
+ UINT32_MAX, v->fam, v->json_family);
903
+ }
904
+ }
905
+ }
906
+ uint64_t digest = f->digest;
907
+ uint32_t anchor_id = f->anchor_id;
908
+ uint8_t child_kind = f->kind;
909
+
910
+ if (kind == YEP_DOM_MAPPING) {
911
+ /* the DOM's odd-child law: an unpaired key pairs with a
912
+ * synthesized null (plain, implicit, empty, tag-free) */
913
+ if (f->pair_slot == 1) {
914
+ yep_vs_facts nf;
915
+ memset(&nf, 0, sizeof(nf));
916
+ nf.kind = (int)YEP_DOM_SCALAR;
917
+ nf.plainish = 1;
918
+ nf.off = UINT32_MAX;
919
+ vs_checks_scalar(v, &nf);
920
+ uint64_t vd = vs_digest_scalar(&nf);
921
+ uint64_t pair = yep_validate_hash_mix(YEP_VAL_DIGEST_PAIR_SEED, f->pair_key_digest);
922
+ pair = yep_validate_hash_mix(pair, vd);
923
+ f->digest ^= pair;
924
+ yep_validate_path_pop(&v->ctx, f->path_mark);
925
+ f->pair_slot = 0;
926
+ }
927
+ vs_flush_stage(v, f);
928
+ }
929
+
930
+ /* digest + pair delivery to the parent; the parent's segment for
931
+ * this child pops here (the seq index / the pair's key segment) */
932
+ yep_vs_frame* p = v->depth >= 2 ? &v->frames[v->depth - 2] : NULL;
933
+ if (p != NULL) {
934
+ if (p->kind == YEP_DOM_SEQUENCE) {
935
+ yep_validate_path_pop(&v->ctx, p->path_mark); /* the index */
936
+ p->digest = yep_validate_hash_mix(p->digest, digest);
937
+ } else if (p->pair_slot == 0) {
938
+ /* the child was the KEY collection: transparent on the
939
+ * path (no own segment). Its dup-table probe runs here
940
+ * (the DOM's table covers collection keys; the detail's
941
+ * key text is the empty view, its position the key's
942
+ * first leaf). The json12 key law lands at the pair's
943
+ * HEAD position (the DOM emits it before the key
944
+ * subtree's walk). */
945
+ if (p->role == 0) {
946
+ yep_vs_keyent e;
947
+ memset(&e, 0, sizeof(e));
948
+ e.h32 = (uint32_t)(digest ^ (digest >> 32));
949
+ e.digest = digest;
950
+ e.used = 1;
951
+ e.is_scalar = 0;
952
+ yep_vs_facts kf;
953
+ memset(&kf, 0, sizeof(kf));
954
+ kf.off = f->first_off;
955
+ vs_dup_probe(v, p, &e, &kf);
956
+ }
957
+ if (v->json_family) {
958
+ yep_vs_capture cap = {v, p, f->pair_tail_mark};
959
+ void (*save_put)(void*, const yeptris_diagnostic*) = v->ctx.stage_put;
960
+ void* save_stage = v->ctx.stage;
961
+ v->ctx.stage_put = vs_capture_pairhead;
962
+ v->ctx.stage = &cap;
963
+ yep_validate_emit(&v->ctx, YEPTRIS_DIAG_KEY_NOT_STRING, f->first_off,
964
+ "mapping key is a %s",
965
+ child_kind == YEP_DOM_SEQUENCE ? "sequence" : "mapping");
966
+ v->ctx.stage_put = save_put;
967
+ v->ctx.stage = save_stage;
968
+ }
969
+ p->pair_key_digest = digest;
970
+ if (yep_validate_path_push(&v->ctx, "", 0) != 0) {
971
+ /* OOM flagged */
972
+ }
973
+ p->pair_slot = 1;
974
+ } else {
975
+ uint64_t pair = yep_validate_hash_mix(YEP_VAL_DIGEST_PAIR_SEED, p->pair_key_digest);
976
+ pair = yep_validate_hash_mix(pair, digest);
977
+ p->digest ^= pair;
978
+ yep_validate_path_pop(&v->ctx, p->path_mark);
979
+ p->pair_slot = 0;
980
+ }
981
+ }
982
+
983
+ /* the anchor's subtree closed */
984
+ if (anchor_id != 0) {
985
+ yep_vs_anchor* a = vs_anchor_at(v, anchor_id);
986
+ if (a != NULL) {
987
+ a->kind = child_kind;
988
+ a->closed = 1;
989
+ a->off = f->first_off;
990
+ a->digest = digest;
991
+ a->digest_known = 1;
992
+ }
993
+ }
994
+
995
+ vs_pop(v);
996
+ }
997
+
998
+ static void vs_doc_start(yep_vstream* v) {
999
+ v->docs_seen++;
1000
+ if (v->docs_seen == 2 && !v->qualified) {
1001
+ /* qualification arrives with the SECOND document: document 0's
1002
+ * diagnostics (all flushed — its root closed before the
1003
+ * DOCUMENT_END) gain the prefix retroactively, with the path
1004
+ * slot's exact truncation */
1005
+ v->qualified = 1;
1006
+ yeptris_validate_report* r = v->ctx.rep;
1007
+ for (uint32_t i = 0; i < r->count; i++) {
1008
+ char* p = r->diags[i].path;
1009
+ size_t plen = strlen(p);
1010
+ size_t keep = plen < YEPTRIS_DIAG_PATH_MAX - 3 ? plen : YEPTRIS_DIAG_PATH_MAX - 3;
1011
+ memmove(p + 2, p, keep);
1012
+ p[0] = '/';
1013
+ p[1] = '0';
1014
+ p[2 + keep] = '\0';
1015
+ }
1016
+ }
1017
+ v->ctx.path_len = 0; /* root-relative per document */
1018
+ if (v->qualified) {
1019
+ if (yep_validate_path_push_index(&v->ctx, v->docs_seen - 1) != 0) {
1020
+ v->ctx.oom = 1;
1021
+ }
1022
+ }
1023
+ }
1024
+
1025
+ static void vs_doc_end(yep_vstream* v) {
1026
+ v->ctx.path_len = 0;
1027
+ }
1028
+
1029
+ static int vs_on_event(void* ctx, const yep_event* ev) {
1030
+ yep_vstream* v = ctx;
1031
+ /* the stage hook rides the mapping state: diagnostics stage while
1032
+ * a mapping is open, otherwise they take the report path (one
1033
+ * home for the cap law) */
1034
+ v->ctx.stage_put = v->maps_open > 0 ? vs_stage_put : NULL;
1035
+ v->ctx.stage = v;
1036
+ switch (ev->type) {
1037
+ case YEP_EV_SCALAR:
1038
+ vs_event_scalar(v, ev);
1039
+ break;
1040
+ case YEP_EV_ALIAS:
1041
+ vs_event_alias(v, ev);
1042
+ break;
1043
+ case YEP_EV_SEQ_START:
1044
+ vs_event_collection_start(v, ev, YEP_DOM_SEQUENCE);
1045
+ break;
1046
+ case YEP_EV_MAP_START:
1047
+ vs_event_collection_start(v, ev, YEP_DOM_MAPPING);
1048
+ break;
1049
+ case YEP_EV_SEQ_END:
1050
+ vs_event_collection_end(v, ev, YEP_DOM_SEQUENCE);
1051
+ break;
1052
+ case YEP_EV_MAP_END:
1053
+ vs_event_collection_end(v, ev, YEP_DOM_MAPPING);
1054
+ break;
1055
+ case YEP_EV_DOCUMENT_START:
1056
+ vs_doc_start(v);
1057
+ break;
1058
+ case YEP_EV_DOCUMENT_END:
1059
+ vs_doc_end(v);
1060
+ break;
1061
+ default:
1062
+ break; /* STREAM_START / STREAM_END frame only */
1063
+ }
1064
+ return v->ctx.oom ? -1 : 0; /* the DOM sink's OOM abort law */
1065
+ }
1066
+
1067
+ /* ---- the face -------------------------------------------------------------- */
1068
+
1069
+ static void vs_free(yep_vstream* v) {
1070
+ if (v == NULL) {
1071
+ return;
1072
+ }
1073
+ const yep_allocator* sys = yep_system_allocator();
1074
+ while (v->depth > 0) {
1075
+ vs_pop(v);
1076
+ }
1077
+ yep_free(sys, v->frames);
1078
+ yep_free(sys, v->anchors);
1079
+ yep_free(sys, v->ctx.line_off);
1080
+ yep_free(sys, v->ctx.path);
1081
+ yeptris_validate_report_free(v->ctx.rep);
1082
+ yep_free(sys, v);
1083
+ }
1084
+
1085
+ static void vs_on_input(void* ctx, const char* base, size_t len) {
1086
+ yep_vstream* v = ctx;
1087
+ v->input_base = base;
1088
+ v->ctx.input = base;
1089
+ v->ctx.input_len = len;
1090
+ }
1091
+
1092
+ YEPTRIS_API YeptrisStatus yeptris_validate_stream(const char* buf, size_t len, YeptrisSchema schema,
1093
+ yeptris_validate_report** out) {
1094
+ if (out == NULL || (buf == NULL && len != 0)) {
1095
+ return YEPTRIS_ERROR_ARG;
1096
+ }
1097
+ *out = NULL;
1098
+ const yep_resolver* fam;
1099
+ int json_family;
1100
+ if (!yep_validate_schema_family(schema, &fam, &json_family)) {
1101
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
1102
+ "schema %d is not a validation family", (int)schema);
1103
+ return YEPTRIS_ERROR_ARG;
1104
+ }
1105
+ yep_vstream* v = yep_alloc(yep_system_allocator(), sizeof(*v));
1106
+ if (v == NULL) {
1107
+ return YEPTRIS_ERROR_MEMORY;
1108
+ }
1109
+ memset(v, 0, sizeof(*v));
1110
+ v->fam = fam;
1111
+ v->json_family = json_family;
1112
+ v->ctx.stage_put = vs_stage_put;
1113
+ v->ctx.stage = v;
1114
+ v->ctx.rep = yep_validate_report_new();
1115
+ if (v->ctx.rep == NULL) {
1116
+ vs_free(v);
1117
+ return YEPTRIS_ERROR_MEMORY;
1118
+ }
1119
+ yep_sink sink;
1120
+ memset(&sink, 0, sizeof(sink));
1121
+ sink.on_event = vs_on_event;
1122
+ sink.ctx = v;
1123
+ sink.compose = 1; /* the engine's undefined-alias law: the DOM
1124
+ * route's parse statuses ride along */
1125
+ YeptrisStatus st = yep_parse_events(buf, len, schema, &sink, vs_on_input, v);
1126
+ v->input_base = NULL; /* the run's bytes died with the run */
1127
+ v->ctx.input = NULL;
1128
+ v->ctx.input_len = 0;
1129
+ if (st != YEPTRIS_OK) {
1130
+ vs_free(v);
1131
+ return st; /* the parse's own error, verbatim */
1132
+ }
1133
+ /* defensive: a clean run leaves no stage open (documents are
1134
+ * balanced); drain anything anyway so the cap law stays the last
1135
+ * writer */
1136
+ while (v->depth > 0) {
1137
+ yep_vs_frame* f = &v->frames[v->depth - 1];
1138
+ if (f->kind == YEP_DOM_MAPPING) {
1139
+ vs_flush_stage(v, f);
1140
+ }
1141
+ vs_pop(v);
1142
+ }
1143
+ v->maps_open = 0;
1144
+ /* the shared tail (the DOM walk's law): OOM frees the report */
1145
+ if (v->ctx.oom) {
1146
+ vs_free(v);
1147
+ return YEPTRIS_ERROR_MEMORY;
1148
+ }
1149
+ yeptris_validate_report* r = v->ctx.rep;
1150
+ st = r->count > 0 ? YEPTRIS_ERROR_SCHEMA : YEPTRIS_OK;
1151
+ *out = r;
1152
+ v->ctx.rep = NULL;
1153
+ vs_free(v);
1154
+ return st;
1155
+ }