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,2375 @@
1
+ /* tape.c — the compact JSON tape (TODO.restructure/85, v2 records).
2
+ *
3
+ * The strict fused walk drives: one THREE-column record per token
4
+ * (kind byte, off, len) — bools encode in the kind, container links
5
+ * ride the off column, numbers record spans only (the lean shape scan
6
+ * validates; yeptris_tape_convert materializes). Strings stay
7
+ * zero-copy spans. The record budget derives from the input length —
8
+ * every token consumes at least one input byte — so len+2 slots
9
+ * suffice with no pre-pass.
10
+ *
11
+ * Route shape mirrors yeptris_parse_json: a gate-clean container root
12
+ * fuses validation into the walk; everything else validates through
13
+ * yep_json_document first (the error precedence json-suite-strict
14
+ * pins), then builds. The walk's DONE delivers the root close as a
15
+ * token; the root OPEN is synthesized here (walk_init consumes the
16
+ * opener without emitting it). */
17
+
18
+ #include <stdlib.h>
19
+ #include <string.h>
20
+
21
+ #include <yeptris/tape.h>
22
+
23
+ #if defined(__GNUC__)
24
+ #define YEP_UNUSED_FN __attribute__((unused))
25
+ #else
26
+ #define YEP_UNUSED_FN
27
+ #endif
28
+
29
+ #ifndef YEP_TOKEN_CONTRACT_ROUTE
30
+ #define YEP_TOKEN_CONTRACT_ROUTE 0
31
+ #endif
32
+
33
+ #include "common/error.h"
34
+ #include "common/simd_text.h"
35
+ #include "encoding/encoding.h"
36
+ #include "memory/allocator.h"
37
+ #include "parse/scalars.h"
38
+ #include "scan/json.h"
39
+
40
+ typedef struct {
41
+ yeptris_json_tape* t;
42
+ size_t cap;
43
+ uint32_t open[YEP_JSON_WALK_DEPTH]; /* record indices */
44
+ int depth;
45
+ int oom;
46
+ } tape_ctx;
47
+
48
+ static int rec_put(tape_ctx* c, uint8_t kind, uint32_t off, uint32_t len) {
49
+ if (c->t->count >= c->cap) {
50
+ c->oom = 1;
51
+ return 0;
52
+ }
53
+ size_t i = c->t->count++;
54
+ c->t->kinds[i] = kind;
55
+ c->t->offs[i] = off;
56
+ c->t->lens[i] = len;
57
+ return 1;
58
+ }
59
+
60
+ static YeptrisStatus tape_carve(yeptris_json_tape* t, size_t len) {
61
+ size_t cap = len + 2;
62
+ size_t off_o = (cap + 15) & ~(size_t)15;
63
+ char* block = yep_alloc(yep_system_allocator(),
64
+ off_o + 2 * cap * sizeof(uint32_t) + cap * sizeof(yeptris_tape_rec));
65
+ if (block == NULL) {
66
+ return YEPTRIS_ERROR_MEMORY;
67
+ }
68
+ t->_block = block;
69
+ t->kinds = (uint8_t*)block;
70
+ t->offs = (uint32_t*)(void*)(block + off_o);
71
+ t->lens = t->offs + cap;
72
+ t->recs = (yeptris_tape_rec*)(void*)(t->lens + cap);
73
+ t->count = 0;
74
+ t->int_min = 0;
75
+ t->_rec_primary = 0;
76
+ t->_cols_ready = 0;
77
+ t->_long_key_known = 0;
78
+ t->_long_key = 0;
79
+ return YEPTRIS_OK;
80
+ }
81
+
82
+ /* The lazy column materialization (TODO.max-perf/07): records are the
83
+ * primary storage on the lenient route; the column ABI materializes
84
+ * from them on first touch. */
85
+ YEPTRIS_API int yeptris_tape_columns(yeptris_json_tape* t) {
86
+ if (t == NULL || !t->_rec_primary || t->recs == NULL || t->_cols_ready) {
87
+ return 0; /* column-primary (the strict route) or ready */
88
+ }
89
+ for (size_t i = 0; i < t->count; i++) {
90
+ yeptris_tape_rec r = t->recs[i];
91
+ t->lens[i] = (uint32_t)((r >> 8) & 0xFFFFFFu);
92
+ t->offs[i] = (uint32_t)(r >> 32);
93
+ t->kinds[i] = (uint8_t)(r & 0xFFu);
94
+ }
95
+ t->_cols_ready = 1;
96
+ return 0;
97
+ }
98
+
99
+ /* Scalar roots: the walk needs an opener, so a bare root value
100
+ * converts through the same kernels. */
101
+ static int tape_put_root_scalar(tape_ctx* c, const char* p, size_t len, size_t at) {
102
+ char ch = p[at];
103
+ if (ch == '"') {
104
+ size_t i = at;
105
+ size_t close = 0;
106
+ int esc = 0;
107
+ if (!yep_json_string(p, len, &i, &close, &esc)) {
108
+ return 0;
109
+ }
110
+ return rec_put(c, YEP_T_STR, (uint32_t)(at + 1), (uint32_t)(close - at - 1));
111
+ }
112
+ if (ch == 't' || ch == 'f' || ch == 'n') {
113
+ const char* word = ch == 't' ? "true" : (ch == 'f' ? "false" : "null");
114
+ size_t i = at;
115
+ if (!yep_json_literal(p, len, &i, word)) {
116
+ return 0;
117
+ }
118
+ uint8_t kind = ch == 'n' ? YEP_T_NULL : (ch == 't' ? YEP_T_TRUE : YEP_T_FALSE);
119
+ return rec_put(c, kind, (uint32_t)at, (uint32_t)(i - at));
120
+ }
121
+ size_t i = 0;
122
+ int flt = 0;
123
+ if (yep_json_number_shape(p + at, len - at, &i, &flt) == 0) {
124
+ return 0;
125
+ }
126
+ return rec_put(c, flt ? YEP_T_FLOAT : YEP_T_INT, (uint32_t)at, (uint32_t)i);
127
+ }
128
+
129
+ /* One strict walk over p[open] -> records. Returns OK, MEMORY
130
+ * (tape zeroed), or PARSE (walk rejected; tape zeroed).
131
+ * check_tail: the fast route has no document validation, so it
132
+ * rejects trailing garbage here.
133
+ *
134
+ * FUSED specialization of yep_json_walk_next: same states, same
135
+ * rejects, byte for byte — but the token never crosses a call
136
+ * boundary or a tok struct. Cursor, record count, and the
137
+ * top-of-stack (kind/expect/open_at) ride in locals; the depth
138
+ * arrays are only touched on push/pop. Columns write directly
139
+ * (capacity is structurally sound: every record consumes at least
140
+ * one input byte, and the carve holds len+2). yep_json_walk stays
141
+ * the reference machine for DOM/push/pull; tape-diff (corpus + the
142
+ * 2M fuzz parity) pins the equivalence. */
143
+ /* The punctuation inline (the simdjson tape-builder lesson): after a
144
+ * value or a key, the next token is a comma/colon or the close —
145
+ * consume it inline instead of paying a full loop iteration for a
146
+ * one-byte state flip. Anything else (close, garbage) falls through
147
+ * with the cursor parked for the main loop to judge. Whitespace here
148
+ * is RFC 8259's set — tab included. */
149
+ static inline uint8_t tape_inline_punct(const char* p, size_t len, size_t* i, uint8_t want) {
150
+ size_t j = *i;
151
+ while (j < len && (p[j] == ' ' || p[j] == '\t' || p[j] == '\n' || p[j] == '\r')) {
152
+ j++;
153
+ }
154
+ if (j < len && (uint8_t)p[j] == want) {
155
+ *i = j + 1;
156
+ return 1;
157
+ }
158
+ *i = j;
159
+ return 0;
160
+ }
161
+
162
+ static YeptrisStatus tape_walk(const char* p, size_t len, size_t open, yeptris_json_tape* t,
163
+ int check_tail) {
164
+ if (tape_carve(t, len) != YEPTRIS_OK) {
165
+ return YEPTRIS_ERROR_MEMORY;
166
+ }
167
+ uint8_t* kinds = t->kinds;
168
+ uint32_t* offs = t->offs;
169
+ uint32_t* lens = t->lens;
170
+ size_t cap = len + 2;
171
+ uint32_t open_at[YEP_JSON_WALK_DEPTH];
172
+ uint8_t kind[YEP_JSON_WALK_DEPTH];
173
+
174
+ size_t count = 0;
175
+ kinds[0] = YEP_T_DOC;
176
+ offs[0] = 0;
177
+ lens[0] = 0;
178
+ count = 1;
179
+
180
+ uint8_t top_kind = p[open] == '[' ? 0 : 1;
181
+ kinds[1] = top_kind ? YEP_T_MAP_OPEN : YEP_T_SEQ_OPEN;
182
+ offs[1] = 0; /* the CLOSE arm writes the link */
183
+ lens[1] = 0;
184
+ uint32_t top_open = 1;
185
+ count = 2;
186
+ uint8_t top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
187
+ int depth = 1;
188
+ open_at[0] = top_open;
189
+ kind[0] = top_kind;
190
+
191
+ size_t i = open + 1;
192
+ for (;;) {
193
+ while (i < len && (p[i] == ' ' || p[i] == '\t' || p[i] == '\n' || p[i] == '\r')) {
194
+ i++;
195
+ }
196
+ if (i >= len) {
197
+ goto reject;
198
+ }
199
+ size_t at = i;
200
+ char c = p[i];
201
+
202
+ /* walker order: punctuation states first so a value arm cannot
203
+ * fire in JW_COMMA_OR_CLOSE / JW_COLON ("[1 2]" is a reject).
204
+ * Close is legal in COMMA_OR_CLOSE, so it falls through. */
205
+ if (top_expect == JW_COLON) {
206
+ if (c != ':') {
207
+ goto reject;
208
+ }
209
+ top_expect = JW_VALUE;
210
+ i = at + 1;
211
+ continue;
212
+ }
213
+ if (top_expect == JW_COMMA_OR_CLOSE) {
214
+ if (c == ',') {
215
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
216
+ i = at + 1;
217
+ continue;
218
+ }
219
+ if (c != ']' && c != '}') {
220
+ goto reject;
221
+ }
222
+ /* close falls through */
223
+ }
224
+
225
+ int key_slot = top_kind == 1 && (top_expect == JW_KEY_OR_CLOSE || top_expect == JW_KEY);
226
+
227
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
228
+ if (key_slot) {
229
+ goto reject;
230
+ }
231
+ int flt = 0;
232
+ if (!yep_json_number_shape(p, len, &i, &flt)) {
233
+ goto reject;
234
+ }
235
+ kinds[count] = flt ? YEP_T_FLOAT : YEP_T_INT;
236
+ offs[count] = (uint32_t)at;
237
+ lens[count] = (uint32_t)(i - at);
238
+ count++;
239
+ if (tape_inline_punct(p, len, &i, ',')) {
240
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
241
+ key_slot = top_kind ? 1 : 0;
242
+ } else {
243
+ top_expect = JW_COMMA_OR_CLOSE;
244
+ }
245
+ continue;
246
+ }
247
+ if (c == '"') {
248
+ size_t close = 0;
249
+ int esc = 0;
250
+ if (!yep_json_string(p, len, &i, &close, &esc)) {
251
+ goto reject;
252
+ }
253
+ kinds[count] = YEP_T_STR;
254
+ offs[count] = (uint32_t)(at + 1);
255
+ lens[count] = (uint32_t)(close - at - 1);
256
+ count++;
257
+ if (key_slot) { /* the key's colon, inline */
258
+ if (tape_inline_punct(p, len, &i, ':')) {
259
+ top_expect = JW_VALUE;
260
+ key_slot = 0;
261
+ } else {
262
+ top_expect = JW_COLON;
263
+ }
264
+ } else if (tape_inline_punct(p, len, &i, ',')) {
265
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
266
+ key_slot = top_kind ? 1 : 0;
267
+ } else {
268
+ top_expect = JW_COMMA_OR_CLOSE;
269
+ }
270
+ continue;
271
+ }
272
+ if (c == ']' || c == '}') {
273
+ int want = c == ']' ? 0 : 1;
274
+ if (top_kind != want ||
275
+ (top_expect != JW_VALUE_OR_CLOSE && top_expect != JW_KEY_OR_CLOSE &&
276
+ top_expect != JW_COMMA_OR_CLOSE)) {
277
+ goto reject;
278
+ }
279
+ kinds[count] = YEP_T_CLOSE;
280
+ offs[count] = top_open; /* link to the OPEN */
281
+ lens[count] = 0;
282
+ offs[top_open] = (uint32_t)count; /* the OPEN's link back */
283
+ count++;
284
+ depth--;
285
+ i = at + 1;
286
+ if (depth == 0) {
287
+ break;
288
+ }
289
+ top_kind = kind[depth - 1];
290
+ top_expect = JW_COMMA_OR_CLOSE;
291
+ top_open = open_at[depth - 1];
292
+ key_slot = 0; /* never read before the comma resets it —
293
+ set anyway so the flag has one owner */
294
+ continue;
295
+ }
296
+ if (c == '{' || c == '[') {
297
+ if (depth >= YEP_JSON_WALK_DEPTH) {
298
+ goto reject;
299
+ }
300
+ if (key_slot) {
301
+ goto reject;
302
+ }
303
+ /* spill current top, then push */
304
+ kind[depth - 1] = top_kind;
305
+ open_at[depth - 1] = top_open;
306
+ top_kind = c == '[' ? 0 : 1;
307
+ kinds[count] = c == '[' ? YEP_T_SEQ_OPEN : YEP_T_MAP_OPEN;
308
+ offs[count] = 0; /* the matching CLOSE writes the link */
309
+ lens[count] = 0;
310
+ top_open = (uint32_t)count;
311
+ count++;
312
+ top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
313
+ kind[depth] = top_kind;
314
+ open_at[depth] = top_open;
315
+ depth++;
316
+ i = at + 1;
317
+ continue;
318
+ }
319
+ if (c == 't' || c == 'f' || c == 'n') {
320
+ /* packed literal compare: bounds-checked span first, then
321
+ * one u32 load (false adds its 5th byte) */
322
+ size_t wl = c == 'f' ? 5 : 4;
323
+ if (at + wl > len) {
324
+ goto reject;
325
+ }
326
+ uint32_t got4;
327
+ memcpy(&got4, p + at, 4);
328
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
329
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
330
+ : 0x736C6166u /* "fals" */) ||
331
+ (c == 'f' && p[at + 4] != 'e')) {
332
+ goto reject;
333
+ }
334
+ if (at + wl < len) {
335
+ char z = p[at + wl];
336
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
337
+ z != '}' && z != ':') {
338
+ goto reject;
339
+ }
340
+ }
341
+ kinds[count] = c == 'n' ? YEP_T_NULL : (c == 't' ? YEP_T_TRUE : YEP_T_FALSE);
342
+ offs[count] = (uint32_t)at;
343
+ lens[count] = (uint32_t)wl;
344
+ count++;
345
+ i = at + wl;
346
+ if (tape_inline_punct(p, len, &i, ',')) {
347
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
348
+ key_slot = top_kind ? 1 : 0;
349
+ } else {
350
+ top_expect = JW_COMMA_OR_CLOSE;
351
+ }
352
+ continue;
353
+ }
354
+ goto reject;
355
+ }
356
+ if (check_tail) {
357
+ size_t tail = i;
358
+ while (tail < len &&
359
+ (p[tail] == ' ' || p[tail] == '\t' || p[tail] == '\n' || p[tail] == '\r')) {
360
+ tail++;
361
+ }
362
+ if (tail != len) {
363
+ goto reject;
364
+ }
365
+ }
366
+ if (count > cap) {
367
+ goto mem;
368
+ }
369
+ t->count = count;
370
+ t->_src = p;
371
+ t->_srclen = len;
372
+ return YEPTRIS_OK;
373
+
374
+ mem:
375
+ yeptris_tape_free(t);
376
+ return YEPTRIS_ERROR_MEMORY;
377
+ reject:
378
+ yeptris_tape_free(t);
379
+ return YEPTRIS_ERROR_PARSE;
380
+ }
381
+
382
+ /* The token-contract walk: consumes stage 1's positions — whitespace
383
+ * is never scanned (a one-byte-typical gap check proves the residue
384
+ * since the last token is whitespace), string interiors never appear
385
+ * (yep_json_string stays the authority on closes, escapes, and raw
386
+ * C0), numbers are span records via the shape scan. Same states and
387
+ * rejects as the fused walk; tape-diff pins the equivalence. */
388
+ static YEP_UNUSED_FN YeptrisStatus tape_walk_idx(const char* p, size_t len, size_t open,
389
+ const uint32_t* idx, size_t nidx,
390
+ yeptris_json_tape* t) {
391
+ if (tape_carve(t, len) != YEPTRIS_OK) {
392
+ return YEPTRIS_ERROR_MEMORY;
393
+ }
394
+ uint8_t* kinds = t->kinds;
395
+ uint32_t* offs = t->offs;
396
+ uint32_t* lens = t->lens;
397
+ uint32_t open_at[YEP_JSON_WALK_DEPTH];
398
+ uint8_t kind[YEP_JSON_WALK_DEPTH];
399
+
400
+ size_t count = 0;
401
+ kinds[0] = YEP_T_DOC;
402
+ offs[0] = 0;
403
+ lens[0] = 0;
404
+ count = 1;
405
+
406
+ uint8_t top_kind = p[open] == '[' ? 0 : 1;
407
+ kinds[1] = top_kind ? YEP_T_MAP_OPEN : YEP_T_SEQ_OPEN;
408
+ offs[1] = 0;
409
+ lens[1] = 0;
410
+ uint32_t top_open = 1;
411
+ count = 2;
412
+ uint8_t top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
413
+ /* key_slot as a maintained flag: transitions update it, the loop
414
+ * never recomputes (two compares per token saved) */
415
+ int key_slot = top_kind ? 1 : 0;
416
+ int depth = 1;
417
+ open_at[0] = top_open;
418
+ kind[0] = top_kind;
419
+
420
+ size_t si = 0;
421
+ while (si < nidx && idx[si] <= open) {
422
+ si++; /* the root opener (and anything before it) is consumed */
423
+ }
424
+ size_t prev_end = open + 1;
425
+ for (;;) {
426
+ if (si >= nidx) {
427
+ goto reject; /* EOF mid-structure */
428
+ }
429
+ size_t at = idx[si++];
430
+ if (at >= len) {
431
+ goto reject;
432
+ }
433
+ char c = p[at];
434
+ /* no gap check: every byte class that could hide between
435
+ * tokens is itself a token (dropped scalars are scalar-run
436
+ * starts, raw C0 is a scalar — the state machine rejects
437
+ * them); legal ws is simply invisible. Tabs are ws-class and
438
+ * fall to the document path, exactly like the fused walk. */
439
+ prev_end = at + 1;
440
+
441
+ if (top_expect == JW_COLON) {
442
+ if (c != ':') {
443
+ goto reject;
444
+ }
445
+ top_expect = JW_VALUE;
446
+ key_slot = 0; /* a value follows the colon */
447
+ continue;
448
+ }
449
+ if (top_expect == JW_COMMA_OR_CLOSE) {
450
+ if (c == ',') {
451
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
452
+ key_slot = top_kind ? 1 : 0;
453
+ continue;
454
+ }
455
+ if (c != ']' && c != '}') {
456
+ goto reject;
457
+ }
458
+ } else if (key_slot) {
459
+ /* KEY position: a string or the container's close — the
460
+ * value arms are unreachable, the chain short-circuits */
461
+ if (c != '"' && c != ']' && c != '}') {
462
+ goto reject;
463
+ }
464
+ }
465
+
466
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
467
+ size_t i = at;
468
+ int flt = 0;
469
+ if (!yep_json_number_shape(p, len, &i, &flt)) {
470
+ goto reject;
471
+ }
472
+ kinds[count] = flt ? YEP_T_FLOAT : YEP_T_INT;
473
+ offs[count] = (uint32_t)at;
474
+ lens[count] = (uint32_t)(i - at);
475
+ count++;
476
+ prev_end = i;
477
+ top_expect = JW_COMMA_OR_CLOSE;
478
+ continue;
479
+ }
480
+ if (c == '"') {
481
+ size_t i = at;
482
+ size_t close = 0;
483
+ int esc = 0;
484
+ if (!yep_json_string(p, len, &i, &close, &esc)) {
485
+ goto reject;
486
+ }
487
+ kinds[count] = YEP_T_STR;
488
+ offs[count] = (uint32_t)(at + 1);
489
+ lens[count] = (uint32_t)(close - at - 1);
490
+ count++;
491
+ prev_end = i;
492
+ top_expect = key_slot ? JW_COLON : JW_COMMA_OR_CLOSE;
493
+ /* key_slot stays 1 through COLON — the value arm clears it */
494
+ continue;
495
+ }
496
+ if (c == ']' || c == '}') {
497
+ int want = c == ']' ? 0 : 1;
498
+ if (top_kind != want ||
499
+ (top_expect != JW_VALUE_OR_CLOSE && top_expect != JW_KEY_OR_CLOSE &&
500
+ top_expect != JW_COMMA_OR_CLOSE)) {
501
+ goto reject;
502
+ }
503
+ kinds[count] = YEP_T_CLOSE;
504
+ offs[count] = top_open;
505
+ lens[count] = 0;
506
+ offs[top_open] = (uint32_t)count;
507
+ count++;
508
+ depth--;
509
+ if (depth == 0) {
510
+ break;
511
+ }
512
+ top_kind = kind[depth - 1];
513
+ top_expect = JW_COMMA_OR_CLOSE;
514
+ top_open = open_at[depth - 1];
515
+ key_slot = 0; /* never read before the comma resets it —
516
+ set anyway so the flag has one owner */
517
+ continue;
518
+ }
519
+ if (c == '{' || c == '[') {
520
+ if (depth >= YEP_JSON_WALK_DEPTH) {
521
+ goto reject;
522
+ }
523
+ kind[depth - 1] = top_kind;
524
+ open_at[depth - 1] = top_open;
525
+ top_kind = c == '[' ? 0 : 1;
526
+ kinds[count] = c == '[' ? YEP_T_SEQ_OPEN : YEP_T_MAP_OPEN;
527
+ offs[count] = 0;
528
+ lens[count] = 0;
529
+ top_open = (uint32_t)count;
530
+ count++;
531
+ top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
532
+ key_slot = top_kind ? 1 : 0;
533
+ kind[depth] = top_kind;
534
+ open_at[depth] = top_open;
535
+ depth++;
536
+ continue;
537
+ }
538
+ if (c == 't' || c == 'f' || c == 'n') {
539
+ size_t wl = c == 'f' ? 5 : 4;
540
+ if (at + wl > len) {
541
+ goto reject;
542
+ }
543
+ uint32_t got4;
544
+ memcpy(&got4, p + at, 4);
545
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
546
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
547
+ : 0x736C6166u /* "fals" */) ||
548
+ (c == 'f' && p[at + 4] != 'e')) {
549
+ goto reject;
550
+ }
551
+ if (at + wl < len) {
552
+ char z = p[at + wl];
553
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
554
+ z != '}' && z != ':') {
555
+ goto reject;
556
+ }
557
+ }
558
+ kinds[count] = c == 'n' ? YEP_T_NULL : (c == 't' ? YEP_T_TRUE : YEP_T_FALSE);
559
+ offs[count] = (uint32_t)at;
560
+ lens[count] = (uint32_t)wl;
561
+ count++;
562
+ prev_end = at + wl;
563
+ top_expect = JW_COMMA_OR_CLOSE;
564
+ key_slot = 0;
565
+ continue;
566
+ }
567
+ goto reject;
568
+ }
569
+ /* trailing whitespace only */
570
+ while (prev_end < len && (p[prev_end] == ' ' || p[prev_end] == '\t' || p[prev_end] == '\n' ||
571
+ p[prev_end] == '\r')) {
572
+ prev_end++;
573
+ }
574
+ if (prev_end != len) {
575
+ goto reject;
576
+ }
577
+ if (count > len + 2) {
578
+ goto mem;
579
+ }
580
+ t->count = count;
581
+ t->_src = p;
582
+ t->_srclen = len;
583
+ return YEPTRIS_OK;
584
+
585
+ mem:
586
+ yeptris_tape_free(t);
587
+ return YEPTRIS_ERROR_MEMORY;
588
+ reject:
589
+ yeptris_tape_free(t);
590
+ return YEPTRIS_ERROR_PARSE;
591
+ }
592
+
593
+ /* The LENIENT token walk (the simdjson deferred contract): stage-1
594
+ * positions consumed with the grammar arms removed. Structural
595
+ * alternation mirrors tape_walk_idx state for state — bracket
596
+ * matching, key/value placement, literals (packed compares), and
597
+ * string closes/escapes (yep_json_string) all stay parse-time
598
+ * checks; NUMBER grammar does not — the arm records the scalar-run
599
+ * span (start to the next token's position, trailing ws trimmed)
600
+ * as YEP_T_NUM and yeptris_tape_convert owns validation and the
601
+ * int/float split. A malformed number ("12ab") therefore rejects at
602
+ * drain, not parse. */
603
+ /* The fused LENIENT walk: the strict walk's byte loop with the number
604
+ * grammar deferred — the arm records the run to its natural delimiter
605
+ * (the classify scan carries no accept/reject structure) and
606
+ * yeptris_tape_convert owns validation. Everything else matches
607
+ * tape_walk state for state. */
608
+ /* LNUM_RUN — the initial digit run of a number span: SWAR words, eight
609
+ * bytes per pass, with the byte loop below as tail and classifier.
610
+ * digit iff 0x30-0x39: a lane flags when (c+0x46) carries into bit7
611
+ * (c > '9'), when ((c|0x80)-0xB0) goes negative (c < '0'), or when the
612
+ * byte is non-ASCII — and since the all-digit prefix never borrows
613
+ * across lanes, the flags are exact up to the first non-digit, where
614
+ * the scan stops and the byte loop classifies what follows (the
615
+ * consumed-only digits_only law is untouched: the run sets saw_digit,
616
+ * nothing else). */
617
+ #define LNUM_RUN \
618
+ do { \
619
+ size_t ks_ = k; \
620
+ while (k + 8 <= len) { \
621
+ uint64_t w_; \
622
+ memcpy(&w_, p + k, 8); \
623
+ uint64_t nd_ = \
624
+ ((w_ + 0x4646464646464646ull) & 0x8080808080808080ull) | \
625
+ (((w_ | 0x8080808080808080ull) - 0xB0B0B0B0B0B0B0B0ull) & 0x8080808080808080ull) | \
626
+ (w_ & 0x8080808080808080ull); \
627
+ if (nd_ != 0) { \
628
+ k += (size_t)yep_ctz64(nd_) >> 3; \
629
+ break; \
630
+ } \
631
+ k += 8; \
632
+ } \
633
+ if (k > ks_) { \
634
+ saw_digit = 1; \
635
+ } \
636
+ } while (0)
637
+
638
+ /* The strict RFC 8259 number check over one number-ish run — the
639
+ * in-walk float/exp validator (the out-of-line number_shape call +
640
+ * rescan was 13% of json-doc; the digits-only case never gets here).
641
+ * One pass, no state machine words: sign, digits (leading-zero rule),
642
+ * optional .digits, optional e[+-]digits, consume exactly len.
643
+ * always_inline: the spans are 3-8 bytes and the out-of-line call was
644
+ * the cost (the rescan itself is nothing). */
645
+ #if defined(__GNUC__)
646
+ #define TAPE_NUM_OK_INLINE __attribute__((always_inline)) inline
647
+ #else
648
+ #define TAPE_NUM_OK_INLINE
649
+ #endif
650
+ static TAPE_NUM_OK_INLINE int tape_num_ok(const char* p, size_t len) {
651
+ size_t k = 0;
652
+ if (k < len && p[k] == '-') {
653
+ k++;
654
+ }
655
+ size_t int_start = k;
656
+ while (k < len && (unsigned)(unsigned char)p[k] - '0' <= 9u) {
657
+ k++;
658
+ }
659
+ if (k == int_start) {
660
+ return 0; /* no integer part */
661
+ }
662
+ if (k - int_start > 1 && p[int_start] == '0') {
663
+ return 0; /* leading zero */
664
+ }
665
+ if (k < len && p[k] == '.') {
666
+ k++;
667
+ size_t frac = k;
668
+ while (k < len && (unsigned)(unsigned char)p[k] - '0' <= 9u) {
669
+ k++;
670
+ }
671
+ if (k == frac) {
672
+ return 0;
673
+ }
674
+ }
675
+ if (k < len && (p[k] == 'e' || p[k] == 'E')) {
676
+ k++;
677
+ if (k < len && (p[k] == '+' || p[k] == '-')) {
678
+ k++;
679
+ }
680
+ size_t exp = k;
681
+ while (k < len && (unsigned)(unsigned char)p[k] - '0' <= 9u) {
682
+ k++;
683
+ }
684
+ if (k == exp) {
685
+ return 0;
686
+ }
687
+ }
688
+ return k == len;
689
+ }
690
+
691
+ /* The >1024 simple-key law for the LENIENT walk's tape — the same
692
+ * verdict yt_flow_scan reads for the record walk: a STR at a map's
693
+ * key slot with a raw span past the limit (the law counts the RAW
694
+ * token, so a quoted key's two quotes add back). The DOM route must
695
+ * reject it BEFORE wrapping (strict JSON itself has no limit — the
696
+ * tape API keeps accepting; only the YAML serialization cannot carry
697
+ * a long simple key, so the fast parse defers to the engine's clean
698
+ * error — the nightly's run 38143486175). */
699
+ int yep_tape_long_simple_key(const yeptris_json_tape* s) {
700
+ if (s->_long_key_known) {
701
+ return s->_long_key; /* settled during the lenient walk */
702
+ }
703
+ struct {
704
+ uint8_t map;
705
+ uint8_t key_next;
706
+ } st[257];
707
+ int d = 0;
708
+ st[0].map = 0;
709
+ st[0].key_next = 0;
710
+ for (uint32_t i = 1; i < s->count; i++) { /* recs[0] is the DOC */
711
+ uint64_t r = s->recs[i];
712
+ uint8_t kind = (uint8_t)(r & 0xFFu);
713
+ if (kind == YEP_T_MAP_OPEN || kind == YEP_T_SEQ_OPEN) {
714
+ if (d >= 256) {
715
+ return 1; /* deeper than the scratch stack: over by
716
+ * definition, the engine's depth law decides */
717
+ }
718
+ /* the container consumes the parent frame's pending slot;
719
+ * after it closes the OTHER slot is next (yt_flow_scan's
720
+ * round-108 lesson: a pinned key_next hid every key that
721
+ * followed a container value) */
722
+ if (d > 0 && st[d].map) {
723
+ st[d].key_next = (uint8_t)!st[d].key_next;
724
+ }
725
+ d++;
726
+ st[d].map = (uint8_t)(kind == YEP_T_MAP_OPEN);
727
+ st[d].key_next = 0;
728
+ continue;
729
+ }
730
+ if (kind == YEP_T_CLOSE) {
731
+ if (d > 0) {
732
+ d--;
733
+ }
734
+ continue;
735
+ }
736
+ if (st[d].map && st[d].key_next == 0) {
737
+ uint32_t mlen = (uint32_t)(r >> 8) & 0xFFFFFFu;
738
+ if (kind == YEP_T_STR && mlen + 2u > 1024u &&
739
+ yep_simple_key_chars((const char*)s->_src + (size_t)((uint32_t)(r >> 32)), mlen) +
740
+ 2 >
741
+ 1024) {
742
+ /* the budget counts characters, not bytes (the round-108
743
+ * shrink: a 2-byte code point at byte delta 1025 is
744
+ * legal); _src rides the walk that produced the tape */
745
+ return 1;
746
+ }
747
+ st[d].key_next = 1;
748
+ } else if (st[d].map) {
749
+ st[d].key_next = 0;
750
+ }
751
+ }
752
+ return 0;
753
+ }
754
+
755
+ YeptrisStatus yep_tape_walk_lenient_fused(const char* p, size_t len, size_t open,
756
+ yeptris_json_tape* t, int strict_nums, int clean_only,
757
+ int reuse_block) {
758
+ /* reuse_block: the caller owns the struct and zeroed it (the flow
759
+ * recorder's scratch) — a prior walk's block may be reused through
760
+ * the _srclen watermark. Consumer structs carry untrustworthy
761
+ * fields and always carve fresh. */
762
+ if (reuse_block && t->_block != NULL && t->recs != NULL && t->_rec_primary &&
763
+ t->_srclen >= len + 2) {
764
+ t->_cols_ready = 0;
765
+ t->_rec_primary = 1;
766
+ t->_long_key_known = 0;
767
+ t->_long_key = 0;
768
+ } else if (tape_carve(t, len) != YEPTRIS_OK) {
769
+ return YEPTRIS_ERROR_MEMORY;
770
+ }
771
+ t->_rec_primary = 1;
772
+ /* the interleaved records are the primary storage (item 07); the
773
+ * columns materialize lazily via yeptris_tape_columns */
774
+ t->_rec_primary = 1;
775
+ yeptris_tape_rec* recs = t->recs;
776
+ uint32_t open_at[YEP_JSON_WALK_DEPTH];
777
+ uint8_t kind[YEP_JSON_WALK_DEPTH];
778
+
779
+ recs[0] = ((uint64_t)0 << 32) | ((uint64_t)0 << 8) | YEP_T_DOC;
780
+
781
+ uint8_t top_kind = p[open] == '[' ? 0 : 1;
782
+ recs[1] =
783
+ ((uint64_t)0 << 32) | ((uint64_t)0 << 8) | (top_kind ? YEP_T_MAP_OPEN : YEP_T_SEQ_OPEN);
784
+ size_t count = 2;
785
+ int depth = 1;
786
+ open_at[0] = 1;
787
+ kind[0] = top_kind;
788
+
789
+ size_t i = open + 1;
790
+ /* clean_only (parse_json's route): tabs and non-ASCII bytes REJECT
791
+ * — the caller falls to the strict sequence, which owns the pinned
792
+ * error precedences those inputs carry (the tab reject, the UTF-8
793
+ * gate). This replaces the entry's full-input gate_scan +
794
+ * tab-memchr pre-passes: same routing by construction, zero scans
795
+ * before the walk. */
796
+ int cl = clean_only;
797
+ if (top_kind) {
798
+ goto lmap1;
799
+ }
800
+ goto lseq1;
801
+
802
+ /* strict_nums: the parse_json route's contract is RFC 8259 AT
803
+ * parse time, so its number arms validate the run in-place (the
804
+ * settle pass disappears — the span is cache-warm here); the
805
+ * standalone lenient entry keeps its deferred contract (0: the
806
+ * run records unvalidated, yeptris_tape_convert owns it). */
807
+
808
+ /* The specialized member loops (the #342 dispatch-chain cut): once
809
+ * inside a container the grammar is a 2-state cycle — member or
810
+ * separator — so the general chain's expect/key_slot machine and
811
+ * its per-token re-dispatch are replaced by direct loops per kind.
812
+ * Acceptance is IDENTICAL to the chain (the pinning suites hold:
813
+ * Tape suite, LenientMatchesStrict, ErrorParity, the json corpora).
814
+ *
815
+ * lmap / lseq: positioned at a member (or the container's closer).
816
+ * lvalue: scans one value (string/number/literal/container).
817
+ * lafter: positioned after a complete value — ',' or the closer.
818
+ * A close pops the frame; depth 0 ends the walk. */
819
+
820
+ #define LWS() \
821
+ do { \
822
+ while (i < len && \
823
+ (p[i] == ' ' || p[i] == '\n' || p[i] == '\r' || (p[i] == '\t' && !cl))) { \
824
+ i++; \
825
+ } \
826
+ if (i >= len) { \
827
+ goto lreject; \
828
+ } \
829
+ } while (0)
830
+
831
+ #define LPUSH(k) \
832
+ do { \
833
+ if (depth >= YEP_JSON_WALK_DEPTH) { \
834
+ goto lreject; \
835
+ } \
836
+ recs[count] = ((uint64_t)0 << 32) | ((uint64_t)0 << 8) | \
837
+ (uint64_t)((k) ? YEP_T_MAP_OPEN : YEP_T_SEQ_OPEN); \
838
+ kind[depth] = (k); \
839
+ open_at[depth] = (uint32_t)count; \
840
+ count++; \
841
+ depth++; \
842
+ i++; \
843
+ } while (0)
844
+
845
+ #define LCLOSE() \
846
+ do { \
847
+ uint32_t lo_ = open_at[depth - 1]; \
848
+ recs[count] = ((uint64_t)lo_ << 32) | ((uint64_t)0 << 8) | YEP_T_CLOSE; \
849
+ recs[lo_] = \
850
+ (recs[lo_] & ~(uint64_t)0xFFFFFFFF00000000u) | ((uint64_t)(uint32_t)count << 32); \
851
+ count++; \
852
+ i++; \
853
+ depth--; \
854
+ if (depth == 0) { \
855
+ goto ldone; \
856
+ } \
857
+ if (kind[depth - 1]) { \
858
+ goto lmapafter; \
859
+ } \
860
+ goto lseqafter; \
861
+ } while (0)
862
+
863
+ /* LSTR_SCAN(after) — the fused string arm, positioned at p[i] == '"'.
864
+ * One SWAR round settles the common short span; quote_scan (the SIMD
865
+ * close scan) settles longer no-escape spans, with a c0 sweep over the
866
+ * content (quote_scan never tests control bytes); the kernel runs only
867
+ * when escapes appear — its grammar authority covers the escape rules
868
+ * quote_scan skips. `after` names the post-member label. */
869
+ #define LSTR_SCAN(after) \
870
+ do { \
871
+ size_t j_ = i + 1; \
872
+ int esc_ = 0; \
873
+ if (j_ + 8 <= len) { \
874
+ uint64_t w_; \
875
+ memcpy(&w_, p + j_, 8); \
876
+ uint64_t qm_ = (w_ ^ 0x2222222222222222ull); \
877
+ uint64_t bm_ = (w_ ^ 0x5C5C5C5C5C5C5C5Cull); \
878
+ qm_ = (qm_ - 0x0101010101010101ull) & ~qm_ & 0x8080808080808080ull; \
879
+ bm_ = (bm_ - 0x0101010101010101ull) & ~bm_ & 0x8080808080808080ull; \
880
+ uint64_t c0_ = (w_ - 0x2020202020202020ull) & ~w_ & 0x8080808080808080ull; \
881
+ uint64_t hi_ = cl ? (w_ & 0x8080808080808080ull) : 0; \
882
+ if (qm_ != 0 && ((bm_ | c0_ | hi_) & (qm_ - 1)) == 0) { \
883
+ size_t cl_ = j_ + (size_t)yep_ctz64(qm_) / 8; \
884
+ recs[count] = ((uint64_t)((uint32_t)j_) << 32) | \
885
+ ((uint64_t)((uint32_t)(cl_ - j_)) << 8) | (uint64_t)(YEP_T_STR); \
886
+ count++; \
887
+ i = cl_ + 1; \
888
+ goto after; \
889
+ } \
890
+ /* rounds 2-4 (up to 32 bytes inline): the email-class strings \
891
+ * close here without the quote_scan call and its redundant \
892
+ * content re-sweep; reached only when round 1 had no close, \
893
+ * so the short-string case pays nothing */ \
894
+ if ((qm_ | bm_ | c0_ | hi_) == 0) { \
895
+ size_t nr_ = (len - j_) >> 3; \
896
+ if (nr_ > 4) { \
897
+ nr_ = 4; \
898
+ } \
899
+ for (size_t r_ = 1; r_ < nr_; r_++) { \
900
+ size_t b2_ = j_ + (r_ << 3); \
901
+ memcpy(&w_, p + b2_, 8); \
902
+ qm_ = (w_ ^ 0x2222222222222222ull); \
903
+ bm_ = (w_ ^ 0x5C5C5C5C5C5C5C5Cull); \
904
+ qm_ = (qm_ - 0x0101010101010101ull) & ~qm_ & 0x8080808080808080ull; \
905
+ bm_ = (bm_ - 0x0101010101010101ull) & ~bm_ & 0x8080808080808080ull; \
906
+ c0_ = (w_ - 0x2020202020202020ull) & ~w_ & 0x8080808080808080ull; \
907
+ hi_ = cl ? (w_ & 0x8080808080808080ull) : 0; \
908
+ if (qm_ != 0 && ((bm_ | c0_ | hi_) & (qm_ - 1)) == 0) { \
909
+ size_t cl_ = b2_ + (size_t)yep_ctz64(qm_) / 8; \
910
+ recs[count] = ((uint64_t)((uint32_t)j_) << 32) | \
911
+ ((uint64_t)((uint32_t)(cl_ - j_)) << 8) | \
912
+ (uint64_t)(YEP_T_STR); \
913
+ count++; \
914
+ i = cl_ + 1; \
915
+ goto after; \
916
+ } \
917
+ if ((qm_ | bm_ | c0_ | hi_) != 0) { \
918
+ break; /* escape, c0 or hi: kernel authority */ \
919
+ } \
920
+ } \
921
+ } \
922
+ } \
923
+ { \
924
+ const yep_text_kernels* kk_ = yep_text_active(); \
925
+ ptrdiff_t r_ = kk_->quote_scan(p + j_, len - j_, '"', &esc_); \
926
+ if (r_ >= 0 && esc_ == 0) { \
927
+ size_t cl_ = j_ + (size_t)r_; \
928
+ for (size_t b_ = j_; b_ < cl_;) { \
929
+ size_t room_ = cl_ - b_; \
930
+ if (room_ >= 8) { \
931
+ uint64_t v_; \
932
+ memcpy(&v_, p + b_, 8); \
933
+ uint64_t bad_ = \
934
+ (v_ - 0x2020202020202020ull) & ~v_ & 0x8080808080808080ull; \
935
+ if (cl) { \
936
+ bad_ |= v_ & 0x8080808080808080ull; \
937
+ } \
938
+ if (bad_) { \
939
+ goto lreject; \
940
+ } \
941
+ b_ += 8; \
942
+ } else { \
943
+ if ((unsigned char)p[b_] < 0x20 || (cl && (unsigned char)p[b_] >= 0x80)) { \
944
+ goto lreject; \
945
+ } \
946
+ b_++; \
947
+ } \
948
+ } \
949
+ recs[count] = ((uint64_t)((uint32_t)j_) << 32) | \
950
+ ((uint64_t)((uint32_t)(cl_ - j_)) << 8) | (uint64_t)(YEP_T_STR); \
951
+ count++; \
952
+ i = cl_ + 1; \
953
+ goto after; \
954
+ } \
955
+ } \
956
+ { \
957
+ size_t at_ = i; \
958
+ size_t cl_ = 0; \
959
+ if (!yep_json_string(p, len, &i, &cl_, &esc_)) { \
960
+ goto lreject; \
961
+ } \
962
+ if (cl) { /* escaped strings skip the sweeps above: the \
963
+ * clean route still owes the strict sequence's \
964
+ * byte guarantees over the content */ \
965
+ for (size_t b_ = at_ + 1; b_ < cl_; b_++) { \
966
+ unsigned char uc_ = (unsigned char)p[b_]; \
967
+ if (uc_ < 0x20 || uc_ >= 0x80) { \
968
+ goto lreject; \
969
+ } \
970
+ } \
971
+ } \
972
+ recs[count] = ((uint64_t)((uint32_t)(at_ + 1)) << 32) | \
973
+ ((uint64_t)((uint32_t)(cl_ - at_ - 1)) << 8) | (uint64_t)(YEP_T_STR); \
974
+ count++; \
975
+ i = cl_ + 1; \
976
+ } \
977
+ goto after; \
978
+ } while (0)
979
+
980
+ /* The map member cycle — ONE contiguous region (the json-doc lesson:
981
+ * a goto web across value/key/after regions scattered the hot path and
982
+ * cost 2.7x on the flat-map shape). Values scan inline; only nested
983
+ * containers, literals, and the kernel string fallback leave the
984
+ * cycle. Acceptance is the chain's: no trailing commas, string keys
985
+ * only, `:` required, closers only in first-member position. */
986
+ lmap1: /* {} closes here */
987
+ LWS();
988
+ if (p[i] == '}') {
989
+ LCLOSE();
990
+ }
991
+ if (p[i] != '"') {
992
+ goto lreject;
993
+ }
994
+ goto lmapkey;
995
+
996
+ lmap: /* a member MUST follow (after ','): JW_KEY — a closer rejects */
997
+ /* compact fast path: the byte is the key's quote already — LWS's
998
+ * loop would examine it once and exit */
999
+ if (i < len && p[i] == '"') {
1000
+ goto lmapkey;
1001
+ }
1002
+ LWS();
1003
+ if (p[i] != '"') {
1004
+ goto lreject;
1005
+ }
1006
+ lmapkey: { LSTR_SCAN(lmapcolon); }
1007
+ lmapcolon: { /* the key verdict rides the walk: the just-emitted record IS the
1008
+ * key — a >1024-byte candidate pays the char-budget check once
1009
+ * instead of a post-parse full-tape re-walk (#739) */
1010
+ uint64_t kr_ = recs[count - 1];
1011
+ uint32_t kl_ = (uint32_t)(kr_ >> 8) & 0xFFFFFFu;
1012
+ if (kl_ + 2u > 1024u && !t->_long_key_known) {
1013
+ t->_long_key_known = 1;
1014
+ t->_long_key =
1015
+ (unsigned char)(yep_simple_key_chars(p + (size_t)(uint32_t)(kr_ >> 32), kl_) + 2 >
1016
+ 1024);
1017
+ }
1018
+ }
1019
+ if (i < len && p[i] == ':') {
1020
+ i++;
1021
+ /* compact fast path: the value byte follows the colon with no
1022
+ * whitespace — one load dispatches it (the ws test below is
1023
+ * what LWS would run anyway) */
1024
+ if (i < len) {
1025
+ char v = p[i];
1026
+ if (v != ' ' && v != '\n' && v != '\r' && (cl || v != '\t')) {
1027
+ goto lmapval;
1028
+ }
1029
+ }
1030
+ LWS();
1031
+ goto lmapval;
1032
+ }
1033
+ LWS();
1034
+ if (p[i] != ':') {
1035
+ goto lreject;
1036
+ }
1037
+ i++;
1038
+ LWS();
1039
+ lmapval: {
1040
+ char c = p[i];
1041
+ if (c == '"') {
1042
+ LSTR_SCAN(lmapafter);
1043
+ }
1044
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
1045
+ size_t k = i + 1;
1046
+ /* the digits-only tracking makes the pure-integer case a
1047
+ * 3-cycle leading-zero check — number_shape (a call + a
1048
+ * rescan, 13% of json-doc) runs only for dot/exp spans */
1049
+ int digits_only = 1, saw_digit = (c != '-');
1050
+ LNUM_RUN;
1051
+ while (k < len) {
1052
+ char d = p[k];
1053
+ if ((unsigned)(d - '0') <= 9u) {
1054
+ k++;
1055
+ saw_digit = 1;
1056
+ continue;
1057
+ }
1058
+ /* the flag clears only on a CONSUMED byte: the run's
1059
+ * terminator (',', '}', ']', whitespace) must leave
1060
+ * digits_only intact, or every plain integer followed
1061
+ * by a delimiter falls into the full validator */
1062
+ if (d == '-' || d == '+' || d == '.' || d == 'e' || d == 'E') {
1063
+ digits_only = 0;
1064
+ k++;
1065
+ continue;
1066
+ }
1067
+ break;
1068
+ }
1069
+ if (strict_nums) {
1070
+ int ok;
1071
+ if (digits_only && saw_digit) {
1072
+ size_t d0 = (c == '-') ? i + 1 : i;
1073
+ ok = (k - d0 == 1) || p[d0] != '0';
1074
+ } else {
1075
+ ok = tape_num_ok(p + i, (size_t)(k - i));
1076
+ }
1077
+ if (!ok) {
1078
+ goto lreject;
1079
+ }
1080
+ }
1081
+ recs[count] =
1082
+ ((uint64_t)((uint32_t)i) << 32) | ((uint64_t)((uint32_t)(k - i)) << 8) |
1083
+ (uint64_t)(strict_nums ? ((digits_only && saw_digit) ? YEP_T_INT : YEP_T_FLOAT)
1084
+ : (uint64_t)(YEP_T_NUM));
1085
+ count++;
1086
+ i = k;
1087
+ goto lmapafter;
1088
+ }
1089
+ if (c == 't' || c == 'f' || c == 'n') {
1090
+ size_t wl = c == 'f' ? 5 : 4;
1091
+ if (i + wl > len) {
1092
+ goto lreject;
1093
+ }
1094
+ uint32_t got4;
1095
+ memcpy(&got4, p + i, 4);
1096
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
1097
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
1098
+ : 0x736C6166u /* "fals" */) ||
1099
+ (c == 'f' && p[i + 4] != 'e')) {
1100
+ goto lreject;
1101
+ }
1102
+ if (i + wl < len) {
1103
+ char z = p[i + wl];
1104
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
1105
+ z != '}' && z != ':') {
1106
+ goto lreject;
1107
+ }
1108
+ }
1109
+ recs[count] = ((uint64_t)((uint32_t)i) << 32) | ((uint64_t)((uint32_t)wl) << 8) |
1110
+ (uint64_t)(c == 'n' ? YEP_T_NULL : (c == 't' ? YEP_T_TRUE : YEP_T_FALSE));
1111
+ count++;
1112
+ i += wl;
1113
+ goto lmapafter;
1114
+ }
1115
+ if (c == '{') {
1116
+ LPUSH(1);
1117
+ goto lmap1;
1118
+ }
1119
+ if (c == '[') {
1120
+ LPUSH(0);
1121
+ goto lseq1;
1122
+ }
1123
+ goto lreject;
1124
+ }
1125
+
1126
+ lmapafter:
1127
+ /* compact fast path: value ',' key directly — the two LWS hops the
1128
+ * general form runs both exit on byte one here */
1129
+ if (i < len && p[i] == ',') {
1130
+ i++;
1131
+ if (i < len && p[i] == '"') {
1132
+ goto lmapkey;
1133
+ }
1134
+ goto lmap;
1135
+ }
1136
+ LWS();
1137
+ if (p[i] == ',') {
1138
+ i++;
1139
+ goto lmap;
1140
+ }
1141
+ if (p[i] == '}') {
1142
+ LCLOSE();
1143
+ }
1144
+ goto lreject;
1145
+
1146
+ /* The sequence member cycle — the same single-region shape. */
1147
+ lseq1: /* [] closes here */
1148
+ LWS();
1149
+ if (p[i] == ']') {
1150
+ LCLOSE();
1151
+ }
1152
+ goto lseqval;
1153
+
1154
+ lseq: /* a member MUST follow (after ','): JW_VALUE */
1155
+ LWS();
1156
+ lseqval: {
1157
+ char c = p[i];
1158
+ if (c == '"') {
1159
+ LSTR_SCAN(lseqafter);
1160
+ }
1161
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
1162
+ size_t k = i + 1;
1163
+ int digits_only = 1, saw_digit = (c != '-');
1164
+ LNUM_RUN;
1165
+ while (k < len) {
1166
+ char d = p[k];
1167
+ if ((unsigned)(d - '0') <= 9u) {
1168
+ k++;
1169
+ saw_digit = 1;
1170
+ continue;
1171
+ }
1172
+ /* consumed-only clearing: see the map arm's comment */
1173
+ if (d == '-' || d == '+' || d == '.' || d == 'e' || d == 'E') {
1174
+ digits_only = 0;
1175
+ k++;
1176
+ continue;
1177
+ }
1178
+ break;
1179
+ }
1180
+ if (strict_nums) {
1181
+ int ok;
1182
+ if (digits_only && saw_digit) {
1183
+ size_t d0 = (c == '-') ? i + 1 : i;
1184
+ ok = (k - d0 == 1) || p[d0] != '0';
1185
+ } else {
1186
+ ok = tape_num_ok(p + i, (size_t)(k - i));
1187
+ }
1188
+ if (!ok) {
1189
+ goto lreject;
1190
+ }
1191
+ }
1192
+ recs[count] =
1193
+ ((uint64_t)((uint32_t)i) << 32) | ((uint64_t)((uint32_t)(k - i)) << 8) |
1194
+ (uint64_t)(strict_nums ? ((digits_only && saw_digit) ? YEP_T_INT : YEP_T_FLOAT)
1195
+ : (uint64_t)(YEP_T_NUM));
1196
+ count++;
1197
+ i = k;
1198
+ goto lseqafter;
1199
+ }
1200
+ if (c == 't' || c == 'f' || c == 'n') {
1201
+ size_t wl = c == 'f' ? 5 : 4;
1202
+ if (i + wl > len) {
1203
+ goto lreject;
1204
+ }
1205
+ uint32_t got4;
1206
+ memcpy(&got4, p + i, 4);
1207
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
1208
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
1209
+ : 0x736C6166u /* "fals" */) ||
1210
+ (c == 'f' && p[i + 4] != 'e')) {
1211
+ goto lreject;
1212
+ }
1213
+ if (i + wl < len) {
1214
+ char z = p[i + wl];
1215
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
1216
+ z != '}' && z != ':') {
1217
+ goto lreject;
1218
+ }
1219
+ }
1220
+ recs[count] = ((uint64_t)((uint32_t)i) << 32) | ((uint64_t)((uint32_t)wl) << 8) |
1221
+ (uint64_t)(c == 'n' ? YEP_T_NULL : (c == 't' ? YEP_T_TRUE : YEP_T_FALSE));
1222
+ count++;
1223
+ i += wl;
1224
+ goto lseqafter;
1225
+ }
1226
+ if (c == '{') {
1227
+ LPUSH(1);
1228
+ goto lmap1;
1229
+ }
1230
+ if (c == '[') {
1231
+ LPUSH(0);
1232
+ goto lseq1;
1233
+ }
1234
+ goto lreject;
1235
+ }
1236
+
1237
+ lseqafter:
1238
+ /* compact fast path: value ',' value directly — both LWS hops exit
1239
+ * on byte one here */
1240
+ if (i < len && p[i] == ',') {
1241
+ i++;
1242
+ if (i < len) {
1243
+ char v = p[i];
1244
+ if (v != ' ' && v != '\n' && v != '\r' && (cl || v != '\t')) {
1245
+ goto lseqval;
1246
+ }
1247
+ }
1248
+ goto lseq;
1249
+ }
1250
+ LWS();
1251
+ if (p[i] == ',') {
1252
+ i++;
1253
+ goto lseq;
1254
+ }
1255
+ if (p[i] == ']') {
1256
+ LCLOSE();
1257
+ }
1258
+ goto lreject;
1259
+
1260
+ ldone:
1261
+ #undef LWS
1262
+ #undef LNUM_RUN
1263
+ #undef LPUSH
1264
+ #undef LCLOSE
1265
+ #undef LSTR_SCAN
1266
+
1267
+ {
1268
+ size_t tail = i;
1269
+ while (tail < len &&
1270
+ (p[tail] == ' ' || p[tail] == '\n' || p[tail] == '\r' || (p[tail] == '\t' && !cl))) {
1271
+ tail++;
1272
+ }
1273
+ if (tail != len) {
1274
+ goto lreject;
1275
+ }
1276
+ }
1277
+ /* a col-0 tab-led continuation line inside the span is the suite's
1278
+ * Y79Y#4 error: defer to the engine, whose e_flow_ws owns the law
1279
+ * -- the walk's whitespace loops treat the tab as separation, which
1280
+ * accepted exactly the span the marshal drain's engine rejected,
1281
+ * splitting parseable from marshalable (the nightly's round 120).
1282
+ * clean_only skips the scan: its byte guarantees already rejected
1283
+ * every tab in the span (LWS stops on one, the string c0 sweeps
1284
+ * and the tail reject it), so the pair cannot exist there. The
1285
+ * lenient lane rides memchr — a '\n'-hit vector pass instead of a
1286
+ * two-compare byte loop over the whole span (13% of json-doc). */
1287
+ if (!cl) {
1288
+ size_t k = open + 1;
1289
+ while (k < len) {
1290
+ const char* nl = (const char*)memchr(p + k, '\n', len - k);
1291
+ if (nl == NULL) {
1292
+ break;
1293
+ }
1294
+ k = (size_t)(nl - p) + 1;
1295
+ if (k < len && p[k] == '\t') {
1296
+ yeptris_tape_free(t);
1297
+ return YEPTRIS_ERROR_PARSE;
1298
+ }
1299
+ }
1300
+ }
1301
+ /* the >1024 simple-key verdict settles HERE when no key crossed the
1302
+ * raw cap in lmapcolon: chars <= bytes closes the char budget too,
1303
+ * so no key can violate the law — without this, every clean
1304
+ * document paid yep_tape_long_simple_key's full-tape re-walk (16%
1305
+ * of json-doc parse, the #739 residual) */
1306
+ t->_long_key_known = 1;
1307
+ t->count = count;
1308
+ t->_src = p;
1309
+ t->_srclen = len;
1310
+ return YEPTRIS_OK;
1311
+
1312
+ lreject:
1313
+ yeptris_tape_free(t);
1314
+ return YEPTRIS_ERROR_PARSE;
1315
+ }
1316
+
1317
+ #define LNUM_RUN \
1318
+ do { \
1319
+ size_t ks_ = k; \
1320
+ while (k + 8 <= len) { \
1321
+ uint64_t w_; \
1322
+ memcpy(&w_, p + k, 8); \
1323
+ uint64_t nd_ = \
1324
+ ((w_ + 0x4646464646464646ull) & 0x8080808080808080ull) | \
1325
+ (((w_ | 0x8080808080808080ull) - 0xB0B0B0B0B0B0B0B0ull) & 0x8080808080808080ull) | \
1326
+ (w_ & 0x8080808080808080ull); \
1327
+ if (nd_ != 0) { \
1328
+ k += (size_t)yep_ctz64(nd_) >> 3; \
1329
+ break; \
1330
+ } \
1331
+ k += 8; \
1332
+ } \
1333
+ if (k > ks_) { \
1334
+ saw_digit = 1; \
1335
+ } \
1336
+ } while (0)
1337
+ static YeptrisStatus yep_tape_walk_columns_fused(const char* p, size_t len, size_t open,
1338
+ yeptris_json_tape* t, int strict_nums,
1339
+ int clean_only) {
1340
+ /* The columns-primary edition of the record walk below: the same
1341
+ * arms (specialized member cycles, LSTR_SCAN, the digits law)
1342
+ * emitting the three columns directly — one pass, no records, no
1343
+ * materialization pass. The strict route's columns-eager contract
1344
+ * at the lenient lane's memory traffic. The record walk stays
1345
+ * byte-identical beside it; the differential suites pin the
1346
+ * equivalence. */
1347
+ if (tape_carve(t, len) != YEPTRIS_OK) {
1348
+ return YEPTRIS_ERROR_MEMORY;
1349
+ }
1350
+ uint8_t* kinds = t->kinds;
1351
+ uint32_t* offs = t->offs;
1352
+ uint32_t* lens = t->lens;
1353
+ uint32_t open_at[YEP_JSON_WALK_DEPTH];
1354
+ uint8_t kind[YEP_JSON_WALK_DEPTH];
1355
+
1356
+ kinds[0] = (uint8_t)YEP_T_DOC;
1357
+ offs[0] = 0;
1358
+ lens[0] = 0;
1359
+
1360
+ uint8_t top_kind = p[open] == '[' ? 0 : 1;
1361
+ kinds[1] = top_kind ? (uint8_t)YEP_T_MAP_OPEN : (uint8_t)YEP_T_SEQ_OPEN;
1362
+ offs[1] = 0;
1363
+ lens[1] = 0;
1364
+ size_t count = 2;
1365
+ int depth = 1;
1366
+ open_at[0] = 1;
1367
+ kind[0] = top_kind;
1368
+
1369
+ size_t i = open + 1;
1370
+ /* clean_only (parse_json's route): tabs and non-ASCII bytes REJECT
1371
+ * — the caller falls to the strict sequence, which owns the pinned
1372
+ * error precedences those inputs carry (the tab reject, the UTF-8
1373
+ * gate). This replaces the entry's full-input gate_scan +
1374
+ * tab-memchr pre-passes: same routing by construction, zero scans
1375
+ * before the walk. */
1376
+ int cl = clean_only;
1377
+ if (top_kind) {
1378
+ goto lmap1;
1379
+ }
1380
+ goto lseq1;
1381
+
1382
+ /* strict_nums: the parse_json route's contract is RFC 8259 AT
1383
+ * parse time, so its number arms validate the run in-place (the
1384
+ * settle pass disappears — the span is cache-warm here); the
1385
+ * standalone lenient entry keeps its deferred contract (0: the
1386
+ * run records unvalidated, yeptris_tape_convert owns it). */
1387
+
1388
+ /* The specialized member loops (the #342 dispatch-chain cut): once
1389
+ * inside a container the grammar is a 2-state cycle — member or
1390
+ * separator — so the general chain's expect/key_slot machine and
1391
+ * its per-token re-dispatch are replaced by direct loops per kind.
1392
+ * Acceptance is IDENTICAL to the chain (the pinning suites hold:
1393
+ * Tape suite, LenientMatchesStrict, ErrorParity, the json corpora).
1394
+ *
1395
+ * lmap / lseq: positioned at a member (or the container's closer).
1396
+ * lvalue: scans one value (string/number/literal/container).
1397
+ * lafter: positioned after a complete value — ',' or the closer.
1398
+ * A close pops the frame; depth 0 ends the walk. */
1399
+
1400
+ #define LWS() \
1401
+ do { \
1402
+ while (i < len && \
1403
+ (p[i] == ' ' || p[i] == '\n' || p[i] == '\r' || (p[i] == '\t' && !cl))) { \
1404
+ i++; \
1405
+ } \
1406
+ if (i >= len) { \
1407
+ goto lreject; \
1408
+ } \
1409
+ } while (0)
1410
+
1411
+ #define LPUSH(k) \
1412
+ do { \
1413
+ if (depth >= YEP_JSON_WALK_DEPTH) { \
1414
+ goto lreject; \
1415
+ } \
1416
+ kinds[count] = (k) ? (uint8_t)YEP_T_MAP_OPEN : (uint8_t)YEP_T_SEQ_OPEN; \
1417
+ offs[count] = 0; \
1418
+ lens[count] = 0; \
1419
+ kind[depth] = (k); \
1420
+ open_at[depth] = (uint32_t)count; \
1421
+ count++; \
1422
+ depth++; \
1423
+ i++; \
1424
+ } while (0)
1425
+
1426
+ #define LCLOSE() \
1427
+ do { \
1428
+ uint32_t lo_ = open_at[depth - 1]; \
1429
+ kinds[count] = (uint8_t)YEP_T_CLOSE; \
1430
+ offs[count] = lo_; \
1431
+ lens[count] = 0; \
1432
+ offs[lo_] = (uint32_t)count; \
1433
+ count++; \
1434
+ i++; \
1435
+ depth--; \
1436
+ if (depth == 0) { \
1437
+ goto ldone; \
1438
+ } \
1439
+ if (kind[depth - 1]) { \
1440
+ goto lmapafter; \
1441
+ } \
1442
+ goto lseqafter; \
1443
+ } while (0)
1444
+
1445
+ /* LSTR_SCAN(after) — the fused string arm, positioned at p[i] == '"'.
1446
+ * One SWAR round settles the common short span; quote_scan (the SIMD
1447
+ * close scan) settles longer no-escape spans, with a c0 sweep over the
1448
+ * content (quote_scan never tests control bytes); the kernel runs only
1449
+ * when escapes appear — its grammar authority covers the escape rules
1450
+ * quote_scan skips. `after` names the post-member label. */
1451
+ #define LSTR_SCAN(after) \
1452
+ do { \
1453
+ size_t j_ = i + 1; \
1454
+ int esc_ = 0; \
1455
+ if (j_ + 8 <= len) { \
1456
+ uint64_t w_; \
1457
+ memcpy(&w_, p + j_, 8); \
1458
+ uint64_t qm_ = (w_ ^ 0x2222222222222222ull); \
1459
+ uint64_t bm_ = (w_ ^ 0x5C5C5C5C5C5C5C5Cull); \
1460
+ qm_ = (qm_ - 0x0101010101010101ull) & ~qm_ & 0x8080808080808080ull; \
1461
+ bm_ = (bm_ - 0x0101010101010101ull) & ~bm_ & 0x8080808080808080ull; \
1462
+ uint64_t c0_ = (w_ - 0x2020202020202020ull) & ~w_ & 0x8080808080808080ull; \
1463
+ uint64_t hi_ = cl ? (w_ & 0x8080808080808080ull) : 0; \
1464
+ if (qm_ != 0 && ((bm_ | c0_ | hi_) & (qm_ - 1)) == 0) { \
1465
+ size_t cl_ = j_ + (size_t)yep_ctz64(qm_) / 8; \
1466
+ kinds[count] = (uint8_t)YEP_T_STR; \
1467
+ offs[count] = (uint32_t)(j_); \
1468
+ lens[count] = (uint32_t)((cl_ - j_)); \
1469
+ count++; \
1470
+ i = cl_ + 1; \
1471
+ goto after; \
1472
+ } \
1473
+ /* rounds 2-4 (up to 32 bytes inline): the email-class strings \
1474
+ * close here without the quote_scan call and its redundant \
1475
+ * content re-sweep; reached only when round 1 had no close, \
1476
+ * so the short-string case pays nothing */ \
1477
+ if ((qm_ | bm_ | c0_ | hi_) == 0) { \
1478
+ size_t nr_ = (len - j_) >> 3; \
1479
+ if (nr_ > 4) { \
1480
+ nr_ = 4; \
1481
+ } \
1482
+ for (size_t r_ = 1; r_ < nr_; r_++) { \
1483
+ size_t b2_ = j_ + (r_ << 3); \
1484
+ memcpy(&w_, p + b2_, 8); \
1485
+ qm_ = (w_ ^ 0x2222222222222222ull); \
1486
+ bm_ = (w_ ^ 0x5C5C5C5C5C5C5C5Cull); \
1487
+ qm_ = (qm_ - 0x0101010101010101ull) & ~qm_ & 0x8080808080808080ull; \
1488
+ bm_ = (bm_ - 0x0101010101010101ull) & ~bm_ & 0x8080808080808080ull; \
1489
+ c0_ = (w_ - 0x2020202020202020ull) & ~w_ & 0x8080808080808080ull; \
1490
+ hi_ = cl ? (w_ & 0x8080808080808080ull) : 0; \
1491
+ if (qm_ != 0 && ((bm_ | c0_ | hi_) & (qm_ - 1)) == 0) { \
1492
+ size_t cl_ = b2_ + (size_t)yep_ctz64(qm_) / 8; \
1493
+ kinds[count] = (uint8_t)YEP_T_STR; \
1494
+ offs[count] = (uint32_t)(j_); \
1495
+ lens[count] = (uint32_t)((cl_ - j_)); \
1496
+ count++; \
1497
+ i = cl_ + 1; \
1498
+ goto after; \
1499
+ } \
1500
+ if ((qm_ | bm_ | c0_ | hi_) != 0) { \
1501
+ break; /* escape, c0 or hi: kernel authority */ \
1502
+ } \
1503
+ } \
1504
+ } \
1505
+ } \
1506
+ { \
1507
+ const yep_text_kernels* kk_ = yep_text_active(); \
1508
+ ptrdiff_t r_ = kk_->quote_scan(p + j_, len - j_, '"', &esc_); \
1509
+ if (r_ >= 0 && esc_ == 0) { \
1510
+ size_t cl_ = j_ + (size_t)r_; \
1511
+ for (size_t b_ = j_; b_ < cl_;) { \
1512
+ size_t room_ = cl_ - b_; \
1513
+ if (room_ >= 8) { \
1514
+ uint64_t v_; \
1515
+ memcpy(&v_, p + b_, 8); \
1516
+ uint64_t bad_ = \
1517
+ (v_ - 0x2020202020202020ull) & ~v_ & 0x8080808080808080ull; \
1518
+ if (cl) { \
1519
+ bad_ |= v_ & 0x8080808080808080ull; \
1520
+ } \
1521
+ if (bad_) { \
1522
+ goto lreject; \
1523
+ } \
1524
+ b_ += 8; \
1525
+ } else { \
1526
+ if ((unsigned char)p[b_] < 0x20 || (cl && (unsigned char)p[b_] >= 0x80)) { \
1527
+ goto lreject; \
1528
+ } \
1529
+ b_++; \
1530
+ } \
1531
+ } \
1532
+ kinds[count] = (uint8_t)YEP_T_STR; \
1533
+ offs[count] = (uint32_t)(j_); \
1534
+ lens[count] = (uint32_t)((cl_ - j_)); \
1535
+ count++; \
1536
+ i = cl_ + 1; \
1537
+ goto after; \
1538
+ } \
1539
+ } \
1540
+ { \
1541
+ size_t at_ = i; \
1542
+ size_t cl_ = 0; \
1543
+ if (!yep_json_string(p, len, &i, &cl_, &esc_)) { \
1544
+ goto lreject; \
1545
+ } \
1546
+ if (cl) { /* escaped strings skip the sweeps above: the \
1547
+ * clean route still owes the strict sequence's \
1548
+ * byte guarantees over the content */ \
1549
+ for (size_t b_ = at_ + 1; b_ < cl_; b_++) { \
1550
+ unsigned char uc_ = (unsigned char)p[b_]; \
1551
+ if (uc_ < 0x20 || uc_ >= 0x80) { \
1552
+ goto lreject; \
1553
+ } \
1554
+ } \
1555
+ } \
1556
+ kinds[count] = (uint8_t)YEP_T_STR; \
1557
+ offs[count] = (uint32_t)((at_ + 1)); \
1558
+ lens[count] = (uint32_t)((cl_ - at_ - 1)); \
1559
+ count++; \
1560
+ i = cl_ + 1; \
1561
+ } \
1562
+ goto after; \
1563
+ } while (0)
1564
+
1565
+ /* The map member cycle — ONE contiguous region (the json-doc lesson:
1566
+ * a goto web across value/key/after regions scattered the hot path and
1567
+ * cost 2.7x on the flat-map shape). Values scan inline; only nested
1568
+ * containers, literals, and the kernel string fallback leave the
1569
+ * cycle. Acceptance is the chain's: no trailing commas, string keys
1570
+ * only, `:` required, closers only in first-member position. */
1571
+ lmap1: /* {} closes here */
1572
+ LWS();
1573
+ if (p[i] == '}') {
1574
+ LCLOSE();
1575
+ }
1576
+ if (p[i] != '"') {
1577
+ goto lreject;
1578
+ }
1579
+ goto lmapkey;
1580
+
1581
+ lmap: /* a member MUST follow (after ','): JW_KEY — a closer rejects */
1582
+ /* compact fast path: the byte is the key's quote already — LWS's
1583
+ * loop would examine it once and exit */
1584
+ if (i < len && p[i] == '"') {
1585
+ goto lmapkey;
1586
+ }
1587
+ LWS();
1588
+ if (p[i] != '"') {
1589
+ goto lreject;
1590
+ }
1591
+ lmapkey: { LSTR_SCAN(lmapcolon); }
1592
+ lmapcolon:
1593
+ if (i < len && p[i] == ':') {
1594
+ i++;
1595
+ /* compact fast path: the value byte follows the colon with no
1596
+ * whitespace — one load dispatches it (the ws test below is
1597
+ * what LWS would run anyway) */
1598
+ if (i < len) {
1599
+ char v = p[i];
1600
+ if (v != ' ' && v != '\n' && v != '\r' && (cl || v != '\t')) {
1601
+ goto lmapval;
1602
+ }
1603
+ }
1604
+ LWS();
1605
+ goto lmapval;
1606
+ }
1607
+ LWS();
1608
+ if (p[i] != ':') {
1609
+ goto lreject;
1610
+ }
1611
+ i++;
1612
+ LWS();
1613
+ lmapval: {
1614
+ char c = p[i];
1615
+ if (c == '"') {
1616
+ LSTR_SCAN(lmapafter);
1617
+ }
1618
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
1619
+ size_t k = i + 1;
1620
+ /* the digits-only tracking makes the pure-integer case a
1621
+ * 3-cycle leading-zero check — number_shape (a call + a
1622
+ * rescan, 13% of json-doc) runs only for dot/exp spans */
1623
+ int digits_only = 1, saw_digit = (c != '-');
1624
+ LNUM_RUN;
1625
+ while (k < len) {
1626
+ char d = p[k];
1627
+ if ((unsigned)(d - '0') <= 9u) {
1628
+ k++;
1629
+ saw_digit = 1;
1630
+ continue;
1631
+ }
1632
+ /* the flag clears only on a CONSUMED byte: the run's
1633
+ * terminator (',', '}', ']', whitespace) must leave
1634
+ * digits_only intact, or every plain integer followed
1635
+ * by a delimiter falls into the full validator */
1636
+ if (d == '-' || d == '+' || d == '.' || d == 'e' || d == 'E') {
1637
+ digits_only = 0;
1638
+ k++;
1639
+ continue;
1640
+ }
1641
+ break;
1642
+ }
1643
+ if (strict_nums) {
1644
+ int ok;
1645
+ if (digits_only && saw_digit) {
1646
+ size_t d0 = (c == '-') ? i + 1 : i;
1647
+ ok = (k - d0 == 1) || p[d0] != '0';
1648
+ } else {
1649
+ ok = tape_num_ok(p + i, (size_t)(k - i));
1650
+ }
1651
+ if (!ok) {
1652
+ goto lreject;
1653
+ }
1654
+ }
1655
+ kinds[count] =
1656
+ strict_nums ? ((digits_only && saw_digit) ? (uint8_t)YEP_T_INT : (uint8_t)YEP_T_FLOAT)
1657
+ : (uint8_t)YEP_T_NUM;
1658
+ offs[count] = (uint32_t)i;
1659
+ lens[count] = (uint32_t)(k - i);
1660
+ count++;
1661
+ i = k;
1662
+ goto lmapafter;
1663
+ }
1664
+ if (c == 't' || c == 'f' || c == 'n') {
1665
+ size_t wl = c == 'f' ? 5 : 4;
1666
+ if (i + wl > len) {
1667
+ goto lreject;
1668
+ }
1669
+ uint32_t got4;
1670
+ memcpy(&got4, p + i, 4);
1671
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
1672
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
1673
+ : 0x736C6166u /* "fals" */) ||
1674
+ (c == 'f' && p[i + 4] != 'e')) {
1675
+ goto lreject;
1676
+ }
1677
+ if (i + wl < len) {
1678
+ char z = p[i + wl];
1679
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
1680
+ z != '}' && z != ':') {
1681
+ goto lreject;
1682
+ }
1683
+ }
1684
+ kinds[count] = c == 'n' ? (uint8_t)YEP_T_NULL
1685
+ : (c == 't' ? (uint8_t)YEP_T_TRUE : (uint8_t)YEP_T_FALSE);
1686
+ offs[count] = (uint32_t)i;
1687
+ lens[count] = (uint32_t)wl;
1688
+ count++;
1689
+ i += wl;
1690
+ goto lmapafter;
1691
+ }
1692
+ if (c == '{') {
1693
+ LPUSH(1);
1694
+ goto lmap1;
1695
+ }
1696
+ if (c == '[') {
1697
+ LPUSH(0);
1698
+ goto lseq1;
1699
+ }
1700
+ goto lreject;
1701
+ }
1702
+
1703
+ lmapafter:
1704
+ /* compact fast path: value ',' key directly — the two LWS hops the
1705
+ * general form runs both exit on byte one here */
1706
+ if (i < len && p[i] == ',') {
1707
+ i++;
1708
+ if (i < len && p[i] == '"') {
1709
+ goto lmapkey;
1710
+ }
1711
+ goto lmap;
1712
+ }
1713
+ LWS();
1714
+ if (p[i] == ',') {
1715
+ i++;
1716
+ goto lmap;
1717
+ }
1718
+ if (p[i] == '}') {
1719
+ LCLOSE();
1720
+ }
1721
+ goto lreject;
1722
+
1723
+ /* The sequence member cycle — the same single-region shape. */
1724
+ lseq1: /* [] closes here */
1725
+ LWS();
1726
+ if (p[i] == ']') {
1727
+ LCLOSE();
1728
+ }
1729
+ goto lseqval;
1730
+
1731
+ lseq: /* a member MUST follow (after ','): JW_VALUE */
1732
+ LWS();
1733
+ lseqval: {
1734
+ char c = p[i];
1735
+ if (c == '"') {
1736
+ LSTR_SCAN(lseqafter);
1737
+ }
1738
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
1739
+ size_t k = i + 1;
1740
+ int digits_only = 1, saw_digit = (c != '-');
1741
+ LNUM_RUN;
1742
+ while (k < len) {
1743
+ char d = p[k];
1744
+ if ((unsigned)(d - '0') <= 9u) {
1745
+ k++;
1746
+ saw_digit = 1;
1747
+ continue;
1748
+ }
1749
+ /* consumed-only clearing: see the map arm's comment */
1750
+ if (d == '-' || d == '+' || d == '.' || d == 'e' || d == 'E') {
1751
+ digits_only = 0;
1752
+ k++;
1753
+ continue;
1754
+ }
1755
+ break;
1756
+ }
1757
+ if (strict_nums) {
1758
+ int ok;
1759
+ if (digits_only && saw_digit) {
1760
+ size_t d0 = (c == '-') ? i + 1 : i;
1761
+ ok = (k - d0 == 1) || p[d0] != '0';
1762
+ } else {
1763
+ ok = tape_num_ok(p + i, (size_t)(k - i));
1764
+ }
1765
+ if (!ok) {
1766
+ goto lreject;
1767
+ }
1768
+ }
1769
+ kinds[count] =
1770
+ strict_nums ? ((digits_only && saw_digit) ? (uint8_t)YEP_T_INT : (uint8_t)YEP_T_FLOAT)
1771
+ : (uint8_t)YEP_T_NUM;
1772
+ offs[count] = (uint32_t)i;
1773
+ lens[count] = (uint32_t)(k - i);
1774
+ count++;
1775
+ i = k;
1776
+ goto lseqafter;
1777
+ }
1778
+ if (c == 't' || c == 'f' || c == 'n') {
1779
+ size_t wl = c == 'f' ? 5 : 4;
1780
+ if (i + wl > len) {
1781
+ goto lreject;
1782
+ }
1783
+ uint32_t got4;
1784
+ memcpy(&got4, p + i, 4);
1785
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
1786
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
1787
+ : 0x736C6166u /* "fals" */) ||
1788
+ (c == 'f' && p[i + 4] != 'e')) {
1789
+ goto lreject;
1790
+ }
1791
+ if (i + wl < len) {
1792
+ char z = p[i + wl];
1793
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
1794
+ z != '}' && z != ':') {
1795
+ goto lreject;
1796
+ }
1797
+ }
1798
+ kinds[count] = c == 'n' ? (uint8_t)YEP_T_NULL
1799
+ : (c == 't' ? (uint8_t)YEP_T_TRUE : (uint8_t)YEP_T_FALSE);
1800
+ offs[count] = (uint32_t)i;
1801
+ lens[count] = (uint32_t)wl;
1802
+ count++;
1803
+ i += wl;
1804
+ goto lseqafter;
1805
+ }
1806
+ if (c == '{') {
1807
+ LPUSH(1);
1808
+ goto lmap1;
1809
+ }
1810
+ if (c == '[') {
1811
+ LPUSH(0);
1812
+ goto lseq1;
1813
+ }
1814
+ goto lreject;
1815
+ }
1816
+
1817
+ lseqafter:
1818
+ /* compact fast path: value ',' value directly — both LWS hops exit
1819
+ * on byte one here */
1820
+ if (i < len && p[i] == ',') {
1821
+ i++;
1822
+ if (i < len) {
1823
+ char v = p[i];
1824
+ if (v != ' ' && v != '\n' && v != '\r' && (cl || v != '\t')) {
1825
+ goto lseqval;
1826
+ }
1827
+ }
1828
+ goto lseq;
1829
+ }
1830
+ LWS();
1831
+ if (p[i] == ',') {
1832
+ i++;
1833
+ goto lseq;
1834
+ }
1835
+ if (p[i] == ']') {
1836
+ LCLOSE();
1837
+ }
1838
+ goto lreject;
1839
+
1840
+ ldone:
1841
+ #undef LWS
1842
+ #undef LNUM_RUN
1843
+ #undef LPUSH
1844
+ #undef LCLOSE
1845
+ #undef LSTR_SCAN
1846
+
1847
+ {
1848
+ size_t tail = i;
1849
+ while (tail < len &&
1850
+ (p[tail] == ' ' || p[tail] == '\n' || p[tail] == '\r' || (p[tail] == '\t' && !cl))) {
1851
+ tail++;
1852
+ }
1853
+ if (tail != len) {
1854
+ goto lreject;
1855
+ }
1856
+ }
1857
+ t->count = count;
1858
+ t->_src = p;
1859
+ t->_srclen = len;
1860
+ t->_cols_ready = 1;
1861
+ return YEPTRIS_OK;
1862
+
1863
+ lreject:
1864
+ yeptris_tape_free(t);
1865
+ return YEPTRIS_ERROR_PARSE;
1866
+ }
1867
+
1868
+ static YEP_UNUSED_FN YeptrisStatus tape_walk_lnt(const char* p, size_t len, size_t open,
1869
+ const uint32_t* idx, size_t nidx,
1870
+ yeptris_json_tape* t) {
1871
+ if (tape_carve(t, len) != YEPTRIS_OK) {
1872
+ return YEPTRIS_ERROR_MEMORY;
1873
+ }
1874
+ uint8_t* kinds = t->kinds;
1875
+ uint32_t* offs = t->offs;
1876
+ uint32_t* lens = t->lens;
1877
+ uint32_t open_at[YEP_JSON_WALK_DEPTH];
1878
+ uint8_t kind[YEP_JSON_WALK_DEPTH];
1879
+
1880
+ kinds[0] = YEP_T_DOC;
1881
+ offs[0] = 0;
1882
+ lens[0] = 0;
1883
+
1884
+ uint8_t top_kind = p[open] == '[' ? 0 : 1;
1885
+ kinds[1] = top_kind ? YEP_T_MAP_OPEN : YEP_T_SEQ_OPEN;
1886
+ offs[1] = 0;
1887
+ lens[1] = 0;
1888
+ uint32_t top_open = 1;
1889
+ size_t count = 2;
1890
+ uint8_t top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
1891
+ int key_slot = top_kind ? 1 : 0;
1892
+ int depth = 1;
1893
+ open_at[0] = top_open;
1894
+ kind[0] = top_kind;
1895
+
1896
+ size_t si = 0;
1897
+ while (si < nidx && idx[si] <= open) {
1898
+ si++; /* the root opener (and anything before it) is consumed */
1899
+ }
1900
+ for (;;) {
1901
+ if (si >= nidx) {
1902
+ goto reject; /* EOF mid-structure */
1903
+ }
1904
+ size_t at = idx[si++];
1905
+ if (at >= len) {
1906
+ goto reject;
1907
+ }
1908
+ char c = p[at];
1909
+
1910
+ if (top_expect == JW_COLON) {
1911
+ if (c != ':') {
1912
+ goto reject;
1913
+ }
1914
+ top_expect = JW_VALUE;
1915
+ key_slot = 0;
1916
+ continue;
1917
+ }
1918
+ if (top_expect == JW_COMMA_OR_CLOSE) {
1919
+ if (c == ',') {
1920
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
1921
+ key_slot = top_kind ? 1 : 0;
1922
+ continue;
1923
+ }
1924
+ if (c != ']' && c != '}') {
1925
+ goto reject;
1926
+ }
1927
+ } else if (key_slot) {
1928
+ if (c != '"' && c != ']' && c != '}') {
1929
+ goto reject;
1930
+ }
1931
+ }
1932
+
1933
+ if (c == '"') {
1934
+ size_t j = at + 1;
1935
+ size_t close = 0;
1936
+ int esc = 0;
1937
+ /* SWAR settle: one u64 window closes the corpus's 1-8
1938
+ * byte strings without the call; anything else (escape,
1939
+ * C0, longer) falls back to the kernel authority */
1940
+ if (j + 8 <= len) {
1941
+ uint64_t w;
1942
+ memcpy(&w, p + j, 8);
1943
+ uint64_t qm = (w ^ 0x2222222222222222ull);
1944
+ uint64_t bm = (w ^ 0x5C5C5C5C5C5C5C5Cull);
1945
+ qm = (qm - 0x0101010101010101ull) & ~qm & 0x8080808080808080ull;
1946
+ bm = (bm - 0x0101010101010101ull) & ~bm & 0x8080808080808080ull;
1947
+ uint64_t c0 = (w - 0x2020202020202020ull) & ~w & 0x8080808080808080ull;
1948
+ if (qm != 0 && ((bm | c0) & (qm - 1)) == 0) {
1949
+ close = j + (size_t)yep_ctz64(qm) / 8;
1950
+ kinds[count] = YEP_T_STR;
1951
+ offs[count] = (uint32_t)j;
1952
+ lens[count] = (uint32_t)(close - j);
1953
+ count++;
1954
+ goto str_done;
1955
+ }
1956
+ }
1957
+ size_t i = at;
1958
+ if (!yep_json_string(p, len, &i, &close, &esc)) {
1959
+ goto reject;
1960
+ }
1961
+ kinds[count] = YEP_T_STR;
1962
+ offs[count] = (uint32_t)(at + 1);
1963
+ lens[count] = (uint32_t)(close - at - 1);
1964
+ count++;
1965
+ str_done:
1966
+ if (key_slot) {
1967
+ /* colon peek: consume it without a loop round */
1968
+ if (si < nidx && p[idx[si]] == ':') {
1969
+ si++;
1970
+ top_expect = JW_VALUE;
1971
+ key_slot = 0;
1972
+ } else {
1973
+ top_expect = JW_COLON;
1974
+ }
1975
+ } else {
1976
+ goto comma_peek;
1977
+ }
1978
+ continue;
1979
+ }
1980
+ if ((unsigned)(c - '0') <= 9u || c == '-') {
1981
+ /* the deferred arm: the span runs to the next token,
1982
+ * trailing ws trimmed — no grammar scan, no shape split */
1983
+ size_t end = (si < nidx) ? idx[si] : len;
1984
+ while (end > at && (p[end - 1] == ' ' || p[end - 1] == '\t' || p[end - 1] == '\n' ||
1985
+ p[end - 1] == '\r')) {
1986
+ end--;
1987
+ }
1988
+ kinds[count] = YEP_T_NUM;
1989
+ offs[count] = (uint32_t)at;
1990
+ lens[count] = (uint32_t)(end - at);
1991
+ count++;
1992
+ comma_peek:
1993
+ /* comma peek: the value-or-key separator consumes without
1994
+ * a loop round (the corpus's dominant cycle) */
1995
+ if (si < nidx && p[idx[si]] == ',') {
1996
+ si++;
1997
+ top_expect = top_kind ? JW_KEY : JW_VALUE;
1998
+ key_slot = top_kind ? 1 : 0;
1999
+ } else {
2000
+ top_expect = JW_COMMA_OR_CLOSE;
2001
+ }
2002
+ continue;
2003
+ }
2004
+ if (c == ']' || c == '}') {
2005
+ int want = c == ']' ? 0 : 1;
2006
+ if (top_kind != want ||
2007
+ (top_expect != JW_VALUE_OR_CLOSE && top_expect != JW_KEY_OR_CLOSE &&
2008
+ top_expect != JW_COMMA_OR_CLOSE)) {
2009
+ goto reject;
2010
+ }
2011
+ kinds[count] = YEP_T_CLOSE;
2012
+ offs[count] = top_open;
2013
+ lens[count] = 0;
2014
+ offs[top_open] = (uint32_t)count;
2015
+ count++;
2016
+ depth--;
2017
+ if (depth == 0) {
2018
+ break;
2019
+ }
2020
+ top_kind = kind[depth - 1];
2021
+ top_expect = JW_COMMA_OR_CLOSE;
2022
+ top_open = open_at[depth - 1];
2023
+ key_slot = 0;
2024
+ continue;
2025
+ }
2026
+ if (c == '{' || c == '[') {
2027
+ if (depth >= YEP_JSON_WALK_DEPTH) {
2028
+ goto reject;
2029
+ }
2030
+ kind[depth - 1] = top_kind;
2031
+ open_at[depth - 1] = top_open;
2032
+ top_kind = c == '[' ? 0 : 1;
2033
+ kinds[count] = c == '[' ? YEP_T_SEQ_OPEN : YEP_T_MAP_OPEN;
2034
+ offs[count] = 0;
2035
+ lens[count] = 0;
2036
+ top_open = (uint32_t)count;
2037
+ count++;
2038
+ top_expect = top_kind ? JW_KEY_OR_CLOSE : JW_VALUE_OR_CLOSE;
2039
+ key_slot = top_kind ? 1 : 0;
2040
+ kind[depth] = top_kind;
2041
+ open_at[depth] = top_open;
2042
+ depth++;
2043
+ continue;
2044
+ }
2045
+ if (c == 't' || c == 'f' || c == 'n') {
2046
+ size_t wl = c == 'f' ? 5 : 4;
2047
+ if (at + wl > len) {
2048
+ goto reject;
2049
+ }
2050
+ uint32_t got4;
2051
+ memcpy(&got4, p + at, 4);
2052
+ if (got4 != (c == 't' ? 0x65757274u /* "true" */
2053
+ : c == 'n' ? 0x6C6C756Eu /* "null" */
2054
+ : 0x736C6166u /* "fals" */) ||
2055
+ (c == 'f' && p[at + 4] != 'e')) {
2056
+ goto reject;
2057
+ }
2058
+ if (at + wl < len) {
2059
+ char z = p[at + wl];
2060
+ if (z != ' ' && z != '\t' && z != '\n' && z != '\r' && z != ',' && z != ']' &&
2061
+ z != '}' && z != ':') {
2062
+ goto reject;
2063
+ }
2064
+ }
2065
+ kinds[count] = c == 'n' ? YEP_T_NULL : (c == 't' ? YEP_T_TRUE : YEP_T_FALSE);
2066
+ offs[count] = (uint32_t)at;
2067
+ lens[count] = (uint32_t)wl;
2068
+ count++;
2069
+ top_expect = JW_COMMA_OR_CLOSE;
2070
+ key_slot = 0;
2071
+ continue;
2072
+ }
2073
+ goto reject;
2074
+ }
2075
+ /* trailing residue: after the root close, any remaining token is
2076
+ * non-ws garbage (pure trailing ws emits no tokens) */
2077
+ if (si != nidx) {
2078
+ goto reject;
2079
+ }
2080
+ t->count = count;
2081
+ t->_src = p;
2082
+ t->_srclen = len;
2083
+ return YEPTRIS_OK;
2084
+
2085
+ reject:
2086
+ yeptris_tape_free(t);
2087
+ return YEPTRIS_ERROR_PARSE;
2088
+ }
2089
+
2090
+ YEPTRIS_API YeptrisStatus yeptris_parse_json_tape_lenient(const char* source, size_t len,
2091
+ yeptris_json_tape* tape) {
2092
+ if ((source == NULL && len != 0) || tape == NULL) {
2093
+ return YEPTRIS_ERROR_ARG;
2094
+ }
2095
+ memset(tape, 0, sizeof(*tape));
2096
+ if (len > 0xFFFFFFF0u) {
2097
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
2098
+ "tape spans are uint32; document exceeds 4 GiB");
2099
+ return YEPTRIS_ERROR_ARG;
2100
+ }
2101
+
2102
+ /* lenient defers grammar only; strings borrow validated spans, so
2103
+ * the encoding gate keeps the strict fallback (non-ASCII rides
2104
+ * yeptris_parse_json_tape unchanged) */
2105
+ if (len > 0 && yep_text_active()->gate_scan(source, len)) {
2106
+ return yeptris_parse_json_tape(source, len, tape);
2107
+ }
2108
+
2109
+ size_t at = 0;
2110
+ while (at < len &&
2111
+ (source[at] == ' ' || source[at] == '\t' || source[at] == '\n' || source[at] == '\r')) {
2112
+ at++;
2113
+ }
2114
+ if (at >= len) {
2115
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, at,
2116
+ "empty document (lenient tape)");
2117
+ return YEPTRIS_ERROR_PARSE;
2118
+ }
2119
+
2120
+ if (source[at] == '[' || source[at] == '{') {
2121
+ /* the fused walk IS the lenient route: one pass beats the
2122
+ * stage-1 + span two-pass on every bench corpus (json-doc
2123
+ * 1.65x, json-users 2.2x — the ledger's #85 scoping), and
2124
+ * this exact call already carried the route's verdicts as the
2125
+ * escape/C0 fallback, so acceptance is unchanged. */
2126
+ int rc = yep_tape_walk_lenient_fused(source, len, at, tape, 0, 0, 0);
2127
+ if (rc != YEPTRIS_OK) {
2128
+ return rc;
2129
+ }
2130
+ /* the YAML simple-key law rides the fused route too: a key
2131
+ * whose raw span crosses 1024 defers to the engine's clean
2132
+ * error — the JSON-class walk has no YAML laws, and the other
2133
+ * routes (engine_run, the marshal drains) reject the same
2134
+ * span, so acceptance here broke whole/chunked marshal parity
2135
+ * (the nightly's round 108) */
2136
+ if (yep_tape_long_simple_key(tape)) {
2137
+ yep_error_set(yep_error_tls(), YEP_ERR_KEY_TOO_LONG, 1, (uint32_t)(at + 1), at, NULL);
2138
+ return YEPTRIS_ERROR_PARSE;
2139
+ }
2140
+ return YEPTRIS_OK;
2141
+ }
2142
+
2143
+ /* scalar root: one record, same deferred split for numbers */
2144
+ if (tape_carve(tape, len) != YEPTRIS_OK) {
2145
+ return YEPTRIS_ERROR_MEMORY;
2146
+ }
2147
+ tape->_src = source;
2148
+ tape->_srclen = len;
2149
+ tape_ctx c = {tape, len + 2, {0}, 0, 0};
2150
+ if (!rec_put(&c, YEP_T_DOC, 0, 0)) {
2151
+ goto lnt_root_mem;
2152
+ }
2153
+ if (source[at] == '"') {
2154
+ size_t i = at;
2155
+ size_t close = 0;
2156
+ int esc = 0;
2157
+ if (!yep_json_string(source, len, &i, &close, &esc) ||
2158
+ !rec_put(&c, YEP_T_STR, (uint32_t)(at + 1), (uint32_t)(close - at - 1))) {
2159
+ goto lnt_root_reject;
2160
+ }
2161
+ while (i < len &&
2162
+ (source[i] == ' ' || source[i] == '\t' || source[i] == '\n' || source[i] == '\r')) {
2163
+ i++;
2164
+ }
2165
+ if (i != len) {
2166
+ goto lnt_root_reject;
2167
+ }
2168
+ return YEPTRIS_OK;
2169
+ }
2170
+ if (source[at] == 't' || source[at] == 'f' || source[at] == 'n') {
2171
+ const char* word = source[at] == 't' ? "true" : (source[at] == 'f' ? "false" : "null");
2172
+ size_t i = at;
2173
+ if (!yep_json_literal(source, len, &i, word)) {
2174
+ goto lnt_root_reject;
2175
+ }
2176
+ while (i < len &&
2177
+ (source[i] == ' ' || source[i] == '\t' || source[i] == '\n' || source[i] == '\r')) {
2178
+ i++;
2179
+ }
2180
+ if (i != len) {
2181
+ goto lnt_root_reject;
2182
+ }
2183
+ uint8_t k = source[at] == 'n' ? YEP_T_NULL : (source[at] == 't' ? YEP_T_TRUE : YEP_T_FALSE);
2184
+ if (!rec_put(&c, k, (uint32_t)at, (uint32_t)(i - at))) {
2185
+ goto lnt_root_mem;
2186
+ }
2187
+ return YEPTRIS_OK;
2188
+ }
2189
+ {
2190
+ size_t end = len;
2191
+ while (end > at && (source[end - 1] == ' ' || source[end - 1] == '\t' ||
2192
+ source[end - 1] == '\n' || source[end - 1] == '\r')) {
2193
+ end--;
2194
+ }
2195
+ if (!rec_put(&c, YEP_T_NUM, (uint32_t)at, (uint32_t)(end - at))) {
2196
+ goto lnt_root_mem;
2197
+ }
2198
+ return YEPTRIS_OK;
2199
+ }
2200
+
2201
+ lnt_root_mem:
2202
+ yeptris_tape_free(tape);
2203
+ return YEPTRIS_ERROR_MEMORY;
2204
+ lnt_root_reject:
2205
+ yeptris_tape_free(tape);
2206
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, at,
2207
+ "not strict JSON at byte %zu (lenient tape)", at);
2208
+ return YEPTRIS_ERROR_PARSE;
2209
+ }
2210
+
2211
+ YEPTRIS_API YeptrisStatus yeptris_parse_json_tape(const char* source, size_t len,
2212
+ yeptris_json_tape* tape) {
2213
+ if ((source == NULL && len != 0) || tape == NULL) {
2214
+ return YEPTRIS_ERROR_ARG;
2215
+ }
2216
+ memset(tape, 0, sizeof(*tape));
2217
+ if (len > 0xFFFFFFF0u) {
2218
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, 0,
2219
+ "tape spans are uint32; document exceeds 4 GiB");
2220
+ return YEPTRIS_ERROR_ARG;
2221
+ }
2222
+
2223
+ /* #474: the entry's full-input gate_scan pre-pass moves to the
2224
+ * reject path (the #342/#81 law on parse_json, now here): the
2225
+ * columns-primary fused walk runs clean_only — tabs and non-ASCII
2226
+ * bytes REJECT inline — so on accepted documents the NEON gate
2227
+ * scan (the profile's 5% lane) never runs. A walk reject re-gates:
2228
+ * the record walk and the encoding-owed decision read `gated` only
2229
+ * when actually taken, and every error precedence below is
2230
+ * unchanged (the validating sequence consumes the same bit). */
2231
+ int gated;
2232
+ size_t off = 0;
2233
+ while (off < len && (source[off] == ' ' || source[off] == '\t' || source[off] == '\n' ||
2234
+ source[off] == '\r')) {
2235
+ off++;
2236
+ }
2237
+ int container = off < len && (source[off] == '[' || source[off] == '{');
2238
+ if (container) {
2239
+ /* the columns-primary fused walk: one pass, columns eager
2240
+ * by construction, strict numbers classified at walk time */
2241
+ if (yep_tape_walk_columns_fused(source, len, off, tape, 1, 1) == YEPTRIS_OK) {
2242
+ return YEPTRIS_OK;
2243
+ }
2244
+ }
2245
+ gated = len > 0 && yep_text_active()->gate_scan(source, len);
2246
+ if (container && !gated) {
2247
+ /* the token-contract route: stage 1 (vector indexes) + the
2248
+ * lean walk. Measured BEHIND the fused walk on json-doc
2249
+ * (417-426 vs 526-533: stage 1 at 1408 MB/s + a walk whose
2250
+ * arms carry the same grammar cost) — gated OFF until the
2251
+ * sized cuts land (the op-table classifier trick, the
2252
+ * specialized map/seq loops, stage-1 string closes). The
2253
+ * fused walk is the shipped route; the law. */
2254
+ #if YEP_TOKEN_CONTRACT_ROUTE
2255
+ uint32_t* idx = (uint32_t*)malloc((len + 2) * sizeof(uint32_t));
2256
+ if (idx == NULL) {
2257
+ return YEPTRIS_ERROR_MEMORY;
2258
+ }
2259
+ size_t nidx = 0;
2260
+ unsigned sflags = 0;
2261
+ if (yep_text_active()->json_stage1(source, len, idx, &nidx, &sflags)) {
2262
+ YeptrisStatus st = tape_walk_idx(source, len, off, idx, nidx, tape);
2263
+ free(idx);
2264
+ if (st != YEPTRIS_ERROR_PARSE) {
2265
+ return st; /* OK or MEMORY; a reject falls through */
2266
+ }
2267
+ } else {
2268
+ free(idx);
2269
+ }
2270
+ #endif
2271
+ /* the record walk — the DOM route's exact call and flags
2272
+ * (strict numbers at record time). Non-ASCII documents stay
2273
+ * out of it: the gate above keeps today's routing, where the
2274
+ * validated lenient walk below owns them. */
2275
+ YeptrisStatus st = tape_walk(source, len, off, tape, 1);
2276
+ if (st != YEPTRIS_ERROR_PARSE) {
2277
+ return st; /* OK or MEMORY; a reject falls through */
2278
+ }
2279
+ }
2280
+
2281
+ /* The validating sequence — parse_json's fallback, byte for byte
2282
+ * in precedence: grammar, then encoding when the gate tripped. */
2283
+ size_t verr = 0;
2284
+ if (!yep_json_document(source, len, &verr)) {
2285
+ yep_error_set(yep_error_tls(), YEP_ERR_UNEXPECTED, 0, 0, verr,
2286
+ "not strict JSON at byte %zu", verr);
2287
+ return YEPTRIS_ERROR_PARSE;
2288
+ }
2289
+ if (gated) {
2290
+ size_t uerr = 0;
2291
+ if (!yep_utf8_validate((const unsigned char*)source, len, &uerr)) {
2292
+ yep_error_set(yep_error_tls(), YEP_ERR_ENCODING, 0, 0, uerr,
2293
+ "ill-formed UTF-8 at byte %zu", uerr);
2294
+ return YEPTRIS_ERROR_ENCODING;
2295
+ }
2296
+ }
2297
+
2298
+ size_t at = 0;
2299
+ while (at < len &&
2300
+ (source[at] == ' ' || source[at] == '\t' || source[at] == '\n' || source[at] == '\r')) {
2301
+ at++;
2302
+ }
2303
+ if (at < len && (source[at] == '[' || source[at] == '{')) {
2304
+ YeptrisStatus st = tape_walk(source, len, at, tape, 0);
2305
+ if (st == YEPTRIS_OK || st == YEPTRIS_ERROR_MEMORY) {
2306
+ return st;
2307
+ }
2308
+ yep_error_set(yep_error_tls(), YEP_ERR_INTERNAL, 0, 0, at,
2309
+ "tape walk rejected a validated document at byte %zu", at);
2310
+ return YEPTRIS_ERROR_INTERNAL;
2311
+ }
2312
+ if (tape_carve(tape, len) != YEPTRIS_OK) {
2313
+ return YEPTRIS_ERROR_MEMORY;
2314
+ }
2315
+ tape->_src = source;
2316
+ tape->_srclen = len;
2317
+ tape_ctx c = {tape, len + 2, {0}, 0, 0};
2318
+ if (!rec_put(&c, YEP_T_DOC, 0, 0) || !tape_put_root_scalar(&c, source, len, at)) {
2319
+ yep_error_set(yep_error_tls(), YEP_ERR_INTERNAL, 0, 0, at,
2320
+ "tape scalar conversion rejected a validated root at byte %zu", at);
2321
+ yeptris_tape_free(tape);
2322
+ return YEPTRIS_ERROR_INTERNAL;
2323
+ }
2324
+ return YEPTRIS_OK;
2325
+ }
2326
+
2327
+ YEPTRIS_API size_t yeptris_tape_convert(yeptris_json_tape* t, size_t from, size_t to,
2328
+ int64_t* ivals, double* dvals) {
2329
+ yeptris_tape_columns(t);
2330
+ if (t == NULL || t->_src == NULL || from > to || to > t->count) {
2331
+ return SIZE_MAX;
2332
+ }
2333
+ const char* p = (const char*)t->_src;
2334
+ size_t n = 0;
2335
+ for (size_t i = from; i < to; i++) {
2336
+ uint8_t k = t->kinds[i];
2337
+ if (k != YEP_T_INT && k != YEP_T_FLOAT && k != YEP_T_NUM) {
2338
+ continue;
2339
+ }
2340
+ size_t j = 0;
2341
+ int64_t iv = 0;
2342
+ double dv = 0;
2343
+ int shape = 0;
2344
+ if (!yep_json_number_scan(p + t->offs[i], t->lens[i], &j, &shape, &iv, &dv) ||
2345
+ j != t->lens[i]) {
2346
+ /* full-span consumption: strict spans always re-scan whole
2347
+ * (grammar-validated at parse); a lenient NUM run that
2348
+ * stops early carries non-number bytes — the drain-side
2349
+ * reject the deferred contract promises */
2350
+ return SIZE_MAX;
2351
+ }
2352
+ if (shape == 0) {
2353
+ if (ivals != NULL) {
2354
+ ivals[i] = iv;
2355
+ }
2356
+ } else {
2357
+ if (shape == 2) {
2358
+ t->int_min = 1;
2359
+ }
2360
+ if (dvals != NULL) {
2361
+ dvals[i] = dv;
2362
+ }
2363
+ }
2364
+ n++;
2365
+ }
2366
+ return n;
2367
+ }
2368
+
2369
+ YEPTRIS_API void yeptris_tape_free(yeptris_json_tape* tape) {
2370
+ if (tape == NULL) {
2371
+ return;
2372
+ }
2373
+ yep_free(yep_system_allocator(), tape->_block);
2374
+ memset(tape, 0, sizeof(*tape));
2375
+ }