km-psych 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +129 -0
- data/ext/psych/emitter.c +488 -0
- data/ext/psych/emitter.h +8 -0
- data/ext/psych/extconf.rb +22 -0
- data/ext/psych/parser.c +349 -0
- data/ext/psych/parser.h +6 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +20 -0
- data/ext/psych/to_ruby.c +41 -0
- data/ext/psych/to_ruby.h +8 -0
- data/ext/psych/yaml_tree.c +24 -0
- data/ext/psych/yaml_tree.h +8 -0
- data/lib/km-psych.rb +244 -0
- data/lib/psych/coder.rb +86 -0
- data/lib/psych/core_ext.rb +38 -0
- data/lib/psych/deprecated.rb +82 -0
- data/lib/psych/handler.rb +221 -0
- data/lib/psych/json.rb +6 -0
- data/lib/psych/json/stream.rb +32 -0
- data/lib/psych/json/tree_builder.rb +32 -0
- data/lib/psych/nodes.rb +77 -0
- data/lib/psych/nodes/alias.rb +18 -0
- data/lib/psych/nodes/document.rb +60 -0
- data/lib/psych/nodes/mapping.rb +56 -0
- data/lib/psych/nodes/node.rb +42 -0
- data/lib/psych/nodes/scalar.rb +67 -0
- data/lib/psych/nodes/sequence.rb +81 -0
- data/lib/psych/nodes/stream.rb +37 -0
- data/lib/psych/omap.rb +4 -0
- data/lib/psych/parser.rb +44 -0
- data/lib/psych/scalar_scanner.rb +105 -0
- data/lib/psych/set.rb +4 -0
- data/lib/psych/stream.rb +53 -0
- data/lib/psych/tree_builder.rb +94 -0
- data/lib/psych/visitors.rb +5 -0
- data/lib/psych/visitors/emitter.rb +41 -0
- data/lib/psych/visitors/json_tree.rb +14 -0
- data/lib/psych/visitors/to_ruby.rb +263 -0
- data/lib/psych/visitors/visitor.rb +27 -0
- data/lib/psych/visitors/yaml_tree.rb +342 -0
- data/test/psych/helper.rb +63 -0
- data/test/psych/json/test_stream.rb +75 -0
- data/test/psych/test_alias_and_anchor.rb +26 -0
- data/test/psych/test_array.rb +19 -0
- data/test/psych/test_boolean.rb +36 -0
- data/test/psych/test_class.rb +17 -0
- data/test/psych/test_coder.rb +169 -0
- data/test/psych/test_date_time.rb +17 -0
- data/test/psych/test_deprecated.rb +210 -0
- data/test/psych/test_document.rb +46 -0
- data/test/psych/test_emitter.rb +88 -0
- data/test/psych/test_encoding.rb +179 -0
- data/test/psych/test_engine_manager.rb +57 -0
- data/test/psych/test_exception.rb +39 -0
- data/test/psych/test_hash.rb +30 -0
- data/test/psych/test_json_tree.rb +43 -0
- data/test/psych/test_null.rb +19 -0
- data/test/psych/test_object.rb +27 -0
- data/test/psych/test_omap.rb +68 -0
- data/test/psych/test_parser.rb +216 -0
- data/test/psych/test_psych.rb +133 -0
- data/test/psych/test_scalar.rb +11 -0
- data/test/psych/test_scalar_scanner.rb +70 -0
- data/test/psych/test_serialize_subclasses.rb +38 -0
- data/test/psych/test_set.rb +49 -0
- data/test/psych/test_stream.rb +49 -0
- data/test/psych/test_string.rb +49 -0
- data/test/psych/test_struct.rb +51 -0
- data/test/psych/test_symbol.rb +17 -0
- data/test/psych/test_to_yaml_properties.rb +63 -0
- data/test/psych/test_tree_builder.rb +79 -0
- data/test/psych/test_yaml.rb +1251 -0
- data/test/psych/visitors/test_emitter.rb +124 -0
- data/test/psych/visitors/test_to_ruby.rb +325 -0
- data/test/psych/visitors/test_yaml_tree.rb +149 -0
- metadata +187 -0
@@ -0,0 +1,221 @@
|
|
1
|
+
module Psych
|
2
|
+
###
|
3
|
+
# Psych::Handler is an abstract base class that defines the events used
|
4
|
+
# when dealing with Psych::Parser. Clients who want to use Psych::Parser
|
5
|
+
# should implement a class that inherits from Psych::Handler and define
|
6
|
+
# events that they can handle.
|
7
|
+
#
|
8
|
+
# Psych::Handler defines all events that Psych::Parser can possibly send to
|
9
|
+
# event handlers.
|
10
|
+
#
|
11
|
+
# See Psych::Parser for more details
|
12
|
+
class Handler
|
13
|
+
###
|
14
|
+
# Called with +encoding+ when the YAML stream starts. This method is
|
15
|
+
# called once per stream. A stream may contain multiple documents.
|
16
|
+
#
|
17
|
+
# See the constants in Psych::Parser for the possible values of +encoding+.
|
18
|
+
def start_stream encoding
|
19
|
+
end
|
20
|
+
|
21
|
+
###
|
22
|
+
# Called when the document starts with the declared +version+,
|
23
|
+
# +tag_directives+, if the document is +implicit+.
|
24
|
+
#
|
25
|
+
# +version+ will be an array of integers indicating the YAML version being
|
26
|
+
# dealt with, +tag_directives+ is a list of tuples indicating the prefix
|
27
|
+
# and suffix of each tag, and +implicit+ is a boolean indicating whether
|
28
|
+
# the document is started implicitly.
|
29
|
+
#
|
30
|
+
# === Example
|
31
|
+
#
|
32
|
+
# Given the following YAML:
|
33
|
+
#
|
34
|
+
# %YAML 1.1
|
35
|
+
# %TAG ! tag:tenderlovemaking.com,2009:
|
36
|
+
# --- !squee
|
37
|
+
#
|
38
|
+
# The parameters for start_document must be this:
|
39
|
+
#
|
40
|
+
# version # => [1, 1]
|
41
|
+
# tag_directives # => [["!", "tag:tenderlovemaking.com,2009:"]]
|
42
|
+
# implicit # => false
|
43
|
+
def start_document version, tag_directives, implicit
|
44
|
+
end
|
45
|
+
|
46
|
+
###
|
47
|
+
# Called with the document ends. +implicit+ is a boolean value indicating
|
48
|
+
# whether or not the document has an implicit ending.
|
49
|
+
#
|
50
|
+
# === Example
|
51
|
+
#
|
52
|
+
# Given the following YAML:
|
53
|
+
#
|
54
|
+
# ---
|
55
|
+
# hello world
|
56
|
+
#
|
57
|
+
# +implicit+ will be true. Given this YAML:
|
58
|
+
#
|
59
|
+
# ---
|
60
|
+
# hello world
|
61
|
+
# ...
|
62
|
+
#
|
63
|
+
# +implicit+ will be false.
|
64
|
+
def end_document implicit
|
65
|
+
end
|
66
|
+
|
67
|
+
###
|
68
|
+
# Called when an alias is found to +anchor+. +anchor+ will be the name
|
69
|
+
# of the anchor found.
|
70
|
+
#
|
71
|
+
# === Example
|
72
|
+
#
|
73
|
+
# Here we have an example of an array that references itself in YAML:
|
74
|
+
#
|
75
|
+
# --- &ponies
|
76
|
+
# - first element
|
77
|
+
# - *ponies
|
78
|
+
#
|
79
|
+
# &ponies is the achor, *ponies is the alias. In this case, alias is
|
80
|
+
# called with "ponies".
|
81
|
+
def alias anchor
|
82
|
+
end
|
83
|
+
|
84
|
+
###
|
85
|
+
# Called when a scalar +value+ is found. The scalar may have an
|
86
|
+
# +anchor+, a +tag+, be implicitly +plain+ or implicitly +quoted+
|
87
|
+
#
|
88
|
+
# +value+ is the string value of the scalar
|
89
|
+
# +anchor+ is an associated anchor or nil
|
90
|
+
# +tag+ is an associated tag or nil
|
91
|
+
# +plain+ is a boolean value
|
92
|
+
# +quoted+ is a boolean value
|
93
|
+
# +style+ is an integer idicating the string style
|
94
|
+
#
|
95
|
+
# See the constants in Psych::Nodes::Scalar for the possible values of
|
96
|
+
# +style+
|
97
|
+
#
|
98
|
+
# === Example
|
99
|
+
#
|
100
|
+
# Here is a YAML document that exercises most of the possible ways this
|
101
|
+
# method can be called:
|
102
|
+
#
|
103
|
+
# ---
|
104
|
+
# - !str "foo"
|
105
|
+
# - &anchor fun
|
106
|
+
# - many
|
107
|
+
# lines
|
108
|
+
# - |
|
109
|
+
# many
|
110
|
+
# newlines
|
111
|
+
#
|
112
|
+
# The above YAML document contains a list with four strings. Here are
|
113
|
+
# the parameters sent to this method in the same order:
|
114
|
+
#
|
115
|
+
# # value anchor tag plain quoted style
|
116
|
+
# ["foo", nil, "!str", false, false, 3 ]
|
117
|
+
# ["fun", "anchor", nil, true, false, 1 ]
|
118
|
+
# ["many lines", nil, nil, true, false, 1 ]
|
119
|
+
# ["many\nnewlines\n", nil, nil, false, true, 4 ]
|
120
|
+
#
|
121
|
+
def scalar value, anchor, tag, plain, quoted, style
|
122
|
+
end
|
123
|
+
|
124
|
+
###
|
125
|
+
# Called when a sequence is started.
|
126
|
+
#
|
127
|
+
# +anchor+ is the anchor associated with the sequence or nil.
|
128
|
+
# +tag+ is the tag associated with the sequence or nil.
|
129
|
+
# +implicit+ a boolean indicating whether or not the sequence was implicitly
|
130
|
+
# started.
|
131
|
+
# +style+ is an integer indicating the list style.
|
132
|
+
#
|
133
|
+
# See the constants in Psych::Nodes::Sequence for the possible values of
|
134
|
+
# +style+.
|
135
|
+
#
|
136
|
+
# === Example
|
137
|
+
#
|
138
|
+
# Here is a YAML document that exercises most of the possible ways this
|
139
|
+
# method can be called:
|
140
|
+
#
|
141
|
+
# ---
|
142
|
+
# - !!seq [
|
143
|
+
# a
|
144
|
+
# ]
|
145
|
+
# - &pewpew
|
146
|
+
# - b
|
147
|
+
#
|
148
|
+
# The above YAML document consists of three lists, an outer list that
|
149
|
+
# contains two inner lists. Here is a matrix of the parameters sent
|
150
|
+
# to represent these lists:
|
151
|
+
#
|
152
|
+
# # anchor tag implicit style
|
153
|
+
# [nil, nil, true, 1 ]
|
154
|
+
# [nil, "tag:yaml.org,2002:seq", false, 2 ]
|
155
|
+
# ["pewpew", nil, true, 1 ]
|
156
|
+
|
157
|
+
def start_sequence anchor, tag, implicit, style
|
158
|
+
end
|
159
|
+
|
160
|
+
###
|
161
|
+
# Called when a sequence ends.
|
162
|
+
def end_sequence
|
163
|
+
end
|
164
|
+
|
165
|
+
###
|
166
|
+
# Called when a map starts.
|
167
|
+
#
|
168
|
+
# +anchor+ is the anchor associated with the map or +nil+.
|
169
|
+
# +tag+ is the tag associated with the map or +nil+.
|
170
|
+
# +implicit+ is a boolean indicating whether or not the map was implicitly
|
171
|
+
# started.
|
172
|
+
# +style+ is an integer indicating the mapping style.
|
173
|
+
#
|
174
|
+
# See the constants in Psych::Nodes::Mapping for the possible values of
|
175
|
+
# +style+.
|
176
|
+
#
|
177
|
+
# === Example
|
178
|
+
#
|
179
|
+
# Here is a YAML document that exercises most of the possible ways this
|
180
|
+
# method can be called:
|
181
|
+
#
|
182
|
+
# ---
|
183
|
+
# k: !!map { hello: world }
|
184
|
+
# v: &pewpew
|
185
|
+
# hello: world
|
186
|
+
#
|
187
|
+
# The above YAML document consists of three maps, an outer map that contains
|
188
|
+
# two inner maps. Below is a matrix of the parameters sent in order to
|
189
|
+
# represent these three maps:
|
190
|
+
#
|
191
|
+
# # anchor tag implicit style
|
192
|
+
# [nil, nil, true, 1 ]
|
193
|
+
# [nil, "tag:yaml.org,2002:map", false, 2 ]
|
194
|
+
# ["pewpew", nil, true, 1 ]
|
195
|
+
|
196
|
+
def start_mapping anchor, tag, implicit, style
|
197
|
+
end
|
198
|
+
|
199
|
+
###
|
200
|
+
# Called when a map ends
|
201
|
+
def end_mapping
|
202
|
+
end
|
203
|
+
|
204
|
+
###
|
205
|
+
# Called when an empty event happens. (Which, as far as I can tell, is
|
206
|
+
# never).
|
207
|
+
def empty
|
208
|
+
end
|
209
|
+
|
210
|
+
###
|
211
|
+
# Called when the YAML stream ends
|
212
|
+
def end_stream
|
213
|
+
end
|
214
|
+
|
215
|
+
###
|
216
|
+
# Is this handler a streaming handler?
|
217
|
+
def streaming?
|
218
|
+
false
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
data/lib/psych/json.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Psych
|
2
|
+
module JSON
|
3
|
+
class Stream < Psych::Stream
|
4
|
+
class Emitter < Psych::Stream::Emitter # :nodoc:
|
5
|
+
def start_document version, tag_directives, implicit
|
6
|
+
super(version, tag_directives, !streaming?)
|
7
|
+
end
|
8
|
+
|
9
|
+
def start_mapping anchor, tag, implicit, style
|
10
|
+
super(anchor, tag, implicit, Nodes::Mapping::FLOW)
|
11
|
+
end
|
12
|
+
|
13
|
+
def start_sequence anchor, tag, implicit, style
|
14
|
+
super(anchor, tag, implicit, Nodes::Sequence::FLOW)
|
15
|
+
end
|
16
|
+
|
17
|
+
def scalar value, anchor, tag, plain, quoted, style
|
18
|
+
if "tag:yaml.org,2002:null" == tag
|
19
|
+
super('null', nil, nil, true, false, Nodes::Scalar::PLAIN)
|
20
|
+
else
|
21
|
+
super
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def visit_String o
|
27
|
+
@emitter.scalar o.to_s, nil, nil, false, true, Nodes::Scalar::ANY
|
28
|
+
end
|
29
|
+
alias :visit_Symbol :visit_String
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Psych
|
2
|
+
module JSON
|
3
|
+
###
|
4
|
+
# Psych::JSON::TreeBuilder is an event based AST builder. Events are sent
|
5
|
+
# to an instance of Psych::JSON::TreeBuilder and a JSON AST is constructed.
|
6
|
+
class TreeBuilder < Psych::TreeBuilder
|
7
|
+
def start_document version, tag_directives, implicit
|
8
|
+
super(version, tag_directives, !streaming?)
|
9
|
+
end
|
10
|
+
|
11
|
+
def end_document implicit_end = !streaming?
|
12
|
+
super(implicit_end)
|
13
|
+
end
|
14
|
+
|
15
|
+
def start_mapping anchor, tag, implicit, style
|
16
|
+
super(anchor, tag, implicit, Nodes::Mapping::FLOW)
|
17
|
+
end
|
18
|
+
|
19
|
+
def start_sequence anchor, tag, implicit, style
|
20
|
+
super(anchor, tag, implicit, Nodes::Sequence::FLOW)
|
21
|
+
end
|
22
|
+
|
23
|
+
def scalar value, anchor, tag, plain, quoted, style
|
24
|
+
if "tag:yaml.org,2002:null" == tag
|
25
|
+
super('null', nil, nil, true, false, Nodes::Scalar::PLAIN)
|
26
|
+
else
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/psych/nodes.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'psych/nodes/node'
|
2
|
+
require 'psych/nodes/stream'
|
3
|
+
require 'psych/nodes/document'
|
4
|
+
require 'psych/nodes/sequence'
|
5
|
+
require 'psych/nodes/scalar'
|
6
|
+
require 'psych/nodes/mapping'
|
7
|
+
require 'psych/nodes/alias'
|
8
|
+
|
9
|
+
module Psych
|
10
|
+
###
|
11
|
+
# = Overview
|
12
|
+
#
|
13
|
+
# When using Psych.load to deserialize a YAML document, the document is
|
14
|
+
# translated to an intermediary AST. That intermediary AST is then
|
15
|
+
# translated in to a Ruby object graph.
|
16
|
+
#
|
17
|
+
# In the opposite direction, when using Psych.dump, the Ruby object graph is
|
18
|
+
# translated to an intermediary AST which is then converted to a YAML
|
19
|
+
# document.
|
20
|
+
#
|
21
|
+
# Psych::Nodes contains all of the classes that make up the nodes of a YAML
|
22
|
+
# AST. You can manually build an AST and use one of the visitors (see
|
23
|
+
# Psych::Visitors) to convert that AST to either a YAML document or to a
|
24
|
+
# Ruby object graph.
|
25
|
+
#
|
26
|
+
# Here is an example of building an AST that represents a list with one
|
27
|
+
# scalar:
|
28
|
+
#
|
29
|
+
# # Create our nodes
|
30
|
+
# stream = Psych::Nodes::Stream.new
|
31
|
+
# doc = Psych::Nodes::Document.new
|
32
|
+
# seq = Psych::Nodes::Sequence.new
|
33
|
+
# scalar = Psych::Nodes::Scalar.new('foo')
|
34
|
+
#
|
35
|
+
# # Build up our tree
|
36
|
+
# stream.children << doc
|
37
|
+
# doc.children << seq
|
38
|
+
# seq.children << scalar
|
39
|
+
#
|
40
|
+
# The stream is the root of the tree. We can then convert the tree to YAML:
|
41
|
+
#
|
42
|
+
# stream.to_yaml => "---\n- foo\n"
|
43
|
+
#
|
44
|
+
# Or convert it to Ruby:
|
45
|
+
#
|
46
|
+
# stream.to_ruby => [["foo"]]
|
47
|
+
#
|
48
|
+
# == YAML AST Requirements
|
49
|
+
#
|
50
|
+
# A valid YAML AST *must* have one Psych::Nodes::Stream at the root. A
|
51
|
+
# Psych::Nodes::Stream node must have 1 or more Psych::Nodes::Document nodes
|
52
|
+
# as children.
|
53
|
+
#
|
54
|
+
# Psych::Nodes::Document nodes must have one and *only* one child. That child
|
55
|
+
# may be one of:
|
56
|
+
#
|
57
|
+
# * Psych::Nodes::Sequence
|
58
|
+
# * Psych::Nodes::Mapping
|
59
|
+
# * Psych::Nodes::Scalar
|
60
|
+
#
|
61
|
+
# Psych::Nodes::Sequence and Psych::Nodes::Mapping nodes may have many
|
62
|
+
# children, but Psych::Nodes::Mapping nodes should have an even number of
|
63
|
+
# children.
|
64
|
+
#
|
65
|
+
# All of these are valid children for Psych::Nodes::Sequence and
|
66
|
+
# Psych::Nodes::Mapping nodes:
|
67
|
+
#
|
68
|
+
# * Psych::Nodes::Sequence
|
69
|
+
# * Psych::Nodes::Mapping
|
70
|
+
# * Psych::Nodes::Scalar
|
71
|
+
# * Psych::Nodes::Alias
|
72
|
+
#
|
73
|
+
# Psych::Nodes::Scalar and Psych::Nodes::Alias are both terminal nodes and
|
74
|
+
# should not have any children.
|
75
|
+
module Nodes
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Psych
|
2
|
+
module Nodes
|
3
|
+
###
|
4
|
+
# This class represents a {YAML Alias}[http://yaml.org/spec/1.1/#alias].
|
5
|
+
# It points to an +anchor+.
|
6
|
+
#
|
7
|
+
# A Psych::Nodes::Alias is a terminal node and may have no children.
|
8
|
+
class Alias < Psych::Nodes::Node
|
9
|
+
# The anchor this alias links to
|
10
|
+
attr_accessor :anchor
|
11
|
+
|
12
|
+
# Create a new Alias that points to an +anchor+
|
13
|
+
def initialize anchor
|
14
|
+
@anchor = anchor
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Psych
|
2
|
+
module Nodes
|
3
|
+
###
|
4
|
+
# This represents a YAML Document. This node must be a child of
|
5
|
+
# Psych::Nodes::Stream. A Psych::Nodes::Document must have one child,
|
6
|
+
# and that child may be one of the following:
|
7
|
+
#
|
8
|
+
# * Psych::Nodes::Sequence
|
9
|
+
# * Psych::Nodes::Mapping
|
10
|
+
# * Psych::Nodes::Scalar
|
11
|
+
class Document < Psych::Nodes::Node
|
12
|
+
# The version of the YAML document
|
13
|
+
attr_accessor :version
|
14
|
+
|
15
|
+
# A list of tag directives for this document
|
16
|
+
attr_accessor :tag_directives
|
17
|
+
|
18
|
+
# Was this document implicitly created?
|
19
|
+
attr_accessor :implicit
|
20
|
+
|
21
|
+
# Is the end of the document implicit?
|
22
|
+
attr_accessor :implicit_end
|
23
|
+
|
24
|
+
###
|
25
|
+
# Create a new Psych::Nodes::Document object.
|
26
|
+
#
|
27
|
+
# +version+ is a list indicating the YAML version.
|
28
|
+
# +tags_directives+ is a list of tag directive declarations
|
29
|
+
# +implicit+ is a flag indicating whether the document will be implicitly
|
30
|
+
# started.
|
31
|
+
#
|
32
|
+
# == Example:
|
33
|
+
# This creates a YAML document object that represents a YAML 1.1 document
|
34
|
+
# with one tag directive, and has an implicit start:
|
35
|
+
#
|
36
|
+
# Psych::Nodes::Document.new(
|
37
|
+
# [1,1],
|
38
|
+
# [["!", "tag:tenderlovemaking.com,2009:"]],
|
39
|
+
# true
|
40
|
+
# )
|
41
|
+
#
|
42
|
+
# == See Also
|
43
|
+
# See also Psych::Handler#start_document
|
44
|
+
def initialize version = [], tag_directives = [], implicit = false
|
45
|
+
super()
|
46
|
+
@version = version
|
47
|
+
@tag_directives = tag_directives
|
48
|
+
@implicit = implicit
|
49
|
+
@implicit_end = true
|
50
|
+
end
|
51
|
+
|
52
|
+
###
|
53
|
+
# Returns the root node. A Document may only have one root node:
|
54
|
+
# http://yaml.org/spec/1.1/#id898031
|
55
|
+
def root
|
56
|
+
children.first
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|