1 | /*============================================================================= |
2 | Copyright (c) 2001-2010 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_plus.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 | |
19 | #include <iostream> |
20 | #include <string> |
21 | #include <vector> |
22 | #include "test.hpp" |
23 | |
24 | struct x_attr |
25 | { |
26 | }; |
27 | |
28 | namespace boost { namespace spirit { namespace traits |
29 | { |
30 | template <> |
31 | struct container_value<x_attr> |
32 | { |
33 | typedef char type; // value type of container |
34 | }; |
35 | |
36 | template <> |
37 | struct push_back_container<x_attr, char> |
38 | { |
39 | static bool call(x_attr& /*c*/, char /*val*/) |
40 | { |
41 | // push back value type into container |
42 | return true; |
43 | } |
44 | }; |
45 | }}} |
46 | |
47 | int |
48 | main() |
49 | { |
50 | using spirit_test::test; |
51 | using spirit_test::test_attr; |
52 | using namespace boost::spirit::ascii; |
53 | using boost::spirit::qi::int_; |
54 | using boost::spirit::qi::omit; |
55 | using boost::spirit::qi::lit; |
56 | using boost::spirit::qi::_1; |
57 | using boost::spirit::qi::lexeme; |
58 | |
59 | { |
60 | BOOST_TEST(test("aaaaaaaa" , +char_)); |
61 | BOOST_TEST(test("a" , +char_)); |
62 | BOOST_TEST(!test("" , +char_)); |
63 | BOOST_TEST(test("aaaaaaaa" , +alpha)); |
64 | BOOST_TEST(!test("aaaaaaaa" , +upper)); |
65 | } |
66 | |
67 | { |
68 | BOOST_TEST(test(" a a aaa aa" , +char_, space)); |
69 | BOOST_TEST(test("12345 678 9 " , +digit, space)); |
70 | } |
71 | |
72 | { |
73 | BOOST_TEST(test("aBcdeFGH" , no_case[+char_])); |
74 | BOOST_TEST(test("a B cde FGH " , no_case[+char_], space)); |
75 | } |
76 | |
77 | { |
78 | std::vector<int> v; |
79 | BOOST_TEST(test_attr("123 456 789 10" , +int_, v, space) && 4 == v.size() && |
80 | v[0] == 123 && v[1] == 456 && v[2] == 789 && v[3] == 10); |
81 | } |
82 | |
83 | { |
84 | std::vector<std::string> v; |
85 | BOOST_TEST(test_attr("a b c d" , +lexeme[+alpha], v, space) && 4 == v.size() && |
86 | v[0] == "a" && v[1] == "b" && v[2] == "c" && v[3] == "d" ); |
87 | } |
88 | |
89 | { |
90 | BOOST_TEST(test("Kim Kim Kim" , +lit("Kim" ), space)); |
91 | } |
92 | |
93 | { |
94 | // The following 2 tests show that omit does not inhibit explicit attributes |
95 | |
96 | std::string s; |
97 | BOOST_TEST(test_attr("bbbb" , omit[+char_('b')], s) && s == "bbbb" ); |
98 | |
99 | s.clear(); |
100 | BOOST_TEST(test_attr("b b b b " , omit[+char_('b')], s, space) && s == "bbbb" ); |
101 | } |
102 | |
103 | { // actions |
104 | namespace phx = boost::phoenix; |
105 | |
106 | std::vector<char> v; |
107 | BOOST_TEST(test("bbbb" , (+char_)[phx::ref(v) = _1]) && 4 == v.size() && |
108 | v[0] == 'b' && v[1] == 'b' && v[2] == 'b' && v[3] == 'b'); |
109 | } |
110 | |
111 | { // more actions |
112 | namespace phx = boost::phoenix; |
113 | |
114 | std::vector<int> v; |
115 | BOOST_TEST(test("1 2 3" , (+int_)[phx::ref(v) = _1], space) && 3 == v.size() && |
116 | v[0] == 1 && v[1] == 2 && v[2] == 3); |
117 | } |
118 | |
119 | { // attribute customization |
120 | |
121 | x_attr x; |
122 | test_attr(in: "abcde" , p: +char_, attr&: x); |
123 | } |
124 | |
125 | return boost::report_errors(); |
126 | } |
127 | |
128 | |