RbYAML 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README +1 -1
  2. data/lib/rbyaml/composer.rb +28 -25
  3. data/lib/rbyaml/composer.rb.~1.2.~ +109 -0
  4. data/lib/rbyaml/constructor.rb +94 -84
  5. data/lib/rbyaml/constructor.rb.~1.2.~ +381 -0
  6. data/lib/rbyaml/dumper.rb +10 -17
  7. data/lib/rbyaml/dumper.rb.~1.2.~ +43 -0
  8. data/lib/rbyaml/emitter.rb +13 -26
  9. data/lib/rbyaml/emitter.rb.~1.2.~ +1116 -0
  10. data/lib/rbyaml/error.rb +15 -21
  11. data/lib/rbyaml/events.rb +29 -5
  12. data/lib/rbyaml/events.rb.~1.2.~ +93 -0
  13. data/lib/rbyaml/loader.rb +11 -23
  14. data/lib/rbyaml/loader.rb.~1.2.~ +52 -0
  15. data/lib/rbyaml/nodes.rb +13 -9
  16. data/lib/rbyaml/nodes.rb.~1.2.~ +52 -0
  17. data/lib/rbyaml/parser.rb +481 -343
  18. data/lib/rbyaml/parser.rb.old +531 -0
  19. data/lib/rbyaml/parser.rb.~1.2.~ +494 -0
  20. data/lib/rbyaml/reader.rb.~1.1.1.1.~ +127 -0
  21. data/lib/rbyaml/representer.rb +26 -17
  22. data/lib/rbyaml/representer.rb.~1.2.~ +239 -0
  23. data/lib/rbyaml/resolver.rb +15 -15
  24. data/lib/rbyaml/resolver.rb.~1.1.~ +163 -0
  25. data/lib/rbyaml/scanner.rb +457 -366
  26. data/lib/rbyaml/scanner.rb.~1.2.~ +1259 -0
  27. data/lib/rbyaml/serializer.rb +19 -17
  28. data/lib/rbyaml/serializer.rb.~1.2.~ +115 -0
  29. data/lib/rbyaml/tokens.rb +44 -4
  30. data/lib/rbyaml/tokens.rb.~1.2.~ +164 -0
  31. data/lib/rbyaml/util.rb +28 -0
  32. data/lib/rbyaml/yaml.rb +12 -12
  33. data/lib/rbyaml/yaml.rb.~1.2.~ +136 -0
  34. data/test/test_bm.rb +28 -0
  35. data/test/test_bm_syck.rb +28 -0
  36. data/test/test_invoke.rb +31 -0
  37. data/test/test_one.rb +5 -0
  38. data/test/test_profile.rb +32 -0
  39. data/test/test_rbyaml.rb +2 -1
  40. data/test/test_rbyaml.rb.~1.2.~ +31 -0
  41. data/test/test_time.rb +13 -8
  42. data/test/test_time.rb.~1.1.~ +29 -0
  43. data/test/yamlx.rb +3563 -0
  44. metadata +27 -2
