yeptris 0.6.64.0-riscv64-linux-gnu

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 (164) hide show
  1. checksums.yaml +7 -0
  2. data/README.adoc +150 -0
  3. data/ext/build_windows_native.rb +60 -0
  4. data/ext/libyeptris/extconf.rb +57 -0
  5. data/ext/yeptris_native/cbor_ruby.c +198 -0
  6. data/ext/yeptris_native/extconf.rb +99 -0
  7. data/ext/yeptris_native/json_ruby.c +414 -0
  8. data/ext/yeptris_native/version.h +29 -0
  9. data/ext/yeptris_native/yeptris_native.c +507 -0
  10. data/lib/yeptris/3.3/native.so +0 -0
  11. data/lib/yeptris/cbor.rb +169 -0
  12. data/lib/yeptris/document.rb +319 -0
  13. data/lib/yeptris/ffi.rb +572 -0
  14. data/lib/yeptris/json/descriptor.rb +266 -0
  15. data/lib/yeptris/json.rb +502 -0
  16. data/lib/yeptris/materializer.rb +389 -0
  17. data/lib/yeptris/node.rb +482 -0
  18. data/lib/yeptris/psych/class_loader.rb +24 -0
  19. data/lib/yeptris/psych/coder_shim.rb +55 -0
  20. data/lib/yeptris/psych/drop_in.rb +63 -0
  21. data/lib/yeptris/psych/encodable.rb +14 -0
  22. data/lib/yeptris/psych/handler.rb +95 -0
  23. data/lib/yeptris/psych/parser.rb +114 -0
  24. data/lib/yeptris/psych/scalar_scanner.rb +149 -0
  25. data/lib/yeptris/psych/tree_builder.rb +119 -0
  26. data/lib/yeptris/psych/visitors.rb +670 -0
  27. data/lib/yeptris/psych.rb +796 -0
  28. data/lib/yeptris/schema.rb +253 -0
  29. data/lib/yeptris/validation.rb +320 -0
  30. data/lib/yeptris/valueml.rb +477 -0
  31. data/lib/yeptris/yaml/descriptor.rb +56 -0
  32. data/lib/yeptris/yaml.rb +668 -0
  33. data/lib/yeptris.rb +130 -0
  34. data/libyeptris.so +0 -0
  35. data/vendor/libyeptris/CMakeLists.txt +280 -0
  36. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  37. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  38. data/vendor/libyeptris/src/CMakeLists.txt +183 -0
  39. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  40. data/vendor/libyeptris/src/include/yeptris/cbor.h +174 -0
  41. data/vendor/libyeptris/src/include/yeptris/dom.h +196 -0
  42. data/vendor/libyeptris/src/include/yeptris/emit.h +128 -0
  43. data/vendor/libyeptris/src/include/yeptris/error.h +42 -0
  44. data/vendor/libyeptris/src/include/yeptris/events.h +171 -0
  45. data/vendor/libyeptris/src/include/yeptris/json.h +57 -0
  46. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  47. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  48. data/vendor/libyeptris/src/include/yeptris/marshal.h +75 -0
  49. data/vendor/libyeptris/src/include/yeptris/parse.h +121 -0
  50. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  51. data/vendor/libyeptris/src/include/yeptris/resolve.h +81 -0
  52. data/vendor/libyeptris/src/include/yeptris/schema.h +121 -0
  53. data/vendor/libyeptris/src/include/yeptris/tape.h +128 -0
  54. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  55. data/vendor/libyeptris/src/include/yeptris/validate.h +401 -0
  56. data/vendor/libyeptris/src/include/yeptris/values.h +108 -0
  57. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  58. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  59. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  60. data/vendor/libyeptris/src/include/yeptris.h +24 -0
  61. data/vendor/libyeptris/src/yeptris/build.c +393 -0
  62. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +35 -0
  63. data/vendor/libyeptris/src/yeptris/cbor/decode.c +1054 -0
  64. data/vendor/libyeptris/src/yeptris/cbor/encode.c +836 -0
  65. data/vendor/libyeptris/src/yeptris/cbor/encode_priv.h +32 -0
  66. data/vendor/libyeptris/src/yeptris/cbor/from_marshal.c +982 -0
  67. data/vendor/libyeptris/src/yeptris/cbor/sink.h +36 -0
  68. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  69. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  70. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  71. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  72. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  73. data/vendor/libyeptris/src/yeptris/common/error.h +81 -0
  74. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  75. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  76. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  77. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  78. data/vendor/libyeptris/src/yeptris/common/simd_text.c +49 -0
  79. data/vendor/libyeptris/src/yeptris/common/simd_text.h +342 -0
  80. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +672 -0
  81. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +793 -0
  82. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +324 -0
  83. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  84. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  85. data/vendor/libyeptris/src/yeptris/doc.h +86 -0
  86. data/vendor/libyeptris/src/yeptris/dom/dom.c +1312 -0
  87. data/vendor/libyeptris/src/yeptris/dom/dom.h +332 -0
  88. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  89. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +173 -0
  90. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  91. data/vendor/libyeptris/src/yeptris/dom/mutate.c +431 -0
  92. data/vendor/libyeptris/src/yeptris/dom/ytape.c +2323 -0
  93. data/vendor/libyeptris/src/yeptris/dom/ytape.h +96 -0
  94. data/vendor/libyeptris/src/yeptris/emit/emit.c +670 -0
  95. data/vendor/libyeptris/src/yeptris/emit/float/api.h +43 -0
  96. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  97. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +231 -0
  98. data/vendor/libyeptris/src/yeptris/emit/float/fpconv.c +428 -0
  99. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  100. data/vendor/libyeptris/src/yeptris/emit/style.c +21 -0
  101. data/vendor/libyeptris/src/yeptris/emit/style.h +32 -0
  102. data/vendor/libyeptris/src/yeptris/emit/writer.c +1951 -0
  103. data/vendor/libyeptris/src/yeptris/emit/writer.h +111 -0
  104. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  105. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  106. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +157 -0
  107. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  108. data/vendor/libyeptris/src/yeptris/events/capture.c +166 -0
  109. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  110. data/vendor/libyeptris/src/yeptris/events/iterparse.c +298 -0
  111. data/vendor/libyeptris/src/yeptris/events/pull.c +117 -0
  112. data/vendor/libyeptris/src/yeptris/events/push.c +151 -0
  113. data/vendor/libyeptris/src/yeptris/events/recorder.c +144 -0
  114. data/vendor/libyeptris/src/yeptris/events/values.c +630 -0
  115. data/vendor/libyeptris/src/yeptris/events/values_priv.h +53 -0
  116. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  117. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  118. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  119. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  120. data/vendor/libyeptris/src/yeptris/marshal/to_json.c +650 -0
  121. data/vendor/libyeptris/src/yeptris/marshal.c +988 -0
  122. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  123. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  124. data/vendor/libyeptris/src/yeptris/memory/arena.c +150 -0
  125. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  126. data/vendor/libyeptris/src/yeptris/memory/pool.c +133 -0
  127. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  128. data/vendor/libyeptris/src/yeptris/parse/engine.c +6528 -0
  129. data/vendor/libyeptris/src/yeptris/parse/engine.h +96 -0
  130. data/vendor/libyeptris/src/yeptris/parse/events.h +137 -0
  131. data/vendor/libyeptris/src/yeptris/parse/numbers.c +374 -0
  132. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  133. data/vendor/libyeptris/src/yeptris/parse/scalars.c +435 -0
  134. data/vendor/libyeptris/src/yeptris/parse/scalars.h +87 -0
  135. data/vendor/libyeptris/src/yeptris/parse/stream_iter.c +358 -0
  136. data/vendor/libyeptris/src/yeptris/parse.c +1074 -0
  137. data/vendor/libyeptris/src/yeptris/plan.c +824 -0
  138. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +339 -0
  139. data/vendor/libyeptris/src/yeptris/resolve/core12.c +153 -0
  140. data/vendor/libyeptris/src/yeptris/resolve/full12.c +201 -0
  141. data/vendor/libyeptris/src/yeptris/resolve/json12.c +109 -0
  142. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +103 -0
  143. data/vendor/libyeptris/src/yeptris/resolve/tags.c +250 -0
  144. data/vendor/libyeptris/src/yeptris/scan/json.c +839 -0
  145. data/vendor/libyeptris/src/yeptris/scan/json.h +116 -0
  146. data/vendor/libyeptris/src/yeptris/scan/scan.c +862 -0
  147. data/vendor/libyeptris/src/yeptris/scan/scan.h +177 -0
  148. data/vendor/libyeptris/src/yeptris/schema.c +309 -0
  149. data/vendor/libyeptris/src/yeptris/tape.c +2375 -0
  150. data/vendor/libyeptris/src/yeptris/tape_in.h +39 -0
  151. data/vendor/libyeptris/src/yeptris/validate/compile.c +306 -0
  152. data/vendor/libyeptris/src/yeptris/validate/internal.h +347 -0
  153. data/vendor/libyeptris/src/yeptris/validate/jsonschema.c +1922 -0
  154. data/vendor/libyeptris/src/yeptris/validate/output.c +246 -0
  155. data/vendor/libyeptris/src/yeptris/validate/regexp.c +1600 -0
  156. data/vendor/libyeptris/src/yeptris/validate/registry.c +118 -0
  157. data/vendor/libyeptris/src/yeptris/validate/resources.c +601 -0
  158. data/vendor/libyeptris/src/yeptris/validate/stream.c +1155 -0
  159. data/vendor/libyeptris/src/yeptris/validate/validate.c +1043 -0
  160. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  161. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  162. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  163. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  164. metadata +221 -0
