document.cpp 1.13 KB
Newer Older
Jesse Beder's avatar
Jesse Beder committed
1
2
#include "document.h"
#include "node.h"
3
4
#include "token.h"
#include "scanner.h"
5

Jesse Beder's avatar
Jesse Beder committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace YAML
{
	Document::Document(): m_pRoot(0)
	{
	}

	Document::~Document()
	{
		Clear();
	}

	void Document::Clear()
	{
		delete m_pRoot;
		m_pRoot = 0;
	}
22

23
	void Document::Parse(Scanner *pScanner, const ParserState& state)
24
25
26
27
28
29
30
31
32
33
34
35
36
	{
		Clear();

		// we better have some tokens in the queue
		if(!pScanner->PeekNextToken())
			return;

		// first eat doc start (optional)
		if(pScanner->PeekNextToken()->type == TT_DOC_START)
			pScanner->EatNextToken();

		// now create our root node and parse it
		m_pRoot = new Node;
37
		m_pRoot->Parse(pScanner, state);
38
39
40
41
42
43

		// and finally eat any doc ends we see
		while(pScanner->PeekNextToken() && pScanner->PeekNextToken()->type == TT_DOC_END)
			pScanner->EatNextToken();
	}

44
45
46
47
48
49
50
51
	const Node& Document::GetRoot() const
	{
		if(!m_pRoot)
			throw;

		return *m_pRoot;
	}

52
53
	std::ostream& operator << (std::ostream& out, const Document& doc)
	{
54
		out << "---\n";
55
56
57
58
59
60
61
62
		if(!doc.m_pRoot) {
			out << "{empty node}\n";
			return out;
		}

		doc.m_pRoot->Write(out, 0);
		return out;
	}
Jesse Beder's avatar
Jesse Beder committed
63
}