Вопрос по c++ – Попытка разобрать SQL-подобный оператор с Boost-Spirit

2

Error: User Rate Limit Exceeded

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct db_select {
    void exec() {}

    std::string filed;
    std::string table;
    std::string condition;
};

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "filed: " << se.filed << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), ascii::space_type> {
    selecter() : selecter::base_type(se) {
            se %= "select" >> +qi::char_ << "from" << +qi::char_ << "where" << +qi::char_;
    } 

    qi::rule<Iterator, db_select (), ascii::space_type> se;
};

int main(int argc, char* argv[]) {
    if (argc < 2)
            return -1;

    std::string str(argv[1]);
    const char* first = str.c_str();
    const char* last = &str[str.size()];
    selecter<const char*> se;
    db_select rst;

    bool r = qi::phrase_parse(first, last, se, ascii::space, rst);
    if (!r || first != last) {
            std::cout << "parse failed, at: " << std::string(first, last) << std::endl;
            return -1;
    } else
            std::cout << "success, " << rst << std::endl;

    return 0;
}
Error: User Rate Limit Exceeded 大宝剑
Error: User Rate Limit Exceeded sehe
Error: User Rate Limit Exceeded 大宝剑

Ваш Ответ

1   ответ
5

Edit Finally behind a computer, revised answer:

Error: User Rate Limit Exceeded

1. Syntax errors

Error: User Rate Limit Exceeded<<Error: User Rate Limit Exceeded>>Error: User Rate Limit Exceeded*******Error: User Rate Limit Exceeded

/.../qi/nonterminal/rule.hpp|176 col 13| error: no matching function for call to ‘assertion_failed(mpl_::failed************ 

Error: User Rate Limit Exceeded

// Report invalid expression error as early as possible.
// If you got an error_invalid_expression error message here,
// then the expression (expr) is not a valid spirit qi expression.
BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);

Error: User Rate Limit Exceeded

2. Adapting the struct

Error: User Rate Limit Exceededfusion compatibleError: User Rate Limit Exceeded

 BOOST_FUSION_ADAPT_STRUCT(db_select,
    (std::string,field)(std::string,table)(std::string,condition));

Error: User Rate Limit ExceededcompilesError: User Rate Limit Exceeded

3. Lexemes

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

   lexeme [  (!lit("where")  >> +qi::graph) % +qi::space ]
  • lexeme inhibits the skipper for the enclosed expression
  • ! Asserts that the specified keyword must not match

Error: User Rate Limit Exceededqi::no_caseError: User Rate Limit Exceeded

FULL CODE

#include <iostream>
#include <string>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

struct db_select {
    void exec() {}

    std::string field;
    std::string table;
    std::string condition;
};

BOOST_FUSION_ADAPT_STRUCT(db_select,(std::string,field)(std::string,table)(std::string,condition));

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "field: " << se.field << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), qi::space_type> {
    selecter() : selecter::base_type(se) {
        using namespace qi;
        se %= "select"
            >> lexeme [ (!lit("from")  >> +graph) % +space ] >> "from"
            >> lexeme [ (!lit("where") >> +graph) % +space ] >> "where" 
            >> +qi::char_;
    } 

    qi::rule<Iterator, db_select (), qi::space_type> se;
};

int main(int argc, char* argv[]) {
    if (argc < 2)
            return -1;

    std::string str(argv[1]);
    const char* first = str.c_str();
    const char* last = &str[str.size()];
    selecter<const char*> se;
    db_select rst;

    bool r = qi::phrase_parse(first, last, se, qi::space, rst);
    if (!r || first != last) {
            std::cout << "parse failed, at: " << std::string(first, last) << std::endl;
            return -1;
    } else
            std::cout << "success, " << rst << std::endl;

    return 0;
}

Test:

g++ test.cpp -o test
./test "select aap, noot, mies from table where field = 'value'"

Error: User Rate Limit Exceeded

success, field: aap,noot,mies table: table condition: field='value'
Error: User Rate Limit Exceeded

Похожие вопросы