@@ -0,0 +1,381 @@
1
+ require 'base64'
2
+ require 'set'
3
+
4
+ require 'rbyaml/error'
5
+ require 'rbyaml/nodes'
6
+ require 'rbyaml/composer'
7
+
8
+ module RbYAML
9
+ class ConstructorError < MarkedYAMLError
10
+ end
11
+
12
+ module BaseConstructor
13
+ include Composer
14
+
15
+ @@yaml_constructors = {}
16
+ @@yaml_multi_constructors = {}
17
+
18
+ def initialize_constructor
19
+ @constructed_objects = {}
20
+ @recursive_objects = {}
21
+ end
22
+
23
+ def check_data
24
+ # If there are more documents available?
25
+ check_node
26
+ end
27
+
28
+ def get_data
29
+ # Construct and return the next document.
30
+ construct_document(get_node) if check_node
31
+ end
32
+
33
+ def each_document
34
+ # Iterator protocol.
35
+ while check_node
36
+ yield construct_document(get_node)
37
+ end
38
+ end
39
+
40
+ def construct_document(node)
41
+ data = construct_object(node)
42
+ @constructed_objects = {}
43
+ @recursive_objects = {}
44
+ data
45
+ end
46
+
47
+ def construct_object(node)
48
+ return @constructed_objects[node] if @constructed_objects.include?(node)
49
+ raise ConstructorError.new(nil,nil,"found recursive nod",node.start_mark) if @recursive_objects.include?(node)
50
+ @recursive_objects[node] = nil
51
+ constructor = nil
52
+ ruby_cls = RbYAML::tagged_classes[node.tag]
53
+ if @@yaml_constructors.include?(node.tag)
54
+ constructor = @@yaml_constructors[node.tag]
55
+ elsif !ruby_cls.nil? && self.respond_to?(:construct_ruby_object) && (ruby_cls.method_defined?(:yaml_initialize) || ruby_cls.respond_to?(:yaml_new))
56
+ constructor = lambda { |node| send(:construct_ruby_object,RbYAML::tagged_classes[node.tag],node) }
57
+ else
58
+ through = false
59
+ for tag_prefix in @@yaml_multi_constructors.keys
60
+ through = true
61
+ if Regexp.new("^"+Regexp.escape(tag_prefix)) =~ node.tag
62
+ tag_suffix = node.tag[tag_prefix.length..-1]
63
+ if Symbol === @@yaml_multi_constructors[tag_prefix]
64
+ constructor = lambda { |node| send(@@yaml_multi_constructors[tag_prefix],tag_suffix, node) }
65
+ else
66
+ constructor = lambda { |node| @@yaml_multi_constructors[tag_prefix].call(tag_suffix, node) }
67
+ end
68
+ break
69
+ end
70
+ end
71
+ if !through
72
+ if @@yaml_multi_constructors.include?(nil)
73
+ if Symbol === @@yaml_multi_constructors[nil]
74
+ constructor = lambda { |node| send(@@yaml_multi_constructors[nil],node.tag, node) }
75
+ else
76
+ constructor = lambda { |node| @@yaml_multi_constructors[nil].call(node.tag, node) }
77
+ end
78
+ elsif @@yaml_constructors.include?(nil)
79
+ constructor = @@yaml_constructors[nil]
80
+ else
81
+ constructor = lambda { |node| construct_primitive(node) }
82
+ end
83
+ end
84
+ end
85
+ data = (Symbol === constructor) ? send(constructor,node) : constructor.call(node)
86
+ @constructed_objects[node] = data
87
+ @recursive_objects.delete(node)
88
+ data
89
+ end
90
+
91
+ def construct_primitive(node)
92
+ if ScalarNode === node
93
+ construct_scalar(node)
94
+ elsif SequenceNode === node
95
+ construct_sequence(node)
96
+ elsif MappingNode === node
97
+ construct_mapping(node)
98
+ else
99
+ puts node.tag
100
+ end
101
+ end
102
+
103
+ def construct_scalar(node)
104
+ if !ScalarNode === node
105
+ if MappingNode === node
106
+ for key_node in node.value.keys
107
+ if key_node.tag == "tag:yaml.org,2002:value"
108
+ return construct_scalar(node.value[key_node])
109
+ end
110
+ end
111
+ end
112
+ raise ConstructorError.new(nil, nil,"expected a scalar node, but found #{node.tid}",node.start_mark)
113
+ end
114
+ node.value
115
+ end
116
+
117
+ def construct_sequence(node)
118
+ raise ConstructorError.new(nil,nil,"expected a sequence node, but found #{node.tid}",node.start_mark) if !SequenceNode === node
119
+ sequence = []
120
+ for child in node.value
121
+ sequence << construct_object(child)
122
+ end
123
+ sequence
124
+ end
125
+
126
+ def construct_mapping(node)
127
+ raise ConstructorError.new(nil,nil,"expected a mapping node, but found #{node.tid}",node.start_mark) if !MappingNode === node
128
+ mapping = {}
129
+ merge = nil
130
+ for key_node in node.value.keys
131
+ if key_node.tag == "tag:yaml.org,2002:merge"
132
+ raise ConstructorError.new("while constructing a mapping", node.start_mark,"found duplicate merge key", key_node.start_mark) if !merge.nil?
133
+ value_node = node.value[key_node]
134
+ if MappingNode === value_node
135
+ merge = [construct_mapping(value_node)]
136
+ elsif SequenceNode === value_node
137
+ merge = []
138
+ for subnode in value_node.value
139
+ if !MappingNode === subnode
140
+ raise ConstructorError.new("while constructing a mapping",node.start_mark,"expected a mapping for merging, but found #{subnode.tid}", subnode.start_mark)
141
+ end
142
+ merge << construct_mapping(subnode)
143
+ end
144
+ merge.reverse!
145
+ else
146
+ raise ConstructorError.new("while constructing a mapping", node.start_mark,"expected a mapping or list of mappings for merging, but found #{value_node.tid}", value_node.start_mark)
147
+ end
148
+ elsif key_node.tag == "tag:yaml.org,2002:value"
149
+ raise ConstructorError.new("while construction a mapping", node.start_mark,"found duplicate value key", key_node.start_mark) if mapping.include?("=")
150
+ value = construct_object(node.value[key_node])
151
+ mapping["="] = value
152
+
153
+ else
154
+ key = construct_object(key_node)
155
+ # raise ConstructorError.new("while constructing a mapping", node.start_mark,"found duplicate key", key_node.start_mark) if mapping.include?(key)
156
+ end
157
+ value = construct_object(node.value[key_node])
158
+ mapping[key] = value
159
+ end
160
+ if !merge.nil?
161
+ merge << mapping
162
+ mapping = {}
163
+ for submapping in merge
164
+ mapping.merge!(submapping)
165
+ end
166
+ end
167
+ mapping
168
+ end
169
+
170
+ def construct_pairs(node)
171
+ raise ConstructorError.new(nil,nil,"expected a mapping node, but found #{node.tid}",node.start_mark) if !MappingNode === node
172
+ pairs = []
173
+ for key_node in node.value.keys
174
+ key = construct_object(key_node)
175
+ value = construct_object(node.value[key_node])
176
+ pairs << [key, value]
177
+ end
178
+ pairs
179
+ end
180
+
181
+ def self.add_constructor(tag, constructor)
182
+ @@yaml_constructors[tag] = constructor
183
+ end
184
+
185
+ def self.add_multi_constructor(tag_prefix, multi_constructor)
186
+ @@yaml_multi_constructors[tag_prefix] = multi_constructor
187
+ end
188
+ end
189
+
190
+ module SafeConstructor
191
+ include BaseConstructor
192
+
193
+ def construct_yaml_null(node)
194
+ construct_scalar(node)
195
+ nil
196
+ end
197
+
198
+ BOOL_VALUES = {
199
+ "y" => true,
200
+ "n" => false,
201
+ "yes" => true,
202
+ "no" => false,
203
+ "true" => true,
204
+ "false" => false,
205
+ "on" => true,
206
+ "off" => false
207
+ }
208
+
209
+ def construct_yaml_bool(node)
210
+ value = construct_scalar(node)
211
+ SafeConstructor::BOOL_VALUES[value.downcase]
212
+ end
213
+
214
+ def construct_yaml_int(node)
215
+ value = construct_scalar(node).to_s
216
+ value = value.gsub(/_/, '')
217
+ sign = +1
218
+ sign = -1 if value[0] == ?-
219
+ value = value[1..-1] if "+-".include?(value[0])
220
+ if value == "0"
221
+ return 0
222
+ elsif value[0..1] == "0b"
223
+ return sign*value[2..-1].to_i(2)
224
+ elsif value[0..1] == "0x"
225
+ return sign*value[2..-1].to_i(16)
226
+ elsif value[0] == ?0
227
+ return sign*value[1..-1].to_i(8)
228
+ elsif value.include?(?:)
229
+ digits = (value.split(/:/).map {|val| val.to_i}).reverse
230
+ base = 1
231
+ value = 0
232
+ for digit in digits
233
+ value += digit*base
234
+ base *= 60
235
+ end
236
+ return sign*value
237
+ else
238
+ return sign*value.to_i
239
+ end
240
+ end
241
+
242
+ INF_VALUE = +1.0/0.0
243
+ NAN_VALUE = 0.0/0.0
244
+
245
+ def construct_yaml_float(node)
246
+ value = construct_scalar(node).to_s
247
+ value = value.gsub(/_/, '')
248
+ sign = +1
249
+ sign = -1 if value[0] == ?-
250
+ value = value[1..-1] if "+-".include?(value[0])
251
+ if value.downcase == ".inf"
252
+ return sign*SafeConstructor::INF_VALUE
253
+ elsif value.downcase == ".nan"
254
+ return SafeConstructor::NAN_VALUE
255
+ elsif value.include?(?:)
256
+ digits = (value.split(/:/).map {|val| val.to_f}).reverse
257
+ base = 1
258
+ value = 0.0
259
+ for digit in digits
260
+ value += digit*base
261
+ base *= 60
262
+ end
263
+ return sign*value
264
+ else
265
+ return value.to_f
266
+ end
267
+ end
268
+
269
+ def construct_yaml_binary(node)
270
+ value = construct_scalar(node)
271
+ Base64.decode64(value.to_s)
272
+ end
273
+
274
+ TIMESTAMP_REGEXP = /^([0-9][0-9][0-9][0-9])-([0-9][0-9]?)-([0-9][0-9]?)(?:(?:[Tt]|[ \t]+)([0-9][0-9]?):([0-9][0-9]):([0-9][0-9])(?:\.([0-9]*))?(?:[ \t]*(?:Z|([-+][0-9][0-9]?)(?::([0-9][0-9])?)?))?)?$/
275
+
276
+ def construct_yaml_timestamp(node)
277
+ value = construct_scalar(node)
278
+ match = SafeConstructor::TIMESTAMP_REGEXP.match(node.value)
279
+ values = match.captures.map {|val| val.to_i}
280
+ fraction = values[6]
281
+ if fraction != 0
282
+ fraction *= 10 while 10*fraction < 1000
283
+ values[6] = fraction
284
+ end
285
+ stamp = Time.gm(values[0],values[1],values[2],values[3],values[4],values[5],values[6])
286
+
287
+ diff = values[7] * 3600 + values[8] * 60
288
+ return stamp-diff
289
+ end
290
+
291
+ def construct_yaml_omap(node)
292
+ # Note: we do not check for duplicate keys, because its too
293
+ # CPU-expensive.
294
+ raise ConstructorError.new("while constructing an ordered map", node.start_mark,
295
+ "expected a sequence, but found #{node.tid}", node.start_mark) if !SequenceNode === node
296
+ omap = []
297
+ for subnode in node.value
298
+ raise ConstructorError.new("while constructing an ordered map", node.start_mark,
299
+ "expected a mapping of length 1, but found #{subnode.tid}",subnode.start_mark) if !MappingNode === subnode
300
+ raise ConstructorError.new("while constructing an ordered map", node.start_mark,
301
+ "expected a single mapping item, but found #{subnode.value.length} items",subnode.start_mark) if subnode.value.length != 1
302
+ key_node = subnode.value.keys[0]
303
+ key = construct_object(key_node)
304
+ value = construct_object(subnode.value[key_node])
305
+ omap << [key, value]
306
+ end
307
+ omap
308
+ end
309
+
310
+ def construct_yaml_pairs(node)
311
+ construct_yaml_omap(node)
312
+ end
313
+
314
+ def construct_yaml_set(node)
315
+ Set.new(construct_mapping(node).keys)
316
+ end
317
+
318
+ def construct_yaml_str(node)
319
+ construct_scalar(node).to_s
320
+ end
321
+
322
+ def construct_yaml_seq(node)
323
+ construct_sequence(node)
324
+ end
325
+
326
+ def construct_yaml_map(node)
327
+ construct_mapping(node)
328
+ end
329
+
330
+ def construct_yaml_object(node, cls)
331
+ mapping = construct_mapping(node)
332
+ data = cls.new
333
+ mapping.each {|key,val| data.instance_variable_set("@#{key}",val)}
334
+ data
335
+ end
336
+
337
+ def construct_undefined(node)
338
+ raise ConstructorError.new(nil,nil,"could not determine a constructor for the tag #{node.tag}",node.start_mark)
339
+ end
340
+
341
+ def construct_ruby_object(cls,node)
342
+ val = construct_primitive(node)
343
+ if cls.respond_to?(:yaml_new)
344
+ obj = cls.yaml_new(cls,node.tag,val)
345
+ else
346
+ obj = cls.allocate
347
+ obj.yaml_initialize(node.tag,val)
348
+ end
349
+ obj
350
+ end
351
+
352
+ def construct_ruby(tag,node)
353
+ obj_class = Object
354
+ tag.split( "::" ).each { |c| obj_class = obj_class.const_get( c ) } if tag
355
+ o = obj_class.allocate
356
+ mapping = construct_mapping(node)
357
+ mapping.each {|key,val| o.instance_variable_set("@#{key}",val)}
358
+ o
359
+ end
360
+ end
361
+
362
+ BaseConstructor::add_constructor('tag:yaml.org,2002:null',:construct_yaml_null)
363
+ BaseConstructor::add_constructor('tag:yaml.org,2002:bool',:construct_yaml_bool)
364
+ BaseConstructor::add_constructor('tag:yaml.org,2002:int',:construct_yaml_int)
365
+ BaseConstructor::add_constructor('tag:yaml.org,2002:float',:construct_yaml_float)
366
+ BaseConstructor::add_constructor('tag:yaml.org,2002:binary',:construct_yaml_binary)
367
+ BaseConstructor::add_constructor('tag:yaml.org,2002:timestamp',:construct_yaml_timestamp)
368
+ BaseConstructor::add_constructor('tag:yaml.org,2002:omap',:construct_yaml_omap)
369
+ BaseConstructor::add_constructor('tag:yaml.org,2002:pairs',:construct_yaml_pairs)
370
+ BaseConstructor::add_constructor('tag:yaml.org,2002:set',:construct_yaml_set)
371
+ BaseConstructor::add_constructor('tag:yaml.org,2002:str',:construct_yaml_str)
372
+ BaseConstructor::add_constructor('tag:yaml.org,2002:seq',:construct_yaml_seq)
373
+ BaseConstructor::add_constructor('tag:yaml.org,2002:map',:construct_yaml_map)
374
+ BaseConstructor::add_constructor(nil,:construct_undefined)
375
+
376
+ BaseConstructor::add_multi_constructor("!ruby/object:",:construct_ruby)
377
+
378
+ module Constructor
379
+ include SafeConstructor
380
+ end
381
+ end
@@ -5,39 +5,32 @@ require 'rbyaml/resolver'
5
5
 
