RbYAML 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rbyaml.rb +14 -256
- data/lib/rbyaml.rb.~1.2.~ +383 -0
- data/lib/rbyaml/composer.rb +9 -11
- data/lib/rbyaml/{composer.rb.~1.2.~ → composer.rb.~1.3.~} +28 -25
- data/lib/rbyaml/constants.rb +95 -0
- data/lib/rbyaml/constructor.rb +180 -89
- data/lib/rbyaml/{constructor.rb.~1.2.~ → constructor.rb.~1.9.~} +137 -95
- data/lib/rbyaml/dumper.rb +12 -9
- data/lib/rbyaml/dumper.rb.~1.3.~ +36 -0
- data/lib/rbyaml/emitter.rb +14 -28
- data/lib/rbyaml/{emitter.rb.~1.2.~ → emitter.rb.~1.6.~} +22 -33
- data/lib/rbyaml/error.rb +4 -57
- data/lib/rbyaml/error.rb.~1.2.~ +75 -0
- data/lib/rbyaml/events.rb +8 -14
- data/lib/rbyaml/{events.rb.~1.2.~ → events.rb.~1.4.~} +29 -6
- data/lib/rbyaml/nodes.rb +5 -5
- data/lib/rbyaml/{nodes.rb.~1.2.~ → nodes.rb.~1.3.~} +13 -9
- data/lib/rbyaml/parser.rb +70 -108
- data/lib/rbyaml/parser.rb.~1.4.~ +632 -0
- data/lib/rbyaml/representer.rb +19 -157
- data/lib/rbyaml/representer.rb.old +317 -0
- data/lib/rbyaml/{representer.rb.~1.2.~ → representer.rb.~1.5.~} +60 -26
- data/lib/rbyaml/resolver.rb +6 -6
- data/lib/rbyaml/{resolver.rb.~1.1.~ → resolver.rb.~1.6.~} +20 -20
- data/lib/rbyaml/rubytypes.rb +391 -0
- data/lib/rbyaml/scanner.rb +123 -225
- data/lib/rbyaml/{scanner.rb.~1.2.~ → scanner.rb.~1.5.~} +466 -378
- data/lib/rbyaml/serializer.rb +9 -9
- data/lib/rbyaml/{serializer.rb.~1.2.~ → serializer.rb.~1.4.~} +19 -17
- data/lib/rbyaml/stream.rb +48 -0
- data/lib/rbyaml/tag.rb +72 -0
- data/lib/rbyaml/tokens.rb +22 -16
- data/lib/rbyaml/{tokens.rb.~1.2.~ → tokens.rb.~1.3.~} +44 -4
- data/lib/rbyaml/types.rb +146 -0
- data/lib/rbyaml/util.rb.~1.3.~ +38 -0
- data/lib/rbyaml/yaml.rb +22 -32
- data/lib/rbyaml/{yaml.rb.~1.2.~ → yaml.rb.~1.5.~} +17 -17
- data/test/load_one.rb +6 -0
- data/test/load_one_yaml.rb +6 -0
- data/test/output_events.rb +9 -0
- data/test/test_add_ctor.rb +51 -0
- data/test/test_add_ctor.rb.~1.1.~ +30 -0
- data/test/test_bm.rb +2 -2
- data/test/test_bm.rb.~1.1.~ +28 -0
- data/test/test_gems.rb +10 -0
- data/test/test_one.rb.~1.1.~ +5 -0
- data/test/test_one_syck.rb +5 -0
- data/test/test_rbyaml.rb +63 -32
- data/test/test_rbyaml.rb.~1.6.~ +59 -0
- data/test/{test_rbyaml.rb.~1.2.~ → test_rbyaml_old.rb} +13 -4
- data/test/test_time_events.rb +24 -0
- data/test/test_time_nodes.rb +24 -0
- data/test/test_time_tokens.rb +24 -0
- data/test/yaml/gems_new.yml +147456 -0
- data/test/yaml/test1.rb +8 -0
- data/test/yaml/test10.rb +14 -0
- data/test/yaml/test11.rb +13 -0
- data/test/yaml/test12.rb +9 -0
- data/test/yaml/test13.rb +9 -0
- data/test/yaml/test14.rb +13 -0
- data/test/yaml/test15.rb +12 -0
- data/test/yaml/test16.rb +11 -0
- data/test/yaml/test16.rb.~1.1.~ +11 -0
- data/test/yaml/test17.rb +10 -0
- data/test/yaml/test18.rb +13 -0
- data/test/yaml/test19.rb +9 -0
- data/test/yaml/test19.yml +1 -1
- data/test/yaml/test2.rb +8 -0
- data/test/yaml/test20.rb +11 -0
- data/test/yaml/test20.rb.~1.1.~ +9 -0
- data/test/yaml/test20.yml +1 -1
- data/test/yaml/test3.rb +13 -0
- data/test/yaml/test4.rb +13 -0
- data/test/yaml/test5.rb +8 -0
- data/test/yaml/test6.rb +10 -0
- data/test/yaml/test7.rb +15 -0
- data/test/yaml/test8.rb +15 -0
- data/test/yaml/test9.rb +13 -0
- metadata +61 -16
- data/lib/rbyaml/dumper.rb.~1.2.~ +0 -43
- data/lib/rbyaml/parser.rb.~1.2.~ +0 -494
@@ -8,11 +8,12 @@ module RbYAML
|
|
8
8
|
class RepresenterError < YAMLError
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
class BaseRepresenter
|
12
12
|
@@yaml_representers = {}
|
13
13
|
@@yaml_multi_representers = {}
|
14
14
|
|
15
|
-
def
|
15
|
+
def initialize(serializer, default_style=nil, default_flow_style=nil)
|
16
|
+
@serializer = serializer
|
16
17
|
@default_style = default_style
|
17
18
|
@default_flow_style = default_flow_style
|
18
19
|
@represented_objects = {}
|
@@ -20,7 +21,7 @@ module RbYAML
|
|
20
21
|
|
21
22
|
def represent(data)
|
22
23
|
node = represent_data(data)
|
23
|
-
serialize(node)
|
24
|
+
@serializer.serialize(node)
|
24
25
|
represented_objects = {}
|
25
26
|
end
|
26
27
|
|
@@ -93,14 +94,14 @@ module RbYAML
|
|
93
94
|
|
94
95
|
def represent_scalar(tag, value, style=nil)
|
95
96
|
style ||= @default_style
|
96
|
-
ScalarNode.new(tag,
|
97
|
+
ScalarNode.new(tag,value,nil,nil,style)
|
97
98
|
end
|
98
99
|
|
99
100
|
def represent_sequence(tag, sequence, flow_style=nil)
|
100
101
|
best_style = true
|
101
102
|
value = sequence.map {|item|
|
102
103
|
node_item = represent_data(item)
|
103
|
-
best_style = false if !
|
104
|
+
best_style = false if !node_item.__is_scalar && !node_item.flow_style
|
104
105
|
node_item
|
105
106
|
}
|
106
107
|
flow_style ||= (@flow_default_style || best_style)
|
@@ -114,8 +115,8 @@ module RbYAML
|
|
114
115
|
for item_key,item_value in mapping
|
115
116
|
node_key = represent_data(item_key)
|
116
117
|
node_value = represent_data(item_value)
|
117
|
-
best_style = false if !
|
118
|
-
best_style = false if !
|
118
|
+
best_style = false if !node_key.__is_scalar && !node_key.flow_style
|
119
|
+
best_style = false if !node_value.__is_scalar && !node_value.flow_style
|
119
120
|
value[node_key] = node_value
|
120
121
|
end
|
121
122
|
else
|
@@ -123,8 +124,8 @@ module RbYAML
|
|
123
124
|
for item_key, item_value in mapping
|
124
125
|
node_key = represent_data(item_key)
|
125
126
|
node_value = represent_data(item_value)
|
126
|
-
best_style = false if !
|
127
|
-
best_style = false if !
|
127
|
+
best_style = false if !node_key.__is_scalar && !node_key.flow_style
|
128
|
+
best_style = false if !node_value.__is_scalar && !node_value.flow_style
|
128
129
|
value << [node_key, node_value]
|
129
130
|
end
|
130
131
|
end
|
@@ -137,28 +138,39 @@ module RbYAML
|
|
137
138
|
end
|
138
139
|
end
|
139
140
|
|
140
|
-
|
141
|
-
include BaseRepresenter
|
141
|
+
class SafeRepresenter < BaseRepresenter
|
142
142
|
|
143
143
|
def ignore_aliases(data)
|
144
|
-
data.nil? ||
|
144
|
+
data.nil? || data.__is_str || TrueClass === data || FalseClass === data || data.__is_int || Float === data
|
145
145
|
end
|
146
146
|
|
147
147
|
def represent_none(data)
|
148
|
-
represent_scalar(data.taguri,"null")
|
148
|
+
# represent_scalar(data.taguri,"null")
|
149
|
+
represent_scalar('tag:yaml.org,2002:str',"")
|
149
150
|
end
|
150
151
|
|
152
|
+
NON_PRINTABLE = /[^\x09\x0A\x0D\x20-\x7E\x85\xA0-\xFF]/
|
151
153
|
def represent_str(data)
|
152
|
-
|
154
|
+
style = nil
|
155
|
+
if NON_PRINTABLE =~ data
|
156
|
+
data = Base64.encode64(data)
|
157
|
+
data.taguri ="tag:yaml.org,2002:binary"
|
158
|
+
style = "|"
|
159
|
+
end
|
160
|
+
represent_scalar(data.taguri,data,style)
|
153
161
|
end
|
154
162
|
|
155
163
|
def represent_symbol(data)
|
156
164
|
represent_scalar(data.taguri,data.to_s)
|
157
165
|
end
|
158
166
|
|
167
|
+
def represent_private_type(data)
|
168
|
+
represent_scalar(data.type_id,data.value)
|
169
|
+
end
|
170
|
+
|
159
171
|
def represent_bool(data)
|
160
172
|
value = data ? "true" : "false"
|
161
|
-
represent_scalar(
|
173
|
+
represent_scalar('tag:yaml.org,2002:bool',value)
|
162
174
|
end
|
163
175
|
|
164
176
|
def represent_int(data)
|
@@ -196,25 +208,46 @@ module RbYAML
|
|
196
208
|
end
|
197
209
|
|
198
210
|
def represent_datetime(data)
|
199
|
-
|
200
|
-
|
201
|
-
|
211
|
+
tz = "Z"
|
212
|
+
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
213
|
+
unless data.utc?
|
214
|
+
utc_same_instant = data.dup.utc
|
215
|
+
utc_same_writing = Time.utc(data.year,data.month,data.day,data.hour,data.min,data.sec,data.usec)
|
216
|
+
difference_to_utc = utc_same_writing - utc_same_instant
|
217
|
+
if (difference_to_utc < 0)
|
218
|
+
difference_sign = '-'
|
219
|
+
absolute_difference = -difference_to_utc
|
220
|
+
else
|
221
|
+
difference_sign = '+'
|
222
|
+
absolute_difference = difference_to_utc
|
223
|
+
end
|
224
|
+
difference_minutes = (absolute_difference/60).round
|
225
|
+
tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
|
202
226
|
end
|
203
|
-
|
204
|
-
|
227
|
+
standard = data.strftime( "%Y-%m-%d %H:%M:%S" )
|
228
|
+
standard += ".%06d" % [data.usec] if data.usec.nonzero?
|
229
|
+
standard += " %s" % [tz]
|
230
|
+
represent_scalar(data.taguri, standard)
|
231
|
+
end
|
232
|
+
|
233
|
+
def represent_ruby(data)
|
234
|
+
state = data.to_yaml_properties
|
235
|
+
map = {}
|
236
|
+
state.each do |m|
|
237
|
+
map[m[1..-1]] = data.instance_variable_get(m)
|
205
238
|
end
|
206
|
-
|
239
|
+
represent_mapping("!ruby/object:#{data.class.yaml_tag_class_name}", map,nil)
|
207
240
|
end
|
208
241
|
|
209
242
|
def represent_yaml_object(tag, data, flow_style=nil)
|
210
243
|
state = data.to_yaml_properties
|
211
|
-
|
244
|
+
map = {}
|
212
245
|
state.each do |m|
|
213
246
|
map[m[1..-1]] = data.instance_variable_get(m)
|
214
247
|
end
|
215
|
-
represent_mapping(tag,
|
248
|
+
represent_mapping(tag, map, flow_style)
|
216
249
|
end
|
217
|
-
|
250
|
+
|
218
251
|
def represent_undefined(data)
|
219
252
|
raise RepresenterError.new("cannot represent an object: #{data}")
|
220
253
|
end
|
@@ -231,9 +264,10 @@ module RbYAML
|
|
231
264
|
BaseRepresenter.add_representer(Hash,:represent_dict)
|
232
265
|
BaseRepresenter.add_representer(Set,:represent_set)
|
233
266
|
BaseRepresenter.add_representer(Time,:represent_datetime)
|
267
|
+
BaseRepresenter.add_representer(PrivateType,:represent_private_type)
|
268
|
+
BaseRepresenter.add_representer(Object,:represent_ruby)
|
234
269
|
BaseRepresenter.add_representer(nil,:represent_undefined)
|
235
270
|
|
236
|
-
|
237
|
-
include SafeRepresenter
|
271
|
+
class Representer < SafeRepresenter
|
238
272
|
end
|
239
273
|
end
|
data/lib/rbyaml/resolver.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rbyaml/nodes'
|
|
3
3
|
require 'rbyaml/error'
|
4
4
|
|
5
5
|
module RbYAML
|
6
|
-
class ResolverError <
|
6
|
+
class ResolverError < YAMLError
|
7
7
|
end
|
8
8
|
|
9
9
|
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
@@ -11,7 +11,7 @@ module RbYAML
|
|
11
11
|
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
12
12
|
|
13
13
|
class BaseResolver
|
14
|
-
@@yaml_implicit_resolvers = {}
|
14
|
+
@@yaml_implicit_resolvers = {""=>[['tag:yaml.org,2002:null',/^$/]]}
|
15
15
|
@@yaml_path_resolvers = {}
|
16
16
|
|
17
17
|
def initialize
|
@@ -86,7 +86,7 @@ module RbYAML
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
else
|
89
|
-
for path, kind in @@yaml_path_resolvers
|
89
|
+
for path, kind in @@yaml_path_resolvers.keys
|
90
90
|
if !path
|
91
91
|
exact_paths[kind] = @@yaml_path_resolvers[[path, kind]]
|
92
92
|
else
|
@@ -149,11 +149,11 @@ module RbYAML
|
|
149
149
|
class Resolver < BaseResolver
|
150
150
|
end
|
151
151
|
|
152
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:bool',/^(?:
|
153
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:float',/^(?:[-+]?(?:[0-9][0-9_]*)
|
152
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:bool',/^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$/,'yYnNtTfFoO')
|
153
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:float',/^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]*)?\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN))$/,'-+0123456789.')
|
154
154
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:int',/^(?:[-+]?0b[0-1_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[0-9a-fA-F_]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$/,'-+0123456789')
|
155
155
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:merge',/^(?:<<)$/,'<')
|
156
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:null',/^(
|
156
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:null',/^(?:~|null|Null|NULL| )$/,'~nN' + ?\0.chr)
|
157
157
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:timestamp',/^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[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])?))?)$/,'0123456789')
|
158
158
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:value',/^(?:=)$/,'=')
|
159
159
|
# The following implicit resolver is only for documentation purposes. It cannot work
|
@@ -1,19 +1,20 @@
|
|
1
|
+
require 'rbyaml/util'
|
1
2
|
require 'rbyaml/nodes'
|
2
3
|
require 'rbyaml/error'
|
3
4
|
|
4
5
|
module RbYAML
|
5
|
-
class ResolverError <
|
6
|
+
class ResolverError < YAMLError
|
6
7
|
end
|
7
8
|
|
8
9
|
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
9
10
|
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
|
10
11
|
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
11
12
|
|
12
|
-
|
13
|
+
class BaseResolver
|
13
14
|
@@yaml_implicit_resolvers = {}
|
14
15
|
@@yaml_path_resolvers = {}
|
15
16
|
|
16
|
-
def
|
17
|
+
def initialize
|
17
18
|
@resolver_exact_paths = []
|
18
19
|
@resolver_prefix_paths = []
|
19
20
|
end
|
@@ -31,7 +32,7 @@ module RbYAML
|
|
31
32
|
def self.add_path_resolver(tag, path, kind=nil)
|
32
33
|
new_path = []
|
33
34
|
for element in path
|
34
|
-
if
|
35
|
+
if element.__is_a
|
35
36
|
if element.length == 2
|
36
37
|
node_check, index_check = element
|
37
38
|
elsif element.length == 1
|
@@ -50,10 +51,10 @@ module RbYAML
|
|
50
51
|
node_check = SequenceNode
|
51
52
|
elsif Hash == node_check
|
52
53
|
node_check = MappingNode
|
53
|
-
elsif ![ScalarNode, SequenceNode, MappingNode].include?(node_check) && !
|
54
|
+
elsif ![ScalarNode, SequenceNode, MappingNode].include?(node_check) && !node_check.__is_sym && !node_check.nil?
|
54
55
|
raise ResolverError.new("Invalid node checker: #{node_check}")
|
55
56
|
end
|
56
|
-
if !(
|
57
|
+
if !(index_check.__is_str || index_check.__is_int) && !index_check.nil?
|
57
58
|
raise ResolverError.new("Invalid index checker: #{index_check}")
|
58
59
|
end
|
59
60
|
new_path << [node_check, index_check]
|
@@ -85,7 +86,7 @@ module RbYAML
|
|
85
86
|
end
|
86
87
|
end
|
87
88
|
else
|
88
|
-
for path, kind in @@yaml_path_resolvers
|
89
|
+
for path, kind in @@yaml_path_resolvers.keys
|
89
90
|
if !path
|
90
91
|
exact_paths[kind] = @@yaml_path_resolvers[[path, kind]]
|
91
92
|
else
|
@@ -104,16 +105,16 @@ module RbYAML
|
|
104
105
|
|
105
106
|
def check_resolver_prefix(depth, path, kind, current_node, current_index)
|
106
107
|
node_check, index_check = path[depth-1]
|
107
|
-
if
|
108
|
+
if node_check.__is_str
|
108
109
|
return false if current_node.tag != node_check
|
109
110
|
elsif !node_check.nil?
|
110
111
|
return false if !node_check === current_node
|
111
112
|
end
|
112
113
|
return false if index_check==true && !current_index.nil?
|
113
114
|
return false if !index_check && current_index.nil?
|
114
|
-
if
|
115
|
-
return false if !(
|
116
|
-
elsif
|
115
|
+
if index_check.__is_str
|
116
|
+
return false if !(current_index.__is_scalar && index_check == current_index.value)
|
117
|
+
elsif index_check.__is_int
|
117
118
|
return false if index_check != current_index
|
118
119
|
end
|
119
120
|
true
|
@@ -122,11 +123,11 @@ module RbYAML
|
|
122
123
|
def resolve(kind, value, implicit)
|
123
124
|
if ScalarNode == kind && implicit[0]
|
124
125
|
if value == ""
|
125
|
-
resolvers = @@yaml_implicit_resolvers.fetch("",
|
126
|
+
resolvers = @@yaml_implicit_resolvers.fetch("",[])
|
126
127
|
else
|
127
|
-
resolvers = @@yaml_implicit_resolvers.fetch(value[0],
|
128
|
+
resolvers = @@yaml_implicit_resolvers.fetch(value[0],[])
|
128
129
|
end
|
129
|
-
resolvers += @@yaml_implicit_resolvers.fetch(nil,
|
130
|
+
resolvers += @@yaml_implicit_resolvers.fetch(nil,[])
|
130
131
|
for tag, regexp in resolvers
|
131
132
|
return tag if regexp =~ value
|
132
133
|
end
|
@@ -140,20 +141,19 @@ module RbYAML
|
|
140
141
|
elsif SequenceNode == kind
|
141
142
|
return RbYAML::DEFAULT_SEQUENCE_TAG
|
142
143
|
elsif MappingNode == kind
|
143
|
-
|
144
|
+
return RbYAML::DEFAULT_MAPPING_TAG
|
144
145
|
end
|
145
146
|
end
|
146
147
|
end
|
147
148
|
|
148
|
-
|
149
|
-
include BaseResolver
|
149
|
+
class Resolver < BaseResolver
|
150
150
|
end
|
151
151
|
|
152
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:bool',/^(?:
|
153
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:float',/^(?:[-+]?(?:[0-9][0-9_]*)
|
152
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:bool',/^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$/,'yYnNtTfFoO')
|
153
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:float',/^(?:[-+]?(?:[0-9][0-9_]*)\.[0-9_]*(?:[eE][-+][0-9]+)?|[-+]?(?:[0-9][0-9_]*)?\.[0-9_]+(?:[eE][-+][0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\.[0-9_]*|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN))$/,'-+0123456789.')
|
154
154
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:int',/^(?:[-+]?0b[0-1_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[0-9a-fA-F_]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$/,'-+0123456789')
|
155
155
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:merge',/^(?:<<)$/,'<')
|
156
|
-
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:null',/^(
|
156
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:null',/^(?:~|null|Null|NULL| )$/,'~nN' + ?\0.chr)
|
157
157
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:timestamp',/^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[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])?))?)$/,'0123456789')
|
158
158
|
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:value',/^(?:=)$/,'=')
|
159
159
|
# The following implicit resolver is only for documentation purposes. It cannot work
|
@@ -0,0 +1,391 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class Class
|
4
|
+
def to_yaml( opts = {} )
|
5
|
+
raise RbYAML::TypeError, "can't dump anonymous class %s" % self.class
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Object
|
10
|
+
# yaml_as "tag:ruby.yaml.org,2002:object"
|
11
|
+
def is_complex_yaml?; true; end
|
12
|
+
def to_yaml_style; end
|
13
|
+
def to_yaml_properties; instance_variables.sort; end
|
14
|
+
def to_yaml( opts = {} )
|
15
|
+
RbYAML::dump(self,nil,opts)
|
16
|
+
end
|
17
|
+
def taguri
|
18
|
+
"!ruby/object:#{self.class.name}"
|
19
|
+
end
|
20
|
+
def to_yaml_node( repre )
|
21
|
+
RbYAML::quick_emit_node( object_id, repre ) do |out|
|
22
|
+
mep = {}
|
23
|
+
to_yaml_properties.each do |m|
|
24
|
+
mep[m[1..-1]] = instance_variable_get(m)
|
25
|
+
end
|
26
|
+
out.map(taguri,mep,to_yaml_style)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class Hash
|
32
|
+
yaml_as "tag:ruby.yaml.org,2002:hash"
|
33
|
+
yaml_as "tag:yaml.org,2002:map"
|
34
|
+
def is_complex_yaml?; true; end
|
35
|
+
def yaml_initialize( tag, val )
|
36
|
+
if Array === val
|
37
|
+
update Hash.[]( *val ) # Convert the map to a sequence
|
38
|
+
elsif Hash === val
|
39
|
+
update val
|
40
|
+
else
|
41
|
+
raise RbYAML::TypeError, "Invalid map explicitly tagged #{ tag }: " + val.inspect
|
42
|
+
end
|
43
|
+
end
|
44
|
+
def to_yaml_node(repre)
|
45
|
+
RbYAML::quick_emit_node(object_id, repre) {|rep|
|
46
|
+
rep.map(taguri,self,to_yaml_style)
|
47
|
+
}
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class Struct
|
52
|
+
yaml_as "tag:ruby.yaml.org,2002:struct"
|
53
|
+
def self.yaml_tag_class_name; self.name.gsub( "Struct::", "" ); end
|
54
|
+
def self.yaml_tag_read_class( name ); "Struct::#{ name }"; end
|
55
|
+
def self.yaml_new( klass, tag, val )
|
56
|
+
if Hash === val
|
57
|
+
struct_type = nil
|
58
|
+
|
59
|
+
#
|
60
|
+
# Use existing Struct if it exists
|
61
|
+
#
|
62
|
+
props = {}
|
63
|
+
val.delete_if { |k,v| props[k] = v if k =~ /^@/ }
|
64
|
+
begin
|
65
|
+
struct_name, struct_type = RbYAML.read_type_class( tag, Struct )
|
66
|
+
rescue NameError
|
67
|
+
end
|
68
|
+
if !struct_type
|
69
|
+
struct_def = [ tag.split( /:/, 4 ).last ]
|
70
|
+
struct_type = Struct.new( *struct_def.concat( val.keys.collect { |k| k.intern } ) )
|
71
|
+
end
|
72
|
+
|
73
|
+
#
|
74
|
+
# Set the Struct properties
|
75
|
+
#
|
76
|
+
st = RbYAML::object_maker( struct_type, {} )
|
77
|
+
st.members.each do |m|
|
78
|
+
st.send( "#{m}=", val[m] )
|
79
|
+
end
|
80
|
+
props.each do |k,v|
|
81
|
+
st.instance_variable_set(k, v)
|
82
|
+
end
|
83
|
+
st
|
84
|
+
else
|
85
|
+
raise RbYAML::TypeError, "Invalid Ruby Struct: " + val.inspect
|
86
|
+
end
|
87
|
+
end
|
88
|
+
def to_yaml_node( repr)
|
89
|
+
RbYAML::quick_emit_node( object_id, repr ) do |out|
|
90
|
+
mep = {}
|
91
|
+
self.members.each do |m|
|
92
|
+
mep[m] = self[m]
|
93
|
+
end
|
94
|
+
self.to_yaml_properties.each do |m|
|
95
|
+
mep[m]=instance_variable_get( m )
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Basic struct is passed as a YAML map
|
100
|
+
#
|
101
|
+
out.map( taguri, mep, to_yaml_style )
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class Array
|
107
|
+
yaml_as "tag:ruby.yaml.org,2002:array"
|
108
|
+
yaml_as "tag:yaml.org,2002:seq"
|
109
|
+
def is_complex_yaml?; true; end
|
110
|
+
def yaml_initialize( tag, val ); concat( val.to_a ); end
|
111
|
+
def to_yaml_node(repre)
|
112
|
+
RbYAML::quick_emit_node(object_id, repre) {|rep|
|
113
|
+
rep.seq(taguri,self,to_yaml_style)
|
114
|
+
}
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Exception
|
119
|
+
yaml_as "tag:ruby.yaml.org,2002:exception"
|
120
|
+
def Exception.yaml_new( klass, tag, val )
|
121
|
+
o = RbYAML.object_maker( klass, { 'mesg' => val.delete( 'message' ) } )
|
122
|
+
val.each_pair do |k,v|
|
123
|
+
o.instance_variable_set("@#{k}", v)
|
124
|
+
end
|
125
|
+
o
|
126
|
+
end
|
127
|
+
def to_yaml_node( repr )
|
128
|
+
RbYAML::quick_emit_node( object_id, repr ) do |out|
|
129
|
+
mep = {}
|
130
|
+
mep['message'] = message
|
131
|
+
to_yaml_properties.each do |m|
|
132
|
+
mep[m[1..-1]] = instance_variable_get( m )
|
133
|
+
end
|
134
|
+
out.map( taguri, mep, to_yaml_style )
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
class String
|
140
|
+
yaml_as "tag:ruby.yaml.org,2002:string"
|
141
|
+
yaml_as "tag:yaml.org,2002:binary"
|
142
|
+
yaml_as "tag:yaml.org,2002:str"
|
143
|
+
def is_complex_yaml?
|
144
|
+
to_yaml_style or not to_yaml_properties.empty? or self =~ /\n.+/
|
145
|
+
end
|
146
|
+
def is_binary_data?
|
147
|
+
( self.count( "^ -~", "^\r\n" ) / self.size > 0.3 || self.count( "\x00" ) > 0 ) unless empty?
|
148
|
+
end
|
149
|
+
def String.yaml_new( klass, tag, val )
|
150
|
+
val = val.unpack("m")[0] if tag == "tag:yaml.org,2002:binary"
|
151
|
+
val = { 'str' => val } if String === val
|
152
|
+
if Hash === val
|
153
|
+
s = klass.allocate
|
154
|
+
# Thank you, NaHi
|
155
|
+
String.instance_method(:initialize).
|
156
|
+
bind(s).
|
157
|
+
call( val.delete( 'str' ) )
|
158
|
+
val.each { |k,v| s.instance_variable_set( k, v ) }
|
159
|
+
s
|
160
|
+
else
|
161
|
+
raise RbYAML::TypeError, "Invalid String: " + val.inspect
|
162
|
+
end
|
163
|
+
end
|
164
|
+
def to_yaml_node(repre)
|
165
|
+
RbYAML::quick_emit_node( is_complex_yaml? ? object_id : nil, repre ) do |rep|
|
166
|
+
if is_binary_data?
|
167
|
+
rep.scalar( "tag:yaml.org,2002:binary", [self].pack("m"), "|")
|
168
|
+
elsif to_yaml_properties.empty?
|
169
|
+
rep.scalar(taguri, self, self =~ /^:/ ? '"' : to_yaml_style )
|
170
|
+
else
|
171
|
+
mep = {}
|
172
|
+
mep['str'] = "#{self}"
|
173
|
+
to_yaml_properties.each do |m|
|
174
|
+
mep.put[m] = instance_variable_get( m )
|
175
|
+
end
|
176
|
+
rep.map( taguri, mep, to_yaml_style )
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
class Symbol
|
183
|
+
yaml_as "tag:ruby.yaml.org,2002:symbol"
|
184
|
+
yaml_as "tag:ruby.yaml.org,2002:sym"
|
185
|
+
def is_complex_yaml?; false; end
|
186
|
+
def Symbol.yaml_new( klass, tag, val )
|
187
|
+
if String === val
|
188
|
+
val.intern
|
189
|
+
else
|
190
|
+
raise RbYAML::TypeError, "Invalid Symbol: " + val.inspect
|
191
|
+
end
|
192
|
+
end
|
193
|
+
def to_yaml_node( repr )
|
194
|
+
RbYAML::quick_emit_node( nil, repr ) do |out|
|
195
|
+
out.scalar( "tag:yaml.org,2002:str", self.inspect, nil )
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
class Range
|
201
|
+
yaml_as "tag:ruby.yaml.org,2002:range"
|
202
|
+
def Range.yaml_new( klass, tag, val )
|
203
|
+
inr = %r'(\w+|[+-]?\d+(?:\.\d+)?(?:e[+-]\d+)?|"(?:[^\\"]|\\.)*")'
|
204
|
+
opts = {}
|
205
|
+
if String === val and val =~ /^#{inr}(\.{2,3})#{inr}$/o
|
206
|
+
r1, rdots, r2 = $1, $2, $3
|
207
|
+
opts = {
|
208
|
+
'begin' => RbYAML.load( "--- #{r1}" ),
|
209
|
+
'end' => RbYAML.load( "--- #{r2}" ),
|
210
|
+
'excl' => rdots.length == 3
|
211
|
+
}
|
212
|
+
val = {}
|
213
|
+
elsif Hash === val
|
214
|
+
opts['begin'] = val.delete('begin')
|
215
|
+
opts['end'] = val.delete('end')
|
216
|
+
opts['excl'] = val.delete('excl')
|
217
|
+
end
|
218
|
+
if Hash === opts
|
219
|
+
r = RbYAML::object_maker( klass, {} )
|
220
|
+
# Thank you, NaHi
|
221
|
+
Range.instance_method(:initialize).
|
222
|
+
bind(r).
|
223
|
+
call( opts['begin'], opts['end'], opts['excl'] )
|
224
|
+
val.each { |k,v| r.instance_variable_set( k, v ) }
|
225
|
+
r
|
226
|
+
else
|
227
|
+
raise RbYAML::TypeError, "Invalid Range: " + val.inspect
|
228
|
+
end
|
229
|
+
end
|
230
|
+
def to_yaml_node( repr )
|
231
|
+
RbYAML::quick_emit_node( object_id, repr ) do |out|
|
232
|
+
mep = {'begin' => self.begin, 'end' => self.end, 'excl' => self.exclude_end?}
|
233
|
+
to_yaml_properties.each do |m|
|
234
|
+
mep[m] = instance_variable_get( m )
|
235
|
+
end
|
236
|
+
out.map( taguri, mep, to_yaml_style )
|
237
|
+
end
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
class Regexp
|
242
|
+
yaml_as "tag:ruby.yaml.org,2002:regexp"
|
243
|
+
def Regexp.yaml_new( klass, tag, val )
|
244
|
+
if String === val and val =~ /^\/(.*)\/([mix]*)$/
|
245
|
+
val = { 'regexp' => $1, 'mods' => $2 }
|
246
|
+
end
|
247
|
+
if Hash === val
|
248
|
+
mods = nil
|
249
|
+
unless val['mods'].to_s.empty?
|
250
|
+
mods = 0x00
|
251
|
+
mods |= Regexp::EXTENDED if val['mods'].include?( 'x' )
|
252
|
+
mods |= Regexp::IGNORECASE if val['mods'].include?( 'i' )
|
253
|
+
mods |= Regexp::MULTILINE if val['mods'].include?( 'm' )
|
254
|
+
end
|
255
|
+
val.delete( 'mods' )
|
256
|
+
r = RbYAML::object_maker( klass, {} )
|
257
|
+
Regexp.instance_method(:initialize).
|
258
|
+
bind(r).
|
259
|
+
call( val.delete( 'regexp' ), mods )
|
260
|
+
val.each { |k,v| r.instance_variable_set( k, v ) }
|
261
|
+
r
|
262
|
+
else
|
263
|
+
raise RbYAML::TypeError, "Invalid Regular expression: " + val.inspect
|
264
|
+
end
|
265
|
+
end
|
266
|
+
def to_yaml_node( repr )
|
267
|
+
RbYAML::quick_emit_node( nil, repr ) do |out|
|
268
|
+
if to_yaml_properties.empty?
|
269
|
+
out.scalar( taguri, self.inspect, nil )
|
270
|
+
else
|
271
|
+
mep = { }
|
272
|
+
src = self.inspect
|
273
|
+
if src =~ /\A\/(.*)\/([a-z]*)\Z/
|
274
|
+
mep['regexp']=$1
|
275
|
+
mep['mods']=$2
|
276
|
+
else
|
277
|
+
raise RbYAML::TypeError, "Invalid Regular expression: " + src
|
278
|
+
end
|
279
|
+
to_yaml_properties.each do |m|
|
280
|
+
mep[m] = instance_variable_get( m )
|
281
|
+
end
|
282
|
+
out.map( taguri, mep, to_yaml_style )
|
283
|
+
end
|
284
|
+
end
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
class Time
|
289
|
+
yaml_as "tag:ruby.yaml.org,2002:time"
|
290
|
+
yaml_as "tag:yaml.org,2002:timestamp"
|
291
|
+
def is_complex_yaml?; false; end
|
292
|
+
def Time.yaml_new( klass, tag, val )
|
293
|
+
if Hash === val
|
294
|
+
t = val.delete( 'at' )
|
295
|
+
val.each { |k,v| t.instance_variable_set( k, v ) }
|
296
|
+
t
|
297
|
+
else
|
298
|
+
raise RbYAML::TypeError, "Invalid Time: " + val.inspect
|
299
|
+
end
|
300
|
+
end
|
301
|
+
def to_yaml_node(repre)
|
302
|
+
RbYAML::quick_emit_node( object_id, repre ) do |out|
|
303
|
+
tz = "Z"
|
304
|
+
# from the tidy Tobias Peters <t-peters@gmx.de> Thanks!
|
305
|
+
unless utc?
|
306
|
+
utc_same_instant = dup.utc
|
307
|
+
utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
|
308
|
+
difference_to_utc = utc_same_writing - utc_same_instant
|
309
|
+
if (difference_to_utc < 0)
|
310
|
+
difference_sign = '-'
|
311
|
+
absolute_difference = -difference_to_utc
|
312
|
+
else
|
313
|
+
difference_sign = '+'
|
314
|
+
absolute_difference = difference_to_utc
|
315
|
+
end
|
316
|
+
difference_minutes = (absolute_difference/60).round
|
317
|
+
tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
|
318
|
+
end
|
319
|
+
standard = strftime( "%Y-%m-%d %H:%M:%S" )
|
320
|
+
standard += ".%06d" % [usec] if usec.nonzero?
|
321
|
+
standard += " %s" % [tz]
|
322
|
+
out.scalar(taguri, standard, nil)
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
class Date
|
328
|
+
yaml_as "tag:yaml.org,2002:timestamp#ymd"
|
329
|
+
def is_complex_yaml?; false; end
|
330
|
+
def to_yaml_node(repre)
|
331
|
+
RbYAML::quick_emit_node( object_id, repre ) do |out|
|
332
|
+
out.scalar( "tag:yaml.org,2002:timestamp", self.to_s, nil )
|
333
|
+
end
|
334
|
+
end
|
335
|
+
end
|
336
|
+
|
337
|
+
class Numeric
|
338
|
+
def is_complex_yaml?; false; end
|
339
|
+
def to_yaml_node( repre )
|
340
|
+
RbYAML::quick_emit_node( nil, repre ) do |rep|
|
341
|
+
str = self.to_s
|
342
|
+
if str == "Infinity"
|
343
|
+
str = ".inf"
|
344
|
+
elsif str == "-Infinity"
|
345
|
+
str = "-.inf"
|
346
|
+
elsif str == "NaN"
|
347
|
+
str = ".nan"
|
348
|
+
end
|
349
|
+
rep.scalar( taguri, str, nil )
|
350
|
+
end
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
class Fixnum
|
355
|
+
yaml_as "tag:yaml.org,2002:int"
|
356
|
+
end
|
357
|
+
|
358
|
+
class Float
|
359
|
+
yaml_as "tag:yaml.org,2002:float"
|
360
|
+
end
|
361
|
+
|
362
|
+
class TrueClass
|
363
|
+
yaml_as "tag:yaml.org,2002:bool#yes"
|
364
|
+
def is_complex_yaml?; false; end
|
365
|
+
def to_yaml_node( repre )
|
366
|
+
RbYAML::quick_emit_node( nil, repre ) do |out|
|
367
|
+
out.scalar('tag:yaml.org,2002:bool', "true", nil)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
class FalseClass
|
373
|
+
yaml_as "tag:yaml.org,2002:bool#no"
|
374
|
+
def is_complex_yaml?; false; end
|
375
|
+
def to_yaml_node( repre )
|
376
|
+
RbYAML::quick_emit_node( nil, repre ) do |out|
|
377
|
+
out.scalar('tag:yaml.org,2002:bool', "false",nil )
|
378
|
+
end
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
class NilClass
|
383
|
+
yaml_as "tag:yaml.org,2002:null"
|
384
|
+
def is_complex_yaml?; false; end
|
385
|
+
def to_yaml_node( repre )
|
386
|
+
RbYAML::quick_emit_node( nil, repre ) do |out|
|
387
|
+
out.scalar(taguri, "", nil )
|
388
|
+
end
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|