Go to the documentation of this file.00001 #include "yaml-cpp-pm/emitfromevents.h"
00002 #include "yaml-cpp-pm/emitter.h"
00003 #include "yaml-cpp-pm/null.h"
00004 #include <cassert>
00005 #include <sstream>
00006
00007 namespace {
00008 std::string ToString(YAML_PM::anchor_t anchor) {
00009 std::stringstream stream;
00010 stream << anchor;
00011 return stream.str();
00012 }
00013 }
00014
00015 namespace YAML_PM
00016 {
00017 EmitFromEvents::EmitFromEvents(Emitter& emitter): m_emitter(emitter)
00018 {
00019 }
00020
00021 void EmitFromEvents::OnDocumentStart(const Mark&)
00022 {
00023 }
00024
00025 void EmitFromEvents::OnDocumentEnd()
00026 {
00027 }
00028
00029 void EmitFromEvents::OnNull(const Mark&, anchor_t anchor)
00030 {
00031 BeginNode();
00032 EmitProps("", anchor);
00033 m_emitter << Null;
00034 }
00035
00036 void EmitFromEvents::OnAlias(const Mark&, anchor_t anchor)
00037 {
00038 BeginNode();
00039 m_emitter << Alias(ToString(anchor));
00040 }
00041
00042 void EmitFromEvents::OnScalar(const Mark&, const std::string& tag, anchor_t anchor, const std::string& value)
00043 {
00044 BeginNode();
00045 EmitProps(tag, anchor);
00046 m_emitter << value;
00047 }
00048
00049 void EmitFromEvents::OnSequenceStart(const Mark&, const std::string& tag, anchor_t anchor)
00050 {
00051 BeginNode();
00052 EmitProps(tag, anchor);
00053 m_emitter << BeginSeq;
00054 m_stateStack.push(State::WaitingForSequenceEntry);
00055 }
00056
00057 void EmitFromEvents::OnSequenceEnd()
00058 {
00059 m_emitter << EndSeq;
00060 assert(m_stateStack.top() == State::WaitingForSequenceEntry);
00061 m_stateStack.pop();
00062 }
00063
00064 void EmitFromEvents::OnMapStart(const Mark&, const std::string& tag, anchor_t anchor)
00065 {
00066 BeginNode();
00067 EmitProps(tag, anchor);
00068 m_emitter << BeginMap;
00069 m_stateStack.push(State::WaitingForKey);
00070 }
00071
00072 void EmitFromEvents::OnMapEnd()
00073 {
00074 m_emitter << EndMap;
00075 assert(m_stateStack.top() == State::WaitingForKey);
00076 m_stateStack.pop();
00077 }
00078
00079 void EmitFromEvents::BeginNode()
00080 {
00081 if(m_stateStack.empty())
00082 return;
00083
00084 switch(m_stateStack.top()) {
00085 case State::WaitingForKey:
00086 m_emitter << Key;
00087 m_stateStack.top() = State::WaitingForValue;
00088 break;
00089 case State::WaitingForValue:
00090 m_emitter << Value;
00091 m_stateStack.top() = State::WaitingForKey;
00092 break;
00093 default:
00094 break;
00095 }
00096 }
00097
00098 void EmitFromEvents::EmitProps(const std::string& tag, anchor_t anchor)
00099 {
00100 if(!tag.empty() && tag != "?")
00101 m_emitter << VerbatimTag(tag);
00102 if(anchor)
00103 m_emitter << Anchor(ToString(anchor));
00104 }
00105 }