C++实现通用AST语法树结构-创新互联
class ASTNode;
typedef std::mapPropertyContainer;
typedef std::vector ChildContainer;
class ASTNode: PropertyContainer, ChildContainer
{
public:
ASTNode(){}
ASTNode(std::initializer_listlist)
: PropertyContainer (list)
{
}
ChildContainer::reference operator[](size_t index) _GLIBCXX_NOEXCEPT
{
return std::vector::operator[](index);
}
ASTNode &operator<< (const ASTNode &node) _GLIBCXX_NOEXCEPT
{
push_back(node); return *this;
}
size_t size() const _GLIBCXX_NOEXCEPT
{
return std::vector::size();
}
templatevoid recursionChilds(Funct_ f) const _GLIBCXX_NOEXCEPT {
foreachChilds([=](ChildContainer::const_reference ref) {
ref.recursionChilds(f);
});
}
templatevoid recursion(Funct_ f) const _GLIBCXX_NOEXCEPT {
f(*this);
recursionChilds(f);
}
templatevoid recursion(Funct_ f, int &level) const _GLIBCXX_NOEXCEPT {
f(*this);
level ++;
foreachChilds([&](ChildContainer::const_reference ref) {
ref.recursion(f, level);
});
level --;
}
templatevoid foreachChilds(Funct_ f) const _GLIBCXX_NOEXCEPT {
std::for_each(ChildContainer::begin(), ChildContainer::end(), f);
}
templatevoid foreachPropertys(Funct_ f) const _GLIBCXX_NOEXCEPT {
std::for_each(PropertyContainer::begin(), PropertyContainer::end(), f);
}
PropertyContainer::mapped_type &operator[](const std::string &key) _GLIBCXX_NOEXCEPT
{
return PropertyContainer::operator[](key);
}
std::vectorkeys() const _GLIBCXX_NOEXCEPT {
std::vectorret;
auto itera = PropertyContainer::begin();
while (itera != PropertyContainer::end()) {
ret.push_back(itera->first);
itera ++;
}
return ret;
}
std::vectorvalues() const _GLIBCXX_NOEXCEPT {
std::vectorret;
auto itera = PropertyContainer::begin();
while (itera != PropertyContainer::end()) {
ret.push_back(itera->second);
itera ++;
}
return ret;
}
size_t count() const _GLIBCXX_NOEXCEPT
{
return PropertyContainer::size();
}
void dump()
{
int level = 0;
recursion([&](ChildContainer::const_reference ref){
auto getHead = [=](int level) ->std::string {
int c = 0; std::string ret;
while (c< level) {
ret += " ";
c++;
}
return ret;
};
std::cout<< getHead(level);
ref.foreachPropertys([&](PropertyContainer::const_reference ref)
{
std::cout<< ref.first<< ":"<< ref.second<< " ";
});
std::cout<< std::endl;
}, level);
}
void dumpSave(const std::string &file)
{
std::ofstream ofstream;
ofstream.open(file);
int level = 0;
recursion([&](ChildContainer::const_reference ref){
auto getHead = [=](int level) ->std::string {
int c = 0; std::string ret;
while (c< level) {
ret += " ";
c++;
}
return ret;
};
ofstream<< getHead(level);
ref.foreachPropertys([&](PropertyContainer::const_reference ref)
{
ofstream<< ref.first<< ":"<< ref.second<< " ";
});
ofstream<< std::endl;
}, level);
}
void dumpLoad(const std::string &file)
{
}
};
ASTNode genRoot() {
ASTNode root{{"name", "root"}};
ASTNode one{{"name", "public"},{"start", "[0:0]"}};
ASTNode two{{"name", "class"},{"start", "[0:7]"}};
ASTNode three{{"name", "Hello"},{"start", "[0:13]"}};
root<< one<< two<< three;
root[0]<< one<< two<< three;
root[0][0]<< one<< two<< three;
root[0][0][0]<< one<< two<< three;
root[1]<< one<< two<< three;
root[2]<< one<< two<< three;
return root;
}
int main(int argc, char *argv[])
{
ASTNode root = genRoot();
root.dump();
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
枞阳网站建设公司成都创新互联,枞阳网站设计制作,有大型网站制作公司丰富经验。已为枞阳成百上千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的枞阳做网站的公司定做!本文题目:C++实现通用AST语法树结构-创新互联
当前URL:http://azwzsj.com/article/dsedgd.html