1

I am porting some legacy code from VS2010 & boost1.53 to VS2017 & boost1.71.

I have got stuck last two hours while trying compiling it.

The code is:

#include <string>
#include <vector>
#include <fstream>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
using qi::_1; using qi::_2; using qi::_3; using qi::_4;

enum TYPE { SEND, CHECK, COMMENT };

struct Command
{
    TYPE type;
    std::string id;
    std::string arg1;
    std::string arg2;
    bool checking;
};

class Parser
{
    typedef boost::spirit::istream_iterator It;
    typedef std::vector<Command> Commands;

    struct deferred_fill
    {
        template <typename R, typename S, typename T, typename U> struct result { typedef void type; };//Not really sure still necessary
        typedef void result_type;//Not really sure still necessary

        void operator() (boost::iterator_range<It> const& id, boost::iterator_range<It> const& arg1, bool checking, Command& command) const
        {
            command.type = TYPE::SEND;
            command.id.assign(id.begin(), id.end());
            command.arg1.assign(arg1.begin(), arg1.end());
            command.checking = checking;
        }
    };

private:
    qi::symbols<char, bool> value;
    qi::rule<It> ignore;
    qi::rule<It, Command()> send;
    qi::rule<It, Commands()> start;
    boost::phoenix::function<deferred_fill> fill;

public:
    std::vector<Command> commands;

    Parser()
    {
        using namespace qi;
        using boost::phoenix::push_back;

        value.add("TRUE", true)
                 ("FALSE", false);

        send = ("SEND_CONFIRM" >> *blank >> '(' >> *blank >> raw[*~char_(',')] >> ','
                                                >> *blank >> raw[*~char_(',')] >> ','
                                                >> *blank >> value >> *blank >> ')' >> *blank >> ';')[fill(_1, _2, _3, _val)];

        ignore = *~char_("\r\n");

        start = (send[push_back(_val, _1)] | ignore) % eol;
    }

    void parse(const std::string& path)
    {
        std::ifstream in(path, std::ios_base::in);
        if (!in) return;

        in >> std::noskipws;//No white space skipping
        boost::spirit::istream_iterator first(in);
        boost::spirit::istream_iterator last;

        qi::parse(first, last, start, commands);
    }
};

int main(int argc, char* argv[])
{
    Parser parser;
    parser.parse("file.txt");

    return 0;
}

The compiler complains in the next way (only copy first lines):

1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(116): error C2039: 'type': no es un miembro de 'boost::result_of<const Parser::deferred_fill (std::vector<Value,std::allocator<char>> &,std::vector<Value,std::allocator<char>> &,boost::iterator_range<Parser::It> &,Command &)>'
1>        with
1>        [
1>            Value=char
1>        ]
1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(114): note: vea la declaración de 'boost::result_of<const Parser::deferred_fill (std::vector<Value,std::allocator<char>> &,std::vector<Value,std::allocator<char>> &,boost::iterator_range<Parser::It> &,Command &)>'
1>        with
1>        [
1>            Value=char
1>        ]
1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(89): note: vea la referencia a la creación de instancias de plantilla clase de 'boost::phoenix::detail::function_eval::result_impl<F,void (Head,const boost::phoenix::actor<boost::spirit::argument<1>>&,const boost::phoenix::actor<boost::spirit::argument<2>>&,const boost::phoenix::actor<boost::spirit::attribute<0>>&),const boost::phoenix::vector2<Env,Actions> &>' que se está compilando
1>        with
1>        [
1>            F=const boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<Parser::deferred_fill>,0> &,
1>            Head=const boost::phoenix::actor<boost::spirit::argument<0>> &,
1>            Env=boost::phoenix::vector4<const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::phoenix::detail::tag::function_eval,boost::proto::argsns_::list5<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<Parser::deferred_fill>,0>,boost::phoenix::actor<boost::spirit::argument<0>>,boost::phoenix::actor<boost::spirit::argument<1>>,boost::phoenix::actor<boost::spirit::argument<2>>,boost::phoenix::actor<boost::spirit::attribute<0>>>,5>> *,boost::fusion::vector<std::vector<char,std::allocator<char>>,std::vector<char,std::allocator<char>>,boost::iterator_range<Parser::It>,std::vector<char,std::allocator<char>>,boost::iterator_range<Parser::It>,std::vector<char,std::allocator<char>>,bool,std::vector<char,std::allocator<char>>,std::vector<char,std::allocator<char>>> &,boost::spirit::context<boost::fusion::cons<Command &,boost::fusion::nil_>,boost::fusion::vector<>> &,bool &> &,
1>            Actions=const boost::phoenix::default_actions &
1>        ]

