-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboost_spirite_11_CDISM_STUDY.cpp
66 lines (58 loc) · 1.76 KB
/
boost_spirite_11_CDISM_STUDY.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <utility>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>
#define QUERYSORTING (1)
namespace client
{
namespace qi = boost::spirit::qi;
#if QUERYSORTING == 0
using pairs_type = std::map<std::string, std::string>;
#else
using pairs_type = std::vector<std::pair<std::string, std::string>>;
#endif
template< typename Iterator >
struct query_parser : qi::grammar<Iterator, pairs_type()> {
query_parser() : query_parser::base_type(query) {
key = qi::char_("a-zA-Z_") >> *qi::char_("a-zA-Z_0-9");
value = +qi::char_("a-zA-Z_0-9");
pair = key >> -('=' >> value);
query = pair >> *((qi::lit(';') | '&') >> pair);
}
private:
qi::rule<Iterator, std::string()> key;
qi::rule<Iterator, std::string()> value;
qi::rule<Iterator, std::pair<std::string, std::string>()> pair;
qi::rule<Iterator, pairs_type()> query;
};
template<typename Iterator>
bool parse_process(Iterator first ,Iterator last, pairs_type & value) {
if (first == last) return false;
client::query_parser<Iterator> q_parser;
parse(first, last, q_parser, value); // parse 스페이스 없이 연이어 파싱 할 사용 , 파싱 된 결과는 value 저장된다.
if (first != last) return false;
};
}
int main() {
std::string cmd;
while (getline(std::cin, cmd))
{
if (cmd.empty() || cmd[0] == 'q' || cmd[0] == 'Q') break;
client::pairs_type value;
if (client::parse_process(std::begin(cmd), std::end(cmd), value))
{
for (const auto & mitem : value) {
std::cout << '[' << mitem.first << ":" << mitem.second << "]" << std::endl;
}
std::cout << std::endl << "Parsing OK" << std::endl;
}
else
{
std::cout << std::endl << "Parsing FAIL" << std::endl;
}
}
return 0;
}