diff options
-rw-r--r-- | lib/psych.rb | 5 | ||||
-rw-r--r-- | lib/psych/nodes/document.rb | 12 | ||||
-rw-r--r-- | lib/psych/nodes/node.rb | 14 | ||||
-rw-r--r-- | lib/psych/nodes/stream.rb | 14 | ||||
-rw-r--r-- | lib/psych/tree_builder.rb | 29 | ||||
-rw-r--r-- | test/psych/test_tree_builder.rb | 28 |
6 files changed, 102 insertions, 0 deletions
diff --git a/lib/psych.rb b/lib/psych.rb index 9d136d5..d39a284 100644 --- a/lib/psych.rb +++ b/lib/psych.rb @@ -1,4 +1,9 @@ +require 'psych/nodes/node' +require 'psych/nodes/stream' +require 'psych/nodes/document' + require 'psych/handler' +require 'psych/tree_builder' require 'psych/parser' require 'psych/psych' diff --git a/lib/psych/nodes/document.rb b/lib/psych/nodes/document.rb new file mode 100644 index 0000000..8dc0a13 --- /dev/null +++ b/lib/psych/nodes/document.rb @@ -0,0 +1,12 @@ +module Psych + module Nodes + class Document < Psych::Nodes::Node + def initialize version, tag_directives, implicit + super() + @version = version + @tag_directives = tag_directives + @implicit = implicit + end + end + end +end diff --git a/lib/psych/nodes/node.rb b/lib/psych/nodes/node.rb new file mode 100644 index 0000000..cb8f632 --- /dev/null +++ b/lib/psych/nodes/node.rb @@ -0,0 +1,14 @@ +module Psych + module Nodes + ### + # The base class for any Node in a YAML parse tree. This class should + # never be instantiated. + class Node + attr_reader :children + + def initialize + @children = [] + end + end + end +end diff --git a/lib/psych/nodes/stream.rb b/lib/psych/nodes/stream.rb new file mode 100644 index 0000000..911bea8 --- /dev/null +++ b/lib/psych/nodes/stream.rb @@ -0,0 +1,14 @@ +module Psych + module Nodes + ### + # Represents a YAML stream. This is the root node for any YAML parse + # tree. + class Stream < Psych::Nodes::Node + attr_reader :encoding + def initialize encoding + super() + @encoding = encoding + end + end + end +end diff --git a/lib/psych/tree_builder.rb b/lib/psych/tree_builder.rb new file mode 100644 index 0000000..3f11fd9 --- /dev/null +++ b/lib/psych/tree_builder.rb @@ -0,0 +1,29 @@ +require 'psych/handler' + +module Psych + ### + # This class builds an in-memory parse tree tree that represents a YAML + # document. + # + # See Psych::Handler for documentation on the event methods used in this + # class. + class TreeBuilder < Psych::Handler + def initialize + @stack = [] + end + + def root + @stack.first + end + + def start_stream encoding + @stack.push Nodes::Stream.new encoding + end + + def start_document version = [], tag_directives = [], implicit = true + doc = Nodes::Document.new version, tag_directives, implicit + @stack.last.children << doc + @stack.push doc + end + end +end diff --git a/test/psych/test_tree_builder.rb b/test/psych/test_tree_builder.rb new file mode 100644 index 0000000..62998db --- /dev/null +++ b/test/psych/test_tree_builder.rb @@ -0,0 +1,28 @@ +require 'helper' + +module Psych + class TestTreeBuilder < Test::Unit::TestCase + def setup + @parser = Psych::Parser.new TreeBuilder.new + @parser.parse(<<-eoyml) +%YAML 1.1 +--- +- foo +- { + bar : &A !!str baz, + boo : *A +} + eoyml + @tree = @parser.handler.root + end + + def test_stream + assert_instance_of Nodes::Stream, @tree + end + + def test_documents + assert_equal 1, @tree.children.length + assert_instance_of Nodes::Document, @tree.children.first + end + end +end |