C++ 使用Spirit x3,如何控制对每个不同的输入调用哪个解析器?

C++ 使用Spirit x3,如何控制对每个不同的输入调用哪个解析器?,c++,boost-spirit,boost-spirit-x3,C++,Boost Spirit,Boost Spirit X3,我正在使用boost spirit x3,有一点我不清楚。我有一个文件,有非常不同和重复的行。前几行可能是注释。接下来的1000行可能是坐标,接下来的1000行可能是int的列表,等等 我的问题是如何识别该行并知道该行使用哪个解析器。例如,这里有两个解析器函数 template <typename Iterator> bool parse_ints(Iterator first, Iterator last, std::vector<int>& v) {

我正在使用boost spirit x3,有一点我不清楚。我有一个文件,有非常不同和重复的行。前几行可能是注释。接下来的1000行可能是坐标,接下来的1000行可能是int的列表,等等

我的问题是如何识别该行并知道该行使用哪个解析器。例如,这里有两个解析器函数

template <typename Iterator>
bool parse_ints(Iterator first, Iterator last, std::vector<int>& v)
{
    using x3::int_;
    using x3::phrase_parse;
    using x3::_attr;   
    using ascii::space;

    auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };

    bool r = phrase_parse(first, last,
        (
            int_[push_back]
                >> *(',' >> int_[push_back])
        )
        ,
        space);

    if (first != last) 
        return false;
    return r;
}

template <typename Iterator>
bool parse_doubles(Iterator first, Iterator last, std::vector<double>& v)
{
    using x3::double_;
    using x3::phrase_parse;
    using x3::_attr;
    using ascii::space;

    auto push_back = [&](auto& ctx){ v.push_back(_attr(ctx)); };

    bool r = phrase_parse(first, last,
        (
            double_[push_back]
                >> *(',' >> double_[push_back])
        )
        ,
        space);

    if (first != last) // fail if we did not get a full match
        return false;
    return r;
}

你的语法可以描述整个输入。所以你可以说

auto input = *headerComment 
          >> points 
          >> edges;
你可以简单地定义

auto headerComment = '#' >> *(char_ - eol) >> eol;
和例如

auto points = skip(blank) [ 
     *(point >> eol)
   ];

auto edges = skip(blank) [ 
     *(edge >> eol)
   ];

point = int_ >> int_ >> int_; // assuming x y z
edge  = int_ >> int_;         // assuming from to
这假定输入格式是明确的。也就是说,如果点是
xy
,那么它就不起作用了,因为它无法与边缘区分

auto points = skip(blank) [ 
     *(point >> eol)
   ];

auto edges = skip(blank) [ 
     *(edge >> eol)
   ];

point = int_ >> int_ >> int_; // assuming x y z
edge  = int_ >> int_;         // assuming from to