RbYAML 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README +1 -1
  2. data/lib/rbyaml/composer.rb +28 -25
  3. data/lib/rbyaml/composer.rb.~1.2.~ +109 -0
  4. data/lib/rbyaml/constructor.rb +94 -84
  5. data/lib/rbyaml/constructor.rb.~1.2.~ +381 -0
  6. data/lib/rbyaml/dumper.rb +10 -17
  7. data/lib/rbyaml/dumper.rb.~1.2.~ +43 -0
  8. data/lib/rbyaml/emitter.rb +13 -26
  9. data/lib/rbyaml/emitter.rb.~1.2.~ +1116 -0
  10. data/lib/rbyaml/error.rb +15 -21
  11. data/lib/rbyaml/events.rb +29 -5
  12. data/lib/rbyaml/events.rb.~1.2.~ +93 -0
  13. data/lib/rbyaml/loader.rb +11 -23
  14. data/lib/rbyaml/loader.rb.~1.2.~ +52 -0
  15. data/lib/rbyaml/nodes.rb +13 -9
  16. data/lib/rbyaml/nodes.rb.~1.2.~ +52 -0
  17. data/lib/rbyaml/parser.rb +481 -343
  18. data/lib/rbyaml/parser.rb.old +531 -0
  19. data/lib/rbyaml/parser.rb.~1.2.~ +494 -0
  20. data/lib/rbyaml/reader.rb.~1.1.1.1.~ +127 -0
  21. data/lib/rbyaml/representer.rb +26 -17
  22. data/lib/rbyaml/representer.rb.~1.2.~ +239 -0
  23. data/lib/rbyaml/resolver.rb +15 -15
  24. data/lib/rbyaml/resolver.rb.~1.1.~ +163 -0
  25. data/lib/rbyaml/scanner.rb +457 -366
  26. data/lib/rbyaml/scanner.rb.~1.2.~ +1259 -0
  27. data/lib/rbyaml/serializer.rb +19 -17
  28. data/lib/rbyaml/serializer.rb.~1.2.~ +115 -0
  29. data/lib/rbyaml/tokens.rb +44 -4
  30. data/lib/rbyaml/tokens.rb.~1.2.~ +164 -0
  31. data/lib/rbyaml/util.rb +28 -0
  32. data/lib/rbyaml/yaml.rb +12 -12
  33. data/lib/rbyaml/yaml.rb.~1.2.~ +136 -0
  34. data/test/test_bm.rb +28 -0
  35. data/test/test_bm_syck.rb +28 -0
  36. data/test/test_invoke.rb +31 -0
  37. data/test/test_one.rb +5 -0
  38. data/test/test_profile.rb +32 -0
  39. data/test/test_rbyaml.rb +2 -1
  40. data/test/test_rbyaml.rb.~1.2.~ +31 -0
  41. data/test/test_time.rb +13 -8
  42. data/test/test_time.rb.~1.1.~ +29 -0
  43. data/test/yamlx.rb +3563 -0
  44. metadata +27 -2
@@ -6,10 +6,12 @@ module RbYAML
6
6
  class SerializerError < YAMLError
7
7
  end
8
8
 
9
- module Serializer
9
+ class Serializer
10
10
  ANCHOR_TEMPLATE = "id%03d"
11
11
 
12
- def initialize_serializer(explicit_start=nil, explicit_end=nil, version=nil, tags=nil)
12
+ def initialize(emitter, resolver, explicit_start=nil, explicit_end=nil, version=nil, tags=nil)
13
+ @emitter = emitter
14
+ @resolver = resolver
13
15
  @use_explicit_start = explicit_start
14
16
  @use_explicit_end = explicit_end
15
17
  @use_version = version
@@ -22,7 +24,7 @@ module RbYAML
22
24
 
23
25
  def open
24
26
  if @closed.nil?
25
- emit(StreamStartEvent.new)
27
+ @emitter.emit(StreamStartEvent.new)
26
28
  @closed = false
27
29
  elsif @closed
28
30
  raise SerializerError.new("serializer is closed")
@@ -35,7 +37,7 @@ module RbYAML
35
37
  if @closed.nil?
36
38
  raise SerializerError.new("serializer is not opened")
37
39
  elsif !@closed
38
- emit(StreamEndEvent.new)
40
+ @emitter.emit(StreamEndEvent.new)
39
41
  @closed = true
40
42
  end
41
43
  end
@@ -46,10 +48,10 @@ module RbYAML
46
48
  elsif @closed
47
49
  raise SerializerError.new("serializer is closed")
48
50
  end
49
- emit(DocumentStartEvent.new(@use_explicit_start,@use_version,@use_tags))
51
+ @emitter.emit(DocumentStartEvent.new(@use_explicit_start,@use_version,@use_tags))
50
52
  anchor_node(node)
