yeptris 0.6.7.2-aarch64-linux → 0.6.7.4-aarch64-linux

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 (111) hide show
  1. checksums.yaml +4 -4
  2. data/lib/yeptris.rb +1 -1
  3. data/vendor/libyeptris/CMakeLists.txt +254 -0
  4. data/vendor/libyeptris/cmake/yeptris-config.cmake.in +5 -0
  5. data/vendor/libyeptris/cmake/yeptris.pc.in +11 -0
  6. data/vendor/libyeptris/src/CMakeLists.txt +167 -0
  7. data/vendor/libyeptris/src/include/yeptris/api.h +32 -0
  8. data/vendor/libyeptris/src/include/yeptris/cbor.h +101 -0
  9. data/vendor/libyeptris/src/include/yeptris/dom.h +181 -0
  10. data/vendor/libyeptris/src/include/yeptris/emit.h +81 -0
  11. data/vendor/libyeptris/src/include/yeptris/error.h +41 -0
  12. data/vendor/libyeptris/src/include/yeptris/events.h +157 -0
  13. data/vendor/libyeptris/src/include/yeptris/json.h +45 -0
  14. data/vendor/libyeptris/src/include/yeptris/json.hpp +258 -0
  15. data/vendor/libyeptris/src/include/yeptris/jsonc_compat.h +115 -0
  16. data/vendor/libyeptris/src/include/yeptris/marshal.h +52 -0
  17. data/vendor/libyeptris/src/include/yeptris/parse.h +48 -0
  18. data/vendor/libyeptris/src/include/yeptris/plan.h +100 -0
  19. data/vendor/libyeptris/src/include/yeptris/resolve.h +71 -0
  20. data/vendor/libyeptris/src/include/yeptris/schema.h +119 -0
  21. data/vendor/libyeptris/src/include/yeptris/tape.h +99 -0
  22. data/vendor/libyeptris/src/include/yeptris/types.h +38 -0
  23. data/vendor/libyeptris/src/include/yeptris/values.h +102 -0
  24. data/vendor/libyeptris/src/include/yeptris/version.h.in +29 -0
  25. data/vendor/libyeptris/src/include/yeptris/visit.h +77 -0
  26. data/vendor/libyeptris/src/include/yeptris/yajl_compat.h +135 -0
  27. data/vendor/libyeptris/src/include/yeptris.h +23 -0
  28. data/vendor/libyeptris/src/yeptris/build.c +371 -0
  29. data/vendor/libyeptris/src/yeptris/cbor/cbor.h +25 -0
  30. data/vendor/libyeptris/src/yeptris/cbor/decode.c +846 -0
  31. data/vendor/libyeptris/src/yeptris/cbor/encode.c +821 -0
  32. data/vendor/libyeptris/src/yeptris/common/chartype.c +41 -0
  33. data/vendor/libyeptris/src/yeptris/common/chartype.h +77 -0
  34. data/vendor/libyeptris/src/yeptris/common/cpu.c +81 -0
  35. data/vendor/libyeptris/src/yeptris/common/cpu.h +40 -0
  36. data/vendor/libyeptris/src/yeptris/common/error.c +65 -0
  37. data/vendor/libyeptris/src/yeptris/common/error.h +79 -0
  38. data/vendor/libyeptris/src/yeptris/common/mutex.h +45 -0
  39. data/vendor/libyeptris/src/yeptris/common/nametab.c +275 -0
  40. data/vendor/libyeptris/src/yeptris/common/nametab.h +68 -0
  41. data/vendor/libyeptris/src/yeptris/common/port.h +74 -0
  42. data/vendor/libyeptris/src/yeptris/common/simd_text.c +46 -0
  43. data/vendor/libyeptris/src/yeptris/common/simd_text.h +295 -0
  44. data/vendor/libyeptris/src/yeptris/common/simd_text_avx2.c +534 -0
  45. data/vendor/libyeptris/src/yeptris/common/simd_text_neon.c +662 -0
  46. data/vendor/libyeptris/src/yeptris/common/simd_text_scalar.c +298 -0
  47. data/vendor/libyeptris/src/yeptris/common/string_view.c +34 -0
  48. data/vendor/libyeptris/src/yeptris/common/string_view.h +93 -0
  49. data/vendor/libyeptris/src/yeptris/doc.h +37 -0
  50. data/vendor/libyeptris/src/yeptris/dom/dom.c +973 -0
  51. data/vendor/libyeptris/src/yeptris/dom/dom.h +290 -0
  52. data/vendor/libyeptris/src/yeptris/dom/hpool.c +107 -0
  53. data/vendor/libyeptris/src/yeptris/dom/mapindex.c +166 -0
  54. data/vendor/libyeptris/src/yeptris/dom/mapindex.h +61 -0
  55. data/vendor/libyeptris/src/yeptris/dom/mutate.c +429 -0
  56. data/vendor/libyeptris/src/yeptris/emit/emit.c +349 -0
  57. data/vendor/libyeptris/src/yeptris/emit/float/api.h +38 -0
  58. data/vendor/libyeptris/src/yeptris/emit/float/dragon.c +405 -0
  59. data/vendor/libyeptris/src/yeptris/emit/float/floatint.h +196 -0
  60. data/vendor/libyeptris/src/yeptris/emit/float/print.c +396 -0
  61. data/vendor/libyeptris/src/yeptris/emit/style.c +81 -0
  62. data/vendor/libyeptris/src/yeptris/emit/style.h +24 -0
  63. data/vendor/libyeptris/src/yeptris/emit/writer.c +746 -0
  64. data/vendor/libyeptris/src/yeptris/emit/writer.h +61 -0
  65. data/vendor/libyeptris/src/yeptris/encoding/bom.c +31 -0
  66. data/vendor/libyeptris/src/yeptris/encoding/encoding.h +63 -0
  67. data/vendor/libyeptris/src/yeptris/encoding/transcode.c +148 -0
  68. data/vendor/libyeptris/src/yeptris/encoding/utf8_validate.c +178 -0
  69. data/vendor/libyeptris/src/yeptris/events/capture.c +150 -0
  70. data/vendor/libyeptris/src/yeptris/events/capture.h +39 -0
  71. data/vendor/libyeptris/src/yeptris/events/iterparse.c +208 -0
  72. data/vendor/libyeptris/src/yeptris/events/pull.c +97 -0
  73. data/vendor/libyeptris/src/yeptris/events/push.c +104 -0
  74. data/vendor/libyeptris/src/yeptris/events/recorder.c +107 -0
  75. data/vendor/libyeptris/src/yeptris/events/values.c +527 -0
  76. data/vendor/libyeptris/src/yeptris/events/values_priv.h +51 -0
  77. data/vendor/libyeptris/src/yeptris/events/yaml_compat.c +86 -0
  78. data/vendor/libyeptris/src/yeptris/events/yaml_compat.h +90 -0
  79. data/vendor/libyeptris/src/yeptris/jsonapi/jsonc_compat.c +687 -0
  80. data/vendor/libyeptris/src/yeptris/jsonapi/yajl_compat.c +728 -0
  81. data/vendor/libyeptris/src/yeptris/marshal.c +744 -0
  82. data/vendor/libyeptris/src/yeptris/memory/allocator.c +20 -0
  83. data/vendor/libyeptris/src/yeptris/memory/allocator.h +43 -0
  84. data/vendor/libyeptris/src/yeptris/memory/arena.c +134 -0
  85. data/vendor/libyeptris/src/yeptris/memory/arena.h +52 -0
  86. data/vendor/libyeptris/src/yeptris/memory/pool.c +115 -0
  87. data/vendor/libyeptris/src/yeptris/memory/pool.h +36 -0
  88. data/vendor/libyeptris/src/yeptris/parse/engine.c +3689 -0
  89. data/vendor/libyeptris/src/yeptris/parse/engine.h +65 -0
  90. data/vendor/libyeptris/src/yeptris/parse/events.h +124 -0
  91. data/vendor/libyeptris/src/yeptris/parse/numbers.c +349 -0
  92. data/vendor/libyeptris/src/yeptris/parse/numbers.h +33 -0
  93. data/vendor/libyeptris/src/yeptris/parse/scalars.c +416 -0
  94. data/vendor/libyeptris/src/yeptris/parse/scalars.h +75 -0
  95. data/vendor/libyeptris/src/yeptris/parse.c +650 -0
  96. data/vendor/libyeptris/src/yeptris/plan.c +817 -0
  97. data/vendor/libyeptris/src/yeptris/resolve/compat11.c +334 -0
  98. data/vendor/libyeptris/src/yeptris/resolve/core12.c +143 -0
  99. data/vendor/libyeptris/src/yeptris/resolve/resolver.h +56 -0
  100. data/vendor/libyeptris/src/yeptris/resolve/tags.c +58 -0
  101. data/vendor/libyeptris/src/yeptris/scan/json.c +792 -0
  102. data/vendor/libyeptris/src/yeptris/scan/json.h +109 -0
  103. data/vendor/libyeptris/src/yeptris/scan/scan.c +463 -0
  104. data/vendor/libyeptris/src/yeptris/scan/scan.h +150 -0
  105. data/vendor/libyeptris/src/yeptris/schema.c +238 -0
  106. data/vendor/libyeptris/src/yeptris/tape.c +1292 -0
  107. data/vendor/libyeptris/src/yeptris/version.c +7 -0
  108. data/vendor/libyeptris/src/yeptris/visit/dom_visit.c +115 -0
  109. data/vendor/libyeptris/src/yeptris/visit/json_visit.c +293 -0
  110. data/vendor/libyeptris/src/yeptris/visit/yaml_visit.c +127 -0
  111. metadata +111 -4
