psych-shopifork 2.0.0

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