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,2323 @@
1
+ /* ytape.c — the packed YAML record tape (#378, TODO.max-perf/12).
2
+ *
3
+ * Recorder: a yep_sink over the SEVEN committed-op callbacks plus the
4
+ * general event path. The engine stays in its fused arms (no event
5
+ * construction on classified shapes — the event-recorder prototype's
6
+ * measured floor); the recorder's writes are 8-byte words instead of
7
+ * the eager builders' 48-byte node inits, arena copies, resolver calls
8
+ * and placement.
9
+ *
10
+ * Replay: decodes words into the SAME builder functions
11
+ * (dom_on_block_pair/open/item, dom_on_scalar, dom_on_flow_build,
12
+ * yep_dom_on_event), so typing, anchoring and placement semantics are
13
+ * the eager path's by construction. The differential gate
14
+ * (test/unit/test_ytape.cpp) pins tree identity across the corpora.
15
+ */
16
+
17
+ #include <stdio.h>
18
+ #include <string.h>
19
+
20
+ #include "dom/dom.h"
21
+ #include "memory/allocator.h"
22
+ #include "memory/pool.h"
23
+ #include "parse/scalars.h"
24
+ #include "resolve/resolver.h"
25
+ #include "scan/json.h"
26
+ #include "scan/scan.h"
27
+ #include "tape_in.h"
28
+ #include "yeptris/tape.h"
29
+ #include "ytape.h"
30
+
31
+ /* ---- word packing ---- */
32
+
33
+ #define YTP_KIND_OF(w) ((uint8_t)((w) >> 56))
34
+
35
+ static int yt_put(yep_ytape* t, uint64_t w);
36
+
37
+ static uint64_t yt_span_in(uint32_t off, uint32_t len, uint8_t kind) {
38
+ return ((uint64_t)kind << 56) | ((uint64_t)(len & 0xFFFFFFu) << 32) | (uint64_t)off;
39
+ }
40
+
41
+ static uint64_t yt_props(uint8_t kind, uint32_t anchor_id, uint32_t flags) {
42
+ return ((uint64_t)kind << 56) | ((uint64_t)(anchor_id & 0xFFFFFFu) << 32) |
43
+ (uint64_t)(flags & 0xFFFFFFFFu);
44
+ }
45
+
46
+ /* ---- the recorder ---- */
47
+
48
+ int ytap_init(yep_ytape* t, const char* input, size_t len, int max_depth) {
49
+ memset(t, 0, sizeof(*t));
50
+ t->input = input;
51
+ t->input_len = len;
52
+ t->max_depth = max_depth;
53
+ size_t cap = len / 2 + 64;
54
+ if (cap > 0xFFFFFFFFu) {
55
+ cap = 0xFFFFFFFFu;
56
+ }
57
+ t->w = (uint64_t*)yep_alloc(yep_system_allocator(), cap * sizeof(uint64_t));
58
+ if (t->w == NULL) {
59
+ return -1;
60
+ }
61
+ t->cap = (uint32_t)cap;
62
+ return 0;
63
+ }
64
+
65
+ void ytap_free(yep_ytape* t) {
66
+ if (t->flow_scratch != NULL) {
67
+ yeptris_tape_free((yeptris_json_tape*)t->flow_scratch);
68
+ yep_free(yep_system_allocator(), t->flow_scratch);
69
+ t->flow_scratch = NULL;
70
+ }
71
+ if (t->w != NULL) {
72
+ yep_free(yep_system_allocator(), t->w);
73
+ t->w = NULL;
74
+ }
75
+ }
76
+
77
+ /* Growth bounded by the input (the bounded-parse law): a 4N-word
78
+ * ceiling, never a runaway realloc chain. The allocator has no
79
+ * realloc — one copy per growth, at most a few per document. */
80
+ static int yt_put(yep_ytape* t, uint64_t w) {
81
+ if (t->count >= t->cap) {
82
+ if (t->oom) {
83
+ return -1;
84
+ }
85
+ size_t ncap = (size_t)t->cap * 2;
86
+ size_t ceiling = t->input_len * 4 + 256;
87
+ if (ncap > ceiling) {
88
+ ncap = ceiling;
89
+ }
90
+ if (ncap <= (size_t)t->cap) {
91
+ t->oom = 1;
92
+ return -1;
93
+ }
94
+ uint64_t* nw = (uint64_t*)yep_alloc(yep_system_allocator(), ncap * sizeof(uint64_t));
95
+ if (nw == NULL) {
96
+ t->oom = 1;
97
+ return -1;
98
+ }
99
+ memcpy(nw, t->w, (size_t)t->count * sizeof(uint64_t));
100
+ yep_free(yep_system_allocator(), t->w);
101
+ t->w = nw;
102
+ t->cap = (uint32_t)ncap;
103
+ }
104
+ t->w[t->count++] = w;
105
+ return 0;
106
+ }
107
+
108
+ /* The pool-form span: two words, 32-bit length, raw pointer (stable:
109
+ * pool blocks never move until the document frees them). */
110
+ static int yt_span_pool(yep_ytape* t, const void* p, size_t len) {
111
+ if (yt_put(t, ((uint64_t)YTP_SPAN_POOL << 56) | (uint64_t)(uint32_t)len) != 0) {
112
+ return -1;
113
+ }
114
+ return yt_put(t, (uint64_t)(uintptr_t)p);
115
+ }
116
+
117
+ /* One span word when the view is input-relative and fits 24 bits; the
118
+ * generic pair otherwise. */
119
+ static int yt_span(yep_ytape* t, const yep_view* v) {
120
+ if (v->p >= t->input && v->p <= t->input + t->input_len && v->len < 0x1000000u) {
121
+ return yt_put(t, yt_span_in((uint32_t)(v->p - t->input), (uint32_t)v->len, YTP_SPAN_IN));
122
+ }
123
+ return yt_span_pool(t, v->p, v->len);
124
+ }
125
+
126
+ /* Span-mode bits shared by the props words: 0 absent, 1 input, 2 pool
127
+ * (mode is re-derived by pointer range — the recorder never trusts
128
+ * borrowedness for tag/anchor views). */
129
+ #define YTM_ABSENT 0
130
+ #define YTM_IN 1
131
+ #define YTM_POOL 2
132
+
133
+ static int yt_mode(const yep_ytape* t, const yep_view* v) {
134
+ if (v->p == NULL || v->len == 0) {
135
+ return YTM_ABSENT;
136
+ }
137
+ if (v->p >= t->input && v->p <= t->input + t->input_len) {
138
+ return YTM_IN;
139
+ }
140
+ return YTM_POOL;
141
+ }
142
+
143
+ static int yt_span_by_mode(yep_ytape* t, const yep_view* v) {
144
+ if (v->p == NULL || v->len == 0) {
145
+ return 0;
146
+ }
147
+ return yt_span(t, v);
148
+ }
149
+
150
+ /* The general event path. STREAM/DOCUMENT frames compact to one word
151
+ * (the DOM reads nothing else off them); SEQ/MAP/SCALAR/ALIAS pack
152
+ * props + value + optional tag/anchor spans. */
153
+ static int yt_on_event(void* ctx, const yep_event* ev) {
154
+ yep_ytape* t = (yep_ytape*)ctx;
155
+ switch (ev->type) {
156
+ case YEP_EV_STREAM_START:
157
+ case YEP_EV_STREAM_END:
158
+ case YEP_EV_DOCUMENT_START:
159
+ case YEP_EV_DOCUMENT_END:
160
+ if (ev->type == YEP_EV_DOCUMENT_START) {
161
+ t->docs++;
162
+ }
163
+ return yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | (uint64_t)ev->type);
164
+ case YEP_EV_SEQ_START:
165
+ case YEP_EV_MAP_START:
166
+ if (t->depth >= YEP_DOM_MAX_DEPTH) {
167
+ return -1; /* the DOM answers -1 here; the engine's error
168
+ * path stays identical */
169
+ }
170
+ t->depth++;
171
+ break;
172
+ case YEP_EV_SEQ_END:
173
+ case YEP_EV_MAP_END:
174
+ t->depth--;
175
+ break;
176
+ default:
177
+ break;
178
+ }
179
+ uint32_t anchor_hi = 0;
180
+ uint32_t aid = ev->anchor_id;
181
+ if (aid > 0xFFFFFFu) {
182
+ anchor_hi = 1;
183
+ aid = 0xFFFFFFu;
184
+ }
185
+ uint32_t tag_mode = (uint32_t)yt_mode(t, &ev->tag);
186
+ uint32_t anchor_mode = (uint32_t)yt_mode(t, &ev->anchor);
187
+ uint32_t flags = (uint32_t)ev->type | ((uint32_t)ev->style << 4) |
188
+ ((uint32_t)ev->implicit << 7) | ((uint32_t)ev->flow << 8) |
189
+ ((uint32_t)ev->multiline << 9) | ((uint32_t)(ev->borrowed != 0) << 10) |
190
+ (tag_mode << 11) | (anchor_mode << 13) | (anchor_hi << 15) |
191
+ ((uint32_t)ev->tag_id << 16);
192
+ if (yt_put(t, yt_props(YTP_EVENT, aid, flags)) != 0) {
193
+ return -1;
194
+ }
195
+ if (yt_span(t, &ev->value) != 0) {
196
+ return -1;
197
+ }
198
+ if (tag_mode != YTM_ABSENT && yt_span_by_mode(t, &ev->tag) != 0) {
199
+ return -1;
200
+ }
201
+ if (anchor_mode != YTM_ABSENT && yt_span_by_mode(t, &ev->anchor) != 0) {
202
+ return -1;
203
+ }
204
+ if (anchor_hi && yt_put(t, (uint64_t)ev->anchor_id) != 0) {
205
+ return -1;
206
+ }
207
+ return 0;
208
+ }
209
+
210
+ static int yt_on_scalar(void* ctx, const yep_view* value, const yep_view* tag,
211
+ const yep_view* anchor, uint32_t anchor_id, uint8_t tag_id, uint8_t style,
212
+ uint8_t implicit, uint8_t flow, int borrowed) {
213
+ yep_ytape* t = (yep_ytape*)ctx;
214
+ uint32_t anchor_hi = 0;
215
+ uint32_t aid = anchor_id;
216
+ if (aid > 0xFFFFFFu) {
217
+ anchor_hi = 1;
218
+ aid = 0xFFFFFFu;
219
+ }
220
+ uint32_t tag_mode = (uint32_t)yt_mode(t, tag);
221
+ uint32_t anchor_mode = (uint32_t)yt_mode(t, anchor);
222
+ uint32_t flags = (uint32_t)tag_id | ((uint32_t)style << 8) | ((uint32_t)implicit << 11) |
223
+ ((uint32_t)flow << 12) | ((uint32_t)(borrowed != 0) << 13) | (tag_mode << 14) |
224
+ (anchor_mode << 16) | (anchor_hi << 18);
225
+ if (yt_put(t, yt_props(YTP_SCALAR, aid, flags)) != 0) {
226
+ return -1;
227
+ }
228
+ if (yt_span(t, value) != 0) {
229
+ return -1;
230
+ }
231
+ if (tag_mode != YTM_ABSENT && yt_span_by_mode(t, tag) != 0) {
232
+ return -1;
233
+ }
234
+ if (anchor_mode != YTM_ABSENT && yt_span_by_mode(t, anchor) != 0) {
235
+ return -1;
236
+ }
237
+ if (anchor_hi && yt_put(t, (uint64_t)anchor_id) != 0) {
238
+ return -1;
239
+ }
240
+ return 0;
241
+ }
242
+
243
+ static int yt_on_block_pair(void* ctx, const yep_view* key, const yep_block_value* v, uint32_t line,
244
+ uint32_t key_col, uint32_t val_col) {
245
+ (void)line;
246
+ (void)key_col;
247
+ (void)val_col; /* yep_dnode carries no positions (64-2c) */
248
+ yep_ytape* t = (yep_ytape*)ctx;
249
+ uint32_t anchor_hi = 0;
250
+ uint32_t aid = v->anchor_id;
251
+ if (aid > 0xFFFFFFu) {
252
+ anchor_hi = 1;
253
+ aid = 0xFFFFFFu;
254
+ }
255
+ uint32_t flags = ((uint32_t)v->cls & 7u) | ((uint32_t)(v->borrowed != 0) << 3) |
256
+ ((uint32_t)yt_mode(t, &v->anchor) << 4) | (anchor_hi << 6);
257
+ if (yt_put(t, yt_props(YTP_PAIR, aid, flags)) != 0) {
258
+ return -1;
259
+ }
260
+ if (yt_span(t, key) != 0) {
261
+ return -1;
262
+ }
263
+ /* ALIAS names record as spans either way (the eager builder
264
+ * stamps borrowed=1 for them); plain values borrow by the flag. */
265
+ if (v->borrowed || v->cls == YEP_LVAL_ALIAS) {
266
+ if (yt_span(t, &v->value) != 0) {
267
+ return -1;
268
+ }
269
+ } else if (yt_span_pool(t, v->value.p, v->value.len) != 0) {
270
+ return -1;
271
+ }
272
+ if (((flags >> 4) & 3u) != YTM_ABSENT && yt_span_by_mode(t, &v->anchor) != 0) {
273
+ return -1;
274
+ }
275
+ if (anchor_hi && yt_put(t, (uint64_t)v->anchor_id) != 0) {
276
+ return -1;
277
+ }
278
+ return 1; /* handled: the engine skips the two events */
279
+ }
280
+
281
+ static int yt_on_block_open(void* ctx, const yep_view* key, uint32_t line, uint32_t key_col) {
282
+ (void)line;
283
+ (void)key_col;
284
+ yep_ytape* t = (yep_ytape*)ctx;
285
+ if (t->depth >= YEP_DOM_MAX_DEPTH) {
286
+ return 0; /* the engine's event path owns the depth error */
287
+ }
288
+ if (key->len < 0x1000000u && key->p >= t->input && key->p <= t->input + t->input_len) {
289
+ if (yt_put(t, yt_span_in((uint32_t)(key->p - t->input), (uint32_t)key->len, YTP_OPEN)) !=
290
+ 0) {
291
+ return -1;
292
+ }
293
+ } else {
294
+ /* a pool or oversized block key: the scalar form's open-key
295
+ * bit carries it (defensive — the engine classifies keys from
296
+ * the line; replay routes back to on_block_open) */
297
+ if (yt_put(t, yt_props(YTP_SCALAR, 0, 1u << 21)) != 0) {
298
+ return -1;
299
+ }
300
+ if (yt_span(t, key) != 0) {
301
+ return -1;
302
+ }
303
+ }
304
+ t->depth++;
305
+ return 1;
306
+ }
307
+
308
+ static int yt_on_block_item(void* ctx, const yep_block_value* v) {
309
+ yep_ytape* t = (yep_ytape*)ctx;
310
+ if (v->borrowed && v->value.len < 0x1000000u && v->value.p >= t->input &&
311
+ v->value.p <= t->input + t->input_len) {
312
+ if (yt_put(t, yt_span_in((uint32_t)(v->value.p - t->input), (uint32_t)v->value.len,
313
+ YTP_ITEM)) != 0) {
314
+ return -1;
315
+ }
316
+ return 1;
317
+ }
318
+ /* pool content or oversized: the scalar form with the item bit */
319
+ if (yt_put(t, yt_props(YTP_SCALAR, 0, 1u << 20)) != 0) {
320
+ return -1;
321
+ }
322
+ if (yt_span_pool(t, v->value.p, v->value.len) != 0) {
323
+ return -1;
324
+ }
325
+ return 1;
326
+ }
327
+
328
+ /* the >1024 simple-key law, read off the scratch records: a STR at a
329
+ * map's key slot over the limit answers the general walk's long_key
330
+ * verdict (the fused walk carries no such flag). Members alternate
331
+ * key/value in a map frame; a container in the value slot completes
332
+ * at its CLOSE. len == 0xFFFFFF (a CONT-extended token, >16 MiB) is
333
+ * over the limit by definition. */
334
+ static uint32_t yt_flow_scan(const yeptris_json_tape* s, int* long_key) {
335
+ /* the record-walk verdicts the fused walk does not carry: the
336
+ * >1024 simple-key law (a STR at a map's key slot over the limit;
337
+ * the law counts the RAW token — a quoted key's record holds the
338
+ * INNER span, so the two quotes add back on; 0xFFFFFF = a
339
+ * CONT-extended >16 MiB token, over by definition) and the span's
340
+ * maximum nesting (the fused walk's depth cap is its own, not the
341
+ * engine's max_depth — the caller falls back when exceeded) */
342
+ struct {
343
+ uint8_t map;
344
+ uint8_t key_next;
345
+ } st[257];
346
+ int d = 0;
347
+ uint32_t maxd = 0;
348
+ st[0].map = 0;
349
+ st[0].key_next = 0;
350
+ for (uint32_t i = 1; i < s->count; i++) { /* recs[0] is the DOC */
351
+ uint64_t r = s->recs[i];
352
+ uint8_t kind = (uint8_t)(r & 0xFFu);
353
+ if (kind == YEP_T_MAP_OPEN || kind == YEP_T_SEQ_OPEN) {
354
+ if (d >= 256) {
355
+ *long_key = 0;
356
+ return 0xFFFFu; /* deeper than the scratch stack: the
357
+ * general kernel's depth path decides */
358
+ }
359
+ /* the container consumes the parent frame's pending slot
360
+ * (key or value); after it closes the OTHER slot is next —
361
+ * a container value left key_next pinned at 1, and every
362
+ * key after a container value skipped the law (the
363
+ * nightly's round-108 shape: {"": [...], "giant": x}) */
364
+ if (d > 0 && st[d].map) {
365
+ st[d].key_next = (uint8_t)!st[d].key_next;
366
+ }
367
+ d++;
368
+ if ((uint32_t)d > maxd) {
369
+ maxd = (uint32_t)d;
370
+ }
371
+ st[d].map = (uint8_t)(kind == YEP_T_MAP_OPEN);
372
+ st[d].key_next = 0;
373
+ continue;
374
+ }
375
+ if (kind == YEP_T_CLOSE) {
376
+ if (d > 0) {
377
+ d--;
378
+ }
379
+ continue;
380
+ }
381
+ if (st[d].map && st[d].key_next == 0) {
382
+ uint32_t mlen = (uint32_t)(r >> 8) & 0xFFFFFFu;
383
+ if (kind == YEP_T_STR && mlen + 2u > 1024u &&
384
+ yep_simple_key_chars((const char*)s->_src + (size_t)((uint32_t)(r >> 32)), mlen) +
385
+ 2 >
386
+ 1024) {
387
+ /* the budget counts characters (the round-108 shrink:
388
+ * a 2-byte code point at byte delta 1025 is legal) */
389
+ *long_key = 1;
390
+ }
391
+ st[d].key_next = 1;
392
+ } else if (st[d].map) {
393
+ st[d].key_next = 0;
394
+ }
395
+ }
396
+ return maxd;
397
+ }
398
+
399
+ static int yt_flow_classify_record(yep_ytape* t, const char* p, size_t open, size_t len,
400
+ const yep_view* anchor, const yep_view* tag, uint32_t anchor_id,
401
+ int max_depth, size_t* close);
402
+
403
+ static int yt_on_flow_build(void* ctx, const char* p, size_t open, size_t len, uint32_t line,
404
+ size_t line_start, yep_view anchor, yep_view tag, uint32_t anchor_id,
405
+ int max_depth, size_t* close) {
406
+ (void)line;
407
+ (void)line_start; /* the flow builder reads neither */
408
+ return yt_flow_classify_record((yep_ytape*)ctx, p, open, len, &anchor, &tag, anchor_id,
409
+ max_depth, close);
410
+ }
411
+
412
+ /* the shared flow-span classification + ONE-record emission (the
413
+ * sink's entry and the flow-rooted fast path both land here). Returns
414
+ * the sink's verdict codes: 0 = not JSON-class (the general kernel),
415
+ * 1 = recorded (with *close set), 2 = long-key (pass 2 owns the
416
+ * error). */
417
+ static int yt_flow_classify_record(yep_ytape* t, const char* p, size_t open, size_t len,
418
+ const yep_view* anchor, const yep_view* tag, uint32_t anchor_id,
419
+ int max_depth, size_t* close) {
420
+ if (t->depth >= YEP_DOM_MAX_DEPTH) {
421
+ return 0; /* the event path owns the error shape */
422
+ }
423
+ /* classification = the DOM's own walk with the builds stripped:
424
+ * the same reject/long-key verdicts, then ONE record for the
425
+ * whole staged span (replay runs the full build through
426
+ * dom_on_flow_build). The verdict comes from the FUSED lenient
427
+ * walk into a reused scratch tape — 3.5x the naive token loop on
428
+ * the spike corpora (item 86); any refusal keeps the naive loop,
429
+ * whose accept/reject contract is the same by construction. */
430
+ if (t->flow_scratch == NULL) {
431
+ t->flow_scratch = yep_alloc(yep_system_allocator(), sizeof(yeptris_json_tape));
432
+ if (t->flow_scratch != NULL) {
433
+ memset(t->flow_scratch, 0, sizeof(yeptris_json_tape));
434
+ }
435
+ }
436
+ t->max_depth = max_depth; /* replay re-walks at the engine's limit */
437
+ /* the naive token loop with a BYTE BUDGET: small spans (the common
438
+ * per-line flow map) finish it — the loop carries the exact close
439
+ * and long_key verdicts for free; a budget exhaustion hands the
440
+ * span to the fused lenient walk, which amortizes its fixed entry
441
+ * cost on large spans (2.1x the naive loop at 6.6 MB, item 86) */
442
+ {
443
+ yep_json_walk w;
444
+ yep_json_tok tok;
445
+ yep_json_walk_init(&w, p, len, open, max_depth);
446
+ w.strict = 1; /* mirrors the engine pass-1 / dom builder gates:
447
+ * reject anything non-JSON so the span falls to
448
+ * the general kernel (whole/chunked agreement) */
449
+ int exhausted = 0;
450
+ for (;;) {
451
+ if (w.i - open > 256) {
452
+ exhausted = 1;
453
+ break;
454
+ }
455
+ yep_jw_status st = yep_json_walk_next(&w, &tok);
456
+ if (st == YEP_JW_REJECT) {
457
+ return 0; /* not JSON-class: the general kernel */
458
+ }
459
+ if (st == YEP_JW_DONE) {
460
+ if (w.long_key) {
461
+ return 2; /* pass 2 owns the simple-key error */
462
+ }
463
+ *close = w.close; /* the walk's own position: exact */
464
+ goto record;
465
+ }
466
+ }
467
+ if (exhausted) {
468
+ yeptris_json_tape* s = t->flow_scratch;
469
+ if (s == NULL) {
470
+ s = (yeptris_json_tape*)yep_alloc(yep_system_allocator(),
471
+ sizeof(yeptris_json_tape));
472
+ if (s != NULL) {
473
+ memset(s, 0, sizeof(yeptris_json_tape));
474
+ t->flow_scratch = s;
475
+ }
476
+ }
477
+ if (s != NULL) {
478
+ /* the carve reuses the block through the _srclen
479
+ * watermark — no per-span free (flow-json walks 200k
480
+ * spans per parse) */
481
+ /* strict_nums=1: the general walk validates number runs
482
+ * even at strict=0 (M5DY's 2001-07-02 dates — an
483
+ * unvalidated accept here diverges from the engine's
484
+ * own reject) */
485
+ if (s->_block != NULL && s->_srclen < len + 2) {
486
+ /* the block cannot hold this span: release it so the
487
+ * walk's carve sizes a fresh one (carve's own growth
488
+ * semantics belong to the strict route's live
489
+ * pointers) */
490
+ yeptris_tape_free(s);
491
+ memset(s, 0, sizeof(yeptris_json_tape));
492
+ }
493
+ if (yep_tape_walk_lenient_fused(p, len, open, s, 1, 0, 1) == YEPTRIS_OK) {
494
+ if (s->_srclen < len + 2) {
495
+ s->_srclen = len + 2; /* the reuse watermark */
496
+ }
497
+ int lk = 0;
498
+ uint32_t smax = yt_flow_scan(s, &lk);
499
+ if (lk) {
500
+ return 2; /* pass 2 owns the simple-key error */
501
+ }
502
+ if (smax > (uint32_t)max_depth) {
503
+ return 0; /* over the engine's depth limit: the
504
+ * general walk owns the exact DEPTH
505
+ * error shape */
506
+ }
507
+ goto record_fused;
508
+ }
509
+ return 0; /* the fused walk refused: the general kernel */
510
+ }
511
+ return 0; /* scratch alloc refused: the general kernel */
512
+ }
513
+ }
514
+ return -1; /* unreachable */
515
+ record_fused: {
516
+ size_t c = open;
517
+ int depth = 0;
518
+ for (;;) {
519
+ unsigned char ch = (unsigned char)p[c];
520
+ if (ch == '"') { /* the walk validated the string; skip runs */
521
+ c++;
522
+ while (p[c] != '"') {
523
+ if (p[c] == '\\') {
524
+ c++;
525
+ }
526
+ c++;
527
+ }
528
+ c++;
529
+ continue;
530
+ }
531
+ if (ch == '{' || ch == '[') {
532
+ depth++;
533
+ } else if (ch == '}' || ch == ']') {
534
+ depth--;
535
+ if (depth == 0) {
536
+ break;
537
+ }
538
+ }
539
+ c++;
540
+ }
541
+ *close = c;
542
+ }
543
+ record:
544
+ t->flow_mark = t->count;
545
+ uint32_t anchor_hi = 0;
546
+ uint32_t aid = anchor_id;
547
+ if (aid > 0xFFFFFFu) {
548
+ anchor_hi = 1;
549
+ aid = 0xFFFFFFu;
550
+ }
551
+ uint32_t tag_mode = (uint32_t)yt_mode(t, tag);
552
+ uint32_t anchor_mode = (uint32_t)yt_mode(t, anchor);
553
+ uint32_t flags = tag_mode | (anchor_mode << 2) | (anchor_hi << 4);
554
+ if (yt_put(t, yt_props(YTP_FLOW, aid, flags)) != 0) {
555
+ return -1;
556
+ }
557
+ if (yt_put(t, (uint64_t)open | ((uint64_t)len << 32)) != 0) {
558
+ return -1;
559
+ }
560
+ if (tag_mode != YTM_ABSENT && yt_span_by_mode(t, tag) != 0) {
561
+ return -1;
562
+ }
563
+ if (anchor_mode != YTM_ABSENT && yt_span_by_mode(t, anchor) != 0) {
564
+ return -1;
565
+ }
566
+ if (anchor_hi && yt_put(t, (uint64_t)anchor_id) != 0) {
567
+ return -1;
568
+ }
569
+ return 1;
570
+ }
571
+
572
+ static int yt_on_flow_commit(void* ctx) {
573
+ (void)ctx; /* the records are already written */
574
+ return 1;
575
+ }
576
+
577
+ static void yt_on_flow_rollback(void* ctx) {
578
+ yep_ytape* t = (yep_ytape*)ctx;
579
+ t->count = t->flow_mark; /* the DOM's stage reset, in words */
580
+ }
581
+
582
+ void ytap_sink(yep_ytape* t, yep_sink* sink) {
583
+ memset(sink, 0, sizeof(*sink));
584
+ sink->on_event = yt_on_event;
585
+ sink->ctx = t;
586
+ sink->on_flow_build = yt_on_flow_build;
587
+ sink->on_flow_commit = yt_on_flow_commit;
588
+ sink->on_flow_rollback = yt_on_flow_rollback;
589
+ sink->on_block_pair = yt_on_block_pair;
590
+ sink->on_scalar = yt_on_scalar;
591
+ sink->on_block_open = yt_on_block_open;
592
+ sink->on_block_item = yt_on_block_item;
593
+ sink->compose = 1; /* the tape replays into a DOM: the composer's
594
+ * undefined-alias rejection stays at parse time */
595
+ }
596
+
597
+ /* ---- replay ---- */
598
+
599
+ static int yt_read_span(const yep_ytape* t, const uint64_t* w, uint32_t* i, uint32_t count,
600
+ yep_view* v) {
601
+ if (*i >= count) {
602
+ return -1;
603
+ }
604
+ uint64_t word = w[(*i)++];
605
+ uint8_t k = YTP_KIND_OF(word);
606
+ if (k == YTP_SPAN_IN) {
607
+ v->p = t->input + (uint32_t)word;
608
+ v->len = (uint32_t)(word >> 32) & 0xFFFFFFu;
609
+ return 0;
610
+ }
611
+ if (k == YTP_SPAN_POOL) {
612
+ if (*i >= count) {
613
+ return -1;
614
+ }
615
+ v->p = (const char*)(uintptr_t)w[(*i)++];
616
+ v->len = (uint32_t)word;
617
+ return 0;
618
+ }
619
+ return -1; /* a props word in a span slot: corruption */
620
+ }
621
+
622
+ int dom_from_ytape(yep_dom* d, const yep_ytape* t) {
623
+ if (d == NULL || t == NULL || t->w == NULL) {
624
+ return -1;
625
+ }
626
+ d->input_base = t->input;
627
+ d->input_len = t->input_len;
628
+ yep_dom_prepare_len(d, t->input_len);
629
+ const uint64_t* w = t->w;
630
+ uint32_t count = t->count;
631
+ uint32_t i = 0;
632
+ while (i < count) {
633
+ uint8_t kind = YTP_KIND_OF(w[i]);
634
+ switch (kind) {
635
+ case YTP_EV_COMPACT: {
636
+ yep_event ev;
637
+ memset(&ev, 0, sizeof(ev));
638
+ ev.type = (yep_event_type)(uint8_t)w[i];
639
+ i++;
640
+ if (yep_dom_on_event(d, &ev) != 0) {
641
+ return -1;
642
+ }
643
+ break;
644
+ }
645
+ case YTP_EVENT: {
646
+ uint32_t flags = (uint32_t)(w[i] & 0xFFFFFFFFu);
647
+ uint32_t aid = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
648
+ i++;
649
+ yep_event ev;
650
+ memset(&ev, 0, sizeof(ev));
651
+ ev.type = (yep_event_type)(flags & 0xFu);
652
+ ev.style = (uint8_t)((flags >> 4) & 7u);
653
+ ev.implicit = (uint8_t)((flags >> 7) & 1u);
654
+ ev.flow = (uint8_t)((flags >> 8) & 1u);
655
+ ev.multiline = (uint8_t)((flags >> 9) & 1u);
656
+ ev.borrowed = (int)((flags >> 10) & 1u);
657
+ uint32_t tag_mode = (flags >> 11) & 3u;
658
+ uint32_t anchor_mode = (flags >> 13) & 3u;
659
+ uint32_t anchor_hi = (flags >> 15) & 1u;
660
+ ev.tag_id = (uint8_t)((flags >> 16) & 0xFFu);
661
+ if (yt_read_span(t, w, &i, count, &ev.value) != 0) {
662
+ return -1;
663
+ }
664
+ if (tag_mode != YTM_ABSENT) {
665
+ if (yt_read_span(t, w, &i, count, &ev.tag) != 0) {
666
+ return -1;
667
+ }
668
+ }
669
+ if (anchor_mode != YTM_ABSENT) {
670
+ if (yt_read_span(t, w, &i, count, &ev.anchor) != 0) {
671
+ return -1;
672
+ }
673
+ }
674
+ if (anchor_hi) {
675
+ if (i >= count) {
676
+ return -1;
677
+ }
678
+ aid = (uint32_t)w[i++];
679
+ }
680
+ ev.anchor_id = aid;
681
+ if (yep_dom_on_event(d, &ev) != 0) {
682
+ return -1;
683
+ }
684
+ break;
685
+ }
686
+ case YTP_SCALAR: {
687
+ uint32_t flags = (uint32_t)(w[i] & 0xFFFFFFFFu);
688
+ uint32_t aid = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
689
+ i++;
690
+ int is_item = (int)((flags >> 20) & 1u);
691
+ int is_open_key = (int)((flags >> 21) & 1u);
692
+ if (is_item || is_open_key) {
693
+ yep_view v;
694
+ if (yt_read_span(t, w, &i, count, &v) != 0) {
695
+ return -1;
696
+ }
697
+ yep_block_value bv;
698
+ memset(&bv, 0, sizeof(bv));
699
+ bv.cls = YEP_LVAL_PLAIN;
700
+ bv.borrowed = (v.p >= t->input && v.p <= t->input + t->input_len) ? 1 : 0;
701
+ bv.value = v;
702
+ int rc = is_item ? dom_on_block_item(d, &bv) : dom_on_block_open(d, &v, 0, 0);
703
+ if (rc < 0) {
704
+ return -1;
705
+ }
706
+ break;
707
+ }
708
+ uint8_t tag_id = (uint8_t)(flags & 0xFFu);
709
+ uint8_t style = (uint8_t)((flags >> 8) & 7u);
710
+ uint8_t implicit = (uint8_t)((flags >> 11) & 1u);
711
+ uint8_t flow = (uint8_t)((flags >> 12) & 1u);
712
+ int borrowed = (int)((flags >> 13) & 1u);
713
+ uint32_t tag_mode = (flags >> 14) & 3u;
714
+ uint32_t anchor_mode = (flags >> 16) & 3u;
715
+ uint32_t anchor_hi = (flags >> 18) & 1u;
716
+ yep_view value, tag = {0}, anchor = {0};
717
+ if (yt_read_span(t, w, &i, count, &value) != 0) {
718
+ return -1;
719
+ }
720
+ if (tag_mode != YTM_ABSENT && yt_read_span(t, w, &i, count, &tag) != 0) {
721
+ return -1;
722
+ }
723
+ if (anchor_mode != YTM_ABSENT && yt_read_span(t, w, &i, count, &anchor) != 0) {
724
+ return -1;
725
+ }
726
+ if (anchor_hi) {
727
+ if (i >= count) {
728
+ return -1;
729
+ }
730
+ aid = (uint32_t)w[i++];
731
+ }
732
+ if (dom_on_scalar(d, &value, tag_mode ? &tag : NULL, anchor_mode ? &anchor : NULL, aid,
733
+ tag_id, style, implicit, flow, borrowed) < 0) {
734
+ return -1;
735
+ }
736
+ break;
737
+ }
738
+ case YTP_ITEM: {
739
+ yep_view v;
740
+ v.p = t->input + (uint32_t)w[i];
741
+ v.len = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
742
+ i++;
743
+ yep_block_value bv;
744
+ memset(&bv, 0, sizeof(bv));
745
+ bv.cls = YEP_LVAL_PLAIN;
746
+ bv.borrowed = 1;
747
+ bv.value = v;
748
+ if (dom_on_block_item(d, &bv) < 0) {
749
+ return -1;
750
+ }
751
+ break;
752
+ }
753
+ case YTP_PAIR: {
754
+ uint32_t flags = (uint32_t)(w[i] & 0xFFFFFFFFu);
755
+ uint32_t aid = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
756
+ i++;
757
+ yep_block_value bv;
758
+ memset(&bv, 0, sizeof(bv));
759
+ bv.cls = (uint8_t)(flags & 7u);
760
+ bv.borrowed = (int)((flags >> 3) & 1u);
761
+ uint32_t anchor_mode = (flags >> 4) & 3u;
762
+ uint32_t anchor_hi = (flags >> 6) & 1u;
763
+ yep_view key;
764
+ if (yt_read_span(t, w, &i, count, &key) != 0) {
765
+ return -1;
766
+ }
767
+ if (yt_read_span(t, w, &i, count, &bv.value) != 0) {
768
+ return -1;
769
+ }
770
+ if (bv.cls == YEP_LVAL_ALIAS) {
771
+ bv.borrowed = 1; /* the eager builder stamps alias names */
772
+ }
773
+ if (anchor_mode != YTM_ABSENT && yt_read_span(t, w, &i, count, &bv.anchor) != 0) {
774
+ return -1;
775
+ }
776
+ if (anchor_hi) {
777
+ if (i >= count) {
778
+ return -1;
779
+ }
780
+ aid = (uint32_t)w[i++];
781
+ }
782
+ bv.anchor_id = aid;
783
+ if (dom_on_block_pair(d, &key, &bv, 0, 0, 0) < 0) {
784
+ return -1;
785
+ }
786
+ break;
787
+ }
788
+ case YTP_OPEN: {
789
+ yep_view key;
790
+ key.p = t->input + (uint32_t)w[i];
791
+ key.len = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
792
+ i++;
793
+ if (dom_on_block_open(d, &key, 0, 0) < 0) {
794
+ return -1;
795
+ }
796
+ break;
797
+ }
798
+ case YTP_FLOW: {
799
+ uint32_t flags = (uint32_t)(w[i] & 0xFFFFFFFFu);
800
+ uint32_t aid = (uint32_t)(w[i] >> 32) & 0xFFFFFFu;
801
+ i++;
802
+ uint32_t tag_mode = flags & 3u;
803
+ uint32_t anchor_mode = (flags >> 2) & 3u;
804
+ uint32_t anchor_hi = (flags >> 4) & 1u;
805
+ if (i >= count) {
806
+ return -1;
807
+ }
808
+ size_t open = (size_t)(uint32_t)w[i];
809
+ size_t len = (size_t)(uint32_t)(w[i] >> 32);
810
+ i++;
811
+ yep_view tag = {0}, anchor = {0};
812
+ if (tag_mode != YTM_ABSENT && yt_read_span(t, w, &i, count, &tag) != 0) {
813
+ return -1;
814
+ }
815
+ if (anchor_mode != YTM_ABSENT && yt_read_span(t, w, &i, count, &anchor) != 0) {
816
+ return -1;
817
+ }
818
+ if (anchor_hi) {
819
+ if (i >= count) {
820
+ return -1;
821
+ }
822
+ aid = (uint32_t)w[i++];
823
+ }
824
+ size_t close = 0;
825
+ if (dom_on_flow_build(d, t->input, open, len, 0, 0, anchor, tag, aid, t->max_depth,
826
+ &close) != 1) {
827
+ return -1;
828
+ }
829
+ if (dom_on_flow_commit(d) < 0) {
830
+ return -1;
831
+ }
832
+ break;
833
+ }
834
+ default:
835
+ return -1; /* a span word where an op belongs: corruption */
836
+ }
837
+ }
838
+ return 0;
839
+ }
840
+
841
+ /* ---- the fused block runner (TODO.restructure/79, session 3) -------
842
+ *
843
+ * ONE SWAR sweep per line + inline classification + an indent stack,
844
+ * emitting the same record stream the engine's sink would — without
845
+ * the engine. Byte-exact against the engine over the block-family
846
+ * corpora (wide 600k / block 1.0M / deep 17k words, differential
847
+ * pinned by the FusedTape test). Any line the loop does not own bails
848
+ * to 1 and the caller re-runs the engine route (restart fallback).
849
+ *
850
+ * Bails (the engine route owns them): quoted/tagged/flow/folded/block
851
+ * scalar values, compact map items, anchored CONTAINER keys (the
852
+ * first pair inside one rides a two-SCALAR form with content-typed
853
+ * tag ids — capture before owning), doc markers/directives, empty
854
+ * "key:" at EOF or mid-doc, undefined aliases, root sequences. */
855
+
856
+ #define YT_FUSED_MAX_FRAMES 256
857
+ #define YT_FUSED_ANCHORS 65536
858
+
859
+ typedef struct {
860
+ uint32_t indent; /* the frame's key column */
861
+ uint8_t kind; /* 0 map, 1 seq */
862
+ uint8_t pending; /* a "key:" awaiting its child */
863
+ uint8_t started; /* the content container's start event went out */
864
+ uint8_t
865
+ own_map; /* the frame carries its own map (OPEN/root/seq); the key-SCALAR form does not */
866
+ uint8_t phantom; /* a content map opened by this frame's pairs is still live */
867
+ uint32_t content_col; /* the column this frame's pairs live at, +1 (0 = none) */
868
+ uint32_t key_off, key_len;
869
+ uint32_t aid; /* anchor ordinal on the pending container */
870
+ uint32_t a_off, a_len; /* the pending anchor's name span */
871
+ uint8_t anchored_first; /* the first pair/item rides the two-SCALAR form */
872
+ } yt_frame;
873
+
874
+ typedef struct {
875
+ yep_ytape* t;
876
+ const char* p;
877
+ size_t len;
878
+ uint32_t anchor_seq;
879
+ const yep_resolver* resolver; /* the first pair inside an anchored container
880
+ * bakes the engine's tag classification */
881
+ yep_pool* pool; /* block-scalar content (transferred to the doc) */
882
+ /* the repeat-alias memo (engine anchor_id_of): the last alias
883
+ * resolution answers by name bytes before any probe, and anchor
884
+ * definitions never invalidate it — re-aliasing after a
885
+ * redefinition still reports the memoized ordinal */
886
+ uint32_t memo_off, memo_len, memo_ord;
887
+ /* anchor name -> ordinal (the alias records carry the target's
888
+ * id; a miss bails: the engine owns undefined aliases). The table
889
+ * is heap-allocated on the first definition — small parses never
890
+ * pay for the 64k slots anchor-heavy documents need. */
891
+ struct {
892
+ uint32_t off, len, aid, tag;
893
+ }* anchors;
894
+ uint8_t* anchors_opened; /* per-aid: the anchored container's start event went out */
895
+ } yt_fused;
896
+
897
+ /* Frame model (engine-verified): st[] frames are the engine's *silent*
898
+ * containers — one OPEN record per frame (or one key SCALAR for the
899
+ * sibling form, which carries no map of its own). Pairs inside a frame
900
+ * open a *phantom* content map at the pair column (one start event per
901
+ * column); a later pair at the frame's own indent closes the phantom
902
+ * and rides the frame silently (the engine's sibling-continue). Maps
903
+ * pop strictly shallower; seqs also leave at their column when a
904
+ * non-dash arrives. The root closes only when its start event fired. */
905
+
906
+ /* chunked name hash: 8 bytes per round, one mix per round (the
907
+ * byte-wise FNV chain cost ~6 cycles/byte on this corpus's 80k
908
+ * lookups+puts per parse). The tail masks to the real bytes so only
909
+ * equal names hash equal. */
910
+ static uint32_t yt_anchor_hash(const char* p, uint32_t off, uint32_t len) {
911
+ uint64_t h = 2166136261u;
912
+ uint32_t i = 0;
913
+ for (; i < len;) {
914
+ uint32_t room = len - i;
915
+ uint64_t w;
916
+ if (room >= 8) {
917
+ memcpy(&w, p + off + i, 8);
918
+ i += 8;
919
+ } else {
920
+ uint64_t tail = 0;
921
+ memcpy(&tail, p + off + i, room);
922
+ w = tail;
923
+ i = len;
924
+ }
925
+ h = (h ^ w) * 11400714819323198485ull;
926
+ h ^= h >> 29;
927
+ }
928
+ return (uint32_t)(h ^ (h >> 32));
929
+ }
930
+
931
+ static int yt_anchor_table(yt_fused* F) {
932
+ if (F->anchors == NULL) {
933
+ F->anchors =
934
+ (void*)yep_alloc(yep_system_allocator(), YT_FUSED_ANCHORS * sizeof(*F->anchors));
935
+ if (F->anchors == NULL) {
936
+ return 0;
937
+ }
938
+ memset(F->anchors, 0, YT_FUSED_ANCHORS * sizeof(*F->anchors));
939
+ F->anchors_opened =
940
+ (void*)yep_alloc(yep_system_allocator(), YT_FUSED_ANCHORS * sizeof(uint8_t));
941
+ if (F->anchors_opened == NULL) {
942
+ return 0;
943
+ }
944
+ memset(F->anchors_opened, 0, YT_FUSED_ANCHORS * sizeof(uint8_t));
945
+ }
946
+ return 1;
947
+ }
948
+
949
+ static void yt_anchor_open(yt_fused* F, uint32_t aid) {
950
+ if (F->anchors_opened != NULL && aid != 0 && aid <= YT_FUSED_ANCHORS) {
951
+ F->anchors_opened[aid - 1] = 1;
952
+ }
953
+ }
954
+
955
+ static void yt_anchor_put(yt_fused* F, uint32_t off, uint32_t len, uint32_t aid) {
956
+ uint32_t m = YT_FUSED_ANCHORS - 1;
957
+ uint32_t tag = yt_anchor_hash(F->p, off, len);
958
+ uint32_t i = tag & m;
959
+ for (uint32_t n = 0; n < 64; n++) {
960
+ if (F->anchors[i].len == 0) {
961
+ F->anchors[i].off = off;
962
+ F->anchors[i].len = len;
963
+ F->anchors[i].aid = aid;
964
+ F->anchors[i].tag = tag;
965
+ return;
966
+ }
967
+ if (F->anchors[i].tag == tag && F->anchors[i].len == len &&
968
+ memcmp(F->p + F->anchors[i].off, F->p + off, len) == 0) {
969
+ F->anchors[i].aid = aid; /* redefinition: latest wins */
970
+ return;
971
+ }
972
+ i = (i + 1) & m;
973
+ }
974
+ /* chain too long: leave unfindable — aliases to it will bail */
975
+ }
976
+
977
+ static int yt_anchor_get(yt_fused* F, uint32_t off, uint32_t len, uint32_t* aid) {
978
+ if (F->anchors == NULL) {
979
+ return 0; /* no definitions yet: the engine owns the error */
980
+ }
981
+ if (F->memo_len != 0 && F->memo_len == len &&
982
+ memcmp(F->p + F->memo_off, F->p + off, len) == 0) {
983
+ *aid = F->memo_ord;
984
+ return 1;
985
+ }
986
+ uint32_t m = YT_FUSED_ANCHORS - 1;
987
+ uint32_t tag = yt_anchor_hash(F->p, off, len);
988
+ uint32_t i = tag & m;
989
+ for (uint32_t n = 0; n < 64; n++) {
990
+ if (F->anchors[i].len == 0) {
991
+ return 0;
992
+ }
993
+ if (F->anchors[i].tag == tag && F->anchors[i].len == len &&
994
+ memcmp(F->p + F->anchors[i].off, F->p + off, len) == 0) {
995
+ *aid = F->anchors[i].aid;
996
+ F->memo_off = off;
997
+ F->memo_len = len;
998
+ F->memo_ord = *aid;
999
+ return 1;
1000
+ }
1001
+ i = (i + 1) & m;
1002
+ }
1003
+ return 0;
1004
+ }
1005
+
1006
+ static void yt_f_empty_span(yt_fused* F) {
1007
+ /* the dead value-span pair the container events always carry */
1008
+ (void)yt_put(F->t, ((uint64_t)YTP_SPAN_POOL << 56) | 0u);
1009
+ (void)yt_put(F->t, 0);
1010
+ }
1011
+
1012
+ static void yt_f_start(yt_fused* F, yt_frame* fr) {
1013
+ if (fr->started) {
1014
+ return;
1015
+ }
1016
+ fr->started = 1;
1017
+ (void)yt_put(F->t, yt_props(YTP_EVENT, 0, fr->kind == 1 ? 5u : 7u));
1018
+ yt_f_empty_span(F);
1019
+ F->t->depth++;
1020
+ if (fr->kind == 0) {
1021
+ fr->phantom = 1;
1022
+ }
1023
+ }
1024
+
1025
+ static void yt_f_close(yt_fused* F, uint8_t kind) {
1026
+ (void)yt_put(F->t, yt_props(YTP_EVENT, 0, kind == 1 ? 6u : 8u));
1027
+ yt_f_empty_span(F);
1028
+ F->t->depth--;
1029
+ }
1030
+
1031
+ /* the OPEN-vs-SCALAR key form: the engine opens via on_block_open
1032
+ * unless its top frame is already a map continuing at this key's
1033
+ * column (a sibling key) — the contentless root always opens */
1034
+ static uint8_t yt_f_open_form(const yt_frame* parent, int parent_is_root, uint32_t key_col) {
1035
+ if (parent_is_root && parent->content_col == 0) {
1036
+ return 1;
1037
+ }
1038
+ if (parent->content_col != 0) {
1039
+ return (uint8_t)(key_col + 1 != parent->content_col && key_col != parent->indent);
1040
+ }
1041
+ return (uint8_t)(key_col != parent->indent);
1042
+ }
1043
+
1044
+ /* resolve a pending "key:": the OPEN record materializes the key's
1045
+ * map (it closes later); the SCALAR form is the bare key — the value
1046
+ * container that follows IS the pair's, no map of its own */
1047
+ static void yt_f_open(yt_fused* F, yt_frame* fr, uint8_t open_form) {
1048
+ if (open_form && fr->aid == 0 && fr->key_len < 0x1000000u && fr->key_off <= F->t->input_len) {
1049
+ (void)yt_put(F->t, yt_span_in(fr->key_off, fr->key_len, YTP_OPEN));
1050
+ fr->own_map = 1;
1051
+ } else {
1052
+ (void)yt_put(F->t, yt_props(YTP_SCALAR, 0, 0x2900u));
1053
+ (void)yt_put(F->t, yt_span_in(fr->key_off, fr->key_len, YTP_SPAN_IN));
1054
+ fr->own_map = 0;
1055
+ }
1056
+ fr->started = 0;
1057
+ fr->phantom = 0;
1058
+ F->t->depth++;
1059
+ }
1060
+
1061
+ /* The flat-map specialist (item 86): plain `key: value` lines at one
1062
+ * fixed content column, one map frame, nothing else. A separate FUNCTION
1063
+ * so its code stays inside the I-cache — the equivalent inline loop
1064
+ * measured 26% SLOWER by bloating yt_f_run past the cache edge. Returns
1065
+ * the position it stopped at; the caller reprocesses that line through
1066
+ * the full arms, so every bail here is a strict subset of what yt_f_run
1067
+ * would decide — parity holds by construction. */
1068
+ static size_t yt_flat_run(yep_ytape* t, const char* p, size_t pos, size_t len, uint32_t run_cc) {
1069
+ const uint64_t k_nl = 0x0A0A0A0A0A0A0A0Aull, k_cr = 0x0D0D0D0D0D0D0D0Dull,
1070
+ k_sp = 0x2020202020202020ull, k_co = 0x3A3A3A3A3A3A3A3Aull,
1071
+ k_ha = 0x2323232323232323ull;
1072
+ for (;;) {
1073
+ if (pos >= len) {
1074
+ return pos;
1075
+ }
1076
+ /* the sweep: the main loop's line facts, unchanged */
1077
+ size_t whole = len - pos;
1078
+ size_t end = whole, ind = whole;
1079
+ int have_ind = 0;
1080
+ size_t i = 0;
1081
+ size_t stop = whole;
1082
+ int stop_set = 0;
1083
+ while (i < whole) {
1084
+ size_t avail = whole - i < 8 ? whole - i : 8;
1085
+ uint64_t x;
1086
+ if (avail == 8) {
1087
+ memcpy(&x, p + pos + i, 8);
1088
+ } else {
1089
+ char buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
1090
+ memcpy(buf, p + pos + i, avail);
1091
+ memcpy(&x, buf, 8);
1092
+ }
1093
+ uint64_t valid = avail == 8 ? ~(uint64_t)0 : (((uint64_t)1 << (avail * 8)) - 1);
1094
+ uint64_t br = (yep_swar_eq8(x, k_nl) | yep_swar_eq8(x, k_cr)) & valid;
1095
+ uint64_t room = (br ? ((br & (~br + 1)) - 1) : valid) & YEP_SWAR_FLAGS & valid;
1096
+ if (!have_ind) {
1097
+ uint64_t nons = ~(yep_swar_eq8(x, k_sp)) & room;
1098
+ if (nons) {
1099
+ ind = i + (size_t)(yep_ctz64(nons) >> 3);
1100
+ have_ind = 1;
1101
+ }
1102
+ }
1103
+ if (have_ind && !stop_set) {
1104
+ uint64_t stm = (yep_swar_eq8(x, k_co) | yep_swar_eq8(x, k_ha)) & room;
1105
+ if (stm) {
1106
+ size_t cand = i + (size_t)(yep_ctz64(stm) >> 3);
1107
+ if (cand >= ind) {
1108
+ stop = cand;
1109
+ stop_set = 1;
1110
+ }
1111
+ }
1112
+ }
1113
+ if (br) {
1114
+ end = i + (size_t)(yep_ctz64(br) >> 3);
1115
+ break;
1116
+ }
1117
+ i += 8;
1118
+ }
1119
+ if (!have_ind) {
1120
+ ind = end;
1121
+ }
1122
+ if (!stop_set) {
1123
+ stop = end;
1124
+ }
1125
+ uint32_t le = (uint32_t)(pos + end);
1126
+ uint32_t li = (uint32_t)(pos + ind);
1127
+ uint32_t ls = (uint32_t)(pos + stop);
1128
+ uint32_t col = (uint32_t)(li - pos);
1129
+ /* blank and comment lines ride silently — the main loop skips
1130
+ * them with no state change */
1131
+ if (ind >= end || (p[li] == '#' && (li == 0 || p[li - 1] == ' ' || p[li - 1] == '\t' ||
1132
+ p[li - 1] == '\n'))) {
1133
+ pos = le < len ? (size_t)le + 1 : len;
1134
+ continue;
1135
+ }
1136
+ if (col + 1 != run_cc) {
1137
+ return pos; /* off-column: the pop/nest logic owns it */
1138
+ }
1139
+ /* document markers are the engine's alone — the "..."+content
1140
+ * error law lives there (the nightly's marshal crasher) */
1141
+ if (le - li >= 3 && (memcmp(p + li, "...", 3) == 0 || memcmp(p + li, "---", 3) == 0)) {
1142
+ size_t mk2 = li + 3;
1143
+ if (mk2 >= le || p[mk2] == ' ' || p[mk2] == '\t') {
1144
+ return pos;
1145
+ }
1146
+ }
1147
+ unsigned char c = (unsigned char)p[li];
1148
+ if (c == '\t' || c == '-' || c == '&' || c == '!' || c == '*' || c == '\'' || c == '"' ||
1149
+ c == '[' || c == '{' || c == '|' || c == '>' || c == '%' || c == '?' ||
1150
+ !yep_plain_first_ok(c)) {
1151
+ return pos; /* indicators, tab-led, and the plain-first law
1152
+ (a ']' key rode the flat-run — the nightly's
1153
+ marshal crasher "<: .ba\n]ge: 21"): full arms */
1154
+ }
1155
+ if (!stop_set || p[ls] != ':' || (ls + 1 < le && p[ls + 1] != ' ' && p[ls + 1] != '\t')) {
1156
+ return pos; /* not a plain key: value line */
1157
+ }
1158
+ size_t t0 = li;
1159
+ uint32_t kend = ls;
1160
+ while (kend > t0 && (p[kend - 1] == ' ' || p[kend - 1] == '\t')) {
1161
+ kend--;
1162
+ }
1163
+ if (kend == t0 || (uint32_t)(kend - t0) > 1024u) {
1164
+ return pos; /* empty key / the simple-key length law */
1165
+ }
1166
+ size_t vt = ls + 1;
1167
+ while (vt < le && (p[vt] == ' ' || p[vt] == '\t')) {
1168
+ vt++;
1169
+ }
1170
+ if (vt >= le || p[vt] == '#') {
1171
+ return pos; /* "key:" pending: the main loop's frame arm */
1172
+ }
1173
+ unsigned char vc = (unsigned char)p[vt];
1174
+ if (vc == '"' || vc == '\'') {
1175
+ /* the quoted plain-value arm, same law as the main loop:
1176
+ * same-line close, no backslash, no '' doubling, a comment
1177
+ * needs separating whitespace */
1178
+ size_t ce = vt + 1;
1179
+ int esc = 0;
1180
+ while (ce < le) {
1181
+ if (p[ce] == '\\') {
1182
+ esc = 1;
1183
+ break;
1184
+ }
1185
+ if (p[ce] == vc) {
1186
+ break;
1187
+ }
1188
+ ce++;
1189
+ }
1190
+ if (ce >= le || esc) {
1191
+ return pos; /* unterminated or escaped: the engine */
1192
+ }
1193
+ if (vc == '\'' && ce + 1 < le && p[ce + 1] == '\'') {
1194
+ return pos; /* the '' escape: the engine */
1195
+ }
1196
+ size_t te = ce + 1;
1197
+ while (te < le && (p[te] == ' ' || p[te] == '\t')) {
1198
+ te++;
1199
+ }
1200
+ if (te < le && (p[te] != '#' || te == ce + 1)) {
1201
+ return pos; /* trailing content after the close */
1202
+ }
1203
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
1204
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
1205
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, vc == '"' ? 0x2300u : 0x2200u));
1206
+ (void)yt_put(t, yt_span_in((uint32_t)(vt + 1), (uint32_t)(ce - vt - 1), YTP_SPAN_IN));
1207
+ pos = le < len ? (size_t)le + 1 : len;
1208
+ continue;
1209
+ }
1210
+ if (vc == '*' || vc == '|' || vc == '>' || vc == '[' || vc == '{' || vc == '!' ||
1211
+ vc == '%' || vc == '&' ||
1212
+ ((vc == '-' || vc == '?') &&
1213
+ (vt + 1 >= le || p[vt + 1] == ' ' || p[vt + 1] == '\t' || p[vt + 1] == '#'))) {
1214
+ return pos; /* block/flow/anchor values: the full arms */
1215
+ }
1216
+ if (!yep_plain_first_ok(vc)) {
1217
+ return pos; /* ']' '}' ',' '@' '`': no plain value can start
1218
+ * there (the nightly's "k1: --a &p{\n0: ]" marshal
1219
+ * crasher) — the engine errors cleanly */
1220
+ }
1221
+ /* a terminating ':' inside the value is the mapping-values error */
1222
+ {
1223
+ const char* h = (const char*)memchr(p + vt, ':', le - vt);
1224
+ while (h != NULL) {
1225
+ if (h + 1 >= p + le || h[1] == ' ' || h[1] == '\t') {
1226
+ return pos; /* the engine's error route */
1227
+ }
1228
+ h = (const char*)memchr(h + 1, ':', le - (size_t)(h + 1 - p));
1229
+ }
1230
+ }
1231
+ uint32_t vend = le;
1232
+ {
1233
+ const char* hh = (const char*)memchr(p + vt, '#', le - vt);
1234
+ while (hh != NULL) {
1235
+ if (hh > p + vt && (hh[-1] == ' ' || hh[-1] == '\t')) {
1236
+ vend = (uint32_t)(hh - p);
1237
+ break;
1238
+ }
1239
+ hh = (const char*)memchr(hh + 1, '#', le - (size_t)(hh + 1 - p));
1240
+ }
1241
+ }
1242
+ while (vend > vt && (p[vend - 1] == ' ' || p[vend - 1] == '\t')) {
1243
+ vend--;
1244
+ }
1245
+ (void)yt_put(t, yt_props(YTP_PAIR, 0, 2u | (1u << 3)));
1246
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
1247
+ (void)yt_put(t, yt_span_in((uint32_t)vt, vend - (uint32_t)vt, YTP_SPAN_IN));
1248
+ pos = le < len ? (size_t)le + 1 : len;
1249
+ }
1250
+ }
1251
+
1252
+ static int yt_f_run(yt_fused* F) {
1253
+ const char* p = F->p;
1254
+ size_t len = F->len;
1255
+ yep_ytape* t = F->t;
1256
+ yt_frame st[YT_FUSED_MAX_FRAMES];
1257
+ int depth = 0;
1258
+ memset(&st[0], 0, sizeof(st[0]));
1259
+ st[0].started = 1; /* the root's start event defers to its first pair */
1260
+ int root_open = 0;
1261
+ int root_evented = 0;
1262
+ uint32_t root_pair_col = 0; /* the root map's first pair line's column
1263
+ * (0 = none seen): the second-root dedent
1264
+ * law reads this, NOT content_col — the
1265
+ * pending arm never set content_col (" k:
1266
+ * &a", the nightly's run 39064966578,
1267
+ * issue #630) and arming content_col here
1268
+ * instead broke the " k: \n j: w" siblings
1269
+ * (the next nightly's crash-753ce4c1: the
1270
+ * map dissolved into four root scalars) */
1271
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 1u);
1272
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 3u);
1273
+ size_t pos = 0;
1274
+ while (pos < len) {
1275
+ /* the sweep: one NUL-padded SWAR walk per line */
1276
+ const uint64_t k_nl = 0x0A0A0A0A0A0A0A0Aull, k_cr = 0x0D0D0D0D0D0D0D0Dull,
1277
+ k_sp = 0x2020202020202020ull, k_co = 0x3A3A3A3A3A3A3A3Aull,
1278
+ k_ha = 0x2323232323232323ull;
1279
+ size_t whole = len - pos;
1280
+ size_t end = whole, indent = whole, stop = whole;
1281
+ int have_indent = 0, stop_set = 0;
1282
+ size_t i = 0;
1283
+ while (i < whole) {
1284
+ size_t avail = whole - i < 8 ? whole - i : 8;
1285
+ uint64_t x;
1286
+ if (avail == 8) {
1287
+ memcpy(&x, p + pos + i, 8);
1288
+ } else {
1289
+ char buf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
1290
+ memcpy(buf, p + pos + i, avail);
1291
+ memcpy(&x, buf, 8);
1292
+ }
1293
+ uint64_t valid = avail == 8 ? ~(uint64_t)0 : (((uint64_t)1 << (avail * 8)) - 1);
1294
+ uint64_t br = (yep_swar_eq8(x, k_nl) | yep_swar_eq8(x, k_cr)) & valid;
1295
+ uint64_t room = (br ? ((br & (~br + 1)) - 1) : valid) & YEP_SWAR_FLAGS & valid;
1296
+ if (!have_indent) {
1297
+ uint64_t nons = ~(yep_swar_eq8(x, k_sp)) & room;
1298
+ if (nons) {
1299
+ indent = i + (size_t)(yep_ctz64(nons) >> 3);
1300
+ have_indent = 1;
1301
+ }
1302
+ }
1303
+ if (have_indent && !stop_set) {
1304
+ uint64_t stm = (yep_swar_eq8(x, k_co) | yep_swar_eq8(x, k_ha)) & room;
1305
+ if (stm) {
1306
+ size_t cand = i + (size_t)(yep_ctz64(stm) >> 3);
1307
+ if (cand >= indent) {
1308
+ stop = cand;
1309
+ stop_set = 1;
1310
+ }
1311
+ }
1312
+ }
1313
+ if (br) {
1314
+ end = i + (size_t)(yep_ctz64(br) >> 3);
1315
+ break;
1316
+ }
1317
+ i += 8;
1318
+ }
1319
+ if (!have_indent) {
1320
+ indent = end;
1321
+ }
1322
+ if (!stop_set) {
1323
+ stop = end;
1324
+ }
1325
+ uint32_t line_end = (uint32_t)(pos + end);
1326
+ uint32_t line_indent = (uint32_t)(pos + indent);
1327
+ uint32_t line_stop = (uint32_t)(pos + stop);
1328
+ uint8_t blank = (uint8_t)(indent >= end);
1329
+ unsigned char c = blank ? 0 : (unsigned char)p[line_indent];
1330
+ uint8_t comment =
1331
+ (uint8_t)(c == '#' && (line_indent == 0 || p[line_indent - 1] == ' ' ||
1332
+ p[line_indent - 1] == '\t' || p[line_indent - 1] == '\n'));
1333
+ uint8_t dash = 0;
1334
+ if (c == '-' && line_indent + 1 < line_end) {
1335
+ unsigned char n1 = (unsigned char)p[line_indent + 1];
1336
+ if (n1 == ' ' || n1 == '\t') {
1337
+ dash = 1;
1338
+ }
1339
+ } else if (c == '-' && line_indent + 1 >= line_end) {
1340
+ dash = 1; /* a line-end dash is an indicator, not the scalar "-" */
1341
+ }
1342
+ size_t next = line_end < len ? (size_t)line_end + 1 : len;
1343
+ if (!blank && !comment) {
1344
+ size_t t0 = line_indent;
1345
+ uint32_t col = (uint32_t)(line_indent - pos);
1346
+ if (c == '\t') {
1347
+ return 1; /* tab-led content: the engine's error */
1348
+ }
1349
+ /* document markers are the engine's alone: only it carries
1350
+ * the "..."+content error law (the nightly's marshal
1351
+ * crasher, "a: b\n... vi an : }" — the runner swallowed
1352
+ * the post-'...' content as a pair) */
1353
+ if (col == 0 && line_end - line_indent >= 3 &&
1354
+ (memcmp(p + line_indent, "...", 3) == 0 ||
1355
+ memcmp(p + line_indent, "---", 3) == 0)) {
1356
+ size_t mk = line_indent + 3;
1357
+ if (mk >= line_end || p[mk] == ' ' || p[mk] == '\t') {
1358
+ return 1;
1359
+ }
1360
+ }
1361
+ if (dash && depth > 0 && st[depth].pending && col >= st[depth].indent) {
1362
+ /* the pending key resolves into a sequence: its own
1363
+ * frame (the key's map survives below it) */
1364
+ if (depth >= YT_FUSED_MAX_FRAMES - 1) {
1365
+ return 1; /* the frame stack is full: the engine route */
1366
+ }
1367
+ int anchored = st[depth].aid != 0;
1368
+ if (anchored) {
1369
+ if (col == st[depth].indent) {
1370
+ return 1; /* indentless anchored seq: the engine owns it
1371
+ * (the frame-close pairing assumes the seq's
1372
+ * own column, deeper than the key's) */
1373
+ }
1374
+ /* anchored sequence: S2900 key + the anchored SEQ start */
1375
+ if (depth == 1) {
1376
+ if (!root_open) {
1377
+ (void)yt_put(t, yt_props(YTP_EVENT, 0, 7u));
1378
+ yt_f_empty_span(F);
1379
+ t->depth++;
1380
+ root_open = 1;
1381
+ root_evented = 1;
1382
+ st[0].started = 1;
1383
+ }
1384
+ } else if (!st[depth - 1].started && col != st[depth - 1].indent) {
1385
+ yt_f_start(F, &st[depth - 1]);
1386
+ }
1387
+ uint32_t xaid = st[depth].aid;
1388
+ uint32_t xoff = st[depth].a_off;
1389
+ uint32_t xlen = st[depth].a_len;
1390
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
1391
+ (void)yt_put(t, yt_span_in(st[depth].key_off, st[depth].key_len, YTP_SPAN_IN));
1392
+ (void)yt_put(t, yt_props(YTP_EVENT, xaid, 5u | (1u << 13)));
1393
+ yt_f_empty_span(F);
1394
+ (void)yt_put(t, yt_span_in(xoff, xlen, YTP_SPAN_IN));
1395
+ yt_anchor_open(F, xaid);
1396
+ st[depth].pending = 0; /* the seq frame shadows this slot */
1397
+ } else {
1398
+ uint8_t of = yt_f_open_form(&st[depth - 1], depth == 1, st[depth].indent);
1399
+ yt_f_open(F, &st[depth], of);
1400
+ if (depth == 1 && st[0].content_col == 0) {
1401
+ /* the root bookkeeping the anchored branch carries:
1402
+ * without it a dedent past the root map's column
1403
+ * pops to a content_col-0 st[0] and the pair arm
1404
+ * read "root pairs at column 0" — the nightly's
1405
+ * run 38128905644, " american:\n - Boston...\n
1406
+ * national:" split into PHANTOM DOCUMENTS
1407
+ * ("---\nb\n---\nv") while the engine rejected */
1408
+ st[0].content_col = st[depth].indent + 1;
1409
+ }
1410
+ if (st[depth].content_col == 0) {
1411
+ /* the opened map's pairs live at the KEY's
1412
+ * column (the anchored branch tracks this;
1413
+ * without it the off-column guard never fires
1414
+ * and a key line BETWEEN the resolved seq's
1415
+ * column and the parent's fell through to a
1416
+ * SILENTLY TRUNCATED document — the nightly's
1417
+ * run 38118641722, "ue:\n - one\n woc:\n
1418
+ * - one\n wt o- : thrt" dropped lines 3-5
1419
+ * while the engine rejected at 3:2) */
1420
+ st[depth].content_col = st[depth].indent + 1;
1421
+ }
1422
+ root_open = 1;
1423
+ st[depth].pending = 0;
1424
+ }
1425
+ depth++;
1426
+ memset(&st[depth], 0, sizeof(st[depth]));
1427
+ st[depth].kind = 1;
1428
+ st[depth].indent = col;
1429
+ st[depth].own_map = 1;
1430
+ st[depth].anchored_first = (uint8_t)anchored;
1431
+ if (anchored) {
1432
+ st[depth].started = 1; /* the E5 went out above */
1433
+ } else {
1434
+ yt_f_start(F, &st[depth]);
1435
+ }
1436
+ } else {
1437
+ while (depth > 0 && (st[depth].kind == 1 ? (col < st[depth].indent ||
1438
+ (col <= st[depth].indent && !dash))
1439
+ : col < st[depth].indent)) {
1440
+ if (st[depth].kind == 1 && col == st[depth].indent && !dash &&
1441
+ !(depth > 1 && st[depth - 1].kind == 0 &&
1442
+ st[depth - 1].indent == st[depth].indent)) {
1443
+ return 1; /* a non-dash line at an INDENTED seq's
1444
+ * column: the seq owns its column, the
1445
+ * pair cannot join it and cannot close
1446
+ * it (libyaml: "did not find expected
1447
+ * '-' indicator"; the nightly's run
1448
+ * 38117287882, "amer:\n - j\n n:\n
1449
+ * - Bksl,} -- }" — the runner popped
1450
+ * the seq and SILENTLY DROPPED the
1451
+ * lines while the engine rejected).
1452
+ * Only the INDENTLESS seq — parent map
1453
+ * keys at the same column — closes on
1454
+ * a key line there ("k:\n- j\nn: v") */
1455
+ }
1456
+ if (st[depth].pending) {
1457
+ return 1; /* empty "key:" mid-doc: the engine owns it */
1458
+ }
1459
+ if (st[depth].phantom) {
1460
+ yt_f_close(F, st[depth].kind);
1461
+ st[depth].phantom = 0;
1462
+ }
1463
+ if (st[depth].own_map) {
1464
+ yt_f_close(F, st[depth].kind);
1465
+ }
1466
+ depth--;
1467
+ }
1468
+ }
1469
+ if (dash) {
1470
+ if (st[depth].kind == 1 && col == st[depth].indent) {
1471
+ yt_f_start(F, &st[depth]);
1472
+ size_t vt = t0 + 1;
1473
+ while (vt < line_end && (p[vt] == ' ' || p[vt] == '\t')) {
1474
+ vt++;
1475
+ }
1476
+ uint32_t vend = line_end;
1477
+ while (vend > vt && (p[vend - 1] == ' ' || p[vend - 1] == '\t')) {
1478
+ vend--;
1479
+ }
1480
+ if (vend == vt) {
1481
+ return 1; /* empty item */
1482
+ }
1483
+ unsigned char vc = (unsigned char)p[vt];
1484
+ if (vc == '"' || vc == '\'' || vc == '*' || vc == '&' || vc == '[' ||
1485
+ vc == '{' || vc == '!' || vc == '|' || vc == '>' || vc == '%' ||
1486
+ vc == '?') {
1487
+ return 1;
1488
+ }
1489
+ if (!yep_plain_first_ok(vc)) {
1490
+ return 1; /* ']'/','/'}'/'@'/'`' first: no plain
1491
+ item can start there (the nightly's
1492
+ marshal crasher, "x:\n- ]\"#") — the
1493
+ engine errors cleanly */
1494
+ }
1495
+ if (vc == '-' && (vt + 1 >= line_end || p[vt + 1] == ' ' || p[vt + 1] == '\t' ||
1496
+ p[vt + 1] == '#')) {
1497
+ return 1; /* nested dash (incl. line-end — the
1498
+ nightly's "n:\n- -\t" crasher: a
1499
+ trailing bare '-' is an indicator,
1500
+ never the plain scalar "-"): the
1501
+ engine's nested seq */
1502
+ }
1503
+ /* compact map item "- k: v"? */
1504
+ for (size_t ic = vt; ic < vend; ic++) {
1505
+ if (p[ic] == ':') {
1506
+ size_t nn = ic + 1;
1507
+ if (nn >= vend || p[nn] == ' ' || p[nn] == '\t') {
1508
+ return 1; /* compact items: the engine */
1509
+ }
1510
+ }
1511
+ }
1512
+ if (st[depth].anchored_first && F->resolver != NULL) {
1513
+ yep_tag_id tid =
1514
+ F->resolver->resolve(F->resolver->ctx, p + vt, (uint32_t)(vend - vt));
1515
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u | (uint32_t)tid));
1516
+ (void)yt_put(t, yt_span_in((uint32_t)vt, vend - (uint32_t)vt, YTP_SPAN_IN));
1517
+ st[depth].anchored_first = 0;
1518
+ } else {
1519
+ (void)yt_put(t, yt_span_in((uint32_t)vt, vend - (uint32_t)vt, YTP_ITEM));
1520
+ }
1521
+ if (!st[depth].content_col) {
1522
+ st[depth].content_col = col + 1;
1523
+ }
1524
+ } else {
1525
+ return 1;
1526
+ }
1527
+ } else if (stop_set && p[line_stop] == ':' &&
1528
+ (line_stop + 1 >= line_end || p[line_stop + 1] == ' ' ||
1529
+ p[line_stop + 1] == '\t') &&
1530
+ !(c == '&' || c == '!' || c == '*' || c == '\'' || c == '"' || c == '[' ||
1531
+ c == '{' || c == '|' || c == '>' || c == '%' || c == '?' || c == '-') &&
1532
+ yep_plain_first_ok((unsigned char)c)) {
1533
+ if (depth == 0) {
1534
+ if (root_pair_col != 0 && col < root_pair_col) {
1535
+ /* a dedent below the root map's own pair column:
1536
+ * a SECOND root without "---" (issue #630, " k:
1537
+ * &a\n - r\na: b" — the pending-value root arm
1538
+ * never arms content_col, so the off-column
1539
+ * guard below cannot see this; the engine and
1540
+ * libyaml reject with "did not find expected
1541
+ * <document start>") */
1542
+ return 1;
1543
+ }
1544
+ if (root_pair_col == 0) {
1545
+ root_pair_col = col; /* the first root pair owns its column */
1546
+ }
1547
+ }
1548
+ if (st[depth].kind == 0 &&
1549
+ ((st[depth].content_col != 0 && col + 1 != st[depth].content_col &&
1550
+ (col != st[depth].indent || (depth == 0 && st[0].content_col > 1))) ||
1551
+ (st[depth].content_col == 0 && depth > 0 && !st[depth].pending &&
1552
+ col != st[depth].indent))) {
1553
+ /* an OFF-COLUMN pair. No pending key awaits it, so a
1554
+ * more-indented line cannot open a child and a
1555
+ * dedented one is a second root (the catch-39 law;
1556
+ * libyaml: "mapping values are not allowed in this
1557
+ * context"). Two states: a frame with its own content
1558
+ * column rejects every other column; a frame whose
1559
+ * content map already closed (the phantom close above)
1560
+ * only accepts pairs at its own key column. The pop
1561
+ * loop pops on col < indent alone — without these
1562
+ * checks "a:\n b: 1\nc: 2\n d: 3" rode the stale
1563
+ * frame as a phantom sibling (the nightly's run
1564
+ * 38179552341) */
1565
+ return 1;
1566
+ }
1567
+ uint32_t kend = line_stop;
1568
+ while (kend > t0 && (p[kend - 1] == ' ' || p[kend - 1] == '\t')) {
1569
+ kend--;
1570
+ }
1571
+ if (kend == t0) {
1572
+ return 1; /* empty plain key (": v"): the engine's two-scalar form */
1573
+ }
1574
+ if ((uint32_t)(kend - t0) > 1024u) {
1575
+ return 1; /* the simple-key length law: the engine errors */
1576
+ }
1577
+ size_t vt = line_stop + 1;
1578
+ while (vt < line_end && (p[vt] == ' ' || p[vt] == '\t')) {
1579
+ vt++;
1580
+ }
1581
+ uint32_t aoff = 0, alen = 0, aid = 0;
1582
+ if (vt < line_end && p[vt] == '&') {
1583
+ size_t ne = vt + 1;
1584
+ while (ne < line_end && ne < len) {
1585
+ unsigned char pc = (unsigned char)p[ne];
1586
+ if (pc <= ' ' || pc == ',' || pc == '[' || pc == ']' || pc == '{' ||
1587
+ pc == '}' || pc == '#') {
1588
+ break;
1589
+ }
1590
+ ne++;
1591
+ }
1592
+ aoff = (uint32_t)(vt + 1);
1593
+ alen = (uint32_t)(ne - vt - 1);
1594
+ if (alen == 0) {
1595
+ return 1; /* an anchor needs a name (libyaml):
1596
+ the engine owns the error */
1597
+ }
1598
+ if (ne < line_end && p[ne] == '#') {
1599
+ return 1; /* a glued '#' is junk on the name
1600
+ (libyaml: "did not find expected
1601
+ alphabetic or numeric character") —
1602
+ the engine owns it */
1603
+ }
1604
+ F->anchor_seq++;
1605
+ aid = F->anchor_seq;
1606
+ if (!yt_anchor_table(F)) {
1607
+ return 1;
1608
+ }
1609
+ yt_anchor_put(F, aoff, alen, aid);
1610
+ vt = ne;
1611
+ while (vt < line_end && (p[vt] == ' ' || p[vt] == '\t')) {
1612
+ vt++;
1613
+ }
1614
+ }
1615
+ if (vt >= line_end || p[vt] == '#') {
1616
+ /* "key:" with a following-lines container: push pending
1617
+ * (aid rides it: an anchored container resolves through
1618
+ * the S2900 + anchored-start form, not on_block_open) */
1619
+ if (st[depth].kind == 0 && st[depth].content_col != 0 &&
1620
+ col + 1 != st[depth].content_col &&
1621
+ (col != st[depth].indent || (depth == 0 && st[0].content_col > 1))) {
1622
+ return 1; /* off-column: a continuation line — the multiline arm;
1623
+ * at the ROOT the indent exemption dies once pairs
1624
+ * live above column 0: a col-0 line under a col-1
1625
+ * root is a DEDENT — a second root without "---",
1626
+ * the engine's error (libyaml: "did not find
1627
+ * expected <document start>") */
1628
+ }
1629
+ if (st[depth].kind != 0) {
1630
+ return 1; /* a pending "key:" over a SEQ frame: the
1631
+ * key cannot join the sequence (libyaml
1632
+ * "mapping values are not allowed in
1633
+ * this context"; the nightly's run
1634
+ * 38127584324, "k:\n- 42\n bar:\n
1635
+ * - 45 e." — the pending pushed UNDER
1636
+ * the seq and materialized as an ITEM
1637
+ * while the engine rejected) */
1638
+ }
1639
+ if (st[depth].kind == 0 && st[depth].content_col != 0 &&
1640
+ col + 1 != st[depth].content_col && st[depth].phantom) {
1641
+ yt_f_close(F, st[depth].kind);
1642
+ st[depth].phantom = 0;
1643
+ st[depth].content_col =
1644
+ 0; /* the content map is gone:
1645
+ * its column must not greet later pairs (the
1646
+ * stale-frame law, run 38179552341) — after the
1647
+ * chained-pending arm resumes here, a stale
1648
+ * column rode yt_f_open_form's sibling test and
1649
+ * misfiled the next pending's container as a
1650
+ * key-SCALAR ("b:\n c: d\nq:\n b:\n c: d\n"
1651
+ * lost q's map; the nightly's run 42488986434) */
1652
+ }
1653
+ if (depth > 0 && st[depth].pending) {
1654
+ /* a chained pending ("a:\na:", the deep-nesting
1655
+ * "k:\n k:\n k:" ladder): resolve the outer
1656
+ * one before the sibling's frame takes the slot.
1657
+ * A same-column sibling key (col == the pending's
1658
+ * own column) gives the pending a NULL value —
1659
+ * the null pair MUST land now, or the OPEN'd
1660
+ * container swallows the sibling key as its
1661
+ * first pair's value (the nightly's run
1662
+ * 38110714729 tree corruption: {a: a, ?{b: c}:
1663
+ * h} instead of {a: null, a: {b: c}, h: v}).
1664
+ * When the OPEN form fired, the null pairs
1665
+ * INSIDE the opened container (parent → {k:
1666
+ * null}); under the key-SCALAR form it completes
1667
+ * the key's pair in the parent's map. A deeper
1668
+ * line (col > indent) opens the pending's
1669
+ * container and the chain nests. Anchored
1670
+ * pendings keep the engine: their anchor rides
1671
+ * the event path (yt_f_open drops the name) */
1672
+ if (st[depth].aid != 0) {
1673
+ return 1;
1674
+ }
1675
+ uint8_t of = yt_f_open_form(&st[depth - 1], depth == 1, st[depth].indent);
1676
+ if (aid != 0 && of == 0) {
1677
+ /* the outer pending resolved as a SIBLING
1678
+ * key-SCALAR: its value container is "the pair's",
1679
+ * materialized by the next OPEN-class record — but
1680
+ * the anchored pending resolves through the event
1681
+ * path, which emits its KEY SCALAR *before* the
1682
+ * anchored MAP_START. That key pairs with the
1683
+ * stranded outer key (the pair's value slot) and
1684
+ * the anchored map strands as a sibling key of the
1685
+ * parent: "2: e\n4:\n 5: &5\n 5: e\n7: s"
1686
+ * built {2:e, 4:"5", {5:e}&5:"7", s:null} instead
1687
+ * of {2:e, 4:{5:{5:e}&5}, 7:s} (the nightly's
1688
+ * crash-d8e3b3f392c37a62257576ce20d5a19b351f66ad,
1689
+ * round 213 — the first shape where a completed
1690
+ * pair arms root_pair_col BEFORE the chain). The
1691
+ * engine resolves the composition. */
1692
+ return 1;
1693
+ }
1694
+ yt_f_open(F, &st[depth], of);
1695
+ root_open = 1;
1696
+ if (col == st[depth].indent) {
1697
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x0904u));
1698
+ yt_f_empty_span(F);
1699
+ }
1700
+ st[depth].pending = 0;
1701
+ }
1702
+ /* a spent key-SCALAR frame (resolved, phantom closed,
1703
+ * no map of its own) gives its slot to the sibling —
1704
+ * chained same-column pendings would otherwise grow
1705
+ * the stack by one per record (the frame-full bail) */
1706
+ if (!(depth > 0 && !st[depth].pending && !st[depth].phantom &&
1707
+ !st[depth].own_map)) {
1708
+ if (depth >= YT_FUSED_MAX_FRAMES - 1) {
1709
+ return 1;
1710
+ }
1711
+ depth++;
1712
+ }
1713
+ memset(&st[depth], 0, sizeof(st[depth]));
1714
+ st[depth].indent = col;
1715
+ st[depth].pending = 1;
1716
+ st[depth].key_off = (uint32_t)t0;
1717
+ st[depth].key_len = kend - (uint32_t)t0;
1718
+ st[depth].aid = aid;
1719
+ st[depth].a_off = aoff;
1720
+ st[depth].a_len = alen;
1721
+ st[depth].anchored_first = (uint8_t)(aid != 0);
1722
+ } else {
1723
+ if (st[depth].kind == 0 && st[depth].content_col != 0 &&
1724
+ col + 1 != st[depth].content_col &&
1725
+ (col != st[depth].indent || (depth == 0 && st[0].content_col > 1))) {
1726
+ return 1; /* off-column: a continuation line — the multiline arm;
1727
+ * the root's indent exemption dies once pairs
1728
+ * live above column 0 (dedent = second root) */
1729
+ }
1730
+ if (depth > 0 && st[depth].pending && st[depth].aid != 0) {
1731
+ if (depth > 1) {
1732
+ /* an anchored pending BELOW the root frame: the
1733
+ * deferral's records only flush at depth 1 — a
1734
+ * deeper one drops its pairs at EOF ("4:\n4: r\n
1735
+ * 5:\n &5\n 5: e" lost the anchored map; the
1736
+ * nightly's run 38202382251) — the engine
1737
+ * resolves it */
1738
+ return 1;
1739
+ }
1740
+ if (col <= st[depth].indent) {
1741
+ return 1; /* same-column "content": the anchored value is
1742
+ * null and this line is a sibling — the engine */
1743
+ }
1744
+ /* an anchored container resolves through the event
1745
+ * path: the parent's content map opens for the key
1746
+ * first, then the key SCALAR + the anchored start */
1747
+ if (depth == 1) {
1748
+ if (!root_open) {
1749
+ (void)yt_put(t, yt_props(YTP_EVENT, 0, 7u));
1750
+ yt_f_empty_span(F);
1751
+ t->depth++;
1752
+ root_open = 1;
1753
+ root_evented = 1;
1754
+ st[0].started = 1;
1755
+ }
1756
+ /* the root now has content: later sibling pendings
1757
+ * take the key-SCALAR form, not OPEN */
1758
+ if (st[0].content_col == 0) {
1759
+ st[0].content_col = st[depth].indent + 1;
1760
+ }
1761
+ } else if (!st[depth - 1].started && col != st[depth - 1].indent) {
1762
+ yt_f_start(F, &st[depth - 1]);
1763
+ }
1764
+ uint32_t xaid = st[depth].aid;
1765
+ uint32_t xoff = st[depth].a_off;
1766
+ uint32_t xlen = st[depth].a_len;
1767
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
1768
+ (void)yt_put(t,
1769
+ yt_span_in(st[depth].key_off, st[depth].key_len, YTP_SPAN_IN));
1770
+ (void)yt_put(t, yt_props(YTP_EVENT, xaid, 7u | (1u << 13)));
1771
+ yt_f_empty_span(F);
1772
+ (void)yt_put(t, yt_span_in(xoff, xlen, YTP_SPAN_IN));
1773
+ yt_anchor_open(F, xaid);
1774
+ memset(&st[depth], 0, sizeof(st[depth]));
1775
+ st[depth].indent = col; /* the engine's frame pops on the CONTENT
1776
+ * column, not the anchored key's */
1777
+ st[depth].own_map = 1;
1778
+ st[depth].started = 1;
1779
+ st[depth].anchored_first = 1;
1780
+ t->depth++;
1781
+ } else if (depth > 0 && st[depth].pending) {
1782
+ uint8_t of = yt_f_open_form(&st[depth - 1], depth == 1, st[depth].indent);
1783
+ yt_f_open(F, &st[depth], of);
1784
+ if (depth == 1 && st[0].content_col == 0) {
1785
+ /* the pending->MAP resolution needs the same
1786
+ * root bookkeeping the seq arm gained (run
1787
+ * 38134498534, " e:\n f: g\nh: i" — the
1788
+ * dedent popped to a content_col-0 root and
1789
+ * the doc accepted what the engine rejects) */
1790
+ st[0].content_col = st[depth].indent + 1;
1791
+ }
1792
+ root_open = 1;
1793
+ st[depth].pending = 0;
1794
+ if (col == st[depth].indent) {
1795
+ /* a same-column sibling pair resolves the pending
1796
+ * key to null (the engine's empty-value scalar) */
1797
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x0904u));
1798
+ yt_f_empty_span(F);
1799
+ }
1800
+ } else if (!root_open && depth == 0) {
1801
+ (void)yt_put(t, yt_props(YTP_EVENT, 0, 7u));
1802
+ yt_f_empty_span(F);
1803
+ t->depth++;
1804
+ root_open = 1;
1805
+ root_evented = 1;
1806
+ st[0].started = 1;
1807
+ }
1808
+ if (col == st[depth].indent) {
1809
+ /* the engine's sibling-continue: the live phantom
1810
+ * content map closes, the pair rides the frame's
1811
+ * own map without a start event */
1812
+ if (st[depth].phantom) {
1813
+ yt_f_close(F, st[depth].kind);
1814
+ st[depth].phantom = 0;
1815
+ st[depth].content_col = 0; /* the content map is
1816
+ * gone: its column must not
1817
+ * greet later pairs (the
1818
+ * stale frame below) */
1819
+ }
1820
+ } else {
1821
+ yt_f_start(F, &st[depth]);
1822
+ }
1823
+ if (st[depth].kind != 0) {
1824
+ return 1;
1825
+ }
1826
+ unsigned char vc = (unsigned char)p[vt];
1827
+ uint32_t flags;
1828
+ if ((vc == '*' && aid != 0) || (vc == '*' && st[depth].anchored_first)) {
1829
+ return 1; /* an anchored/first alias: the engine owns it */
1830
+ }
1831
+ if (vc == '*') {
1832
+ size_t ne = vt + 1;
1833
+ while (ne < line_end) {
1834
+ unsigned char pc = (unsigned char)p[ne];
1835
+ if (pc <= ' ' || pc == ',' || pc == '[' || pc == ']' || pc == '{' ||
1836
+ pc == '}' || pc == '#') {
1837
+ break;
1838
+ }
1839
+ ne++;
1840
+ }
1841
+ if (ne < line_end && p[ne] == '#') {
1842
+ return 1; /* a glued '#' is junk on the
1843
+ * name (the engine errors:
1844
+ * "did not find expected
1845
+ * alphabetic or numeric
1846
+ * character") */
1847
+ }
1848
+ {
1849
+ size_t ae = ne;
1850
+ while (ae < line_end && (p[ae] == ' ' || p[ae] == '\t')) {
1851
+ ae++;
1852
+ }
1853
+ if (ae < line_end && p[ae] != '#') {
1854
+ return 1; /* an alias node is COMPLETE —
1855
+ * on the line only a blank
1856
+ * then a comment may follow
1857
+ * ("<<: *x &y": the engine
1858
+ * errors, libyaml "did not
1859
+ * find expected key"; this
1860
+ * branch silently dropped the
1861
+ * bytes) */
1862
+ }
1863
+ }
1864
+ uint32_t taid = 0;
1865
+ if (!yt_anchor_get(F, (uint32_t)(vt + 1), (uint32_t)(ne - vt - 1), &taid)) {
1866
+ return 1; /* undefined alias: the engine errors */
1867
+ }
1868
+ if (taid == 0 || taid > YT_FUSED_ANCHORS || F->anchors_opened == NULL ||
1869
+ !F->anchors_opened[taid - 1]) {
1870
+ return 1; /* alias to an anchor whose container never opened
1871
+ * (null-valued or still pending): the engine */
1872
+ }
1873
+ (void)yt_put(t, yt_props(YTP_PAIR, taid, 3u /*ALIAS*/));
1874
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
1875
+ (void)yt_put(t, yt_span_in((uint32_t)(vt + 1), (uint32_t)(ne - vt - 1),
1876
+ YTP_SPAN_IN));
1877
+ if (!st[depth].content_col) {
1878
+ st[depth].content_col = col + 1;
1879
+ }
1880
+ goto line_done;
1881
+ }
1882
+ if ((vc == '"' || vc == '\'') && aid == 0) {
1883
+ /* same-line quoted value: the two-SCALAR form (the
1884
+ * key plain + the styled value, quotes stripped);
1885
+ * escapes and multiline quotes stay with the engine */
1886
+ size_t ce = vt + 1;
1887
+ int esc = 0;
1888
+ while (ce < line_end) {
1889
+ if (p[ce] == '\\') {
1890
+ esc = 1;
1891
+ break;
1892
+ }
1893
+ if (p[ce] == vc) {
1894
+ break;
1895
+ }
1896
+ ce++;
1897
+ }
1898
+ if (ce >= line_end || esc) {
1899
+ return 1; /* unterminated or escaped: the engine */
1900
+ }
1901
+ if (vc == '\'' && ce + 1 < line_end && p[ce + 1] == '\'') {
1902
+ return 1; /* the '' escape: the engine */
1903
+ }
1904
+ size_t te = ce + 1;
1905
+ while (te < line_end && (p[te] == ' ' || p[te] == '\t')) {
1906
+ te++;
1907
+ }
1908
+ /* a comment needs separating whitespace — "#"
1909
+ * glued to the quote is content (the engine errors) */
1910
+ if (te < line_end && (p[te] != '#' || te == ce + 1)) {
1911
+ return 1; /* trailing content after the close */
1912
+ }
1913
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
1914
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
1915
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, vc == '"' ? 0x2300u : 0x2200u));
1916
+ (void)yt_put(t, yt_span_in((uint32_t)(vt + 1), (uint32_t)(ce - vt - 1),
1917
+ YTP_SPAN_IN));
1918
+ st[depth].anchored_first = 0;
1919
+ if (!st[depth].content_col) {
1920
+ st[depth].content_col = col + 1;
1921
+ }
1922
+ goto line_done;
1923
+ }
1924
+ if (vc == '|' && aid == 0 && !st[depth].anchored_first &&
1925
+ (vt + 1 >= line_end || p[vt + 1] == ' ' || p[vt + 1] == '\t')) {
1926
+ /* the header tail: only blanks and an optional
1927
+ * comment may follow the indicator (libyaml:
1928
+ * "did not find expected comment or line break";
1929
+ * the nightly's run 38119775008, "ext: | c* " —
1930
+ * this arm took the literal and SILENTLY DROPPED
1931
+ * the trailing header bytes while the engine
1932
+ * rejected) */
1933
+ size_t ht = vt + 1;
1934
+ while (ht < line_end && (p[ht] == ' ' || p[ht] == '\t')) {
1935
+ ht++;
1936
+ }
1937
+ if (ht < line_end && p[ht] != '#') {
1938
+ return 1;
1939
+ }
1940
+ /* bare literal block, clip chomp: measure the block,
1941
+ * copy it into the finish pool, emit the pool form */
1942
+ size_t scan = next;
1943
+ uint32_t bindent = 0;
1944
+ uint32_t lead_blank_max = 0;
1945
+ int have_bi = 0, have_content = 0;
1946
+ size_t clen = 0, tb = 0;
1947
+ size_t end_scan = scan;
1948
+ /* per-line copy facts: blocks whose lines fit
1949
+ * the array copy straight from it — the second,
1950
+ * boundary-rederiving walk disappears. Larger
1951
+ * blocks (nested in block-heavy) keep the walk
1952
+ * below. */
1953
+ struct {
1954
+ uint32_t off;
1955
+ uint32_t n;
1956
+ uint8_t blank;
1957
+ } ln[16];
1958
+ size_t nl = 0;
1959
+ int spilled = 0;
1960
+ while (scan < len) {
1961
+ size_t ls = scan;
1962
+ size_t le = ls;
1963
+ while (le < len && p[le] != '\n' && p[le] != '\r') {
1964
+ le++;
1965
+ }
1966
+ size_t ie = ls;
1967
+ while (ie < le && p[ie] == ' ') {
1968
+ ie++;
1969
+ }
1970
+ uint32_t cpy_off = 0, cpy_n = 0;
1971
+ uint8_t cpy_blank = 0;
1972
+ if (ie == le) {
1973
+ /* a blank line: spaces past the block indent are
1974
+ * CONTENT; only stripped-empty lines clip away */
1975
+ size_t ind = ie - ls;
1976
+ if (!have_content && ind > lead_blank_max) {
1977
+ lead_blank_max = (uint32_t)ind;
1978
+ }
1979
+ size_t res = have_bi && ind > bindent ? ind - bindent : 0;
1980
+ clen += res + 1;
1981
+ if (res == 0) {
1982
+ tb += 1;
1983
+ } else {
1984
+ tb = 0;
1985
+ }
1986
+ cpy_n = (uint32_t)res;
1987
+ cpy_blank = 1;
1988
+ } else {
1989
+ uint32_t ind = (uint32_t)(ie - ls);
1990
+ if (!have_bi) {
1991
+ if (ind <= col) {
1992
+ break; /* dedented past the block */
1993
+ }
1994
+ if (ind < lead_blank_max) {
1995
+ return 1; /* a LEADING blank line
1996
+ * with MORE indent
1997
+ * floors the block
1998
+ * (libyaml's
1999
+ * auto-detect takes
2000
+ * the blanks' max;
2001
+ * content below the
2002
+ * floor breaks the
2003
+ * map — Psych: "did
2004
+ * not find expected
2005
+ * key"; the nightly's
2006
+ * run 38163867815,
2007
+ * "a: |\n \n b"):
2008
+ * the engine's error */
2009
+ }
2010
+ bindent = ind;
2011
+ have_bi = 1;
2012
+ } else if (ind < bindent) {
2013
+ break; /* the block ends */
2014
+ }
2015
+ if (memchr(p + ls, '\t', le - ls) != NULL) {
2016
+ return 1; /* tabs: the engine's error */
2017
+ }
2018
+ clen += (le - (ls + bindent)) + 1;
2019
+ have_content = 1;
2020
+ tb = 0;
2021
+ cpy_off = (uint32_t)(ls + bindent);
2022
+ cpy_n = (uint32_t)(le - (ls + bindent));
2023
+ }
2024
+ if (nl < 16) {
2025
+ ln[nl].off = cpy_off;
2026
+ ln[nl].n = cpy_n;
2027
+ ln[nl].blank = cpy_blank;
2028
+ nl++;
2029
+ } else {
2030
+ spilled = 1;
2031
+ }
2032
+ end_scan =
2033
+ (le < len && p[le] == '\r' && le + 1 < len && p[le + 1] == '\n')
2034
+ ? le + 2
2035
+ : le + 1; /* lone CR is a 1-byte break */
2036
+ scan = end_scan;
2037
+ }
2038
+ if (!have_content) {
2039
+ return 1; /* empty/blank-only block: the engine */
2040
+ }
2041
+ size_t flen = clen - tb; /* clip: the trailing stripped-empty lines */
2042
+ if (F->pool == NULL) {
2043
+ F->pool = yep_pool_create(yep_system_allocator(), 4096);
2044
+ if (F->pool == NULL) {
2045
+ return 1;
2046
+ }
2047
+ }
2048
+ /* the BLOCK is clen-sized: both copy paths write
2049
+ * every measured line (the clip lives in the
2050
+ * SPAN, not the copy) — allocating flen wrote
2051
+ * tb bytes past the block whenever a literal
2052
+ * ended with stripped-empty lines (the nightly's
2053
+ * run 38137087116 ASan heap-buffer-overflow,
2054
+ * WRITE at ytape.c's re-walk memcpy) */
2055
+ char* dst = (char*)yep_pool_alloc(F->pool, clen, 16);
2056
+ if (dst == NULL) {
2057
+ return 1;
2058
+ }
2059
+ {
2060
+ char* q = dst;
2061
+ if (!spilled) {
2062
+ /* the array path: sizing and copy derive
2063
+ * from the SAME facts — exact by
2064
+ * construction, no re-walk */
2065
+ for (size_t x = 0; x < nl; x++) {
2066
+ if (ln[x].blank) {
2067
+ if (ln[x].n > 0) {
2068
+ memset(q, ' ', ln[x].n);
2069
+ q += ln[x].n;
2070
+ }
2071
+ } else {
2072
+ memcpy(q, p + ln[x].off, ln[x].n);
2073
+ q += ln[x].n;
2074
+ }
2075
+ *q++ = '\n';
2076
+ }
2077
+ } else {
2078
+ size_t s2 = next;
2079
+ while (s2 < scan) {
2080
+ size_t ls = s2;
2081
+ size_t le = ls;
2082
+ while (le < len && p[le] != '\n' && p[le] != '\r') {
2083
+ le++;
2084
+ }
2085
+ size_t ie = ls;
2086
+ while (ie < le && p[ie] == ' ') {
2087
+ ie++;
2088
+ }
2089
+ if (ie == le) {
2090
+ size_t ind = ie - ls;
2091
+ size_t res = have_bi && ind > bindent ? ind - bindent : 0;
2092
+ if (res > 0) {
2093
+ memset(q, ' ', res);
2094
+ q += res;
2095
+ }
2096
+ *q++ = '\n';
2097
+ } else {
2098
+ memcpy(q, p + ls + bindent, le - (ls + bindent));
2099
+ q += le - (ls + bindent);
2100
+ *q++ = '\n';
2101
+ }
2102
+ s2 = (le < len && p[le] == '\r' && le + 1 < len &&
2103
+ p[le + 1] == '\n')
2104
+ ? le + 2
2105
+ : le + 1; /* lone CR is a 1-byte break */
2106
+ }
2107
+ }
2108
+ if ((size_t)(q - dst) != clen) {
2109
+ return 1; /* drift: bail, not corrupt */
2110
+ }
2111
+ }
2112
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
2113
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
2114
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x0400u));
2115
+ (void)yt_put(t, ((uint64_t)YTP_SPAN_POOL << 56) | (uint64_t)flen);
2116
+ (void)yt_put(t, (uint64_t)(uintptr_t)dst);
2117
+ if (!st[depth].content_col) {
2118
+ st[depth].content_col = col + 1;
2119
+ }
2120
+ next = scan;
2121
+ goto line_done;
2122
+ }
2123
+ if (vc == '"' || vc == '\'' || vc == '|' || vc == '>' || vc == '[' ||
2124
+ vc == '{' || vc == '!' || vc == '%' || vc == '&' || vc == '*') {
2125
+ return 1; /* quoted/block/flow/tag/anchor/alias values:
2126
+ the engine */
2127
+ }
2128
+ if ((vc == '-' || vc == '?') && (vt + 1 >= line_end || p[vt + 1] == ' ' ||
2129
+ p[vt + 1] == '\t' || p[vt + 1] == '#')) {
2130
+ return 1; /* an indicator-as-value: the engine errors */
2131
+ }
2132
+ if (!yep_plain_first_ok(vc)) {
2133
+ return 1; /* '}' ']' ',' '@' '`' '%': no node
2134
+ * content can start there (the nightly's
2135
+ * fuzz_roundtrip crasher, "u: }") — the engine
2136
+ * errors cleanly */
2137
+ }
2138
+ /* a terminating ':' inside the value is the
2139
+ * mapping-values error — the engine owns it */
2140
+ for (const char* h = (const char*)memchr(p + vt, ':', line_end - vt);
2141
+ h != NULL && h < p + line_end;
2142
+ h = (const char*)memchr(h + 1, ':', line_end - (size_t)(h + 1 - p))) {
2143
+ if (h + 1 >= p + line_end || h[1] == ' ' || h[1] == '\t') {
2144
+ return 1;
2145
+ }
2146
+ }
2147
+ /* trailing comment bounds the plain value */
2148
+ uint32_t vend = line_end;
2149
+ {
2150
+ const char* h = (const char*)memchr(p + vt, '#', line_end - vt);
2151
+ while (h != NULL) {
2152
+ if (h > p + vt && (h[-1] == ' ' || h[-1] == '\t')) {
2153
+ vend = (uint32_t)(h - p);
2154
+ break;
2155
+ }
2156
+ h = (const char*)memchr(h + 1, '#', line_end - (size_t)(h + 1 - p));
2157
+ }
2158
+ }
2159
+ while (vend > vt && (p[vend - 1] == ' ' || p[vend - 1] == '\t')) {
2160
+ vend--;
2161
+ }
2162
+ if (st[depth].anchored_first) {
2163
+ /* the first pair inside an anchored container: the
2164
+ * two-SCALAR form with the engine's tag bake */
2165
+ if (aid != 0 || F->resolver == NULL) {
2166
+ return 1;
2167
+ }
2168
+ yep_tag_id tid =
2169
+ F->resolver->resolve(F->resolver->ctx, p + vt, (uint32_t)(vend - vt));
2170
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u));
2171
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
2172
+ (void)yt_put(t, yt_props(YTP_SCALAR, 0, 0x2900u | (uint32_t)tid));
2173
+ (void)yt_put(t, yt_span_in((uint32_t)vt, vend - (uint32_t)vt, YTP_SPAN_IN));
2174
+ st[depth].anchored_first = 0;
2175
+ if (!st[depth].content_col) {
2176
+ st[depth].content_col = col + 1;
2177
+ }
2178
+ goto line_done;
2179
+ }
2180
+ if (aid != 0) {
2181
+ flags = 4u | (1u << 3) | (1u << 4);
2182
+ } else {
2183
+ flags = 2u | (1u << 3);
2184
+ }
2185
+ (void)yt_put(t, yt_props(YTP_PAIR, aid, flags));
2186
+ (void)yt_put(t, yt_span_in((uint32_t)t0, kend - (uint32_t)t0, YTP_SPAN_IN));
2187
+ (void)yt_put(t, yt_span_in((uint32_t)vt, vend - (uint32_t)vt, YTP_SPAN_IN));
2188
+ if (aid != 0) {
2189
+ (void)yt_put(t, yt_span_in(aoff, alen, YTP_SPAN_IN));
2190
+ }
2191
+ if (!st[depth].content_col) {
2192
+ st[depth].content_col = col + 1;
2193
+ }
2194
+ if (aid == 0 && !st[depth].phantom && st[depth].content_col == col + 1) {
2195
+ /* the specialist takes the run at ANY frame's
2196
+ * content column: nested flat runs (anchored
2197
+ * containers, leaf maps) qualify; an off-column
2198
+ * line bails to the pop/nest arms as usual.
2199
+ * anchored_first is already cleared whenever a
2200
+ * plain emission reaches here (the anchored arm
2201
+ * leaves early), so no extra gate is owed. */
2202
+ size_t sp = yt_flat_run(t, p, next, len, st[depth].content_col);
2203
+ if (sp != next) {
2204
+ pos = sp;
2205
+ goto line_done_keep;
2206
+ }
2207
+ }
2208
+ }
2209
+ } else {
2210
+ return 1; /* unclassified line: the engine route */
2211
+ }
2212
+ }
2213
+ line_done:
2214
+ pos = next;
2215
+ line_done_keep: /* the specialist's stop line: reprocess it in place */
2216
+ ;
2217
+ }
2218
+ if (!root_open) {
2219
+ return 1; /* empty document: the engine owns it */
2220
+ }
2221
+ while (depth > 0) {
2222
+ if (st[depth].pending) {
2223
+ return 1; /* empty "key:" at EOF: the engine */
2224
+ }
2225
+ if (st[depth].phantom) {
2226
+ yt_f_close(F, st[depth].kind);
2227
+ st[depth].phantom = 0;
2228
+ }
2229
+ if (st[depth].own_map) {
2230
+ yt_f_close(F, st[depth].kind);
2231
+ }
2232
+ depth--;
2233
+ }
2234
+ if (root_evented) {
2235
+ yt_f_close(F, 0); /* only an event-started root closes */
2236
+ }
2237
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 4u);
2238
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 2u);
2239
+ t->docs = 1;
2240
+ return 0;
2241
+ }
2242
+
2243
+ /* The flow-rooted fast path (item 86): a document whose entire content
2244
+ * is ONE flow collection plus whitespace skips both the block runner
2245
+ * and the engine — the budgeted classification runs once and the
2246
+ * engine's exact five-word scaffolding lands directly. Any marker,
2247
+ * comment, anchor-bearing prefix, or second content falls back. */
2248
+ int ytap_flow_rooted(yep_ytape* t) {
2249
+ const char* p = t->input;
2250
+ size_t len = t->input_len;
2251
+ size_t i = 0;
2252
+ while (i < len && (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r')) {
2253
+ i++;
2254
+ }
2255
+ if (i >= len || (p[i] != '[' && p[i] != '{')) {
2256
+ return 1; /* not flow-rooted: the block runner's turn */
2257
+ }
2258
+ size_t mark = t->count;
2259
+ size_t open = i;
2260
+ yep_view none = {NULL, 0};
2261
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 1u);
2262
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 3u);
2263
+ size_t close = 0;
2264
+ int v = yt_flow_classify_record(t, p, open, len - open, &none, &none, 0,
2265
+ 1000 /* YEP_MAX_DEPTH: the engine's
2266
+ * default limit (engine.c) */
2267
+ ,
2268
+ &close);
2269
+ if (v != 1) {
2270
+ t->count = (uint32_t)mark; /* unwind: the engine route owns it */
2271
+ t->depth = 0;
2272
+ return 1;
2273
+ }
2274
+ for (size_t e = close + 1; e < len; e++) {
2275
+ if (p[e] != ' ' && p[e] != '\t' && p[e] != '\n' && p[e] != '\r') {
2276
+ t->count = (uint32_t)mark; /* trailing content: the engine */
2277
+ t->depth = 0;
2278
+ return 1;
2279
+ }
2280
+ }
2281
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 4u);
2282
+ (void)yt_put(t, ((uint64_t)YTP_EV_COMPACT << 56) | 2u);
2283
+ t->docs = 1;
2284
+ return 0;
2285
+ }
2286
+
2287
+ int ytap_fused_run(yep_ytape* t, const yep_resolver* resolver) {
2288
+ yt_fused* F = (yt_fused*)yep_alloc(yep_system_allocator(), sizeof(*F));
2289
+ if (F == NULL) {
2290
+ return 1; /* allocation refused: the engine route */
2291
+ }
2292
+ memset(F, 0, sizeof(*F));
2293
+ F->t = t;
2294
+ F->p = t->input;
2295
+ F->len = t->input_len;
2296
+ F->resolver = resolver;
2297
+ size_t mark = t->count;
2298
+ int rc = yt_f_run(F);
2299
+ yep_pool* rpool = F->pool; /* capture before the runner frees */
2300
+ yep_free(yep_system_allocator(), F->anchors);
2301
+ yep_free(yep_system_allocator(), F->anchors_opened);
2302
+ yep_free(yep_system_allocator(), F);
2303
+ if (rc != 0) {
2304
+ t->count = (uint32_t)mark; /* unwind: the caller resets and re-runs */
2305
+ t->depth = 0;
2306
+ if (rpool != NULL) {
2307
+ yep_pool_destroy(rpool);
2308
+ }
2309
+ return 1;
2310
+ }
2311
+ if (rpool != NULL) {
2312
+ t->pool = rpool; /* transferred to the document with the tape */
2313
+ }
2314
+ return 0;
2315
+ }
2316
+
2317
+ void ytap_reset(yep_ytape* t) {
2318
+ t->count = 0;
2319
+ t->depth = 0;
2320
+ t->docs = 0;
2321
+ t->oom = 0;
2322
+ t->flow_mark = 0;
2323
+ }