RbYAML 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +19 -0
- data/README +31 -0
- data/lib/rbyaml.rb +378 -0
- data/lib/rbyaml/composer.rb +189 -0
- data/lib/rbyaml/constructor.rb +374 -0
- data/lib/rbyaml/detector.rb +44 -0
- data/lib/rbyaml/dumper.rb +40 -0
- data/lib/rbyaml/emitter.rb +1116 -0
- data/lib/rbyaml/error.rb +81 -0
- data/lib/rbyaml/events.rb +92 -0
- data/lib/rbyaml/loader.rb +49 -0
- data/lib/rbyaml/nodes.rb +69 -0
- data/lib/rbyaml/parser.rb +488 -0
- data/lib/rbyaml/reader.rb +127 -0
- data/lib/rbyaml/representer.rb +183 -0
- data/lib/rbyaml/scanner.rb +1258 -0
- data/lib/rbyaml/serializer.rb +120 -0
- data/lib/rbyaml/test.rb +56 -0
- data/lib/rbyaml/tokens.rb +163 -0
- data/lib/rbyaml/yaml.rb +143 -0
- data/test/test_rbyaml.rb +18 -0
- data/test/yaml/gems.yml +130951 -0
- data/test/yaml/gems2.yml +113 -0
- data/test/yaml/test1.yml +3 -0
- data/test/yaml/test10.yml +8 -0
- data/test/yaml/test12.yml +8 -0
- data/test/yaml/test13.yml +4 -0
- data/test/yaml/test14.yml +4 -0
- data/test/yaml/test15.yml +8 -0
- data/test/yaml/test16.yml +7 -0
- data/test/yaml/test18.yml +6 -0
- data/test/yaml/test19.yml +5 -0
- data/test/yaml/test2.yml +3 -0
- data/test/yaml/test20.yml +6 -0
- data/test/yaml/test21.yml +4 -0
- data/test/yaml/test22.yml +4 -0
- data/test/yaml/test23.yml +13 -0
- data/test/yaml/test24.yml +14 -0
- data/test/yaml/test25.yml +7 -0
- data/test/yaml/test26.yml +7 -0
- data/test/yaml/test27.yml +29 -0
- data/test/yaml/test28.yml +26 -0
- data/test/yaml/test29.yml +13 -0
- data/test/yaml/test3.yml +8 -0
- data/test/yaml/test30.yml +7 -0
- data/test/yaml/test31.yml +2 -0
- data/test/yaml/test32.yml +13 -0
- data/test/yaml/test33.yml +2 -0
- data/test/yaml/test34.yml +8 -0
- data/test/yaml/test35.yml +4 -0
- data/test/yaml/test36.yml +8 -0
- data/test/yaml/test37.yml +2 -0
- data/test/yaml/test38.yml +8 -0
- data/test/yaml/test39.yml +2 -0
- data/test/yaml/test4.yml +8 -0
- data/test/yaml/test40.yml +3 -0
- data/test/yaml/test41.yml +5 -0
- data/test/yaml/test42.yml +12 -0
- data/test/yaml/test43.yml +15 -0
- data/test/yaml/test44.yml +23 -0
- data/test/yaml/test5.yml +3 -0
- data/test/yaml/test6.yml +5 -0
- data/test/yaml/test7.yml +10 -0
- data/test/yaml/test8.yml +10 -0
- data/test/yaml/test9.yml +8 -0
- metadata +111 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'rbyaml/error'
|
2
|
+
require 'rbyaml/events'
|
3
|
+
require 'rbyaml/nodes'
|
4
|
+
|
5
|
+
module RbYAML
|
6
|
+
class SerializerError < YAMLError
|
7
|
+
end
|
8
|
+
|
9
|
+
module Serializer
|
10
|
+
ANCHOR_TEMPLATE = "id%03d"
|
11
|
+
|
12
|
+
def initialize_serializer(explicit_start=nil, explicit_end=nil, version=nil, tags=nil)
|
13
|
+
@use_explicit_start = explicit_start
|
14
|
+
@use_explicit_end = explicit_end
|
15
|
+
@use_version = version
|
16
|
+
@use_tags = tags
|
17
|
+
@serialized_nodes = {}
|
18
|
+
@anchors = {}
|
19
|
+
@last_anchor_id = 0
|
20
|
+
@closed = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def open
|
24
|
+
if @closed.nil?
|
25
|
+
emit(StreamStartEvent.new)
|
26
|
+
@closed = false
|
27
|
+
elsif @closed
|
28
|
+
raise SerializerError.new("serializer is closed")
|
29
|
+
else
|
30
|
+
raise SerializerError.new("serializer is already opened")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def close
|
35
|
+
if @closed.nil?
|
36
|
+
raise SerializerError.new("serializer is not opened")
|
37
|
+
elsif !@closed
|
38
|
+
emit(StreamEndEvent.new)
|
39
|
+
@closed = true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def serialize(node)
|
44
|
+
if @closed.nil?
|
45
|
+
raise SerializerError.new("serializer is not opened")
|
46
|
+
elsif @closed
|
47
|
+
raise SerializerError.new("serializer is closed")
|
48
|
+
end
|
49
|
+
emit(DocumentStartEvent.new(@use_explicit_start,@use_version,@use_tags))
|
50
|
+
anchor_node(node)
|
51
|
+
serialize_node(node)
|
52
|
+
emit(DocumentEndEvent.new(@use_explicit_end))
|
53
|
+
@serialized_nodes = {}
|
54
|
+
@anchors = {}
|
55
|
+
@last_alias_id = 0
|
56
|
+
end
|
57
|
+
|
58
|
+
def anchor_node(node)
|
59
|
+
if @anchors.include?(node)
|
60
|
+
@anchors[node] ||= generate_anchor(node)
|
61
|
+
else
|
62
|
+
@anchors[node] = nil
|
63
|
+
if SequenceNode === node
|
64
|
+
for item in node.value
|
65
|
+
anchor_node(item)
|
66
|
+
end
|
67
|
+
elsif MappingNode === node
|
68
|
+
for key in node.value.keys
|
69
|
+
anchor_node(key)
|
70
|
+
anchor_node(node.value[key])
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def generate_anchor(node)
|
77
|
+
@last_anchor_id += 1
|
78
|
+
ANCHOR_TEMPLATE % @last_anchor_id
|
79
|
+
end
|
80
|
+
|
81
|
+
def serialize_node(node)
|
82
|
+
talias = @anchors[node]
|
83
|
+
if @serialized_nodes.include?(node)
|
84
|
+
emit(AliasEvent.new(talias))
|
85
|
+
else
|
86
|
+
@serialized_nodes[node] = true
|
87
|
+
if ScalarNode === node
|
88
|
+
detected_tag = detect(node.value)
|
89
|
+
implicit = node.tag == detect(node.value) || (node.tag == DEFAULT_SCALAR_TAG && detected_tag.nil?)
|
90
|
+
emit(ScalarEvent.new(talias, node.tag, implicit, node.value,nil,nil,node.style))
|
91
|
+
elsif SequenceNode === node
|
92
|
+
tag = node.tag
|
93
|
+
tag = nil if tag == DEFAULT_SEQUENCE_TAG && !@canonical
|
94
|
+
emit(SequenceStartEvent.new(talias, tag,nil,nil,node.flow_style))
|
95
|
+
for item in node.value
|
96
|
+
serialize_node(item)
|
97
|
+
end
|
98
|
+
emit(SequenceEndEvent.new)
|
99
|
+
elsif MappingNode === node
|
100
|
+
tag = node.tag
|
101
|
+
tag = nil if tag == DEFAULT_MAPPING_TAG && !@canonical
|
102
|
+
emit(MappingStartEvent.new(talias, tag,nil,nil,node.flow_style))
|
103
|
+
if node.value.respond_to?(:keys)
|
104
|
+
for key in node.value.keys
|
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
|
113
|
+
end
|
114
|
+
emit(MappingEndEvent.new)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
data/lib/rbyaml/test.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rbyaml/error'
|
2
|
+
require 'rbyaml/reader'
|
3
|
+
require 'rbyaml/scanner'
|
4
|
+
require 'rbyaml/parser'
|
5
|
+
require 'rbyaml/composer'
|
6
|
+
require 'rbyaml/constructor'
|
7
|
+
require 'rbyaml/detector'
|
8
|
+
|
9
|
+
class RbYAMLTester
|
10
|
+
include RbYAML::Reader, RbYAML::Scanner, RbYAML::Parser
|
11
|
+
|
12
|
+
def initialize(stream)
|
13
|
+
initialize_reader(stream)
|
14
|
+
initialize_scanner
|
15
|
+
initialize_parser
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class RbYAMLTester2
|
20
|
+
include RbYAML::Reader, RbYAML::Scanner, RbYAML::Parser, RbYAML::Composer
|
21
|
+
|
22
|
+
def initialize(stream)
|
23
|
+
initialize_reader(stream)
|
24
|
+
initialize_scanner
|
25
|
+
initialize_parser
|
26
|
+
initialize_composer
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class RbYAMLTester3
|
31
|
+
include RbYAML::Reader, RbYAML::Scanner, RbYAML::Parser, RbYAML::Composer, RbYAML::Constructor, RbYAML::Detector
|
32
|
+
|
33
|
+
def initialize(stream)
|
34
|
+
initialize_reader(stream)
|
35
|
+
initialize_scanner
|
36
|
+
initialize_parser
|
37
|
+
initialize_composer
|
38
|
+
initialize_constructor
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
i=0
|
43
|
+
begin
|
44
|
+
File.open(ARGV.shift) {|f|
|
45
|
+
tester = RbYAMLTester3.new(f)
|
46
|
+
tester.each_document {|doc|
|
47
|
+
puts "#{doc.inspect}"
|
48
|
+
# i += 1
|
49
|
+
# if (i%10000) == 0
|
50
|
+
# puts "token ##{i}"
|
51
|
+
# end
|
52
|
+
}
|
53
|
+
}
|
54
|
+
rescue RbYAML::MarkedYAMLError => err
|
55
|
+
puts "MarkedYAMLError: #{err}"
|
56
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
|
2
|
+
module RbYAML
|
3
|
+
class Token
|
4
|
+
attr_reader :start_mark, :end_mark
|
5
|
+
def initialize(start_mark, end_mark)
|
6
|
+
@start_mark = start_mark
|
7
|
+
@end_mark = end_mark
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class DirectiveToken < Token
|
12
|
+
def tid
|
13
|
+
"<directive>"
|
14
|
+
end
|
15
|
+
attr_reader :name, :value
|
16
|
+
def initialize(name, value, start_mark, end_mark)
|
17
|
+
super(start_mark,end_mark)
|
18
|
+
@name = name
|
19
|
+
@value = value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class DocumentStartToken < Token
|
24
|
+
def tid
|
25
|
+
"<document start>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class DocumentEndToken < Token
|
30
|
+
def tid
|
31
|
+
"<document end>"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class StreamStartToken < Token
|
36
|
+
def tid
|
37
|
+
"<stream start>"
|
38
|
+
end
|
39
|
+
attr_reader :encoding
|
40
|
+
def initialize(start_mark=nil, end_mark=nil, encoding=nil)
|
41
|
+
super(start_mark,end_mark)
|
42
|
+
@encoding = encoding
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class StreamEndToken < Token
|
47
|
+
def tid
|
48
|
+
"<stream end>"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
class BlockSequenceStartToken < Token
|
53
|
+
def tid
|
54
|
+
"<block sequence start>"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class BlockMappingStartToken < Token
|
59
|
+
def tid
|
60
|
+
"<block mapping start>"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
class BlockEndToken < Token
|
65
|
+
def tid
|
66
|
+
"<block end>"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
class FlowSequenceStartToken < Token
|
71
|
+
def tid
|
72
|
+
"["
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
class FlowMappingStartToken < Token
|
77
|
+
def tid
|
78
|
+
"{"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
class FlowSequenceEndToken < Token
|
83
|
+
def tid
|
84
|
+
"]"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
class FlowMappingEndToken < Token
|
89
|
+
def tid
|
90
|
+
"}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class KeyToken < Token
|
95
|
+
def tid
|
96
|
+
"?"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class ValueToken < Token
|
101
|
+
def tid
|
102
|
+
":"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class BlockEntryToken < Token
|
107
|
+
def tid
|
108
|
+
"-"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
class FlowEntryToken < Token
|
113
|
+
def tid
|
114
|
+
","
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class AliasToken < Token
|
119
|
+
def tid
|
120
|
+
"<alias>"
|
121
|
+
end
|
122
|
+
attr_reader :value
|
123
|
+
def initialize(value, start_mark, end_mark)
|
124
|
+
super(start_mark,end_mark)
|
125
|
+
@value = value
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
class AnchorToken < Token
|
130
|
+
def tid
|
131
|
+
"<anchor>"
|
132
|
+
end
|
133
|
+
attr_reader :value
|
134
|
+
def initialize(value, start_mark, end_mark)
|
135
|
+
super(start_mark,end_mark)
|
136
|
+
@value = value
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
class TagToken < Token
|
141
|
+
def tid
|
142
|
+
"<tag>"
|
143
|
+
end
|
144
|
+
attr_reader :value
|
145
|
+
def initialize(value, start_mark, end_mark)
|
146
|
+
super(start_mark,end_mark)
|
147
|
+
@value = value
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class ScalarToken < Token
|
152
|
+
def tid
|
153
|
+
"<scalar>"
|
154
|
+
end
|
155
|
+
attr_reader :value, :implicit, :style
|
156
|
+
def initialize(value, implicit, start_mark, end_mark, style=nil)
|
157
|
+
super(start_mark, end_mark)
|
158
|
+
@value = value
|
159
|
+
@implicit = implicit
|
160
|
+
@style = style
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
data/lib/rbyaml/yaml.rb
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
require 'rbyaml/error'
|
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
|
+
require 'rbyaml/tokens'
|
18
|
+
require 'rbyaml/events'
|
19
|
+
require 'rbyaml/nodes'
|
20
|
+
|
21
|
+
require 'rbyaml/loader'
|
22
|
+
require 'rbyaml/dumper'
|
23
|
+
|
24
|
+
module RbYAML
|
25
|
+
def self._scan(stream, loader=Loader)
|
26
|
+
l = loader.new(stream)
|
27
|
+
yield l.get_token while l.check_token
|
28
|
+
end
|
29
|
+
|
30
|
+
def self._parse(stream, loader=Loader)
|
31
|
+
l = loader.new(stream)
|
32
|
+
yield l.get_event while l.check_event
|
33
|
+
end
|
34
|
+
|
35
|
+
def self._compose(stream, loader=Loader)
|
36
|
+
l = loader.new(stream)
|
37
|
+
l.get_node if l.check_node
|
38
|
+
end
|
39
|
+
|
40
|
+
def self._compose_all(stream, loader=Loader)
|
41
|
+
l = loader.new(stream)
|
42
|
+
yield l.get_node while l.check_node
|
43
|
+
end
|
44
|
+
|
45
|
+
def self._load_all(stream, loader=Loader)
|
46
|
+
l = loader.new(stream)
|
47
|
+
yield l.get_data while l.check_data
|
48
|
+
end
|
49
|
+
|
50
|
+
def self._load(stream, loader=Loader)
|
51
|
+
l = loader.new(stream)
|
52
|
+
l.get_data if l.check_data
|
53
|
+
end
|
54
|
+
|
55
|
+
def self._safe_load_all(stream)
|
56
|
+
_load_all(stream, SafeLoader)
|
57
|
+
end
|
58
|
+
|
59
|
+
def self._safe_load(stream)
|
60
|
+
_load(stream, SafeLoader)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self._emit(events, stream=nil, dumper=Dumper,canonical=nil, indent=nil, width=nil,line_break=nil)
|
64
|
+
if stream.nil?
|
65
|
+
require 'stringio'
|
66
|
+
stream = StringIO.new
|
67
|
+
end
|
68
|
+
dumper = dumper.new(stream,canonical,indent,width,line_break)
|
69
|
+
for event in events
|
70
|
+
dumper.emit(event)
|
71
|
+
end
|
72
|
+
stream.string if StringIO === stream
|
73
|
+
end
|
74
|
+
|
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)
|
76
|
+
if stream.nil?
|
77
|
+
require 'stringio'
|
78
|
+
stream = StringIO.new
|
79
|
+
end
|
80
|
+
dumper = dumper.new(stream,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
81
|
+
dumper.open
|
82
|
+
for node in nodes
|
83
|
+
dumper.serialize(node)
|
84
|
+
end
|
85
|
+
dumper.close
|
86
|
+
stream.string if StringIO === stream
|
87
|
+
end
|
88
|
+
|
89
|
+
def self._serialize(node, stream=nil, dumper=Dumper, *kwds)
|
90
|
+
_serialize_all([node], stream, dumper, *kwds)
|
91
|
+
end
|
92
|
+
|
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)
|
94
|
+
if stream.nil?
|
95
|
+
require 'stringio'
|
96
|
+
stream = StringIO.new
|
97
|
+
end
|
98
|
+
dumper = dumper.new(stream,canonical,indent,width,line_break,version,tags,explicit_start,explicit_end)
|
99
|
+
dumper.open
|
100
|
+
for data in documents
|
101
|
+
dumper.represent(data)
|
102
|
+
end
|
103
|
+
dumper.close
|
104
|
+
stream.string if StringIO === stream
|
105
|
+
end
|
106
|
+
|
107
|
+
def self._dump(data, stream=nil, dumper=Dumper, *kwds)
|
108
|
+
_dump_all([data], stream, dumper, *kwds)
|
109
|
+
end
|
110
|
+
|
111
|
+
def self._safe_dump_all(documents, stream=nil, *kwds)
|
112
|
+
_dump_all(documents, stream, SafeDumper, *kwds)
|
113
|
+
end
|
114
|
+
|
115
|
+
def self._safe_dump(data, stream=nil, *kwds)
|
116
|
+
_dump_all([data], stream, SafeDumper, *kwds)
|
117
|
+
end
|
118
|
+
|
119
|
+
def self._add_detector(tag, regexp, first=nil, loader=Loader, dumper=Dumper)
|
120
|
+
loader.add_detector(tag, regexp, first)
|
121
|
+
dumper.add_detector(tag, regexp, first)
|
122
|
+
end
|
123
|
+
|
124
|
+
def self._add_resolver(tag, path, loader=Loader)
|
125
|
+
loader.add_resolver(tag, path)
|
126
|
+
end
|
127
|
+
|
128
|
+
def self._add_constructor(tag, constructor, loader=Loader)
|
129
|
+
loader.add_constructor(tag, constructor)
|
130
|
+
end
|
131
|
+
|
132
|
+
def self._add_multi_constructor(tag_prefix, multi_constructor, loader=Loader)
|
133
|
+
loader.add_multi_constructor(tag_prefix, multi_constructor)
|
134
|
+
end
|
135
|
+
|
136
|
+
def self._dump_ruby_object(data, dumper=Dumper)
|
137
|
+
_dump(data,nil,dumper)
|
138
|
+
end
|
139
|
+
|
140
|
+
class SimpleDetector
|
141
|
+
include Detector
|
142
|
+
end
|
143
|
+
end
|