51
53
  serialize_node(node,nil,nil)
52
- emit(DocumentEndEvent.new(@use_explicit_end))
54
+ @emitter.emit(DocumentEndEvent.new(@use_explicit_end))
53
55
  @serialized_nodes = {}
54
56
  @anchors = {}
55
57
  @last_alias_id = 0
@@ -81,32 +83,32 @@ module RbYAML
81
83
  def serialize_node(node,parent,index)
82
84
  talias = @anchors[node]
83
85
  if @serialized_nodes.include?(node)
84
- emit(AliasEvent.new(talias))
86
+ @emitter.emit(AliasEvent.new(talias))
85
87
  else
86
88
  @serialized_nodes[node] = true
87
- descend_resolver(parent, index)
89
+ @resolver.descend_resolver(parent, index)
88
90
  if ScalarNode === node
89
- detected_tag = resolve(ScalarNode, node.value, [true,false])
90
- default_tag = resolve(ScalarNode, node.value, [false,true])
91
+ detected_tag = @resolver.resolve(ScalarNode, node.value, [true,false])
92
+ default_tag = @resolver.resolve(ScalarNode, node.value, [false,true])
91
93
  implicit = (node.tag == detected_tag), (node.tag == default_tag)
92
- emit(ScalarEvent.new(talias, node.tag, implicit, node.value,nil,nil,node.style))
94
+ @emitter.emit(ScalarEvent.new(talias, node.tag, implicit, node.value,nil,nil,node.style))
93
95
  elsif SequenceNode === node
94
- implicit = (node.tag == resolve(SequenceNode, node.value, true))
95
- emit(SequenceStartEvent.new(talias, node.tag, implicit,node.flow_style))
96
+ implicit = (node.tag == @resolver.resolve(SequenceNode, node.value, true))
97
+ @emitter.emit(SequenceStartEvent.new(talias, node.tag, implicit,node.flow_style))
96
98
  index = 0
97
99
  for item in node.value
98
100
  serialize_node(item,node,index)
99
101
  index += 1
100
102
  end
101
- emit(SequenceEndEvent.new)
103
+ @emitter.emit(SequenceEndEvent.new)
102
104
  elsif MappingNode === node
103
- implicit = (node.tag == resolve(MappingNode, node.value, true))
104
- emit(MappingStartEvent.new(talias, node.tag, implicit,node.flow_style))
105
+ implicit = (node.tag == @resolver.resolve(MappingNode, node.value, true))
106
+ @emitter.emit(MappingStartEvent.new(talias, node.tag, implicit,node.flow_style))
105
107
  for key, value in node.value
106
108
  serialize_node(key,node,nil)
107
109
  serialize_node(value,node,key)
108
110
  end
109
- emit(MappingEndEvent.new)
111
+ @emitter.emit(MappingEndEvent.new)
110
112
  end
111
113
  end
112
114
  end
@@ -0,0 +1,115 @@
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,nil,nil)
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,val in node.value
69
+ anchor_node(key)
70
+ anchor_node(val)
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,parent,index)
82
+ talias = @anchors[node]
83
+ if @serialized_nodes.include?(node)
84
+ emit(AliasEvent.new(talias))
85
+ else
86
+ @serialized_nodes[node] = true
87
+ descend_resolver(parent, index)
88
+ if ScalarNode === node
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)
92
+ emit(ScalarEvent.new(talias, node.tag, implicit, node.value,nil,nil,node.style))
93
+ elsif SequenceNode === node
94
+ implicit = (node.tag == resolve(SequenceNode, node.value, true))
95
+ emit(SequenceStartEvent.new(talias, node.tag, implicit,node.flow_style))
96
+ index = 0
97
+ for item in node.value
98
+ serialize_node(item,node,index)
99
+ index += 1
100
+ end
101
+ emit(SequenceEndEvent.new)
102
+ elsif MappingNode === node
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)
108
+ end
109
+ emit(MappingEndEvent.new)
110
+ end
111
+ end
112
+ end
113
+ end
114
+ end
115
+
@@ -1,14 +1,35 @@
1
1
 
2
2
  module RbYAML
3
+ Token = Struct.new(:start_mark, :end_mark)
4
+
3
5
  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
6
+ def __is_document_start; false; end
7
+ def __is_document_end; false; end
8
+ def __is_stream_start; false; end
9
+ def __is_stream_end; false; end
10
+ def __is_directive; false; end
11
+ def __is_block_sequence_start; false; end
12
+ def __is_block_mapping_start; false; end
13
+ def __is_block_end; false; end
14
+ def __is_flow_sequence_start; false; end
15
+ def __is_flow_mapping_start; false; end
16
+ def __is_flow_sequence_end; false; end
17
+ def __is_flow_mapping_end; false; end
18
+ def __is_key; false; end
19
+ def __is_value; false; end
20
+ def __is_block_entry; false; end
21
+ def __is_flow_entry; false; end
22
+ def __is_alias; false; end
23
+ def __is_anchor; false; end
24
+ def __is_tag; false; end
25
+ def __is_scalar; false; end
26
+ def hash
27
+ object_id
8
28
  end
