1 | /*============================================================================= |
2 | Copyright (c) 2001-2011 Joel de Guzman |
3 | |
4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | =============================================================================*/ |
7 | #include <boost/spirit/include/qi_list.hpp> |
8 | |
9 | #include <boost/spirit/include/qi_operator.hpp> |
10 | #include <boost/spirit/include/qi_char.hpp> |
11 | #include <boost/spirit/include/qi_string.hpp> |
12 | #include <boost/spirit/include/qi_numeric.hpp> |
13 | #include <boost/spirit/include/qi_directive.hpp> |
14 | #include <boost/spirit/include/qi_action.hpp> |
15 | #include <boost/spirit/include/support_argument.hpp> |
16 | #include <boost/phoenix/core.hpp> |
17 | #include <boost/phoenix/operator.hpp> |
18 | #include <boost/phoenix/object.hpp> |
19 | #include <boost/phoenix/stl/container.hpp> |
20 | #include <boost/fusion/include/std_pair.hpp> |
21 | |
22 | #include <vector> |
23 | #include <set> |
24 | #include <map> |
25 | #include <string> |
26 | #include <iostream> |
27 | #include "test.hpp" |
28 | |
29 | using namespace spirit_test; |
30 | |
31 | int |
32 | main() |
33 | { |
34 | using namespace boost::spirit::ascii; |
35 | |
36 | { |
37 | BOOST_TEST(test("a,b,c,d,e,f,g,h" , char_ % ',')); |
38 | BOOST_TEST(test("a,b,c,d,e,f,g,h," , char_ % ',', false)); |
39 | } |
40 | |
41 | { |
42 | BOOST_TEST(test("a, b, c, d, e, f, g, h" , char_ % ',', space)); |
43 | BOOST_TEST(test("a, b, c, d, e, f, g, h," , char_ % ',', space, false)); |
44 | } |
45 | |
46 | { |
47 | std::string s; |
48 | BOOST_TEST(test_attr("a,b,c,d,e,f,g,h" , char_ % ',', s)); |
49 | BOOST_TEST(s == "abcdefgh" ); |
50 | |
51 | BOOST_TEST(!test("a,b,c,d,e,f,g,h," , char_ % ',')); |
52 | } |
53 | |
54 | { |
55 | std::string s; |
56 | BOOST_TEST(test_attr("ab,cd,ef,gh" , (char_ >> char_) % ',', s)); |
57 | BOOST_TEST(s == "abcdefgh" ); |
58 | |
59 | BOOST_TEST(!test("ab,cd,ef,gh," , (char_ >> char_) % ',')); |
60 | BOOST_TEST(!test("ab,cd,ef,g" , (char_ >> char_) % ',')); |
61 | |
62 | s.clear(); |
63 | BOOST_TEST(test_attr("ab,cd,efg" , (char_ >> char_) % ',' >> char_, s)); |
64 | BOOST_TEST(s == "abcdefg" ); |
65 | } |
66 | |
67 | { |
68 | using boost::spirit::int_; |
69 | |
70 | std::vector<int> v; |
71 | BOOST_TEST(test_attr("1,2" , int_ % ',', v)); |
72 | BOOST_TEST(2 == v.size() && 1 == v[0] && 2 == v[1]); |
73 | } |
74 | |
75 | { |
76 | using boost::spirit::int_; |
77 | |
78 | std::vector<int> v; |
79 | BOOST_TEST(test_attr("(1,2)" , '(' >> int_ % ',' >> ')', v)); |
80 | BOOST_TEST(2 == v.size() && 1 == v[0] && 2 == v[1]); |
81 | } |
82 | |
83 | { |
84 | std::vector<std::string> v; |
85 | BOOST_TEST(test_attr("a,b,c,d" , +alpha % ',', v)); |
86 | BOOST_TEST(4 == v.size() && "a" == v[0] && "b" == v[1] |
87 | && "c" == v[2] && "d" == v[3]); |
88 | } |
89 | |
90 | { |
91 | std::vector<boost::optional<char> > v; |
92 | BOOST_TEST(test_attr("#a,#" , ('#' >> -alpha) % ',', v)); |
93 | BOOST_TEST(2 == v.size() && |
94 | !!v[0] && 'a' == boost::get<char>(v[0]) && !v[1]); |
95 | |
96 | std::vector<char> v2; |
97 | BOOST_TEST(test_attr("#a,#" , ('#' >> -alpha) % ',', v2)); |
98 | BOOST_TEST(1 == v2.size() && 'a' == v2[0]); |
99 | } |
100 | |
101 | { |
102 | typedef std::set<std::pair<std::string, std::string> > set_type; |
103 | set_type s; |
104 | BOOST_TEST(test_attr("k1=v1&k2=v2" , |
105 | (*(char_ - '=') >> '=' >> *(char_ - '&')) % '&', s)); |
106 | |
107 | set_type::const_iterator it = s.begin(); |
108 | BOOST_TEST(s.size() == 2); |
109 | BOOST_TEST(it != s.end() && (*it).first == "k1" && (*it).second == "v1" ); |
110 | BOOST_TEST(++it != s.end() && (*it).first == "k2" && (*it).second == "v2" ); |
111 | } |
112 | |
113 | { |
114 | typedef std::map<std::string, std::string> map_type; |
115 | map_type m; |
116 | BOOST_TEST(test_attr("k1=v1&k2=v2" , |
117 | (*(char_ - '=') >> '=' >> *(char_ - '&')) % '&', m)); |
118 | |
119 | map_type::const_iterator it = m.begin(); |
120 | BOOST_TEST(m.size() == 2); |
121 | BOOST_TEST(it != m.end() && (*it).first == "k1" && (*it).second == "v1" ); |
122 | BOOST_TEST(++it != m.end() && (*it).first == "k2" && (*it).second == "v2" ); |
123 | } |
124 | |
125 | { // actions |
126 | namespace phx = boost::phoenix; |
127 | using boost::phoenix::begin; |
128 | using boost::phoenix::end; |
129 | using boost::phoenix::construct; |
130 | using boost::spirit::qi::_1; |
131 | |
132 | std::string s; |
133 | BOOST_TEST(test("a,b,c,d,e,f,g,h" , (char_ % ',') |
134 | [phx::ref(s) = construct<std::string>(begin(_1), end(_1))])); |
135 | } |
136 | |
137 | return boost::report_errors(); |
138 | } |
139 | |
140 | |