Search code examples
c++boost-spirit-x3

Compiler errors when parsing into x3::variant


I´m doing more or less my first steps with X3 and already managed to parse a simple struct with 2 members. But I fail to put this struct into a variant.

The (simplified) code looks something like this:

struct Command1
{
            CommandType type;
            std::string objName;
}
BOOST_FUSION_ADAPT_STRUCT(
    Command1,
    type, objName
);

struct Nil {};
using Command = x3::variant<Nil, Command1>;

const x3::rule<struct create_cmd_rule, Command1> ccRule = "ccRule";
const auto ccRule_def = typeRule > identifier;

const x3::rule<struct create_rule, Command> cRule = "cRule";
const auto cRule_def = x3::omit[x3::no_case["CREATE"]] > (ccRule_def);

If I call it like this

Command1 cmd;
x3::phrase_parse(statement.cbegin(), statement.cend(), parser::cRule_def, x3::space, cmd);

everything is fine. But if I pass my variant:

Command cmd;
x3::phrase_parse(statement.cbegin(), statement.cend(), parser::cRule_def, x3::space, cmd);

it does not compile: Severity Code Description Project File Line Suppression State Error C2665 'boost::spirit::x3::traits::detail::move_to': none of the 4 overloads could convert all the argument types ZeusCore d:\boost_1_67_0\boost\spirit\home\x3\support\traits\move_to.hpp 224

I hope, I did not simplify the code to much...

I´m using boost 1.67 and the newest version of Visual Studio 2017.


Solution

  • From what you've posted it seems that the problem in referencing *_def. The cRule_def and ccRule_def are not rules, they just chained parsers stored in variables.

    Try to replace:

    const auto cRule_def = x3::omit[x3::no_case["CREATE"]] > (ccRule_def);
    

    with:

    const auto cRule_def = x3::omit[x3::no_case["CREATE"]] > (ccRule);
    
    BOOST_SPIRIT_DEFINE(cRule, ccRule);
    

    and call it like:

    Command1 cmd;
    x3::phrase_parse(statement.cbegin(), statement.cend(), parser::cRule, x3::space, cmd);
    

    Here is a toy working example which I used to try to replicate the error https://wandbox.org/permlink/BMP5zzHxPZo7LUDi

    Other note: x3::omit in x3::omit[x3::no_case["CREATE"]] is redundant.