Exagon
Exagon

Reputation: 5088

problems parsing into recursive variant using boost spirit x3

I am currently trying to parse into a x3::variant, using boost spirit x3. the variant looks like:

typedef x3::variant<
    nil,
    x3::forward_ast<LambdaType>,
    x3::forward_ast<ClassType>
> Type

where LambdaType and ClassType are looking like:

struct LambdaType {
        std::vector<Type> parameters_;
        Type return_type_;
    };

struct ClassType{
    std::vector<std::string> name_; 
   std::vector<Type> template_args_;
};

if I try to parse into a Type, or one of these structs, I am getting a compiler error, which tells me I cannot assign a const boost::spirit::x3::char_class to Type, I am confused from this, since I dont ever want to assign a const boost::spirit::x3::char_class. I have a live example here, which has a parser and shows the problem and the error, when you try to compile it. I am trying to solve this problem for a whole day and I am now at a point where I have no idea why this doesnt work. For any help with this I would be more than thankful.

Upvotes: 3

Views: 589

Answers (1)

sehe
sehe

Reputation: 392833

You pass x3::space as the last argument to x3::parse, so it will bind it to the attribute for the parser.

Of course you didn't want that.

Use x3::phrase_parse iff you want to pass a skipper.

PS As written your parser has an additional issue: it has left-recursion in type/lambdaType. I reckon this is because you stubbed out identifier parsing (x3::string("foo")) so here's if you fix the input to be (foo, foo) => foo:

Live On Coliru

#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace x3 = boost::spirit::x3;

namespace ast{
    struct LambdaType;
    struct ClassType;
    struct nil{};

    typedef x3::variant<
        nil,
        x3::forward_ast<LambdaType>,
        x3::forward_ast<ClassType>
    > Type;

    struct LambdaType {
        std::vector<Type> parameters_;
        Type return_type_;
    };

    struct ClassType{
        std::vector<std::string> name_; 
        std::vector<Type> template_args_;
    };

}

BOOST_FUSION_ADAPT_STRUCT(ast::LambdaType, parameters_, return_type_)
BOOST_FUSION_ADAPT_STRUCT(ast::ClassType, name_, template_args_)

namespace parser{
    typedef x3::rule<struct lambda_type_class, ast::LambdaType> lambda_type_type;
    typedef x3::rule<struct class_type_class,  ast::ClassType>  class_type_type;
    typedef x3::rule<struct type_class,        ast::Type>       type_type;

    const class_type_type  class_type  = "class_type";
    const lambda_type_type lambda_type = "lambda_type";
    const type_type        type_p      = "type";

    auto const type_p_def = class_type | lambda_type;

    auto const lambda_type_def =
        ("(" >> -(type_p%",") >> ")" >> "=>" >> type_p)
        | (x3::repeat(1)[type_p%","] >> "=>" >> type_p)
            ;

    auto const class_type_def =
            (x3::string("foo")%"::") >> -("<" >> type_p%"," >> ">")
            ;

    BOOST_SPIRIT_DEFINE(
            lambda_type,
            class_type,
            type_p
    )
}

int main()
{
    std::string input = "(foo, foo) => foo";
    x3::phrase_parse(input.begin(), input.end(), parser::type_p, x3::space);
}

With debug output

<type>
  <try>(foo, foo) => foo</try>
  <class_type>
    <try>(foo, foo) => foo</try>
    <fail/>
  </class_type>
  <lambda_type>
    <try>(foo, foo) => foo</try>
    <type>
      <try>foo, foo) => foo</try>
      <class_type>
        <try>foo, foo) => foo</try>
        <success>, foo) => foo</success>
        <attributes>[[[f, o, o]], []]</attributes>
      </class_type>
      <success>, foo) => foo</success>
      <attributes></attributes>
    </type>
    <type>
      <try> foo) => foo</try>
      <class_type>
        <try> foo) => foo</try>
        <success>) => foo</success>
        <attributes>[[[f, o, o]], []]</attributes>
      </class_type>
      <success>) => foo</success>
      <attributes></attributes>
    </type>
    <type>
      <try> foo</try>
      <class_type>
        <try> foo</try>
        <success></success>
      </class_type>
      <success></success>
    </type>
    <success></success>
  </lambda_type>
  <success></success>
</type>

Upvotes: 3

Related Questions