summaryrefslogtreecommitdiffstats
path: root/external_libs/cpp/yaml-cpp/src/emitfromevents.cpp
blob: 49fc10b29e304653c79b834905c4e28291c6171a (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "yaml-cpp/emitfromevents.h"
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/null.h"
#include <cassert>
#include <sstream>

namespace {
	std::string ToString(YAML::anchor_t anchor) {
		std::stringstream stream;
		stream << anchor;
		return stream.str();
	}
}

namespace YAML
{
	EmitFromEvents::EmitFromEvents(Emitter& emitter): m_emitter(emitter)
	{
	}
	
	void EmitFromEvents::OnDocumentStart(const Mark&)
	{
	}
	
	void EmitFromEvents::OnDocumentEnd()
	{
	}
	
	void EmitFromEvents::OnNull(const Mark&, anchor_t anchor)
	{
		BeginNode();
		EmitProps("", anchor);
		m_emitter << Null;
	}
	
	void EmitFromEvents::OnAlias(const Mark&, anchor_t anchor)
	{
		BeginNode();
		m_emitter << Alias(ToString(anchor));
	}
	
	void EmitFromEvents::OnScalar(const Mark&, const std::string& tag, anchor_t anchor, const std::string& value)
	{
		BeginNode();
		EmitProps(tag, anchor);
		m_emitter << value;
	}
	
	void EmitFromEvents::OnSequenceStart(const Mark&, const std::string& tag, anchor_t anchor)
	{
		BeginNode();
		EmitProps(tag, anchor);
		m_emitter << BeginSeq;
		m_stateStack.push(State::WaitingForSequenceEntry);
	}
	
	void EmitFromEvents::OnSequenceEnd()
	{
		m_emitter << EndSeq;
		assert(m_stateStack.top() == State::WaitingForSequenceEntry);
		m_stateStack.pop();
	}
	
	void EmitFromEvents::OnMapStart(const Mark&, const std::string& tag, anchor_t anchor)
	{
		BeginNode();
		EmitProps(tag, anchor);
		m_emitter << BeginMap;
		m_stateStack.push(State::WaitingForKey);
	}

	void EmitFromEvents::OnMapEnd()
	{
		m_emitter << EndMap;
		assert(m_stateStack.top() == State::WaitingForKey);
		m_stateStack.pop();
	}

	void EmitFromEvents::BeginNode()
	{
		if(m_stateStack.empty())
			return;
		
		switch(m_stateStack.top()) {
			case State::WaitingForKey:
				m_emitter << Key;
				m_stateStack.top() = State::WaitingForValue;
				break;
			case State::WaitingForValue:
				m_emitter << Value;
				m_stateStack.top() = State::WaitingForKey;
				break;
			default:
				break;
		}
	}
	
	void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor)
	{
		if(!tag.empty() && tag != "?")
			m_emitter << VerbatimTag(tag);
		if(anchor)
			m_emitter << Anchor(ToString(anchor));
	}
}