Added tags and anchors

This commit is contained in:
Jesse Beder 2012-05-21 23:19:29 -05:00
parent 91eac5d93d
commit 3cae26a75e
4 changed files with 69 additions and 14 deletions

View File

@ -284,17 +284,26 @@ namespace YAML
void Emitter::PrepareTopNode(EmitterNodeType::value child)
{
const bool hasAnchor = m_pState->HasAnchor();
const bool hasTag = m_pState->HasTag();
if(!hasAnchor && !hasTag && m_stream.pos() > 0) {
if(!m_pState->HasBegunNode() && m_stream.pos() > 0) {
EmitBeginDoc();
}
switch(child) {
case EmitterNodeType::None:
case EmitterNodeType::Scalar:
case EmitterNodeType::FlowSeq:
case EmitterNodeType::FlowMap:
// TODO: if we were writing null, and
// we wanted it blank, we wouldn't want a space
if(hasAnchor || hasTag)
if(m_pState->HasBegunNode())
m_stream << " ";
break;
case EmitterNodeType::BlockSeq:
case EmitterNodeType::BlockMap:
if(m_pState->HasBegunNode())
m_stream << "\n";
break;
}
}
void Emitter::FlowSeqPrepareNode(EmitterNodeType::value child)
@ -306,7 +315,7 @@ namespace YAML
const unsigned curIndent = m_pState->CurIndent();
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
if(!m_pState->HasTag() && !m_pState->HasAnchor()) {
if(!m_pState->HasBegunNode()) {
if(m_pState->CurGroupChildCount() > 0) {
m_stream << "\n";
}
@ -342,7 +351,7 @@ namespace YAML
const unsigned nextIndent = curIndent + m_pState->CurGroupIndent();
const std::size_t childCount = m_pState->CurGroupChildCount();
if(!m_pState->HasTag() && !m_pState->HasAnchor()) {
if(!m_pState->HasBegunNode()) {
if(childCount % 2 == 0) {
// key
if(childCount > 0) {
@ -508,7 +517,19 @@ namespace YAML
if(!good())
return *this;
m_pState->BeginScalar();
if(m_pState->HasAnchor()) {
m_pState->SetError(ErrorMsg::INVALID_ANCHOR);
return *this;
}
PrepareNode(EmitterNodeType::None);
if(!Utils::WriteAnchor(m_stream, anchor.content)) {
m_pState->SetError(ErrorMsg::INVALID_ANCHOR);
return *this;
}
m_pState->SetAnchor();
return *this;
}
@ -518,7 +539,27 @@ namespace YAML
if(!good())
return *this;
m_pState->BeginScalar();
if(m_pState->HasTag()) {
m_pState->SetError(ErrorMsg::INVALID_TAG);
return *this;
}
PrepareNode(EmitterNodeType::None);
bool success = false;
if(tag.type == _Tag::Type::Verbatim)
success = Utils::WriteTag(m_stream, tag.content, true);
else if(tag.type == _Tag::Type::PrimaryHandle)
success = Utils::WriteTag(m_stream, tag.content, false);
else
success = Utils::WriteTagWithPrefix(m_stream, tag.prefix, tag.content);
if(!success) {
m_pState->SetError(ErrorMsg::INVALID_TAG);
return *this;
}
m_pState->SetTag();
return *this;
}

View File

@ -43,6 +43,16 @@ namespace YAML
SetMapKeyFormat(value, FmtScope::Local);
}
void EmitterState::SetAnchor()
{
m_hasAnchor = true;
}
void EmitterState::SetTag()
{
m_hasTag = true;
}
void EmitterState::BeginNode()
{
if(!m_groups.empty())

View File

@ -34,6 +34,8 @@ namespace YAML
void SetError(const std::string& error) { throw std::runtime_error(error); m_isGood = false; m_lastError = error; }
// node handling
void SetAnchor();
void SetTag();
void BeginScalar();
void BeginGroup(GroupType::value type);
void EndGroup(GroupType::value type);
@ -49,6 +51,7 @@ namespace YAML
int CurIndent() const { return m_curIndent; }
bool HasAnchor() const { return m_hasAnchor; }
bool HasTag() const { return m_hasTag; }
bool HasBegunNode() const { return m_hasAnchor || m_hasTag; }
void ClearModifiedSettings();

View File

@ -4,9 +4,10 @@
int main()
{
YAML::Emitter out;
out << YAML::Anchor("monkey");
out << YAML::BeginSeq;
out << "foo";
out << "bar";
out << YAML::LocalTag("hi") << "bar";
out << YAML::BeginMap;
out << "a" << "b" << "c" << "d";
out << YAML::EndMap;