6
6
  module RbYAML
7
7
  class CommonDumper
8
- include Emitter, Serializer
9
-
10
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
8
+ attr_accessor :emitter, :serializer, :representer, :resolver
9
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=Representer,resolver=Resolver)
11
10
  super()
12
- initialize_emitter(stream,canonical,indent,width,line_break)
13
- initialize_serializer(explicit_start,explicit_end,version,tags)
11
+ @emitter = emitter.new(stream,canonical,indent,width,line_break)
12
+ @resolver = resolver.new
13
+ @serializer = serializer.new(@emitter,@resolver,explicit_start,explicit_end,version,tags)
14
+ @representer = representer.new(@serializer,default_style,default_flow_style)
14
15
  end
15
16
  end
16
17
 
17
18
  class BaseDumper < CommonDumper
18
- include BaseRepresenter, BaseResolver
19
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
19
+ attr_accessor
20
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=BaseRepresenter,resolver=BaseResolver)
20
21
  super
21
- initialize_representer(default_style,default_flow_style)
22
- initialize_resolver
23
22
  end
24
23
  end
25
24
 
26
25
  class SafeDumper < CommonDumper
27
- include SafeRepresenter, Resolver
28
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
26
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=SafeRepresenter,resolver=Resolver)
29
27
  super
30
- initialize_representer(default_style,default_flow_style)
31
- initialize_resolver
32
28
  end