I guess that error is related with the use of boost::spirit::istream_iterator, instead of char*, but I cann't figure out how to fix it to work again.

I have run out of ideas, please, anyone can see where my mistake is?

sehe
  • 374,641
  • 47
  • 450
  • 633
Pablo
  • 557
  • 3
  • 16

1 Answers1

3

Aw. You're doing awesome things. Sadly/fortunately it's overcomplicated.

So let's first fix, and then simplify.

The Error

It's like you said,

void operator() (boost::iterator_range<It> const& id, boost::iterator_range<It> const& arg1, bool checking, Command& command) const

Doesn't match what actually gets invoked:

void Parser::deferred_fill::operator()(T&& ...) const [with T = {std::vector<char>&, std::vector<char>&, boost::iterator_range<boost::spirit::basic_istream_iterator<...> >&, Command&}]

The reason is NOT the iterator (as you can see it's boost::spirit__istream_iterator alright).

However it's because you're getting other things as attributes. Turns out *blank exposes the attribute as a vector<char>. So you can "fix" that by omit[]-ing those. Let's instead wrap it in an attribute-less rule like ignore so we reduce the clutter.

Now the invocation is with

void Parser::deferred_fill::operator()(T&& ...) const [with T = {boost::iterator_range<It>&, boost::iterator_range<It>&, bool&, Command&}]

So it is compatible and compiles. Parsing:

SEND_CONFIRM("this is the id part", "this is arg1", TRUE);

With

Parser parser;
parser.parse("file.txt");

std::cout << std::boolalpha;
for (auto& cmd : parser.commands) {
    std::cout << '{' << cmd.id << ", "
        << cmd.arg1 << ", "
        << cmd.arg2 << ", "
        << cmd.checking << "}\n";
}

Prints

{"this is the id part", "this is arg1", , TRUE}

Let's improve this

  • This calls for a skipper
  • This calls for automatic attribute propagation
  • Some other elements of style

Skippers

Instead of "calling" a skipper explicitly, let's use the built in capability:

rule<It, Attr(), Skipper> x;

defines a rule that skips over inputs sequences matched by a parser of the Skipper type. You need to actually pass in the skipper of that type.

  • using qi::phrase_parse instead of qi::parse
  • by using the qi::skip() directive

I always advocate the second approach, because it makes for a friendlier, less error-prone interface.

So declaring the skipper type:

qi::rule<It, Command(), qi::blank_type> send;

We can reduce the rule to:

    send = (lit("SEND_CONFIRM") >> '(' 
            >> raw[*~char_(',')] >> ','
            >> raw[*~char_(',')] >> ','
            >> value >> ')' >> ';')
        [fill(_1, _2, _3, _val)];

And than pass a skipper from the start rule:

    start = skip(blank) [
            (send[push_back(_val, _1)] | ignore) % eol
        ];

That's all. Still compiles and matches the same.

Live On Coliru

Skipping with Lexemes

Still the same topic, lexemes actually inhibit the skipper¹, so you don't have to raw[]. This changes the exposed attributes to vector<char> as well:

void operator() (std::vector<char> const& id, std::vector<char> const& arg1, bool checking, Command& command) const

Live On Coliru

Automatic Attribute Propagation

Qi has semantic actions, but its real strength is in them being optional: Boost Spirit: "Semantic actions are evil"?

  • push_back(_val, _1) is actually the automatic attribute propagation semantics anwyays for *p, +p and p % delim² anyways, so just drop it:

    start = skip(blank) [
            (send | ignore) % eol
        ];
    

    (note that send|ignore actually synthesizes optional<Command> which is fine for automatic propagation)

  • std::vector is attribute-compatible with std::string, e.g.. So if we can add a placeholder for arg2 we can match the Command structure layout:

    send = lit("SEND_CONFIRM") >> '(' 
        >> attr(SEND) // fill type
        >> lexeme[*~char_(',')] >> ','
        >> lexeme[*~char_(',')] >> ','
        >> attr(std::string()) // fill for arg2
        >> value >> ')' >> ';'
    ;
    

    Now to be able to drop fill and its implementation, we have to adapt Command as a fusion sequence:

    BOOST_FUSION_ADAPT_STRUCT(Command, type, id, arg1, arg2, checking)
    

Elements of Style 1

Using a namespace for your Command types makes it easier to ADL use the operator<< overloads for Commands, se we can just std::cout << cmd;

At this point, it all works in a fraction of the code: Live On Coliru

Elements of Style 2

  • If you can, make your parser stateless. That means it can be const, so you can:

    • reuse it without costly construction
    • the optimizer has more to work with
    • it's more testable (stateful things are harder to prove idempotent)

    So, instead of having commands a member, just return them. While we're at it, we can make parse a static function

  • Instead of hardcoding the iterator type, it's flexible to have it as a template argument. That way you're not stuck with the overhead of multi_pass_adaptor and istream_iterator if you have a command in a char[] buffer, string or string_view at some point.

  • Also, deriving your Parser from qi::grammar with a suitable entry-point means you can use it as a parser expression (actually a non-terminal, just like rule<>) as any other parser.

  • Consider enabling rule debugging (see example)

Full Code

Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <fstream>

namespace qi = boost::spirit::qi;

namespace Commands {
    enum TYPE { SEND, CHECK, COMMENT };
    enum BOOL { FALSE, TRUE };

    struct Command {
        TYPE type;
        std::string id;
        std::string arg1;
        std::string arg2;
        BOOL checking;
    };

    typedef std::vector<Command> Commands;

    // for (debug) output
    static inline std::ostream& operator<<(std::ostream& os, TYPE t) {
        switch (t) {
            case SEND: return os << "SEND";
            case CHECK: return os << "CHECK";
            case COMMENT: return os << "COMMENT";
        }
        return os << "(unknown)";
    }
    static inline std::ostream& operator<<(std::ostream& os, BOOL b) {
        return os << (b?"TRUE":"FALSE");
    }

    using boost::fusion::operator<<;
}
BOOST_FUSION_ADAPT_STRUCT(Commands::Command, type, id, arg1, arg2, checking)

namespace Commands {
    template <typename It>
    class Parser : public qi::grammar<It, Commands()> {
      public:
        Commands commands;

        Parser() : Parser::base_type(start) {
            using namespace qi;

            value.add("TRUE", TRUE)
                     ("FALSE", FALSE);

            send = lit("SEND_CONFIRM") >> '(' 
                >> attr(SEND) // fill type
                >> lexeme[*~char_(',')] >> ','
                >> lexeme[*~char_(',')] >> ','
                >> attr(std::string()) // fill for arg2
                >> value >> ')' >> ';'
            ;

            ignore = +~char_("\r\n");

            start = skip(blank) [
                    (send | ignore) % eol
                ];

            BOOST_SPIRIT_DEBUG_NODES((start)(send)(ignore))
        }

      private:
        qi::symbols<char, BOOL> value;
        qi::rule<It> ignore;
        qi::rule<It, Command(), qi::blank_type> send;
        qi::rule<It, Commands()> start;
    };

    static Commands parse(std::istream& in) {
        using It = boost::spirit::istream_iterator;

        static const Parser<It> parser;

        It first(in >> std::noskipws), //No white space skipping
           last;

        Commands commands;
        if (!qi::parse(first, last, parser, commands)) {
            throw std::runtime_error("command parse error");
        }

        return commands; // c++11 move semantics
    }
}

int main() {
    try {
        for (auto& cmd : Commands::parse(std::cin))
            std::cout << cmd << "\n";
    } catch(std::exception const& e) {
        std::cout << e.what() << "\n";
    }
}

Prints

(SEND "this is the id part" "this is arg1"  TRUE)

Or indeed with BOOST_SPIRIT_DEBUG defined:

<start>
  <try>SEND_CONFIRM("this i</try>
  <send>
    <try>SEND_CONFIRM("this i</try>
    <success>\n</success>
    <attributes>[[SEND, [", t, h, i, s,  , i, s,  , t, h, e,  , i, d,  , p, a, r, t, "], [", t, h, i, s,  , i, s,  , a, r, g, 1, "], [], TRUE]]</attributes>
  </send>
  <send>
    <try></try>
    <fail/>
  </send>
  <ignore>
    <try></try>
    <fail/>
  </ignore>
  <success>\n</success>
  <attributes>[[[SEND, [", t, h, i, s,  , i, s,  , t, h, e,  , i, d,  , p, a, r, t, "], [", t, h, i, s,  , i, s,  , a, r, g, 1, "], [], TRUE]]]</attributes>
</start>

¹ while pre-skipping as you require; see Boost spirit skipper issues

² (and then some, but let's not digress)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 3
    As always, a top class answer, both fixing the asked problem and providing also a new point of view to improve the code. I always learn at least one new thing from your posts. Thank you so much! – Pablo Jun 17 '20 at 23:08
  • 3
    Cheers. It is what I do it for. I too learn while answering, and I learn from other people's answers in the same way. – sehe Jun 17 '20 at 23:10