From 1d0463ca1f63f84032b5b983962e59f88ff123a0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 12:09:59 -0700 Subject: streams convert to ruby --- lib/psych/visitors/to_ruby.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'lib') diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index a4e6152..27b7778 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -18,6 +18,10 @@ module Psych visitor_for(Nodes::Document) do |o| o.root.accept self end + + visitor_for(Nodes::Stream) do |o| + o.children.map { |c| c.accept self } + end end end end -- cgit v1.2.3 From 5afbd2432372d0d137cd8ef579a1e73e6deb7d87 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 12:13:43 -0700 Subject: initial load is working --- lib/psych.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/psych.rb b/lib/psych.rb index bdc2458..bbb8662 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -19,7 +19,11 @@ module Psych VERSION = '1.0.0' LIBYAML_VERSION = Psych.libyaml_version.join '.' - def self.parse thing - Psych::Parser.new.parse thing + ### + # Load +yaml+ in to a Ruby data structure + def self.load yaml + parser = Psych::Parser.new(TreeBuilder.new) + parser.parse yaml + parser.handler.root.children.first.to_ruby end end -- cgit v1.2.3 From cbf2a14ec5fc5b6fe0b576c2df13cf5e29cefbb3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 15:44:37 -0700 Subject: aliases starting to work --- lib/psych/visitors/to_ruby.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'lib') diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 27b7778..8848c16 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -3,7 +3,13 @@ module Psych ### # This class walks a YAML AST, converting each node to ruby class ToRuby < Psych::Visitors::Visitor + def initialize + super + @st = {} + end + visitor_for(Nodes::Scalar) do |o| + @st[o.anchor] = o.value if o.anchor o.value end @@ -22,6 +28,10 @@ module Psych visitor_for(Nodes::Stream) do |o| o.children.map { |c| c.accept self } end + + visitor_for(Nodes::Alias) do |o| + @st[o.anchor] + end end end end -- cgit v1.2.3 From b8e82a04568ec154154af68614f077d7a754bf8a Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 20:17:03 -0700 Subject: adding the emitter handler and visitor --- lib/psych/emitter.rb | 4 ++++ lib/psych/visitors.rb | 1 + lib/psych/visitors/emitter.rb | 15 +++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 lib/psych/emitter.rb create mode 100644 lib/psych/visitors/emitter.rb (limited to 'lib') diff --git a/lib/psych/emitter.rb b/lib/psych/emitter.rb new file mode 100644 index 0000000..b06ad3a --- /dev/null +++ b/lib/psych/emitter.rb @@ -0,0 +1,4 @@ +module Psych + class Emitter < Psych::Handler + end +end diff --git a/lib/psych/visitors.rb b/lib/psych/visitors.rb index 45b4b58..df58e2e 100644 --- a/lib/psych/visitors.rb +++ b/lib/psych/visitors.rb @@ -1,2 +1,3 @@ require 'psych/visitors/visitor' require 'psych/visitors/to_ruby' +require 'psych/visitors/emitter' diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb new file mode 100644 index 0000000..ff3832b --- /dev/null +++ b/lib/psych/visitors/emitter.rb @@ -0,0 +1,15 @@ +module Psych + module Visitors + class Emitter < Psych::Visitors::Visitor + def initialize io + @handler = Psych::Emitter.new io + end + + visitor_for(Nodes::Stream) do |o| + @handler.start_stream o.encoding + o.children.each { |c| c.accept self } + @handler.end_stream + end + end + end +end -- cgit v1.2.3 From 780b789a2fb985ac8ae24eab21eb939293524542 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 20:53:06 -0700 Subject: adding emitter codes --- lib/psych/visitors/emitter.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index ff3832b..d71ce2a 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -10,6 +10,12 @@ module Psych o.children.each { |c| c.accept self } @handler.end_stream end + + visitor_for(Nodes::Document) do |o| + @handler.start_document o.version, o.tag_directives, o.implicit + o.children.each { |c| c.accept self } + @handler.end_document o.implicit + end end end end -- cgit v1.2.3 From 0f174ee584cd1755819becf1da74e0c010049542 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 22:02:10 -0700 Subject: scalars converted to yaml --- lib/psych/nodes/document.rb | 2 +- lib/psych/tree_builder.rb | 5 ----- lib/psych/visitors/emitter.rb | 4 ++++ 3 files changed, 5 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/psych/nodes/document.rb b/lib/psych/nodes/document.rb index 0bd58b2..be786c6 100644 --- a/lib/psych/nodes/document.rb +++ b/lib/psych/nodes/document.rb @@ -10,7 +10,7 @@ module Psych # Was this document implicitly created? attr_accessor :implicit - def initialize version = [], tag_directives = [], implicit = true + def initialize version = [], tag_directives = [], implicit = false super() @version = version @tag_directives = tag_directives diff --git a/lib/psych/tree_builder.rb b/lib/psych/tree_builder.rb index c158368..efbeaf9 100644 --- a/lib/psych/tree_builder.rb +++ b/lib/psych/tree_builder.rb @@ -23,31 +23,26 @@ module Psych }.each do |node| class_eval %{ def start_#{node.downcase}(*args) - super n = Nodes::#{node}.new(*args) @stack.last.children << n @stack.push n end def end_#{node.downcase}(*args) - super @stack.pop end } end def start_stream encoding - super @stack.push Nodes::Stream.new(encoding) end def scalar(*args) - super @stack.last.children << Nodes::Scalar.new(*args) end def alias(*args) - super @stack.last.children << Nodes::Alias.new(*args) end end diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index d71ce2a..e2dd0be 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -16,6 +16,10 @@ module Psych o.children.each { |c| c.accept self } @handler.end_document o.implicit end + + visitor_for(Nodes::Scalar) do |o| + @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style + end end end end -- cgit v1.2.3 From a2c0c115fc7c90c5a59ef941d2bbd6a6627f1b21 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 28 Sep 2009 22:08:47 -0700 Subject: changing initialize to sensible defaults --- lib/psych/nodes/scalar.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/psych/nodes/scalar.rb b/lib/psych/nodes/scalar.rb index b84e0ed..fac4d69 100644 --- a/lib/psych/nodes/scalar.rb +++ b/lib/psych/nodes/scalar.rb @@ -29,7 +29,7 @@ module Psych # The style of this scalar attr_accessor :style - def initialize value, anchor = nil, tag = nil, plain = true, quoted = true, style = ANY + def initialize value, anchor = nil, tag = nil, plain = true, quoted = false, style = ANY super() @value = value @anchor = anchor -- cgit v1.2.3 From d2bf942ac38b1b6e91116a06855a00d32b95d27b Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 29 Sep 2009 09:53:42 -0700 Subject: sequences emit --- lib/psych/nodes/node.rb | 8 ++++++++ lib/psych/visitors/emitter.rb | 6 ++++++ 2 files changed, 14 insertions(+) (limited to 'lib') diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb index b5ae131..a63fc60 100644 --- a/lib/psych/nodes/node.rb +++ b/lib/psych/nodes/node.rb @@ -1,3 +1,5 @@ +require 'stringio' + module Psych module Nodes ### @@ -15,6 +17,12 @@ module Psych def to_ruby Visitors::ToRuby.new.accept self end + + def to_yaml + io = StringIO.new + Visitors::Emitter.new(io).accept self + io.string + end end end end diff --git a/lib/psych/visitors/emitter.rb b/lib/psych/visitors/emitter.rb index e2dd0be..a0ff9d9 100644 --- a/lib/psych/visitors/emitter.rb +++ b/lib/psych/visitors/emitter.rb @@ -20,6 +20,12 @@ module Psych visitor_for(Nodes::Scalar) do |o| @handler.scalar o.value, o.anchor, o.tag, o.plain, o.quoted, o.style end + + visitor_for(Nodes::Sequence) do |o| + @handler.start_sequence o.anchor, o.tag, o.implicit, o.style + o.children.each { |c| c.accept self } + @handler.end_sequence + end end end end -- cgit v1.2.3