如何把成果转化为使用升压精神STL的地图吗? [英] How to put results into a STL map by using boost-spirit?

查看:121
本文介绍了如何把成果转化为使用升压精神STL的地图吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include <QtCore/QCoreApplication>

 #include <boost/spirit/include/qi.hpp>
 #include <boost/spirit/include/phoenix.hpp>
 #include <iostream>
 #include <string>
 #include <list>
 #include <map>

 #define CODE_CPP_KEYWORD_ENUM "enum"

namespace haha
{
    //简单表示c++的enum的类(A structure use to simply description C++ enum)
    struct CPPCodeEnum
    {
        //enum的名称(enum Name)
        ::std::string enumName;
        //成员的名称(enum Members‘name)
        ::std::list<::std::string> enumMembers;
    };
}

namespace haha
{
    namespace fusion = boost::fusion;
    namespace phoenix = boost::phoenix;
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    //enum的简单解析器
    template <typename Iterator>
    struct CPPCodeEnumGrammar 
        : qi::grammar<Iterator, CPPCodeEnum(),ascii::space_type >
    {
        CPPCodeEnumGrammar() 
            : CPPCodeEnumGrammar::base_type(start)
        {

            using qi::_val;
            using qi::_1;
            using qi::lit;
            using qi::lexeme;
            using qi::raw;
            using qi::space;
            using ascii::char_;
            using ascii::string;
            using phoenix::push_back;
            //解析一个变量名(他不符合C++的命名规范,暂时只是个替代品)
            quoted_string = lexeme[+(qi::alpha|qi::digit | char_('_'))];

            start =
                //解析"enum"
                lit(CODE_CPP_KEYWORD_ENUM)>>
                //取得enum名
                *quoted_string[ phoenix::bind(&CPPCodeEnum::enumName, _val)= _1]>>
                char_("{")>>
                //解析enum的成员
                *(quoted_string[push_back(phoenix::bind(&CPPCodeEnum::enumMembers, _val), _1)]%',')>>
                //可有可无的逗号小尾巴
                -char_(",")>>
                char_("}");

        }

        qi::rule<Iterator, std::string(),ascii::space_type > quoted_string;
        qi::rule<Iterator, CPPCodeEnum(), ascii::space_type > start;
    };
}



int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //模拟C++的一段代码(test string simulate a section of C++ code)
    ::std::string teststr="enum myename {m1,m2  ,m3 ,m4 ,}";

    using boost::spirit::ascii::space;

    std::string::const_iterator iter = teststr.begin();
    std::string::const_iterator end = teststr.end();

    haha::CPPCodeEnumGrammar<std::string::const_iterator> myCPPCodeEnumGrammar;
    haha::CPPCodeEnum data;

    bool r = phrase_parse(iter, end, myCPPCodeEnumGrammar,  space, data);

    return a.exec();
}

以上我的code正常工作,但是,它是太简单,不解析code是这样的:(枚举myename {M1 = 1,M2 = 44立方米= 89,M4 = 0,} )。我需要同时枚举members'name和value.Now我决定另一种类型的CPP codeEnum :: enumMembers

My code above works correctly,however,it is too simple and not to parse code like this:(enum myename {m1=1,m2=44 ,m3=89 ,m4=0 ,}).I need both enum members’name and value.Now I decide alternative type of "CPPCodeEnum::enumMembers"

    list < std::string::string>


    

      map < std::string::string,int>

。但我不知道如何把成果转化为一个STL的地图使用升压精神?

.But I do not know how to put results into a STL map by using boost-spirit?

推荐答案

请看看<一个href=\"http://boost-spirit.com/home/articles/qi-example/parsing-a-list-of-key-value-pairs-using-spirit-qi/\"相对=nofollow>这篇文章,这说明正是你想要达到的目标。该要点是,Spirit.Qi允许直接解析键/值对成图没有任何额外的code。

Please have a look at this article, which describes exactly what you're trying to achieve. The gist is that Spirit.Qi allows to directly parse the key/value pairs into a map without any additional code.

这篇关于如何把成果转化为使用升压精神STL的地图吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