summaryrefslogtreecommitdiff
path: root/ext/psych/lib/psych/nodes
diff options
context:
space:
mode:
Diffstat (limited to 'ext/psych/lib/psych/nodes')
-rw-r--r--ext/psych/lib/psych/nodes/alias.rb18
-rw-r--r--ext/psych/lib/psych/nodes/document.rb60
-rw-r--r--ext/psych/lib/psych/nodes/mapping.rb56
-rw-r--r--ext/psych/lib/psych/nodes/node.rb40
-rw-r--r--ext/psych/lib/psych/nodes/scalar.rb67
-rw-r--r--ext/psych/lib/psych/nodes/sequence.rb81
-rw-r--r--ext/psych/lib/psych/nodes/stream.rb37
7 files changed, 359 insertions, 0 deletions
diff --git a/ext/psych/lib/psych/nodes/alias.rb b/ext/psych/lib/psych/nodes/alias.rb
new file mode 100644
index 0000000000..5bd4df13d1
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/alias.rb
@@ -0,0 +1,18 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a {YAML Alias}[http://yaml.org/spec/1.1/#alias].
+ # It points to an +anchor+.
+ #
+ # A Psych::Nodes::Alias is a terminal node and may have no children.
+ class Alias < Psych::Nodes::Node
+ # The anchor this alias links to
+ attr_accessor :anchor
+
+ # Create a new Alias that points to an +anchor+
+ def initialize anchor
+ @anchor = anchor
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/document.rb b/ext/psych/lib/psych/nodes/document.rb
new file mode 100644
index 0000000000..32014d60dc
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/document.rb
@@ -0,0 +1,60 @@
+module Psych
+ module Nodes
+ ###
+ # This represents a YAML Document. This node must be a child of
+ # Psych::Nodes::Stream. A Psych::Nodes::Document must have one child,
+ # and that child may be one of the following:
+ #
+ # * Psych::Nodes::Sequence
+ # * Psych::Nodes::Mapping
+ # * Psych::Nodes::Scalar
+ class Document < Psych::Nodes::Node
+ # The version of the YAML document
+ attr_accessor :version
+
+ # A list of tag directives for this document
+ attr_accessor :tag_directives
+
+ # Was this document implicitly created?
+ attr_accessor :implicit
+
+ # Is the end of the document implicit?
+ attr_accessor :implicit_end
+
+ ###
+ # Create a new Psych::Nodes::Document object.
+ #
+ # +version+ is a list indicating the YAML version.
+ # +tags_directives+ is a list of tag directive declarations
+ # +implicit+ is a flag indicating whether the document will be implicitly
+ # started.
+ #
+ # == Example:
+ # This creates a YAML document object that represents a YAML 1.1 document
+ # with one tag directive, and has an implicit start:
+ #
+ # Psych::Nodes::Document.new(
+ # [1,1],
+ # [["!", "tag:tenderlovemaking.com,2009:"]],
+ # true
+ # )
+ #
+ # == See Also
+ # See also Psych::Handler#start_document
+ def initialize version = [], tag_directives = [], implicit = false
+ super()
+ @version = version
+ @tag_directives = tag_directives
+ @implicit = implicit
+ @implicit_end = true
+ end
+
+ ###
+ # Returns the root node. A Document may only have one root node:
+ # http://yaml.org/spec/1.1/#id898031
+ def root
+ children.first
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/mapping.rb b/ext/psych/lib/psych/nodes/mapping.rb
new file mode 100644
index 0000000000..5ba95ce4b6
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/mapping.rb
@@ -0,0 +1,56 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a {YAML Mapping}[http://yaml.org/spec/1.1/#mapping].
+ #
+ # A Psych::Nodes::Mapping node may have 0 or more children, but must have
+ # an even number of children. Here are the valid children a
+ # Psych::Nodes::Mapping node may have:
+ #
+ # * Psych::Nodes::Sequence
+ # * Psych::Nodes::Mapping
+ # * Psych::Nodes::Scalar
+ # * Psych::Nodes::Alias
+ class Mapping < Psych::Nodes::Node
+ # Any Map Style
+ ANY = 0
+
+ # Block Map Style
+ BLOCK = 1
+
+ # Flow Map Style
+ FLOW = 2
+
+ # The optional anchor for this mapping
+ attr_accessor :anchor
+
+ # The optional tag for this mapping
+ attr_accessor :tag
+
+ # Is this an implicit mapping?
+ attr_accessor :implicit
+
+ # The style of this mapping
+ attr_accessor :style
+
+ ###
+ # Create a new Psych::Nodes::Mapping object.
+ #
+ # +anchor+ is the anchor associated with the map or +nil+.
+ # +tag+ is the tag associated with the map or +nil+.
+ # +implicit+ is a boolean indicating whether or not the map was implicitly
+ # started.
+ # +style+ is an integer indicating the mapping style.
+ #
+ # == See Also
+ # See also Psych::Handler#start_mapping
+ def initialize anchor = nil, tag = nil, implicit = true, style = BLOCK
+ super()
+ @anchor = anchor
+ @tag = tag
+ @implicit = implicit
+ @style = style
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/node.rb b/ext/psych/lib/psych/nodes/node.rb
new file mode 100644
index 0000000000..0de768c7ac
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/node.rb
@@ -0,0 +1,40 @@
+require 'stringio'
+
+module Psych
+ module Nodes
+ ###
+ # The base class for any Node in a YAML parse tree. This class should
+ # never be instantiated.
+ class Node
+ # The children of this node
+ attr_reader :children
+
+ # An associated tag
+ attr_reader :tag
+
+ # Create a new Psych::Nodes::Node
+ def initialize
+ @children = []
+ end
+
+ ###
+ # Convert this node to Ruby.
+ #
+ # See also Psych::Visitors::ToRuby
+ def to_ruby
+ Visitors::ToRuby.new.accept self
+ end
+ alias :transform :to_ruby
+
+ ###
+ # Convert this node to YAML.
+ #
+ # See also Psych::Visitors::Emitter
+ def to_yaml
+ io = StringIO.new
+ Visitors::Emitter.new(io).accept self
+ io.string
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/scalar.rb b/ext/psych/lib/psych/nodes/scalar.rb
new file mode 100644
index 0000000000..1b1b25b98a
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/scalar.rb
@@ -0,0 +1,67 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a {YAML Scalar}[http://yaml.org/spec/1.1/#id858081].
+ #
+ # This node type is a terminal node and should not have any children.
+ class Scalar < Psych::Nodes::Node
+ # Any style scalar, the emitter chooses
+ ANY = 0
+
+ # Plain scalar style
+ PLAIN = 1
+
+ # Single quoted style
+ SINGLE_QUOTED = 2
+
+ # Double quoted style
+ DOUBLE_QUOTED = 3
+
+ # Literal style
+ LITERAL = 4
+
+ # Folded style
+ FOLDED = 5
+
+ # 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
+
+ ###
+ # Create a new Psych::Nodes::Scalar object.
+ #
+ # +value+ is the string value of the scalar
+ # +anchor+ is an associated anchor or nil
+ # +tag+ is an associated tag or nil
+ # +plain+ is a boolean value
+ # +quoted+ is a boolean value
+ # +style+ is an integer idicating the string style
+ #
+ # == See Also
+ #
+ # See also Psych::Handler#scalar
+ def initialize value, anchor = nil, tag = nil, plain = true, quoted = false, style = ANY
+ @value = value
+ @anchor = anchor
+ @tag = tag
+ @plain = plain
+ @quoted = quoted
+ @style = style
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/sequence.rb b/ext/psych/lib/psych/nodes/sequence.rb
new file mode 100644
index 0000000000..e4b833d330
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/sequence.rb
@@ -0,0 +1,81 @@
+module Psych
+ module Nodes
+ ###
+ # This class represents a
+ # {YAML sequence}[http://yaml.org/spec/1.1/#sequence/syntax].
+ #
+ # A YAML sequence is basically a list, and looks like this:
+ #
+ # %YAML 1.1
+ # ---
+ # - I am
+ # - a Sequence
+ #
+ # A YAML sequence may have an anchor like this:
+ #
+ # %YAML 1.1
+ # ---
+ # &A [
+ # "This sequence",
+ # "has an anchor"
+ # ]
+ #
+ # A YAML sequence may also have a tag like this:
+ #
+ # %YAML 1.1
+ # ---
+ # !!seq [
+ # "This sequence",
+ # "has a tag"
+ # ]
+ #
+ # This class represents a sequence in a YAML document. A
+ # Psych::Nodes::Sequence node may have 0 or more children. Valid children
+ # for this node are:
+ #
+ # * Psych::Nodes::Sequence
+ # * Psych::Nodes::Mapping
+ # * Psych::Nodes::Scalar
+ # * Psych::Nodes::Alias
+ class Sequence < Psych::Nodes::Node
+ # Any Styles, emitter chooses
+ ANY = 0
+
+ # Block style sequence
+ BLOCK = 1
+
+ # Flow style sequence
+ FLOW = 2
+
+ # The anchor for this sequence (if any)
+ attr_accessor :anchor
+
+ # The tag name for this sequence (if any)
+ attr_accessor :tag
+
+ # Is this sequence started implicitly?
+ attr_accessor :implicit
+
+ # The sequece style used
+ attr_accessor :style
+
+ ###
+ # Create a new object representing a YAML sequence.
+ #
+ # +anchor+ is the anchor associated with the sequence or nil.
+ # +tag+ is the tag associated with the sequence or nil.
+ # +implicit+ a boolean indicating whether or not the sequence was
+ # implicitly started.
+ # +style+ is an integer indicating the list style.
+ #
+ # See Psych::Handler#start_sequence
+ def initialize anchor = nil, tag = nil, implicit = true, style = BLOCK
+ super()
+ @anchor = anchor
+ @tag = tag
+ @implicit = implicit
+ @style = style
+ end
+ end
+ end
+end
diff --git a/ext/psych/lib/psych/nodes/stream.rb b/ext/psych/lib/psych/nodes/stream.rb
new file mode 100644
index 0000000000..f4aab5a7dc
--- /dev/null
+++ b/ext/psych/lib/psych/nodes/stream.rb
@@ -0,0 +1,37 @@
+module Psych
+ module Nodes
+ ###
+ # Represents a YAML stream. This is the root node for any YAML parse
+ # tree. This node must have one or more child nodes. The only valid
+ # child node for a Psych::Nodes::Stream node is Psych::Nodes::Document.
+ class Stream < Psych::Nodes::Node
+
+ # Encodings supported by Psych (and libyaml)
+
+ # Any encoding
+ ANY = Psych::Parser::ANY
+
+ # UTF-8 encoding
+ UTF8 = Psych::Parser::UTF8
+
+ # UTF-16LE encoding
+ UTF16LE = Psych::Parser::UTF16LE
+
+ # UTF-16BE encoding
+ UTF16BE = Psych::Parser::UTF16BE
+
+ # The encoding used for this stream
+ attr_reader :encoding
+
+ ###
+ # Create a new Psych::Nodes::Stream node with an +encoding+ that
+ # defaults to Psych::Nodes::Stream::UTF8.
+ #
+ # See also Psych::Handler#start_stream
+ def initialize encoding = UTF8
+ super()
+ @encoding = encoding
+ end
+ end
+ end
+end