muzgen/lib/Node.cpp

59 lines
1.0 KiB
C++
Raw Permalink Normal View History

#include "Node.hpp"
namespace muz
{
/*explicit*/ Node::Node(NodeType type,
int line,
std::string const& value)
: m_type { type }
, m_line { line }
, m_value { value }
{
}
/*virtual*/ Node::~Node()
{
}
void Node::add_child(std::shared_ptr<Node> child)
{
m_children.push_back(child);
}
std::shared_ptr<Node> Node::child(size_t index) const
{
if (index >= m_children.size())
{
throw node_error {"cannot get node child: bad index"};
}
return m_children[index];
}
std::string Node::string() const
{
std::stringstream ss;
ss << NodeTypeStr[type()] + strlen("NODE_");
if (m_value.empty() == false)
{
ss << "[" << m_value << "]";
}
if (m_children.empty() == false)
{
std::string sep;
ss << "(";
for (auto& c: m_children)
{
ss << sep << c->string();
sep = ",";
}
ss << ")";
}
return ss.str();
}
}