@@ -0,0 +1,1312 @@
1
+ /* dom.c — event-built DOM (builder sink + storage). */
2
+
3
+ #include <stdio.h>
4
+ #include <string.h>
5
+
6
+ #include "common/simd_text.h"
7
+
8
+ #include "common/mutex.h"
9
+
10
+ #include "dom.h"
11
+
12
+ #include "../scan/scan.h" /* the dq-forced SSOT (the fact bit) */
13
+
14
+ #include <yeptris/resolve.h> /* the tag-id SSOT (the strict literal arm) */
15
+
16
+ /* The bounded-parse caps: each node consumes at least one input byte
17
+ * (the grammar guarantees it), so len + 1024 is airtight for valid
18
+ * documents; docs and anchors are sparser still. The arena copies
19
+ * content the input already paid for (folds, tags, anchors) — generous
20
+ * 128x covers pathological tag expansion while still bounding any
21
+ * loop bug linearly in the input. */
22
+ static int cap_nodes(const yep_dom* d) {
23
+ return d->input_len == 0 || (size_t)d->ncount <= d->input_len + 1024;
24
+ }
25
+ static int cap_arena(const yep_dom* d, uint32_t need) {
26
+ return d->input_len == 0 || (size_t)(d->str_len + need) <= d->input_len * 128 + (1u << 20);
27
+ }
28
+
29
+ /* Doubling with the uint32 ceiling (the audit's wrap finding): the old
30
+ * `while (cap < target) cap *= 2` wrapped to 0 and span forever once a
31
+ * cap crossed 2^31 with a target above it. A cap that cannot double
32
+ * inside uint32 — or a target past the 32-bit space at all — is an
33
+ * alloc failure, never a wrapped capacity. */
34
+ static int dom_cap_next(uint32_t cap, size_t target, uint32_t* out) {
35
+ if (target > UINT32_MAX) {
36
+ return 0;
37
+ }
38
+ while (cap < target) {
39
+ if (cap > UINT32_MAX / 2) {
40
+ return 0;
41
+ }
42
+ cap *= 2;
43
+ }
44
+ *out = cap;
45
+ return 1;
46
+ }
47
+
48
+ int dom_grow_nodes(yep_dom* d, uint32_t need) {
49
+ if ((size_t)d->ncount + need <= d->ncap) {
50
+ return 1;
51
+ }
52
+ if (!cap_nodes(d)) {
53
+ return 0;
54
+ }
55
+ uint32_t ncap;
56
+ if (!dom_cap_next(d->ncap ? d->ncap : 64, (size_t)d->ncount + need, &ncap)) {
57
+ return 0;
58
+ }
59
+ yep_dnode* nd = yep_pool_alloc(d->pool, (size_t)ncap * sizeof(yep_dnode), 16);
60
+ if (nd == NULL) {
61
+ return 0;
62
+ }
63
+ if (d->ncount > 0) {
64
+ memcpy(nd, d->nodes, (size_t)d->ncount * sizeof(yep_dnode));
65
+ }
66
+ d->nodes = nd;
67
+ d->ncap = ncap;
68
+ return 1;
69
+ }
70
+
71
+ int dom_grow_docs(yep_dom* d, uint32_t need) {
72
+ if ((size_t)d->dcount + need <= d->dcap) {
73
+ return 1;
74
+ }
75
+ if (d->input_len != 0 && (size_t)d->dcount > d->input_len / 2 + 16) {
76
+ return 0;
77
+ }
78
+ uint32_t ncap;
79
+ if (!dom_cap_next(d->dcap ? d->dcap : 16, (size_t)d->dcount + need, &ncap)) {
80
+ return 0;
81
+ }
82
+ uint32_t* nd = yep_pool_alloc(d->pool, (size_t)ncap * sizeof(uint32_t), 16);
83
+ if (nd == NULL) {
84
+ return 0;
85
+ }
86
+ if (d->dcount > 0) {
87
+ memcpy(nd, d->docs, (size_t)d->dcount * sizeof(uint32_t));
88
+ }
89
+ d->docs = nd;
90
+ d->dcap = ncap;
91
+ return 1;
92
+ }
93
+
94
+ /* Doubles the arena until cap bytes fit; 0 on OOM. Offsets stored in
95
+ * nodes stay valid across the move. */
96
+ static int str_grow(yep_dom* d, uint32_t need) {
97
+ if ((size_t)d->str_len + need <= d->str_cap) {
98
+ return 1;
99
+ }
100
+ if (!cap_arena(d, need)) {
101
+ return 0;
102
+ }
103
+ uint32_t cap;
104
+ if (!dom_cap_next(d->str_cap ? d->str_cap : 256, (size_t)d->str_len + need, &cap)) {
105
+ return 0;
106
+ }
107
+ char* ns = yep_alloc(d->sys, cap);
108
+ if (ns == NULL) {
109
+ return 0;
110
+ }
111
+ if (d->str_len > 0) {
112
+ memcpy(ns, d->str, d->str_len);
113
+ }
114
+ yep_free(d->sys, d->str);
115
+ d->str = ns;
116
+ d->str_cap = cap;
117
+ return 1;
118
+ }
119
+
120
+ yep_sview yep_dom_str_put(yep_dom* d, const char* p, uint32_t len) {
121
+ yep_sview sv = {YEP_SV_INPUT, 0}; /* empty: arena offset 0 */
122
+ if (len == 0) {
123
+ return sv;
124
+ }
125
+ if (!str_grow(d, len)) {
126
+ /* OOM: an empty view here would silently corrupt the document
127
+ * (the value reads as ""). Record it; dom_place/dom_open_node
128
+ * fail the parse with the memory status. */
129
+ d->oom = 1;
130
+ return sv;
131
+ }
132
+ memcpy(d->str + d->str_len, p, len);
133
+ sv.off = YEP_SV_INPUT | d->str_len;
134
+ sv.len = len;
135
+ d->str_len += len;
136
+ return sv;
137
+ }
138
+
139
+ char* yep_dom_str_tail(yep_dom* d, uint32_t cap) {
140
+ if (!str_grow(d, cap)) {
141
+ return NULL;
142
+ }
143
+ return d->str + d->str_len;
144
+ }
145
+
146
+ yep_sview yep_dom_str_commit(yep_dom* d, uint32_t written) {
147
+ yep_sview sv = {YEP_SV_INPUT | d->str_len, written};
148
+ d->str_len += written;
149
+ return sv;
150
+ }
151
+
152
+ /* Node string: borrowed views stay INPUT offsets (zero copy);
153
+ * everything the engine produced (folded, escaped, resolved tags,
154
+ * anchor names) is copied into the arena — the finish pool is then
155
+ * free to die with the engine. */
156
+ static yep_sview dom_str_in(yep_dom* d, const yep_view* v, int borrowed) {
157
+ if (v == NULL || v->len == 0) {
158
+ yep_sview sv = {0, 0};
159
+ return sv;
160
+ }
161
+ /* The zero-copy law: borrow ONLY a view demonstrably inside the
162
+ * recorded input window. The old heap-order probe (v->p >=
163
+ * input_base) misfiled every engine-owned buffer that happened to
164
+ * sit above the caller's input — the r171 normalizer copy path
165
+ * (norm_buf) hands exactly such views to the pair/event builders —
166
+ * and the resulting input-relative offset was then layout luck per
167
+ * platform/build (the ubuntu-gcc / MSVC-arm / TSan / UBSan CI
168
+ * divergences of PR #1042). The upper bound makes the decision a
169
+ * pure function of the view's location class: both engines of a
170
+ * differential run now store identically on every platform. */
171
+ if (borrowed && v->p != NULL && d->input_base != NULL &&
172
+ (size_t)((const char*)v->p - d->input_base) + v->len <= d->input_len) {
173
+ return yep_sv_input(d, *v);
174
+ }
175
+ return yep_dom_str_put(d, (const char*)v->p, v->len);
176
+ }
177
+
178
+ /* The node-init law (every builder's SSOT): creates one node with the
179
+ * given metadata; tag/anchor views encode through dom_str_in (tags and
180
+ * event-path anchors copy to the arena; input-backed views borrow).
181
+ * tag_id stays 0 — the caller resolves (scalars) or leaves 0. */
182
+ /* Value assign + the emit-side fact bits (issues #352 lanes 2-3):
183
+ * no_exotic is "verified clean" of the dq-forced byte set (sc_route
184
+ * skips its rescan); plain_ok is the full plain-safety verdict (the
185
+ * emitter skips its walk — the block kernel's 35% frame). Both facts
186
+ * ride the scan/'s SSOT predicates; every value assignment routes
187
+ * through here so no path can leave a dirty scalar marked clean. */
188
+ static void dom_set_value(yep_dom* d, uint32_t id, const yep_view* v, int borrowed) {
189
+ d->nodes[id].value = dom_str_in(d, v, borrowed);
190
+ d->nodes[id].no_exotic = !yep_scan_dq_forced(v->p, v->len);
191
+ d->nodes[id].plain_ok = (uint8_t)yep_scan_plain_safe(v->p, v->len);
192
+ }
193
+
194
+ uint32_t dom_open_node(yep_dom* d, uint8_t kind, const yep_view* tag, const yep_view* anchor,
195
+ int anchor_borrowed, uint8_t style, uint8_t implicit, uint8_t flow) {
196
+ if (!dom_grow_nodes(d, 1)) {
197
+ return UINT32_MAX;
198
+ }
199
+ yep_dnode* n = &d->nodes[d->ncount];
200
+ /* template copy: one 48 B move replaces memset + four sentinel
201
+ * patches (TODO.restructure/73) — the hot per-node init */
202
+ static const yep_dnode k_init = {
203
+ .first_child = UINT32_MAX,
204
+ .last_child = UINT32_MAX,
205
+ .next_sibling = UINT32_MAX,
206
+ .target = UINT32_MAX,
207
+ };
208
+ *n = k_init;
209
+ n->kind = kind;
210
+ n->tag = dom_str_in(d, tag, 0);
211
+ n->anchor = dom_str_in(d, anchor, anchor_borrowed);
212
+ if (d->oom) {
213
+ /* the tag/anchor arena write failed: refuse the node (the slot
214
+ * is uncommitted — the next open re-initializes it) */
215
+ return UINT32_MAX;
216
+ }
217
+ n->style = style;
218
+ n->implicit = implicit;
219
+ n->flow = flow;
220
+ return d->ncount++;
221
+ }
222
+
223
+ uint32_t dom_new_node(yep_dom* d, const yep_event* ev, uint8_t kind) {
224
+ if (ev == NULL) {
225
+ return dom_open_node(d, kind, NULL, NULL, 0, 0, 0, 0);
226
+ }
227
+ /* event-path anchors ride the VALUE's borrowedness (historical
228
+ * law, kept byte-identical) */
229
+ uint32_t id = dom_open_node(d, kind, &ev->tag, &ev->anchor, ev->borrowed, ev->style,
230
+ ev->implicit, ev->flow);
231
+ if (id == UINT32_MAX) {
232
+ return UINT32_MAX;
233
+ }
234
+ d->nodes[id].tag_id = ev->tag_id;
235
+ return id;
236
+ }
237
+
238
+ static int dom_anchor_set(yep_dom* d, uint32_t ordinal, uint32_t node) {
239
+ if (ordinal == 0) {
240
+ return -1;
241
+ }
242
+ if (d->input_len != 0 && ordinal > d->input_len + 16) {
243
+ return -1; /* more anchors than input bytes: a bug, not a doc */
244
+ }
245
+ if (ordinal > d->anchor_nodes_cap) {
246
+ /* First allocation sizes from the input (TODO.restructure/71):
247
+ * growth from a bare 64 re-copied the table ~18 times on
248
+ * anchor-heavy (one entry per '&x…' line). len/32 overshoots
249
+ * only pathological one-anchor-per-2-bytes documents, which
250
+ * then double as before. */
251
+ uint32_t cap = d->anchor_nodes_cap ? d->anchor_nodes_cap * 2 : 64;
252
+ if (d->anchor_nodes_cap == 0 && d->input_len > 2048) {
253
+ uint32_t sized = (uint32_t)(d->input_len / 32) + 64;
254
+ if (sized > cap) {
255
+ cap = sized;
256
+ }
257
+ }
258
+ if (!dom_cap_next(cap, ordinal, &cap)) {
259
+ return -1; /* the wrap-checked doubling (audit round 5) */
260
+ }
261
+ uint32_t* na = yep_alloc(d->sys, cap * sizeof(*na));
262
+ if (na == NULL) {
263
+ return -1;
264
+ }
265
+ if (d->anchor_nodes != NULL) {
266
+ memcpy(na, d->anchor_nodes, d->anchor_nodes_cap * sizeof(*na));
267
+ yep_free(d->sys, d->anchor_nodes);
268
+ /* ONLY the anchor table: the mut side tables belong to
269
+ * dom_mut_grow alone (it allocates, copies and frees them
270
+ * together with the cap that tracks their size). These two
271
+ * frees were copy-paste debris from that path -- harmless
272
+ * today only because the mutation tables cannot exist
273
+ * during a parse -- but the ownership must not rely on
274
+ * that accident (audit 2026-10-31). */
275
+ }
276
+ d->anchor_nodes = na;
277
+ d->anchor_nodes_cap = cap;
278
+ }
279
+ d->anchor_nodes[ordinal - 1] = node;
280
+ if (ordinal > d->anchor_max) {
281
+ d->anchor_max = ordinal;
282
+ }
283
+ return 0;
284
+ }
285
+
286
+ static uint32_t dom_anchor_get(const yep_dom* d, uint32_t ordinal) {
287
+ if (ordinal == 0 || ordinal > d->anchor_max) {
288
+ return UINT32_MAX;
289
+ }
290
+ return d->anchor_nodes[ordinal - 1];
291
+ }
292
+
293
+ /* The one place links form (builder and mutation both): records
294
+ * attachment and depth so mutation can reject double-parents and cap
295
+ * nesting without parent pointers. */
296
+ void dom_link(yep_dom* d, uint32_t parent, uint32_t child) {
297
+ yep_dnode* p = &d->nodes[parent];
298
+ if (p->count == 0) {
299
+ p->first_child = child;
300
+ } else {
301
+ d->nodes[p->last_child].next_sibling = child;
302
+ }
303
+ p->last_child = child;
304
+ p->count++;
305
+ /* every link changes SOME container's child list (#377's cache) */
306
+ d->child_cache_id = UINT32_MAX;
307
+ d->child_cache_len = 0;
308
+ }
309
+
310
+ /* ---- mutation side tables (64-2a) ---- */
311
+
312
+ static int dom_mut_grow(yep_dom* d, uint32_t need) {
313
+ if (need <= d->mut_tbl_cap) {
314
+ return 1;
315
+ }
316
+ uint32_t cap;
317
+ if (!dom_cap_next(d->mut_tbl_cap ? d->mut_tbl_cap : 64, need, &cap)) {
318
+ return 0; /* the wrap-checked doubling (audit round 5) */
319
+ }
320
+ uint8_t* na = yep_alloc(d->sys, (size_t)cap / 8 + 1);
321
+ uint16_t* nd = yep_alloc(d->sys, (size_t)cap * sizeof(uint16_t));
322
+ if (na == NULL || nd == NULL) {
323
+ yep_free(d->sys, na);
324
+ yep_free(d->sys, nd);
325
+ return 0;
326
+ }
327
+ size_t old_bytes = ((size_t)d->mut_tbl_cap + 7) / 8;
328
+ if (d->mut_att != NULL) {
329
+ memcpy(na, d->mut_att, old_bytes);
330
+ memcpy(nd, d->mut_depth, (size_t)d->mut_tbl_cap * sizeof(uint16_t));
331
+ }
332
+ /* the grown region is FRESH facts: unattached, depth 0 (a CI
333
+ * failure traced to garbage bits here — local mallocs were zero) */
334
+ memset(na + old_bytes, 0, (size_t)cap / 8 + 1 - old_bytes);
335
+ memset(nd + d->mut_tbl_cap, 0, (size_t)(cap - d->mut_tbl_cap) * sizeof(uint16_t));
336
+ yep_free(d->sys, d->mut_att);
337
+ yep_free(d->sys, d->mut_depth);
338
+ d->mut_att = na;
339
+ d->mut_depth = nd;
340
+ d->mut_tbl_cap = cap;
341
+ return 1;
342
+ }
343
+
344
+ int dom_mut_att(const yep_dom* d, uint32_t id) {
345
+ yep_dom* m = (yep_dom*)d; /* lazy growth is benign bookkeeping */
346
+ if (id >= m->mut_tbl_cap && !dom_mut_grow(m, id + 1)) {
347
+ return 0;
348
+ }
349
+ return (m->mut_att[id >> 3] >> (id & 7)) & 1;
350
+ }
351
+
352
+ void dom_mut_set_att(yep_dom* d, uint32_t id, int v) {
353
+ if (id >= d->mut_tbl_cap && !dom_mut_grow(d, id + 1)) {
354
+ return;
355
+ }
356
+ uint8_t bit = (uint8_t)(1u << (id & 7));
357
+ if (v) {
358
+ d->mut_att[id >> 3] |= bit;
359
+ } else {
360
+ d->mut_att[id >> 3] &= (uint8_t)~bit;
361
+ }
362
+ }
363
+
364
+ uint16_t dom_mut_depth(const yep_dom* d, uint32_t id) {
365
+ yep_dom* m = (yep_dom*)d;
366
+ if (id >= m->mut_tbl_cap && !dom_mut_grow(m, id + 1)) {
367
+ return 0;
368
+ }
369
+ return m->mut_depth[id];
370
+ }
371
+
372
+ void dom_mut_set_depth(yep_dom* d, uint32_t id, uint16_t depth) {
373
+ if (id >= d->mut_tbl_cap && !dom_mut_grow(d, id + 1)) {
374
+ return;
375
+ }
376
+ d->mut_depth[id] = depth;
377
+ }
378
+
379
+ /* Places a completed node: value for a pending key, child of the top
380
+ * collection, or document root. Every builder path ends here, so it is
381
+ * also the OOM choke: an arena write that failed (d->oom) must fail
382
+ * the node, never leave a silently-empty value in the tree. */
383
+ int dom_place(yep_dom* d, uint32_t id) {
384
+ if (d->oom) {
385
+ return -1;
386
+ }
387
+ if (d->depth == 0) {
388
+ if (!dom_grow_docs(d, 1)) {
389
+ return -1;
390
+ }
391
+ d->docs[d->dcount++] = id;
392
+ return 0;
393
+ }
394
+ uint32_t top = d->stack[d->depth - 1];
395
+ yep_dnode* p = &d->nodes[top];
396
+ if (p->kind == YEP_DOM_MAPPING) {
397
+ /* per-frame pairing: a map nested as a KEY consumes this frame's
398
+ * slot without disturbing the parent's (global state corrupted
399
+ * complex keys) */
400
+ if (!d->map_pending_key[d->depth - 1]) {
401
+ d->pending_key_id[d->depth - 1] = id;
402
+ d->map_pending_key[d->depth - 1] = 1;
403
+ return 0;
404
+ }
405
+ d->map_pending_key[d->depth - 1] = 0;
406
+ dom_link(d, top, d->pending_key_id[d->depth - 1]); /* key */
407
+ dom_link(d, top, id); /* value */
408
+ return 0;
409
+ }
410
+ dom_link(d, top, id);
411
+ return 0;
412
+ }
413
+
414
+ int yep_dom_on_event(void* ctx, const yep_event* ev) {
415
+ yep_dom* d = (yep_dom*)ctx;
416
+ switch (ev->type) {
417
+ case YEP_EV_STREAM_START:
418
+ case YEP_EV_DOCUMENT_START:
419
+ case YEP_EV_DOCUMENT_END:
420
+ /* bindings are document-scoped, like the engine's names */
421
+ if (d->anchor_max != 0) {
422
+ memset(d->anchor_nodes, 0, d->anchor_max * sizeof(*d->anchor_nodes));
423
+ d->anchor_max = 0;
424
+ }
425
+ return 0;
426
+ case YEP_EV_STREAM_END:
427
+ /* no depth walk: the parse links TOP-DOWN (every child is
428
+ * linked the moment it starts, under a parent whose depth is
429
+ * final), so dom_link's O(1) assignment already globalizes
430
+ * them. The walk remains the MUTATION builder's job — it
431
+ * attaches pre-built subtrees whose descendants keep local
432
+ * depths (yep_mut_set_depths after attach). */
433
+ return 0;
434
+
435
+ case YEP_EV_SEQ_START:
436
+ case YEP_EV_MAP_START: {
437
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
438
+ return -1;
439
+ }
440
+ uint8_t kind = (ev->type == YEP_EV_SEQ_START) ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING;
441
+ uint32_t id = dom_new_node(d, ev, kind);
442
+ if (id == UINT32_MAX) {
443
+ return -1;
444
+ }
445
+ if (ev->anchor_id != 0 && dom_anchor_set(d, ev->anchor_id, id) != 0) {
446
+ return -1;
447
+ }
448
+ if (dom_place(d, id) != 0) {
449
+ return -1;
450
+ }
451
+ d->map_pending_key[d->depth] = 0;
452
+ d->stack[d->depth++] = id;
453
+ return 0;
454
+ }
455
+
456
+ case YEP_EV_SEQ_END:
457
+ case YEP_EV_MAP_END:
458
+ if (d->depth == 0) {
459
+ return -1;
460
+ }
461
+ if (ev->type == YEP_EV_MAP_END && d->map_pending_key[d->depth - 1]) {
462
+ /* an odd child reached the mapping: the unpaired node —
463
+ * possibly an ANCHORED one — used to be dropped here, its
464
+ * anchor lost, and a later alias then emitted a dangling
465
+ * reference the re-parse rejected (the nightly's round
466
+ * 102). Pair it with a null: "k:" with nothing is a null
467
+ * value in every other position. */
468
+ yep_event nv;
469
+ memset(&nv, 0, sizeof(nv));
470
+ nv.type = YEP_EV_SCALAR;
471
+ nv.style = YEP_STYLE_PLAIN;
472
+ nv.implicit = 1;
473
+ uint32_t nid = dom_new_node(d, &nv, YEP_DOM_SCALAR);
474
+ if (nid == UINT32_MAX) {
475
+ return -1;
476
+ }
477
+ uint32_t top = d->stack[d->depth - 1];
478
+ dom_link(d, top, d->pending_key_id[d->depth - 1]);
479
+ dom_link(d, top, nid);
480
+ d->map_pending_key[d->depth - 1] = 0;
481
+ }
482
+ d->depth--;
483
+ return 0;
484
+
485
+ case YEP_EV_SCALAR: {
486
+ uint32_t id = dom_new_node(d, ev, YEP_DOM_SCALAR);
487
+ if (id == UINT32_MAX) {
488
+ return -1;
489
+ }
490
+ dom_set_value(d, id, &ev->value, ev->borrowed);
491
+ if (ev->anchor_id != 0 && dom_anchor_set(d, ev->anchor_id, id) != 0) {
492
+ return -1;
493
+ }
494
+ return dom_place(d, id);
495
+ }
496
+
497
+ case YEP_EV_ALIAS: {
498
+ uint32_t target = dom_anchor_get(d, ev->anchor_id);
499
+ if (target == UINT32_MAX) {
500
+ return -1;
501
+ }
502
+ uint32_t id = dom_new_node(d, ev, YEP_DOM_ALIAS);
503
+ if (id == UINT32_MAX) {
504
+ return -1;
505
+ }
506
+ dom_set_value(d, id, &ev->value, ev->borrowed);
507
+ d->nodes[id].target = target;
508
+ return dom_place(d, id);
509
+ }
510
+
511
+ default:
512
+ return -1;
513
+ }
514
+ }
515
+
516
+ void yep_dom_reserve(yep_dom* d, uint32_t node_hint, uint32_t str_hint) {
517
+ if (d == NULL) {
518
+ return;
519
+ }
520
+ if (node_hint > d->ncount) {
521
+ /* capacity for node_hint TOTAL nodes; growth remains on OOM */
522
+ (void)dom_grow_nodes(d, node_hint - d->ncount);
523
+ }
524
+ if (str_hint > d->str_cap) {
525
+ (void)str_grow(d, str_hint);
526
+ }
527
+ }
528
+
529
+ /* Content-derived sizing (TODO.impl/06): three SIMD count passes,
530
+ * then reserves. Every structural byte bounds a node boundary; the
531
+ * arena reserve only pays when the content copies (quoted scalars
532
+ * escape, block scalars fold) — borrowed-only documents must not
533
+ * allocate an arena at all. Hints are advisory. */
534
+ void yep_dom_prepare(yep_dom* d, const yep_text_stats* st) {
535
+ if (d == NULL || st == NULL) {
536
+ return;
537
+ }
538
+ /* a block line carries at least TWO nodes (a key and its value,
539
+ * or a dash and its entry): nl-colons cancels to ~zero on plain
540
+ * scalar maps, so the old hint undershot by 200k nodes on
541
+ * anchor-heavy (the growth memmove profiled at 5% of parse) */
542
+ size_t structural = st->comma + st->dash + st->bracket + st->brace + 8;
543
+ size_t by_line = st->nl * 2 + 8;
544
+ uint32_t node_hint = (uint32_t)(structural > by_line ? structural : by_line);
545
+ uint32_t str_hint = (st->dq + st->sq + st->pipe) > 0
546
+ ? (uint32_t)(st->dq * 16 + st->sq * 8 + st->pipe * 128 + 64)
547
+ : 0;
548
+ yep_dom_reserve(d, node_hint, str_hint);
549
+ }
550
+
551
+ void yep_dom_prepare_len(yep_dom* d, size_t len) {
552
+ if (d == NULL) {
553
+ return;
554
+ }
555
+ yep_dom_reserve(d, (uint32_t)(len / 2 + 64), (uint32_t)(len / 16 + 256));
556
+ }
557
+
558
+ yep_dom* yep_dom_create(const yep_allocator* sys) {
559
+ if (sys == NULL) {
560
+ return NULL;
561
+ }
562
+ yep_pool* pool = yep_pool_create(sys, 8192);
563
+ if (pool == NULL) {
564
+ return NULL;
565
+ }
566
+ yep_dom* d = yep_alloc(sys, sizeof(yep_dom));
567
+ if (d == NULL) {
568
+ yep_pool_destroy(pool);
569
+ return NULL;
570
+ }
571
+ memset(d, 0, sizeof(*d));
572
+ d->sys = sys;
573
+ d->pool = pool;
574
+ /* #157: the handle pool is lazy — two allocations and a mutex per
575
+ * document that decode-only consumers (CBOR load, serialize, free)
576
+ * never touch; yep_dom_handles() materializes it on first use */
577
+ d->handles = NULL;
578
+ d->child_cache = NULL;
579
+ d->child_cache_id = UINT32_MAX;
580
+ d->child_cache_len = 0;
581
+ if (yep_mutex_init(&d->midx.mu) != 0) {
582
+ yep_pool_destroy(pool);
583
+ yep_free(sys, d);
584
+ return NULL;
585
+ }
586
+ d->midx.mu_ready = 1;
587
+ return d;
588
+ }
589
+
590
+ /* Lazy handle pool, mirroring the map-index's discipline (mapindex.c):
591
+ * EVERY access rides the dom's lazy-init mutex — read-only sharing
592
+ * means multiple threads may race the FIRST handle creation on a
593
+ * shared document (Threads.ReadOnlySharing; the TSAN catch on the
594
+ * first cut). The uncontended lock costs less than the hpool's own
595
+ * per-allocation lock that follows. */
596
+ struct yep_hpool* yep_dom_handles(yep_dom* d) {
597
+ if (d == NULL || !d->midx.mu_ready) {
598
+ return NULL;
599
+ }
600
+ yep_mutex_lock(&d->midx.mu);
601
+ if (d->handles == NULL) {
602
+ d->handles = yep_hpool_create(d->sys);
603
+ }
604
+ struct yep_hpool* h = d->handles;
605
+ yep_mutex_unlock(&d->midx.mu);
606
+ return h;
607
+ }
608
+
609
+ /* #377 (ruby #168): seq_at/map_at were O(i) sibling walks — the
610
+ * bindings' per-element loops made an 80k-row sequence quadratic
611
+ * (the relaton index: 238-328s where stdlib takes ~3s). The bulk
612
+ * drain (yeptris_node_children) fixed the bindings; THIS fixes the
613
+ * accessor: the last indexed container's child ids cache under the
614
+ * lazy-init mutex, O(1) after the first call. Invalidated by every
615
+ * child-list mutation (dom_invalidate_child_cache). */
616
+ void dom_invalidate_child_cache(yep_dom* d) {
617
+ if (d == NULL) {
618
+ return;
619
+ }
620
+ d->child_cache_id = UINT32_MAX;
621
+ d->child_cache_len = 0;
622
+ }
623
+
624
+ /* Returns child id at index (UINT32_MAX when out of range); the
625
+ * container's child count rides *count_out. */
626
+ uint32_t dom_indexed_child(yep_dom* d, uint32_t cid, size_t index, uint32_t* count_out) {
627
+ const yep_dnode* n = yep_dom_node(d, cid);
628
+ if (n == NULL) {
629
+ *count_out = 0;
630
+ return UINT32_MAX;
631
+ }
632
+ /* the midx discipline (TSAN, Threads.ReadOnlySharing): READS of
633
+ * the cache ride the mutex too — a concurrent builder publishes
634
+ * under it, so the old unlocked hit path raced the build's
635
+ * stores. Uncontended in the single-threaded hot path. */
636
+ yep_mutex_lock(&d->midx.mu);
637
+ uint32_t out;
638
+ *count_out = n->count;
639
+ if (d->child_cache_id == cid) {
640
+ out = index < d->child_cache_len ? d->child_cache[index] : UINT32_MAX;
641
+ } else {
642
+ /* miss: build once under the lazy-init mutex (concurrent
643
+ * first calls race here exactly like the handle pool's) */
644
+ yep_free(d->sys, d->child_cache);
645
+ d->child_cache = NULL;
646
+ d->child_cache_len = 0;
647
+ if (n->count > 0) {
648
+ uint32_t* arr = yep_alloc(d->sys, n->count * sizeof(uint32_t));
649
+ if (arr != NULL) {
650
+ uint32_t k = 0;
651
+ for (uint32_t id = n->first_child; id != UINT32_MAX && k < n->count;) {
652
+ const yep_dnode* cn = yep_dom_node(d, id);
653
+ if (cn == NULL) {
654
+ break;
655
+ }
656
+ arr[k++] = id;
657
+ id = cn->next_sibling;
658
+ }
659
+ d->child_cache = arr;
660
+ d->child_cache_len = k;
661
+ }
662
+ }
663
+ d->child_cache_id = cid;
664
+ out = index < d->child_cache_len ? d->child_cache[index] : UINT32_MAX;
665
+ }
666
+ yep_mutex_unlock(&d->midx.mu);
667
+ return out;
668
+ }
669
+
670
+ void yep_dom_destroy(yep_dom* d) {
671
+ if (d == NULL) {
672
+ return;
673
+ }
674
+ yep_free(d->sys, d->str);
675
+ yep_midx_destroy(d);
676
+ yep_free(d->sys, d->anchor_nodes);
677
+ yep_free(d->sys, d->mut_att);
678
+ yep_free(d->sys, d->mut_depth);
679
+ yep_hpool_destroy(d->handles);
680
+ yep_free(d->sys, d->child_cache);
681
+ yep_pool_destroy(d->pool);
682
+ yep_free(d->sys, d);
683
+ }
684
+
685
+ /* ------------------------------------------------- direct builders */
686
+ /* Two direct builders, one set of laws: yep_dom_build_json walks a
687
+ * whole strictly-validated JSON buffer (TODO.impl/27, the parse_json
688
+ * seam); dom_on_flow_json walks an engine-validated span mid-document
689
+ * (TODO.restructure/50, the sink fast path). Both create nodes through
690
+ * dom_open_node and place them through dom_place on the DOM's own
691
+ * stack — the event sink's pairing/link/anchor laws, shared, so the
692
+ * trees cannot diverge (the flow-direct-diff ctest pins it). */
693
+
694
+ #include "parse/scalars.h"
695
+ #include "resolve/resolver.h"
696
+ #include "scan/json.h"
697
+ #include "scan/scan.h"
698
+
699
+ const yep_resolver* yep_dom_resolver(const yep_dom* d) {
700
+ return d->resolver != NULL ? d->resolver : yep_resolver_core12();
701
+ }
702
+
703
+ typedef struct {
704
+ yep_dom* d;
705
+ const char* p;
706
+ size_t len;
707
+ size_t i;
708
+ } jbuilder;
709
+
710
+ /* Whole-buffer JSON: value-first recursion (the grammar is validated,
711
+ * the walk only advances). */
712
+ static int jb_value(jbuilder* b);
713
+
714
+ static int jb_string_node(jbuilder* b) {
715
+ uint32_t id = dom_open_node(b->d, YEP_DOM_SCALAR, NULL, NULL, 0, 0, 0, 0);
716
+ if (id == UINT32_MAX) {
717
+ return -1;
718
+ }
719
+ yep_dnode* n = &b->d->nodes[id];
720
+ size_t close;
721
+ int has_esc = 0;
722
+ size_t start = b->i;
723
+ if (!yep_json_string(b->p, b->len, &b->i, &close, &has_esc)) {
724
+ return -2;
725
+ }
726
+ if (has_esc) {
727
+ uint32_t span = (uint32_t)(close - start - 1);
728
+ char* dst = yep_dom_str_tail(b->d, span);
729
+ if (dst == NULL) {
730
+ return -1;
731
+ }
732
+ n->value = yep_dom_str_commit(
733
+ b->d, yep_finish_double_into(b->p, (uint32_t)(start + 1), (uint32_t)close, dst, span));
734
+ } else {
735
+ yep_view v = {b->p + start + 1, (uint32_t)(close - start - 1)};
736
+ n->value = dom_str_in(b->d, &v, 1);
737
+ }
738
+ n->style = YEP_STYLE_DOUBLE_QUOTED;
739
+ n->tag_id = 0; /* str */
740
+ return dom_place(b->d, id) == 0 ? 0 : -1;
741
+ }
742
+
743
+ static int jb_scalar_node(jbuilder* b) {
744
+ uint32_t id = dom_open_node(b->d, YEP_DOM_SCALAR, NULL, NULL, 0, 0, 0, 0);
745
+ if (id == UINT32_MAX) {
746
+ return -1;
747
+ }
748
+ yep_dnode* n = &b->d->nodes[id];
749
+ size_t start = b->i;
750
+ size_t i = b->i;
751
+ const yep_resolver* r = yep_dom_resolver(b->d);
752
+ if (b->p[i] == 't') {
753
+ if (!yep_json_literal(b->p, b->len, &i, "true")) {
754
+ return -2;
755
+ }
756
+ } else if (b->p[i] == 'f') {
757
+ if (!yep_json_literal(b->p, b->len, &i, "false")) {
758
+ return -2;
759
+ }
760
+ } else if (b->p[i] == 'n') {
761
+ if (!yep_json_literal(b->p, b->len, &i, "null")) {
762
+ return -2;
763
+ }
764
+ } else {
765
+ if (!yep_json_number(b->p, b->len, &i)) {
766
+ return -2;
767
+ }
768
+ }
769
+ b->i = i;
770
+ yep_view v = {b->p + start, (uint32_t)(i - start)};
771
+ n->value = dom_str_in(b->d, &v, 1);
772
+ n->style = YEP_STYLE_PLAIN;
773
+ n->implicit = 1;
774
+ n->tag_id = r->resolve(NULL, v.p, v.len);
775
+ return dom_place(b->d, id) == 0 ? 0 : -1;
776
+ }
777
+
778
+ static void jb_ws(jbuilder* b) {
779
+ while (b->i < b->len) {
780
+ char c = b->p[b->i];
781
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
782
+ b->i++;
783
+ } else {
784
+ break;
785
+ }
786
+ }
787
+ }
788
+
789
+ static int jb_value(jbuilder* b) {
790
+ yep_dom* d = b->d;
791
+ jb_ws(b);
792
+ if (b->i >= b->len) {
793
+ return -2;
794
+ }
795
+ char c = b->p[b->i];
796
+ if (c == '{' || c == '[') {
797
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
798
+ return -2;
799
+ }
800
+ int map = (c == '{');
801
+ b->i++;
802
+ uint32_t id =
803
+ dom_open_node(d, map ? YEP_DOM_MAPPING : YEP_DOM_SEQUENCE, NULL, NULL, 0, 0, 0, 1);
804
+ if (id == UINT32_MAX) {
805
+ return -1;
806
+ }
807
+ if (dom_place(d, id) != 0) {
808
+ return -1;
809
+ }
810
+ d->map_pending_key[d->depth] = 0;
811
+ d->stack[d->depth++] = id;
812
+ jb_ws(b);
813
+ if (b->i < b->len && b->p[b->i] == (map ? '}' : ']')) {
814
+ b->i++;
815
+ d->depth--;
816
+ return 0;
817
+ }
818
+ for (;;) {
819
+ size_t member_start = b->i;
820
+ int rc = jb_value(b);
821
+ if (rc != 0) {
822
+ return rc;
823
+ }
824
+ jb_ws(b);
825
+ if (map) {
826
+ /* a value must be followed by , or } — the map's key
827
+ * slot is pending until the ':' value completes */
828
+ if (b->i < b->len && b->p[b->i] == ':' && d->map_pending_key[d->depth - 1] != 0) {
829
+ /* libyaml's simple-key law rides the JSON-class
830
+ * route too: a key whose colon distance crosses
831
+ * 1024 characters defers to the engine's clean
832
+ * error (run 38143486175) */
833
+ if (b->i - member_start > 1024 &&
834
+ yep_simple_key_chars(b->p + member_start, b->i - member_start) > 1024) {
835
+ return -2;
836
+ }
837
+ b->i++;
838
+ rc = jb_value(b);
839
+ if (rc != 0) {
840
+ return rc;
841
+ }
842
+ jb_ws(b);
843
+ }
844
+ }
845
+ if (b->i >= b->len) {
846
+ return -2;
847
+ }
848
+ if (b->p[b->i] == ',') {
849
+ b->i++;
850
+ continue;
851
+ }
852
+ if (b->p[b->i] == (map ? '}' : ']')) {
853
+ b->i++;
854
+ d->depth--;
855
+ return 0;
856
+ }
857
+ return -2;
858
+ }
859
+ }
860
+ if (c == '"') {
861
+ return jb_string_node(b);
862
+ }
863
+ return jb_scalar_node(b);
864
+ }
865
+
866
+ int yep_dom_build_json(yep_dom* d, const char* buf, size_t len) {
867
+ jbuilder b = {d, buf, len, 0};
868
+ int rc = jb_value(&b);
869
+ if (rc != 0) {
870
+ return rc;
871
+ }
872
+ jb_ws(&b);
873
+ if (b.i != len) {
874
+ return -2;
875
+ }
876
+ return 0;
877
+ }
878
+
879
+ /* --- the flow fast path (TODO.restructure/50) --- */
880
+
881
+ /* --- the fused flow build (TODO.restructure/53) --- */
882
+
883
+ /* One walk validates AND builds: the shared grammar walker drives,
884
+ * nodes are created through the DOM's own laws (dom_open_node,
885
+ * dom_place on the live stack above the entry depth). Staging: the
886
+ * root's placement and anchor binding defer to commit; scratch
887
+ * nodes live past the caller's ncount until then — ANY deviation
888
+ * restores both counters with zero live-state mutation, so the
889
+ * engine's fallback paths are exactly as before. */
890
+ static void dom_flow_stage_reset(yep_dom* d) {
891
+ d->ncount = d->flow_stage_base;
892
+ d->str_len = d->flow_stage_str;
893
+ d->flow_staged = 0;
894
+ }
895
+
896
+ int dom_on_flow_build(void* ctx, const char* p, size_t open, size_t len, uint32_t line,
897
+ size_t line_start, yep_view anchor, yep_view tag, uint32_t anchor_id,
898
+ int max_depth, size_t* close) {
899
+ yep_dom* d = (yep_dom*)ctx;
900
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
901
+ return 0; /* the event path owns the depth-limit error shape */
902
+ }
903
+ int entry_depth = d->depth;
904
+ d->flow_stage_base = d->ncount;
905
+ d->flow_stage_str = d->str_len;
906
+
907
+ uint32_t root = dom_open_node(d, p[open] == '[' ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING, &tag,
908
+ &anchor, 0, 0, 0, 1);
909
+ if (root == UINT32_MAX) {
910
+ goto fail;
911
+ }
912
+ d->map_pending_key[d->depth] = 0;
913
+ d->stack[d->depth++] = root;
914
+
915
+ const yep_resolver* r = yep_dom_resolver(d);
916
+ yep_json_walk w;
917
+ yep_json_tok t;
918
+ yep_json_walk_init(&w, p, len, open, max_depth);
919
+ w.strict = 1; /* the same byte-for-byte JSON gate as the engine's
920
+ * pass 1: a lenient walk split number/literal keys
921
+ * across the fast/slow paths (whole {9->1} vs chunked
922
+ * {"9:1"->null}); flow_strict keeps owning the TAG
923
+ * route only (TODO.restructure/81) */
924
+ for (;;) {
925
+ yep_jw_status st = yep_json_walk_next(&w, &t);
926
+ if (st == YEP_JW_REJECT) {
927
+ d->depth = entry_depth;
928
+ dom_flow_stage_reset(d);
929
+ return 0; /* not JSON-class: the general kernel */
930
+ }
931
+ if (st == YEP_JW_DONE) {
932
+ break;
933
+ }
934
+ switch (t.cls) {
935
+ case '[':
936
+ case '{': {
937
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
938
+ goto fail;
939
+ }
940
+ uint32_t cid = dom_open_node(d, t.cls == '[' ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING, NULL,
941
+ NULL, 0, 0, 0, 1);
942
+ if (cid == UINT32_MAX) {
943
+ goto fail;
944
+ }
945
+ if (dom_place(d, cid) != 0) {
946
+ goto fail;
947
+ }
948
+ d->map_pending_key[d->depth] = 0;
949
+ d->stack[d->depth++] = cid;
950
+ break;
951
+ }
952
+ case ']':
953
+ case '}':
954
+ d->depth--;
955
+ break;
956
+ case '"': {
957
+ uint32_t sid =
958
+ dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_DOUBLE_QUOTED, 0, 0);
959
+ if (sid == UINT32_MAX) {
960
+ goto fail;
961
+ }
962
+ if (t.has_escape) {
963
+ uint32_t span = (uint32_t)(t.end - t.at - 2);
964
+ char* dst = yep_dom_str_tail(d, span);
965
+ if (dst == NULL) {
966
+ goto fail;
967
+ }
968
+ d->nodes[sid].value =
969
+ yep_dom_str_commit(d, yep_finish_double_into(p, (uint32_t)(t.at + 1),
970
+ (uint32_t)(t.end - 1), dst, span));
971
+ d->nodes[sid].no_exotic = 1; /* JSON-class strings: the
972
+ walker rejected every control byte */
973
+ } else {
974
+ yep_view v = {p + t.at + 1, (uint32_t)(t.end - t.at - 2)};
975
+ dom_set_value(d, sid, &v, 1);
976
+ }
977
+ if (dom_place(d, sid) != 0) {
978
+ goto fail;
979
+ }
980
+ break;
981
+ }
982
+ default: { /* number or literal: the walker validated the span */
983
+ uint32_t sid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
984
+ if (sid == UINT32_MAX) {
985
+ goto fail;
986
+ }
987
+ yep_view v = {p + t.at, (uint32_t)(t.end - t.at)};
988
+ d->nodes[sid].value = dom_str_in(d, &v, 1);
989
+ d->nodes[sid].no_exotic = 1; /* the walker validated the
990
+ span as a JSON number or literal */
991
+ if (t.cls == '#') {
992
+ d->nodes[sid].tag_id = yep_resolve_number(r, t.is_float);
993
+ } else if (d->flow_strict) {
994
+ /* strict JSON: the walker validated the span is exactly
995
+ * one of the three RFC 8259 words — no resolver dispatch */
996
+ d->nodes[sid].tag_id = (v.p[0] == 'n') ? YEPTRIS_TAG_NULL : YEPTRIS_TAG_BOOL;
997
+ } else {
998
+ d->nodes[sid].tag_id = r->resolve(NULL, v.p, v.len);
999
+ }
1000
+ if (dom_place(d, sid) != 0) {
1001
+ goto fail;
1002
+ }
1003
+ break;
1004
+ }
1005
+ }
1006
+ }
1007
+ d->depth = entry_depth;
1008
+ if (w.long_key) {
1009
+ dom_flow_stage_reset(d);
1010
+ return 2; /* grammar-valid; pass 2 owns the simple-key error */
1011
+ }
1012
+ d->flow_staged = 1;
1013
+ d->flow_stage_root = root;
1014
+ d->flow_stage_anchor = anchor_id;
1015
+ *close = w.close;
1016
+ return 1;
1017
+ fail:
1018
+ d->depth = entry_depth;
1019
+ dom_flow_stage_reset(d);
1020
+ return -1;
1021
+ }
1022
+
1023
+ int dom_on_flow_commit(void* ctx) {
1024
+ yep_dom* d = (yep_dom*)ctx;
1025
+ if (!d->flow_staged) {
1026
+ return 0;
1027
+ }
1028
+ d->flow_staged = 0;
1029
+ if (d->flow_stage_anchor != 0 &&
1030
+ dom_anchor_set(d, d->flow_stage_anchor, d->flow_stage_root) != 0) {
1031
+ return -1;
1032
+ }
1033
+ if (dom_place(d, d->flow_stage_root) != 0) {
1034
+ return -1;
1035
+ }
1036
+ return 1;
1037
+ }
1038
+
1039
+ int dom_from_tape(yep_dom* d, const yeptris_json_tape* t) {
1040
+ if (d == NULL || t == NULL || t->_src == NULL) {
1041
+ return -1;
1042
+ }
1043
+ /* the columns are the one representation valid on every route
1044
+ * (strict: native; lenient fused: lazily materialized from the
1045
+ * records; scalar-root lenient: native column writes) */
1046
+ if (yeptris_tape_columns((yeptris_json_tape*)t) != 0) {
1047
+ return -1;
1048
+ }
1049
+ if (t->kinds == NULL) {
1050
+ return -1;
1051
+ }
1052
+ d->input_base = (const char*)t->_src;
1053
+ d->input_len = t->_srclen;
1054
+ const char* src = (const char*)t->_src;
1055
+ d->depth = 0;
1056
+ for (uint32_t i = 0; i < t->count; i++) {
1057
+ const uint8_t kind = t->kinds[i];
1058
+ const uint32_t len = t->lens[i];
1059
+ const uint32_t off = t->offs[i];
1060
+ switch (kind) {
1061
+ case YEP_T_DOC:
1062
+ break; /* the stream boundary record */
1063
+ case YEP_T_SEQ_OPEN:
1064
+ case YEP_T_MAP_OPEN: {
1065
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
1066
+ return -1;
1067
+ }
1068
+ uint32_t id =
1069
+ dom_open_node(d, kind == YEP_T_SEQ_OPEN ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING, NULL,
1070
+ NULL, 0, 0, 0, 1);
1071
+ if (id == UINT32_MAX || dom_place(d, id) != 0) {
1072
+ return -1;
1073
+ }
1074
+ d->map_pending_key[d->depth] = 0;
1075
+ d->stack[d->depth++] = id;
1076
+ break;
1077
+ }
1078
+ case YEP_T_CLOSE:
1079
+ if (d->depth == 0) {
1080
+ return -1;
1081
+ }
1082
+ if (d->nodes[d->stack[d->depth - 1]].kind == YEP_DOM_MAPPING &&
1083
+ d->map_pending_key[d->depth - 1]) {
1084
+ /* an odd child reached the mapping (see the event-path
1085
+ * MAP_END): pair it with a null instead of dropping it */
1086
+ yep_event nv;
1087
+ memset(&nv, 0, sizeof(nv));
1088
+ nv.type = YEP_EV_SCALAR;
1089
+ nv.style = YEP_STYLE_PLAIN;
1090
+ nv.implicit = 1;
1091
+ uint32_t nid = dom_new_node(d, &nv, YEP_DOM_SCALAR);
1092
+ if (nid == UINT32_MAX) {
1093
+ return -1;
1094
+ }
1095
+ dom_link(d, d->stack[d->depth - 1], d->pending_key_id[d->depth - 1]);
1096
+ dom_link(d, d->stack[d->depth - 1], nid);
1097
+ d->map_pending_key[d->depth - 1] = 0;
1098
+ }
1099
+ d->depth--;
1100
+ break;
1101
+ case YEP_T_NULL:
1102
+ case YEP_T_TRUE:
1103
+ case YEP_T_FALSE:
1104
+ case YEP_T_INT:
1105
+ case YEP_T_FLOAT:
1106
+ case YEP_T_NUM:
1107
+ case YEP_T_STR: {
1108
+ yep_tag_id tag = YEPTRIS_TAG_STR;
1109
+ uint8_t style = YEP_STYLE_PLAIN;
1110
+ int implicit = 0;
1111
+ switch (kind) {
1112
+ case YEP_T_NULL:
1113
+ tag = YEPTRIS_TAG_NULL;
1114
+ implicit = 1;
1115
+ break;
1116
+ case YEP_T_TRUE:
1117
+ case YEP_T_FALSE:
1118
+ tag = YEPTRIS_TAG_BOOL;
1119
+ implicit = 1;
1120
+ break;
1121
+ case YEP_T_INT:
1122
+ case YEP_T_FLOAT:
1123
+ case YEP_T_NUM:
1124
+ tag = YEPTRIS_TAG_INT;
1125
+ implicit = 1;
1126
+ if (kind == YEP_T_NUM) {
1127
+ /* the lenient route's deferred contract: the span
1128
+ * carried NO number grammar at parse — classify
1129
+ * now (the tape convert's own authority) */
1130
+ size_t adv = 0;
1131
+ int flt = 0;
1132
+ if (yep_json_number_shape(src + off, len, &adv, &flt) == 0 || adv != len) {
1133
+ return -1; /* malformed: the strict route rejected at parse */
1134
+ }
1135
+ tag = flt ? YEPTRIS_TAG_FLOAT : YEPTRIS_TAG_INT;
1136
+ } else if (kind == YEP_T_FLOAT) {
1137
+ tag = YEPTRIS_TAG_FLOAT;
1138
+ }
1139
+ break;
1140
+ default:
1141
+ style = YEP_STYLE_DOUBLE_QUOTED;
1142
+ break;
1143
+ }
1144
+ uint32_t id =
1145
+ dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, style, (uint8_t)implicit, 0);
1146
+ if (id == UINT32_MAX) {
1147
+ return -1;
1148
+ }
1149
+ if (kind == YEP_T_STR) {
1150
+ /* the record's span is the INNER bytes (quotes off) */
1151
+ if (memchr(src + off, '\\', len) != NULL) {
1152
+ /* escaped: unescape into the arena (the fused
1153
+ * builder's arm, byte for byte) */
1154
+ char* dst = yep_dom_str_tail(d, len);
1155
+ if (dst == NULL) {
1156
+ return -1;
1157
+ }
1158
+ d->nodes[id].value = yep_dom_str_commit(
1159
+ d, yep_finish_double_into(src, off, off + len, dst, len));
1160
+ d->nodes[id].no_exotic = 1; /* JSON-class: escapes
1161
+ only, the walker's law */
1162
+ } else {
1163
+ yep_view v = {src + off, len};
1164
+ dom_set_value(d, id, &v, 1);
1165
+ }
1166
+ } else {
1167
+ yep_view v = {src + off, len};
1168
+ dom_set_value(d, id, &v, 1);
1169
+ }
1170
+ d->nodes[id].tag_id = tag;
1171
+ if (dom_place(d, id) != 0) {
1172
+ return -1;
1173
+ }
1174
+ break;
1175
+ }
1176
+ default:
1177
+ return -1; /* a CONT run or an unknown kind: neither reaches a
1178
+ strict-legal tape (CONT only past 16 MiB tokens) */
1179
+ }
1180
+ }
1181
+ return d->depth == 0 ? 0 : -1;
1182
+ }
1183
+
1184
+ void dom_on_flow_rollback(void* ctx) {
1185
+ yep_dom* d = (yep_dom*)ctx;
1186
+ if (d->flow_staged) {
1187
+ dom_flow_stage_reset(d);
1188
+ }
1189
+ }
1190
+
1191
+ /* The sink's block-open fast path (TODO.restructure/57): a `key:` line
1192
+ * whose value follows becomes the map node plus its pending key in one
1193
+ * call — the DOM stack mirrors what the MAP_START event would have
1194
+ * pushed. 0 only for the depth cap (the engine then takes the events,
1195
+ * which own the error). */
1196
+ int dom_on_block_open(void* ctx, const yep_view* key, uint32_t line, uint32_t key_col) {
1197
+ yep_dom* d = (yep_dom*)ctx;
1198
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
1199
+ return 0;
1200
+ }
1201
+ const yep_resolver* r = yep_dom_resolver(d);
1202
+ uint32_t mid = dom_open_node(d, YEP_DOM_MAPPING, NULL, NULL, 0, 0, 0, 0);
1203
+ if (mid == UINT32_MAX) {
1204
+ return -1;
1205
+ }
1206
+ if (dom_place(d, mid) != 0) {
1207
+ return -1;
1208
+ }
1209
+ d->map_pending_key[d->depth] = 0;
1210
+ d->stack[d->depth++] = mid;
1211
+ uint32_t kid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
1212
+ if (kid == UINT32_MAX) {
1213
+ d->depth--;
1214
+ return -1;
1215
+ }
1216
+ dom_set_value(d, kid, key, 1);
1217
+ d->nodes[kid].tag_id = r->resolve(NULL, key->p, key->len);
1218
+ return dom_place(d, kid) == 0 ? 1 : -1;
1219
+ }
1220
+
1221
+ /* The sink's dash-item fast path (TODO.restructure/78): one node
1222
+ * through the node-init law, placed into the sequence the engine
1223
+ * opened before offering — node-for-node the event path's item. */
1224
+ int dom_on_block_item(void* ctx, const yep_block_value* v) {
1225
+ yep_dom* d = (yep_dom*)ctx;
1226
+ const yep_resolver* r = yep_dom_resolver(d);
1227
+ uint32_t vid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
1228
+ if (vid == UINT32_MAX) {
1229
+ return -1;
1230
+ }
1231
+ dom_set_value(d, vid, &v->value, v->borrowed);
1232
+ d->nodes[vid].tag_id = r->resolve(NULL, v->value.p, v->value.len);
1233
+ return dom_place(d, vid) == 0 ? 1 : -1;
1234
+ }
1235
+
1236
+ /* The sink's block fast path (TODO.restructure/54): one classified
1237
+ * `key: value` line becomes two nodes — the key scalar resolved like
1238
+ * every implicit key event, the value through its class — placed with
1239
+ * the shared pairing law. The engine offers the line BEFORE emitting;
1240
+ * returning 0 here (only for an unbound alias target, which cannot
1241
+ * happen when the engine resolved the name) falls back to the two
1242
+ * events with nothing built. */
1243
+ int dom_on_block_pair(void* ctx, const yep_view* key, const yep_block_value* v, uint32_t line,
1244
+ uint32_t key_col, uint32_t val_col) {
1245
+ yep_dom* d = (yep_dom*)ctx;
1246
+ uint32_t target = 0;
1247
+ if (v->cls == YEP_LVAL_ALIAS) {
1248
+ target = dom_anchor_get(d, v->anchor_id);
1249
+ if (target == UINT32_MAX) {
1250
+ return 0;
1251
+ }
1252
+ }
1253
+ const yep_resolver* r = yep_dom_resolver(d);
1254
+ uint32_t kid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
1255
+ if (kid == UINT32_MAX) {
1256
+ return -1;
1257
+ }
1258
+ dom_set_value(d, kid, key, 1);
1259
+ d->nodes[kid].tag_id = r->resolve(NULL, key->p, key->len);
1260
+ if (dom_place(d, kid) != 0) {
1261
+ return -1;
1262
+ }
1263
+ if (v->cls == YEP_LVAL_ALIAS) {
1264
+ uint32_t vid = dom_open_node(d, YEP_DOM_ALIAS, NULL, NULL, 0, 0, 0, 0);
1265
+ if (vid == UINT32_MAX) {
1266
+ return -1;
1267
+ }
1268
+ dom_set_value(d, vid, &v->value, 1); /* borrowed — the
1269
+ event path stamps it too now (73); the gates compare decoded
1270
+ views, not arena placement */
1271
+ d->nodes[vid].target = target;
1272
+ return dom_place(d, vid) == 0 ? 1 : -1;
1273
+ }
1274
+ int anchored = (v->cls == YEP_LVAL_ANCHOR_PLAIN);
1275
+ uint32_t vid = dom_open_node(d, YEP_DOM_SCALAR, NULL, anchored ? &v->anchor : NULL, v->borrowed,
1276
+ YEP_STYLE_PLAIN, 1, 0);
1277
+ if (vid == UINT32_MAX) {
1278
+ return -1;
1279
+ }
1280
+ dom_set_value(d, vid, &v->value, v->borrowed);
1281
+ d->nodes[vid].tag_id = r->resolve(NULL, v->value.p, v->value.len);
1282
+ if (anchored && v->anchor_id != 0 && dom_anchor_set(d, v->anchor_id, vid) != 0) {
1283
+ return -1;
1284
+ }
1285
+ return dom_place(d, vid) == 0 ? 1 : -1;
1286
+ }
1287
+
1288
+ /* The direct scalar path (TODO.restructure/79): the YEP_EV_SCALAR
1289
+ * case's node build, minus the event seam. The engine resolved
1290
+ * tag_id (the typing SSOT); everything else is the case body. */
1291
+ int dom_on_scalar(void* ctx, const yep_view* value, const yep_view* tag, const yep_view* anchor,
1292
+ uint32_t anchor_id, uint8_t tag_id, uint8_t style, uint8_t implicit, uint8_t flow,
1293
+ int borrowed) {
1294
+ yep_dom* d = (yep_dom*)ctx;
1295
+ uint32_t id = dom_open_node(d, YEP_DOM_SCALAR, tag, anchor, borrowed, style, implicit, flow);
1296
+ if (id == UINT32_MAX) {
1297
+ return -1;
1298
+ }
1299
+ dom_set_value(d, id, value, borrowed);
1300
+ d->nodes[id].tag_id = tag_id;
1301
+ if (anchor_id != 0 && dom_anchor_set(d, anchor_id, id) != 0) {
1302
+ return -1;
1303
+ }
1304
+ return dom_place(d, id);
1305
+ }
1306
+
1307
+ const yep_dnode* yep_dom_node(const yep_dom* d, uint32_t id) {
1308
+ if (d == NULL || id >= d->ncount) {
1309
+ return NULL;
1310
+ }
1311
+ return &d->nodes[id];
1312
+ }