简体   繁体   中英

Boost spirit x3 tuple member in fusion-adapted struct

The following test case is a reduction of a larger, multi-file parser, hence the slightly odd order of declarations and definitions. It does not compile and my understanding is that the std::tuple trips it. From the docs it seems to me that the synthesized attribute there should be an std::tuple provided the correct includes are present. What am I doing wrong?

// -*- mode: c++; -*-

#include <iostream>
#include <tuple>

#include <boost/fusion/include/io.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/std_tuple.hpp>
#include <boost/fusion/adapted/std_tuple.hpp>

#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;

#include <boost/spirit/include/support_istream_iterator.hpp>

namespace ast {

struct S {
    std::tuple< int, int > t;
    int i;
};

using boost::fusion::operator<<;

} // namespace ast

BOOST_FUSION_ADAPT_STRUCT (ast::S, t, i)

namespace parser {

using S_type = x3::rule< struct S_class, ast::S >;
BOOST_SPIRIT_DECLARE(S_type)

struct S_class;
const S_type S = "S";

using x3::int_;
const auto S_def = ( int_ >> int_ ) >> int_;

BOOST_SPIRIT_DEFINE(S)

struct S_class { };

} // namespace parser

template< typename Iterator >
bool parse (Iterator& iter, Iterator last, ast::S& s) {
    using x3::ascii::space;

#if 1
    return x3::phrase_parse (iter, last, parser::S, space, s);
#else
    return x3::parse (iter, last, ( x3::int_ >> x3::int_ ) >> x3::int_, s);
#endif // 0
}

int main () {
    const std::string s = "1 2 3";
    auto iter = s.begin ();

    ast::S obj;
    return !parse (iter, s.end (), obj);
}

Thanks a bunch.

It's a nested structure. However you parse into a flat synthesized tuple that doesn't match the AST structure:

In other words ((int, int), int) is not structurally compatible with (int, (int, int)) or even (int, int, int) as your rule would parse.

The mentioned workarounds help with the attribute coercion to reflect the desired (and required) structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM