summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/psych.rb2
-rw-r--r--lib/psych/nodes/mapping.rb15
-rw-r--r--lib/psych/nodes/scalar.rb35
-rw-r--r--lib/psych/tree_builder.rb32
-rw-r--r--test/psych/test_tree_builder.rb24
5 files changed, 97 insertions, 11 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index 9c6397d..aaec4a7 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -2,6 +2,8 @@ require 'psych/nodes/node'
require 'psych/nodes/stream'
require 'psych/nodes/document'
require 'psych/nodes/sequence'
+require 'psych/nodes/scalar'
+require 'psych/nodes/mapping'
require 'psych/handler'
require 'psych/tree_builder'
diff --git a/lib/psych/nodes/mapping.rb b/lib/psych/nodes/mapping.rb
new file mode 100644
index 0000000..b1137e1
--- /dev/null
+++ b/lib/psych/nodes/mapping.rb
@@ -0,0 +1,15 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a {YAML Mapping}[http://yaml.org/spec/1.1/#mapping].
+ class Mapping < Psych::Nodes::Node
+ def initialize anchor, tag, implicit, style
+ super()
+ @anchor = anchor
+ @tag = tag
+ @implicit = implicit
+ @style = style
+ end
+ end
+ end
+end
diff --git a/lib/psych/nodes/scalar.rb b/lib/psych/nodes/scalar.rb
new file mode 100644
index 0000000..bfa494f
--- /dev/null
+++ b/lib/psych/nodes/scalar.rb
@@ -0,0 +1,35 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a {YAML Scalar}[http://yaml.org/spec/1.1/#id858081].
+ class Scalar < Psych::Nodes::Node
+ # The scalar value
+ attr_accessor :value
+
+ # The anchor value (if there is one)
+ attr_accessor :anchor
+
+ # The tag value (if there is one)
+ attr_accessor :tag
+
+ # Is this a plain scalar?
+ attr_accessor :plain
+
+ # Is this scalar quoted?
+ attr_accessor :quoted
+
+ # The style of this scalar
+ attr_accessor :style
+
+ def initialize value, anchor, tag, plain, quoted, style
+ super()
+ @value = value
+ @anchor = anchor
+ @tag = tag
+ @plain = plain
+ @quoted = quoted
+ @style = style
+ end
+ end
+ end
+end
diff --git a/lib/psych/tree_builder.rb b/lib/psych/tree_builder.rb
index c7e46fb..6a10354 100644
--- a/lib/psych/tree_builder.rb
+++ b/lib/psych/tree_builder.rb
@@ -16,23 +16,33 @@ module Psych
@stack.first
end
- def start_stream encoding
- super
- @stack.push Nodes::Stream.new encoding
+ %w{
+ Document
+ Sequence
+ Mapping
+ }.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)
+ @stack.pop
+ end
+ }
end
- def start_document version = [], tag_directives = [], implicit = true
+ def start_stream encoding
super
- doc = Nodes::Document.new version, tag_directives, implicit
- @stack.last.children << doc
- @stack.push doc
+ @stack.push Nodes::Stream.new encoding
end
- def start_sequence anchor = nil, tag = nil, implicit = true, style = BLOCK_SEQUENCE_STYLE
+ def scalar(*args)
super
- seq = Nodes::Sequence.new anchor, tag, implicit, style
- @stack.last.children << seq
- @stack.push seq
+ @stack.last.children << Nodes::Scalar.new(*args)
end
end
end
diff --git a/test/psych/test_tree_builder.rb b/test/psych/test_tree_builder.rb
index 0e3c59e..0b51a10 100644
--- a/test/psych/test_tree_builder.rb
+++ b/test/psych/test_tree_builder.rb
@@ -12,6 +12,7 @@ module Psych
bar : &A !!str baz,
boo : *A
}
+- baz
eoyml
@tree = @parser.handler.root
end
@@ -41,5 +42,28 @@ module Psych
assert_equal true, seq.implicit
assert_equal BLOCK_SEQUENCE_STYLE, seq.style
end
+
+ def test_scalar
+ doc = @tree.children.first
+ seq = doc.children.first
+
+ assert_equal 3, seq.children.length
+ scalar = seq.children.first
+ assert_instance_of Nodes::Scalar, scalar
+ assert_equal 'foo', scalar.value
+ assert_nil scalar.anchor
+ assert_nil scalar.tag
+ assert_equal true, scalar.plain
+ assert_equal false, scalar.quoted
+ assert_equal PLAIN_SCALAR_STYLE, scalar.style
+ end
+
+ def test_mapping
+ doc = @tree.children.first
+ seq = doc.children.first
+ map = seq.children[1]
+
+ assert_instance_of Nodes::Mapping, map
+ end
end
end