@@ -0,0 +1,973 @@
1
+ /* dom.c — event-built DOM (builder sink + storage). */
2
+
3
+ #include <string.h>
4
+
5
+ #include "common/simd_text.h"
6
+
7
+ #include "common/mutex.h"
8
+
9
+ #include "dom.h"
10
+
11
+ /* The bounded-parse caps: each node consumes at least one input byte
12
+ * (the grammar guarantees it), so len + 1024 is airtight for valid
13
+ * documents; docs and anchors are sparser still. The arena copies
14
+ * content the input already paid for (folds, tags, anchors) — generous
15
+ * 128x covers pathological tag expansion while still bounding any
16
+ * loop bug linearly in the input. */
17
+ static int cap_nodes(const yep_dom* d) {
18
+ return d->input_len == 0 || (size_t)d->ncount <= d->input_len + 1024;
19
+ }
20
+ static int cap_arena(const yep_dom* d, uint32_t need) {
21
+ return d->input_len == 0 || (size_t)(d->str_len + need) <= d->input_len * 128 + (1u << 20);
22
+ }
23
+
24
+ int dom_grow_nodes(yep_dom* d, uint32_t need) {
25
+ if (d->ncount + need <= d->ncap) {
26
+ return 1;
27
+ }
28
+ if (!cap_nodes(d)) {
29
+ return 0;
30
+ }
31
+ uint32_t ncap = d->ncap ? d->ncap * 2 : 64;
32
+ while (ncap < d->ncount + need) {
33
+ ncap *= 2;
34
+ }
35
+ yep_dnode* nd = yep_pool_alloc(d->pool, (size_t)ncap * sizeof(yep_dnode), 16);
36
+ if (nd == NULL) {
37
+ return 0;
38
+ }
39
+ if (d->ncount > 0) {
40
+ memcpy(nd, d->nodes, (size_t)d->ncount * sizeof(yep_dnode));
41
+ }
42
+ d->nodes = nd;
43
+ d->ncap = ncap;
44
+ return 1;
45
+ }
46
+
47
+ int dom_grow_docs(yep_dom* d, uint32_t need) {
48
+ if (d->dcount + need <= d->dcap) {
49
+ return 1;
50
+ }
51
+ if (d->input_len != 0 && (size_t)d->dcount > d->input_len / 2 + 16) {
52
+ return 0;
53
+ }
54
+ uint32_t ncap = d->dcap ? d->dcap * 2 : 16;
55
+ while (ncap < d->dcount + need) {
56
+ ncap *= 2;
57
+ }
58
+ uint32_t* nd = yep_pool_alloc(d->pool, (size_t)ncap * sizeof(uint32_t), 16);
59
+ if (nd == NULL) {
60
+ return 0;
61
+ }
62
+ if (d->dcount > 0) {
63
+ memcpy(nd, d->docs, (size_t)d->dcount * sizeof(uint32_t));
64
+ }
65
+ d->docs = nd;
66
+ d->dcap = ncap;
67
+ return 1;
68
+ }
69
+
70
+ /* Doubles the arena until cap bytes fit; 0 on OOM. Offsets stored in
71
+ * nodes stay valid across the move. */
72
+ static int str_grow(yep_dom* d, uint32_t need) {
73
+ if (d->str_len + need <= d->str_cap) {
74
+ return 1;
75
+ }
76
+ if (!cap_arena(d, need)) {
77
+ return 0;
78
+ }
79
+ uint32_t cap = d->str_cap ? d->str_cap : 256;
80
+ while (cap < d->str_len + need) {
81
+ cap *= 2;
82
+ }
83
+ char* ns = yep_alloc(d->sys, cap);
84
+ if (ns == NULL) {
85
+ return 0;
86
+ }
87
+ if (d->str_len > 0) {
88
+ memcpy(ns, d->str, d->str_len);
89
+ }
90
+ yep_free(d->sys, d->str);
91
+ d->str = ns;
92
+ d->str_cap = cap;
93
+ return 1;
94
+ }
95
+
96
+ yep_sview yep_dom_str_put(yep_dom* d, const char* p, uint32_t len) {
97
+ yep_sview sv = {YEP_SV_INPUT, 0}; /* empty: arena offset 0 */
98
+ if (len == 0 || !str_grow(d, len)) {
99
+ return sv; /* len 0 or OOM: empty view */
100
+ }
101
+ memcpy(d->str + d->str_len, p, len);
102
+ sv.off = YEP_SV_INPUT | d->str_len;
103
+ sv.len = len;
104
+ d->str_len += len;
105
+ return sv;
106
+ }
107
+
108
+ char* yep_dom_str_tail(yep_dom* d, uint32_t cap) {
109
+ if (!str_grow(d, cap)) {
110
+ return NULL;
111
+ }
112
+ return d->str + d->str_len;
113
+ }
114
+
115
+ yep_sview yep_dom_str_commit(yep_dom* d, uint32_t written) {
116
+ yep_sview sv = {YEP_SV_INPUT | d->str_len, written};
117
+ d->str_len += written;
118
+ return sv;
119
+ }
120
+
121
+ /* Node string: borrowed views stay INPUT offsets (zero copy);
122
+ * everything the engine produced (folded, escaped, resolved tags,
123
+ * anchor names) is copied into the arena — the finish pool is then
124
+ * free to die with the engine. */
125
+ static yep_sview dom_str_in(yep_dom* d, const yep_view* v, int borrowed) {
126
+ if (v == NULL || v->len == 0) {
127
+ yep_sview sv = {0, 0};
128
+ return sv;
129
+ }
130
+ if (borrowed && v->p != NULL && d->input_base != NULL && (const char*)v->p >= d->input_base) {
131
+ return yep_sv_input(d, *v);
132
+ }
133
+ return yep_dom_str_put(d, (const char*)v->p, v->len);
134
+ }
135
+
136
+ /* The node-init law (every builder's SSOT): creates one node with the
137
+ * given metadata; tag/anchor views encode through dom_str_in (tags and
138
+ * event-path anchors copy to the arena; input-backed views borrow).
139
+ * tag_id stays 0 — the caller resolves (scalars) or leaves 0. */
140
+ uint32_t dom_open_node(yep_dom* d, uint8_t kind, const yep_view* tag, const yep_view* anchor,
141
+ int anchor_borrowed, uint8_t style, uint8_t implicit, uint8_t flow) {
142
+ if (!dom_grow_nodes(d, 1)) {
143
+ return UINT32_MAX;
144
+ }
145
+ yep_dnode* n = &d->nodes[d->ncount];
146
+ /* template copy: one 48 B move replaces memset + four sentinel
147
+ * patches (TODO.restructure/73) — the hot per-node init */
148
+ static const yep_dnode k_init = {
149
+ .first_child = UINT32_MAX,
150
+ .last_child = UINT32_MAX,
151
+ .next_sibling = UINT32_MAX,
152
+ .target = UINT32_MAX,
153
+ };
154
+ *n = k_init;
155
+ n->kind = kind;
156
+ n->tag = dom_str_in(d, tag, 0);
157
+ n->anchor = dom_str_in(d, anchor, anchor_borrowed);
158
+ n->style = style;
159
+ n->implicit = implicit;
160
+ n->flow = flow;
161
+ return d->ncount++;
162
+ }
163
+
164
+ uint32_t dom_new_node(yep_dom* d, const yep_event* ev, uint8_t kind) {
165
+ if (ev == NULL) {
166
+ return dom_open_node(d, kind, NULL, NULL, 0, 0, 0, 0);
167
+ }
168
+ /* event-path anchors ride the VALUE's borrowedness (historical
169
+ * law, kept byte-identical) */
170
+ uint32_t id = dom_open_node(d, kind, &ev->tag, &ev->anchor, ev->borrowed, ev->style,
171
+ ev->implicit, ev->flow);
172
+ if (id == UINT32_MAX) {
173
+ return UINT32_MAX;
174
+ }
175
+ d->nodes[id].tag_id = ev->tag_id;
176
+ return id;
177
+ }
178
+
179
+ static int dom_anchor_set(yep_dom* d, uint32_t ordinal, uint32_t node) {
180
+ if (ordinal == 0) {
181
+ return -1;
182
+ }
183
+ if (d->input_len != 0 && ordinal > d->input_len + 16) {
184
+ return -1; /* more anchors than input bytes: a bug, not a doc */
185
+ }
186
+ if (ordinal > d->anchor_nodes_cap) {
187
+ /* First allocation sizes from the input (TODO.restructure/71):
188
+ * growth from a bare 64 re-copied the table ~18 times on
189
+ * anchor-heavy (one entry per '&x…' line). len/32 overshoots
190
+ * only pathological one-anchor-per-2-bytes documents, which
191
+ * then double as before. */
192
+ uint32_t cap = d->anchor_nodes_cap ? d->anchor_nodes_cap * 2 : 64;
193
+ if (d->anchor_nodes_cap == 0 && d->input_len > 2048) {
194
+ uint32_t sized = (uint32_t)(d->input_len / 32) + 64;
195
+ if (sized > cap) {
196
+ cap = sized;
197
+ }
198
+ }
199
+ while (cap < ordinal) {
200
+ cap *= 2;
201
+ }
202
+ uint32_t* na = yep_alloc(d->sys, cap * sizeof(*na));
203
+ if (na == NULL) {
204
+ return -1;
205
+ }
206
+ if (d->anchor_nodes != NULL) {
207
+ memcpy(na, d->anchor_nodes, d->anchor_nodes_cap * sizeof(*na));
208
+ yep_free(d->sys, d->anchor_nodes);
209
+ yep_free(d->sys, d->mut_att);
210
+ yep_free(d->sys, d->mut_depth);
211
+ }
212
+ d->anchor_nodes = na;
213
+ d->anchor_nodes_cap = cap;
214
+ }
215
+ d->anchor_nodes[ordinal - 1] = node;
216
+ if (ordinal > d->anchor_max) {
217
+ d->anchor_max = ordinal;
218
+ }
219
+ return 0;
220
+ }
221
+
222
+ static uint32_t dom_anchor_get(const yep_dom* d, uint32_t ordinal) {
223
+ if (ordinal == 0 || ordinal > d->anchor_max) {
224
+ return UINT32_MAX;
225
+ }
226
+ return d->anchor_nodes[ordinal - 1];
227
+ }
228
+
229
+ /* The one place links form (builder and mutation both): records
230
+ * attachment and depth so mutation can reject double-parents and cap
231
+ * nesting without parent pointers. */
232
+ void dom_link(yep_dom* d, uint32_t parent, uint32_t child) {
233
+ yep_dnode* p = &d->nodes[parent];
234
+ if (p->count == 0) {
235
+ p->first_child = child;
236
+ } else {
237
+ d->nodes[p->last_child].next_sibling = child;
238
+ }
239
+ p->last_child = child;
240
+ p->count++;
241
+ }
242
+
243
+ /* ---- mutation side tables (64-2a) ---- */
244
+
245
+ static int dom_mut_grow(yep_dom* d, uint32_t need) {
246
+ if (need <= d->mut_tbl_cap) {
247
+ return 1;
248
+ }
249
+ uint32_t cap = d->mut_tbl_cap ? d->mut_tbl_cap : 64;
250
+ while (cap < need) {
251
+ cap *= 2;
252
+ }
253
+ uint8_t* na = yep_alloc(d->sys, (size_t)cap / 8 + 1);
254
+ uint16_t* nd = yep_alloc(d->sys, (size_t)cap * sizeof(uint16_t));
255
+ if (na == NULL || nd == NULL) {
256
+ yep_free(d->sys, na);
257
+ yep_free(d->sys, nd);
258
+ return 0;
259
+ }
260
+ size_t old_bytes = ((size_t)d->mut_tbl_cap + 7) / 8;
261
+ if (d->mut_att != NULL) {
262
+ memcpy(na, d->mut_att, old_bytes);
263
+ memcpy(nd, d->mut_depth, (size_t)d->mut_tbl_cap * sizeof(uint16_t));
264
+ }
265
+ /* the grown region is FRESH facts: unattached, depth 0 (a CI
266
+ * failure traced to garbage bits here — local mallocs were zero) */
267
+ memset(na + old_bytes, 0, (size_t)cap / 8 + 1 - old_bytes);
268
+ memset(nd + d->mut_tbl_cap, 0, (size_t)(cap - d->mut_tbl_cap) * sizeof(uint16_t));
269
+ yep_free(d->sys, d->mut_att);
270
+ yep_free(d->sys, d->mut_depth);
271
+ d->mut_att = na;
272
+ d->mut_depth = nd;
273
+ d->mut_tbl_cap = cap;
274
+ return 1;
275
+ }
276
+
277
+ int dom_mut_att(const yep_dom* d, uint32_t id) {
278
+ yep_dom* m = (yep_dom*)d; /* lazy growth is benign bookkeeping */
279
+ if (id >= m->mut_tbl_cap && !dom_mut_grow(m, id + 1)) {
280
+ return 0;
281
+ }
282
+ return (m->mut_att[id >> 3] >> (id & 7)) & 1;
283
+ }
284
+
285
+ void dom_mut_set_att(yep_dom* d, uint32_t id, int v) {
286
+ if (id >= d->mut_tbl_cap && !dom_mut_grow(d, id + 1)) {
287
+ return;
288
+ }
289
+ uint8_t bit = (uint8_t)(1u << (id & 7));
290
+ if (v) {
291
+ d->mut_att[id >> 3] |= bit;
292
+ } else {
293
+ d->mut_att[id >> 3] &= (uint8_t)~bit;
294
+ }
295
+ }
296
+
297
+ uint16_t dom_mut_depth(const yep_dom* d, uint32_t id) {
298
+ yep_dom* m = (yep_dom*)d;
299
+ if (id >= m->mut_tbl_cap && !dom_mut_grow(m, id + 1)) {
300
+ return 0;
301
+ }
302
+ return m->mut_depth[id];
303
+ }
304
+
305
+ void dom_mut_set_depth(yep_dom* d, uint32_t id, uint16_t depth) {
306
+ if (id >= d->mut_tbl_cap && !dom_mut_grow(d, id + 1)) {
307
+ return;
308
+ }
309
+ d->mut_depth[id] = depth;
310
+ }
311
+
312
+ /* Places a completed node: value for a pending key, child of the top
313
+ * collection, or document root. */
314
+ int dom_place(yep_dom* d, uint32_t id) {
315
+ if (d->depth == 0) {
316
+ if (!dom_grow_docs(d, 1)) {
317
+ return -1;
318
+ }
319
+ d->docs[d->dcount++] = id;
320
+ return 0;
321
+ }
322
+ uint32_t top = d->stack[d->depth - 1];
323
+ yep_dnode* p = &d->nodes[top];
324
+ if (p->kind == YEP_DOM_MAPPING) {
325
+ /* per-frame pairing: a map nested as a KEY consumes this frame's
326
+ * slot without disturbing the parent's (global state corrupted
327
+ * complex keys) */
328
+ if (!d->map_pending_key[d->depth - 1]) {
329
+ d->pending_key_id[d->depth - 1] = id;
330
+ d->map_pending_key[d->depth - 1] = 1;
331
+ return 0;
332
+ }
333
+ d->map_pending_key[d->depth - 1] = 0;
334
+ dom_link(d, top, d->pending_key_id[d->depth - 1]); /* key */
335
+ dom_link(d, top, id); /* value */
336
+ return 0;
337
+ }
338
+ dom_link(d, top, id);
339
+ return 0;
340
+ }
341
+
342
+ int yep_dom_on_event(void* ctx, const yep_event* ev) {
343
+ yep_dom* d = (yep_dom*)ctx;
344
+ switch (ev->type) {
345
+ case YEP_EV_STREAM_START:
346
+ case YEP_EV_DOCUMENT_START:
347
+ case YEP_EV_DOCUMENT_END:
348
+ /* bindings are document-scoped, like the engine's names */
349
+ if (d->anchor_max != 0) {
350
+ memset(d->anchor_nodes, 0, d->anchor_max * sizeof(*d->anchor_nodes));
351
+ d->anchor_max = 0;
352
+ }
353
+ return 0;
354
+ case YEP_EV_STREAM_END:
355
+ /* no depth walk: the parse links TOP-DOWN (every child is
356
+ * linked the moment it starts, under a parent whose depth is
357
+ * final), so dom_link's O(1) assignment already globalizes
358
+ * them. The walk remains the MUTATION builder's job — it
359
+ * attaches pre-built subtrees whose descendants keep local
360
+ * depths (yep_mut_set_depths after attach). */
361
+ return 0;
362
+
363
+ case YEP_EV_SEQ_START:
364
+ case YEP_EV_MAP_START: {
365
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
366
+ return -1;
367
+ }
368
+ uint8_t kind = (ev->type == YEP_EV_SEQ_START) ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING;
369
+ uint32_t id = dom_new_node(d, ev, kind);
370
+ if (id == UINT32_MAX) {
371
+ return -1;
372
+ }
373
+ if (ev->anchor_id != 0 && dom_anchor_set(d, ev->anchor_id, id) != 0) {
374
+ return -1;
375
+ }
376
+ if (dom_place(d, id) != 0) {
377
+ return -1;
378
+ }
379
+ d->map_pending_key[d->depth] = 0;
380
+ d->stack[d->depth++] = id;
381
+ return 0;
382
+ }
383
+
384
+ case YEP_EV_SEQ_END:
385
+ case YEP_EV_MAP_END:
386
+ if (d->depth == 0) {
387
+ return -1;
388
+ }
389
+ d->depth--;
390
+ return 0;
391
+
392
+ case YEP_EV_SCALAR: {
393
+ uint32_t id = dom_new_node(d, ev, YEP_DOM_SCALAR);
394
+ if (id == UINT32_MAX) {
395
+ return -1;
396
+ }
397
+ d->nodes[id].value = dom_str_in(d, &ev->value, ev->borrowed);
398
+ if (ev->anchor_id != 0 && dom_anchor_set(d, ev->anchor_id, id) != 0) {
399
+ return -1;
400
+ }
401
+ return dom_place(d, id);
402
+ }
403
+
404
+ case YEP_EV_ALIAS: {
405
+ uint32_t target = dom_anchor_get(d, ev->anchor_id);
406
+ if (target == UINT32_MAX) {
407
+ return -1;
408
+ }
409
+ uint32_t id = dom_new_node(d, ev, YEP_DOM_ALIAS);
410
+ if (id == UINT32_MAX) {
411
+ return -1;
412
+ }
413
+ d->nodes[id].value = dom_str_in(d, &ev->value, ev->borrowed);
414
+ d->nodes[id].target = target;
415
+ return dom_place(d, id);
416
+ }
417
+
418
+ default:
419
+ return -1;
420
+ }
421
+ }
422
+
423
+ void yep_dom_reserve(yep_dom* d, uint32_t node_hint, uint32_t str_hint) {
424
+ if (d == NULL) {
425
+ return;
426
+ }
427
+ if (node_hint > d->ncount) {
428
+ /* capacity for node_hint TOTAL nodes; growth remains on OOM */
429
+ (void)dom_grow_nodes(d, node_hint - d->ncount);
430
+ }
431
+ if (str_hint > d->str_cap) {
432
+ (void)str_grow(d, str_hint);
433
+ }
434
+ }
435
+
436
+ /* Content-derived sizing (TODO.impl/06): three SIMD count passes,
437
+ * then reserves. Every structural byte bounds a node boundary; the
438
+ * arena reserve only pays when the content copies (quoted scalars
439
+ * escape, block scalars fold) — borrowed-only documents must not
440
+ * allocate an arena at all. Hints are advisory. */
441
+ void yep_dom_prepare(yep_dom* d, const yep_text_stats* st) {
442
+ if (d == NULL || st == NULL) {
443
+ return;
444
+ }
445
+ /* a block line carries at least TWO nodes (a key and its value,
446
+ * or a dash and its entry): nl-colons cancels to ~zero on plain
447
+ * scalar maps, so the old hint undershot by 200k nodes on
448
+ * anchor-heavy (the growth memmove profiled at 5% of parse) */
449
+ size_t structural = st->comma + st->dash + st->bracket + st->brace + 8;
450
+ size_t by_line = st->nl * 2 + 8;
451
+ uint32_t node_hint = (uint32_t)(structural > by_line ? structural : by_line);
452
+ uint32_t str_hint = (st->dq + st->sq + st->pipe) > 0
453
+ ? (uint32_t)(st->dq * 16 + st->sq * 8 + st->pipe * 128 + 64)
454
+ : 0;
455
+ yep_dom_reserve(d, node_hint, str_hint);
456
+ }
457
+
458
+ void yep_dom_prepare_len(yep_dom* d, size_t len) {
459
+ if (d == NULL) {
460
+ return;
461
+ }
462
+ yep_dom_reserve(d, (uint32_t)(len / 2 + 64), (uint32_t)(len / 16 + 256));
463
+ }
464
+
465
+ yep_dom* yep_dom_create(const yep_allocator* sys) {
466
+ if (sys == NULL) {
467
+ return NULL;
468
+ }
469
+ yep_pool* pool = yep_pool_create(sys, 8192);
470
+ if (pool == NULL) {
471
+ return NULL;
472
+ }
473
+ yep_dom* d = yep_alloc(sys, sizeof(yep_dom));
474
+ if (d == NULL) {
475
+ yep_pool_destroy(pool);
476
+ return NULL;
477
+ }
478
+ memset(d, 0, sizeof(*d));
479
+ d->sys = sys;
480
+ d->pool = pool;
481
+ d->handles = yep_hpool_create(sys);
482
+ if (d->handles == NULL) {
483
+ yep_pool_destroy(pool);
484
+ yep_free(sys, d);
485
+ return NULL;
486
+ }
487
+ if (yep_mutex_init(&d->midx.mu) != 0) {
488
+ yep_free(d->sys, d->anchor_nodes);
489
+ yep_free(d->sys, d->mut_att);
490
+ yep_free(d->sys, d->mut_depth);
491
+ yep_hpool_destroy(d->handles);
492
+ yep_pool_destroy(pool);
493
+ yep_free(sys, d);
494
+ return NULL;
495
+ }
496
+ d->midx.mu_ready = 1;
497
+ return d;
498
+ }
499
+
500
+ void yep_dom_destroy(yep_dom* d) {
501
+ if (d == NULL) {
502
+ return;
503
+ }
504
+ yep_free(d->sys, d->str);
505
+ yep_midx_destroy(d);
506
+ yep_free(d->sys, d->anchor_nodes);
507
+ yep_free(d->sys, d->mut_att);
508
+ yep_free(d->sys, d->mut_depth);
509
+ yep_hpool_destroy(d->handles);
510
+ yep_pool_destroy(d->pool);
511
+ yep_free(d->sys, d);
512
+ }
513
+
514
+ /* ------------------------------------------------- direct builders */
515
+ /* Two direct builders, one set of laws: yep_dom_build_json walks a
516
+ * whole strictly-validated JSON buffer (TODO.impl/27, the parse_json
517
+ * seam); dom_on_flow_json walks an engine-validated span mid-document
518
+ * (TODO.restructure/50, the sink fast path). Both create nodes through
519
+ * dom_open_node and place them through dom_place on the DOM's own
520
+ * stack — the event sink's pairing/link/anchor laws, shared, so the
521
+ * trees cannot diverge (the flow-direct-diff ctest pins it). */
522
+
523
+ #include "parse/scalars.h"
524
+ #include "resolve/resolver.h"
525
+ #include "scan/json.h"
526
+ #include "scan/scan.h"
527
+
528
+ static const yep_resolver* dom_resolver(const yep_dom* d) {
529
+ return d->resolver != NULL ? d->resolver : yep_resolver_core12();
530
+ }
531
+
532
+ typedef struct {
533
+ yep_dom* d;
534
+ const char* p;
535
+ size_t len;
536
+ size_t i;
537
+ } jbuilder;
538
+
539
+ /* Whole-buffer JSON: value-first recursion (the grammar is validated,
540
+ * the walk only advances). */
541
+ static int jb_value(jbuilder* b);
542
+
543
+ static int jb_string_node(jbuilder* b) {
544
+ uint32_t id = dom_open_node(b->d, YEP_DOM_SCALAR, NULL, NULL, 0, 0, 0, 0);
545
+ if (id == UINT32_MAX) {
546
+ return -1;
547
+ }
548
+ yep_dnode* n = &b->d->nodes[id];
549
+ size_t close;
550
+ int has_esc = 0;
551
+ size_t start = b->i;
552
+ if (!yep_json_string(b->p, b->len, &b->i, &close, &has_esc)) {
553
+ return -2;
554
+ }
555
+ if (has_esc) {
556
+ uint32_t span = (uint32_t)(close - start - 1);
557
+ char* dst = yep_dom_str_tail(b->d, span);
558
+ if (dst == NULL) {
559
+ return -1;
560
+ }
561
+ n->value = yep_dom_str_commit(
562
+ b->d, yep_finish_double_into(b->p, (uint32_t)(start + 1), (uint32_t)close, dst, span));
563
+ } else {
564
+ yep_view v = {b->p + start + 1, (uint32_t)(close - start - 1)};
565
+ n->value = dom_str_in(b->d, &v, 1);
566
+ }
567
+ n->style = YEP_STYLE_DOUBLE_QUOTED;
568
+ n->tag_id = 0; /* str */
569
+ return dom_place(b->d, id) == 0 ? 0 : -1;
570
+ }
571
+
572
+ static int jb_scalar_node(jbuilder* b) {
573
+ uint32_t id = dom_open_node(b->d, YEP_DOM_SCALAR, NULL, NULL, 0, 0, 0, 0);
574
+ if (id == UINT32_MAX) {
575
+ return -1;
576
+ }
577
+ yep_dnode* n = &b->d->nodes[id];
578
+ size_t start = b->i;
579
+ size_t i = b->i;
580
+ const yep_resolver* r = dom_resolver(b->d);
581
+ if (b->p[i] == 't') {
582
+ if (!yep_json_literal(b->p, b->len, &i, "true")) {
583
+ return -2;
584
+ }
585
+ } else if (b->p[i] == 'f') {
586
+ if (!yep_json_literal(b->p, b->len, &i, "false")) {
587
+ return -2;
588
+ }
589
+ } else if (b->p[i] == 'n') {
590
+ if (!yep_json_literal(b->p, b->len, &i, "null")) {
591
+ return -2;
592
+ }
593
+ } else {
594
+ if (!yep_json_number(b->p, b->len, &i)) {
595
+ return -2;
596
+ }
597
+ }
598
+ b->i = i;
599
+ yep_view v = {b->p + start, (uint32_t)(i - start)};
600
+ n->value = dom_str_in(b->d, &v, 1);
601
+ n->style = YEP_STYLE_PLAIN;
602
+ n->implicit = 1;
603
+ n->tag_id = r->resolve(NULL, v.p, v.len);
604
+ return dom_place(b->d, id) == 0 ? 0 : -1;
605
+ }
606
+
607
+ static void jb_ws(jbuilder* b) {
608
+ while (b->i < b->len) {
609
+ char c = b->p[b->i];
610
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
611
+ b->i++;
612
+ } else {
613
+ break;
614
+ }
615
+ }
616
+ }
617
+
618
+ static int jb_value(jbuilder* b) {
619
+ yep_dom* d = b->d;
620
+ jb_ws(b);
621
+ if (b->i >= b->len) {
622
+ return -2;
623
+ }
624
+ char c = b->p[b->i];
625
+ if (c == '{' || c == '[') {
626
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
627
+ return -2;
628
+ }
629
+ int map = (c == '{');
630
+ b->i++;
631
+ uint32_t id =
632
+ dom_open_node(d, map ? YEP_DOM_MAPPING : YEP_DOM_SEQUENCE, NULL, NULL, 0, 0, 0, 1);
633
+ if (id == UINT32_MAX) {
634
+ return -1;
635
+ }
636
+ if (dom_place(d, id) != 0) {
637
+ return -1;
638
+ }
639
+ d->map_pending_key[d->depth] = 0;
640
+ d->stack[d->depth++] = id;
641
+ jb_ws(b);
642
+ if (b->i < b->len && b->p[b->i] == (map ? '}' : ']')) {
643
+ b->i++;
644
+ d->depth--;
645
+ return 0;
646
+ }
647
+ for (;;) {
648
+ int rc = jb_value(b);
649
+ if (rc != 0) {
650
+ return rc;
651
+ }
652
+ jb_ws(b);
653
+ if (map) {
654
+ /* a value must be followed by , or } — the map's key
655
+ * slot is pending until the ':' value completes */
656
+ if (b->i < b->len && b->p[b->i] == ':' && d->map_pending_key[d->depth - 1] != 0) {
657
+ b->i++;
658
+ rc = jb_value(b);
659
+ if (rc != 0) {
660
+ return rc;
661
+ }
662
+ jb_ws(b);
663
+ }
664
+ }
665
+ if (b->i >= b->len) {
666
+ return -2;
667
+ }
668
+ if (b->p[b->i] == ',') {
669
+ b->i++;
670
+ continue;
671
+ }
672
+ if (b->p[b->i] == (map ? '}' : ']')) {
673
+ b->i++;
674
+ d->depth--;
675
+ return 0;
676
+ }
677
+ return -2;
678
+ }
679
+ }
680
+ if (c == '"') {
681
+ return jb_string_node(b);
682
+ }
683
+ return jb_scalar_node(b);
684
+ }
685
+
686
+ int yep_dom_build_json(yep_dom* d, const char* buf, size_t len) {
687
+ jbuilder b = {d, buf, len, 0};
688
+ int rc = jb_value(&b);
689
+ if (rc != 0) {
690
+ return rc;
691
+ }
692
+ jb_ws(&b);
693
+ if (b.i != len) {
694
+ return -2;
695
+ }
696
+ return 0;
697
+ }
698
+
699
+ /* --- the flow fast path (TODO.restructure/50) --- */
700
+
701
+ /* --- the fused flow build (TODO.restructure/53) --- */
702
+
703
+ /* One walk validates AND builds: the shared grammar walker drives,
704
+ * nodes are created through the DOM's own laws (dom_open_node,
705
+ * dom_place on the live stack above the entry depth). Staging: the
706
+ * root's placement and anchor binding defer to commit; scratch
707
+ * nodes live past the caller's ncount until then — ANY deviation
708
+ * restores both counters with zero live-state mutation, so the
709
+ * engine's fallback paths are exactly as before. */
710
+ static void dom_flow_stage_reset(yep_dom* d) {
711
+ d->ncount = d->flow_stage_base;
712
+ d->str_len = d->flow_stage_str;
713
+ d->flow_staged = 0;
714
+ }
715
+
716
+ int dom_on_flow_build(void* ctx, const char* p, size_t open, size_t len, uint32_t line,
717
+ size_t line_start, yep_view anchor, yep_view tag, uint32_t anchor_id,
718
+ int max_depth, size_t* close) {
719
+ yep_dom* d = (yep_dom*)ctx;
720
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
721
+ return 0; /* the event path owns the depth-limit error shape */
722
+ }
723
+ int entry_depth = d->depth;
724
+ d->flow_stage_base = d->ncount;
725
+ d->flow_stage_str = d->str_len;
726
+
727
+ uint32_t root = dom_open_node(d, p[open] == '[' ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING, &tag,
728
+ &anchor, 0, 0, 0, 1);
729
+ if (root == UINT32_MAX) {
730
+ goto fail;
731
+ }
732
+ d->map_pending_key[d->depth] = 0;
733
+ d->stack[d->depth++] = root;
734
+
735
+ const yep_resolver* r = dom_resolver(d);
736
+ yep_json_walk w;
737
+ yep_json_tok t;
738
+ yep_json_walk_init(&w, p, len, open, max_depth);
739
+ w.strict = d->flow_strict; /* the strict-JSON route rides the same
740
+ fused walk (TODO.restructure/81) */
741
+ for (;;) {
742
+ yep_jw_status st = yep_json_walk_next(&w, &t);
743
+ if (st == YEP_JW_REJECT) {
744
+ d->depth = entry_depth;
745
+ dom_flow_stage_reset(d);
746
+ return 0; /* not JSON-class: the general kernel */
747
+ }
748
+ if (st == YEP_JW_DONE) {
749
+ break;
750
+ }
751
+ switch (t.cls) {
752
+ case '[':
753
+ case '{': {
754
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
755
+ goto fail;
756
+ }
757
+ uint32_t cid = dom_open_node(d, t.cls == '[' ? YEP_DOM_SEQUENCE : YEP_DOM_MAPPING, NULL,
758
+ NULL, 0, 0, 0, 1);
759
+ if (cid == UINT32_MAX) {
760
+ goto fail;
761
+ }
762
+ if (dom_place(d, cid) != 0) {
763
+ goto fail;
764
+ }
765
+ d->map_pending_key[d->depth] = 0;
766
+ d->stack[d->depth++] = cid;
767
+ break;
768
+ }
769
+ case ']':
770
+ case '}':
771
+ d->depth--;
772
+ break;
773
+ case '"': {
774
+ uint32_t sid =
775
+ dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_DOUBLE_QUOTED, 0, 0);
776
+ if (sid == UINT32_MAX) {
777
+ goto fail;
778
+ }
779
+ if (t.has_escape) {
780
+ uint32_t span = (uint32_t)(t.end - t.at - 2);
781
+ char* dst = yep_dom_str_tail(d, span);
782
+ if (dst == NULL) {
783
+ goto fail;
784
+ }
785
+ d->nodes[sid].value =
786
+ yep_dom_str_commit(d, yep_finish_double_into(p, (uint32_t)(t.at + 1),
787
+ (uint32_t)(t.end - 1), dst, span));
788
+ } else {
789
+ yep_view v = {p + t.at + 1, (uint32_t)(t.end - t.at - 2)};
790
+ d->nodes[sid].value = dom_str_in(d, &v, 1);
791
+ }
792
+ if (dom_place(d, sid) != 0) {
793
+ goto fail;
794
+ }
795
+ break;
796
+ }
797
+ default: { /* number or literal: the walker validated the span */
798
+ uint32_t sid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
799
+ if (sid == UINT32_MAX) {
800
+ goto fail;
801
+ }
802
+ yep_view v = {p + t.at, (uint32_t)(t.end - t.at)};
803
+ d->nodes[sid].value = dom_str_in(d, &v, 1);
804
+ d->nodes[sid].tag_id =
805
+ t.cls == '#' ? yep_resolve_number(r, t.is_float) : r->resolve(NULL, v.p, v.len);
806
+ if (dom_place(d, sid) != 0) {
807
+ goto fail;
808
+ }
809
+ break;
810
+ }
811
+ }
812
+ }
813
+ d->depth = entry_depth;
814
+ if (w.long_key) {
815
+ dom_flow_stage_reset(d);
816
+ return 2; /* grammar-valid; pass 2 owns the simple-key error */
817
+ }
818
+ d->flow_staged = 1;
819
+ d->flow_stage_root = root;
820
+ d->flow_stage_anchor = anchor_id;
821
+ *close = w.close;
822
+ return 1;
823
+ fail:
824
+ d->depth = entry_depth;
825
+ dom_flow_stage_reset(d);
826
+ return -1;
827
+ }
828
+
829
+ int dom_on_flow_commit(void* ctx) {
830
+ yep_dom* d = (yep_dom*)ctx;
831
+ if (!d->flow_staged) {
832
+ return 0;
833
+ }
834
+ d->flow_staged = 0;
835
+ if (d->flow_stage_anchor != 0 &&
836
+ dom_anchor_set(d, d->flow_stage_anchor, d->flow_stage_root) != 0) {
837
+ return -1;
838
+ }
839
+ if (dom_place(d, d->flow_stage_root) != 0) {
840
+ return -1;
841
+ }
842
+ return 1;
843
+ }
844
+
845
+ void dom_on_flow_rollback(void* ctx) {
846
+ yep_dom* d = (yep_dom*)ctx;
847
+ if (d->flow_staged) {
848
+ dom_flow_stage_reset(d);
849
+ }
850
+ }
851
+
852
+ /* The sink's block-open fast path (TODO.restructure/57): a `key:` line
853
+ * whose value follows becomes the map node plus its pending key in one
854
+ * call — the DOM stack mirrors what the MAP_START event would have
855
+ * pushed. 0 only for the depth cap (the engine then takes the events,
856
+ * which own the error). */
857
+ int dom_on_block_open(void* ctx, const yep_view* key, uint32_t line, uint16_t key_col) {
858
+ yep_dom* d = (yep_dom*)ctx;
859
+ if (d->depth >= YEP_DOM_MAX_DEPTH) {
860
+ return 0;
861
+ }
862
+ const yep_resolver* r = dom_resolver(d);
863
+ uint32_t mid = dom_open_node(d, YEP_DOM_MAPPING, NULL, NULL, 0, 0, 0, 0);
864
+ if (mid == UINT32_MAX) {
865
+ return -1;
866
+ }
867
+ if (dom_place(d, mid) != 0) {
868
+ return -1;
869
+ }
870
+ d->map_pending_key[d->depth] = 0;
871
+ d->stack[d->depth++] = mid;
872
+ uint32_t kid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
873
+ if (kid == UINT32_MAX) {
874
+ d->depth--;
875
+ return -1;
876
+ }
877
+ d->nodes[kid].value = dom_str_in(d, key, 1);
878
+ d->nodes[kid].tag_id = r->resolve(NULL, key->p, key->len);
879
+ return dom_place(d, kid) == 0 ? 1 : -1;
880
+ }
881
+
882
+ /* The sink's dash-item fast path (TODO.restructure/78): one node
883
+ * through the node-init law, placed into the sequence the engine
884
+ * opened before offering — node-for-node the event path's item. */
885
+ int dom_on_block_item(void* ctx, const yep_block_value* v) {
886
+ yep_dom* d = (yep_dom*)ctx;
887
+ const yep_resolver* r = dom_resolver(d);
888
+ uint32_t vid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
889
+ if (vid == UINT32_MAX) {
890
+ return -1;
891
+ }
892
+ d->nodes[vid].value = dom_str_in(d, &v->value, v->borrowed);
893
+ d->nodes[vid].tag_id = r->resolve(NULL, v->value.p, v->value.len);
894
+ return dom_place(d, vid) == 0 ? 1 : -1;
895
+ }
896
+
897
+ /* The sink's block fast path (TODO.restructure/54): one classified
898
+ * `key: value` line becomes two nodes — the key scalar resolved like
899
+ * every implicit key event, the value through its class — placed with
900
+ * the shared pairing law. The engine offers the line BEFORE emitting;
901
+ * returning 0 here (only for an unbound alias target, which cannot
902
+ * happen when the engine resolved the name) falls back to the two
903
+ * events with nothing built. */
904
+ int dom_on_block_pair(void* ctx, const yep_view* key, const yep_block_value* v, uint32_t line,
905
+ uint16_t key_col, uint16_t val_col) {
906
+ yep_dom* d = (yep_dom*)ctx;
907
+ uint32_t target = 0;
908
+ if (v->cls == YEP_LVAL_ALIAS) {
909
+ target = dom_anchor_get(d, v->anchor_id);
910
+ if (target == UINT32_MAX) {
911
+ return 0;
912
+ }
913
+ }
914
+ const yep_resolver* r = dom_resolver(d);
915
+ uint32_t kid = dom_open_node(d, YEP_DOM_SCALAR, NULL, NULL, 0, YEP_STYLE_PLAIN, 1, 0);
916
+ if (kid == UINT32_MAX) {
917
+ return -1;
918
+ }
919
+ d->nodes[kid].value = dom_str_in(d, key, 1);
920
+ d->nodes[kid].tag_id = r->resolve(NULL, key->p, key->len);
921
+ if (dom_place(d, kid) != 0) {
922
+ return -1;
923
+ }
924
+ if (v->cls == YEP_LVAL_ALIAS) {
925
+ uint32_t vid = dom_open_node(d, YEP_DOM_ALIAS, NULL, NULL, 0, 0, 0, 0);
926
+ if (vid == UINT32_MAX) {
927
+ return -1;
928
+ }
929
+ d->nodes[vid].value = dom_str_in(d, &v->value, 1); /* borrowed — the
930
+ event path stamps it too now (73); the gates compare decoded
931
+ views, not arena placement */
932
+ d->nodes[vid].target = target;
933
+ return dom_place(d, vid) == 0 ? 1 : -1;
934
+ }
935
+ int anchored = (v->cls == YEP_LVAL_ANCHOR_PLAIN);
936
+ uint32_t vid = dom_open_node(d, YEP_DOM_SCALAR, NULL, anchored ? &v->anchor : NULL, v->borrowed,
937
+ YEP_STYLE_PLAIN, 1, 0);
938
+ if (vid == UINT32_MAX) {
939
+ return -1;
940
+ }
941
+ d->nodes[vid].value = dom_str_in(d, &v->value, v->borrowed);
942
+ d->nodes[vid].tag_id = r->resolve(NULL, v->value.p, v->value.len);
943
+ if (anchored && v->anchor_id != 0 && dom_anchor_set(d, v->anchor_id, vid) != 0) {
944
+ return -1;
945
+ }
946
+ return dom_place(d, vid) == 0 ? 1 : -1;
947
+ }
948
+
949
+ /* The direct scalar path (TODO.restructure/79): the YEP_EV_SCALAR
950
+ * case's node build, minus the event seam. The engine resolved
951
+ * tag_id (the typing SSOT); everything else is the case body. */
952
+ int dom_on_scalar(void* ctx, const yep_view* value, const yep_view* tag, const yep_view* anchor,
953
+ uint32_t anchor_id, uint8_t tag_id, uint8_t style, uint8_t implicit, uint8_t flow,
954
+ int borrowed) {
955
+ yep_dom* d = (yep_dom*)ctx;
956
+ uint32_t id = dom_open_node(d, YEP_DOM_SCALAR, tag, anchor, borrowed, style, implicit, flow);
957
+ if (id == UINT32_MAX) {
958
+ return -1;
959
+ }
960
+ d->nodes[id].value = dom_str_in(d, value, borrowed);
961
+ d->nodes[id].tag_id = tag_id;
962
+ if (anchor_id != 0 && dom_anchor_set(d, anchor_id, id) != 0) {
963
+ return -1;
964
+ }
965
+ return dom_place(d, id);
966
+ }
967
+
968
+ const yep_dnode* yep_dom_node(const yep_dom* d, uint32_t id) {
969
+ if (d == NULL || id >= d->ncount) {
970
+ return NULL;
971
+ }
972
+ return &d->nodes[id];
973
+ }