9
29
  end
10
30
 
11
31
  class DirectiveToken < Token
32
+ def __is_directive; true; end
12
33
  def tid
13
34
  "<directive>"
14
35
  end
@@ -21,18 +42,21 @@ module RbYAML
21
42
  end
22
43
 
23
44
  class DocumentStartToken < Token
45
+ def __is_document_start; true; end
24
46
  def tid
25
47
  "<document start>"
26
48
  end
27
49
  end
28
50
 
29
51
  class DocumentEndToken < Token
52
+ def __is_document_end; true; end
30
53
  def tid
31
54
  "<document end>"
32
55
  end
33
56
  end
34
57
 
35
58
  class StreamStartToken < Token
59
+ def __is_stream_start; true; end
36
60
  def tid
37
61
  "<stream start>"
38
62
  end
@@ -44,78 +68,91 @@ module RbYAML
44
68
  end
45
69
 
46
70
  class StreamEndToken < Token
71
+ def __is_stream_end; true; end
47
72
  def tid
48
73
  "<stream end>"
49
74
  end
50
75
  end
51
76
 
52
77
  class BlockSequenceStartToken < Token
78
+ def __is_block_sequence_start; true; end
53
79
  def tid
54
80
  "<block sequence start>"
55
81
  end
56
82
  end
57
83
 
58
84
  class BlockMappingStartToken < Token
85
+ def __is_block_mapping_start; true; end
59
86
  def tid
60
87
  "<block mapping start>"
61
88
  end
62
89
  end
63
90
 
64
91
  class BlockEndToken < Token
92
+ def __is_block_end; true; end
65
93
  def tid
66
94
  "<block end>"
67
95
  end
68
96
  end
69
97
 
70
98
  class FlowSequenceStartToken < Token
99
+ def __is_flow_sequence_start; true; end
71
100
  def tid
72
101
  "["
73
102
  end
74
103
  end
75
104
 
76
105
  class FlowMappingStartToken < Token
106
+ def __is_flow_mapping_start; true; end
77
107
  def tid
78
108
  "{"
79
109
  end
80
110
  end
81
111
 
82
112
  class FlowSequenceEndToken < Token
113
+ def __is_flow_sequence_end; true; end
83
114
  def tid
84
115
  "]"
85
116
  end
86
117
  end
87
118
 
88
119
  class FlowMappingEndToken < Token
120
+ def __is_flow_mapping_end; true; end
89
121
  def tid
90
122
  "}"
91
123
  end
92
124
  end
93
125
 
94
126
  class KeyToken < Token
127
+ def __is_key; true; end
95
128
  def tid
96
129
  "?"
97
130
  end
98
131
  end
99
132
 
100
133
  class ValueToken < Token
134
+ def __is_value; true; end
101
135
  def tid
102
136
  ":"
103
137
  end
104
138
  end
105
139
 
106
140
  class BlockEntryToken < Token
141
+ def __is_block_entry; true; end
107
142
  def tid
108
143
  "-"
109
144
  end
110
145
  end
111
146
 
112
147
  class FlowEntryToken < Token
148
+ def __is_flow_entry; true; end
113
149
  def tid
114
150
  ","
115
151
  end
116
152
  end
117
153
 
118
154
  class AliasToken < Token
155
+ def __is_alias; true; end
119
156
  def tid
120
157
  "<alias>"
121
158
  end
@@ -127,6 +164,7 @@ module RbYAML
127
164
  end
128
165
 
129
166
  class AnchorToken < Token
167
+ def __is_anchor; true; end
130
168
  def tid
131
169
  "<anchor>"
132
170
  end
@@ -138,6 +176,7 @@ module RbYAML
138
176
  end
139
177
 
140
178
  class TagToken < Token
179
+ def __is_tag; true; end
141
180
  def tid
142
181
  "<tag>"
143
182
  end
@@ -149,6 +188,7 @@ module RbYAML
149
188
  end
150
189
 
151
190
  class ScalarToken < Token
191
+ def __is_scalar; true; end
152
192
  def tid
153
193
  "<scalar>"
154
194
  end
@@ -0,0 +1,164 @@
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, :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)
158
+ super(start_mark, end_mark)
159
+ @value = value
160
+ @plain = plain
161
+ @style = style
162
+ end
163
+ end
164
+ end