Implemented begin/end doc

This commit is contained in:
Jesse Beder
2012-05-21 16:54:45 -05:00
parent 5a2183f55b
commit 923ccc8fed
5 changed files with 93 additions and 35 deletions

View File

@@ -81,8 +81,6 @@ namespace YAML
void EmitEndSeq(); void EmitEndSeq();
void EmitBeginMap(); void EmitBeginMap();
void EmitEndMap(); void EmitEndMap();
void EmitKey();
void EmitValue();
void EmitNewline(); void EmitNewline();
void EmitKindTag(); void EmitKindTag();
void EmitTag(bool verbatim, const _Tag& tag); void EmitTag(bool verbatim, const _Tag& tag);

View File

@@ -131,10 +131,8 @@ namespace YAML
EmitEndMap(); EmitEndMap();
break; break;
case Key: case Key:
EmitKey();
break;
case Value: case Value:
EmitValue(); // deprecated (these can be deduced by the parity of nodes in a map)
break; break;
case TagByKind: case TagByKind:
EmitKindTag(); EmitKindTag();
@@ -169,6 +167,20 @@ namespace YAML
{ {
if(!good()) if(!good())
return; return;
if(m_pState->CurGroupType() != GroupType::None) {
m_pState->SetError("Unexpected begin document");
return;
}
if(m_pState->HasAnchor() || m_pState->HasTag()) {
m_pState->SetError("Unexpected begin document");
return;
}
if(m_stream.col() > 0)
m_stream << "\n";
m_stream << "---\n";
} }
// EmitEndDoc // EmitEndDoc
@@ -176,6 +188,20 @@ namespace YAML
{ {
if(!good()) if(!good())
return; return;
if(m_pState->CurGroupType() != GroupType::None) {
m_pState->SetError("Unexpected begin document");
return;
}
if(m_pState->HasAnchor() || m_pState->HasTag()) {
m_pState->SetError("Unexpected begin document");
return;
}
if(m_stream.col() > 0)
m_stream << "\n";
m_stream << "...\n";
} }
// EmitBeginSeq // EmitBeginSeq
@@ -183,6 +209,8 @@ namespace YAML
{ {
if(!good()) if(!good())
return; return;
m_pState->BeginGroup(GroupType::Seq);
} }
// EmitEndSeq // EmitEndSeq
@@ -190,6 +218,8 @@ namespace YAML
{ {
if(!good()) if(!good())
return; return;
m_pState->EndGroup(GroupType::Seq);
} }
// EmitBeginMap // EmitBeginMap
@@ -197,26 +227,17 @@ namespace YAML
{ {
if(!good()) if(!good())
return; return;
m_pState->BeginGroup(GroupType::Map);
} }
// EmitEndMap // EmitEndMap
void Emitter::EmitEndMap() void Emitter::EmitEndMap()
{
if(!good())
return; }
// EmitKey
void Emitter::EmitKey()
{ {
if(!good()) if(!good())
return; return;
}
// EmitValue m_pState->EndGroup(GroupType::Map);
void Emitter::EmitValue()
{
if(!good())
return;
} }
// EmitNewline // EmitNewline
@@ -238,6 +259,9 @@ namespace YAML
{ {
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -291,6 +315,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -299,6 +325,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -307,6 +335,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -315,6 +345,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -323,6 +355,7 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -337,6 +370,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -345,6 +380,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
@@ -355,6 +392,8 @@ namespace YAML
if(!good()) if(!good())
return *this; return *this;
m_pState->BeginScalar();
return *this; return *this;
} }
} }

View File

