提升::精神.将(名称)(描述)文本解析到地图

boost::spirit. Parsing (name) (description) text to a map

本文关键字:文本 地图 描述 名称 提升 精神      更新时间:2023-10-16

>我目前正在以下一个格式解析文本文件中的数据库模式:

(table_name) (table_description)

元素之间的分隔符是双回车(nn)我需要将其解析为地图,使用 boost::spirit 进行解析。

问题是table_description也可以包含双重回车(nn)。

table_name有严格的格式,这是*qi::char_("a-z0-9_").table_description可以包含任何字符,但以后始终从大写字母开始。

任何想法如何为这个解析器创建语法?

这与Spirit

docs:使用Spirit.Qi解析键值对列表(2009年11月15日)中的文章非常相似。

我能想到的最直接的语法依赖于括号:

    start         = pair % "nn";
    parenthesized = '(' > *(char_ - ')') > ')';
    pair          = parenthesized >> "nn" >> parenthesized;

您当然可以增强它以要求您需要的表名和描述(例如以大写字母开头)的确切语法,但以上是用于说明的。

唯一的/nifty/位是:

  • 使用 char_ - ')' _greedily匹配括号内的任何内容(请注意,这尚不支持嵌套的括号集)
  • 使用融合适配器对 std::p air 直接解析为 std::p air
  • 使用qi::blank(不是qi::space)船长以避免忽略换行

下面是一个完整的示例:

//#define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi    = boost::spirit::qi;
namespace karma = boost::spirit::karma;
typedef std::map<std::string, std::string> map_t;
template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, map_t(), Skipper>
{
    parser() : parser::base_type(start)
    {
        using namespace qi;
        // using phx::bind; using phx::ref; using phx::val;
        start         = pair % "nn";
        pair          = parenthesized >> "nn" >> parenthesized;
        parenthesized = '(' > *(char_ - ')') > ')';
        BOOST_SPIRIT_DEBUG_NODE(parenthesized);
        BOOST_SPIRIT_DEBUG_NODE(pair);
        BOOST_SPIRIT_DEBUG_NODE(start);
    }
  private:
    qi::rule<It, std::string(), Skipper > parenthesized;
    qi::rule<It, std::pair<std::string, std::string>(), Skipper> pair;
    qi::rule<It, std::map <std::string, std::string>(), Skipper> start;
};
template <typename C, typename Skipper>
    bool doParse(const C& input, const Skipper& skipper)
{
    auto f(std::begin(input)), l(std::end(input));
    parser<decltype(f), Skipper> p;
    map_t data;
    try
    {
        bool ok = qi::phrase_parse(f,l,p,skipper,data);
        if (ok)   
        {
            std::cout << "parse successn";
            std::cout << "data: " << karma::format(
                 (karma::auto_ << ": "" << karma::auto_ << """) % karma::eol, 
                 data) << "n";
        }
        else      std::cerr << "parse failed: '" << std::string(f,l) << "'n";
        if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'n";
        return ok;
    } catch(const qi::expectation_failure<decltype(f)>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'n";
    }
    return false;
}
template <typename C>
    bool doParse(const C& input)
{
    return doParse(input, qi::blank);
}
int main()
{
    const std::string input = "(table_name)nn(table_description)nn(other_table)nn(othernndescription)";
    bool ok = doParse(input);
    return ok? 0 : 255;
}

测试输出:

parse success
data: other_table: "other
description"
table_name: "table_description"