psych 3.0.0.beta2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.travis.yml +20 -0
  4. data/CHANGELOG.rdoc +576 -0
  5. data/Gemfile +3 -0
  6. data/Mavenfile +7 -0
  7. data/README.md +73 -0
  8. data/Rakefile +46 -0
  9. data/bin/console +7 -0
  10. data/bin/setup +6 -0
  11. data/ext/psych/.gitignore +11 -0
  12. data/ext/psych/depend +3 -0
  13. data/ext/psych/extconf.rb +39 -0
  14. data/ext/psych/psych.c +34 -0
  15. data/ext/psych/psych.h +17 -0
  16. data/ext/psych/psych_emitter.c +554 -0
  17. data/ext/psych/psych_emitter.h +8 -0
  18. data/ext/psych/psych_parser.c +568 -0
  19. data/ext/psych/psych_parser.h +6 -0
  20. data/ext/psych/psych_to_ruby.c +39 -0
  21. data/ext/psych/psych_to_ruby.h +8 -0
  22. data/ext/psych/psych_yaml_tree.c +24 -0
  23. data/ext/psych/psych_yaml_tree.h +8 -0
  24. data/ext/psych/yaml/LICENSE +19 -0
  25. data/ext/psych/yaml/api.c +1392 -0
  26. data/ext/psych/yaml/config.h +10 -0
  27. data/ext/psych/yaml/dumper.c +394 -0
  28. data/ext/psych/yaml/emitter.c +2329 -0
  29. data/ext/psych/yaml/loader.c +444 -0
  30. data/ext/psych/yaml/parser.c +1374 -0
  31. data/ext/psych/yaml/reader.c +469 -0
  32. data/ext/psych/yaml/scanner.c +3576 -0
  33. data/ext/psych/yaml/writer.c +141 -0
  34. data/ext/psych/yaml/yaml.h +1971 -0
  35. data/ext/psych/yaml/yaml_private.h +662 -0
  36. data/lib/psych.rb +511 -0
  37. data/lib/psych/class_loader.rb +102 -0
  38. data/lib/psych/coder.rb +95 -0
  39. data/lib/psych/core_ext.rb +19 -0
  40. data/lib/psych/exception.rb +14 -0
  41. data/lib/psych/handler.rb +250 -0
  42. data/lib/psych/handlers/document_stream.rb +23 -0
  43. data/lib/psych/handlers/recorder.rb +40 -0
  44. data/lib/psych/json/ruby_events.rb +20 -0
  45. data/lib/psych/json/stream.rb +17 -0
  46. data/lib/psych/json/tree_builder.rb +13 -0
  47. data/lib/psych/json/yaml_events.rb +30 -0
  48. data/lib/psych/nodes.rb +78 -0
  49. data/lib/psych/nodes/alias.rb +19 -0
  50. data/lib/psych/nodes/document.rb +61 -0
  51. data/lib/psych/nodes/mapping.rb +57 -0
  52. data/lib/psych/nodes/node.rb +56 -0
  53. data/lib/psych/nodes/scalar.rb +68 -0
  54. data/lib/psych/nodes/sequence.rb +82 -0
  55. data/lib/psych/nodes/stream.rb +38 -0
  56. data/lib/psych/omap.rb +5 -0
  57. data/lib/psych/parser.rb +52 -0
  58. data/lib/psych/scalar_scanner.rb +149 -0
  59. data/lib/psych/set.rb +5 -0
  60. data/lib/psych/stream.rb +38 -0
  61. data/lib/psych/streaming.rb +28 -0
  62. data/lib/psych/syntax_error.rb +22 -0
  63. data/lib/psych/tree_builder.rb +97 -0
  64. data/lib/psych/versions.rb +9 -0
  65. data/lib/psych/visitors.rb +7 -0
  66. data/lib/psych/visitors/depth_first.rb +27 -0
  67. data/lib/psych/visitors/emitter.rb +52 -0
  68. data/lib/psych/visitors/json_tree.rb +25 -0
  69. data/lib/psych/visitors/to_ruby.rb +401 -0
  70. data/lib/psych/visitors/visitor.rb +20 -0
  71. data/lib/psych/visitors/yaml_tree.rb +551 -0
  72. data/lib/psych/y.rb +10 -0
  73. data/psych.gemspec +64 -0
  74. metadata +175 -0
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ # The version is Psych you're using
4
+ VERSION = '3.0.0.beta2'
5
+
6
+ if RUBY_ENGINE == 'jruby'
7
+ DEFAULT_SNAKEYAML_VERSION = '1.18'.freeze
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'psych/visitors/visitor'
3
+ require 'psych/visitors/to_ruby'
4
+ require 'psych/visitors/emitter'
5
+ require 'psych/visitors/yaml_tree'
6
+ require 'psych/visitors/json_tree'
7
+ require 'psych/visitors/depth_first'
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ module Visitors
4
+ class DepthFirst < Psych::Visitors::Visitor
5
+ def initialize block
6
+ @block = block
7
+ end
8
+
9
+ private
10
+
11
+ def nary o
12
+ o.children.each { |x| visit x }
13
+ @block.call o
14
+ end
15
+ alias :visit_Psych_Nodes_Stream :nary
16
+ alias :visit_Psych_Nodes_Document :nary
17
+ alias :visit_Psych_Nodes_Sequence :nary
18
+ alias :visit_Psych_Nodes_Mapping :nary
19
+
20
+ def terminal o
21
+ @block.call o
22
+ end
23
+ alias :visit_Psych_Nodes_Scalar :terminal
24
+ alias :visit_Psych_Nodes_Alias :terminal
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+ module Psych
3
+ module Visitors
4
+ class Emitter < Psych::Visitors::Visitor
5
+ def initialize io, options = {}
6
+ opts = [:indentation, :canonical, :line_width].find_all { |opt|
7
+ options.key?(opt)
8
+ }
9
+
10
+ if opts.empty?
11
+ @handler = Psych::Emitter.new io
12
+ else
13
+ du = Handler::DumperOptions.new
14
+ opts.each { |option| du.send :"#{option}=", options[option] }
15
+ @handler = Psych::Emitter.new io, du
16
+ end
17
+ end
18
+
19
+ def visit_Psych_Nodes_Stream o
20
+ @handler.start_stream o.encoding
21
+ o.children.each { |c| accept c }
22
+ @handler.end_stream
23
+ end
24
+
25
+ def visit_Psych_Nodes_Document o
26
+ @handler.start_document o.version, o.tag_directives, o.implicit
27
+ o.children.each { |c| accept c }
28
+ @handler.end_document o.implicit_end
29
+ end
30
+
31
+ def visit_Psych_Nodes_Scalar o
32
+ @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style
33
+ end
34
+
35
+ def visit_Psych_Nodes_Sequence o
36
+ @handler.start_sequence o.anchor, o.tag, o.implicit, o.style
37
+ o.children.each { |c| accept c }
38
+ @handler.end_sequence
39
+ end
40
+
41
+ def visit_Psych_Nodes_Mapping o
42
+ @handler.start_mapping o.anchor, o.tag, o.implicit, o.style
43
+ o.children.each { |c| accept c }
44
+ @handler.end_mapping
45
+ end
46
+
47
+ def visit_Psych_Nodes_Alias o
48
+ @handler.alias o.anchor
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+ require 'psych/json/ruby_events'
3
+
4
+ module Psych
5
+ module Visitors
6
+ class JSONTree < YAMLTree
7
+ include Psych::JSON::RubyEvents
8
+
9
+ def self.create options = {}
10
+ emitter = Psych::JSON::TreeBuilder.new
11
+ class_loader = ClassLoader.new
12
+ ss = ScalarScanner.new class_loader
13
+ new(emitter, ss, options)
14
+ end
15
+
16
+ def accept target
17
+ if target.respond_to?(:encode_with)
18
+ dump_coder target
19
+ else
20
+ send(@dispatch_cache[target.class], target)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,401 @@
1
+ # frozen_string_literal: true
2
+ require 'psych/scalar_scanner'
3
+ require 'psych/class_loader'
4
+ require 'psych/exception'
5
+
6
+ unless defined?(Regexp::NOENCODING)
7
+ Regexp::NOENCODING = 32
8
+ end
9
+
10
+ module Psych
11
+ module Visitors
12
+ ###
13
+ # This class walks a YAML AST, converting each node to Ruby
14
+ class ToRuby < Psych::Visitors::Visitor
15
+ def self.create
16
+ class_loader = ClassLoader.new
17
+ scanner = ScalarScanner.new class_loader
18
+ new(scanner, class_loader)
19
+ end
20
+
21
+ attr_reader :class_loader
22
+
23
+ def initialize ss, class_loader
24
+ super()
25
+ @st = {}
26
+ @ss = ss
27
+ @domain_types = Psych.domain_types
28
+ @class_loader = class_loader
29
+ end
30
+
31
+ def accept target
32
+ result = super
33
+ return result if @domain_types.empty? || !target.tag
34
+
35
+ key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
36
+ key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/
37
+
38
+ if @domain_types.key? key
39
+ value, block = @domain_types[key]
40
+ return block.call value, result
41
+ end
42
+
43
+ result
44
+ end
45
+
46
+ def deserialize o
47
+ if klass = resolve_class(Psych.load_tags[o.tag])
48
+ instance = klass.allocate
49
+
50
+ if instance.respond_to?(:init_with)
51
+ coder = Psych::Coder.new(o.tag)
52
+ coder.scalar = o.value
53
+ instance.init_with coder
54
+ end
55
+
56
+ return instance
57
+ end
58
+
59
+ return o.value if o.quoted
60
+ return @ss.tokenize(o.value) unless o.tag
61
+
62
+ case o.tag
63
+ when '!binary', 'tag:yaml.org,2002:binary'
64
+ o.value.unpack('m').first
65
+ when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
66
+ klass = resolve_class($1)
67
+ if klass
68
+ klass.allocate.replace o.value
69
+ else
70
+ o.value
71
+ end
72
+ when '!ruby/object:BigDecimal'
73
+ require 'bigdecimal' unless defined? BigDecimal
74
+ class_loader.big_decimal._load o.value
75
+ when "!ruby/object:DateTime"
76
+ class_loader.date_time
77
+ require 'date' unless defined? DateTime
78
+ @ss.parse_time(o.value).to_datetime
79
+ when '!ruby/encoding'
80
+ ::Encoding.find o.value
81
+ when "!ruby/object:Complex"
82
+ class_loader.complex
83
+ Complex(o.value)
84
+ when "!ruby/object:Rational"
85
+ class_loader.rational
86
+ Rational(o.value)
87
+ when "!ruby/class", "!ruby/module"
88
+ resolve_class o.value
89
+ when "tag:yaml.org,2002:float", "!float"
90
+ Float(@ss.tokenize(o.value))
91
+ when "!ruby/regexp"
92
+ klass = class_loader.regexp
93
+ o.value =~ /^\/(.*)\/([mixn]*)$/m
94
+ source = $1
95
+ options = 0
96
+ lang = nil
97
+ ($2 || '').split('').each do |option|
98
+ case option
99
+ when 'x' then options |= Regexp::EXTENDED
100
+ when 'i' then options |= Regexp::IGNORECASE
101
+ when 'm' then options |= Regexp::MULTILINE
102
+ when 'n' then options |= Regexp::NOENCODING
103
+ else lang = option
104
+ end
105
+ end
106
+ klass.new(*[source, options, lang].compact)
107
+ when "!ruby/range"
108
+ klass = class_loader.range
109
+ args = o.value.split(/([.]{2,3})/, 2).map { |s|
110
+ accept Nodes::Scalar.new(s)
111
+ }
112
+ args.push(args.delete_at(1) == '...')
113
+ klass.new(*args)
114
+ when /^!ruby\/sym(bol)?:?(.*)?$/
115
+ class_loader.symbolize o.value
116
+ else
117
+ @ss.tokenize o.value
118
+ end
119
+ end
120
+ private :deserialize
121
+
122
+ def visit_Psych_Nodes_Scalar o
123
+ register o, deserialize(o)
124
+ end
125
+
126
+ def visit_Psych_Nodes_Sequence o
127
+ if klass = resolve_class(Psych.load_tags[o.tag])
128
+ instance = klass.allocate
129
+
130
+ if instance.respond_to?(:init_with)
131
+ coder = Psych::Coder.new(o.tag)
132
+ coder.seq = o.children.map { |c| accept c }
133
+ instance.init_with coder
134
+ end
135
+
136
+ return instance
137
+ end
138
+
139
+ case o.tag
140
+ when nil
141
+ register_empty(o)
142
+ when '!omap', 'tag:yaml.org,2002:omap'
143
+ map = register(o, Psych::Omap.new)
144
+ o.children.each { |a|
145
+ map[accept(a.children.first)] = accept a.children.last
146
+ }
147
+ map
148
+ when /^!(?:seq|ruby\/array):(.*)$/
149
+ klass = resolve_class($1)
150
+ list = register(o, klass.allocate)
151
+ o.children.each { |c| list.push accept c }
152
+ list
153
+ else
154
+ register_empty(o)
155
+ end
156
+ end
157
+
158
+ def visit_Psych_Nodes_Mapping o
159
+ if Psych.load_tags[o.tag]
160
+ return revive(resolve_class(Psych.load_tags[o.tag]), o)
161
+ end
162
+ return revive_hash(register(o, {}), o) unless o.tag
163
+
164
+ case o.tag
165
+ when /^!ruby\/struct:?(.*)?$/
166
+ klass = resolve_class($1) if $1
167
+
168
+ if klass
169
+ s = register(o, klass.allocate)
170
+
171
+ members = {}
172
+ struct_members = s.members.map { |x| class_loader.symbolize x }
173
+ o.children.each_slice(2) do |k,v|
174
+ member = accept(k)
175
+ value = accept(v)
176
+ if struct_members.include?(class_loader.symbolize(member))
177
+ s.send("#{member}=", value)
178
+ else
179
+ members[member.to_s.sub(/^@/, '')] = value
180
+ end
181
+ end
182
+ init_with(s, members, o)
183
+ else
184
+ klass = class_loader.struct
185
+ members = o.children.map { |c| accept c }
186
+ h = Hash[*members]
187
+ s = klass.new(*h.map { |k,v|
188
+ class_loader.symbolize k
189
+ }).new(*h.map { |k,v| v })
190
+ register(o, s)
191
+ s
192
+ end
193
+
194
+ when /^!ruby\/object:?(.*)?$/
195
+ name = $1 || 'Object'
196
+
197
+ if name == 'Complex'
198
+ class_loader.complex
199
+ h = Hash[*o.children.map { |c| accept c }]
200
+ register o, Complex(h['real'], h['image'])
201
+ elsif name == 'Rational'
202
+ class_loader.rational
203
+ h = Hash[*o.children.map { |c| accept c }]
204
+ register o, Rational(h['numerator'], h['denominator'])
205
+ elsif name == 'Hash'
206
+ revive_hash(register(o, {}), o)
207
+ else
208
+ obj = revive((resolve_class(name) || class_loader.object), o)
209
+ obj
210
+ end
211
+
212
+ when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
213
+ klass = resolve_class($1)
214
+ members = {}
215
+ string = nil
216
+
217
+ o.children.each_slice(2) do |k,v|
218
+ key = accept k
219
+ value = accept v
220
+
221
+ if key == 'str'
222
+ if klass
223
+ string = klass.allocate.replace value
224
+ else
225
+ string = value
226
+ end
227
+ register(o, string)
228
+ else
229
+ members[key] = value
230
+ end
231
+ end
232
+ init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
233
+ when /^!ruby\/array:(.*)$/
234
+ klass = resolve_class($1)
235
+ list = register(o, klass.allocate)
236
+
237
+ members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a]
238
+ list.replace members['internal']
239
+
240
+ members['ivars'].each do |ivar, v|
241
+ list.instance_variable_set ivar, v
242
+ end
243
+ list
244
+
245
+ when '!ruby/range'
246
+ klass = class_loader.range
247
+ h = Hash[*o.children.map { |c| accept c }]
248
+ register o, klass.new(h['begin'], h['end'], h['excl'])
249
+
250
+ when /^!ruby\/exception:?(.*)?$/
251
+ h = Hash[*o.children.map { |c| accept c }]
252
+
253
+ e = build_exception((resolve_class($1) || class_loader.exception),
254
+ h.delete('message'))
255
+ init_with(e, h, o)
256
+
257
+ when '!set', 'tag:yaml.org,2002:set'
258
+ set = class_loader.psych_set.new
259
+ @st[o.anchor] = set if o.anchor
260
+ o.children.each_slice(2) do |k,v|
261
+ set[accept(k)] = accept(v)
262
+ end
263
+ set
264
+
265
+ when /^!ruby\/hash-with-ivars(?::(.*))?$/
266
+ hash = $1 ? resolve_class($1).allocate : {}
267
+ register o, hash
268
+ o.children.each_slice(2) do |key, value|
269
+ case key.value
270
+ when 'elements'
271
+ revive_hash hash, value
272
+ when 'ivars'
273
+ value.children.each_slice(2) do |k,v|
274
+ hash.instance_variable_set accept(k), accept(v)
275
+ end
276
+ end
277
+ end
278
+ hash
279
+
280
+ when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
281
+ revive_hash register(o, resolve_class($1).allocate), o
282
+
283
+ when '!omap', 'tag:yaml.org,2002:omap'
284
+ map = register(o, class_loader.psych_omap.new)
285
+ o.children.each_slice(2) do |l,r|
286
+ map[accept(l)] = accept r
287
+ end
288
+ map
289
+
290
+ when /^!ruby\/marshalable:(.*)$/
291
+ name = $1
292
+ klass = resolve_class(name)
293
+ obj = register(o, klass.allocate)
294
+
295
+ if obj.respond_to?(:init_with)
296
+ init_with(obj, revive_hash({}, o), o)
297
+ elsif obj.respond_to?(:marshal_load)
298
+ marshal_data = o.children.map(&method(:accept))
299
+ obj.marshal_load(marshal_data)
300
+ obj
301
+ else
302
+ raise ArgumentError, "Cannot deserialize #{name}"
303
+ end
304
+
305
+ else
306
+ revive_hash(register(o, {}), o)
307
+ end
308
+ end
309
+
310
+ def visit_Psych_Nodes_Document o
311
+ accept o.root
312
+ end
313
+
314
+ def visit_Psych_Nodes_Stream o
315
+ o.children.map { |c| accept c }
316
+ end
317
+
318
+ def visit_Psych_Nodes_Alias o
319
+ @st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" }
320
+ end
321
+
322
+ private
323
+ def register node, object
324
+ @st[node.anchor] = object if node.anchor
325
+ object
326
+ end
327
+
328
+ def register_empty object
329
+ list = register(object, [])
330
+ object.children.each { |c| list.push accept c }
331
+ list
332
+ end
333
+
334
+ SHOVEL = '<<'
335
+ def revive_hash hash, o
336
+ o.children.each_slice(2) { |k,v|
337
+ key = accept(k)
338
+ val = accept(v)
339
+
340
+ if key == SHOVEL && k.tag != "tag:yaml.org,2002:str"
341
+ case v
342
+ when Nodes::Alias, Nodes::Mapping
343
+ begin
344
+ hash.merge! val
345
+ rescue TypeError
346
+ hash[key] = val
347
+ end
348
+ when Nodes::Sequence
349
+ begin
350
+ h = {}
351
+ val.reverse_each do |value|
352
+ h.merge! value
353
+ end
354
+ hash.merge! h
355
+ rescue TypeError
356
+ hash[key] = val
357
+ end
358
+ else
359
+ hash[key] = val
360
+ end
361
+ else
362
+ hash[key] = val
363
+ end
364
+
365
+ }
366
+ hash
367
+ end
368
+
369
+ def merge_key hash, key, val
370
+ end
371
+
372
+ def revive klass, node
373
+ s = register(node, klass.allocate)
374
+ init_with(s, revive_hash({}, node), node)
375
+ end
376
+
377
+ def init_with o, h, node
378
+ c = Psych::Coder.new(node.tag)
379
+ c.map = h
380
+
381
+ if o.respond_to?(:init_with)
382
+ o.init_with c
383
+ else
384
+ h.each { |k,v| o.instance_variable_set(:"@#{k}", v) }
385
+ end
386
+ o
387
+ end
388
+
389
+ # Convert +klassname+ to a Class
390
+ def resolve_class klassname
391
+ class_loader.load klassname
392
+ end
393
+ end
394
+
395
+ class NoAliasRuby < ToRuby
396
+ def visit_Psych_Nodes_Alias o
397
+ raise BadAlias, "Unknown alias: #{o.anchor}"
398
+ end
399
+ end
400
+ end
401
+ end