2

以下のルールを使用するパーサーが空のコンテナーを返すのはなぜですか? 3つのルールがあります。1 つは二重引用符を除く文字列を解析するためのもので、2 番目はペア (例: "col1" : 2) を解析し、3 番目はそのようなペアのベクトルを解析します。MSVS2012 での以下のプログラムの出力は次のとおりです。

parse success
result: '' : 0
result: '' : 0
result: '' : 0

.

namespace parsers
{

            spirit::qi::rule< iterator, column_name_t() > quoted_string = 
                    spirit::qi::lexeme["\"" >> +~spirit::qi::char_("\"") >> "\""];
                spirit::qi::rule< iterator, column_and_aggregate(), spirit::qi::space_type > agg_pair =
                    quoted_string//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )]
                    > ':'
                    // A rule validation technic is used below.
                    > spirit::int_[spirit::qi::_pass = (spirit::qi::_1 >=AVG && spirit::qi::_1<=SUM)];//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )];
                spirit::qi::rule< iterator, column_and_aggregate_container(), spirit::qi::space_type > aggregates_parser =
                      '{'
                    > agg_pair/*[phoenix::push_back(spirit::qi::_val, spirit::qi::_1)]*/ % ',' // N.B.!!! list-redux technic
                    > '}';
}
using namespace parsers;
using namespace boost::spirit;
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;
    typedef BOOST_TYPEOF(qi::space) skipper_type; 
    try
    {
        bool ok = qi::phrase_parse(f,l,aggregates_parser,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 }");
    doParse("{ \"column 1\" : 1, \"column 2\" : 0, \"column 3\" : 1 }");
    //return ok? 0 : 255;
}


template <typename it, typename skipper = qi::space_type>
struct quoted_string_parser
{
    quoted_string_parser()
    {
        using namespace qi;
        quoted_string %= lexeme['"' >> *~char_('"') >> '"'];
        BOOST_SPIRIT_DEBUG_NODE(quoted_string);
    }
    qi::rule<it, std::string(), skipper> quoted_string;
};
template <typename it, typename skipper = qi::space_type>
struct aggregates_parser : qi::grammar<it, column_and_aggregate_container(), skipper>
{
    aggregates_parser() : aggregates_parser::base_type(aggregates_parser_)
    {
        using namespace qi;
        agg_pair %= quoted_string_parser<it,skipper> > ':' > int_[_pass = (qi::_1 >= AVG && qi::_1 <= SUM)];
        aggregates_parser_ = '{' > agg_pair % ',' > '}';
        BOOST_SPIRIT_DEBUG_NODE(aggregates_parser_);
    }
  private:    
    qi::rule<it, sql_faggregate(), skipper> faggr;
    qi::rule<it, column_and_aggregate(), skipper> agg_pair;
    qi::rule<it, column_and_aggregate_container(), skipper> aggregates_parser_;
};
4

1 に答える 1