]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
2 | // | |
3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
5 | ||
6 | #include <boost/config/warning_disable.hpp> | |
7 | #include <boost/detail/lightweight_test.hpp> | |
8 | ||
9 | #include <boost/spirit/include/karma_operator.hpp> | |
10 | #include <boost/spirit/include/karma_char.hpp> | |
11 | #include <boost/spirit/include/karma_auxiliary.hpp> | |
12 | #include <boost/spirit/include/karma_string.hpp> | |
13 | #include <boost/spirit/include/karma_numeric.hpp> | |
14 | #include <boost/spirit/include/karma_nonterminal.hpp> | |
15 | #include <boost/spirit/include/karma_action.hpp> | |
16 | #include <boost/spirit/include/phoenix_core.hpp> | |
17 | #include <boost/spirit/include/phoenix_operator.hpp> | |
7c673cae FG |
18 | |
19 | #include "test.hpp" | |
20 | ||
21 | using namespace spirit_test; | |
22 | ||
23 | /////////////////////////////////////////////////////////////////////////////// | |
24 | int main() | |
25 | { | |
26 | using namespace boost; | |
27 | using namespace boost::spirit; | |
28 | using namespace boost::spirit::ascii; | |
29 | ||
30 | typedef spirit_test::output_iterator<char>::type outiter_type; | |
31 | ||
32 | // locals test | |
33 | { | |
34 | karma::rule<outiter_type, locals<std::string> > start; | |
35 | ||
20effc67 | 36 | start = string[(_1 = "abc", _a = _1)] << int_[_1 = 10] << string[_1 = _a]; |
7c673cae FG |
37 | BOOST_TEST(test("abc10abc", start)); |
38 | } | |
39 | ||
40 | { | |
41 | karma::rule<outiter_type, space_type, locals<std::string> > start; | |
42 | ||
20effc67 | 43 | start = string[(_1 = "abc", _a = _1)] << int_[_1 = 10] << string[_1 = _a]; |
7c673cae FG |
44 | BOOST_TEST(test_delimited("abc 10 abc ", start, space)); |
45 | } | |
46 | ||
47 | // alias tests | |
48 | { | |
49 | typedef variant<char, int, double> var_type; | |
50 | ||
51 | karma::rule<outiter_type, var_type()> d, start; | |
52 | ||
53 | d = start.alias(); // d will always track start | |
54 | ||
55 | start = (char_ | int_ | double_)[_1 = _val]; | |
56 | ||
57 | var_type v ('a'); | |
58 | BOOST_TEST(test("a", d, v)); | |
59 | v = 10; | |
60 | BOOST_TEST(test("10", d, v)); | |
61 | v = 12.4; | |
62 | BOOST_TEST(test("12.4", d, v)); | |
63 | } | |
64 | ||
65 | { | |
66 | typedef variant<char, int, double> var_type; | |
67 | ||
68 | karma::rule<outiter_type, space_type, var_type()> d, start; | |
69 | ||
70 | d = start.alias(); // d will always track start | |
71 | ||
72 | start = (char_ | int_ | double_)[_1 = _val]; | |
73 | ||
74 | var_type v ('a'); | |
75 | BOOST_TEST(test_delimited("a ", d, v, space)); | |
76 | v = 10; | |
77 | BOOST_TEST(test_delimited("10 ", d, v, space)); | |
78 | v = 12.4; | |
79 | BOOST_TEST(test_delimited("12.4 ", d, v, space)); | |
80 | } | |
81 | ||
82 | { | |
83 | typedef variant<char, int, double> var_type; | |
84 | ||
85 | karma::rule<outiter_type, var_type()> d, start; | |
86 | ||
87 | d = start.alias(); // d will always track start | |
88 | ||
89 | start %= char_ | int_ | double_; | |
90 | ||
91 | var_type v ('a'); | |
92 | BOOST_TEST(test("a", d, v)); | |
93 | v = 10; | |
94 | BOOST_TEST(test("10", d, v)); | |
95 | v = 12.4; | |
96 | BOOST_TEST(test("12.4", d, v)); | |
97 | ||
98 | start = char_ | int_ | double_; | |
99 | ||
100 | v = 'a'; | |
101 | BOOST_TEST(test("a", d, v)); | |
102 | v = 10; | |
103 | BOOST_TEST(test("10", d, v)); | |
104 | v = 12.4; | |
105 | BOOST_TEST(test("12.4", d, v)); | |
106 | } | |
107 | ||
108 | { | |
109 | typedef variant<char, int, double> var_type; | |
110 | ||
111 | karma::rule<outiter_type, space_type, var_type()> d, start; | |
112 | ||
113 | d = start.alias(); // d will always track start | |
114 | ||
115 | start %= char_ | int_ | double_; | |
116 | ||
117 | var_type v ('a'); | |
118 | BOOST_TEST(test_delimited("a ", d, v, space)); | |
119 | v = 10; | |
120 | BOOST_TEST(test_delimited("10 ", d, v, space)); | |
121 | v = 12.4; | |
122 | BOOST_TEST(test_delimited("12.4 ", d, v, space)); | |
123 | ||
124 | start = char_ | int_ | double_; | |
125 | ||
126 | v = 'a'; | |
127 | BOOST_TEST(test_delimited("a ", d, v, space)); | |
128 | v = 10; | |
129 | BOOST_TEST(test_delimited("10 ", d, v, space)); | |
130 | v = 12.4; | |
131 | BOOST_TEST(test_delimited("12.4 ", d, v, space)); | |
132 | } | |
133 | ||
134 | /////////////////////////////////////////////////////////////////////////// | |
135 | { | |
136 | using boost::spirit::karma::int_; | |
137 | using boost::spirit::karma::_1; | |
138 | using boost::spirit::karma::_val; | |
139 | using boost::spirit::karma::space; | |
140 | using boost::spirit::karma::space_type; | |
141 | ||
142 | karma::rule<outiter_type, int()> r1 = int_; | |
143 | karma::rule<outiter_type, space_type, int()> r2 = int_; | |
144 | ||
145 | int i = 123; | |
146 | int j = 456; | |
147 | BOOST_TEST(test("123", r1[_1 = _val], i)); | |
148 | BOOST_TEST(test_delimited("456 ", r2[_1 = _val], j, space)); | |
149 | } | |
150 | ||
151 | return boost::report_errors(); | |
152 | } | |
153 |