Search code examples
c++c++17boost-spirit-x3

Simple Spirit X3 word splitter won't compile, attribute mismatch


I'm trying to use a Spirit X3 parser to process the output from a command-line tool, but have been having issues. I've narrowed them down to a minimal example whose behaviour I don't understand:

#include <string>
#include <vector>
#include <boost/spirit/home/x3.hpp>

int main() {
    namespace x3 = boost::spirit::x3;

    std::wstring const str = L"bonjour je suis un  petit panda";

    auto word = x3::lexeme[+x3::alpha];

    std::wstring s;
    x3::phrase_parse(begin(str), end(str), word, x3::space, s); // OK

    std::vector<std::wstring> v;
    x3::phrase_parse(begin(str), end(str), *word, x3::space, v); // Compiler error
}

Live demo on Coliru

The error is very hairy, but boils down to not being able to call move_to, which IIUC is a symptom of an attribute type mismatch.

/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:180:9: error: no matching function for call to 'move_to'
        detail::move_to(std::move(src), dest
        ^~~~~~~~~~~~~~~

[...]

/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:56:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::unused_attribute' for 3rd argument
        move_to(Source&&, Dest&, unused_attribute) {}
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:74:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::plain_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, plain_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:97:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::tuple_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, tuple_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:106:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::tuple_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, tuple_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:150:9: note: candidate function not viable: no known conversion from 'typename attribute_category<basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t> > >::type' (aka 'boost::spirit::x3::traits::container_attribute') to 'boost::spirit::x3::traits::variant_attribute' for 3rd argument
        move_to(Source&& src, Dest& dest, variant_attribute tag)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:85:35: note: candidate template ignored: disabled by 'enable_if' [with Source = const wchar_t, Dest = std::__1::basic_string<wchar_t, std::__1::char_traits<wchar_t>, std::__1::allocator<wchar_t> >]
        inline typename enable_if<is_container<Source>>::type
                                  ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:113:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Source&& src, Dest& dest, variant_attribute, mpl::false_)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:143:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Source&& src, Dest& dest, variant_attribute, mpl::true_)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:157:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator, Iterator, unused_type, unused_attribute) {}
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:161:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator first, Iterator last, Dest& dest, container_attribute)
        ^
/usr/local/include/boost/spirit/home/x3/support/traits/move_to.hpp:171:9: note: candidate function template not viable: requires 4 arguments, but 3 were provided
        move_to(Iterator first, Iterator last, boost::iterator_range<Iterator>& rng, container_attribute)
        ^

My goal is to word-split the sentence by whitespace. The word parser returns the first full word into a std::string as expected. Why isn't *word directly compatible with std::vector<std::string>, and what am I supposed to write instead?


Solution

  • I am not sure it actually should work even for the first case.

    You are using ASCII version of character parsers (x3::alpha is a synonym for x3::standard::alpha), passed iterator value type is wchar_t, but boost::spirit::char_encoding::standard::ischar() returns false for non-ascii characters.

    With x3::standard_wide::alpha it works:

    #include <string>
    #include <vector>
    #include <boost/spirit/home/x3.hpp>
    
    int main() {
        namespace x3 = boost::spirit::x3;
    
        std::wstring const str = L"bonjour je suis un  petit panda";
    
        auto word = x3::lexeme[+x3::standard_wide::alpha];
    
        std::wstring s;
        x3::phrase_parse(begin(str), end(str), word, x3::space, s); // OK
    
        std::vector<std::wstring> v;
        x3::phrase_parse(begin(str), end(str), *word, x3::space, v); // OK
    }
    

    Another good question should it work with ASCII skipper, and if there a difference between x3::standard::space and x3::standard_wide::space (wide one might consider more characters as white spaces because of Unicode).