33
29
  end
34
30
 
35
31
  class Dumper < CommonDumper
36
- include Representer, Resolver
37
- def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
32
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil,emitter=Emitter,serializer=Serializer,representer=Representer,resolver=Resolver)
38
33
  super
39
- initialize_representer(default_style,default_flow_style)
40
- initialize_resolver
41
34
  end
42
35
  end
43
36
  end
@@ -0,0 +1,43 @@
1
+ require 'rbyaml/emitter'
2
+ require 'rbyaml/serializer'
3
+ require 'rbyaml/representer'
4
+ require 'rbyaml/resolver'
5
+
6
+ module RbYAML
7
+ class CommonDumper
8
+ include Emitter, Serializer
9
+
10
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
11
+ super()
12
+ initialize_emitter(stream,canonical,indent,width,line_break)
13
+ initialize_serializer(explicit_start,explicit_end,version,tags)
14
+ end
15
+ end
16
+
17
+ class BaseDumper < CommonDumper
18
+ include BaseRepresenter, BaseResolver
19
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
20
+ super
21
+ initialize_representer(default_style,default_flow_style)
22
+ initialize_resolver
23
+ end
24
+ end
25
+
26
+ class SafeDumper < CommonDumper
27
+ include SafeRepresenter, Resolver
28
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
29
+ super
30
+ initialize_representer(default_style,default_flow_style)
31
+ initialize_resolver
32
+ end
33
+ end
34
+
35
+ class Dumper < CommonDumper
36
+ include Representer, Resolver
37
+ def initialize(stream,default_style=nil,default_flow_style=nil,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
38
+ super
39
+ initialize_representer(default_style,default_flow_style)
40
+ initialize_resolver
41
+ end
42
+ end
43
+ end