1

There is a need of providing the values from an object of type boost::variant for an std::pair object. How would you implement this idea using other resources? Any other way than this is done below?

    struct aggr_pair_visitor : public ::boost::static_visitor<void>
    {
    public:
        explicit aggr_pair_visitor( column_and_aggregate & pair_ ) : pair(pair_)
        {
        }
        void operator()(column_name_t const & column)
        {
            pair.first = column;
        }
        void operator()(unsigned const & faggr)
        {
            if ( faggr > static_cast<unsigned>(sql_faggregate::SUM) || faggr < static_cast<unsigned>(sql_faggregate::AVG) )
                throw std::runtime_error("Failed to parse aggregate type : Not valid integer");
            else pair.second = static_cast<sql_faggregate>(faggr);
        }
    private:
        column_and_aggregate & pair;
    };
    void apply_col_and_aggr_visitor( column_and_aggregate & col_and_aggr_pair, ::boost::variant< column_name_t, unsigned > const & val )
    {
        aggr_pair_visitor pair_visitor( col_and_aggr_pair );
        ::boost::apply_visitor( pair_visitor, val ); // N.B.!!! Runtime execution of operator()!
    }
    spirit::qi::rule< iterator, column_and_aggregate(), ascii::space_type > agg_pair =
        quoted_string[::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )]
        > ':'
        > spirit::int_[::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )];
    spirit::qi::rule< iterator, column_and_aggregate_container(), ascii::space_type > aggregates_parser =
          '{'
        > agg_pair[phoenix::push_back(spirit::qi::_val, spirit::qi::_1)] % ',' // N.B.!!! list-redux technic
        > '}';
alexander.sivak
  • 4,352
  • 3
  • 18
  • 27
  • I can think of a dozen other ways to do this. Which one to choose depends on where you'd use this. Can you ("please") give a relevant part of your parser, or preferrably, a reduced minimal example 'grammar' showing just this? Without further information, I'd say you will want to use `qi::attr` for default values and `qi::symbols` for aggregate types. Another hint: don't use semantic actions unless you _need_ to implement _state_ and/or _logic_ (not for storing the result). I'll await further input before attempting to demonstrate any particular technique. – sehe Jun 27 '13 at 15:43

1 Answers1

2

Okay, on second glance I think you just missed the ability to fusion adapt std::pair:

#include <boost/fusion/adapted/std_pair.hpp>
// Or:
#include <boost/fusion/adapted.hpp>

Using this, the whole complexity vanishes and there is no need for anything involving the variant. Let's assume the following types:

typedef std::string column_name_t;

enum sql_faggregate
{
    SUM,
    // ....
    AVG,
};

typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;

A simple grammar would be:

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
    parser() : parser::base_type(aggregates_parser)
    {
        using namespace qi;
        // using phx::bind; using phx::ref; using phx::val;

        quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
        faggr = int_;
        agg_pair = quoted_string > ':' > faggr;
        aggregates_parser = '{' > agg_pair % ',' > '}';
    }

  private:
    qi::rule<It, std::string(), qi::space_type>           quoted_string;
    qi::rule<It, sql_faggregate(), qi::space_type>        faggr;
    qi::rule<It, column_and_aggregate(), qi::space_type>   agg_pair;
    qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};

You could add the input validation too:

faggr %= int_ [ qi::_pass = (qi::_1 >=SUM and qi::_1<=AVG) ];

Note the %= to ensure attribute propagation.

Full Working Demonstration

Program code:

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
// #include <boost/spirit/include/phoenix.hpp>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

typedef std::string column_name_t;

enum sql_faggregate
{
    SUM,
    // ....
    AVG,
};

typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
    parser() : parser::base_type(aggregates_parser)
    {
        using namespace qi;
        // using phx::bind; using phx::ref; using phx::val;

        quoted_string = lexeme [ "'" >> *~qi::char_("'") >> "'" ];
        faggr = int_;
        agg_pair = quoted_string > ':' > faggr;
        aggregates_parser = '{' > agg_pair % ',' > '}';

        BOOST_SPIRIT_DEBUG_NODE(aggregates_parser);
    }

  private:
    qi::rule<It, std::string(), qi::space_type>           quoted_string;
    qi::rule<It, sql_faggregate(), qi::space_type>        faggr;
    qi::rule<It, column_and_aggregate(), qi::space_type>           agg_pair;
    qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};

bool doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    auto f(begin(input)), l(end(input));

    parser<It, qi::space_type> p;
    column_and_aggregate_container data;

    try
    {
        bool ok = qi::phrase_parse(f,l,p,qi::space,data);
        if (ok)   
        {
            std::cout << "parse success\n";
            for (auto& pair : data)
                std::cout << "result: '" << pair.first << "' : " << (int) pair.second << "\n";
        }
        else      std::cerr << "parse failed: '" << std::string(f,l) << "'\n";

        if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
        return ok;
    } catch(const qi::expectation_failure<It>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    bool ok = doParse("{ 'column 1' : 1, 'column 2' : 0, 'column 3' : 1 }");
    return ok? 0 : 255;
}

Print output:

parse success
result: 'column 1' : 1
result: 'column 2' : 0
result: 'column 3' : 1

PS: If you wanted to do the same in semantic actions, you'd probably want to write it like:

agg_pair = 
      quoted_string [ phx::bind(&column_and_aggregate::first, _val)  = _1 ]
    > ':'
    > faggr         [ phx::bind(&column_and_aggregate::second, _val) = _1 ];

You'll see that you can just drop it in the above sample and it works exactly the same. For this particular grammar, it's just more verbose, so I don't recommend it :)

sehe
  • 374,641
  • 47
  • 450
  • 633
  • PS. added (showing the simpler Phoenix approach assigning to the pair members _directly_ from inside the _semantic action_) – sehe Jun 27 '13 at 16:43
  • If there're possible UNICODE strings in subparsers in any language, how would you handle the situation? p.s. The example provided in the first post does not compile, because _val and _1 have its own types. – alexander.sivak Jun 27 '13 at 19:34
  • @user2496553 I don't understand. "in any language"? Huh. "the first post"? Huh. Of course the code in my answer does compile, I wouldn't have posted it otherwise. Perhaps you can clarify what the question is, or [search for information on Spirit & Unicode](http://stackoverflow.com/questions/9852558/how-to-use-boost-spirit-to-parse-chineseunicode-utf-16) (or [here](http://stackoverflow.com/search?q=%28%5Bboost-spirit%5D+OR+%5Bboost-spirit-qi%5D%29+AND+%28%5Bunicode%5D+OR+utf8%29)) – sehe Jun 27 '13 at 20:26
  • Any language(Italian, English, Chinese etc.), the first post is my post. – alexander.sivak Jun 27 '13 at 21:05
  • @user2496553 Okay :) I think the links should get you started. I also have my own demo JSON parser up on github that supports full UTF-8 as per the JSON specifications: https://github.com/sehe/spirit-v2-json (see e.g. [testcase 1](https://github.com/sehe/spirit-v2-json/blob/master/testcases/test1.json)) – sehe Jun 27 '13 at 21:08
  • if you made it support UTF-8, then you could give me advices. – alexander.sivak Jun 27 '13 at 22:10
  • Do you have the same JSON parser, but compiling on VS2012? – alexander.sivak Jun 29 '13 at 17:18
  • @user2496553 Just a heads up: I just pushed a version of JSON.hpp/JSON.cpp that doesn't use `wstring`/`wchar_t` and uses utf8 internally instead. It might work better in MSVC (because it no longer assumes that `wchar_t` be 32 bits). See it here **[https://github.com/sehe/spirit-v2-json/tree/nowide](https://github.com/sehe/spirit-v2-json/tree/nowide)** – sehe Jun 30 '13 at 22:31