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,1074 @@
1
+ /* parse.c — public API glue: encoding front-end → engine → DOM. */
2
+
3
+ #include <string.h>
4
+
5
+ #include "common/simd_text.h"
6
+ #include "doc.h"
7
+ #include "dom/dom.h"
8
+ #include "dom/ytape.h"
9
+ #include "encoding/encoding.h"
10
+ #include "memory/allocator.h"
11
+ #include "memory/pool.h"
12
+ #include "parse/engine.h"
13
+ #include "parse/numbers.h"
14
+ #include "resolve/resolver.h"
15
+ #include "scan/json.h"
16
+ #include "tape_in.h"
17
+
18
+ #include "parse/events.h"
19
+ #include <errno.h>
20
+ #include <math.h>
21
+ #include <stdlib.h>
22
+
23
+ #include <yeptris.h>
24
+
25
+ YEPTRIS_API const char* yeptris_last_error(uint32_t* line, uint32_t* col) {
26
+ const yep_error* tls = yep_error_tls();
27
+ if (line != NULL) {
28
+ *line = tls->line;
29
+ }
30
+ if (col != NULL) {
31
+ *col = tls->col;
32
+ }
33
+ return tls->msg;
34
+ }
35
+
36
+ static YeptrisDocument parse_impl(const char* buf, size_t len, const YeptrisParseOptions* opts,
37
+ int json_mode, int lazy, YeptrisStatus* status);
38
+
39
+ /* The schema -> resolver dispatch point (OCP: the typing SSOT's ONE
40
+ * switch). Every build route selects its resolver here — the event
41
+ * entry, the engine and the direct-builder DOM, the fused block
42
+ * runner (and validate/stream.c's event route, through the event
43
+ * entry). */
44
+ const yep_resolver* resolver_for_schema(YeptrisSchema schema) {
45
+ switch (schema) {
46
+ case YEPTRIS_SCHEMA_11_COMPAT:
47
+ return yep_resolver_compat11();
48
+ case YEPTRIS_SCHEMA_12_JSON:
49
+ return yep_resolver_json12();
50
+ case YEPTRIS_SCHEMA_12_FULL:
51
+ return yep_resolver_full12();
52
+ default:
53
+ return yep_resolver_core12(); /* 12_CORE and the zero default */
54
+ }
55
+ }
56
+
57
+ YEPTRIS_API YeptrisDocument yeptris_parse(const char* buf, size_t len, YeptrisStatus* status) {
58
+ return yeptris_parse_ex(buf, len, NULL, status);
59
+ }
60
+
61
+ /* #342 slice 2: materialize the deferred tree on first access. The
62
+ * gate-clean JSON route carries the parsed TAPE (simdjson's design:
63
+ * its "DOM" is a tape) and pays dom_from_tape only when a consumer
64
+ * touches the tree — parse-only workloads never build nodes. */
65
+ yep_dom* yep_doc_dom(yeptris_document* doc) {
66
+ if (doc == NULL) {
67
+ return NULL;
68
+ }
69
+ /* the midx discipline (the #157 TSAN lesson): EVERY dom read and
70
+ * write rides the mutex — the unlock publishes the built tree
71
+ * (readers get their happens-before through the lock), so the
72
+ * plain field needs no atomics. Entry-level only: node walks ride
73
+ * handles created after this returns. */
74
+ yep_mutex_lock(&doc->lazy_mu);
75
+ if (doc->dom == NULL && doc->lazy_tape != NULL) {
76
+ if (doc->lazy_kind == 1) {
77
+ /* #378: the YAML record tape — replay through the eager
78
+ * builders; the finish pool (whose spans the records
79
+ * carry) dies once the builders copied what must outlive
80
+ * it */
81
+ yep_ytape* t = (yep_ytape*)doc->lazy_tape;
82
+ yep_dom* dom = yep_dom_create(doc->sys);
83
+ if (dom != NULL) {
84
+ dom->resolver = resolver_for_schema(doc->schema);
85
+ }
86
+ if (dom != NULL && dom_from_ytape(dom, t) == 0) {
87
+ doc->dom = dom;
88
+ ytap_free(t);
89
+ yep_free(doc->sys, t);
90
+ doc->lazy_tape = NULL;
91
+ yep_pool_destroy((yep_pool*)doc->finish_pool);
92
+ doc->finish_pool = NULL;
93
+ } else {
94
+ yep_dom_destroy(dom);
95
+ /* the tape stays: a later access retries, or free
96
+ * drops it (replay fails only on allocation/depth, as
97
+ * the builders do) */
98
+ }
99
+ } else {
100
+ yeptris_json_tape* t = (yeptris_json_tape*)doc->lazy_tape;
101
+ yep_dom* dom = yep_dom_create(doc->sys);
102
+ if (dom != NULL && dom_from_tape(dom, t) == 0) {
103
+ doc->dom = dom;
104
+ yeptris_tape_free(t);
105
+ yep_free(doc->sys, t);
106
+ doc->lazy_tape = NULL;
107
+ } else {
108
+ yep_dom_destroy(dom);
109
+ /* the tape stays: a later access retries, or free
110
+ * drops it (the materializer only fails on malformed
111
+ * NUM spans, which the strict gate already rejected at
112
+ * parse) */
113
+ }
114
+ }
115
+ }
116
+ yep_dom* dom = doc->dom;
117
+ yep_mutex_unlock(&doc->lazy_mu);
118
+ return dom;
119
+ }
120
+
121
+ /* The strict-JSON document wrapper (both routes share it). */
122
+ static YeptrisDocument yep_json_doc_wrap(yep_dom* dom, const char* buf, size_t len,
123
+ const yep_allocator* sys, YeptrisStatus* status) {
124
+ (void)len;
125
+ yeptris_document* doc = yep_alloc(sys, sizeof(yeptris_document));
126
+ if (doc == NULL) {
127
+ yep_dom_destroy(dom);
128
+ if (status != NULL) {
129
+ *status = YEPTRIS_ERROR_MEMORY;
130
+ }
131
+ return NULL;
132
+ }
133
+ yep_mutex_init(&doc->lazy_mu);
134
+ doc->dom = dom;
135
+ doc->sys = sys;
136
+ doc->lazy_tape = NULL;
137
+ doc->lazy_kind = 0;
138
+ doc->schema = YEPTRIS_SCHEMA_12_CORE; /* strict JSON is core by construction */
139
+ doc->transcoded = NULL;
140
+ doc->transcoded_len = 0;
141
+ doc->input = buf;
142
+ doc->finish_pool = NULL;
143
+ if (status != NULL) {
144
+ *status = YEPTRIS_OK;
145
+ }
146
+ return (YeptrisDocument)doc;
147
+ }
148
+
149
+ YEPTRIS_API YeptrisDocument yeptris_parse_json(const char* buf, size_t len, YeptrisStatus* status) {
150
+ YeptrisStatus st = YEPTRIS_OK;
151
+ if (buf == NULL && len != 0) {
152
+ st = YEPTRIS_ERROR_ARG;
153
+ goto jfail;
154
+ }
155
+ /* Clean-walk fast route (TODO.restructure/81 stage 2, #342): the
156
+ * fused walk runs clean_only — tabs and non-ASCII bytes REJECT —
157
+ * and ANY reject falls to the original sequence below, whose error
158
+ * precedence is byte-for-byte the pinned behavior
159
+ * (json-suite-strict gates this). The entry's full-input gate_scan
160
+ * + tab-memchr pre-passes are GONE: the walk's own byte guarantees
161
+ * route identically by construction, and the strict path re-gates
162
+ * only when actually taken (the profile's 5% + 2% lanes). */
163
+ {
164
+ size_t off = 0;
165
+ while (off < len &&
166
+ (buf[off] == ' ' || buf[off] == '\t' || buf[off] == '\n' || buf[off] == '\r')) {
167
+ off++;
168
+ }
169
+ if (off < len && (buf[off] == '[' || buf[off] == '{')) {
170
+ const yep_allocator* sys = yep_system_allocator();
171
+ yeptris_json_tape* t = yep_alloc(sys, sizeof(*t));
172
+ if (t == NULL) {
173
+ st = YEPTRIS_ERROR_MEMORY;
174
+ goto jfail;
175
+ }
176
+ if (yep_tape_walk_lenient_fused(buf, len, off, t, 1, 1, 0) == YEPTRIS_OK &&
177
+ !yep_tape_long_simple_key(t)) {
178
+ /* strict_nums=1: the arms validated the numbers at
179
+ * record time — the settle pass is gone (the profile's
180
+ * 10% lane; the spans were cache-warm in the walk).
181
+ * The simple-key law: a >1024 flow key cannot
182
+ * re-serialize — the engine's clean error decides (run
183
+ * 38143486175) */
184
+ YeptrisDocument h = yep_json_doc_wrap(NULL, buf, len, sys, status);
185
+ if (h != NULL) {
186
+ ((yeptris_document*)h)->lazy_tape = t;
187
+ ((yeptris_document*)h)->lazy_kind = 0;
188
+ return h;
189
+ }
190
+ yeptris_tape_free(t); /* wrap failed (memory): below */
191
+ } else {
192
+ yeptris_tape_free(t); /* reject (incl. clean-route tabs
193
+ and non-ASCII): below */
194
+ }
195
+ yep_free(sys, t);
196
+ }
197
+ }
198
+ size_t verr = 0;
199
+ if (!yep_json_document(buf, len, &verr)) {
200
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, verr,
201
+ "not strict JSON at byte %zu", verr);
202
+ st = YEPTRIS_ERROR_PARSE;
203
+ goto jfail;
204
+ }
205
+ /* the fallback re-gates: a buffer here was walk-rejected, so its
206
+ * printability decides whether UTF-8 validation is owed */
207
+ int gated = len > 0 && yep_text_active()->gate_scan(buf, len);
208
+ size_t uerr = 0;
209
+ if (gated && !yep_utf8_validate((const unsigned char*)buf, len, &uerr)) {
210
+ yep_error_set(yep_error_tls(), YEP_ERR_ENCODING, 0, 0, uerr, "ill-formed UTF-8 at byte %zu",
211
+ uerr);
212
+ st = YEPTRIS_ERROR_ENCODING;
213
+ goto jfail;
214
+ }
215
+ /* Direct DOM construction (TODO.impl/27): the validated buffer
216
+ * needs no engine — one walk builds the tree, skipping the event
217
+ * pipeline. A builder/validator disagreement defers to the engine
218
+ * (never happens in-tree; belt and braces). */
219
+ {
220
+ const yep_allocator* sys = yep_system_allocator();
221
+ yep_dom* dom = yep_dom_create(sys);
222
+ if (dom == NULL) {
223
+ st = YEPTRIS_ERROR_MEMORY;
224
+ goto jfail;
225
+ }
226
+ dom->input_base = buf; /* strict JSON is UTF-8 by definition */
227
+ dom->input_len = len;
228
+ yep_dom_prepare_len(dom, len);
229
+ int brc = yep_dom_build_json(dom, buf, len);
230
+ if (brc == -1) {
231
+ yep_dom_destroy(dom);
232
+ st = YEPTRIS_ERROR_MEMORY;
233
+ goto jfail;
234
+ }
235
+ if (brc == -2) {
236
+ yep_dom_destroy(dom);
237
+ return parse_impl(buf, len, NULL, 1, 0, status);
238
+ }
239
+ return yep_json_doc_wrap(dom, buf, len, sys, status);
240
+ }
241
+ jfail:
242
+ if (status != NULL) {
243
+ *status = st;
244
+ }
245
+ return NULL;
246
+ }
247
+
248
+ /* The shared parse front-end (the encoding law, one home): BOM sniff;
249
+ * borrow UTF-8, transcode the rest; printability; NEL normalization.
250
+ * Returns 0 ok, -1 failed (*st carries ENCODING). *transcoded owns any
251
+ * copy (the system allocator frees it); *data and *data_len point at
252
+ * the bytes the engine runs on. json_mode skips the YAML-only passes (the
253
+ * JSON reader validated its own grammar and well-formedness at the
254
+ * entry and keeps U+0085 raw inside strings). */
255
+ static int parse_frontend(const char* buf, size_t len, const yep_allocator* sys, int json_mode,
256
+ const char** data_out, size_t* data_len_out,
257
+ unsigned char** transcoded_out, size_t* transcoded_len_out,
258
+ int* norm_kept_raw_out, YeptrisStatus* st) {
259
+ /* a NULL/0-byte stream is a valid empty input: parse it through a
260
+ * non-null base so the BOM arithmetic never computes NULL+0
261
+ * (clang's UBSan flags the zero-offset-null form; gcc's does not —
262
+ * the schema-econ BadInputs pin surfaced it on the macOS arm64 leg) */
263
+ const char* src = buf != NULL ? buf : "";
264
+ const char* data = src;
265
+ size_t data_len = len;
266
+ unsigned char* transcoded = NULL;
267
+ size_t transcoded_len = 0;
268
+ yep_encoding enc = YEP_ENC_UNKNOWN;
269
+ int norm_kept_raw = 0; /* raw NEL/LS/PS survived the quote-aware
270
+ * walk: the engine route owns the document
271
+ * (the tape runner predates the cut) */
272
+
273
+ if (json_mode) {
274
+ /* JSON mode validated grammar and UTF-8 well-formedness at the
275
+ * entry: no BOM, no transcoding, and never the printable set —
276
+ * RFC 8259 allows DEL and noncharacters that YAML c-printable
277
+ * excludes. The engine takes the bytes directly. */
278
+ *data_out = data;
279
+ *data_len_out = data_len;
280
+ *transcoded_out = NULL;
281
+ *transcoded_len_out = 0;
282
+ *norm_kept_raw_out = 0;
283
+ return 0;
284
+ }
285
+
286
+ size_t bom = yep_bom_sniff((const unsigned char*)src, len, &enc);
287
+ data = src + bom;
288
+ data_len = len - bom;
289
+
290
+ if (enc == YEP_ENC_UTF16LE || enc == YEP_ENC_UTF16BE || enc == YEP_ENC_UTF32LE ||
291
+ enc == YEP_ENC_UTF32BE) {
292
+ size_t terr = 0;
293
+ int rc = yep_transcode_to_utf8(sys, enc, (const unsigned char*)data, data_len, &transcoded,
294
+ &transcoded_len, &terr);
295
+ if (rc != 0) {
296
+ yep_error_set(yep_error_tls(), YEP_ERR_ENCODING, 0, 0, terr,
297
+ "ill-formed input encoding");
298
+ yep_free(sys, transcoded);
299
+ *st = YEPTRIS_ERROR_ENCODING;
300
+ return -1;
301
+ }
302
+ data = (const char*)transcoded;
303
+ data_len = transcoded_len;
304
+ size_t pverr = 0;
305
+ if (!yep_printable_validate((const unsigned char*)data, data_len, &pverr)) {
306
+ yep_error_set(yep_error_tls(), YEP_ERR_ENCODING, 0, 0, pverr,
307
+ "non-printable character at byte %zu", pverr);
308
+ yep_free(sys, transcoded);
309
+ *st = YEPTRIS_ERROR_ENCODING;
310
+ return -1;
311
+ }
312
+ } else {
313
+ /* The encoding gate rides the cheap gate_scan kernel; sizing
314
+ * rides length heuristics (TODO.restructure/66: the fused
315
+ * multi-class stats sweep cost ~12% on the ubuntu asymmetry
316
+ * shapes — ryml pays no pre-pass). Pure gate-safe input skips
317
+ * the SWAR validator entirely; anything else falls to it for
318
+ * the authoritative answer + error position. */
319
+ if (yep_text_active()->gate_scan(data, data_len)) {
320
+ size_t verr = 0;
321
+ if (!yep_printable_validate((const unsigned char*)data, data_len, &verr)) {
322
+ yep_error_set(yep_error_tls(), YEP_ERR_ENCODING, 0, 0, verr,
323
+ "ill-formed or non-printable UTF-8 at byte %zu", verr);
324
+ *st = YEPTRIS_ERROR_ENCODING;
325
+ return -1;
326
+ }
327
+ }
328
+ }
329
+
330
+ /* Unicode breaks (NEL/LS/PS) normalize to '\n' BEFORE the engine:
331
+ * the copy rides `transcoded`, whose ownership plumbing (input_base
332
+ * views, doc free, fail paths) already exists. Letting the engine's
333
+ * per-feed copy fire here returned views into ENGINE-owned memory
334
+ * that dies at yeptris_parse teardown — every one-shot NEL document
335
+ * dangled (no test ever covered one; found by the NormBreakLeads
336
+ * slice, 2026-09-27). JSON mode skips this: the JSON reader keeps
337
+ * U+0085 raw inside strings, only the YAML reader normalizes. */
338
+ {
339
+ size_t nlen = 0;
340
+ char* norm = yep_engine_normalize_breaks(sys, data, data_len, &nlen, &norm_kept_raw);
341
+ if (norm != NULL) {
342
+ yep_free(sys, transcoded);
343
+ transcoded = (unsigned char*)norm;
344
+ transcoded_len = nlen;
345
+ data = norm;
346
+ data_len = nlen;
347
+ }
348
+ }
349
+
350
+ *data_out = data;
351
+ *data_len_out = data_len;
352
+ *transcoded_out = transcoded;
353
+ *transcoded_len_out = transcoded_len;
354
+ *norm_kept_raw_out = norm_kept_raw;
355
+ return 0;
356
+ }
357
+
358
+ /* parse.c's sink-entry seam (#902): the DOM route's exact front-end and
359
+ * engine run into a caller sink — no DOM, no tape. Statuses are the
360
+ * parse route's (OK, or PARSE / DEPTH / ENCODING / MEMORY; the sink's
361
+ * own failure rides MEMORY like the DOM builder's). on_input (may be
362
+ * NULL) receives the run's input view — the DOM route's input_base law
363
+ * (the transcoded copy, or the caller's bytes) and its post-BOM length
364
+ * — immediately before the engine runs, because the events' borrowed
365
+ * views subtract from it. The bytes live only for the run. */
366
+ YeptrisStatus yep_parse_events(const char* buf, size_t len, YeptrisSchema schema,
367
+ const yep_sink* sink,
368
+ void (*on_input)(void* ctx, const char* base, size_t len),
369
+ void* on_ctx) {
370
+ if (sink == NULL || (buf == NULL && len != 0)) {
371
+ return YEPTRIS_ERROR_ARG;
372
+ }
373
+ const yep_allocator* sys = yep_system_allocator();
374
+ const char* data = NULL;
375
+ size_t data_len = 0;
376
+ unsigned char* transcoded = NULL;
377
+ size_t transcoded_len = 0;
378
+ int norm_kept_raw = 0;
379
+ YeptrisStatus st = YEPTRIS_OK;
380
+ if (parse_frontend(buf, len, sys, 0, &data, &data_len, &transcoded, &transcoded_len,
381
+ &norm_kept_raw, &st) != 0) {
382
+ return st;
383
+ }
384
+ yep_engine* eng = yep_engine_create(sys);
385
+ if (eng == NULL) {
386
+ yep_free(sys, transcoded);
387
+ return YEPTRIS_ERROR_MEMORY;
388
+ }
389
+ /* the typing SSOT: the family's resolver (resolver_for_schema is
390
+ * the one switch) */
391
+ yep_engine_set_resolver(eng, resolver_for_schema(schema));
392
+ if (schema == YEPTRIS_SCHEMA_11_COMPAT) {
393
+ /* the compat schema is the psych/libyaml surface (see
394
+ * parse_impl's twin comment) */
395
+ yep_engine_set_compat_grammar(eng, 1);
396
+ }
397
+ {
398
+ /* the nametab reserve survives: a memchr chain for '&' is far
399
+ * cheaper than the class sweep it replaced */
400
+ yep_text_stats amp_only = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
401
+ const char* q = data == NULL ? "" : data;
402
+ const char* end = q + data_len;
403
+ if (q != NULL) {
404
+ while ((q = memchr(q, '&', (size_t)(end - q))) != NULL) {
405
+ amp_only.amp++;
406
+ q++;
407
+ }
408
+ }
409
+ yep_engine_prepare(eng, &amp_only);
410
+ }
411
+ yep_engine_front_done(eng);
412
+ if (on_input != NULL) {
413
+ on_input(on_ctx, transcoded ? (const char*)transcoded : buf, data_len);
414
+ }
415
+ int rc = yep_engine_run(eng, data, data_len, sink);
416
+ if (rc != 0) {
417
+ /* the error CODE decides the public status — the parse route's
418
+ * law (parse_impl carries the full comment): a CLEAN channel
419
+ * under a failed run means the SINK failed */
420
+ const yep_error* ee = yep_engine_error(eng);
421
+ yep_err_code code = (ee && ee->code != YEP_ERR_NONE) ? ee->code : YEP_ERR_MEMORY;
422
+ if (ee) {
423
+ yep_error* tls = yep_error_tls();
424
+ *tls = *ee;
425
+ }
426
+ yep_pool* finish = yep_engine_detach_pool(eng);
427
+ yep_pool_destroy(finish);
428
+ yep_engine_destroy(eng);
429
+ yep_free(sys, transcoded);
430
+ return (code == YEP_ERR_MEMORY) ? YEPTRIS_ERROR_MEMORY
431
+ : (code == YEP_ERR_DEPTH) ? YEPTRIS_ERROR_DEPTH
432
+ : YEPTRIS_ERROR_PARSE;
433
+ }
434
+ yep_pool* finish = yep_engine_detach_pool(eng);
435
+ yep_pool_destroy(finish); /* nothing borrows the finish pool: every
436
+ * consumer copies (snapshot law) */
437
+ yep_engine_destroy(eng);
438
+ yep_free(sys, transcoded);
439
+ return YEPTRIS_OK;
440
+ }
441
+
442
+ static YeptrisDocument parse_impl(const char* buf, size_t len, const YeptrisParseOptions* opts,
443
+ int json_mode, int lazy, YeptrisStatus* status) {
444
+ YeptrisStatus st = YEPTRIS_OK;
445
+ if (buf == NULL && len != 0) {
446
+ st = YEPTRIS_ERROR_ARG;
447
+ goto fail;
448
+ }
449
+
450
+ const yep_allocator* sys = yep_system_allocator();
451
+
452
+ /* Encoding front-end (parse_frontend owns the law): BOM sniff;
453
+ * borrow UTF-8, transcode the rest; NEL normalization. */
454
+ const char* data = NULL;
455
+ size_t data_len = 0;
456
+ unsigned char* transcoded = NULL;
457
+ size_t transcoded_len = 0;
458
+ int norm_kept_raw = 0; /* raw NEL/LS/PS survived the quote-aware
459
+ * walk: the engine route owns the document
460
+ * (the tape runner predates the cut) */
461
+ char* nbuf = NULL; /* the engine's break-normalizer copy (#953):
462
+ * DOM and tape scalar slices point INTO it,
463
+ * so the document must own it like the
464
+ * finish pool and transcoded */
465
+ size_t nblen = 0;
466
+
467
+ if (parse_frontend(buf, len, sys, json_mode, &data, &data_len, &transcoded, &transcoded_len,
468
+ &norm_kept_raw, &st) != 0) {
469
+ goto fail;
470
+ }
471
+
472
+ /* Engine → DOM. */
473
+ const YeptrisSchema schema = opts != NULL ? opts->schema : YEPTRIS_SCHEMA_12_CORE;
474
+ yep_engine* eng = NULL;
475
+ eng = yep_engine_create(sys);
476
+ if (opts != NULL) {
477
+ /* the typing SSOT: the family's resolver (resolver_for_schema
478
+ * is the one switch) */
479
+ yep_engine_set_resolver(eng, resolver_for_schema(schema));
480
+ if (opts->schema == YEPTRIS_SCHEMA_11_COMPAT) {
481
+ /* the compat schema is the psych/libyaml surface: its grammar
482
+ * must accept what libyaml accepts (the flow floor is suite-
483
+ * strict, not libyaml — see yep_engine_set_compat_grammar) */
484
+ yep_engine_set_compat_grammar(eng, 1);
485
+ }
486
+ if (opts->max_depth > 0) {
487
+ yep_engine_set_max_depth(eng, opts->max_depth);
488
+ }
489
+ }
490
+ if (eng == NULL) {
491
+ yep_free(sys, transcoded);
492
+ st = YEPTRIS_ERROR_MEMORY;
493
+ goto fail;
494
+ }
495
+ /* the lazy route builds NO tree at parse: the dom (and its whole
496
+ * setup) exists only for the eager sink (the ASAN leak lesson —
497
+ * an unused dom leaked per lazy parse) */
498
+ yep_dom* dom = NULL;
499
+ if (!lazy) {
500
+ dom = yep_dom_create(sys);
501
+ if (dom == NULL) {
502
+ yep_engine_destroy(eng);
503
+ yep_free(sys, transcoded);
504
+ st = YEPTRIS_ERROR_MEMORY;
505
+ goto fail;
506
+ }
507
+ /* the direct builders resolve with the engine's schema (the typing
508
+ * SSOT — one resolver decision per scalar, whichever path builds) */
509
+ dom->resolver = resolver_for_schema(schema);
510
+ /* BEFORE the run: input-slice strings borrow as views (zero copy)
511
+ * — the document keeps `transcoded` alive for exactly this. Set
512
+ * after, every scalar was arena-copied (found 2026-09-10). */
513
+ dom->input_len = data_len;
514
+ dom->input_base = transcoded ? (const char*)transcoded : buf;
515
+ yep_dom_prepare_len(dom, data_len);
516
+ }
517
+ if (!json_mode) {
518
+ /* the nametab reserve survives: a memchr chain for '&' is
519
+ * far cheaper than the class sweep it replaced. JSON skips it
520
+ * entirely — no anchors exist (the profile's 4.4% memchr
521
+ * lane on the json corpora). */
522
+ yep_text_stats amp_only = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
523
+ /* (NULL, 0) passes the arg guard: NULL + 0 is UB, not an
524
+ * empty range (UBSan, the first nightly under the combined
525
+ * fuzz build) */
526
+ const char* q = data == NULL ? "" : data;
527
+ const char* end = q + data_len;
528
+ if (q != NULL) {
529
+ while ((q = memchr(q, '&', (size_t)(end - q))) != NULL) {
530
+ amp_only.amp++;
531
+ q++;
532
+ }
533
+ }
534
+ yep_engine_prepare(eng, &amp_only);
535
+ }
536
+
537
+ yep_ytape yt = {0}; /* zeroed: MSVC cannot couple have_tape to ytap_init's fill */
538
+ int have_tape = 0;
539
+ yep_sink sink;
540
+ if (lazy) {
541
+ /* #378: the recorder rides the SAME engine run — the fused
542
+ * arms stay hot, the builders' node lane defers to replay */
543
+ if (ytap_init(&yt, data, data_len, 0) != 0) {
544
+ yep_engine_destroy(eng);
545
+ yep_free(sys, transcoded);
546
+ st = YEPTRIS_ERROR_MEMORY;
547
+ goto fail;
548
+ }
549
+ ytap_sink(&yt, &sink);
550
+ have_tape = 1;
551
+ } else {
552
+ sink = (yep_sink){.on_event = yep_dom_on_event,
553
+ .ctx = dom,
554
+ .on_flow_build = dom_on_flow_build,
555
+ .on_flow_commit = dom_on_flow_commit,
556
+ .on_flow_rollback = dom_on_flow_rollback,
557
+ .on_block_pair = dom_on_block_pair,
558
+ .on_scalar = dom_on_scalar,
559
+ .on_block_open = dom_on_block_open,
560
+ .on_block_item = dom_on_block_item,
561
+ .compose = 1};
562
+ }
563
+ int rc = 0;
564
+ if (have_tape && !norm_kept_raw && ytap_flow_rooted(&yt) == 0) {
565
+ /* a pure flow-rooted document: the classification ran once and
566
+ * the scaffolding landed — neither runner nor engine runs */
567
+ yep_engine_destroy(eng);
568
+ eng = NULL;
569
+ } else if (have_tape && !norm_kept_raw &&
570
+ ytap_fused_run(&yt, resolver_for_schema(schema)) == 0) {
571
+ /* the fused block runner owned the whole document — the engine
572
+ * never runs (TODO.restructure/79). Its only cost was the parse
573
+ * front-end above; on a bail it unwound and the engine runs. */
574
+ yep_engine_destroy(eng);
575
+ eng = NULL;
576
+ } else {
577
+ if (have_tape) {
578
+ ytap_reset(&yt);
579
+ }
580
+ yep_engine_front_done(eng);
581
+ rc = yep_engine_run(eng, data, data_len, &sink);
582
+ }
583
+ if (rc != 0) {
584
+ const yep_error* ee = yep_engine_error(eng);
585
+ /* a CLEAN channel under a failed run means the SINK failed — a
586
+ * builder ran out of memory (the DOM's oom law), the bytes
587
+ * never were a parse error. Mapping it to PARSE let
588
+ * yep_dom_str_put's swallowed OOM surface as a syntax error
589
+ * (the audit's finding: silent doc corruption reads worse). */
590
+ yep_err_code code = (ee && ee->code != YEP_ERR_NONE) ? ee->code : YEP_ERR_MEMORY;
591
+ if (ee) {
592
+ yep_error* tls = yep_error_tls();
593
+ *tls = *ee;
594
+ }
595
+ if (have_tape) {
596
+ ytap_free(&yt);
597
+ }
598
+ yep_dom_destroy(dom);
599
+ yep_engine_destroy(eng);
600
+ yep_free(sys, transcoded);
601
+ st = (code == YEP_ERR_MEMORY) ? YEPTRIS_ERROR_MEMORY
602
+ : (code == YEP_ERR_DEPTH) ? YEPTRIS_ERROR_DEPTH
603
+ : YEPTRIS_ERROR_PARSE;
604
+ goto fail;
605
+ }
606
+ /* Resolved tags, folded and escaped scalars live in the engine's
607
+ * finish pool; the document must own it or every such string would
608
+ * dangle at engine teardown (found by ASAN). The break-normalizer
609
+ * copy is the same law (#953): scalar value slices in the DOM and
610
+ * the packed tape point INTO it. */
611
+ yep_pool* finish = yep_engine_detach_pool(eng);
612
+ nbuf = yep_engine_detach_norm_buf(eng, &nblen);
613
+ yep_engine_destroy(eng);
614
+
615
+ /* Empty stream (no documents): NULL document with YEPTRIS_OK. */
616
+ if (have_tape ? (yt.docs == 0) : (dom->dcount == 0)) {
617
+ if (have_tape) {
618
+ ytap_free(&yt);
619
+ } else {
620
+ yep_dom_destroy(dom);
621
+ }
622
+ yep_pool_destroy(finish);
623
+ yep_free(sys, transcoded);
624
+ if (nbuf != NULL) {
625
+ yep_free(sys, nbuf); /* not parked: no document survives */
626
+ }
627
+ if (status != NULL) {
628
+ *status = YEPTRIS_OK;
629
+ }
630
+ return NULL;
631
+ }
632
+ if (!have_tape) {
633
+ /* DOM strings are input-offsets or arena copies; nothing in the
634
+ * tree references the finish pool anymore — release it now */
635
+ yep_pool_destroy(finish);
636
+ finish = NULL;
637
+ } /* the lazy route keeps it: the records carry pool spans until
638
+ replay copies (doc.h's finish_pool contract) */
639
+
640
+ yeptris_document* doc = yep_alloc(sys, sizeof(yeptris_document));
641
+ if (doc == NULL) {
642
+ yep_dom_destroy(dom);
643
+ yep_pool_destroy(finish);
644
+ yep_free(sys, transcoded);
645
+ if (nbuf != NULL) {
646
+ yep_free(sys, nbuf); /* not parked: no document survives */
647
+ }
648
+ st = YEPTRIS_ERROR_MEMORY;
649
+ goto fail;
650
+ }
651
+ yep_mutex_init(&doc->lazy_mu);
652
+ doc->sys = sys;
653
+ doc->schema = (opts != NULL && opts->schema != YEPTRIS_SCHEMA_12_CORE) ? opts->schema
654
+ : YEPTRIS_SCHEMA_12_CORE;
655
+ doc->transcoded = transcoded;
656
+ doc->transcoded_len = transcoded_len;
657
+ if (nbuf != NULL) {
658
+ /* #953: the engine re-normalized THIS run (raw NEL/LS/PS rode
659
+ * quoted regions past the front-end), so every engine event
660
+ * view points INTO nbuf — and the lazy tape stored those
661
+ * pointers ABSOLUTE (they fall outside yt.input's range, so
662
+ * the span recorder kept them verbatim). The document owns
663
+ * nbuf like the finish pool, whichever copy the slot held. */
664
+ if (have_tape) {
665
+ /* rebase the tape's span base onto the doc-owned copy:
666
+ * materialization's input_base offsets against LIVE bytes.
667
+ * `transcoded` is referenced by no record (the engine
668
+ * parsed nbuf only) — release it. */
669
+ yt.input = nbuf;
670
+ yt.input_len = nblen;
671
+ yep_free(sys, transcoded);
672
+ transcoded = NULL;
673
+ doc->transcoded = (unsigned char*)nbuf;
674
+ doc->transcoded_len = nblen;
675
+ } else {
676
+ /* eager: the dom copied every scalar during the run (the
677
+ * borrowed views offset against input_base = transcoded);
678
+ * nbuf has no post-run reader — the slot keeps transcoded */
679
+ yep_free(sys, nbuf);
680
+ }
681
+ }
682
+ doc->input = buf;
683
+ doc->finish_pool = finish; /* NULL on the eager route */
684
+ doc->lazy_tape = NULL;
685
+ doc->lazy_kind = 0;
686
+ if (have_tape) {
687
+ yep_ytape* heap_tape = yep_alloc(sys, sizeof(*heap_tape));
688
+ if (heap_tape == NULL) {
689
+ ytap_free(&yt);
690
+ yep_pool_destroy(finish);
691
+ yep_free(sys, transcoded);
692
+ if (nbuf != NULL) {
693
+ yep_free(sys, nbuf); /* not parked: no document survives */
694
+ }
695
+ yep_free(sys, doc);
696
+ st = YEPTRIS_ERROR_MEMORY;
697
+ goto fail;
698
+ }
699
+ *heap_tape = yt;
700
+ if (yt.pool != NULL && doc->finish_pool == NULL) {
701
+ doc->finish_pool = yt.pool; /* fused block-scalar content: the
702
+ * document owns it until replay copies */
703
+ }
704
+ doc->dom = NULL;
705
+ doc->lazy_tape = heap_tape;
706
+ doc->lazy_kind = 1;
707
+ } else {
708
+ doc->dom = dom;
709
+ }
710
+ return (YeptrisDocument)doc;
711
+
712
+ fail:
713
+ if (status != NULL) {
714
+ *status = st;
715
+ }
716
+ return NULL;
717
+ }
718
+
719
+ YEPTRIS_API YeptrisDocument yeptris_parse_ex(const char* buf, size_t len,
720
+ const YeptrisParseOptions* opts,
721
+ YeptrisStatus* status) {
722
+ /* #378 slice 2: the default YAML route carries the packed record
723
+ * tape — the tree materializes on first access (yep_doc_dom).
724
+ * Same grammar, same errors; parse-only workloads never build
725
+ * nodes. The eager form stays on yeptris_parse_eager_ex. */
726
+ return parse_impl(buf, len, opts, 0, 1, status);
727
+ }
728
+
729
+ /* #378 slice 1 (internal seam): the YAML parse rides the packed record
730
+ * tape — the tree materializes from the records on first access. Same
731
+ * grammar, same errors; parse-only workloads never build nodes. */
732
+ YeptrisDocument yeptris_parse_ytape_ex(const char* buf, size_t len, const YeptrisParseOptions* opts,
733
+ YeptrisStatus* status) {
734
+ return parse_impl(buf, len, opts, 0, 1, status);
735
+ }
736
+
737
+ /* The eager form, kept for the parse-only A/B (bench_matrix's #378
738
+ * table) and as the reference lane the differential gates ride. */
739
+ YeptrisDocument yeptris_parse_eager_ex(const char* buf, size_t len, const YeptrisParseOptions* opts,
740
+ YeptrisStatus* status) {
741
+ return parse_impl(buf, len, opts, 0, 0, status);
742
+ }
743
+
744
+ YEPTRIS_API void yeptris_document_free(YeptrisDocument handle) {
745
+ yeptris_document* doc = (yeptris_document*)handle;
746
+ if (doc == NULL) {
747
+ return;
748
+ }
749
+ if (doc->lazy_tape != NULL) { /* never materialized: free stays free */
750
+ if (doc->lazy_kind == 1) {
751
+ ytap_free((yep_ytape*)doc->lazy_tape);
752
+ } else {
753
+ yeptris_tape_free((yeptris_json_tape*)doc->lazy_tape);
754
+ }
755
+ yep_free(doc->sys, doc->lazy_tape);
756
+ }
757
+ yep_mutex_destroy(&doc->lazy_mu);
758
+ yep_dom_destroy(doc->dom);
759
+ yep_pool_destroy((yep_pool*)doc->finish_pool);
760
+ yep_free(doc->sys, doc->transcoded);
761
+ yep_free(doc->sys, doc);
762
+ }
763
+
764
+ YEPTRIS_API size_t yeptris_document_count(YeptrisDocument handle) {
765
+ yeptris_document* doc = (yeptris_document*)handle;
766
+ yep_dom* dom = doc ? yep_doc_dom(doc) : NULL; /* NULL: the lazy
767
+ * route's failed/retrying materialization — an
768
+ * unusable tree counts as zero documents */
769
+ return dom ? dom->dcount : 0;
770
+ }
771
+
772
+ yeptris_node* yep_handle_new(yeptris_document* doc, uint32_t id) {
773
+ yeptris_node* n = yep_hpool_alloc(yep_dom_handles(doc->dom), sizeof(yeptris_node), 16);
774
+ if (n == NULL) {
775
+ return NULL;
776
+ }
777
+ n->doc = doc;
778
+ n->id = id;
779
+ return n;
780
+ }
781
+
782
+ static YeptrisNode node_new_handle(yeptris_document* doc, uint32_t id) {
783
+ return (YeptrisNode)yep_handle_new(doc, id);
784
+ }
785
+
786
+ YEPTRIS_API YeptrisNode yeptris_document_root(YeptrisDocument handle, size_t index) {
787
+ yeptris_document* doc = (yeptris_document*)handle;
788
+ if (doc == NULL) {
789
+ return NULL;
790
+ }
791
+ yep_dom* dom = yep_doc_dom(doc);
792
+ if (dom == NULL || index >= dom->dcount) {
793
+ return NULL;
794
+ }
795
+ return node_new_handle(doc, dom->docs[index]);
796
+ }
797
+
798
+ static const yep_dnode* node_of(YeptrisNode handle) {
799
+ yeptris_node* n = (yeptris_node*)handle;
800
+ if (n == NULL || n->doc == NULL) {
801
+ return NULL;
802
+ }
803
+ return yep_dom_node(n->doc->dom, n->id);
804
+ }
805
+
806
+ YEPTRIS_API uint32_t yeptris_node_id(YeptrisNode handle) {
807
+ yeptris_node* n = (yeptris_node*)handle;
808
+ return n == NULL ? UINT32_MAX : n->id;
809
+ }
810
+
811
+ YEPTRIS_API YeptrisNodeKind yeptris_node_kind(YeptrisNode handle) {
812
+ const yep_dnode* n = node_of(handle);
813
+ if (n == NULL) {
814
+ return YEPTRIS_NODE_SCALAR;
815
+ }
816
+ switch (n->kind) {
817
+ case YEP_DOM_SEQUENCE:
818
+ return YEPTRIS_NODE_SEQUENCE;
819
+ case YEP_DOM_MAPPING:
820
+ return YEPTRIS_NODE_MAPPING;
821
+ case YEP_DOM_ALIAS:
822
+ return YEPTRIS_NODE_ALIAS;
823
+ default:
824
+ return YEPTRIS_NODE_SCALAR;
825
+ }
826
+ }
827
+
828
+ /* Decodes a node's compact string through its document's regions. */
829
+ static yep_view node_view(const yeptris_node* h, yep_sview sv) {
830
+ return yep_dom_view(h->doc->dom, sv);
831
+ }
832
+
833
+ static const char* view_out(yep_view v, size_t* len) {
834
+ if (len != NULL) {
835
+ *len = v.len;
836
+ }
837
+ return v.len ? v.p : (v.p ? v.p : "");
838
+ }
839
+
840
+ YEPTRIS_API const char* yeptris_node_value(YeptrisNode handle, size_t* len) {
841
+ const yep_dnode* n = node_of(handle);
842
+ if (n == NULL || (n->kind != YEP_DOM_SCALAR && n->kind != YEP_DOM_ALIAS)) {
843
+ if (len != NULL) {
844
+ *len = 0;
845
+ }
846
+ return NULL;
847
+ }
848
+ return view_out(node_view((yeptris_node*)handle, n->value), len);
849
+ }
850
+
851
+ YEPTRIS_API YeptrisScalarStyle yeptris_node_style(YeptrisNode handle) {
852
+ const yep_dnode* n = node_of(handle);
853
+ return n ? (YeptrisScalarStyle)n->style : YEPTRIS_STYLE_ANY;
854
+ }
855
+
856
+ YEPTRIS_API const char* yeptris_tag_uri(YeptrisTagId id) {
857
+ return yep_tag_uri((yep_tag_id)id);
858
+ }
859
+
860
+ YEPTRIS_API YeptrisTagId yeptris_node_tag_id(YeptrisNode handle) {
861
+ const yep_dnode* n = node_of(handle);
862
+ if (n == NULL) {
863
+ return YEPTRIS_TAG_CUSTOM;
864
+ }
865
+ /* the INTERNAL kind space here (#904 item 6): n->kind is the
866
+ * dom's yep_dom_kind — the public YEPTRIS_NODE_* constants ride
867
+ * the same values today only by coincidence, and the boundary
868
+ * conversion is yeptris_node_kind's switch alone */
869
+ if (n->kind == YEP_DOM_SEQUENCE) {
870
+ return YEPTRIS_TAG_SEQ;
871
+ }
872
+ if (n->kind == YEP_DOM_MAPPING) {
873
+ return YEPTRIS_TAG_MAP;
874
+ }
875
+ return (YeptrisTagId)n->tag_id;
876
+ }
877
+
878
+ /* strips '_' and ',' into dst (nul-terminated); returns length.
879
+ * The COMMON case (no separators) returns the borrowed view — no
880
+ * copy, no nul-termination needed by the in-place parsers. */
881
+ YEPTRIS_API YeptrisStatus yeptris_node_int(YeptrisNode handle, int64_t* out) {
882
+ /* conversion is the number kernel's alone (MECE: one fold, one
883
+ * fast path, one Psych-sexagesimal weight table) */
884
+ const yep_dnode* n = node_of(handle);
885
+ if (n == NULL || out == NULL) {
886
+ return YEPTRIS_ERROR_ARG;
887
+ }
888
+ if (n->tag_id != YEPTRIS_TAG_INT) {
889
+ return YEPTRIS_ERROR_PARSE;
890
+ }
891
+ yep_view v = node_view((yeptris_node*)handle, n->value);
892
+ return yep_num_i64((const char*)v.p, v.len, out) == 0 ? YEPTRIS_OK : YEPTRIS_ERROR_PARSE;
893
+ }
894
+
895
+ YEPTRIS_API YeptrisStatus yeptris_node_float(YeptrisNode handle, double* out) {
896
+ const yep_dnode* n = node_of(handle);
897
+ if (n == NULL || out == NULL) {
898
+ return YEPTRIS_ERROR_ARG;
899
+ }
900
+ if (n->tag_id != YEPTRIS_TAG_FLOAT) {
901
+ return YEPTRIS_ERROR_PARSE;
902
+ }
903
+ yep_view v = node_view((yeptris_node*)handle, n->value);
904
+ return yep_num_f64((const char*)v.p, v.len, out) == 0 ? YEPTRIS_OK : YEPTRIS_ERROR_PARSE;
905
+ }
906
+ YEPTRIS_API YeptrisStatus yeptris_node_bool(YeptrisNode handle, int* out) {
907
+ const yep_dnode* n = node_of(handle);
908
+ if (n == NULL || out == NULL) {
909
+ return YEPTRIS_ERROR_ARG;
910
+ }
911
+ if (n->tag_id != YEPTRIS_TAG_BOOL) {
912
+ return YEPTRIS_ERROR_PARSE;
913
+ }
914
+ /* the resolver accepted it; true-set membership decides the value */
915
+ static const char* k_true[] = {"true", "True", "TRUE", "y", "Y", "yes",
916
+ "Yes", "YES", "on", "On", "ON"};
917
+ for (size_t i = 0; i < sizeof(k_true) / sizeof(k_true[0]); i++) {
918
+ size_t m = strlen(k_true[i]);
919
+ yep_view v = node_view((yeptris_node*)handle, n->value);
920
+ if (v.len == m && memcmp(v.p, k_true[i], m) == 0) {
921
+ *out = 1;
922
+ return YEPTRIS_OK;
923
+ }
924
+ }
925
+ *out = 0;
926
+ return YEPTRIS_OK;
927
+ }
928
+
929
+ YEPTRIS_API const char* yeptris_node_tag(YeptrisNode handle, size_t* len) {
930
+ const yep_dnode* n = node_of(handle);
931
+ if (n == NULL || n->tag.len == 0) {
932
+ if (len != NULL) {
933
+ *len = 0;
934
+ }
935
+ return n ? NULL : NULL;
936
+ }
937
+ return view_out(node_view((yeptris_node*)handle, n->tag), len);
938
+ }
939
+
940
+ YEPTRIS_API const char* yeptris_node_anchor(YeptrisNode handle, size_t* len) {
941
+ const yep_dnode* n = node_of(handle);
942
+ if (n == NULL || n->anchor.len == 0) {
943
+ if (len != NULL) {
944
+ *len = 0;
945
+ }
946
+ return NULL;
947
+ }
948
+ return view_out(node_view((yeptris_node*)handle, n->anchor), len);
949
+ }
950
+
951
+ static YeptrisNode wrap(yeptris_node* base, uint32_t id) {
952
+ if (base == NULL || base->doc == NULL) {
953
+ return NULL;
954
+ }
955
+ /* Handles live in the document's thread-safe arena: one
956
+ * document_free reclaims every handle ever handed out (zero-leak
957
+ * contract) while read-only sharing stays safe (TODO.impl/19) */
958
+ yeptris_node* n = yep_hpool_alloc(yep_dom_handles(base->doc->dom), sizeof(yeptris_node), 16);
959
+ if (n == NULL) {
960
+ return NULL;
961
+ }
962
+ n->doc = base->doc;
963
+ n->id = id;
964
+ return (YeptrisNode)n;
965
+ }
966
+
967
+ YEPTRIS_API YeptrisNode yeptris_node_alias_target(YeptrisNode handle) {
968
+ const yep_dnode* n = node_of(handle);
969
+ if (n == NULL || n->kind != YEP_DOM_ALIAS || n->target == UINT32_MAX) {
970
+ return NULL;
971
+ }
972
+ return wrap((yeptris_node*)handle, n->target);
973
+ }
974
+
975
+ YEPTRIS_API size_t yeptris_node_seq_count(YeptrisNode handle) {
976
+ const yep_dnode* n = node_of(handle);
977
+ return (n && n->kind == YEP_DOM_SEQUENCE) ? n->count : 0;
978
+ }
979
+
980
+ YEPTRIS_API YeptrisNode yeptris_node_seq_at(YeptrisNode handle, size_t index) {
981
+ const yep_dnode* n = node_of(handle);
982
+ if (n == NULL || n->kind != YEP_DOM_SEQUENCE || index >= n->count) {
983
+ return NULL;
984
+ }
985
+ yep_dom* dom = ((yeptris_node*)handle)->doc->dom;
986
+ uint32_t count = 0;
987
+ /* the node id IS its dnode index */
988
+ uint32_t id = dom_indexed_child(dom, ((yeptris_node*)handle)->id, index, &count); /* #377 */
989
+ if (id == UINT32_MAX) {
990
+ return NULL;
991
+ }
992
+ return wrap((yeptris_node*)handle, id);
993
+ }
994
+
995
+ YEPTRIS_API size_t yeptris_node_children(YeptrisNode handle, YeptrisNode* out, size_t cap) {
996
+ const yep_dnode* n = node_of(handle);
997
+ if (n == NULL || (n->kind != YEP_DOM_SEQUENCE && n->kind != YEP_DOM_MAPPING)) {
998
+ return 0;
999
+ }
1000
+ size_t count = 0;
1001
+ uint32_t id = n->first_child;
1002
+ while (id != UINT32_MAX) {
1003
+ if (out != NULL && count < cap) {
1004
+ out[count] = wrap((yeptris_node*)handle, id);
1005
+ }
1006
+ count++;
1007
+ const yep_dnode* cur = yep_dom_node(((yeptris_node*)handle)->doc->dom, id);
1008
+ id = cur ? cur->next_sibling : UINT32_MAX;
1009
+ }
1010
+ return count;
1011
+ }
1012
+
1013
+ YEPTRIS_API size_t yeptris_node_map_count(YeptrisNode handle) {
1014
+ const yep_dnode* n = node_of(handle);
1015
+ return (n && n->kind == YEP_DOM_MAPPING) ? n->count / 2 : 0;
1016
+ }
1017
+
1018
+ YEPTRIS_API int yeptris_node_map_at(YeptrisNode handle, size_t index, YeptrisNode* key,
1019
+ YeptrisNode* value) {
1020
+ const yep_dnode* n = node_of(handle);
1021
+ /* the internal kind space (#904 item 6, see yeptris_node_tag_id) */
1022
+ if (n == NULL || n->kind != YEP_DOM_MAPPING || index >= n->count / 2) {
1023
+ return -1;
1024
+ }
1025
+ yeptris_node* h = (yeptris_node*)handle;
1026
+ yep_dom* d = h->doc->dom;
1027
+ uint32_t count = 0;
1028
+ uint32_t child = dom_indexed_child(d, h->id, (size_t)index * 2, &count); /* #377 */
1029
+ if (child == UINT32_MAX) {
1030
+ return -1;
1031
+ }
1032
+ if (key != NULL) {
1033
+ *key = wrap(h, child);
1034
+ }
1035
+ if (value != NULL) {
1036
+ *value = wrap(h, d->nodes[child].next_sibling);
1037
+ }
1038
+ return 0;
1039
+ }
1040
+
1041
+ YEPTRIS_API YeptrisNode yeptris_node_map_get(YeptrisNode handle, const char* key, size_t key_len) {
1042
+ const yep_dnode* n = node_of(handle);
1043
+ if (n == NULL || n->kind != YEP_DOM_MAPPING || key == NULL) {
1044
+ return NULL;
1045
+ }
1046
+ yep_dom* dom = ((yeptris_node*)handle)->doc->dom;
1047
+ /* O(1) through the lazy index (first use builds it); the scan
1048
+ * below is the build-failure fallback */
1049
+ uint32_t hit =
1050
+ yep_midx_lookup(dom, ((yeptris_node*)handle)->id, (yep_view){key, (uint32_t)key_len});
1051
+ if (hit != UINT32_MAX) {
1052
+ const yep_dnode* kn = yep_dom_node(dom, hit);
1053
+ if (kn != NULL) {
1054
+ return wrap((yeptris_node*)handle, kn->next_sibling);
1055
+ }
1056
+ }
1057
+ uint32_t id = n->first_child;
1058
+ int want_key = 1;
1059
+ while (id != UINT32_MAX) {
1060
+ const yep_dnode* cur = yep_dom_node(dom, id);
1061
+ if (cur == NULL) {
1062
+ break;
1063
+ }
1064
+ if (want_key && cur->kind == YEP_DOM_SCALAR) {
1065
+ yep_view kv = {key, (uint32_t)key_len};
1066
+ if (yep_view_eq(yep_dom_view(dom, cur->value), kv)) {
1067
+ return wrap((yeptris_node*)handle, cur->next_sibling);
1068
+ }
1069
+ }
1070
+ want_key = !want_key;
1071
+ id = cur->next_sibling;
1072
+ }
1073
+ return NULL;
1074
+ }