summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/psych/nodes/node.rb9
-rw-r--r--lib/psych/visitors/depth_first.rb18
-rw-r--r--lib/psych/visitors/to_ruby.rb20
3 files changed, 38 insertions, 9 deletions
diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb
index 35de224..7c040ec 100644
--- a/lib/psych/nodes/node.rb
+++ b/lib/psych/nodes/node.rb
@@ -6,6 +6,8 @@ module Psych
# The base class for any Node in a YAML parse tree. This class should
# never be instantiated.
class Node
+ include Enumerable
+
# The children of this node
attr_reader :children
@@ -17,7 +19,12 @@ module Psych
@children = []
end
- def each
+ ###
+ # Iterate over each node in the tree. Yields each node to +block+ depth
+ # first.
+ def each &block
+ return enum_for :each unless block_given?
+ Visitors::DepthFirst.new(block).accept self
end
###
diff --git a/lib/psych/visitors/depth_first.rb b/lib/psych/visitors/depth_first.rb
index f202d4d..c6eb814 100644
--- a/lib/psych/visitors/depth_first.rb
+++ b/lib/psych/visitors/depth_first.rb
@@ -1,8 +1,26 @@
module Psych
module Visitors
class DepthFirst < Psych::Visitors::Visitor
+ def initialize block
+ @block = block
+ end
+
private
+ def nary o
+ o.children.each { |x| visit x }
+ @block.call o
+ end
+ alias :visit_Psych_Nodes_Stream :nary
+ alias :visit_Psych_Nodes_Document :nary
+ alias :visit_Psych_Nodes_Sequence :nary
+ alias :visit_Psych_Nodes_Mapping :nary
+
+ def terminal o
+ @block.call o
+ end
+ alias :visit_Psych_Nodes_Scalar :terminal
+ alias :visit_Psych_Nodes_Alias :terminal
end
end
end
diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb
index 745338a..f8b1585 100644
--- a/lib/psych/visitors/to_ruby.rb
+++ b/lib/psych/visitors/to_ruby.rb
@@ -60,7 +60,7 @@ module Psych
when "tag:yaml.org,2002:float", "!float"
Float(@ss.tokenize(o.value))
when "!ruby/regexp"
- o.value =~ /^\/(.*)\/([mix]*)$/
+ o.value =~ /^\/(.*)\/([mixn]*)$/
source = $1
options = 0
lang = nil
@@ -69,6 +69,7 @@ module Psych
when 'x' then options |= Regexp::EXTENDED
when 'i' then options |= Regexp::IGNORECASE
when 'm' then options |= Regexp::MULTILINE
+ when 'n' then options |= Regexp::NOENCODING
else lang = option
end
end
@@ -187,14 +188,17 @@ module Psych
o.children.each_slice(2) { |k,v|
key = accept(k)
- if key == '<<' && Nodes::Alias === v
- # FIXME: remove this when "<<" syntax is deprecated
- if $VERBOSE
- where = caller.find { |x| x !~ /psych/ }
- warn where
- warn "\"<<: *#{v.anchor}\" is no longer supported, please switch to \"*#{v.anchor}\""
+ if key == '<<'
+ case v
+ when Nodes::Alias
+ hash.merge! accept(v)
+ when Nodes::Sequence
+ accept(v).reverse_each do |value|
+ hash.merge! value
+ end
+ else
+ hash[key] = accept(v)
end
- return accept(v)
else
hash[key] = accept(v)
end