RbYAML 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rbyaml/composer.rb +33 -113
- data/lib/rbyaml/constructor.rb +8 -1
- data/lib/rbyaml/dumper.rb +15 -12
- data/lib/rbyaml/emitter.rb +12 -12
- data/lib/rbyaml/events.rb +4 -3
- data/lib/rbyaml/loader.rb +8 -5
- data/lib/rbyaml/nodes.rb +1 -18
- data/lib/rbyaml/parser.rb +16 -10
- data/lib/rbyaml/representer.rb +75 -19
- data/lib/rbyaml/resolver.rb +163 -0
- data/lib/rbyaml/scanner.rb +11 -10
- data/lib/rbyaml/serializer.rb +18 -23
- data/lib/rbyaml/tokens.rb +4 -3
- data/lib/rbyaml/yaml.rb +20 -27
- data/test/test_rbyaml.rb +14 -1
- data/test/test_time.rb +29 -0
- metadata +4 -3
- data/lib/rbyaml/detector.rb +0 -44
data/lib/rbyaml/composer.rb
CHANGED
@@ -6,14 +6,9 @@ module RbYAML
|
|
6
6
|
class ComposerError < MarkedYAMLError
|
7
7
|
end
|
8
8
|
|
9
|
-
module
|
10
|
-
@@yaml_resolvers = {}
|
11
|
-
|
9
|
+
module Composer
|
12
10
|
def initialize_composer
|
13
|
-
@
|
14
|
-
@complete_anchors = {}
|
15
|
-
@resolver_tags = []
|
16
|
-
@resolver_paths = []
|
11
|
+
@anchors = {}
|
17
12
|
end
|
18
13
|
|
19
14
|
def check_node
|
@@ -33,157 +28,82 @@ module RbYAML
|
|
33
28
|
get_event if check_event(StreamStartEvent)
|
34
29
|
get_event
|
35
30
|
# Compose the root node.
|
36
|
-
node = compose_node(
|
31
|
+
node = compose_node(nil,nil)
|
37
32
|
# Drop the DOCUMENT-END event.
|
38
33
|
get_event
|
39
|
-
@
|
40
|
-
@complete_anchors = {}
|
41
|
-
@resolver_tags = []
|
42
|
-
@resolver_paths = []
|
34
|
+
@anchors = {}
|
43
35
|
node
|
44
36
|
end
|
45
37
|
|
46
|
-
def
|
47
|
-
depth = path.length
|
48
|
-
tag = []
|
49
|
-
paths = []
|
50
|
-
if depth == 0
|
51
|
-
for resolver_path in @@yaml_resolvers.keys
|
52
|
-
if resolver_path
|
53
|
-
paths += resolver_path
|
54
|
-
else
|
55
|
-
tag = @@yaml_resolvers[resolver_path]
|
56
|
-
end
|
57
|
-
end
|
58
|
-
else
|
59
|
-
base, index = path[-1]
|
60
|
-
if ScalarNode === index && index.tag == DEFAULT_SCALAR_TAG
|
61
|
-
index = index.value
|
62
|
-
elsif Node === index
|
63
|
-
index = nil
|
64
|
-
end
|
65
|
-
if @resolver_paths[-1]
|
66
|
-
for resolver_path in @resolver_paths[-1]
|
67
|
-
resolver_index = resolver_path[depth-1]
|
68
|
-
if resolver_index.nil? || resolver_index == index
|
69
|
-
if resolver_index.length > depth
|
70
|
-
paths += resolver_path
|
71
|
-
else
|
72
|
-
tag = @@yaml_resolvers[resolver_path]
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
@resolver_tags += tag
|
80
|
-
@resolver_paths += paths
|
81
|
-
end
|
82
|
-
|
83
|
-
def decrease_resolver_depth
|
84
|
-
@resolver_tags.pop
|
85
|
-
@resolver_paths.pop
|
86
|
-
end
|
87
|
-
|
88
|
-
def compose_node(path)
|
38
|
+
def compose_node(parent,index)
|
89
39
|
if check_event(AliasEvent)
|
90
40
|
event = get_event
|
91
41
|
anchor = event.anchor
|
92
|
-
raise ComposerError.new(nil, nil, "found undefined alias #{anchor}", event.start_mark) if !@
|
93
|
-
|
94
|
-
collection_event = @all_anchors[anchor]
|
95
|
-
raise ComposerError.new("while composing a collection",collection_event.start_mark,"found recursive anchor #{anchor}",event.start_mark)
|
96
|
-
end
|
97
|
-
return @complete_anchors[anchor]
|
42
|
+
raise ComposerError.new(nil, nil, "found undefined alias #{anchor}", event.start_mark) if !@anchors.include?(anchor)
|
43
|
+
return @anchors[anchor]
|
98
44
|
end
|
99
|
-
increase_resolver_depth(path)
|
100
45
|
event = peek_event
|
101
46
|
anchor = event.anchor
|
102
47
|
if !anchor.nil?
|
103
|
-
if @
|
104
|
-
raise ComposerError.new("found duplicate anchor #{anchor}; first occurence", @
|
48
|
+
if @anchors.include?(anchor)
|
49
|
+
raise ComposerError.new("found duplicate anchor #{anchor}; first occurence", @anchors[anchor].start_mark,"second occurence", event.start_mark)
|
105
50
|
end
|
106
|
-
@all_anchors[anchor] = event
|
107
51
|
end
|
52
|
+
descend_resolver(parent,index)
|
108
53
|
if check_event(ScalarEvent)
|
109
|
-
node = compose_scalar_node(
|
54
|
+
node = compose_scalar_node(anchor)
|
110
55
|
elsif check_event(SequenceStartEvent)
|
111
|
-
node = compose_sequence_node(
|
56
|
+
node = compose_sequence_node(anchor)
|
112
57
|
elsif check_event(MappingStartEvent)
|
113
|
-
node = compose_mapping_node(
|
58
|
+
node = compose_mapping_node(anchor)
|
114
59
|
end
|
115
|
-
|
116
|
-
if !anchor.nil?
|
117
|
-
@complete_anchors[anchor] = node
|
118
|
-
end
|
119
|
-
decrease_resolver_depth
|
60
|
+
ascend_resolver
|
120
61
|
node
|
121
62
|
end
|
122
63
|
|
123
|
-
def compose_scalar_node(
|
64
|
+
def compose_scalar_node(anchor)
|
124
65
|
event = get_event
|
125
|
-
tag =
|
126
|
-
ScalarNode
|
66
|
+
tag = event.tag
|
67
|
+
tag = resolve(ScalarNode,event.value,event.implicit) if tag.nil? || tag == "!"
|
68
|
+
node = ScalarNode.new(tag, event.value,event.start_mark, event.end_mark, event.style)
|
69
|
+
@anchors[anchor] = node if !anchor.nil?
|
70
|
+
node
|
127
71
|
end
|
128
72
|
|
129
|
-
def compose_sequence_node(
|
73
|
+
def compose_sequence_node(anchor)
|
130
74
|
start_event = get_event
|
131
|
-
tag =
|
132
|
-
|
75
|
+
tag = start_event.tag
|
76
|
+
tag = resolve(SequenceNode,nil,start_event.implicit) if tag.nil? || tag == "!"
|
77
|
+
node = SequenceNode.new(tag,[],start_event.start_mark,nil,start_event.flow_style)
|
78
|
+
@anchors[anchor] = node if !anchor.nil?
|
133
79
|
index = 0
|
134
80
|
while !check_event(SequenceEndEvent)
|
135
|
-
node.value << compose_node(
|
81
|
+
node.value << compose_node(node,index)
|
136
82
|
index += 1
|
137
83
|
end
|
138
|
-
|
139
84
|
end_event = get_event
|
140
85
|
node.end_mark = end_event.end_mark
|
141
86
|
node
|
142
87
|
end
|
143
88
|
|
144
|
-
def compose_mapping_node(
|
89
|
+
def compose_mapping_node(anchor)
|
145
90
|
start_event = get_event
|
146
|
-
tag =
|
147
|
-
|
91
|
+
tag = start_event.tag
|
92
|
+
tag = resolve(MappingNode,nil,start_event.implicit) if tag.nil? || tag == "!"
|
93
|
+
node = MappingNode.new(tag, {},start_event.start_mark,nil,start_event.flow_style)
|
94
|
+
@anchors[anchor] = node if !anchor.nil?
|
148
95
|
while !check_event(MappingEndEvent)
|
149
96
|
key_event = peek_event
|
150
|
-
item_key = compose_node(
|
151
|
-
item_value = compose_node(path+[[node, item_key]])
|
97
|
+
item_key = compose_node(node,nil)
|
152
98
|
if node.value.include?(item_key)
|
153
99
|
raise ComposerError.new("while composing a mapping", start_event.start_mark,"found duplicate key", key_event.start_mark)
|
154
100
|
end
|
101
|
+
item_value = compose_node(node,item_key)
|
155
102
|
node.value[item_key] = item_value
|
156
103
|
end
|
157
104
|
end_event = get_event
|
158
105
|
node.end_mark = end_event.end_mark
|
159
106
|
node
|
160
107
|
end
|
161
|
-
|
162
|
-
def resolve_scalar(path, tag, implicit, value)
|
163
|
-
tag = detect(value) if implicit
|
164
|
-
tag = resolver_tags[-1] if (tag.nil? || tag == []) && @resolver_tags[-1]
|
165
|
-
tag = DEFAULT_SCALAR_TAG if tag.nil? || tag == [] || tag == "!"
|
166
|
-
tag
|
167
|
-
end
|
168
|
-
|
169
|
-
def resolve_sequence(path, tag)
|
170
|
-
tag = resolver_tags[-1] if tag.nil? && @resolver_tags[-1]
|
171
|
-
tag = DEFAULT_SEQUENCE_TAG if tag.nil? || tag == "!"
|
172
|
-
tag
|
173
|
-
end
|
174
|
-
|
175
|
-
def resolve_mapping(path, tag)
|
176
|
-
tag = resolver_tags[-1] if tag.nil? && @resolver_tags[-1]
|
177
|
-
tag = DEFAULT_MAPPING_TAG if tag.nil? || tag == "!"
|
178
|
-
tag
|
179
|
-
end
|
180
|
-
|
181
|
-
def self.add_resolver(tag, path)
|
182
|
-
@@yaml_resolvers[path] = tag
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
module Composer
|
187
|
-
include BaseComposer
|
188
108
|
end
|
189
109
|
end
|
data/lib/rbyaml/constructor.rb
CHANGED
@@ -17,6 +17,7 @@ module RbYAML
|
|
17
17
|
|
18
18
|
def initialize_constructor
|
19
19
|
@constructed_objects = {}
|
20
|
+
@recursive_objects = {}
|
20
21
|
end
|
21
22
|
|
22
23
|
def check_data
|
@@ -39,11 +40,14 @@ module RbYAML
|
|
39
40
|
def construct_document(node)
|
40
41
|
data = construct_object(node)
|
41
42
|
@constructed_objects = {}
|
43
|
+
@recursive_objects = {}
|
42
44
|
data
|
43
45
|
end
|
44
46
|
|
45
47
|
def construct_object(node)
|
46
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
|
47
51
|
constructor = nil
|
48
52
|
ruby_cls = RbYAML::tagged_classes[node.tag]
|
49
53
|
if @@yaml_constructors.include?(node.tag)
|
@@ -61,8 +65,8 @@ module RbYAML
|
|
61
65
|
else
|
62
66
|
constructor = lambda { |node| @@yaml_multi_constructors[tag_prefix].call(tag_suffix, node) }
|
63
67
|
end
|
68
|
+
break
|
64
69
|
end
|
65
|
-
break
|
66
70
|
end
|
67
71
|
if !through
|
68
72
|
if @@yaml_multi_constructors.include?(nil)
|
@@ -80,6 +84,7 @@ module RbYAML
|
|
80
84
|
end
|
81
85
|
data = (Symbol === constructor) ? send(constructor,node) : constructor.call(node)
|
82
86
|
@constructed_objects[node] = data
|
87
|
+
@recursive_objects.delete(node)
|
83
88
|
data
|
84
89
|
end
|
85
90
|
|
@@ -90,6 +95,8 @@ module RbYAML
|
|
90
95
|
construct_sequence(node)
|
91
96
|
elsif MappingNode === node
|
92
97
|
construct_mapping(node)
|
98
|
+
else
|
99
|
+
puts node.tag
|
93
100
|
end
|
94
101
|
end
|
95
102
|
|
data/lib/rbyaml/dumper.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'rbyaml/emitter'
|
2
2
|
require 'rbyaml/serializer'
|
3
3
|
require 'rbyaml/representer'
|
4
|
-
require 'rbyaml/
|
4
|
+
require 'rbyaml/resolver'
|
5
5
|
|
6
6
|
module RbYAML
|
7
7
|
class CommonDumper
|
8
8
|
include Emitter, Serializer
|
9
9
|
|
10
|
-
def initialize(stream,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
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
11
|
super()
|
12
12
|
initialize_emitter(stream,canonical,indent,width,line_break)
|
13
13
|
initialize_serializer(explicit_start,explicit_end,version,tags)
|
@@ -15,26 +15,29 @@ module RbYAML
|
|
15
15
|
end
|
16
16
|
|
17
17
|
class BaseDumper < CommonDumper
|
18
|
-
include BaseRepresenter,
|
19
|
-
def initialize(stream,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
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
20
|
super
|
21
|
-
initialize_representer
|
21
|
+
initialize_representer(default_style,default_flow_style)
|
22
|
+
initialize_resolver
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
25
26
|
class SafeDumper < CommonDumper
|
26
|
-
include SafeRepresenter,
|
27
|
-
def initialize(stream,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
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)
|
28
29
|
super
|
29
|
-
initialize_representer
|
30
|
+
initialize_representer(default_style,default_flow_style)
|
31
|
+
initialize_resolver
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
class Dumper < CommonDumper
|
34
|
-
include Representer,
|
35
|
-
def initialize(stream,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
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)
|
36
38
|
super
|
37
|
-
initialize_representer
|
38
|
-
|
39
|
+
initialize_representer(default_style,default_flow_style)
|
40
|
+
initialize_resolver
|
39
41
|
end
|
42
|
+
end
|
40
43
|
end
|
data/lib/rbyaml/emitter.rb
CHANGED
@@ -491,19 +491,21 @@ module RbYAML
|
|
491
491
|
tag = @event.tag
|
492
492
|
if ScalarEvent === @event
|
493
493
|
@style = choose_scalar_style if @style.nil?
|
494
|
-
if @style == ""
|
494
|
+
if ((!@canonical || tag.nil?) && ((@style == "" && @event.implicit[0]) || (@style != "" && @event.implicit[1])))
|
495
495
|
@prepared_tag = nil
|
496
|
-
return
|
496
|
+
return
|
497
497
|
end
|
498
|
-
if @event.implicit &&
|
498
|
+
if @event.implicit[0] && tag.nil?
|
499
499
|
tag = "!"
|
500
500
|
@prepared_tag = nil
|
501
501
|
end
|
502
|
+
else
|
503
|
+
if (!@canonical || tag.nil?) && @event.implicit
|
504
|
+
@prepared_tag = nil
|
505
|
+
return
|
506
|
+
end
|
502
507
|
end
|
503
|
-
if
|
504
|
-
@prepared_tag = nil
|
505
|
-
return nil
|
506
|
-
end
|
508
|
+
raise EmitterError.new("tag is not specified") if tag.nil?
|
507
509
|
@prepared_tag = prepare_tag(tag) if @prepared_tag.nil?
|
508
510
|
write_indicator(@prepared_tag, true) if @prepared_tag && !@prepared_tag.empty?
|
509
511
|
@prepared_tag = nil
|
@@ -512,14 +514,12 @@ module RbYAML
|
|
512
514
|
def choose_scalar_style
|
513
515
|
@analysis = analyze_scalar(@event.value) if @analysis.nil?
|
514
516
|
return '"' if @event.style == '"' || @canonical
|
515
|
-
if !@event.style && @event.implicit
|
517
|
+
if !@event.style && @event.implicit[0]
|
516
518
|
if !(@simple_key_context && (@analysis.empty || @analysis.multiline)) && ((@flow_level!=0 && @analysis.allow_flow_plain) || (@flow_level == 0 && @analysis.allow_block_plain))
|
517
519
|
return ""
|
518
520
|
end
|
519
521
|
end
|
520
522
|
|
521
|
-
|
522
|
-
|
523
523
|
if !@event.style && @event.implicit && (!(@simple_key_context && (@analysis.empty || @analysis.multiline)) &&
|
524
524
|
(@flow_level!=0 && @analysis.allow_flow_plain || (@flow_level==0 && @analysis.allow_block_plain)))
|
525
525
|
return ""
|
@@ -925,7 +925,7 @@ module RbYAML
|
|
925
925
|
while ending <= text.length
|
926
926
|
ch = nil
|
927
927
|
ch = text[ending] if ending < text.length
|
928
|
-
if ch.nil? ||
|
928
|
+
if ch.nil? || "\"\\\x85".include?(ch) || !(?\x20 <= ch && ch <= ?\x7E)
|
929
929
|
if start < ending
|
930
930
|
data = text[start...ending]
|
931
931
|
@column += data.length
|
@@ -951,7 +951,7 @@ module RbYAML
|
|
951
951
|
write_indent
|
952
952
|
@whitespace = false
|
953
953
|
@indention = false
|
954
|
-
if
|
954
|
+
if text[start] == 32
|
955
955
|
data = "\\"
|
956
956
|
@column += data.length
|
957
957
|
@stream.write(data)
|
data/lib/rbyaml/events.rb
CHANGED
@@ -8,7 +8,7 @@ module RbYAML
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def to_s
|
11
|
-
attributes = ["@anchor","@tag","@value"] & self.instance_variables
|
11
|
+
attributes = ["@anchor","@tag","@implicit","@value"] & self.instance_variables
|
12
12
|
args = attributes.collect {|val| "#{val[1..-1]}=" + eval("#{val}").to_s}.join(", ")
|
13
13
|
"#{self.class.name}(#{args})"
|
14
14
|
end
|
@@ -23,10 +23,11 @@ module RbYAML
|
|
23
23
|
end
|
24
24
|
|
25
25
|
class CollectionStartEvent < NodeEvent
|
26
|
-
attr_reader :tag, :flow_style
|
27
|
-
def initialize(anchor,tag,start_mark=nil, end_mark=nil,flow_style=nil)
|
26
|
+
attr_reader :tag, :implicit, :flow_style
|
27
|
+
def initialize(anchor,tag,implicit,start_mark=nil, end_mark=nil,flow_style=nil)
|
28
28
|
super(anchor,start_mark,end_mark)
|
29
29
|
@tag = tag
|
30
|
+
@implicit = implicit
|
30
31
|
@flow_style = flow_style
|
31
32
|
end
|
32
33
|
end
|
data/lib/rbyaml/loader.rb
CHANGED
@@ -5,7 +5,7 @@ require 'rbyaml/scanner'
|
|
5
5
|
require 'rbyaml/parser'
|
6
6
|
require 'rbyaml/composer'
|
7
7
|
require 'rbyaml/constructor'
|
8
|
-
require 'rbyaml/
|
8
|
+
require 'rbyaml/resolver'
|
9
9
|
|
10
10
|
module RbYAML
|
11
11
|
class CommonLoader
|
@@ -20,29 +20,32 @@ module RbYAML
|
|
20
20
|
end
|
21
21
|
|
22
22
|
class BaseLoader < CommonLoader
|
23
|
-
include
|
23
|
+
include Composer, BaseConstructor, BaseResolver
|
24
24
|
def initialize(stream)
|
25
25
|
super
|
26
26
|
initialize_composer
|
27
27
|
initialize_constructor
|
28
|
+
initialize_resolver
|
28
29
|
end
|
29
30
|
end
|
30
31
|
|
31
32
|
class SafeLoader < CommonLoader
|
32
|
-
include Composer, SafeConstructor,
|
33
|
+
include Composer, SafeConstructor, Resolver
|
33
34
|
def initialize(stream)
|
34
35
|
super
|
35
36
|
initialize_composer
|
36
37
|
initialize_constructor
|
37
|
-
|
38
|
+
initialize_resolver
|
39
|
+
end
|
38
40
|
end
|
39
41
|
|
40
42
|
class Loader < CommonLoader
|
41
|
-
include Composer, Constructor,
|
43
|
+
include Composer, Constructor, Resolver
|
42
44
|
def initialize(stream)
|
43
45
|
super
|
44
46
|
initialize_composer
|
45
47
|
initialize_constructor
|
48
|
+
initialize_resolver
|
46
49
|
end
|
47
50
|
end
|
48
51
|
end
|
data/lib/rbyaml/nodes.rb
CHANGED
@@ -11,24 +11,7 @@ module RbYAML
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def to_s
|
14
|
-
value
|
15
|
-
if Array === value
|
16
|
-
if value.empty?
|
17
|
-
value = "<empty>"
|
18
|
-
elsif value.length == 1
|
19
|
-
value = "<1 item>"
|
20
|
-
else
|
21
|
-
value = "#{value.length} items>"
|
22
|
-
end
|
23
|
-
else
|
24
|
-
if value.length > 75
|
25
|
-
value = value[0..70].to_s+"..."
|
26
|
-
else
|
27
|
-
value = value.to_s
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
"#{self.class.name}(tag=#{@tag}, value=#{value})"
|
14
|
+
"#{self.class.name}(tag=#{@tag}, value=#{@value})"
|
32
15
|
end
|
33
16
|
end
|
34
17
|
|
data/lib/rbyaml/parser.rb
CHANGED
@@ -257,37 +257,43 @@ module RbYAML
|
|
257
257
|
end
|
258
258
|
event = nil
|
259
259
|
collection_events = nil
|
260
|
+
implicit = tag.nil? || tag == ?!
|
260
261
|
if indentless_sequence && check_token(BlockEntryToken)
|
261
262
|
end_mark = peek_token.end_mark
|
262
|
-
event = SequenceStartEvent.new(anchor, tag, start_mark, end_mark)
|
263
|
+
event = SequenceStartEvent.new(anchor, tag, implicit, start_mark, end_mark)
|
263
264
|
collection_events = parse_indentless_sequence
|
264
265
|
else
|
265
266
|
if check_token(ScalarToken)
|
266
267
|
token = get_token
|
267
268
|
end_mark = token.end_mark
|
268
|
-
|
269
|
+
if (token.plain && tag.nil?) || tag == ?!
|
270
|
+
implicit = [true, false]
|
271
|
+
elsif tag.nil?
|
272
|
+
implicit = [false, true]
|
273
|
+
else
|
274
|
+
implicit = [false, false]
|
275
|
+
end
|
269
276
|
event = ScalarEvent.new(anchor, tag, implicit, token.value,start_mark, end_mark,token.style)
|
270
277
|
elsif check_token(FlowSequenceStartToken)
|
271
278
|
end_mark = peek_token.end_mark
|
272
|
-
event = SequenceStartEvent.new(anchor, tag, start_mark, end_mark,true)
|
279
|
+
event = SequenceStartEvent.new(anchor, tag, implicit, start_mark, end_mark,true)
|
273
280
|
collection_events = parse_flow_sequence
|
274
281
|
elsif check_token(FlowMappingStartToken)
|
275
282
|
end_mark = peek_token.end_mark
|
276
|
-
event = MappingStartEvent.new(anchor, tag, start_mark, end_mark,true)
|
283
|
+
event = MappingStartEvent.new(anchor, tag, implicit, start_mark, end_mark,true)
|
277
284
|
collection_events = parse_flow_mapping
|
278
285
|
elsif block && check_token(BlockSequenceStartToken)
|
279
286
|
end_mark = peek_token.start_mark
|
280
|
-
event = SequenceStartEvent.new(anchor, tag, start_mark, end_mark,false)
|
287
|
+
event = SequenceStartEvent.new(anchor, tag, implicit, start_mark, end_mark,false)
|
281
288
|
collection_events = parse_block_sequence
|
282
289
|
elsif block && check_token(BlockMappingStartToken)
|
283
290
|
end_mark = peek_token.start_mark
|
284
|
-
event = MappingStartEvent.new(anchor, tag, start_mark, end_mark,false)
|
291
|
+
event = MappingStartEvent.new(anchor, tag, implicit, start_mark, end_mark,false)
|
285
292
|
collection_events = parse_block_mapping
|
286
293
|
elsif !anchor.nil? || !tag.nil?
|
287
294
|
# Empty scalars are allowed even if a tag or an anchor is
|
288
295
|
# specified.
|
289
|
-
|
290
|
-
event = ScalarEvent.new(anchor, tag, implicit,"",start_mark, end_mark)
|
296
|
+
event = ScalarEvent.new(anchor, tag, [implicit,false],"",start_mark, end_mark)
|
291
297
|
else
|
292
298
|
if block
|
293
299
|
node = "block"
|
@@ -398,7 +404,7 @@ module RbYAML
|
|
398
404
|
while !check_token(FlowSequenceEndToken)
|
399
405
|
if check_token(KeyToken)
|
400
406
|
token = get_token
|
401
|
-
events << MappingStartEvent.new(nil,nil,token.start_mark, token.end_mark,true)
|
407
|
+
events << MappingStartEvent.new(nil,nil,true,token.start_mark, token.end_mark,true)
|
402
408
|
if !check_token(ValueToken,FlowEntryToken, FlowSequenceEndToken)
|
403
409
|
events += parse_flow_node
|
404
410
|
else
|
@@ -481,7 +487,7 @@ module RbYAML
|
|
481
487
|
end
|
482
488
|
|
483
489
|
def process_empty_scalar(mark)
|
484
|
-
ScalarEvent.new(nil, nil,
|
490
|
+
ScalarEvent.new(nil, nil, [true, false], "", mark, mark)
|
485
491
|
end
|
486
492
|
end
|
487
493
|
end
|
data/lib/rbyaml/representer.rb
CHANGED
@@ -3,7 +3,6 @@ require 'set'
|
|
3
3
|
|
4
4
|
require 'rbyaml/error'
|
5
5
|
require 'rbyaml/nodes'
|
6
|
-
require 'rbyaml/detector'
|
7
6
|
|
8
7
|
module RbYAML
|
9
8
|
class RepresenterError < YAMLError
|
@@ -11,18 +10,31 @@ module RbYAML
|
|
11
10
|
|
12
11
|
module BaseRepresenter
|
13
12
|
@@yaml_representers = {}
|
13
|
+
@@yaml_multi_representers = {}
|
14
14
|
|
15
|
-
def initialize_representer
|
15
|
+
def initialize_representer(default_style=nil, default_flow_style=nil)
|
16
|
+
@default_style = default_style
|
17
|
+
@default_flow_style = default_flow_style
|
16
18
|
@represented_objects = {}
|
17
19
|
end
|
18
20
|
|
19
21
|
def represent(data)
|
20
|
-
node =
|
22
|
+
node = represent_data(data)
|
21
23
|
serialize(node)
|
22
24
|
represented_objects = {}
|
23
25
|
end
|
24
26
|
|
25
|
-
|
27
|
+
CLASSOBJ_TYPE = Class
|
28
|
+
INSTANCE_TYPE = Object
|
29
|
+
FUNCTION_TYPE = Method
|
30
|
+
BUILTIN_FUNCTION_TYPE = Method
|
31
|
+
MODULE_TYPE = Module
|
32
|
+
|
33
|
+
def get_classobj_bases(cls)
|
34
|
+
[cls] + cls.ancestors
|
35
|
+
end
|
36
|
+
|
37
|
+
def represent_data(data)
|
26
38
|
if ignore_aliases(data)
|
27
39
|
alias_key = nil
|
28
40
|
else
|
@@ -38,22 +50,35 @@ module RbYAML
|
|
38
50
|
@represented_objects[alias_key] = nil
|
39
51
|
end
|
40
52
|
|
41
|
-
|
53
|
+
data_types = data.class.ancestors
|
54
|
+
data_types = get_classobj_bases(data.class) + data_types if INSTANCE_TYPE === data
|
42
55
|
|
43
|
-
|
56
|
+
if @@yaml_representers.include?(data_types[0])
|
57
|
+
node = send(@@yaml_representers[data_types[0]],data)
|
58
|
+
else
|
44
59
|
rerun = true
|
45
|
-
|
46
|
-
|
47
|
-
|
60
|
+
for data_type in data_types
|
61
|
+
if @@yaml_multi_representers.include?(data_type)
|
62
|
+
node = send(@@yaml_multi_representers[data_type],data)
|
63
|
+
rerun = false
|
64
|
+
break
|
65
|
+
elsif @@yaml_representers.include?(data_type)
|
66
|
+
node = send(@@yaml_representers[data_type],data)
|
67
|
+
rerun = false
|
68
|
+
break
|
69
|
+
end
|
48
70
|
end
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
71
|
+
if rerun
|
72
|
+
if @@yaml_multi_representers.include?(nil)
|
73
|
+
node = send(@@yaml_multi_representers[nil],data)
|
74
|
+
elsif @@yaml_representers.include?(nil)
|
75
|
+
node = send(@@yaml_representers[nil],data)
|
76
|
+
else
|
77
|
+
node = ScalarNode.new(nil, data)
|
78
|
+
end
|
55
79
|
end
|
56
80
|
end
|
81
|
+
|
57
82
|
@represented_objects[alias_key] = node if !alias_key.nil?
|
58
83
|
node
|
59
84
|
end
|
@@ -61,19 +86,49 @@ module RbYAML
|
|
61
86
|
def self.add_representer(data_type, representer)
|
62
87
|
@@yaml_representers[data_type] = representer
|
63
88
|
end
|
89
|
+
|
90
|
+
def self.add_multi_representer(data_type, representer)
|
91
|
+
@@yaml_multi_representers[data_type] = representer
|
92
|
+
end
|
64
93
|
|
65
94
|
def represent_scalar(tag, value, style=nil)
|
95
|
+
style ||= @default_style
|
66
96
|
ScalarNode.new(tag, value, style)
|
67
97
|
end
|
68
98
|
|
69
99
|
def represent_sequence(tag, sequence, flow_style=nil)
|
70
|
-
|
100
|
+
best_style = true
|
101
|
+
value = sequence.map {|item|
|
102
|
+
node_item = represent_data(item)
|
103
|
+
best_style = false if !ScalarNode === node_item && !node_item.style
|
104
|
+
node_item
|
105
|
+
}
|
106
|
+
flow_style ||= (@flow_default_style || best_style)
|
71
107
|
SequenceNode.new(tag, value, flow_style)
|
72
108
|
end
|
73
109
|
|
74
110
|
def represent_mapping(tag, mapping, flow_style=nil)
|
75
|
-
|
76
|
-
mapping.
|
111
|
+
best_style = true
|
112
|
+
if mapping.respond_to?(:keys)
|
113
|
+
value = {}
|
114
|
+
for item_key,item_value in mapping
|
115
|
+
node_key = represent_data(item_key)
|
116
|
+
node_value = represent_data(item_value)
|
117
|
+
best_style = false if !ScalarNode === node_key && !node_key.style
|
118
|
+
best_style = false if !ScalarNode === node_value && !node_value.style
|
119
|
+
value[node_key] = node_value
|
120
|
+
end
|
121
|
+
else
|
122
|
+
value = []
|
123
|
+
for item_key, item_value in mapping
|
124
|
+
node_key = represent_data(item_key)
|
125
|
+
node_value = represent_data(item_value)
|
126
|
+
best_style = false if !ScalarNode === node_key && !node_key.style
|
127
|
+
best_style = false if !ScalarNode === node_value && !node_value.style
|
128
|
+
value << [node_key, node_value]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
flow_style ||= (@flow_default_style || best_style)
|
77
132
|
MappingNode.new(tag, value, flow_style)
|
78
133
|
end
|
79
134
|
|
@@ -94,7 +149,7 @@ module RbYAML
|
|
94
149
|
end
|
95
150
|
|
96
151
|
def represent_str(data)
|
97
|
-
represent_scalar(data.taguri,data)
|
152
|
+
represent_scalar(data.taguri,data,nil)
|
98
153
|
end
|
99
154
|
|
100
155
|
def represent_symbol(data)
|
@@ -124,6 +179,7 @@ module RbYAML
|
|
124
179
|
end
|
125
180
|
|
126
181
|
def represent_list(data)
|
182
|
+
#no support for pairs right now. should probably be there, though...
|
127
183
|
represent_sequence(data.taguri, data)
|
128
184
|
end
|
129
185
|
|
@@ -0,0 +1,163 @@
|
|
1
|
+
require 'rbyaml/nodes'
|
2
|
+
require 'rbyaml/error'
|
3
|
+
|
4
|
+
module RbYAML
|
5
|
+
class ResolverError < MarkedYAMLError
|
6
|
+
end
|
7
|
+
|
8
|
+
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
9
|
+
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
|
10
|
+
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
11
|
+
|
12
|
+
module BaseResolver
|
13
|
+
@@yaml_implicit_resolvers = {}
|
14
|
+
@@yaml_path_resolvers = {}
|
15
|
+
|
16
|
+
def initialize_resolver
|
17
|
+
@resolver_exact_paths = []
|
18
|
+
@resolver_prefix_paths = []
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.add_implicit_resolver(tag, regexp, first)
|
22
|
+
if first.nil?
|
23
|
+
first = ""
|
24
|
+
end
|
25
|
+
first.each_byte { |ch|
|
26
|
+
@@yaml_implicit_resolvers[ch] ||= []
|
27
|
+
@@yaml_implicit_resolvers[ch] << [tag,regexp]
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.add_path_resolver(tag, path, kind=nil)
|
32
|
+
new_path = []
|
33
|
+
for element in path
|
34
|
+
if Array === element
|
35
|
+
if element.length == 2
|
36
|
+
node_check, index_check = element
|
37
|
+
elsif element.length == 1
|
38
|
+
node_check = element[0]
|
39
|
+
index_check = true
|
40
|
+
else
|
41
|
+
raise ResolverError.new("Invalid path element: #{element}")
|
42
|
+
end
|
43
|
+
else
|
44
|
+
node_check = nil
|
45
|
+
index_check = element
|
46
|
+
end
|
47
|
+
if String == node_check
|
48
|
+
node_check = ScalarNode
|
49
|
+
elsif Array == node_check
|
50
|
+
node_check = SequenceNode
|
51
|
+
elsif Hash == node_check
|
52
|
+
node_check = MappingNode
|
53
|
+
elsif ![ScalarNode, SequenceNode, MappingNode].include?(node_check) && !Symbol === node_check && !node_check.nil?
|
54
|
+
raise ResolverError.new("Invalid node checker: #{node_check}")
|
55
|
+
end
|
56
|
+
if !(String === index_check || Integer === index_check) && !index_check.nil?
|
57
|
+
raise ResolverError.new("Invalid index checker: #{index_check}")
|
58
|
+
end
|
59
|
+
new_path << [node_check, index_check]
|
60
|
+
end
|
61
|
+
if String == kind
|
62
|
+
kind = ScalarNode
|
63
|
+
elsif Array == kind
|
64
|
+
kind = SequenceNode
|
65
|
+
elsif Hash == kind
|
66
|
+
kind = MappingNode
|
67
|
+
elsif ![ScalarNode, SequenceNode, MappingNode].include?(kind) && !kind.nil?
|
68
|
+
raise ResolverError.new("Invalid node kind: #{kind}")
|
69
|
+
end
|
70
|
+
@@yaml_path_resolvers[[[new_path], kind]] = tag
|
71
|
+
end
|
72
|
+
|
73
|
+
def descend_resolver(current_node, current_index)
|
74
|
+
exact_paths = {}
|
75
|
+
prefix_paths = []
|
76
|
+
if current_node
|
77
|
+
depth = @resolver_prefix_paths.length
|
78
|
+
for path, kind in @resolver_prefix_paths[-1]
|
79
|
+
if check_resolver_prefix(depth, path, kind,current_node, current_index)
|
80
|
+
if path.length > depth
|
81
|
+
prefix_paths << [path, kind]
|
82
|
+
else
|
83
|
+
exact_paths[kind] = @@yaml_path_resolvers[[path, kind]]
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
else
|
88
|
+
for path, kind in @@yaml_path_resolvers
|
89
|
+
if !path
|
90
|
+
exact_paths[kind] = @@yaml_path_resolvers[[path, kind]]
|
91
|
+
else
|
92
|
+
prefix_paths << [path, kind]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
@resolver_exact_paths << exact_paths
|
97
|
+
@resolver_prefix_paths << prefix_paths
|
98
|
+
end
|
99
|
+
|
100
|
+
def ascend_resolver
|
101
|
+
@resolver_exact_paths.pop
|
102
|
+
@resolver_prefix_paths.pop
|
103
|
+
end
|
104
|
+
|
105
|
+
def check_resolver_prefix(depth, path, kind, current_node, current_index)
|
106
|
+
node_check, index_check = path[depth-1]
|
107
|
+
if String === node_check
|
108
|
+
return false if current_node.tag != node_check
|
109
|
+
elsif !node_check.nil?
|
110
|
+
return false if !node_check === current_node
|
111
|
+
end
|
112
|
+
return false if index_check==true && !current_index.nil?
|
113
|
+
return false if !index_check && current_index.nil?
|
114
|
+
if String === index_check
|
115
|
+
return false if !(ScalarNode === current_index && index_check == current_index.value)
|
116
|
+
elsif Integer === index_check
|
117
|
+
return false if index_check != current_index
|
118
|
+
end
|
119
|
+
true
|
120
|
+
end
|
121
|
+
|
122
|
+
def resolve(kind, value, implicit)
|
123
|
+
if ScalarNode == kind && implicit[0]
|
124
|
+
if value == ""
|
125
|
+
resolvers = @@yaml_implicit_resolvers.fetch("", [])
|
126
|
+
else
|
127
|
+
resolvers = @@yaml_implicit_resolvers.fetch(value[0], [])
|
128
|
+
end
|
129
|
+
resolvers += @@yaml_implicit_resolvers.fetch(nil, [])
|
130
|
+
for tag, regexp in resolvers
|
131
|
+
return tag if regexp =~ value
|
132
|
+
end
|
133
|
+
implicit = implicit[1]
|
134
|
+
end
|
135
|
+
exact_paths = @resolver_exact_paths[-1]
|
136
|
+
return exact_paths[kind] if exact_paths.include?(kind)
|
137
|
+
return exact_paths[nil] if exact_paths.include?(nil)
|
138
|
+
if ScalarNode == kind
|
139
|
+
return RbYAML::DEFAULT_SCALAR_TAG
|
140
|
+
elsif SequenceNode == kind
|
141
|
+
return RbYAML::DEFAULT_SEQUENCE_TAG
|
142
|
+
elsif MappingNode == kind
|
143
|
+
return RbYAML::DEFAULT_MAPPING_TAG
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
module Resolver
|
149
|
+
include BaseResolver
|
150
|
+
end
|
151
|
+
|
152
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:bool',/^(?:y|Y|yes|Yes|YES|n|N|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-5]?[0-9])+\.[0-9_]*|[-+]?\.(?:inf|Inf|INF)|\.(?:nan|NaN|NAN))$/,'-+0123456789.')
|
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
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:merge',/^(?:<<)$/,'<')
|
156
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:null',/^(?: ~|null|Null|NULL| )$/,'~nN' + ?\0.chr)
|
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
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:value',/^(?:=)$/,'=')
|
159
|
+
# The following implicit resolver is only for documentation purposes. It cannot work
|
160
|
+
# because plain scalars cannot start with '!', '&', or '*'.
|
161
|
+
BaseResolver.add_implicit_resolver('tag:yaml.org,2002:yaml',/^(?:!|&|\*)$/,'!&*')
|
162
|
+
end
|
163
|
+
|
data/lib/rbyaml/scanner.rb
CHANGED
@@ -579,10 +579,7 @@ module RbYAML
|
|
579
579
|
|
580
580
|
def check_document_end
|
581
581
|
# DOCUMENT-END: ^ '...' (' '|'\n')
|
582
|
-
|
583
|
-
prefix = peek(4)
|
584
|
-
return true if prefix(3) == "..." && "\0 \t\r\n\x85".include?(peek(3))
|
585
|
-
end
|
582
|
+
@column == 0 && prefix(3) == "..." && "\0 \t\r\n\x85".include?(peek(3))
|
586
583
|
end
|
587
584
|
|
588
585
|
def check_block_entry
|
@@ -1112,13 +1109,17 @@ module RbYAML
|
|
1112
1109
|
while true
|
1113
1110
|
length = 0
|
1114
1111
|
break if peek == ?#
|
1115
|
-
|
1116
|
-
|
1117
|
-
|
1118
|
-
|
1119
|
-
end
|
1120
|
-
length += 1
|
1112
|
+
while true
|
1113
|
+
ch = peek(length)
|
1114
|
+
if "\0 \t\r\n\x85".include?(ch) || (@flow_level==0 && ch == ?: && "\0 \t\r\n\x28".include?(peek(length+1))) || (@flow_level!=0 && ",:?[]{}".include?(ch))
|
1115
|
+
break
|
1121
1116
|
end
|
1117
|
+
length += 1
|
1118
|
+
end
|
1119
|
+
if @flow_level != 0 && ch == ?: && !"\0 \t\r\n\x28[]{}".include?(peek(length+1))
|
1120
|
+
forward(length)
|
1121
|
+
raise ScannerError.new("while scanning a plain scalar",start_mark,"found unexpected ':'",get_mark,"Please check http://pyyaml.org/wiki/YAMLColonInFlowContext for details.")
|
1122
|
+
end
|
1122
1123
|
break if length == 0
|
1123
1124
|
@allow_simple_key = false
|
1124
1125
|
chunks += spaces
|
data/lib/rbyaml/serializer.rb
CHANGED
@@ -48,7 +48,7 @@ module RbYAML
|
|
48
48
|
end
|
49
49
|
emit(DocumentStartEvent.new(@use_explicit_start,@use_version,@use_tags))
|
50
50
|
anchor_node(node)
|
51
|
-
serialize_node(node)
|
51
|
+
serialize_node(node,nil,nil)
|
52
52
|
emit(DocumentEndEvent.new(@use_explicit_end))
|
53
53
|
@serialized_nodes = {}
|
54
54
|
@anchors = {}
|
@@ -65,9 +65,9 @@ module RbYAML
|
|
65
65
|
anchor_node(item)
|
66
66
|
end
|
67
67
|
elsif MappingNode === node
|
68
|
-
for key in node.value
|
68
|
+
for key,val in node.value
|
69
69
|
anchor_node(key)
|
70
|
-
anchor_node(
|
70
|
+
anchor_node(val)
|
71
71
|
end
|
72
72
|
end
|
73
73
|
end
|
@@ -78,38 +78,33 @@ module RbYAML
|
|
78
78
|
ANCHOR_TEMPLATE % @last_anchor_id
|
79
79
|
end
|
80
80
|
|
81
|
-
def serialize_node(node)
|
81
|
+
def serialize_node(node,parent,index)
|
82
82
|
talias = @anchors[node]
|
83
83
|
if @serialized_nodes.include?(node)
|
84
84
|
emit(AliasEvent.new(talias))
|
85
85
|
else
|
86
86
|
@serialized_nodes[node] = true
|
87
|
+
descend_resolver(parent, index)
|
87
88
|
if ScalarNode === node
|
88
|
-
detected_tag =
|
89
|
-
|
89
|
+
detected_tag = resolve(ScalarNode, node.value, [true,false])
|
90
|
+
default_tag = resolve(ScalarNode, node.value, [false,true])
|
91
|
+
implicit = (node.tag == detected_tag), (node.tag == default_tag)
|
90
92
|
emit(ScalarEvent.new(talias, node.tag, implicit, node.value,nil,nil,node.style))
|
91
93
|
elsif SequenceNode === node
|
92
|
-
|
93
|
-
|
94
|
-
|
94
|
+
implicit = (node.tag == resolve(SequenceNode, node.value, true))
|
95
|
+
emit(SequenceStartEvent.new(talias, node.tag, implicit,node.flow_style))
|
96
|
+
index = 0
|
95
97
|
for item in node.value
|
96
|
-
serialize_node(item)
|
98
|
+
serialize_node(item,node,index)
|
99
|
+
index += 1
|
97
100
|
end
|
98
101
|
emit(SequenceEndEvent.new)
|
99
102
|
elsif MappingNode === node
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
serialize_node(key)
|
106
|
-
serialize_node(node.value[key])
|
107
|
-
end
|
108
|
-
else
|
109
|
-
for key, value in node.value
|
110
|
-
serialize_node(key)
|
111
|
-
serialize_node(value)
|
112
|
-
end
|
103
|
+
implicit = (node.tag == resolve(MappingNode, node.value, true))
|
104
|
+
emit(MappingStartEvent.new(talias, node.tag, implicit,node.flow_style))
|
105
|
+
for key, value in node.value
|
106
|
+
serialize_node(key,node,nil)
|
107
|
+
serialize_node(value,node,key)
|
113
108
|
end
|
114
109
|
emit(MappingEndEvent.new)
|
115
110
|
end
|
data/lib/rbyaml/tokens.rb
CHANGED
@@ -152,11 +152,12 @@ module RbYAML
|
|
152
152
|
def tid
|
153
153
|
"<scalar>"
|
154
154
|
end
|
155
|
-
attr_reader :value, :
|
156
|
-
|
155
|
+
attr_reader :value, :plain, :style
|
156
|
+
alias :implicit :plain #Until all references to ScalarToken.implicit has been removed
|
157
|
+
def initialize(value, plain, start_mark, end_mark, style=nil)
|
157
158
|
super(start_mark, end_mark)
|
158
159
|
@value = value
|
159
|
-
@
|
160
|
+
@plain = plain
|
160
161
|
@style = style
|
161
162
|
end
|
162
163
|
end
|
data/lib/rbyaml/yaml.rb
CHANGED
@@ -2,18 +2,6 @@ require 'stringio'
|
|
2
2
|
|
3
3
|
require 'rbyaml/error'
|
4
4
|
|
5
|
-
require 'rbyaml/reader'
|
6
|
-
require 'rbyaml/scanner'
|
7
|
-
require 'rbyaml/parser'
|
8
|
-
require 'rbyaml/composer'
|
9
|
-
require 'rbyaml/constructor'
|
10
|
-
|
11
|
-
require 'rbyaml/emitter'
|
12
|
-
require 'rbyaml/serializer'
|
13
|
-
require 'rbyaml/representer'
|
14
|
-
|
15
|
-
require 'rbyaml/detector'
|
16
|
-
|
17
5
|
require 'rbyaml/tokens'
|
18
6
|
require 'rbyaml/events'
|
19
7
|
require 'rbyaml/nodes'
|
@@ -60,24 +48,24 @@ module RbYAML
|
|
60
48
|
_load(stream, SafeLoader)
|
61
49
|
end
|
62
50
|
|
63
|
-
def self._emit(events, stream=nil, dumper=Dumper,canonical=nil, indent=nil, width=nil,line_break=nil)
|
51
|
+
def self._emit(events, stream=nil, dumper=Dumper,default_style=nil,default_flow_style=nil,canonical=nil, indent=nil, width=nil,line_break=nil)
|
64
52
|
if stream.nil?
|
65
53
|
require 'stringio'
|
66
54
|
stream = StringIO.new
|
67
55
|
end
|
68
|
-
dumper = dumper.new(stream,canonical,indent,width,line_break)
|
56
|
+
dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break)
|
69
57
|
for event in events
|
70
58
|
dumper.emit(event)
|
71
59
|
end
|
72
60
|
stream.string if StringIO === stream
|
73
61
|
end
|
74
62
|
|
75
|
-
def self._serialize_all(nodes,stream=nil,dumper=Dumper,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
63
|
+
def self._serialize_all(nodes,stream=nil,dumper=Dumper,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)
|
76
64
|
if stream.nil?
|
77
65
|
require 'stringio'
|
78
66
|
stream = StringIO.new
|
79
67
|
end
|
80
|
-
dumper = dumper.new(stream,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
68
|
+
dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
81
69
|
dumper.open
|
82
70
|
for node in nodes
|
83
71
|
dumper.serialize(node)
|
@@ -90,12 +78,12 @@ module RbYAML
|
|
90
78
|
_serialize_all([node], stream, dumper, *kwds)
|
91
79
|
end
|
92
80
|
|
93
|
-
def self._dump_all(documents,stream=nil,dumper=Dumper,canonical=nil,indent=nil,width=nil,line_break=nil,explicit_start=nil,explicit_end=nil,version=nil,tags=nil)
|
81
|
+
def self._dump_all(documents,stream=nil,dumper=Dumper,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)
|
94
82
|
if stream.nil?
|
95
83
|
require 'stringio'
|
96
84
|
stream = StringIO.new
|
97
85
|
end
|
98
|
-
dumper = dumper.new(stream,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
86
|
+
dumper = dumper.new(stream,default_style,default_flow_style,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
99
87
|
dumper.open
|
100
88
|
for data in documents
|
101
89
|
dumper.represent(data)
|
@@ -116,13 +104,14 @@ module RbYAML
|
|
116
104
|
_dump_all([data], stream, SafeDumper, *kwds)
|
117
105
|
end
|
118
106
|
|
119
|
-
def self.
|
120
|
-
loader.
|
121
|
-
dumper.
|
107
|
+
def self._add_implicit_resolver(tag, regexp, first=nil, loader=Loader, dumper=Dumper)
|
108
|
+
loader.add_implicit_resolver(tag, regexp, first)
|
109
|
+
dumper.add_implicit_resolver(tag, regexp, first)
|
122
110
|
end
|
123
111
|
|
124
|
-
def self.
|
125
|
-
loader.
|
112
|
+
def self._add_path_resolver(tag, path, kind=nil, loader=Loader, dumper=Dumper)
|
113
|
+
loader.add_path_resolver(tag, path, kind)
|
114
|
+
lumper.add_path_resolver(tag, path, kind)
|
126
115
|
end
|
127
116
|
|
128
117
|
def self._add_constructor(tag, constructor, loader=Loader)
|
@@ -133,11 +122,15 @@ module RbYAML
|
|
133
122
|
loader.add_multi_constructor(tag_prefix, multi_constructor)
|
134
123
|
end
|
135
124
|
|
136
|
-
def self.
|
137
|
-
|
125
|
+
def self._add_representer(data_type, representer, dumper=Dumper)
|
126
|
+
dumper.add_representer(data_type, representer)
|
138
127
|
end
|
139
128
|
|
140
|
-
|
141
|
-
|
129
|
+
def self._add_multi_representer(data_type, multi_representer, dumper=Dumper)
|
130
|
+
dumper.add_multi_representer(data_type, multi_representer)
|
131
|
+
end
|
132
|
+
|
133
|
+
def self._dump_ruby_object(data, dumper=Dumper)
|
134
|
+
_dump(data,nil,dumper)
|
142
135
|
end
|
143
136
|
end
|
data/test/test_rbyaml.rb
CHANGED
@@ -11,8 +11,21 @@ class TestRbYAML < Test::Unit::TestCase
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_basic_dump
|
14
|
-
|
14
|
+
answ,ver1,ver2 = RbYAML.dump({"A"=>"b","c"=>3.14}),"A: b\nc: 3.14\n", "c: 3.14\nA: b\n"
|
15
|
+
assert_equal(true, ver1 == answ || ver2 == answ,"was #{answ.inspect} but should be #{ver1.inspect} or #{ver2.inspect}")
|
15
16
|
assert_equal("- A\n- b\n- c\n",RbYAML.dump(["A","b","c"]))
|
16
17
|
assert_equal("foo\n",RbYAML.dump("foo"))
|
17
18
|
end
|
18
19
|
end
|
20
|
+
|
21
|
+
class TestRbYAMLFromFiles < Test::Unit::TestCase
|
22
|
+
FILES = %w{test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test12 test13 test14 test15 test16 test18 test19 test20}
|
23
|
+
|
24
|
+
FILES.each { |test|
|
25
|
+
self.class_eval <<-ENDCLS
|
26
|
+
def test_#{test}
|
27
|
+
RbYAML.dump(open(File.expand_path(File.join(File.dirname(__FILE__),"yaml","#{test}.yml")),"r") {|f| RbYAML.load(f)})
|
28
|
+
end
|
29
|
+
ENDCLS
|
30
|
+
}
|
31
|
+
end
|
data/test/test_time.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
|
2
|
+
|
3
|
+
require 'rbyaml'
|
4
|
+
|
5
|
+
class Time
|
6
|
+
def self.time(times = 1)
|
7
|
+
if block_given?
|
8
|
+
before = Time.now
|
9
|
+
times.times {
|
10
|
+
yield
|
11
|
+
}
|
12
|
+
after = Time.now
|
13
|
+
return after-before
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
DO_TIMES = 10000
|
19
|
+
ttimes = Time.time(DO_TIMES) {
|
20
|
+
open("yaml/test1.yml") { |f| RbYAML::load(f) }
|
21
|
+
}
|
22
|
+
|
23
|
+
ttimes2 = Time.time(DO_TIMES) {
|
24
|
+
RbYAML::dump(open("yaml/test1.yml") { |f| RbYAML::load(f) })
|
25
|
+
} - ttimes
|
26
|
+
|
27
|
+
puts "loading a file #{DO_TIMES} times took #{ttimes.to_f * 1000} milli seconds"
|
28
|
+
|
29
|
+
puts "dumping a file #{DO_TIMES} times took #{ttimes2.to_f * 1000} milliseconds"
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: RbYAML
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-05-
|
6
|
+
version: 0.0.2
|
7
|
+
date: 2006-05-19 00:00:00 -07:00
|
8
8
|
summary: A pure Ruby YAML parser and emitter, mostly supporting YAML 1.1.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -32,7 +32,6 @@ files:
|
|
32
32
|
- lib/rbyaml.rb
|
33
33
|
- lib/rbyaml/composer.rb
|
34
34
|
- lib/rbyaml/constructor.rb
|
35
|
-
- lib/rbyaml/detector.rb
|
36
35
|
- lib/rbyaml/dumper.rb
|
37
36
|
- lib/rbyaml/emitter.rb
|
38
37
|
- lib/rbyaml/error.rb
|
@@ -42,12 +41,14 @@ files:
|
|
42
41
|
- lib/rbyaml/parser.rb
|
43
42
|
- lib/rbyaml/reader.rb
|
44
43
|
- lib/rbyaml/representer.rb
|
44
|
+
- lib/rbyaml/resolver.rb
|
45
45
|
- lib/rbyaml/scanner.rb
|
46
46
|
- lib/rbyaml/serializer.rb
|
47
47
|
- lib/rbyaml/test.rb
|
48
48
|
- lib/rbyaml/tokens.rb
|
49
49
|
- lib/rbyaml/yaml.rb
|
50
50
|
- test/test_rbyaml.rb
|
51
|
+
- test/test_time.rb
|
51
52
|
- test/yaml
|
52
53
|
- test/yaml/gems.yml
|
53
54
|
- test/yaml/gems2.yml
|
data/lib/rbyaml/detector.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
|
2
|
-
module RbYAML
|
3
|
-
DEFAULT_SCALAR_TAG = 'tag:yaml.org,2002:str'
|
4
|
-
DEFAULT_SEQUENCE_TAG = 'tag:yaml.org,2002:seq'
|
5
|
-
DEFAULT_MAPPING_TAG = 'tag:yaml.org,2002:map'
|
6
|
-
|
7
|
-
module BaseDetector
|
8
|
-
@@yaml_detectors = {}
|
9
|
-
|
10
|
-
def self.add_detector(tag, regexp, first)
|
11
|
-
first.each_byte { |ch|
|
12
|
-
@@yaml_detectors[ch] ||= []
|
13
|
-
@@yaml_detectors[ch] << [tag,regexp]
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
def detect(value)
|
18
|
-
if value == ""
|
19
|
-
detectors = @@yaml_detectors.fetch(?\0, [])
|
20
|
-
else
|
21
|
-
detectors = @@yaml_detectors.fetch(value[0], [])
|
22
|
-
end
|
23
|
-
detectors += @@yaml_detectors.fetch(nil, [])
|
24
|
-
for tag, regexp in detectors
|
25
|
-
return tag if regexp =~ value
|
26
|
-
end
|
27
|
-
nil
|
28
|
-
end
|
29
|
-
end
|
30
|
-
module Detector
|
31
|
-
include BaseDetector
|
32
|
-
end
|
33
|
-
|
34
|
-
BaseDetector.add_detector('tag:yaml.org,2002:bool',/^(?:y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$/,'yYnNtTfFoO')
|
35
|
-
BaseDetector.add_detector('tag:yaml.org,2002:float',/^(?:[-+]?(?:[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.')
|
36
|
-
BaseDetector.add_detector('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')
|
37
|
-
BaseDetector.add_detector('tag:yaml.org,2002:merge',/^(?:<<)$/,'<')
|
38
|
-
BaseDetector.add_detector('tag:yaml.org,2002:null',/^(?: ~|null|Null|NULL| )$/,'~nN' + ?\0.chr)
|
39
|
-
BaseDetector.add_detector('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')
|
40
|
-
BaseDetector.add_detector('tag:yaml.org,2002:value',/^(?:=)$/,'=')
|
41
|
-
# The following detector is only for documentation purposes. It cannot work
|
42
|
-
# because plain scalars cannot start with '!', '&', or '*'.
|
43
|
-
BaseDetector.add_detector('tag:yaml.org,2002:yaml',/^(?:!|&|\*)$/,'!&*')
|
44
|
-
end
|