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,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yeptris
4
+ module Psych
5
+ # The event-level API (TODO.impl/15 phase E): the abstract base
6
+ # Psych::Parser dispatches onto. Every event a YAML stream can
7
+ # produce is a method; the arg shapes are Psych's own (verified
8
+ # differentially against the stdlib recorder in the specs).
9
+ class Handler
10
+ # Scalar styles — the same integers Psych::Nodes::Scalar uses,
11
+ # and the same values YeptrisEventRecord.style carries.
12
+ ANY = 0
13
+ PLAIN = 1
14
+ SINGLE_QUOTED = 2
15
+ DOUBLE_QUOTED = 3
16
+ LITERAL = 4
17
+ FOLDED = 5
18
+
19
+ # Collection styles (Psych::Nodes::Sequence / Mapping).
20
+ BLOCK = 1
21
+ FLOW = 2
22
+
23
+ class DumperOptions
24
+ attr_accessor :line_width, :indentation, :canonical
25
+
26
+ def initialize
27
+ @line_width = 0
28
+ @indentation = 2
29
+ @canonical = false
30
+ end
31
+ end
32
+
33
+ OPTIONS = DumperOptions.new
34
+
35
+ EVENTS = %i[alias empty end_document end_mapping end_sequence end_stream
36
+ scalar start_document start_mapping start_sequence start_stream].freeze
37
+
38
+ # Called once per stream; +encoding+ is a Psych::Parser
39
+ # encoding constant (we always parse to UTF-8).
40
+ def start_stream(encoding); end
41
+
42
+ # +version+ is [major, minor] when %YAML declared, else nil
43
+ # (yeptris does not carry the directive into the event).
44
+ # +tag_directives+ is a list of [handle, prefix] pairs ([] for
45
+ # us: same directive omission). +implicit+ true without ---.
46
+ def start_document(version, tag_directives, implicit); end
47
+
48
+ def end_document(implicit); end
49
+
50
+ def alias(anchor); end
51
+
52
+ # #179: called before EVERY event with the event's 0-based marks
53
+ # (psych 5's contract — TreeBuilder stashes them for node
54
+ # locations). No-op by default; handlers that care override it.
55
+ def event_location(start_line, start_column, end_line, end_column); end
56
+
57
+ # +style+ is one of the scalar constants above; +plain+ /
58
+ # +quoted+ are the implicit-typing flags (plain: resolvable
59
+ # without a tag).
60
+ def scalar(value, anchor, tag, plain, quoted, style); end
61
+
62
+ def start_sequence(anchor, tag, implicit, style); end
63
+
64
+ def end_sequence; end
65
+
66
+ def start_mapping(anchor, tag, implicit, style); end
67
+
68
+ def end_mapping; end
69
+
70
+ def end_stream; end
71
+
72
+ # libyaml artifact (its NO_EVENT after stream end); the
73
+ # yeptris engine never produces one, so this never fires.
74
+ def empty; end
75
+ end
76
+
77
+ module Handlers
78
+ # Captures every event as [name, args] — Psych's own Recorder
79
+ # shape, so recordings replay through any Handler-compatible
80
+ # emitter.
81
+ class Recorder < Handler
82
+ attr_reader :events
83
+
84
+ def initialize
85
+ @events = []
86
+ super()
87
+ end
88
+
89
+ Handler::EVENTS.each do |event|
90
+ define_method(event) { |*args| @events << [event, args] }
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yeptris
4
+ module Psych
5
+ # The event parser (TODO.impl/15 phase E): drives a Handler with
6
+ # Psych's exact event shapes over the yeptris recorder — one bulk
7
+ # record drain (the Materializer's seam), then a pure-Ruby
8
+ # dispatch loop. No callback marshaling, no per-event FFI calls.
9
+ class Parser
10
+ attr_accessor :handler
11
+ attr_reader :filename
12
+
13
+ ANY = 0
14
+ UTF8 = 1
15
+ UTF16LE = 2
16
+ UTF16BE = 3
17
+
18
+ def initialize(handler = Handler.new)
19
+ @handler = handler
20
+ @filename = nil
21
+ end
22
+
23
+ def parse(yaml, filename = nil)
24
+ yaml = Yeptris.read_input(yaml)
25
+ yaml = yaml.to_s
26
+ @filename = filename
27
+ flat, arena = Materializer.drain(yaml, schema: :compat_11)
28
+ # the engine's arena is UTF-8 bytes; without the tag every
29
+ # scalar reached handlers as ASCII-8BIT (stdlib Psych hands
30
+ # UTF-8; TreeBuilder then propagated binary into node values)
31
+ arena.force_encoding(Encoding::UTF_8)
32
+ dispatch(flat, arena)
33
+ self
34
+ rescue ParseError => e
35
+ raise SyntaxError.from_parse_error(e)
36
+ end
37
+
38
+ private
39
+
40
+ STREAM_START = Materializer::STREAM_START
41
+ STREAM_END = Materializer::STREAM_END
42
+ DOCUMENT_START = Materializer::DOCUMENT_START
43
+ DOCUMENT_END = Materializer::DOCUMENT_END
44
+ SEQUENCE_START = Materializer::SEQUENCE_START
45
+ MAPPING_START = Materializer::MAPPING_START
46
+ SCALAR = Materializer::SCALAR
47
+ ALIAS = Materializer::ALIAS
48
+
49
+ EF_FLOW = 1 << 0
50
+ EF_EXPLICIT = 1 << 1
51
+ STYLE_PLAIN = 1
52
+ STYLE_SINGLE_QUOTED = 2
53
+ STYLE_DOUBLE_QUOTED = 3
54
+
55
+ def dispatch(flat, arena)
56
+ h = @handler
57
+ i = 0
58
+ while i < flat.length
59
+ # #179: psych's parser calls event_location before every
60
+ # event; TreeBuilder stashes the marks for node locations
61
+ # (0-based).
62
+ h.event_location(flat[i + 4] - 1, flat[i + 5] - 1,
63
+ flat[i + 6] - 1, flat[i + 7] - 1)
64
+ type = flat[i]
65
+ style = flat[i + 1]
66
+ flags = flat[i + 2]
67
+ value = field(flat, arena, i + 8)
68
+ anchor = field(flat, arena, i + 10)
69
+ tag = field(flat, arena, i + 12)
70
+ case type
71
+ when STREAM_START then h.start_stream(UTF8)
72
+ when STREAM_END then h.end_stream
73
+ # version: [] when no %YAML directive (Psych's own shape);
74
+ # a declared directive's [major, minor] is not carried by
75
+ # the record — the one documented divergence
76
+ when DOCUMENT_START then h.start_document([], [], (flags & EF_EXPLICIT).zero?)
77
+ when DOCUMENT_END then h.end_document((flags & EF_EXPLICIT).zero?)
78
+ when SEQUENCE_START
79
+ h.start_sequence(anchor, tag, tag.nil?, (flags & EF_FLOW).zero? ? Handler::BLOCK : Handler::FLOW)
80
+ when Materializer::SEQUENCE_END then h.end_sequence
81
+ when MAPPING_START
82
+ h.start_mapping(anchor, tag, tag.nil?, (flags & EF_FLOW).zero? ? Handler::BLOCK : Handler::FLOW)
83
+ when Materializer::MAPPING_END then h.end_mapping
84
+ when SCALAR
85
+ # A synthesized empty scalar (empty document, '?' empty
86
+ # key) carries style ANY(0); Psych's C emits plain(1)
87
+ # with an empty String value — normalized here, at the
88
+ # boundary, so the emitter's style chooser is untouched
89
+ if style.zero?
90
+ style = STYLE_PLAIN
91
+ end
92
+ value = +"" if value.nil?
93
+ # libyaml's plain_implicit: a plain scalar that carried
94
+ # no explicit tag (an explicit tag kills implicit typing)
95
+ plain = style == STYLE_PLAIN && tag.nil?
96
+ # quoted_implicit covers every explicit style — single,
97
+ # double, literal, folded — when the tag was absent
98
+ quoted = !plain && tag.nil? && style > STYLE_PLAIN
99
+ h.scalar(value, anchor, tag, plain, quoted, style)
100
+ when ALIAS then h.alias(value)
101
+ end
102
+ i += Materializer::FIELDS
103
+ end
104
+ end
105
+
106
+ def field(flat, arena, at)
107
+ len = flat[at + 1]
108
+ return nil if len.zero?
109
+
110
+ arena.byteslice(flat[at], len)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Yeptris
6
+ module Psych
7
+ ###
8
+ # Scan scalars for built in types — the stdlib Psych::ScalarScanner
9
+ # port (#179): same regexes, same precedence, same edge verdicts.
10
+ # The load path types through the C resolver; this class is the
11
+ # standalone API stdlib code (and Psych's own tests) drive directly.
12
+ class ScalarScanner
13
+ # Taken from http://yaml.org/type/timestamp.html
14
+ TIME = /^-?\d{4}-\d{1,2}-\d{1,2}(?:[Tt]|\s+)\d{1,2}:\d\d:\d\d(?:\.\d*)?(?:\s*(?:Z|[-+]\d{1,2}:?(?:\d\d)?))?$/
15
+
16
+ # Taken from http://yaml.org/type/float.html
17
+ # Base 60, [-+]inf and NaN are handled separately
18
+ FLOAT = /^(?:[-+]?([0-9][0-9_,]*)?\.[0-9]*([eE][-+][0-9]+)?(?# base 10))$/x
19
+
20
+ # Taken from http://yaml.org/type/int.html and modified to ensure at least one numerical symbol exists
21
+ INTEGER_STRICT = /^(?:[-+]?0b[_]*[0-1][0-1_]* (?# base 2)
22
+ |[-+]?0[_]*[0-7][0-7_]* (?# base 8)
23
+ |[-+]?(0|[1-9][0-9_]*) (?# base 10)
24
+ |[-+]?0x[_]*[0-9a-fA-F][0-9a-fA-F_]* (?# base 16))$/x
25
+
26
+ # Same as above, but allows commas.
27
+ # Not to YML spec, but kept for backwards compatibility
28
+ INTEGER_LEGACY = /^(?:[-+]?0b[_,]*[0-1][0-1_,]* (?# base 2)
29
+ |[-+]?0[_,]*[0-7][0-7_,]* (?# base 8)
30
+ |[-+]?(?:0|[1-9](?:[0-9]|,[0-9]|_[0-9])*) (?# base 10)
31
+ |[-+]?0x[_,]*[0-9a-fA-F][0-9a-fA-F_,]* (?# base 16))$/x
32
+
33
+ BOOLEAN_TRUE = /^(yes|true|on)$/i
34
+ BOOLEAN_FALSE = /^(no|false|off)$/i
35
+
36
+ attr_reader :class_loader
37
+
38
+ # Create a new scanner
39
+ def initialize class_loader = ClassLoader, strict_integer: false, parse_symbols: true
40
+ @symbol_cache = {}
41
+ @class_loader = class_loader
42
+ @strict_integer = strict_integer
43
+ @parse_symbols = parse_symbols
44
+ end
45
+
46
+ # Tokenize +string+ returning the Ruby object
47
+ def tokenize string
48
+ return nil if string.empty?
49
+ return @symbol_cache[string] if @symbol_cache.key?(string)
50
+ integer_regex = @strict_integer ? INTEGER_STRICT : INTEGER_LEGACY
51
+ # Check for a String type, being careful not to get caught by hash keys, hex values, and
52
+ # special floats (e.g., -.inf).
53
+ if string.match?(%r{^[^\d.:-]?[[:alpha:]_\s!@#$%\^&*(){}<>|/\\~;=]+}) || string.match?(/\n/)
54
+ return string if string.length > 5
55
+
56
+ if string.match?(/^[^ytonf~]/i)
57
+ string
58
+ elsif string == '~' || string.match?(/^null$/i)
59
+ nil
60
+ elsif string.match?(BOOLEAN_TRUE)
61
+ true
62
+ elsif string.match?(BOOLEAN_FALSE)
63
+ false
64
+ else
65
+ string
66
+ end
67
+ elsif string.match?(TIME)
68
+ begin
69
+ parse_time string
70
+ rescue ArgumentError
71
+ string
72
+ end
73
+ elsif string.match?(/^\d{4}-(?:1[012]|0\d|\d)-(?:[12]\d|3[01]|0\d|\d)$/)
74
+ begin
75
+ class_loader.date.strptime(string, '%F', Date::GREGORIAN)
76
+ rescue ArgumentError
77
+ string
78
+ end
79
+ elsif string.match?(/^\+?\.inf$/i)
80
+ Float::INFINITY
81
+ elsif string.match?(/^-\.inf$/i)
82
+ -Float::INFINITY
83
+ elsif string.match?(/^\.nan$/i)
84
+ Float::NAN
85
+ elsif @parse_symbols && string.match?(/^:./)
86
+ if string =~ /^:(["'])(.*)\1/
87
+ @symbol_cache[string] = class_loader.symbolize($2.sub(/^:/, ''))
88
+ else
89
+ @symbol_cache[string] = class_loader.symbolize(string.sub(/^:/, ''))
90
+ end
91
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}$/)
92
+ i = 0
93
+ string.split(':').each_with_index do |n, e|
94
+ i += (n.to_i * 60**(e - 2).abs)
95
+ end
96
+ i
97
+ elsif string.match?(/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9]){1,2}\.[0-9_]*$/)
98
+ i = 0
99
+ string.split(':').each_with_index do |n, e|
100
+ i += (n.to_f * 60**(e - 2).abs)
101
+ end
102
+ i
103
+ elsif string.match?(FLOAT)
104
+ if string.match?(/\A[-+]?\.\Z/)
105
+ string
106
+ else
107
+ Float(string.delete(',_').gsub(/\.([Ee]|$)/, '\1'))
108
+ end
109
+ elsif string.match?(integer_regex)
110
+ parse_int string
111
+ else
112
+ string
113
+ end
114
+ end
115
+
116
+ # Parse and return an int from +string+
117
+ def parse_int string
118
+ Integer(string.delete(',_'))
119
+ end
120
+
121
+ ###
122
+ # Parse and return a Time from +string+
123
+ def parse_time string
124
+ date, time = *(string.split(/[Tt]|\s+/, 2))
125
+ (yy, m, dd) = date.match(/^(-?\d{4})-(\d{1,2})-(\d{1,2})/).captures.map { |x| x.to_i }
126
+ md = time.match(/(\d+:\d+:\d+)(?:\.(\d*))?\s*(Z|[-+]\d+(:\d\d)?)?/)
127
+
128
+ (hh, mm, ss) = md[1].split(':').map { |x| x.to_i }
129
+ us = (md[2] ? Rational("0.#{md[2]}") : 0) * 1_000_000
130
+
131
+ time = Time.utc(yy, m, dd, hh, mm, ss, us)
132
+
133
+ return time if 'Z' == md[3]
134
+ return Time.at(time.to_i, us) unless md[3]
135
+
136
+ tz = md[3].match(/^([+\-]?\d{1,2})\:?(\d{1,2})?$/)[1..-1].compact.map { |digit| Integer(digit, 10) }
137
+ offset = tz.first * 3600
138
+
139
+ if offset < 0
140
+ offset -= ((tz[1] || 0) * 60)
141
+ else
142
+ offset += ((tz[1] || 0) * 60)
143
+ end
144
+
145
+ Time.new(yy, m, dd, hh, mm, ss + us / 1_000_000r, offset)
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,119 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yeptris
4
+ module Psych
5
+ # The stdlib TreeBuilder port (#179): builds a Nodes tree carrying
6
+ # start/end line:column on every node. The parser calls
7
+ # event_location before each event (psych 5's exact contract);
8
+ # stdlib's class is the reference — this port follows it
9
+ # function-for-function over the binding's keyword-arg Nodes.
10
+ class TreeBuilder < Handler
11
+ attr_reader :root
12
+
13
+ def initialize
14
+ super
15
+ @stack = []
16
+ @last = nil
17
+ @root = nil
18
+ @start_line = nil
19
+ @start_column = nil
20
+ @end_line = nil
21
+ @end_column = nil
22
+ end
23
+
24
+ def event_location(start_line, start_column, end_line, end_column)
25
+ @start_line = start_line
26
+ @start_column = start_column
27
+ @end_line = end_line
28
+ @end_column = end_column
29
+ end
30
+
31
+ %w[Sequence Mapping].each do |node|
32
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
33
+ def start_#{node.downcase}(anchor, tag, implicit, style)
34
+ n = Nodes::#{node}.new(anchor: anchor, tag: tag, style: style)
35
+ set_start_location(n)
36
+ @last.children << n
37
+ push n
38
+ end
39
+
40
+ def end_#{node.downcase}
41
+ n = pop
42
+ set_end_location(n)
43
+ n
44
+ end
45
+ RUBY
46
+ end
47
+
48
+ def start_document(version, tag_directives, implicit)
49
+ n = Nodes::Document.new(version, tag_directives)
50
+ set_start_location(n)
51
+ @last.children << n
52
+ push n
53
+ end
54
+
55
+ def end_document(_implicit = false)
56
+ n = pop
57
+ set_end_location(n)
58
+ n
59
+ end
60
+
61
+ def start_stream(_encoding)
62
+ n = Nodes::Stream.new
63
+ set_start_location(n)
64
+ @root = n
65
+ push n
66
+ end
67
+
68
+ def end_stream
69
+ n = pop
70
+ set_end_location(n)
71
+ n
72
+ end
73
+
74
+ def scalar(value, anchor, tag, plain, quoted, style)
75
+ n = Nodes::Scalar.new(value, anchor: anchor, tag: tag,
76
+ plain: plain, quoted: quoted, style: style)
77
+ set_location(n)
78
+ @last.children << n
79
+ n
80
+ end
81
+
82
+ def alias(anchor)
83
+ n = Nodes::Alias.new(anchor)
84
+ set_location(n)
85
+ @last.children << n
86
+ n
87
+ end
88
+
89
+ private
90
+
91
+ def push(value)
92
+ @stack.push(value)
93
+ @last = value
94
+ end
95
+
96
+ def pop
97
+ value = @stack.pop
98
+ @last = @stack.last
99
+ value
100
+ end
101
+
102
+ def set_location(n)
103
+ n.start_line = @start_line
104
+ n.start_column = @start_column
105
+ n.end_line = @end_line
106
+ n.end_column = @end_column
107
+ end
108
+
109
+ def set_start_location(n)
110
+ set_location(n)
111
+ end
112
+
113
+ def set_end_location(n)
114
+ n.end_line = @end_line
115
+ n.end_column = @end_column
116
+ end
117
+ end
118
+ end
119
+ end