summaryrefslogtreecommitdiff
path: root/lib/psych/tree_builder.rb
blob: 3f11fd925c190e8b298a66bb66d4b2cddf74d350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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