@@ -4,7 +4,7 @@
namespace YAML namespace YAML
{ {
EmitterState::EmitterState(): m_isGood(true), m_curIndent(0) EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_hasAnchor(false), m_hasTag(false)
{ {
// set default global manipulators // set default global manipulators
m_charset.set(EmitNonAscii); m_charset.set(EmitNonAscii);
@@ -43,8 +43,24 @@ namespace YAML
SetMapKeyFormat(value, FmtScope::Local); SetMapKeyFormat(value, FmtScope::Local);
} }
void EmitterState::BeginNode()
{
if(!m_groups.empty())
m_groups.top().childCount++;
m_hasAnchor = false;
m_hasTag = false;
}
void EmitterState::BeginScalar()
{
BeginNode();
}
void EmitterState::BeginGroup(GroupType::value type) void EmitterState::BeginGroup(GroupType::value type)
{ {
BeginNode();
unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent); unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
m_curIndent += lastIndent; m_curIndent += lastIndent;
@@ -82,7 +98,7 @@ namespace YAML
m_globalModifiedSettings.restore(); m_globalModifiedSettings.restore();
} }
GroupType::value EmitterState::GetCurGroupType() const GroupType::value EmitterState::CurGroupType() const
{ {
if(m_groups.empty()) if(m_groups.empty())
return GroupType::None; return GroupType::None;
@@ -90,7 +106,7 @@ namespace YAML
return m_groups.top().type; return m_groups.top().type;
} }
FlowType::value EmitterState::GetCurGroupFlowType() const FlowType::value EmitterState::CurGroupFlowType() const
{ {
if(m_groups.empty()) if(m_groups.empty())
return FlowType::None; return FlowType::None;
@@ -222,8 +238,7 @@ namespace YAML
EMITTER_MANIP EmitterState::GetFlowType(GroupType::value groupType) const EMITTER_MANIP EmitterState::GetFlowType(GroupType::value groupType) const
{ {
// force flow style if we're currently in a flow // force flow style if we're currently in a flow
FlowType::value flowType = GetCurGroupFlowType(); if(CurGroupFlowType() == FlowType::Flow)
if(flowType == FlowType::Flow)
return Flow; return Flow;
// otherwise, go with what's asked of us // otherwise, go with what's asked of us

View File

@@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <stack> #include <stack>
#include <memory> #include <memory>
#include <stdexcept>
namespace YAML namespace YAML
{ {
@@ -29,15 +30,18 @@ namespace YAML
// basic state checking // basic state checking
bool good() const { return m_isGood; } bool good() const { return m_isGood; }
const std::string GetLastError() const { return m_lastError; } const std::string GetLastError() const { return m_lastError; }
void SetError(const std::string& error) { m_isGood = false; m_lastError = error; } void SetError(const std::string& error) { throw std::runtime_error(error); m_isGood = false; m_lastError = error; }
// group handling // node handling
void BeginScalar();
void BeginGroup(GroupType::value type); void BeginGroup(GroupType::value type);
void EndGroup(GroupType::value type); void EndGroup(GroupType::value type);
GroupType::value GetCurGroupType() const; GroupType::value CurGroupType() const;
FlowType::value GetCurGroupFlowType() const; FlowType::value CurGroupFlowType() const;
int GetCurIndent() const { return m_curIndent; } int CurIndent() const { return m_curIndent; }
bool HasAnchor() const { return m_hasAnchor; }
bool HasTag() const { return m_hasTag; }
void ClearModifiedSettings(); void ClearModifiedSettings();
@@ -85,6 +89,8 @@ namespace YAML
template <typename T> template <typename T>
void _Set(Setting<T>& fmt, T value, FmtScope::value scope); void _Set(Setting<T>& fmt, T value, FmtScope::value scope);
void BeginNode();
private: private:
// basic state ok? // basic state ok?
bool m_isGood; bool m_isGood;
@@ -109,17 +115,20 @@ namespace YAML
SettingChanges m_globalModifiedSettings; SettingChanges m_globalModifiedSettings;
struct Group { struct Group {
Group(GroupType::value type_): type(type_), indent(0) {} explicit Group(GroupType::value type_): type(type_), indent(0), childCount(0) {}
GroupType::value type; GroupType::value type;
EMITTER_MANIP flow; EMITTER_MANIP flow;
int indent; int indent;
std::size_t childCount;
SettingChanges modifiedSettings; SettingChanges modifiedSettings;
}; };
ptr_stack<Group> m_groups; ptr_stack<Group> m_groups;
unsigned m_curIndent; unsigned m_curIndent;
bool m_hasAnchor;
bool m_hasTag;
}; };
template <typename T> template <typename T>

View File

@@ -4,14 +4,11 @@
int main() int main()
{ {
YAML::Emitter out; YAML::Emitter out;
out << YAML::BeginDoc;
out << YAML::BeginSeq; out << YAML::BeginSeq;
out << "foo"; out << "foo";
out << YAML::Comment("Skills");
out << YAML::BeginMap;
out << YAML::Key << "attack" << YAML::Value << 23;
out << YAML::Key << "intelligence" << YAML::Value << 56;
out << YAML::EndMap;
out << YAML::EndSeq; out << YAML::EndSeq;
out << YAML::EndDoc;
std::cout << out.c_str() << "\n"; std::cout << out.c_str() << "\n";
return 0; return 0;