psych 3.0.0.beta2-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +16 -0
- data/.travis.yml +20 -0
- data/CHANGELOG.rdoc +576 -0
- data/Gemfile +3 -0
- data/Mavenfile +7 -0
- data/README.md +73 -0
- data/Rakefile +46 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/ext/psych/.gitignore +11 -0
- data/ext/psych/depend +3 -0
- data/ext/psych/extconf.rb +39 -0
- data/ext/psych/psych.c +34 -0
- data/ext/psych/psych.h +17 -0
- data/ext/psych/psych_emitter.c +554 -0
- data/ext/psych/psych_emitter.h +8 -0
- data/ext/psych/psych_parser.c +568 -0
- data/ext/psych/psych_parser.h +6 -0
- data/ext/psych/psych_to_ruby.c +39 -0
- data/ext/psych/psych_to_ruby.h +8 -0
- data/ext/psych/psych_yaml_tree.c +24 -0
- data/ext/psych/psych_yaml_tree.h +8 -0
- data/ext/psych/yaml/LICENSE +19 -0
- data/ext/psych/yaml/api.c +1392 -0
- data/ext/psych/yaml/config.h +10 -0
- data/ext/psych/yaml/dumper.c +394 -0
- data/ext/psych/yaml/emitter.c +2329 -0
- data/ext/psych/yaml/loader.c +444 -0
- data/ext/psych/yaml/parser.c +1374 -0
- data/ext/psych/yaml/reader.c +469 -0
- data/ext/psych/yaml/scanner.c +3576 -0
- data/ext/psych/yaml/writer.c +141 -0
- data/ext/psych/yaml/yaml.h +1971 -0
- data/ext/psych/yaml/yaml_private.h +662 -0
- data/lib/2.2/psych.so +0 -0
- data/lib/2.3/psych.so +0 -0
- data/lib/2.4/psych.so +0 -0
- data/lib/psych/class_loader.rb +102 -0
- data/lib/psych/coder.rb +95 -0
- data/lib/psych/core_ext.rb +19 -0
- data/lib/psych/exception.rb +14 -0
- data/lib/psych/handler.rb +250 -0
- data/lib/psych/handlers/document_stream.rb +23 -0
- data/lib/psych/handlers/recorder.rb +40 -0
- data/lib/psych/json/ruby_events.rb +20 -0
- data/lib/psych/json/stream.rb +17 -0
- data/lib/psych/json/tree_builder.rb +13 -0
- data/lib/psych/json/yaml_events.rb +30 -0
- data/lib/psych/nodes/alias.rb +19 -0
- data/lib/psych/nodes/document.rb +61 -0
- data/lib/psych/nodes/mapping.rb +57 -0
- data/lib/psych/nodes/node.rb +56 -0
- data/lib/psych/nodes/scalar.rb +68 -0
- data/lib/psych/nodes/sequence.rb +82 -0
- data/lib/psych/nodes/stream.rb +38 -0
- data/lib/psych/nodes.rb +78 -0
- data/lib/psych/omap.rb +5 -0
- data/lib/psych/parser.rb +52 -0
- data/lib/psych/scalar_scanner.rb +149 -0
- data/lib/psych/set.rb +5 -0
- data/lib/psych/stream.rb +38 -0
- data/lib/psych/streaming.rb +28 -0
- data/lib/psych/syntax_error.rb +22 -0
- data/lib/psych/tree_builder.rb +97 -0
- data/lib/psych/versions.rb +9 -0
- data/lib/psych/visitors/depth_first.rb +27 -0
- data/lib/psych/visitors/emitter.rb +52 -0
- data/lib/psych/visitors/json_tree.rb +25 -0
- data/lib/psych/visitors/to_ruby.rb +401 -0
- data/lib/psych/visitors/visitor.rb +20 -0
- data/lib/psych/visitors/yaml_tree.rb +551 -0
- data/lib/psych/visitors.rb +7 -0
- data/lib/psych/y.rb +10 -0
- data/lib/psych.rb +511 -0
- data/psych.gemspec +64 -0
- metadata +175 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Psych
|
3
|
+
module Streaming
|
4
|
+
module ClassMethods
|
5
|
+
###
|
6
|
+
# Create a new streaming emitter. Emitter will print to +io+. See
|
7
|
+
# Psych::Stream for an example.
|
8
|
+
def new io
|
9
|
+
emitter = const_get(:Emitter).new(io)
|
10
|
+
class_loader = ClassLoader.new
|
11
|
+
ss = ScalarScanner.new class_loader
|
12
|
+
super(emitter, ss, {})
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
###
|
17
|
+
# Start streaming using +encoding+
|
18
|
+
def start encoding = Nodes::Stream::UTF8
|
19
|
+
super.tap { yield self if block_given? }
|
20
|
+
ensure
|
21
|
+
finish if block_given?
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def register target, obj
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'psych/exception'
|
3
|
+
|
4
|
+
module Psych
|
5
|
+
class SyntaxError < Psych::Exception
|
6
|
+
attr_reader :file, :line, :column, :offset, :problem, :context
|
7
|
+
|
8
|
+
def initialize file, line, col, offset, problem, context
|
9
|
+
err = [problem, context].compact.join ' '
|
10
|
+
filename = file || '<unknown>'
|
11
|
+
message = "(%s): %s at line %d column %d" % [filename, err, line, col]
|
12
|
+
|
13
|
+
@file = file
|
14
|
+
@line = line
|
15
|
+
@column = col
|
16
|
+
@offset = offset
|
17
|
+
@problem = problem
|
18
|
+
@context = context
|
19
|
+
super(message)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'psych/handler'
|
3
|
+
|
4
|
+
module Psych
|
5
|
+
###
|
6
|
+
# This class works in conjunction with Psych::Parser to build an in-memory
|
7
|
+
# parse tree that represents a YAML document.
|
8
|
+
#
|
9
|
+
# == Example
|
10
|
+
#
|
11
|
+
# parser = Psych::Parser.new Psych::TreeBuilder.new
|
12
|
+
# parser.parse('--- foo')
|
13
|
+
# tree = parser.handler.root
|
14
|
+
#
|
15
|
+
# See Psych::Handler for documentation on the event methods used in this
|
16
|
+
# class.
|
17
|
+
class TreeBuilder < Psych::Handler
|
18
|
+
# Returns the root node for the built tree
|
19
|
+
attr_reader :root
|
20
|
+
|
21
|
+
# Create a new TreeBuilder instance
|
22
|
+
def initialize
|
23
|
+
@stack = []
|
24
|
+
@last = nil
|
25
|
+
@root = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
%w{
|
29
|
+
Sequence
|
30
|
+
Mapping
|
31
|
+
}.each do |node|
|
32
|
+
class_eval %{
|
33
|
+
def start_#{node.downcase}(anchor, tag, implicit, style)
|
34
|
+
n = Nodes::#{node}.new(anchor, tag, implicit, style)
|
35
|
+
@last.children << n
|
36
|
+
push n
|
37
|
+
end
|
38
|
+
|
39
|
+
def end_#{node.downcase}
|
40
|
+
pop
|
41
|
+
end
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
###
|
46
|
+
# Handles start_document events with +version+, +tag_directives+,
|
47
|
+
# and +implicit+ styling.
|
48
|
+
#
|
49
|
+
# See Psych::Handler#start_document
|
50
|
+
def start_document version, tag_directives, implicit
|
51
|
+
n = Nodes::Document.new version, tag_directives, implicit
|
52
|
+
@last.children << n
|
53
|
+
push n
|
54
|
+
end
|
55
|
+
|
56
|
+
###
|
57
|
+
# Handles end_document events with +version+, +tag_directives+,
|
58
|
+
# and +implicit+ styling.
|
59
|
+
#
|
60
|
+
# See Psych::Handler#start_document
|
61
|
+
def end_document implicit_end = !streaming?
|
62
|
+
@last.implicit_end = implicit_end
|
63
|
+
pop
|
64
|
+
end
|
65
|
+
|
66
|
+
def start_stream encoding
|
67
|
+
@root = Nodes::Stream.new(encoding)
|
68
|
+
push @root
|
69
|
+
end
|
70
|
+
|
71
|
+
def end_stream
|
72
|
+
pop
|
73
|
+
end
|
74
|
+
|
75
|
+
def scalar value, anchor, tag, plain, quoted, style
|
76
|
+
s = Nodes::Scalar.new(value,anchor,tag,plain,quoted,style)
|
77
|
+
@last.children << s
|
78
|
+
s
|
79
|
+
end
|
80
|
+
|
81
|
+
def alias anchor
|
82
|
+
@last.children << Nodes::Alias.new(anchor)
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
def push value
|
87
|
+
@stack.push value
|
88
|
+
@last = value
|
89
|
+
end
|
90
|
+
|
91
|
+
def pop
|
92
|
+
x = @stack.pop
|
93
|
+
@last = @stack.last
|
94
|
+
x
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Psych
|
3
|
+
module Visitors
|
4
|
+
class DepthFirst < Psych::Visitors::Visitor
|
5
|
+
def initialize block
|
6
|
+
@block = block
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def nary o
|
12
|
+
o.children.each { |x| visit x }
|
13
|
+
@block.call o
|
14
|
+
end
|
15
|
+
alias :visit_Psych_Nodes_Stream :nary
|
16
|
+
alias :visit_Psych_Nodes_Document :nary
|
17
|
+
alias :visit_Psych_Nodes_Sequence :nary
|
18
|
+
alias :visit_Psych_Nodes_Mapping :nary
|
19
|
+
|
20
|
+
def terminal o
|
21
|
+
@block.call o
|
22
|
+
end
|
23
|
+
alias :visit_Psych_Nodes_Scalar :terminal
|
24
|
+
alias :visit_Psych_Nodes_Alias :terminal
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Psych
|
3
|
+
module Visitors
|
4
|
+
class Emitter < Psych::Visitors::Visitor
|
5
|
+
def initialize io, options = {}
|
6
|
+
opts = [:indentation, :canonical, :line_width].find_all { |opt|
|
7
|
+
options.key?(opt)
|
8
|
+
}
|
9
|
+
|
10
|
+
if opts.empty?
|
11
|
+
@handler = Psych::Emitter.new io
|
12
|
+
else
|
13
|
+
du = Handler::DumperOptions.new
|
14
|
+
opts.each { |option| du.send :"#{option}=", options[option] }
|
15
|
+
@handler = Psych::Emitter.new io, du
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def visit_Psych_Nodes_Stream o
|
20
|
+
@handler.start_stream o.encoding
|
21
|
+
o.children.each { |c| accept c }
|
22
|
+
@handler.end_stream
|
23
|
+
end
|
24
|
+
|
25
|
+
def visit_Psych_Nodes_Document o
|
26
|
+
@handler.start_document o.version, o.tag_directives, o.implicit
|
27
|
+
o.children.each { |c| accept c }
|
28
|
+
@handler.end_document o.implicit_end
|
29
|
+
end
|
30
|
+
|
31
|
+
def visit_Psych_Nodes_Scalar o
|
32
|
+
@handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style
|
33
|
+
end
|
34
|
+
|
35
|
+
def visit_Psych_Nodes_Sequence o
|
36
|
+
@handler.start_sequence o.anchor, o.tag, o.implicit, o.style
|
37
|
+
o.children.each { |c| accept c }
|
38
|
+
@handler.end_sequence
|
39
|
+
end
|
40
|
+
|
41
|
+
def visit_Psych_Nodes_Mapping o
|
42
|
+
@handler.start_mapping o.anchor, o.tag, o.implicit, o.style
|
43
|
+
o.children.each { |c| accept c }
|
44
|
+
@handler.end_mapping
|
45
|
+
end
|
46
|
+
|
47
|
+
def visit_Psych_Nodes_Alias o
|
48
|
+
@handler.alias o.anchor
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'psych/json/ruby_events'
|
3
|
+
|
4
|
+
module Psych
|
5
|
+
module Visitors
|
6
|
+
class JSONTree < YAMLTree
|
7
|
+
include Psych::JSON::RubyEvents
|
8
|
+
|
9
|
+
def self.create options = {}
|
10
|
+
emitter = Psych::JSON::TreeBuilder.new
|
11
|
+
class_loader = ClassLoader.new
|
12
|
+
ss = ScalarScanner.new class_loader
|
13
|
+
new(emitter, ss, options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def accept target
|
17
|
+
if target.respond_to?(:encode_with)
|
18
|
+
dump_coder target
|
19
|
+
else
|
20
|
+
send(@dispatch_cache[target.class], target)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,401 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'psych/scalar_scanner'
|
3
|
+
require 'psych/class_loader'
|
4
|
+
require 'psych/exception'
|
5
|
+
|
6
|
+
unless defined?(Regexp::NOENCODING)
|
7
|
+
Regexp::NOENCODING = 32
|
8
|
+
end
|
9
|
+
|
10
|
+
module Psych
|
11
|
+
module Visitors
|
12
|
+
###
|
13
|
+
# This class walks a YAML AST, converting each node to Ruby
|
14
|
+
class ToRuby < Psych::Visitors::Visitor
|
15
|
+
def self.create
|
16
|
+
class_loader = ClassLoader.new
|
17
|
+
scanner = ScalarScanner.new class_loader
|
18
|
+
new(scanner, class_loader)
|
19
|
+
end
|
20
|
+
|
21
|
+
attr_reader :class_loader
|
22
|
+
|
23
|
+
def initialize ss, class_loader
|
24
|
+
super()
|
25
|
+
@st = {}
|
26
|
+
@ss = ss
|
27
|
+
@domain_types = Psych.domain_types
|
28
|
+
@class_loader = class_loader
|
29
|
+
end
|
30
|
+
|
31
|
+
def accept target
|
32
|
+
result = super
|
33
|
+
return result if @domain_types.empty? || !target.tag
|
34
|
+
|
35
|
+
key = target.tag.sub(/^[!\/]*/, '').sub(/(,\d+)\//, '\1:')
|
36
|
+
key = "tag:#{key}" unless key =~ /^(?:tag:|x-private)/
|
37
|
+
|
38
|
+
if @domain_types.key? key
|
39
|
+
value, block = @domain_types[key]
|
40
|
+
return block.call value, result
|
41
|
+
end
|
42
|
+
|
43
|
+
result
|
44
|
+
end
|
45
|
+
|
46
|
+
def deserialize o
|
47
|
+
if klass = resolve_class(Psych.load_tags[o.tag])
|
48
|
+
instance = klass.allocate
|
49
|
+
|
50
|
+
if instance.respond_to?(:init_with)
|
51
|
+
coder = Psych::Coder.new(o.tag)
|
52
|
+
coder.scalar = o.value
|
53
|
+
instance.init_with coder
|
54
|
+
end
|
55
|
+
|
56
|
+
return instance
|
57
|
+
end
|
58
|
+
|
59
|
+
return o.value if o.quoted
|
60
|
+
return @ss.tokenize(o.value) unless o.tag
|
61
|
+
|
62
|
+
case o.tag
|
63
|
+
when '!binary', 'tag:yaml.org,2002:binary'
|
64
|
+
o.value.unpack('m').first
|
65
|
+
when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
|
66
|
+
klass = resolve_class($1)
|
67
|
+
if klass
|
68
|
+
klass.allocate.replace o.value
|
69
|
+
else
|
70
|
+
o.value
|
71
|
+
end
|
72
|
+
when '!ruby/object:BigDecimal'
|
73
|
+
require 'bigdecimal' unless defined? BigDecimal
|
74
|
+
class_loader.big_decimal._load o.value
|
75
|
+
when "!ruby/object:DateTime"
|
76
|
+
class_loader.date_time
|
77
|
+
require 'date' unless defined? DateTime
|
78
|
+
@ss.parse_time(o.value).to_datetime
|
79
|
+
when '!ruby/encoding'
|
80
|
+
::Encoding.find o.value
|
81
|
+
when "!ruby/object:Complex"
|
82
|
+
class_loader.complex
|
83
|
+
Complex(o.value)
|
84
|
+
when "!ruby/object:Rational"
|
85
|
+
class_loader.rational
|
86
|
+
Rational(o.value)
|
87
|
+
when "!ruby/class", "!ruby/module"
|
88
|
+
resolve_class o.value
|
89
|
+
when "tag:yaml.org,2002:float", "!float"
|
90
|
+
Float(@ss.tokenize(o.value))
|
91
|
+
when "!ruby/regexp"
|
92
|
+
klass = class_loader.regexp
|
93
|
+
o.value =~ /^\/(.*)\/([mixn]*)$/m
|
94
|
+
source = $1
|
95
|
+
options = 0
|
96
|
+
lang = nil
|
97
|
+
($2 || '').split('').each do |option|
|
98
|
+
case option
|
99
|
+
when 'x' then options |= Regexp::EXTENDED
|
100
|
+
when 'i' then options |= Regexp::IGNORECASE
|
101
|
+
when 'm' then options |= Regexp::MULTILINE
|
102
|
+
when 'n' then options |= Regexp::NOENCODING
|
103
|
+
else lang = option
|
104
|
+
end
|
105
|
+
end
|
106
|
+
klass.new(*[source, options, lang].compact)
|
107
|
+
when "!ruby/range"
|
108
|
+
klass = class_loader.range
|
109
|
+
args = o.value.split(/([.]{2,3})/, 2).map { |s|
|
110
|
+
accept Nodes::Scalar.new(s)
|
111
|
+
}
|
112
|
+
args.push(args.delete_at(1) == '...')
|
113
|
+
klass.new(*args)
|
114
|
+
when /^!ruby\/sym(bol)?:?(.*)?$/
|
115
|
+
class_loader.symbolize o.value
|
116
|
+
else
|
117
|
+
@ss.tokenize o.value
|
118
|
+
end
|
119
|
+
end
|
120
|
+
private :deserialize
|
121
|
+
|
122
|
+
def visit_Psych_Nodes_Scalar o
|
123
|
+
register o, deserialize(o)
|
124
|
+
end
|
125
|
+
|
126
|
+
def visit_Psych_Nodes_Sequence o
|
127
|
+
if klass = resolve_class(Psych.load_tags[o.tag])
|
128
|
+
instance = klass.allocate
|
129
|
+
|
130
|
+
if instance.respond_to?(:init_with)
|
131
|
+
coder = Psych::Coder.new(o.tag)
|
132
|
+
coder.seq = o.children.map { |c| accept c }
|
133
|
+
instance.init_with coder
|
134
|
+
end
|
135
|
+
|
136
|
+
return instance
|
137
|
+
end
|
138
|
+
|
139
|
+
case o.tag
|
140
|
+
when nil
|
141
|
+
register_empty(o)
|
142
|
+
when '!omap', 'tag:yaml.org,2002:omap'
|
143
|
+
map = register(o, Psych::Omap.new)
|
144
|
+
o.children.each { |a|
|
145
|
+
map[accept(a.children.first)] = accept a.children.last
|
146
|
+
}
|
147
|
+
map
|
148
|
+
when /^!(?:seq|ruby\/array):(.*)$/
|
149
|
+
klass = resolve_class($1)
|
150
|
+
list = register(o, klass.allocate)
|
151
|
+
o.children.each { |c| list.push accept c }
|
152
|
+
list
|
153
|
+
else
|
154
|
+
register_empty(o)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def visit_Psych_Nodes_Mapping o
|
159
|
+
if Psych.load_tags[o.tag]
|
160
|
+
return revive(resolve_class(Psych.load_tags[o.tag]), o)
|
161
|
+
end
|
162
|
+
return revive_hash(register(o, {}), o) unless o.tag
|
163
|
+
|
164
|
+
case o.tag
|
165
|
+
when /^!ruby\/struct:?(.*)?$/
|
166
|
+
klass = resolve_class($1) if $1
|
167
|
+
|
168
|
+
if klass
|
169
|
+
s = register(o, klass.allocate)
|
170
|
+
|
171
|
+
members = {}
|
172
|
+
struct_members = s.members.map { |x| class_loader.symbolize x }
|
173
|
+
o.children.each_slice(2) do |k,v|
|
174
|
+
member = accept(k)
|
175
|
+
value = accept(v)
|
176
|
+
if struct_members.include?(class_loader.symbolize(member))
|
177
|
+
s.send("#{member}=", value)
|
178
|
+
else
|
179
|
+
members[member.to_s.sub(/^@/, '')] = value
|
180
|
+
end
|
181
|
+
end
|
182
|
+
init_with(s, members, o)
|
183
|
+
else
|
184
|
+
klass = class_loader.struct
|
185
|
+
members = o.children.map { |c| accept c }
|
186
|
+
h = Hash[*members]
|
187
|
+
s = klass.new(*h.map { |k,v|
|
188
|
+
class_loader.symbolize k
|
189
|
+
}).new(*h.map { |k,v| v })
|
190
|
+
register(o, s)
|
191
|
+
s
|
192
|
+
end
|
193
|
+
|
194
|
+
when /^!ruby\/object:?(.*)?$/
|
195
|
+
name = $1 || 'Object'
|
196
|
+
|
197
|
+
if name == 'Complex'
|
198
|
+
class_loader.complex
|
199
|
+
h = Hash[*o.children.map { |c| accept c }]
|
200
|
+
register o, Complex(h['real'], h['image'])
|
201
|
+
elsif name == 'Rational'
|
202
|
+
class_loader.rational
|
203
|
+
h = Hash[*o.children.map { |c| accept c }]
|
204
|
+
register o, Rational(h['numerator'], h['denominator'])
|
205
|
+
elsif name == 'Hash'
|
206
|
+
revive_hash(register(o, {}), o)
|
207
|
+
else
|
208
|
+
obj = revive((resolve_class(name) || class_loader.object), o)
|
209
|
+
obj
|
210
|
+
end
|
211
|
+
|
212
|
+
when /^!(?:str|ruby\/string)(?::(.*))?$/, 'tag:yaml.org,2002:str'
|
213
|
+
klass = resolve_class($1)
|
214
|
+
members = {}
|
215
|
+
string = nil
|
216
|
+
|
217
|
+
o.children.each_slice(2) do |k,v|
|
218
|
+
key = accept k
|
219
|
+
value = accept v
|
220
|
+
|
221
|
+
if key == 'str'
|
222
|
+
if klass
|
223
|
+
string = klass.allocate.replace value
|
224
|
+
else
|
225
|
+
string = value
|
226
|
+
end
|
227
|
+
register(o, string)
|
228
|
+
else
|
229
|
+
members[key] = value
|
230
|
+
end
|
231
|
+
end
|
232
|
+
init_with(string, members.map { |k,v| [k.to_s.sub(/^@/, ''),v] }, o)
|
233
|
+
when /^!ruby\/array:(.*)$/
|
234
|
+
klass = resolve_class($1)
|
235
|
+
list = register(o, klass.allocate)
|
236
|
+
|
237
|
+
members = Hash[o.children.map { |c| accept c }.each_slice(2).to_a]
|
238
|
+
list.replace members['internal']
|
239
|
+
|
240
|
+
members['ivars'].each do |ivar, v|
|
241
|
+
list.instance_variable_set ivar, v
|
242
|
+
end
|
243
|
+
list
|
244
|
+
|
245
|
+
when '!ruby/range'
|
246
|
+
klass = class_loader.range
|
247
|
+
h = Hash[*o.children.map { |c| accept c }]
|
248
|
+
register o, klass.new(h['begin'], h['end'], h['excl'])
|
249
|
+
|
250
|
+
when /^!ruby\/exception:?(.*)?$/
|
251
|
+
h = Hash[*o.children.map { |c| accept c }]
|
252
|
+
|
253
|
+
e = build_exception((resolve_class($1) || class_loader.exception),
|
254
|
+
h.delete('message'))
|
255
|
+
init_with(e, h, o)
|
256
|
+
|
257
|
+
when '!set', 'tag:yaml.org,2002:set'
|
258
|
+
set = class_loader.psych_set.new
|
259
|
+
@st[o.anchor] = set if o.anchor
|
260
|
+
o.children.each_slice(2) do |k,v|
|
261
|
+
set[accept(k)] = accept(v)
|
262
|
+
end
|
263
|
+
set
|
264
|
+
|
265
|
+
when /^!ruby\/hash-with-ivars(?::(.*))?$/
|
266
|
+
hash = $1 ? resolve_class($1).allocate : {}
|
267
|
+
register o, hash
|
268
|
+
o.children.each_slice(2) do |key, value|
|
269
|
+
case key.value
|
270
|
+
when 'elements'
|
271
|
+
revive_hash hash, value
|
272
|
+
when 'ivars'
|
273
|
+
value.children.each_slice(2) do |k,v|
|
274
|
+
hash.instance_variable_set accept(k), accept(v)
|
275
|
+
end
|
276
|
+
end
|
277
|
+
end
|
278
|
+
hash
|
279
|
+
|
280
|
+
when /^!map:(.*)$/, /^!ruby\/hash:(.*)$/
|
281
|
+
revive_hash register(o, resolve_class($1).allocate), o
|
282
|
+
|
283
|
+
when '!omap', 'tag:yaml.org,2002:omap'
|
284
|
+
map = register(o, class_loader.psych_omap.new)
|
285
|
+
o.children.each_slice(2) do |l,r|
|
286
|
+
map[accept(l)] = accept r
|
287
|
+
end
|
288
|
+
map
|
289
|
+
|
290
|
+
when /^!ruby\/marshalable:(.*)$/
|
291
|
+
name = $1
|
292
|
+
klass = resolve_class(name)
|
293
|
+
obj = register(o, klass.allocate)
|
294
|
+
|
295
|
+
if obj.respond_to?(:init_with)
|
296
|
+
init_with(obj, revive_hash({}, o), o)
|
297
|
+
elsif obj.respond_to?(:marshal_load)
|
298
|
+
marshal_data = o.children.map(&method(:accept))
|
299
|
+
obj.marshal_load(marshal_data)
|
300
|
+
obj
|
301
|
+
else
|
302
|
+
raise ArgumentError, "Cannot deserialize #{name}"
|
303
|
+
end
|
304
|
+
|
305
|
+
else
|
306
|
+
revive_hash(register(o, {}), o)
|
307
|
+
end
|
308
|
+
end
|
309
|
+
|
310
|
+
def visit_Psych_Nodes_Document o
|
311
|
+
accept o.root
|
312
|
+
end
|
313
|
+
|
314
|
+
def visit_Psych_Nodes_Stream o
|
315
|
+
o.children.map { |c| accept c }
|
316
|
+
end
|
317
|
+
|
318
|
+
def visit_Psych_Nodes_Alias o
|
319
|
+
@st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" }
|
320
|
+
end
|
321
|
+
|
322
|
+
private
|
323
|
+
def register node, object
|
324
|
+
@st[node.anchor] = object if node.anchor
|
325
|
+
object
|
326
|
+
end
|
327
|
+
|
328
|
+
def register_empty object
|
329
|
+
list = register(object, [])
|
330
|
+
object.children.each { |c| list.push accept c }
|
331
|
+
list
|
332
|
+
end
|
333
|
+
|
334
|
+
SHOVEL = '<<'
|
335
|
+
def revive_hash hash, o
|
336
|
+
o.children.each_slice(2) { |k,v|
|
337
|
+
key = accept(k)
|
338
|
+
val = accept(v)
|
339
|
+
|
340
|
+
if key == SHOVEL && k.tag != "tag:yaml.org,2002:str"
|
341
|
+
case v
|
342
|
+
when Nodes::Alias, Nodes::Mapping
|
343
|
+
begin
|
344
|
+
hash.merge! val
|
345
|
+
rescue TypeError
|
346
|
+
hash[key] = val
|
347
|
+
end
|
348
|
+
when Nodes::Sequence
|
349
|
+
begin
|
350
|
+
h = {}
|
351
|
+
val.reverse_each do |value|
|
352
|
+
h.merge! value
|
353
|
+
end
|
354
|
+
hash.merge! h
|
355
|
+
rescue TypeError
|
356
|
+
hash[key] = val
|
357
|
+
end
|
358
|
+
else
|
359
|
+
hash[key] = val
|
360
|
+
end
|
361
|
+
else
|
362
|
+
hash[key] = val
|
363
|
+
end
|
364
|
+
|
365
|
+
}
|
366
|
+
hash
|
367
|
+
end
|
368
|
+
|
369
|
+
def merge_key hash, key, val
|
370
|
+
end
|
371
|
+
|
372
|
+
def revive klass, node
|
373
|
+
s = register(node, klass.allocate)
|
374
|
+
init_with(s, revive_hash({}, node), node)
|
375
|
+
end
|
376
|
+
|
377
|
+
def init_with o, h, node
|
378
|
+
c = Psych::Coder.new(node.tag)
|
379
|
+
c.map = h
|
380
|
+
|
381
|
+
if o.respond_to?(:init_with)
|
382
|
+
o.init_with c
|
383
|
+
else
|
384
|
+
h.each { |k,v| o.instance_variable_set(:"@#{k}", v) }
|
385
|
+
end
|
386
|
+
o
|
387
|
+
end
|
388
|
+
|
389
|
+
# Convert +klassname+ to a Class
|
390
|
+
def resolve_class klassname
|
391
|
+
class_loader.load klassname
|
392
|
+
end
|
393
|
+
end
|
394
|
+
|
395
|
+
class NoAliasRuby < ToRuby
|
396
|
+
def visit_Psych_Nodes_Alias o
|
397
|
+
raise BadAlias, "Unknown alias: #{o.anchor}"
|
398
|
+
end
|
399
|
+
end
|
400
|
+
end
|
401
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Psych
|
3
|
+
module Visitors
|
4
|
+
class Visitor
|
5
|
+
def accept target
|
6
|
+
visit target
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
DISPATCH = Hash.new do |hash, klass|
|
12
|
+
hash[klass] = "visit_#{klass.name.gsub('::', '_')}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def visit target
|
16
|
+
send DISPATCH[target.class], target
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|