erlking
erlking

Reputation: 145

Simple string parser in boost::spirit::x3 not working

For learning purposes I'm trying to write a simple parser that accepts a string literal and puts it in a custom struct using the x3 library from boost. However, the following minimal example which is adapted from the example here does not compile.

#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <string>

namespace x3 = boost::spirit::x3;

namespace ast {
struct Symbol {
    std::string val;
  };
}
BOOST_FUSION_ADAPT_STRUCT(
ast::Symbol,
val
)

namespace parser {
  x3::rule<class Symbol, ast::Symbol> const
  symbol = "symbol";

  auto const symbol_def = x3::string("asd");// | x3::string("qwe");

  BOOST_SPIRIT_DEFINE(
  symbol
  );
}

int main(int argc, char** argv) {
  std::string input = "asd";
  if (argc > 1) {
    input = argv[1];
  }
  std::string::const_iterator begin = input.begin();
  std::string::const_iterator end = input.end();

  ast::Symbol sym;
  bool success = x3::phrase_parse(begin, end, parser::symbol, 
                                  x3::ascii::space, sym);
  std::cout << success << std::endl;
  std::cout << sym.val << std::endl;
  return 0;
 }

This gives a very long, template compiler error that boils down to

cannot convert ‘dest’ (type ‘ast::Symbol’) to type 
‘boost::spirit::x3::traits::variant_attribute’

This does not make sense to me because the parser x3::string should have a string attribute and the ast::Symbol struct has a string field which x3 should be able to populate automatically because I have adapted the struct with the Fusion macro. What's more confusing is that if I change the definition of the parser to read

auto const symbol_def = x3::string("asd") | x3::string("qwe");

it compiles and works, even though now the parser should have an attribute of type variant. Maybe someone can clarify why this happens because I seem to be missing something about how the library works.

Upvotes: 2

Views: 206

Answers (1)

sehe
sehe

Reputation: 393009

I think that's an issue that has been fixed:

Upvotes: 3

Related Questions