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,389 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+ require "time"
5
+
6
+ module Yeptris
7
+ # Recorder-driven Ruby materialization (TODO.impl/15 phase B).
8
+ #
9
+ # One bulk drain: the record array and string arena are read in two
10
+ # calls, then a pure-Ruby stack machine walks fixed-layout records —
11
+ # the FFI tax is O(chunks), never O(events). The DOM-walk
12
+ # materializer (Node#to_ruby) stays for node-based use; this is the
13
+ # Yeptris::YAML fast path.
14
+ class Materializer
15
+ RECORD_SIZE = 44 # YeptrisEventRecord layout (events.h, ABI-pinned;
16
+ # #179 added end_line/end_col in the 0.6.19 window)
17
+ FIELDS = 14 # unpacked values per record
18
+ RECORD_UNPACK = "C4V10" # type/style/flags/tag_id, line..tag_len
19
+
20
+ STREAM_START = 1
21
+ STREAM_END = 2
22
+ DOCUMENT_START = 3
23
+ DOCUMENT_END = 4
24
+ SEQUENCE_START = 5
25
+ SEQUENCE_END = 6
26
+ MAPPING_START = 7
27
+ MAPPING_END = 8
28
+ SCALAR = 9
29
+ ALIAS = 10
30
+
31
+ EF_IMPLICIT = 1 << 2
32
+ STYLE_PLAIN = 1
33
+
34
+ # YeptrisTagId values (resolve.h; FFI mirrors them)
35
+ TAG_STR = Yeptris::FFI::TAG_STR
36
+ TAG_INT = Yeptris::FFI::TAG_INT
37
+ TAG_FLOAT = Yeptris::FFI::TAG_FLOAT
38
+ TAG_BOOL = Yeptris::FFI::TAG_BOOL
39
+ TAG_NULL = Yeptris::FFI::TAG_NULL
40
+ TAG_TIMESTAMP = Yeptris::FFI::TAG_TIMESTAMP
41
+ TAG_BINARY = Yeptris::FFI::TAG_BINARY
42
+ TAG_MERGE = 9 # YEPTRIS_TAG_MERGE (resolve.h): a plain '<<' key
43
+
44
+ INF_WORDS = {
45
+ ".inf" => Float::INFINITY, ".Inf" => Float::INFINITY, ".INF" => Float::INFINITY,
46
+ "+.inf" => Float::INFINITY, "+.Inf" => Float::INFINITY, "+.INF" => Float::INFINITY,
47
+ "-.inf" => -Float::INFINITY, "-.Inf" => -Float::INFINITY, "-.INF" => -Float::INFINITY,
48
+ ".nan" => Float::NAN, ".NaN" => Float::NAN, ".NAN" => Float::NAN,
49
+ }.freeze
50
+ SEXAGESIMAL_INT = /\A[-+]?[1-9][0-9_]*(:[0-5]?[0-9])+\z/
51
+ SEXAGESIMAL_FLOAT = /\A[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+:[0-5]?[0-9]\.[0-9_]*\z/
52
+
53
+ # Psych's quoted-symbol capture (scalar_scanner tokenize, the /^:/
54
+ # arm): /^:(["'])(.*)\1/ — a QUOTED symbol literal names the
55
+ # UNQUOTED string; the quotes are the scanner's, not the name's
56
+ # (#1304 — the plain-text strip rode every face as :"\"x y\"").
57
+ # Escapes do not decode: the literal is a plain scalar, so the
58
+ # inner text is captured byte for byte. Greedy .* + the backref —
59
+ # psych's own spelling, no end anchor.
60
+ QUOTED_SYMBOL = /\A:(["'])(.*)\1/
61
+
62
+ class << self
63
+ # The scalar→Symbol name law, ONE place: the quoted literal form
64
+ # (:"x y" / :'q z') strips the quoting before resolution; the
65
+ # bare form strips only the leading colon. Every materialization
66
+ # face rides this — the record walk (scan_by_tag), the columns
67
+ # walk (str_to_value) and the DOM walk (symbolize). The callers
68
+ # keep the ":": guard (plain-colon-only, :: stays a String).
69
+ def resolve_symbol(text)
70
+ m = QUOTED_SYMBOL.match(text)
71
+ return m[2].sub(/\A:/, "").to_sym if m
72
+
73
+ text[1..].to_sym
74
+ end
75
+ # First document of the stream, or nil when the stream is empty.
76
+ def load(yaml, schema: :compat_11)
77
+ docs = load_stream(yaml, schema: schema)
78
+ docs.empty? ? nil : docs.first
79
+ end
80
+
81
+ # Every document in the stream, in order.
82
+ def load_stream(yaml, schema: :compat_11)
83
+ new(schema: schema).materialize(yaml)
84
+ end
85
+
86
+ # The shared drain seam: one parse, records + arena read once,
87
+ # ONE unpack into a flat Integer array (12 fields per record:
88
+ # type style flags tag_id line col v_off v_len a_off a_len
89
+ # t_off t_len). Both consumers ride it — the stack machine
90
+ # (materialize) and the Psych::Parser dispatch loop.
91
+ def drain(yaml, schema: :compat_11)
92
+ rec = Yeptris::FFI.yeptris_recorder_new_ex(
93
+ schema == :compat_11 ? Yeptris::FFI::SCHEMA_11_COMPAT : Yeptris::FFI::SCHEMA_12_CORE
94
+ )
95
+ begin
96
+ status = Yeptris::FFI.yeptris_recorder_feed(rec, yaml, yaml.bytesize, 1)
97
+ if status != Yeptris::FFI::OK
98
+ raise Yeptris::ParseError,
99
+ "parse failed: #{Yeptris::FFI.last_error_message}"
100
+ end
101
+ count_p = ::FFI::MemoryPointer.new(:size_t)
102
+ records = Yeptris::FFI.yeptris_recorder_records(rec, count_p)
103
+ count = Yeptris::FFI.read_c_size_t(count_p)
104
+ arena_len = ::FFI::MemoryPointer.new(:size_t)
105
+ arena_ptr = Yeptris::FFI.yeptris_recorder_arena(rec, arena_len)
106
+ arena_len_v = Yeptris::FFI.read_c_size_t(arena_len)
107
+ arena = arena_ptr.null? || arena_len_v.zero? ? +"" : arena_ptr.read_bytes(arena_len_v)
108
+ flat = records.read_bytes(count * RECORD_SIZE)
109
+ .unpack(RECORD_UNPACK * count)
110
+ [flat, arena]
111
+ ensure
112
+ Yeptris::FFI.yeptris_recorder_free(rec)
113
+ end
114
+ end
115
+
116
+ # The Ruby value of a scalar per schema — the ScalarScanner rule
117
+ # set, spec-verified against Psych case by case. Only implicit
118
+ # PLAIN scalars scan; quoting is the escape hatch.
119
+ # The record's tag_id IS the resolver's verdict (the typing
120
+ # SSOT): conversion by tag, Kernel#Integer/Float for the bytes —
121
+ # no host-side grammar. Two Psych-quirk overrides where Psych's
122
+ # scanner disagrees with the 1.1 resolver: single-char y/Y/n/N
123
+ # are STRINGS in Psych (libyaml says bool), and values the
124
+ # resolver tagged INT but Kernel rejects (mixed forms) fall back
125
+ # through sexagesimal to String.
126
+ PSYCH_TRUE = %w[y yes true on].freeze
127
+
128
+ # '<<' merge: existing keys win; sequences merge in order.
129
+ # Shared by the record walk and the value-stream walk.
130
+ def merge_into(map, obj)
131
+ case obj
132
+ when Hash
133
+ obj.each { |k, v| map[k] = v unless map.key?(k) }
134
+ when Array
135
+ obj.each { |e| merge_into(map, e) if e.is_a?(Hash) }
136
+ end
137
+ end
138
+
139
+ def parse_timestamp(v)
140
+ # stdlib scalar_scanner: date-only strings parse with
141
+ # strptime('%F', Date::GREGORIAN) — the PROLEPTIC calendar —
142
+ # not Date.parse's default ITALY reform (a 1582 cycle fails
143
+ # the ported test_julian_date otherwise)
144
+ return ::Date.strptime(v, "%F", ::Date::GREGORIAN) unless v.match?(/[Tt ]\d/)
145
+
146
+ # stdlib scalar_scanner's parse_time, verbatim arithmetic:
147
+ # Time.xmlschema REJECTS the short offsets psych accepts
148
+ # ("2001-12-14 21:59:43 -5" — psych's zone is [-+]\d+(:\d\d)?),
149
+ # so a tagged timestamp must not ride xmlschema's zone
150
+ # grammar; the offset is applied by hand exactly as psych
151
+ # does, and a NAIVE timestamp (no zone, no Z) is read as a
152
+ # UTC instant in the local zone (klass.at in psych — one
153
+ # wall-clock hour off from reading it as local time; pinned
154
+ # by spec against Psych.unsafe_load across tz shapes)
155
+ date, time = v.split(/[Tt]|\s+/, 2)
156
+ (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map(&:to_i)
157
+ md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)
158
+ (hh, mm, ss) = md[1].split(":").map(&:to_i)
159
+ us = (md[2] ? Rational("0.#{md[2]}") : 0) * 1_000_000
160
+
161
+ t = ::Time.utc(yy, m, dd, hh, mm, ss, us)
162
+ return t if md[3] == "Z"
163
+ return t.getlocal unless md[3]
164
+
165
+ tz = md[3].match(/^([+-]?\d{1,2}):?(\d{1,2})?$/)[1..].compact.map { |d| Integer(d, 10) }
166
+ offset = tz.first * 3600
167
+ offset += (offset.negative? ? -1 : 1) * ((tz[1] || 0) * 60)
168
+ ::Time.new(yy, m, dd, hh, mm, ss + Rational(us, 1_000_000), offset)
169
+ rescue ArgumentError, NoMethodError
170
+ v
171
+ end
172
+
173
+ def scan_by_tag(value, tag_id, implicit)
174
+ case tag_id
175
+ when TAG_STR
176
+ return value unless implicit
177
+
178
+ value.start_with?(":") && !value.start_with?("::") &&
179
+ value.length > 1 ? resolve_symbol(value) : value
180
+ when TAG_NULL then nil
181
+ when TAG_BOOL
182
+ return value if value.length == 1 # Psych: "y"/"n" stay Strings
183
+
184
+ PSYCH_TRUE.include?(value.downcase)
185
+ when TAG_INT
186
+ int_or_string(value)
187
+ when TAG_FLOAT
188
+ float_or_string(value)
189
+ when TAG_TIMESTAMP
190
+ parse_timestamp(value)
191
+ when TAG_BINARY
192
+ value.unpack1("m") # psych to_ruby: !binary -> decode64, ASCII-8BIT (#168)
193
+ else
194
+ value
195
+ end
196
+ end
197
+
198
+ def int_or_string(value)
199
+ Integer(value.tr("_", ""))
200
+ rescue ArgumentError
201
+ # the resolver said INT but Kernel disagrees (mixed form):
202
+ # sexagesimal or back to String
203
+ return sexagesimal(value) if SEXAGESIMAL_INT.match?(value) ||
204
+ SEXAGESIMAL_FLOAT.match?(value)
205
+
206
+ value
207
+ end
208
+
209
+ def float_or_string(value)
210
+ return INF_WORDS[value] if INF_WORDS.key?(value)
211
+ return value unless value.include?(".")
212
+
213
+ # Psych's FLOAT: the exponent carries a mandatory sign —
214
+ # "1e3" and "1.5e3" are Strings (resolver quirk override)
215
+ return value if /[eE][^+-]/.match?(value)
216
+
217
+ Float(value.tr("_", ""))
218
+ rescue ArgumentError
219
+ return sexagesimal(value) if SEXAGESIMAL_FLOAT.match?(value)
220
+
221
+ value
222
+ end
223
+
224
+ private
225
+
226
+ # Psych's scanner IS Kernel#Integer on the plain digits: Ruby
227
+ # reads leading-zero strings as octal, rejects "018", takes
228
+ # 0x/0b/bases — verified case by case against Psych.
229
+ def sexagesimal(v)
230
+ is_float = v.include?(".")
231
+ total = 0
232
+ v.split(":").each_with_index do |n, e|
233
+ total += (is_float ? n.to_f : n.to_i) * (60**(e - 2).abs)
234
+ end
235
+ total
236
+ end
237
+
238
+
239
+ end
240
+
241
+ def initialize(schema: :compat_11)
242
+ @schema = schema
243
+ end
244
+
245
+ def materialize(yaml)
246
+ yaml = Yeptris.read_input(yaml)
247
+ yaml = yaml.to_s
248
+ flat, arena = Materializer.drain(yaml, schema: @schema)
249
+ walk(flat, arena)
250
+ end
251
+
252
+ private
253
+
254
+ # The stack machine: containers on a stack, a pending-key slot per
255
+ # open mapping, anchors by name (first definition wins; an alias
256
+ # yields the SAME Ruby object — identity preserved). Merge keys
257
+ # (<<) resolve inline: existing keys win, sequences merge in
258
+ # order — Psych load-time semantics.
259
+ #
260
+ # flat: one Integer per unpack field, 12 per record:
261
+ # 0 type, 1 style, 2 flags, 3 tag_id, 4 line, 5 col, 6 value_off,
262
+ # 7 value_len, 8 anchor_off, 9 anchor_len, 10 tag_off, 11 tag_len.
263
+ def walk(flat, arena)
264
+ docs = []
265
+ stack = []
266
+ anchors = {}
267
+ pending_key = []
268
+ pending_key_merge = []
269
+ # per open container: the map to merge into when this container
270
+ # closes (inline `<<:` values), nil otherwise
271
+ merge_target = []
272
+
273
+ # the arena is UTF-8 by construction (validated at parse), so
274
+ # forcing its encoding ONCE makes every slice UTF-8 for free —
275
+ # no per-scalar force_encoding
276
+ arena.force_encoding(Encoding::UTF_8)
277
+ # (each_slice DESTRUCTURING measured SLOWER than plain indexing
278
+ # here: it allocates a 12-slot Array per record. Index away.)
279
+ i = 0
280
+ n = flat.length
281
+ while i < n
282
+ type = flat[i]
283
+ case type
284
+ when SCALAR
285
+ # a plain, resolver-tagged '<<' (TAG_MERGE) merges; a
286
+ # QUOTED '<<' is a literal key — Psych merges on the tag,
287
+ # not the text
288
+ tag_id = flat[i + 3]
289
+ v = Materializer.scan_by_tag(
290
+ arena[flat[i + 8], flat[i + 9]], tag_id, (flat[i + 2] & EF_IMPLICIT) != 0
291
+ )
292
+ l = flat[i + 11]
293
+ anchors[arena[flat[i + 10], l]] = v if l != 0
294
+ # place(): the scalar fast path inlined — the overwhelming
295
+ # majority of events land here
296
+ if stack.empty?
297
+ docs[-1] = v
298
+ else
299
+ parent = stack.last
300
+ if parent.is_a?(Hash)
301
+ if (key = pending_key[-1]).nil?
302
+ pending_key[-1] = v
303
+ pending_key_merge[-1] = flat[i + 3] == TAG_MERGE
304
+ else
305
+ pending_key[-1] = nil
306
+ if pending_key_merge[-1]
307
+ merge_into(parent, v)
308
+ else
309
+ parent[key] = v
310
+ end
311
+ end
312
+ else
313
+ parent.push(v)
314
+ end
315
+ end
316
+ when MAPPING_START
317
+ h = {}
318
+ l = flat[i + 11]
319
+ anchors[arena[flat[i + 10], l]] = h if l != 0
320
+ merge_target.push(place(docs, stack, pending_key, pending_key_merge, h))
321
+ stack.push(h)
322
+ pending_key.push(nil)
323
+ pending_key_merge.push(nil)
324
+ when SEQUENCE_START
325
+ a = []
326
+ l = flat[i + 11]
327
+ anchors[arena[flat[i + 10], l]] = a if l != 0
328
+ merge_target.push(place(docs, stack, pending_key, pending_key_merge, a))
329
+ stack.push(a)
330
+ pending_key.push(nil)
331
+ pending_key_merge.push(nil)
332
+ when ALIAS
333
+ name = arena[flat[i + 8], flat[i + 9]]
334
+ raise Yeptris::ParseError, "unknown anchor: #{name.inspect}" unless anchors.key?(name)
335
+
336
+ place(docs, stack, pending_key, pending_key_merge, anchors[name])
337
+ when MAPPING_END, SEQUENCE_END
338
+ done = stack.pop
339
+ pending_key.pop
340
+ pending_key_merge.pop
341
+ target = merge_target.pop
342
+ merge_into(target, done) if target
343
+ when DOCUMENT_START
344
+ # anchors are per-document (the engine memo law; PyYAML's
345
+ # composer resets per document): a later document's alias
346
+ # must not resolve into an earlier one's name
347
+ anchors.clear
348
+ docs.push(nil)
349
+ end
350
+ i += FIELDS
351
+ end
352
+ docs
353
+ end
354
+
355
+ # Attaches obj: as the current document's root when nothing is
356
+ # open, as a sequence entry, or as the pending mapping key/value.
357
+ # Returns the merge TARGET when obj is a container placed under a
358
+ # "<<" key — the caller defers the merge to the container's END
359
+ # (an inline map's contents arrive after its start event); scalar
360
+ # and alias merges apply immediately.
361
+ def place(docs, stack, pending_key, pending_key_merge, obj)
362
+ if stack.empty?
363
+ docs[-1] = obj
364
+ return nil
365
+ end
366
+ parent = stack.last
367
+ if parent.is_a?(Hash)
368
+ if pending_key.last.nil?
369
+ pending_key[-1] = obj
370
+ pending_key_merge[-1] = false
371
+ return nil
372
+ end
373
+ key = pending_key.last
374
+ pending_key[-1] = nil
375
+ if pending_key_merge[-1]
376
+ merge_into(parent, obj)
377
+ parent
378
+ else
379
+ parent[key] = obj
380
+ nil
381
+ end
382
+ else # Array
383
+ parent.push(obj)
384
+ nil
385
+ end
386
+ end
387
+
388
+ end
389
+ end